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 // For other types of constants, some conversion may be needed.
78 // First, extract the constant operand according to its own type
79 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
80 switch(CE->getOpcode()) {
81 case Instruction::Cast: // recursively get the value as cast
82 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
85 default: // not simplifying other ConstantExprs
88 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
89 isValidConstant = true;
90 C = CI->getRawValue();
92 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
93 isValidConstant = true;
94 double fC = CFP->getValue();
95 C = (destType->isSigned()? (uint64_t) (int64_t) fC
99 // Now if a valid value was found, convert it to destType.
100 if (isValidConstant) {
101 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
102 unsigned destSize = target.getTargetData().getTypeSize(destType);
103 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
104 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
106 if (destType->isSigned()) {
107 if (opSize > destSize) // operand is larger than dest:
108 C = C & maskHi; // mask high bits
110 if (opSize > destSize ||
111 (opSize == destSize && ! V->getType()->isSigned()))
112 if (C & (1U << (8*destSize - 1)))
113 C = C | ~maskHi; // sign-extend from destSize to 64 bits
116 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
117 // operand is larger than dest,
118 // OR both are equal but smaller than the full register size
119 // AND operand is signed, so it may have extra sign bits:
130 //----------------------------------------------------------------------------
131 // Function: CreateSETUWConst
133 // Set a 32-bit unsigned constant in the register `dest', using
134 // SETHI, OR in the worst case. This function correctly emulates
135 // the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
137 // The isSigned=true case is used to implement SETSW without duplicating code.
139 // Optimize some common cases:
140 // (1) Small value that fits in simm13 field of OR: don't need SETHI.
141 // (2) isSigned = true and C is a small negative signed value, i.e.,
142 // high bits are 1, and the remaining bits fit in simm13(OR).
143 //----------------------------------------------------------------------------
146 CreateSETUWConst(const TargetMachine& target, uint32_t C,
147 Instruction* dest, std::vector<MachineInstr*>& mvec,
148 bool isSigned = false)
150 MachineInstr *miSETHI = NULL, *miOR = NULL;
152 // In order to get efficient code, we should not generate the SETHI if
153 // all high bits are 1 (i.e., this is a small signed value that fits in
154 // the simm13 field of OR). So we check for and handle that case specially.
155 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
156 // In fact, sC == -sC, so we have to check for this explicitly.
157 int32_t sC = (int32_t) C;
158 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
160 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
161 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
162 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
163 miSETHI->setOperandHi32(0);
164 mvec.push_back(miSETHI);
167 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
168 // was generated, or if the low 10 bits are non-zero.
169 if (miSETHI==NULL || C & MAXLO) {
171 // unsigned value with high-order bits set using SETHI
172 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
173 miOR->setOperandLo32(1);
175 // unsigned or small signed value that fits in simm13 field of OR
176 assert(smallNegValue || (C & ~MAXSIMM) == 0);
177 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()->getZeroRegNum())
178 .addSImm(sC).addRegDef(dest);
180 mvec.push_back(miOR);
183 assert((miSETHI || miOR) && "Oops, no code was generated!");
187 //----------------------------------------------------------------------------
188 // Function: CreateSETSWConst
190 // Set a 32-bit signed constant in the register `dest', with sign-extension
191 // to 64 bits. This uses SETHI, OR, SRA in the worst case.
192 // This function correctly emulates the SETSW pseudo-op for SPARC v9.
194 // Optimize the same cases as SETUWConst, plus:
195 // (1) SRA is not needed for positive or small negative values.
196 //----------------------------------------------------------------------------
199 CreateSETSWConst(const TargetMachine& target, int32_t C,
200 Instruction* dest, std::vector<MachineInstr*>& mvec)
202 // Set the low 32 bits of dest
203 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
205 // Sign-extend to the high 32 bits if needed.
206 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
207 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
208 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
212 //----------------------------------------------------------------------------
213 // Function: CreateSETXConst
215 // Set a 64-bit signed or unsigned constant in the register `dest'.
216 // Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
217 // This function correctly emulates the SETX pseudo-op for SPARC v9.
219 // Optimize the same cases as SETUWConst for each 32 bit word.
220 //----------------------------------------------------------------------------
223 CreateSETXConst(const TargetMachine& target, uint64_t C,
224 Instruction* tmpReg, Instruction* dest,
225 std::vector<MachineInstr*>& mvec)
227 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
231 // Code to set the upper 32 bits of the value in register `tmpReg'
232 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
234 // Shift tmpReg left by 32 bits
235 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
238 // Code to set the low 32 bits of the value in register `dest'
239 CreateSETUWConst(target, C, dest, mvec);
241 // dest = OR(tmpReg, dest)
242 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
246 //----------------------------------------------------------------------------
247 // Function: CreateSETUWLabel
249 // Set a 32-bit constant (given by a symbolic label) in the register `dest'.
250 //----------------------------------------------------------------------------
253 CreateSETUWLabel(const TargetMachine& target, Value* val,
254 Instruction* dest, std::vector<MachineInstr*>& mvec)
258 // Set the high 22 bits in dest
259 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
260 MI->setOperandHi32(0);
263 // Set the low 10 bits in dest
264 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
265 MI->setOperandLo32(1);
270 //----------------------------------------------------------------------------
271 // Function: CreateSETXLabel
273 // Set a 64-bit constant (given by a symbolic label) in the register `dest'.
274 //----------------------------------------------------------------------------
277 CreateSETXLabel(const TargetMachine& target,
278 Value* val, Instruction* tmpReg, Instruction* dest,
279 std::vector<MachineInstr*>& mvec)
281 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
282 "I only know about constant values and global addresses");
286 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
287 MI->setOperandHi64(0);
290 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
291 MI->setOperandLo64(1);
294 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
296 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
297 MI->setOperandHi32(0);
300 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
303 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
304 MI->setOperandLo32(1);
309 //----------------------------------------------------------------------------
310 // Function: CreateUIntSetInstruction
312 // Create code to Set an unsigned constant in the register `dest'.
313 // Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
314 // CreateSETSWConst is an optimization for the case that the unsigned value
315 // has all ones in the 33 high bits (so that sign-extension sets them all).
316 //----------------------------------------------------------------------------
319 CreateUIntSetInstruction(const TargetMachine& target,
320 uint64_t C, Instruction* dest,
321 std::vector<MachineInstr*>& mvec,
322 MachineCodeForInstruction& mcfi)
324 static const uint64_t lo32 = (uint32_t) ~0;
325 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
326 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
327 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
328 // All high 33 (not 32) bits are 1s: sign-extension will take care
329 // of high 32 bits, so use the sequence for signed int
330 CreateSETSWConst(target, (int32_t) C, dest, mvec);
331 } else if (C > lo32) {
332 // C does not fit in 32 bits
333 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
334 CreateSETXConst(target, C, tmpReg, dest, mvec);
339 //----------------------------------------------------------------------------
340 // Function: CreateIntSetInstruction
342 // Create code to Set a signed constant in the register `dest'.
343 // Really the same as CreateUIntSetInstruction.
344 //----------------------------------------------------------------------------
347 CreateIntSetInstruction(const TargetMachine& target,
348 int64_t C, Instruction* dest,
349 std::vector<MachineInstr*>& mvec,
350 MachineCodeForInstruction& mcfi)
352 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
356 //---------------------------------------------------------------------------
357 // Create a table of LLVM opcode -> max. immediate constant likely to
358 // be usable for that operation.
359 //---------------------------------------------------------------------------
361 // Entry == 0 ==> no immediate constant field exists at all.
362 // Entry > 0 ==> abs(immediate constant) <= Entry
364 std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
367 MaxConstantForInstr(unsigned llvmOpCode)
369 int modelOpCode = -1;
371 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
372 llvmOpCode < Instruction::BinaryOpsEnd)
373 modelOpCode = V9::ADDi;
376 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
378 case Instruction::Malloc:
379 case Instruction::Alloca:
380 case Instruction::GetElementPtr:
381 case Instruction::PHI:
382 case Instruction::Cast:
383 case Instruction::Call: modelOpCode = V9::ADDi; break;
385 case Instruction::Shl:
386 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
391 return (modelOpCode < 0)? 0: SparcV9MachineInstrDesc[modelOpCode].maxImmedConst;
395 InitializeMaxConstantsTable()
398 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
399 "assignments below will be illegal!");
400 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
401 MaxConstantsTable[op] = MaxConstantForInstr(op);
402 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
403 MaxConstantsTable[op] = MaxConstantForInstr(op);
404 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
405 MaxConstantsTable[op] = MaxConstantForInstr(op);
406 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
407 MaxConstantsTable[op] = MaxConstantForInstr(op);
411 //---------------------------------------------------------------------------
412 // class SparcV9InstrInfo
415 // Information about individual instructions.
416 // Most information is stored in the SparcV9MachineInstrDesc array above.
417 // Other information is computed on demand, and most such functions
418 // default to member functions in base class TargetInstrInfo.
419 //---------------------------------------------------------------------------
421 SparcV9InstrInfo::SparcV9InstrInfo()
422 : TargetInstrInfo(SparcV9MachineInstrDesc, V9::NUM_TOTAL_OPCODES) {
423 InitializeMaxConstantsTable();
427 SparcV9InstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
428 const Instruction* I) const
430 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
433 if (isa<ConstantPointerNull>(CV)) // can always use %g0
436 if (isa<SwitchInst>(I)) // Switch instructions will be lowered!
439 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
440 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
442 if (isa<ConstantBool>(CV))
443 return 1 > MaxConstantsTable[I->getOpcode()];
449 // Create an instruction sequence to put the constant `val' into
450 // the virtual register `dest'. `val' may be a Constant or a
451 // GlobalValue, viz., the constant address of a global variable or function.
452 // The generated instructions are returned in `mvec'.
453 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
454 // Any stack space required is allocated via MachineFunction.
457 SparcV9InstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
461 std::vector<MachineInstr*>& mvec,
462 MachineCodeForInstruction& mcfi) const
464 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
465 "I only know about constant values and global addresses");
467 // Use a "set" instruction for known constants or symbolic constants (labels)
468 // that can go in an integer reg.
469 // We have to use a "load" instruction for all other constants,
470 // in particular, floating point constants.
472 const Type* valType = val->getType();
474 // A ConstantPointerRef is just a reference to GlobalValue.
475 while (isa<ConstantPointerRef>(val))
476 val = cast<ConstantPointerRef>(val)->getValue();
478 if (isa<GlobalValue>(val)) {
479 TmpInstruction* tmpReg =
480 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
481 CreateSETXLabel(target, val, tmpReg, dest, mvec);
486 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
488 if (dest->getType()->isSigned())
489 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
491 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
494 // Make an instruction sequence to load the constant, viz:
495 // SETX <addr-of-constant>, tmpReg, addrReg
496 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
498 // First, create a tmp register to be used by the SETX sequence.
499 TmpInstruction* tmpReg =
500 new TmpInstruction(mcfi, PointerType::get(val->getType()));
502 // Create another TmpInstruction for the address register
503 TmpInstruction* addrReg =
504 new TmpInstruction(mcfi, PointerType::get(val->getType()));
506 // Get the constant pool index for this constant
507 MachineConstantPool *CP = MachineFunction::get(F).getConstantPool();
508 Constant *C = cast<Constant>(val);
509 unsigned CPI = CP->getConstantPoolIndex(C);
511 // Put the address of the constant into a register
514 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(tmpReg);
515 MI->setOperandHi64(0);
518 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addConstantPoolIndex(CPI)
520 MI->setOperandLo64(1);
523 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
525 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(addrReg);
526 MI->setOperandHi32(0);
529 MI = BuildMI(V9::ORr, 3).addReg(addrReg).addReg(tmpReg).addRegDef(addrReg);
532 MI = BuildMI(V9::ORi, 3).addReg(addrReg).addConstantPoolIndex(CPI)
534 MI->setOperandLo32(1);
537 // Now load the constant from out ConstantPool label
538 unsigned Opcode = ChooseLoadInstruction(val->getType());
539 Opcode = convertOpcodeFromRegToImm(Opcode);
540 mvec.push_back(BuildMI(Opcode, 3)
541 .addReg(addrReg).addSImm((int64_t)0).addRegDef(dest));
546 // Create an instruction sequence to copy an integer register `val'
547 // to a floating point register `dest' by copying to memory and back.
548 // val must be an integral type. dest must be a Float or Double.
549 // The generated instructions are returned in `mvec'.
550 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
551 // Any stack space required is allocated via MachineFunction.
554 SparcV9InstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
558 std::vector<MachineInstr*>& mvec,
559 MachineCodeForInstruction& mcfi) const
561 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
562 && "Source type must be integral (integer or bool) or pointer");
563 assert(dest->getType()->isFloatingPoint()
564 && "Dest type must be float/double");
566 // Get a stack slot to use for the copy
567 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
569 // Get the size of the source value being copied.
570 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
572 // Store instruction stores `val' to [%fp+offset].
573 // The store and load opCodes are based on the size of the source value.
574 // If the value is smaller than 32 bits, we must sign- or zero-extend it
575 // to 32 bits since the load-float will load 32 bits.
576 // Note that the store instruction is the same for signed and unsigned ints.
577 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
578 Value* storeVal = val;
579 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
580 // sign- or zero-extend respectively
581 storeVal = new TmpInstruction(mcfi, storeType, val);
582 if (val->getType()->isSigned())
583 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
586 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
590 unsigned FPReg = target.getRegInfo()->getFramePointer();
591 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
592 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
593 mvec.push_back(BuildMI(StoreOpcode, 3)
594 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
596 // Load instruction loads [%fp+offset] to `dest'.
597 // The type of the load opCode is the floating point type that matches the
598 // stored type in size:
599 // On SparcV9: float for int or smaller, double for long.
601 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
602 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
603 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
604 mvec.push_back(BuildMI(LoadOpcode, 3)
605 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
608 // Similarly, create an instruction sequence to copy an FP register
609 // `val' to an integer register `dest' by copying to memory and back.
610 // The generated instructions are returned in `mvec'.
611 // Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
612 // Temporary stack space required is allocated via MachineFunction.
615 SparcV9InstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
619 std::vector<MachineInstr*>& mvec,
620 MachineCodeForInstruction& mcfi) const
622 const Type* opTy = val->getType();
623 const Type* destTy = dest->getType();
625 assert(opTy->isFloatingPoint() && "Source type must be float/double");
626 assert((destTy->isIntegral() || isa<PointerType>(destTy))
627 && "Dest type must be integer, bool or pointer");
629 // FIXME: For now, we allocate permanent space because the stack frame
630 // manager does not allow locals to be allocated (e.g., for alloca) after
631 // a temp is allocated!
633 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
635 unsigned FPReg = target.getRegInfo()->getFramePointer();
637 // Store instruction stores `val' to [%fp+offset].
638 // The store opCode is based only the source value being copied.
640 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
641 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
642 mvec.push_back(BuildMI(StoreOpcode, 3)
643 .addReg(val).addMReg(FPReg).addSImm(offset));
645 // Load instruction loads [%fp+offset] to `dest'.
646 // The type of the load opCode is the integer type that matches the
647 // source type in size:
648 // On SparcV9: int for float, long for double.
649 // Note that we *must* use signed loads even for unsigned dest types, to
650 // ensure correct sign-extension for UByte, UShort or UInt:
652 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
653 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
654 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
655 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
656 .addSImm(offset).addRegDef(dest));
660 // Create instruction(s) to copy src to dest, for arbitrary types
661 // The generated instructions are returned in `mvec'.
662 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
663 // Any stack space required is allocated via MachineFunction.
666 SparcV9InstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
670 std::vector<MachineInstr*>& mvec,
671 MachineCodeForInstruction& mcfi) const
673 bool loadConstantToReg = false;
675 const Type* resultType = dest->getType();
677 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
678 assert (opCode != V9::INVALID_OPCODE
679 && "Unsupported result type in CreateCopyInstructionsByType()");
681 // if `src' is a constant that doesn't fit in the immed field or if it is
682 // a global variable (i.e., a constant address), generate a load
683 // instruction instead of an add
685 if (isa<Constant>(src)) {
686 unsigned int machineRegNum;
688 MachineOperand::MachineOperandType opType =
689 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
690 machineRegNum, immedValue);
692 if (opType == MachineOperand::MO_VirtualRegister)
693 loadConstantToReg = true;
695 else if (isa<GlobalValue>(src))
696 loadConstantToReg = true;
698 if (loadConstantToReg) {
699 // `src' is constant and cannot fit in immed field for the ADD
700 // Insert instructions to "load" the constant into a register
701 target.getInstrInfo()->CreateCodeToLoadConst(target, F, src, dest,
704 // Create a reg-to-reg copy instruction for the given type:
705 // -- For FP values, create a FMOVS or FMOVD instruction
706 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
707 // Make `src' the second operand, in case it is a small constant!
710 if (resultType->isFloatingPoint())
711 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
712 .addReg(src).addRegDef(dest));
714 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
715 MI = (BuildMI(opCode, 3)
716 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
723 // Helper function for sign-extension and zero-extension.
724 // For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
726 CreateBitExtensionInstructions(bool signExtend,
727 const TargetMachine& target,
731 unsigned int numLowBits,
732 std::vector<MachineInstr*>& mvec,
733 MachineCodeForInstruction& mcfi)
737 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
739 if (numLowBits < 32) {
740 // SLL is needed since operand size is < 32 bits.
741 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
742 srcVal, destVal, "make32");
743 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
744 .addZImm(32-numLowBits).addRegDef(tmpI));
748 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
749 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
753 // Create instruction sequence to produce a sign-extended register value
754 // from an arbitrary-sized integer value (sized in bits, not bytes).
755 // The generated instructions are returned in `mvec'.
756 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
757 // Any stack space required is allocated via MachineFunction.
760 SparcV9InstrInfo::CreateSignExtensionInstructions(
761 const TargetMachine& target,
765 unsigned int numLowBits,
766 std::vector<MachineInstr*>& mvec,
767 MachineCodeForInstruction& mcfi) const
769 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
770 destVal, numLowBits, mvec, mcfi);
774 // Create instruction sequence to produce a zero-extended register value
775 // from an arbitrary-sized integer value (sized in bits, not bytes).
776 // For SPARC v9, we sign-extend the given operand using SLL; SRL.
777 // The generated instructions are returned in `mvec'.
778 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
779 // Any stack space required is allocated via MachineFunction.
782 SparcV9InstrInfo::CreateZeroExtensionInstructions(
783 const TargetMachine& target,
787 unsigned int numLowBits,
788 std::vector<MachineInstr*>& mvec,
789 MachineCodeForInstruction& mcfi) const
791 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
792 destVal, numLowBits, mvec, mcfi);
795 } // End llvm namespace