1 //===-- SparcInstrInfo.cpp ------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
12 #include "SparcInternals.h"
13 #include "SparcInstrSelectionSupport.h"
14 #include "llvm/CodeGen/InstrSelection.h"
15 #include "llvm/CodeGen/InstrSelectionSupport.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionInfo.h"
18 #include "llvm/CodeGen/MachineCodeForInstruction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/Function.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
24 static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
25 static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
28 //---------------------------------------------------------------------------
29 // Function ConvertConstantToIntType
31 // Function to get the value of an integral constant in the form
32 // that must be put into the machine register. The specified constant is
33 // interpreted as (i.e., converted if necessary to) the specified destination
34 // type. The result is always returned as an uint64_t, since the representation
35 // of int64_t and uint64_t are identical. The argument can be any known const.
37 // isValidConstant is set to true if a valid constant was found.
38 //---------------------------------------------------------------------------
41 UltraSparcInstrInfo::ConvertConstantToIntType(const TargetMachine &target,
44 bool &isValidConstant) const
46 isValidConstant = false;
49 if (! destType->isIntegral() && ! isa<PointerType>(destType))
52 if (! isa<Constant>(V))
55 // ConstantPointerRef: no conversions needed: get value and return it
56 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
57 // A ConstantPointerRef is just a reference to GlobalValue.
58 isValidConstant = true; // may be overwritten by recursive call
59 return (CPR->isNullValue()? 0
60 : ConvertConstantToIntType(target, CPR->getValue(), destType,
64 // ConstantBool: no conversions needed: get value and return it
65 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
66 isValidConstant = true;
67 return (uint64_t) CB->getValue();
70 // For other types of constants, some conversion may be needed.
71 // First, extract the constant operand according to its own type
72 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
73 switch(CE->getOpcode()) {
74 case Instruction::Cast: // recursively get the value as cast
75 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
78 default: // not simplifying other ConstantExprs
81 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
82 isValidConstant = true;
83 C = CI->getRawValue();
85 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
86 isValidConstant = true;
87 double fC = CFP->getValue();
88 C = (destType->isSigned()? (uint64_t) (int64_t) fC
92 // Now if a valid value was found, convert it to destType.
93 if (isValidConstant) {
94 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
95 unsigned destSize = target.getTargetData().getTypeSize(destType);
96 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
97 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
99 if (destType->isSigned()) {
100 if (opSize > destSize) // operand is larger than dest:
101 C = C & maskHi; // mask high bits
103 if (opSize > destSize ||
104 (opSize == destSize && ! V->getType()->isSigned()))
105 if (C & (1U << (8*destSize - 1)))
106 C = C | ~maskHi; // sign-extend from destSize to 64 bits
109 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
110 // operand is larger than dest,
111 // OR both are equal but smaller than the full register size
112 // AND operand is signed, so it may have extra sign bits:
123 //----------------------------------------------------------------------------
124 // Function: CreateSETUWConst
126 // Set a 32-bit unsigned constant in the register `dest', using
127 // SETHI, OR in the worst case. This function correctly emulates
128 // the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
130 // The isSigned=true case is used to implement SETSW without duplicating code.
132 // Optimize some common cases:
133 // (1) Small value that fits in simm13 field of OR: don't need SETHI.
134 // (2) isSigned = true and C is a small negative signed value, i.e.,
135 // high bits are 1, and the remaining bits fit in simm13(OR).
136 //----------------------------------------------------------------------------
139 CreateSETUWConst(const TargetMachine& target, uint32_t C,
140 Instruction* dest, std::vector<MachineInstr*>& mvec,
141 bool isSigned = false)
143 MachineInstr *miSETHI = NULL, *miOR = NULL;
145 // In order to get efficient code, we should not generate the SETHI if
146 // all high bits are 1 (i.e., this is a small signed value that fits in
147 // the simm13 field of OR). So we check for and handle that case specially.
148 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
149 // In fact, sC == -sC, so we have to check for this explicitly.
150 int32_t sC = (int32_t) C;
151 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
153 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
154 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
155 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
156 miSETHI->setOperandHi32(0);
157 mvec.push_back(miSETHI);
160 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
161 // was generated, or if the low 10 bits are non-zero.
162 if (miSETHI==NULL || C & MAXLO) {
164 // unsigned value with high-order bits set using SETHI
165 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
166 miOR->setOperandLo32(1);
168 // unsigned or small signed value that fits in simm13 field of OR
169 assert(smallNegValue || (C & ~MAXSIMM) == 0);
170 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()
172 .addSImm(sC).addRegDef(dest);
174 mvec.push_back(miOR);
177 assert((miSETHI || miOR) && "Oops, no code was generated!");
181 //----------------------------------------------------------------------------
182 // Function: CreateSETSWConst
184 // Set a 32-bit signed constant in the register `dest', with sign-extension
185 // to 64 bits. This uses SETHI, OR, SRA in the worst case.
186 // This function correctly emulates the SETSW pseudo-op for SPARC v9.
188 // Optimize the same cases as SETUWConst, plus:
189 // (1) SRA is not needed for positive or small negative values.
190 //----------------------------------------------------------------------------
193 CreateSETSWConst(const TargetMachine& target, int32_t C,
194 Instruction* dest, std::vector<MachineInstr*>& mvec)
196 // Set the low 32 bits of dest
197 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
199 // Sign-extend to the high 32 bits if needed.
200 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
201 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
202 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
206 //----------------------------------------------------------------------------
207 // Function: CreateSETXConst
209 // Set a 64-bit signed or unsigned constant in the register `dest'.
210 // Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
211 // This function correctly emulates the SETX pseudo-op for SPARC v9.
213 // Optimize the same cases as SETUWConst for each 32 bit word.
214 //----------------------------------------------------------------------------
217 CreateSETXConst(const TargetMachine& target, uint64_t C,
218 Instruction* tmpReg, Instruction* dest,
219 std::vector<MachineInstr*>& mvec)
221 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
225 // Code to set the upper 32 bits of the value in register `tmpReg'
226 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
228 // Shift tmpReg left by 32 bits
229 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
232 // Code to set the low 32 bits of the value in register `dest'
233 CreateSETUWConst(target, C, dest, mvec);
235 // dest = OR(tmpReg, dest)
236 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
240 //----------------------------------------------------------------------------
241 // Function: CreateSETUWLabel
243 // Set a 32-bit constant (given by a symbolic label) in the register `dest'.
244 //----------------------------------------------------------------------------
247 CreateSETUWLabel(const TargetMachine& target, Value* val,
248 Instruction* dest, std::vector<MachineInstr*>& mvec)
252 // Set the high 22 bits in dest
253 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
254 MI->setOperandHi32(0);
257 // Set the low 10 bits in dest
258 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
259 MI->setOperandLo32(1);
264 //----------------------------------------------------------------------------
265 // Function: CreateSETXLabel
267 // Set a 64-bit constant (given by a symbolic label) in the register `dest'.
268 //----------------------------------------------------------------------------
271 CreateSETXLabel(const TargetMachine& target,
272 Value* val, Instruction* tmpReg, Instruction* dest,
273 std::vector<MachineInstr*>& mvec)
275 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
276 "I only know about constant values and global addresses");
280 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
281 MI->setOperandHi64(0);
284 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
285 MI->setOperandLo64(1);
288 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
290 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
291 MI->setOperandHi32(0);
294 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
297 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
298 MI->setOperandLo32(1);
303 //----------------------------------------------------------------------------
304 // Function: CreateUIntSetInstruction
306 // Create code to Set an unsigned constant in the register `dest'.
307 // Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
308 // CreateSETSWConst is an optimization for the case that the unsigned value
309 // has all ones in the 33 high bits (so that sign-extension sets them all).
310 //----------------------------------------------------------------------------
313 CreateUIntSetInstruction(const TargetMachine& target,
314 uint64_t C, Instruction* dest,
315 std::vector<MachineInstr*>& mvec,
316 MachineCodeForInstruction& mcfi)
318 static const uint64_t lo32 = (uint32_t) ~0;
319 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
320 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
321 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
322 // All high 33 (not 32) bits are 1s: sign-extension will take care
323 // of high 32 bits, so use the sequence for signed int
324 CreateSETSWConst(target, (int32_t) C, dest, mvec);
325 } else if (C > lo32) {
326 // C does not fit in 32 bits
327 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
328 CreateSETXConst(target, C, tmpReg, dest, mvec);
333 //----------------------------------------------------------------------------
334 // Function: CreateIntSetInstruction
336 // Create code to Set a signed constant in the register `dest'.
337 // Really the same as CreateUIntSetInstruction.
338 //----------------------------------------------------------------------------
341 CreateIntSetInstruction(const TargetMachine& target,
342 int64_t C, Instruction* dest,
343 std::vector<MachineInstr*>& mvec,
344 MachineCodeForInstruction& mcfi)
346 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
350 //---------------------------------------------------------------------------
351 // Create a table of LLVM opcode -> max. immediate constant likely to
352 // be usable for that operation.
353 //---------------------------------------------------------------------------
355 // Entry == 0 ==> no immediate constant field exists at all.
356 // Entry > 0 ==> abs(immediate constant) <= Entry
358 std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
361 MaxConstantForInstr(unsigned llvmOpCode)
363 int modelOpCode = -1;
365 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
366 llvmOpCode < Instruction::BinaryOpsEnd)
367 modelOpCode = V9::ADDi;
370 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
372 case Instruction::Malloc:
373 case Instruction::Alloca:
374 case Instruction::GetElementPtr:
375 case Instruction::PHI:
376 case Instruction::Cast:
377 case Instruction::Call: modelOpCode = V9::ADDi; break;
379 case Instruction::Shl:
380 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
385 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
389 InitializeMaxConstantsTable()
392 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
393 "assignments below will be illegal!");
394 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
395 MaxConstantsTable[op] = MaxConstantForInstr(op);
396 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
397 MaxConstantsTable[op] = MaxConstantForInstr(op);
398 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
399 MaxConstantsTable[op] = MaxConstantForInstr(op);
400 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
401 MaxConstantsTable[op] = MaxConstantForInstr(op);
405 //---------------------------------------------------------------------------
406 // class UltraSparcInstrInfo
409 // Information about individual instructions.
410 // Most information is stored in the SparcMachineInstrDesc array above.
411 // Other information is computed on demand, and most such functions
412 // default to member functions in base class TargetInstrInfo.
413 //---------------------------------------------------------------------------
416 UltraSparcInstrInfo::UltraSparcInstrInfo()
417 : TargetInstrInfo(SparcMachineInstrDesc,
418 /*descSize = */ V9::NUM_TOTAL_OPCODES,
419 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
421 InitializeMaxConstantsTable();
425 UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
426 const Instruction* I) const
428 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
431 if (isa<ConstantPointerNull>(CV)) // can always use %g0
434 if (isa<SwitchInst>(I)) // Switch instructions will be lowered!
437 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
438 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
440 if (isa<ConstantBool>(CV))
441 return 1 > MaxConstantsTable[I->getOpcode()];
447 // Create an instruction sequence to put the constant `val' into
448 // the virtual register `dest'. `val' may be a Constant or a
449 // GlobalValue, viz., the constant address of a global variable or function.
450 // The generated instructions are returned in `mvec'.
451 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
452 // Any stack space required is allocated via MachineFunction.
455 UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
459 std::vector<MachineInstr*>& mvec,
460 MachineCodeForInstruction& mcfi) const
462 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
463 "I only know about constant values and global addresses");
465 // Use a "set" instruction for known constants or symbolic constants (labels)
466 // that can go in an integer reg.
467 // We have to use a "load" instruction for all other constants,
468 // in particular, floating point constants.
470 const Type* valType = val->getType();
472 // A ConstantPointerRef is just a reference to GlobalValue.
473 while (isa<ConstantPointerRef>(val))
474 val = cast<ConstantPointerRef>(val)->getValue();
476 if (isa<GlobalValue>(val)) {
477 TmpInstruction* tmpReg =
478 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
479 CreateSETXLabel(target, val, tmpReg, dest, mvec);
484 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
486 if (dest->getType()->isSigned())
487 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
489 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
492 // Make an instruction sequence to load the constant, viz:
493 // SETX <addr-of-constant>, tmpReg, addrReg
494 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
496 // First, create a tmp register to be used by the SETX sequence.
497 TmpInstruction* tmpReg =
498 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
500 // Create another TmpInstruction for the address register
501 TmpInstruction* addrReg =
502 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
504 // Put the address (a symbolic name) into a register
505 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
507 // Generate the load instruction
508 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
509 unsigned Opcode = ChooseLoadInstruction(val->getType());
510 Opcode = convertOpcodeFromRegToImm(Opcode);
511 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
512 addSImm(zeroOffset).addRegDef(dest));
514 // Make sure constant is emitted to constant pool in assembly code.
515 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
520 // Create an instruction sequence to copy an integer register `val'
521 // to a floating point register `dest' by copying to memory and back.
522 // val must be an integral type. dest must be a Float or Double.
523 // The generated instructions are returned in `mvec'.
524 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
525 // Any stack space required is allocated via MachineFunction.
528 UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
532 std::vector<MachineInstr*>& mvec,
533 MachineCodeForInstruction& mcfi) const
535 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
536 && "Source type must be integral (integer or bool) or pointer");
537 assert(dest->getType()->isFloatingPoint()
538 && "Dest type must be float/double");
540 // Get a stack slot to use for the copy
541 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
543 // Get the size of the source value being copied.
544 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
546 // Store instruction stores `val' to [%fp+offset].
547 // The store and load opCodes are based on the size of the source value.
548 // If the value is smaller than 32 bits, we must sign- or zero-extend it
549 // to 32 bits since the load-float will load 32 bits.
550 // Note that the store instruction is the same for signed and unsigned ints.
551 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
552 Value* storeVal = val;
553 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
554 // sign- or zero-extend respectively
555 storeVal = new TmpInstruction(mcfi, storeType, val);
556 if (val->getType()->isSigned())
557 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
560 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
564 unsigned FPReg = target.getRegInfo().getFramePointer();
565 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
566 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
567 mvec.push_back(BuildMI(StoreOpcode, 3)
568 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
570 // Load instruction loads [%fp+offset] to `dest'.
571 // The type of the load opCode is the floating point type that matches the
572 // stored type in size:
573 // On SparcV9: float for int or smaller, double for long.
575 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
576 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
577 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
578 mvec.push_back(BuildMI(LoadOpcode, 3)
579 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
582 // Similarly, create an instruction sequence to copy an FP register
583 // `val' to an integer register `dest' by copying to memory and back.
584 // The generated instructions are returned in `mvec'.
585 // Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
586 // Temporary stack space required is allocated via MachineFunction.
589 UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
593 std::vector<MachineInstr*>& mvec,
594 MachineCodeForInstruction& mcfi) const
596 const Type* opTy = val->getType();
597 const Type* destTy = dest->getType();
599 assert(opTy->isFloatingPoint() && "Source type must be float/double");
600 assert((destTy->isIntegral() || isa<PointerType>(destTy))
601 && "Dest type must be integer, bool or pointer");
603 // FIXME: For now, we allocate permanent space because the stack frame
604 // manager does not allow locals to be allocated (e.g., for alloca) after
605 // a temp is allocated!
607 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
609 unsigned FPReg = target.getRegInfo().getFramePointer();
611 // Store instruction stores `val' to [%fp+offset].
612 // The store opCode is based only the source value being copied.
614 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
615 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
616 mvec.push_back(BuildMI(StoreOpcode, 3)
617 .addReg(val).addMReg(FPReg).addSImm(offset));
619 // Load instruction loads [%fp+offset] to `dest'.
620 // The type of the load opCode is the integer type that matches the
621 // source type in size:
622 // On SparcV9: int for float, long for double.
623 // Note that we *must* use signed loads even for unsigned dest types, to
624 // ensure correct sign-extension for UByte, UShort or UInt:
626 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
627 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
628 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
629 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
630 .addSImm(offset).addRegDef(dest));
634 // Create instruction(s) to copy src to dest, for arbitrary types
635 // The generated instructions are returned in `mvec'.
636 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
637 // Any stack space required is allocated via MachineFunction.
640 UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
644 std::vector<MachineInstr*>& mvec,
645 MachineCodeForInstruction& mcfi) const
647 bool loadConstantToReg = false;
649 const Type* resultType = dest->getType();
651 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
652 if (opCode == V9::INVALID_OPCODE) {
653 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
657 // if `src' is a constant that doesn't fit in the immed field or if it is
658 // a global variable (i.e., a constant address), generate a load
659 // instruction instead of an add
661 if (isa<Constant>(src)) {
662 unsigned int machineRegNum;
664 MachineOperand::MachineOperandType opType =
665 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
666 machineRegNum, immedValue);
668 if (opType == MachineOperand::MO_VirtualRegister)
669 loadConstantToReg = true;
671 else if (isa<GlobalValue>(src))
672 loadConstantToReg = true;
674 if (loadConstantToReg) {
675 // `src' is constant and cannot fit in immed field for the ADD
676 // Insert instructions to "load" the constant into a register
677 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
680 // Create a reg-to-reg copy instruction for the given type:
681 // -- For FP values, create a FMOVS or FMOVD instruction
682 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
683 // Make `src' the second operand, in case it is a small constant!
686 if (resultType->isFloatingPoint())
687 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
688 .addReg(src).addRegDef(dest));
690 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
691 MI = (BuildMI(opCode, 3)
692 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
699 // Helper function for sign-extension and zero-extension.
700 // For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
702 CreateBitExtensionInstructions(bool signExtend,
703 const TargetMachine& target,
707 unsigned int numLowBits,
708 std::vector<MachineInstr*>& mvec,
709 MachineCodeForInstruction& mcfi)
713 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
715 if (numLowBits < 32) {
716 // SLL is needed since operand size is < 32 bits.
717 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
718 srcVal, destVal, "make32");
719 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
720 .addZImm(32-numLowBits).addRegDef(tmpI));
724 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
725 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
729 // Create instruction sequence to produce a sign-extended register value
730 // from an arbitrary-sized integer value (sized in bits, not bytes).
731 // The generated instructions are returned in `mvec'.
732 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
733 // Any stack space required is allocated via MachineFunction.
736 UltraSparcInstrInfo::CreateSignExtensionInstructions(
737 const TargetMachine& target,
741 unsigned int numLowBits,
742 std::vector<MachineInstr*>& mvec,
743 MachineCodeForInstruction& mcfi) const
745 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
746 destVal, numLowBits, mvec, mcfi);
750 // Create instruction sequence to produce a zero-extended register value
751 // from an arbitrary-sized integer value (sized in bits, not bytes).
752 // For SPARC v9, we sign-extend the given operand using SLL; SRL.
753 // The generated instructions are returned in `mvec'.
754 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
755 // Any stack space required is allocated via MachineFunction.
758 UltraSparcInstrInfo::CreateZeroExtensionInstructions(
759 const TargetMachine& target,
763 unsigned int numLowBits,
764 std::vector<MachineInstr*>& mvec,
765 MachineCodeForInstruction& mcfi) const
767 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
768 destVal, numLowBits, mvec, mcfi);