blob: f654e90100bb8742960779c610eb353948d61408 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- InstrSelectionSupport.cpp -----------------------------------------===//
2//
3// Target-independent instruction selection code. See SparcInstrSelection.cpp
4// for usage.
Vikram S. Advea1d14f32001-10-10 20:50:43 +00005//
Chris Lattner035dfbe2002-08-09 20:08:06 +00006//===----------------------------------------------------------------------===//
Vikram S. Advea1d14f32001-10-10 20:50:43 +00007
8#include "llvm/CodeGen/InstrSelectionSupport.h"
9#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000010#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000011#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000012#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000013#include "llvm/Target/TargetMachine.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000014#include "llvm/Target/TargetRegInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000015#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner795ba6c2003-01-15 21:36:50 +000017#include "llvm/BasicBlock.h"
Chris Lattnerc5b8b1a2002-10-28 23:54:47 +000018#include "llvm/DerivedTypes.h"
Misha Brukman88ba2542003-05-30 20:36:27 +000019#include "../../Target/Sparc/SparcInstrSelectionSupport.h"
Chris Lattner697954c2002-01-20 22:54:45 +000020using std::vector;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000021
22//*************************** Local Functions ******************************/
23
Vikram S. Advea1d14f32001-10-10 20:50:43 +000024
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000025// Generate code to load the constant into a TmpInstruction (virtual reg) and
26// returns the virtual register.
27//
Vikram S. Adve6d353262001-10-17 23:57:50 +000028static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000029InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000030 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000031 Instruction* vmInstr,
32 vector<MachineInstr*>& loadConstVec,
33 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000034{
Vikram S. Adve6d353262001-10-17 23:57:50 +000035 // Create a tmp virtual register to hold the constant.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000036 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
Vikram S. Advef3d3ca12003-05-31 07:41:24 +000037 TmpInstruction* tmpReg = new TmpInstruction(mcfi, opValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000038
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000039 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
40 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000041
42 // Record the mapping from the tmp VM instruction to machine instruction.
43 // Do this for all machine instructions that were not mapped to any
44 // other temp values created by
45 // tmpReg->addMachineInstruction(loadConstVec.back());
46
47 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000048}
49
50
Vikram S. Advea1d14f32001-10-10 20:50:43 +000051MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +000052ChooseRegOrImmed(int64_t intValue,
53 bool isSigned,
54 MachineOpCode opCode,
55 const TargetMachine& target,
56 bool canUseImmed,
57 unsigned int& getMachineRegNum,
58 int64_t& getImmedValue)
59{
60 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
61 getMachineRegNum = 0;
62 getImmedValue = 0;
63
64 if (canUseImmed &&
Chris Lattner795ba6c2003-01-15 21:36:50 +000065 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
Vikram S. Advefd0ec802002-09-16 15:15:57 +000066 {
67 opType = isSigned? MachineOperand::MO_SignExtendedImmed
68 : MachineOperand::MO_UnextendedImmed;
69 getImmedValue = intValue;
70 }
71 else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
72 {
73 opType = MachineOperand::MO_MachineRegister;
74 getMachineRegNum = target.getRegInfo().getZeroRegNum();
75 }
76
77 return opType;
78}
79
80
81MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +000082ChooseRegOrImmed(Value* val,
83 MachineOpCode opCode,
84 const TargetMachine& target,
85 bool canUseImmed,
86 unsigned int& getMachineRegNum,
87 int64_t& getImmedValue)
88{
Vikram S. Advea1d14f32001-10-10 20:50:43 +000089 getMachineRegNum = 0;
90 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +000091
92 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Chris Lattnere9bb2df2001-12-03 22:26:30 +000093 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Advefd0ec802002-09-16 15:15:57 +000094 if (CPV == NULL ||
95 (! CPV->getType()->isIntegral() &&
96 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
97 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000098
Vikram S. Advea1d14f32001-10-10 20:50:43 +000099 // Now get the constant value and check if it fits in the IMMED field.
100 // Take advantage of the fact that the max unsigned value will rarely
101 // fit into any IMMED field and ignore that case (i.e., cast smaller
102 // unsigned constants to signed).
103 //
104 int64_t intValue;
Chris Lattner9b625032002-05-06 16:15:30 +0000105 if (isa<PointerType>(CPV->getType()))
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000106 intValue = 0; // We checked above that it is NULL
107 else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
108 intValue = (int64_t) CB->getValue();
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000109 else if (CPV->getType()->isSigned())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000110 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000111 else
Vikram S. Adve1c10f172002-09-27 14:26:20 +0000112 { // get the int value and sign-extend if original was less than 64 bits
113 intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
114 switch(CPV->getType()->getPrimitiveID())
115 {
116 case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
117 case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
118 case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
119 default: break;
120 }
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000121 }
122
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000123 return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
124 opCode, target, canUseImmed,
125 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000126}
127
Vikram S. Adve6d353262001-10-17 23:57:50 +0000128
Chris Lattner04120772003-01-15 19:47:53 +0000129
Vikram S. Adve6d353262001-10-17 23:57:50 +0000130//---------------------------------------------------------------------------
131// Function: FixConstantOperandsForInstr
132//
133// Purpose:
134// Special handling for constant operands of a machine instruction
135// -- if the constant is 0, use the hardwired 0 register, if any;
136// -- if the constant fits in the IMMEDIATE field, use that field;
137// -- else create instructions to put the constant into a register, either
138// directly or by loading explicitly from the constant pool.
139//
140// In the first 2 cases, the operand of `minstr' is modified in place.
141// Returns a vector of machine instructions generated for operands that
142// fall under case 3; these must be inserted before `minstr'.
143//---------------------------------------------------------------------------
144
145vector<MachineInstr*>
146FixConstantOperandsForInstr(Instruction* vmInstr,
147 MachineInstr* minstr,
148 TargetMachine& target)
149{
Chris Lattner04120772003-01-15 19:47:53 +0000150 vector<MachineInstr*> MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000151
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000152 MachineOpCode opCode = minstr->getOpCode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000153 const TargetInstrInfo& instrInfo = target.getInstrInfo();
Chris Lattner8f780272002-10-29 17:25:41 +0000154 int resultPos = instrInfo.getResultPos(opCode);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000155 int immedPos = instrInfo.getImmedConstantPos(opCode);
156
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000157 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000158
Vikram S. Adve6d353262001-10-17 23:57:50 +0000159 for (unsigned op=0; op < minstr->getNumOperands(); op++)
160 {
161 const MachineOperand& mop = minstr->getOperand(op);
162
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000163 // Skip the result position, preallocated machine registers, or operands
164 // that cannot be constants (CC regs or PC-relative displacements)
Chris Lattner8f780272002-10-29 17:25:41 +0000165 if (resultPos == (int)op ||
Chris Lattner133f0792002-10-28 04:45:29 +0000166 mop.getType() == MachineOperand::MO_MachineRegister ||
167 mop.getType() == MachineOperand::MO_CCRegister ||
168 mop.getType() == MachineOperand::MO_PCRelativeDisp)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000169 continue;
170
Vikram S. Adve6d353262001-10-17 23:57:50 +0000171 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000172 unsigned int machineRegNum = 0;
173 int64_t immedValue = 0;
174 Value* opValue = NULL;
175 MachineOperand::MachineOperandType opType =
176 MachineOperand::MO_VirtualRegister;
177
178 // Operand may be a virtual register or a compile-time constant
Chris Lattner133f0792002-10-28 04:45:29 +0000179 if (mop.getType() == MachineOperand::MO_VirtualRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000180 {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000181 assert(mop.getVRegValue() != NULL);
Chris Lattnerc7c7b7a2003-01-15 20:32:15 +0000182 opValue = mop.getVRegValue();
183 if (Constant *opConst = dyn_cast<Constant>(opValue)) {
Chris Lattner04120772003-01-15 19:47:53 +0000184 opType = ChooseRegOrImmed(opConst, opCode, target,
185 (immedPos == (int)op), machineRegNum,
186 immedValue);
187 if (opType == MachineOperand::MO_VirtualRegister)
188 constantThatMustBeLoaded = true;
189 }
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000190 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000191 else
192 {
Chris Lattner04120772003-01-15 19:47:53 +0000193 assert(mop.isImmediate());
194 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000195
196 // Bit-selection flags indicate an instruction that is extracting
197 // bits from its operand so ignore this even if it is a big constant.
198 if (mop.opHiBits32() || mop.opLoBits32() ||
199 mop.opHiBits64() || mop.opLoBits64())
200 continue;
201
202 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
203 opCode, target, (immedPos == (int)op),
204 machineRegNum, immedValue);
205
Chris Lattner133f0792002-10-28 04:45:29 +0000206 if (opType == mop.getType())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000207 continue; // no change: this is the most common case
208
209 if (opType == MachineOperand::MO_VirtualRegister)
210 {
211 constantThatMustBeLoaded = true;
212 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000213 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
214 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000215 }
216 }
217
218 if (opType == MachineOperand::MO_MachineRegister)
219 minstr->SetMachineOperandReg(op, machineRegNum);
220 else if (opType == MachineOperand::MO_SignExtendedImmed ||
Misha Brukmanc740aae2003-06-03 03:18:20 +0000221 opType == MachineOperand::MO_UnextendedImmed) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000222 minstr->SetMachineOperandConst(op, opType, immedValue);
Misha Brukmanc740aae2003-06-03 03:18:20 +0000223 // The optype has changed from being a register to an immediate
224 // This means we need to change the opcode, e.g. ADDr -> ADDi
225 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
226 minstr->setOpcode(newOpcode);
227 } else if (constantThatMustBeLoaded ||
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000228 (opValue && isa<GlobalValue>(opValue)))
229 { // opValue is a constant that must be explicitly loaded into a reg
230 assert(opValue);
231 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000232 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000233 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
234 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000235 }
236 }
237
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000238 // Also, check for implicit operands used by the machine instruction
239 // (no need to check those defined since they cannot be constants).
240 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000241 // -- arguments to a Call
242 // -- return value of a Return
243 // Any such operand that is a constant value needs to be fixed also.
244 // The current instructions with implicit refs (viz., Call and Return)
245 // have no immediate fields, so the constant always needs to be loaded
246 // into a register.
247 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000248 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000249 unsigned lastCallArgNum = 0; // unused if not a call
250 CallArgsDescriptor* argDesc = NULL; // unused if not a call
251 if (isCall)
252 argDesc = CallArgsDescriptor::get(minstr);
253
Vikram S. Adve6d353262001-10-17 23:57:50 +0000254 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000255 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000256 isa<GlobalValue>(minstr->getImplicitRef(i)))
257 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000258 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000259 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000260 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000261 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000262
263 if (isCall)
264 { // find and replace the argument in the CallArgsDescriptor
265 unsigned i=lastCallArgNum;
266 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
267 ++i;
268 assert(i < argDesc->getNumArgs() &&
269 "Constant operands to a call *must* be in the arg list");
270 lastCallArgNum = i;
271 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
272 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000273 }
274
Chris Lattner04120772003-01-15 19:47:53 +0000275 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000276}