blob: 8f35be8a07d3ce38802e9baeac385941128896d9 [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
Misha Brukman6fe69052003-06-07 02:34:43 +0000206 if (opType == MachineOperand::MO_SignExtendedImmed ||
207 opType == MachineOperand::MO_UnextendedImmed) {
Misha Brukmand15cd272003-06-04 04:54:06 +0000208 // The optype is an immediate value
209 // This means we need to change the opcode, e.g. ADDr -> ADDi
210 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
211 minstr->setOpcode(newOpcode);
212 }
213
Chris Lattner133f0792002-10-28 04:45:29 +0000214 if (opType == mop.getType())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000215 continue; // no change: this is the most common case
216
217 if (opType == MachineOperand::MO_VirtualRegister)
218 {
219 constantThatMustBeLoaded = true;
220 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000221 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
222 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000223 }
224 }
225
226 if (opType == MachineOperand::MO_MachineRegister)
227 minstr->SetMachineOperandReg(op, machineRegNum);
228 else if (opType == MachineOperand::MO_SignExtendedImmed ||
Misha Brukmanc740aae2003-06-03 03:18:20 +0000229 opType == MachineOperand::MO_UnextendedImmed) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000230 minstr->SetMachineOperandConst(op, opType, immedValue);
Misha Brukman6fe69052003-06-07 02:34:43 +0000231 // The optype is or has become an immediate
232 // This means we need to change the opcode, e.g. ADDr -> ADDi
233 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
234 minstr->setOpcode(newOpcode);
Misha Brukmanc740aae2003-06-03 03:18:20 +0000235 } else if (constantThatMustBeLoaded ||
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000236 (opValue && isa<GlobalValue>(opValue)))
237 { // opValue is a constant that must be explicitly loaded into a reg
238 assert(opValue);
239 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000240 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000241 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
242 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000243 }
244 }
245
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000246 // Also, check for implicit operands used by the machine instruction
247 // (no need to check those defined since they cannot be constants).
248 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000249 // -- arguments to a Call
250 // -- return value of a Return
251 // Any such operand that is a constant value needs to be fixed also.
252 // The current instructions with implicit refs (viz., Call and Return)
253 // have no immediate fields, so the constant always needs to be loaded
254 // into a register.
255 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000256 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000257 unsigned lastCallArgNum = 0; // unused if not a call
258 CallArgsDescriptor* argDesc = NULL; // unused if not a call
259 if (isCall)
260 argDesc = CallArgsDescriptor::get(minstr);
261
Vikram S. Adve6d353262001-10-17 23:57:50 +0000262 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000263 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000264 isa<GlobalValue>(minstr->getImplicitRef(i)))
265 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000266 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000267 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000268 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000269 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000270
271 if (isCall)
272 { // find and replace the argument in the CallArgsDescriptor
273 unsigned i=lastCallArgNum;
274 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
275 ++i;
276 assert(i < argDesc->getNumArgs() &&
277 "Constant operands to a call *must* be in the arg list");
278 lastCallArgNum = i;
279 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
280 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000281 }
282
Chris Lattner04120772003-01-15 19:47:53 +0000283 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000284}