1 //===-- SparcV9InstrInfo.cpp - SparcV9 Instr. Selection Support Methods ---===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains various methods of the class SparcV9InstrInfo, many of
11 // which appear to build canned sequences of MachineInstrs, and are
12 // used in instruction selection.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/iTerminators.h"
20 #include "llvm/CodeGen/InstrSelection.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionInfo.h"
24 #include "llvm/CodeGen/MachineCodeForInstruction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "SparcV9Internals.h"
27 #include "SparcV9InstrSelectionSupport.h"
28 #include "SparcV9InstrInfo.h"
32 static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
33 static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
35 //---------------------------------------------------------------------------
36 // Function ConvertConstantToIntType
38 // Function to get the value of an integral constant in the form
39 // that must be put into the machine register. The specified constant is
40 // interpreted as (i.e., converted if necessary to) the specified destination
41 // type. The result is always returned as an uint64_t, since the representation
42 // of int64_t and uint64_t are identical. The argument can be any known const.
44 // isValidConstant is set to true if a valid constant was found.
45 //---------------------------------------------------------------------------
48 SparcV9InstrInfo::ConvertConstantToIntType(const TargetMachine &target,
51 bool &isValidConstant) const
53 isValidConstant = false;
56 if (! destType->isIntegral() && ! isa<PointerType>(destType))
59 if (! isa<Constant>(V))
62 // ConstantPointerRef: no conversions needed: get value and return it
63 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
64 // A ConstantPointerRef is just a reference to GlobalValue.
65 isValidConstant = true; // may be overwritten by recursive call
66 return (CPR->isNullValue()? 0
67 : ConvertConstantToIntType(target, CPR->getValue(), destType,
71 // ConstantBool: no conversions needed: get value and return it
72 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
73 isValidConstant = true;
74 return (uint64_t) CB->getValue();
77 // ConstantPointerNull: it's really just a big, shiny version of zero.
78 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(V)) {
79 isValidConstant = true;
83 // For other types of constants, some conversion may be needed.
84 // First, extract the constant operand according to its own type
85 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
86 switch(CE->getOpcode()) {
87 case Instruction::Cast: // recursively get the value as cast
88 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
91 default: // not simplifying other ConstantExprs
94 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
95 isValidConstant = true;
96 C = CI->getRawValue();
98 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
99 isValidConstant = true;
100 double fC = CFP->getValue();
101 C = (destType->isSigned()? (uint64_t) (int64_t) fC
105 // Now if a valid value was found, convert it to destType.
106 if (isValidConstant) {
107 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
108 unsigned destSize = target.getTargetData().getTypeSize(destType);
109 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
110 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
112 if (destType->isSigned()) {
113 if (opSize > destSize) // operand is larger than dest:
114 C = C & maskHi; // mask high bits
116 if (opSize > destSize ||
117 (opSize == destSize && ! V->getType()->isSigned()))
118 if (C & (1U << (8*destSize - 1)))
119 C = C | ~maskHi; // sign-extend from destSize to 64 bits
122 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
123 // operand is larger than dest,
124 // OR both are equal but smaller than the full register size
125 // AND operand is signed, so it may have extra sign bits:
136 //----------------------------------------------------------------------------
137 // Function: CreateSETUWConst
139 // Set a 32-bit unsigned constant in the register `dest', using
140 // SETHI, OR in the worst case. This function correctly emulates
141 // the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
143 // The isSigned=true case is used to implement SETSW without duplicating code.
145 // Optimize some common cases:
146 // (1) Small value that fits in simm13 field of OR: don't need SETHI.
147 // (2) isSigned = true and C is a small negative signed value, i.e.,
148 // high bits are 1, and the remaining bits fit in simm13(OR).
149 //----------------------------------------------------------------------------
152 CreateSETUWConst(const TargetMachine& target, uint32_t C,
153 Instruction* dest, std::vector<MachineInstr*>& mvec,
154 bool isSigned = false)
156 MachineInstr *miSETHI = NULL, *miOR = NULL;
158 // In order to get efficient code, we should not generate the SETHI if
159 // all high bits are 1 (i.e., this is a small signed value that fits in
160 // the simm13 field of OR). So we check for and handle that case specially.
161 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
162 // In fact, sC == -sC, so we have to check for this explicitly.
163 int32_t sC = (int32_t) C;
164 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
166 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
167 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
168 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
169 miSETHI->setOperandHi32(0);
170 mvec.push_back(miSETHI);
173 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
174 // was generated, or if the low 10 bits are non-zero.
175 if (miSETHI==NULL || C & MAXLO) {
177 // unsigned value with high-order bits set using SETHI
178 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
179 miOR->setOperandLo32(1);
181 // unsigned or small signed value that fits in simm13 field of OR
182 assert(smallNegValue || (C & ~MAXSIMM) == 0);
183 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()->getZeroRegNum())
184 .addSImm(sC).addRegDef(dest);
186 mvec.push_back(miOR);
189 assert((miSETHI || miOR) && "Oops, no code was generated!");
193 //----------------------------------------------------------------------------
194 // Function: CreateSETSWConst
196 // Set a 32-bit signed constant in the register `dest', with sign-extension
197 // to 64 bits. This uses SETHI, OR, SRA in the worst case.
198 // This function correctly emulates the SETSW pseudo-op for SPARC v9.
200 // Optimize the same cases as SETUWConst, plus:
201 // (1) SRA is not needed for positive or small negative values.
202 //----------------------------------------------------------------------------
205 CreateSETSWConst(const TargetMachine& target, int32_t C,
206 Instruction* dest, std::vector<MachineInstr*>& mvec)
208 // Set the low 32 bits of dest
209 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
211 // Sign-extend to the high 32 bits if needed.
212 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
213 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
214 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
218 //----------------------------------------------------------------------------
219 // Function: CreateSETXConst
221 // Set a 64-bit signed or unsigned constant in the register `dest'.
222 // Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
223 // This function correctly emulates the SETX pseudo-op for SPARC v9.
225 // Optimize the same cases as SETUWConst for each 32 bit word.
226 //----------------------------------------------------------------------------
229 CreateSETXConst(const TargetMachine& target, uint64_t C,
230 Instruction* tmpReg, Instruction* dest,
231 std::vector<MachineInstr*>& mvec)
233 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
237 // Code to set the upper 32 bits of the value in register `tmpReg'
238 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
240 // Shift tmpReg left by 32 bits
241 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
244 // Code to set the low 32 bits of the value in register `dest'
245 CreateSETUWConst(target, C, dest, mvec);
247 // dest = OR(tmpReg, dest)
248 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
252 //----------------------------------------------------------------------------
253 // Function: CreateSETUWLabel
255 // Set a 32-bit constant (given by a symbolic label) in the register `dest'.
256 //----------------------------------------------------------------------------
259 CreateSETUWLabel(const TargetMachine& target, Value* val,
260 Instruction* dest, std::vector<MachineInstr*>& mvec)
264 // Set the high 22 bits in dest
265 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
266 MI->setOperandHi32(0);
269 // Set the low 10 bits in dest
270 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
271 MI->setOperandLo32(1);
276 //----------------------------------------------------------------------------
277 // Function: CreateSETXLabel
279 // Set a 64-bit constant (given by a symbolic label) in the register `dest'.
280 //----------------------------------------------------------------------------
283 CreateSETXLabel(const TargetMachine& target,
284 Value* val, Instruction* tmpReg, Instruction* dest,
285 std::vector<MachineInstr*>& mvec)
287 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
288 "I only know about constant values and global addresses");
292 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
293 MI->setOperandHi64(0);
296 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
297 MI->setOperandLo64(1);
300 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
302 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
303 MI->setOperandHi32(0);
306 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
309 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
310 MI->setOperandLo32(1);
315 //----------------------------------------------------------------------------
316 // Function: CreateUIntSetInstruction
318 // Create code to Set an unsigned constant in the register `dest'.
319 // Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
320 // CreateSETSWConst is an optimization for the case that the unsigned value
321 // has all ones in the 33 high bits (so that sign-extension sets them all).
322 //----------------------------------------------------------------------------
325 CreateUIntSetInstruction(const TargetMachine& target,
326 uint64_t C, Instruction* dest,
327 std::vector<MachineInstr*>& mvec,
328 MachineCodeForInstruction& mcfi)
330 static const uint64_t lo32 = (uint32_t) ~0;
331 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
332 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
333 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
334 // All high 33 (not 32) bits are 1s: sign-extension will take care
335 // of high 32 bits, so use the sequence for signed int
336 CreateSETSWConst(target, (int32_t) C, dest, mvec);
337 } else if (C > lo32) {
338 // C does not fit in 32 bits
339 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
340 CreateSETXConst(target, C, tmpReg, dest, mvec);
345 //----------------------------------------------------------------------------
346 // Function: CreateIntSetInstruction
348 // Create code to Set a signed constant in the register `dest'.
349 // Really the same as CreateUIntSetInstruction.
350 //----------------------------------------------------------------------------
353 CreateIntSetInstruction(const TargetMachine& target,
354 int64_t C, Instruction* dest,
355 std::vector<MachineInstr*>& mvec,
356 MachineCodeForInstruction& mcfi)
358 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
362 //---------------------------------------------------------------------------
363 // Create a table of LLVM opcode -> max. immediate constant likely to
364 // be usable for that operation.
365 //---------------------------------------------------------------------------
367 // Entry == 0 ==> no immediate constant field exists at all.
368 // Entry > 0 ==> abs(immediate constant) <= Entry
370 std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
373 MaxConstantForInstr(unsigned llvmOpCode)
375 int modelOpCode = -1;
377 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
378 llvmOpCode < Instruction::BinaryOpsEnd)
379 modelOpCode = V9::ADDi;
382 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
384 case Instruction::Malloc:
385 case Instruction::Alloca:
386 case Instruction::GetElementPtr:
387 case Instruction::PHI:
388 case Instruction::Cast:
389 case Instruction::Call: modelOpCode = V9::ADDi; break;
391 case Instruction::Shl:
392 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
397 return (modelOpCode < 0)? 0: SparcV9MachineInstrDesc[modelOpCode].maxImmedConst;
401 InitializeMaxConstantsTable()
404 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
405 "assignments below will be illegal!");
406 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
407 MaxConstantsTable[op] = MaxConstantForInstr(op);
408 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
409 MaxConstantsTable[op] = MaxConstantForInstr(op);
410 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
411 MaxConstantsTable[op] = MaxConstantForInstr(op);
412 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
413 MaxConstantsTable[op] = MaxConstantForInstr(op);
417 //---------------------------------------------------------------------------
418 // class SparcV9InstrInfo
421 // Information about individual instructions.
422 // Most information is stored in the SparcV9MachineInstrDesc array above.
423 // Other information is computed on demand, and most such functions
424 // default to member functions in base class TargetInstrInfo.
425 //---------------------------------------------------------------------------
427 SparcV9InstrInfo::SparcV9InstrInfo()
428 : TargetInstrInfo(SparcV9MachineInstrDesc, V9::NUM_TOTAL_OPCODES) {
429 InitializeMaxConstantsTable();
433 SparcV9InstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
434 const Instruction* I) const
436 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
439 if (isa<ConstantPointerNull>(CV)) // can always use %g0
442 if (isa<SwitchInst>(I)) // Switch instructions will be lowered!
445 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
446 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
448 if (isa<ConstantBool>(CV))
449 return 1 > MaxConstantsTable[I->getOpcode()];
455 // Create an instruction sequence to put the constant `val' into
456 // the virtual register `dest'. `val' may be a Constant or a
457 // GlobalValue, viz., the constant address of a global variable or function.
458 // The generated instructions are returned in `mvec'.
459 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
460 // Any stack space required is allocated via MachineFunction.
463 SparcV9InstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
467 std::vector<MachineInstr*>& mvec,
468 MachineCodeForInstruction& mcfi) const
470 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
471 "I only know about constant values and global addresses");
473 // Use a "set" instruction for known constants or symbolic constants (labels)
474 // that can go in an integer reg.
475 // We have to use a "load" instruction for all other constants,
476 // in particular, floating point constants.
478 const Type* valType = val->getType();
480 // A ConstantPointerRef is just a reference to GlobalValue.
481 while (isa<ConstantPointerRef>(val))
482 val = cast<ConstantPointerRef>(val)->getValue();
484 if (isa<GlobalValue>(val)) {
485 TmpInstruction* tmpReg =
486 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
487 CreateSETXLabel(target, val, tmpReg, dest, mvec);
492 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
494 if (dest->getType()->isSigned())
495 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
497 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
500 // Make an instruction sequence to load the constant, viz:
501 // SETX <addr-of-constant>, tmpReg, addrReg
502 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
504 // First, create a tmp register to be used by the SETX sequence.
505 TmpInstruction* tmpReg =
506 new TmpInstruction(mcfi, PointerType::get(val->getType()));
508 // Create another TmpInstruction for the address register
509 TmpInstruction* addrReg =
510 new TmpInstruction(mcfi, PointerType::get(val->getType()));
512 // Get the constant pool index for this constant
513 MachineConstantPool *CP = MachineFunction::get(F).getConstantPool();
514 Constant *C = cast<Constant>(val);
515 unsigned CPI = CP->getConstantPoolIndex(C);
517 // Put the address of the constant into a register
520 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(tmpReg);
521 MI->setOperandHi64(0);
524 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addConstantPoolIndex(CPI)
526 MI->setOperandLo64(1);
529 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
531 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(addrReg);
532 MI->setOperandHi32(0);
535 MI = BuildMI(V9::ORr, 3).addReg(addrReg).addReg(tmpReg).addRegDef(addrReg);
538 MI = BuildMI(V9::ORi, 3).addReg(addrReg).addConstantPoolIndex(CPI)
540 MI->setOperandLo32(1);
543 // Now load the constant from out ConstantPool label
544 unsigned Opcode = ChooseLoadInstruction(val->getType());
545 Opcode = convertOpcodeFromRegToImm(Opcode);
546 mvec.push_back(BuildMI(Opcode, 3)
547 .addReg(addrReg).addSImm((int64_t)0).addRegDef(dest));
552 // Create an instruction sequence to copy an integer register `val'
553 // to a floating point register `dest' by copying to memory and back.
554 // val must be an integral type. dest must be a Float or Double.
555 // The generated instructions are returned in `mvec'.
556 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
557 // Any stack space required is allocated via MachineFunction.
560 SparcV9InstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
564 std::vector<MachineInstr*>& mvec,
565 MachineCodeForInstruction& mcfi) const
567 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
568 && "Source type must be integral (integer or bool) or pointer");
569 assert(dest->getType()->isFloatingPoint()
570 && "Dest type must be float/double");
572 // Get a stack slot to use for the copy
573 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
575 // Get the size of the source value being copied.
576 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
578 // Store instruction stores `val' to [%fp+offset].
579 // The store and load opCodes are based on the size of the source value.
580 // If the value is smaller than 32 bits, we must sign- or zero-extend it
581 // to 32 bits since the load-float will load 32 bits.
582 // Note that the store instruction is the same for signed and unsigned ints.
583 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
584 Value* storeVal = val;
585 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
586 // sign- or zero-extend respectively
587 storeVal = new TmpInstruction(mcfi, storeType, val);
588 if (val->getType()->isSigned())
589 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
592 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
596 unsigned FPReg = target.getRegInfo()->getFramePointer();
597 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
598 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
599 mvec.push_back(BuildMI(StoreOpcode, 3)
600 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
602 // Load instruction loads [%fp+offset] to `dest'.
603 // The type of the load opCode is the floating point type that matches the
604 // stored type in size:
605 // On SparcV9: float for int or smaller, double for long.
607 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
608 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
609 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
610 mvec.push_back(BuildMI(LoadOpcode, 3)
611 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
614 // Similarly, create an instruction sequence to copy an FP register
615 // `val' to an integer register `dest' by copying to memory and back.
616 // The generated instructions are returned in `mvec'.
617 // Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
618 // Temporary stack space required is allocated via MachineFunction.
621 SparcV9InstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
625 std::vector<MachineInstr*>& mvec,
626 MachineCodeForInstruction& mcfi) const
628 const Type* opTy = val->getType();
629 const Type* destTy = dest->getType();
631 assert(opTy->isFloatingPoint() && "Source type must be float/double");
632 assert((destTy->isIntegral() || isa<PointerType>(destTy))
633 && "Dest type must be integer, bool or pointer");
635 // FIXME: For now, we allocate permanent space because the stack frame
636 // manager does not allow locals to be allocated (e.g., for alloca) after
637 // a temp is allocated!
639 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
641 unsigned FPReg = target.getRegInfo()->getFramePointer();
643 // Store instruction stores `val' to [%fp+offset].
644 // The store opCode is based only the source value being copied.
646 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
647 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
648 mvec.push_back(BuildMI(StoreOpcode, 3)
649 .addReg(val).addMReg(FPReg).addSImm(offset));
651 // Load instruction loads [%fp+offset] to `dest'.
652 // The type of the load opCode is the integer type that matches the
653 // source type in size:
654 // On SparcV9: int for float, long for double.
655 // Note that we *must* use signed loads even for unsigned dest types, to
656 // ensure correct sign-extension for UByte, UShort or UInt:
658 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
659 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
660 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
661 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
662 .addSImm(offset).addRegDef(dest));
666 // Create instruction(s) to copy src to dest, for arbitrary types
667 // The generated instructions are returned in `mvec'.
668 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
669 // Any stack space required is allocated via MachineFunction.
672 SparcV9InstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
676 std::vector<MachineInstr*>& mvec,
677 MachineCodeForInstruction& mcfi) const
679 bool loadConstantToReg = false;
681 const Type* resultType = dest->getType();
683 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
684 assert (opCode != V9::INVALID_OPCODE
685 && "Unsupported result type in CreateCopyInstructionsByType()");
687 // if `src' is a constant that doesn't fit in the immed field or if it is
688 // a global variable (i.e., a constant address), generate a load
689 // instruction instead of an add
691 if (isa<Constant>(src)) {
692 unsigned int machineRegNum;
694 MachineOperand::MachineOperandType opType =
695 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
696 machineRegNum, immedValue);
698 if (opType == MachineOperand::MO_VirtualRegister)
699 loadConstantToReg = true;
701 else if (isa<GlobalValue>(src))
702 loadConstantToReg = true;
704 if (loadConstantToReg) {
705 // `src' is constant and cannot fit in immed field for the ADD
706 // Insert instructions to "load" the constant into a register
707 target.getInstrInfo()->CreateCodeToLoadConst(target, F, src, dest,
710 // Create a reg-to-reg copy instruction for the given type:
711 // -- For FP values, create a FMOVS or FMOVD instruction
712 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
713 // Make `src' the second operand, in case it is a small constant!
716 if (resultType->isFloatingPoint())
717 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
718 .addReg(src).addRegDef(dest));
720 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
721 MI = (BuildMI(opCode, 3)
722 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
729 // Helper function for sign-extension and zero-extension.
730 // For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
732 CreateBitExtensionInstructions(bool signExtend,
733 const TargetMachine& target,
737 unsigned int numLowBits,
738 std::vector<MachineInstr*>& mvec,
739 MachineCodeForInstruction& mcfi)
743 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
745 if (numLowBits < 32) {
746 // SLL is needed since operand size is < 32 bits.
747 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
748 srcVal, destVal, "make32");
749 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
750 .addZImm(32-numLowBits).addRegDef(tmpI));
754 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
755 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
759 // Create instruction sequence to produce a sign-extended register value
760 // from an arbitrary-sized integer value (sized in bits, not bytes).
761 // The generated instructions are returned in `mvec'.
762 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
763 // Any stack space required is allocated via MachineFunction.
766 SparcV9InstrInfo::CreateSignExtensionInstructions(
767 const TargetMachine& target,
771 unsigned int numLowBits,
772 std::vector<MachineInstr*>& mvec,
773 MachineCodeForInstruction& mcfi) const
775 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
776 destVal, numLowBits, mvec, mcfi);
780 // Create instruction sequence to produce a zero-extended register value
781 // from an arbitrary-sized integer value (sized in bits, not bytes).
782 // For SPARC v9, we sign-extend the given operand using SLL; SRL.
783 // The generated instructions are returned in `mvec'.
784 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
785 // Any stack space required is allocated via MachineFunction.
788 SparcV9InstrInfo::CreateZeroExtensionInstructions(
789 const TargetMachine& target,
793 unsigned int numLowBits,
794 std::vector<MachineInstr*>& mvec,
795 MachineCodeForInstruction& mcfi) const
797 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
798 destVal, numLowBits, mvec, mcfi);
801 } // End llvm namespace