blob: ff214500dbcd48bef81a1810fbc598a0b9d3cd00 [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. Adve97539fc2003-07-06 20:33:21 +000094 if (CPV == NULL
95 || CPV->isConstantExpr()
96 || (! CPV->getType()->isIntegral() &&
97 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
Vikram S. Advefd0ec802002-09-16 15:15:57 +000098 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000099
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000100 // Now get the constant value and check if it fits in the IMMED field.
101 // Take advantage of the fact that the max unsigned value will rarely
102 // fit into any IMMED field and ignore that case (i.e., cast smaller
103 // unsigned constants to signed).
104 //
105 int64_t intValue;
Chris Lattner9b625032002-05-06 16:15:30 +0000106 if (isa<PointerType>(CPV->getType()))
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000107 intValue = 0; // We checked above that it is NULL
108 else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
109 intValue = (int64_t) CB->getValue();
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000110 else if (CPV->getType()->isSigned())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000111 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000112 else
Vikram S. Adve1c10f172002-09-27 14:26:20 +0000113 { // get the int value and sign-extend if original was less than 64 bits
114 intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
115 switch(CPV->getType()->getPrimitiveID())
116 {
117 case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
118 case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
119 case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
120 default: break;
121 }
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000122 }
123
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000124 return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
125 opCode, target, canUseImmed,
126 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000127}
128
Vikram S. Adve6d353262001-10-17 23:57:50 +0000129
Chris Lattner04120772003-01-15 19:47:53 +0000130
Vikram S. Adve6d353262001-10-17 23:57:50 +0000131//---------------------------------------------------------------------------
132// Function: FixConstantOperandsForInstr
133//
134// Purpose:
135// Special handling for constant operands of a machine instruction
136// -- if the constant is 0, use the hardwired 0 register, if any;
137// -- if the constant fits in the IMMEDIATE field, use that field;
138// -- else create instructions to put the constant into a register, either
139// directly or by loading explicitly from the constant pool.
140//
141// In the first 2 cases, the operand of `minstr' is modified in place.
142// Returns a vector of machine instructions generated for operands that
143// fall under case 3; these must be inserted before `minstr'.
144//---------------------------------------------------------------------------
145
146vector<MachineInstr*>
147FixConstantOperandsForInstr(Instruction* vmInstr,
148 MachineInstr* minstr,
149 TargetMachine& target)
150{
Chris Lattner04120772003-01-15 19:47:53 +0000151 vector<MachineInstr*> MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000152
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000153 MachineOpCode opCode = minstr->getOpCode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000154 const TargetInstrInfo& instrInfo = target.getInstrInfo();
Chris Lattner8f780272002-10-29 17:25:41 +0000155 int resultPos = instrInfo.getResultPos(opCode);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000156 int immedPos = instrInfo.getImmedConstantPos(opCode);
157
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000158 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000159
Vikram S. Adve6d353262001-10-17 23:57:50 +0000160 for (unsigned op=0; op < minstr->getNumOperands(); op++)
161 {
162 const MachineOperand& mop = minstr->getOperand(op);
163
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000164 // Skip the result position, preallocated machine registers, or operands
165 // that cannot be constants (CC regs or PC-relative displacements)
Chris Lattner8f780272002-10-29 17:25:41 +0000166 if (resultPos == (int)op ||
Chris Lattner133f0792002-10-28 04:45:29 +0000167 mop.getType() == MachineOperand::MO_MachineRegister ||
168 mop.getType() == MachineOperand::MO_CCRegister ||
169 mop.getType() == MachineOperand::MO_PCRelativeDisp)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000170 continue;
171
Vikram S. Adve6d353262001-10-17 23:57:50 +0000172 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000173 unsigned int machineRegNum = 0;
174 int64_t immedValue = 0;
175 Value* opValue = NULL;
176 MachineOperand::MachineOperandType opType =
177 MachineOperand::MO_VirtualRegister;
178
179 // Operand may be a virtual register or a compile-time constant
Chris Lattner133f0792002-10-28 04:45:29 +0000180 if (mop.getType() == MachineOperand::MO_VirtualRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000181 {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000182 assert(mop.getVRegValue() != NULL);
Chris Lattnerc7c7b7a2003-01-15 20:32:15 +0000183 opValue = mop.getVRegValue();
184 if (Constant *opConst = dyn_cast<Constant>(opValue)) {
Chris Lattner04120772003-01-15 19:47:53 +0000185 opType = ChooseRegOrImmed(opConst, opCode, target,
186 (immedPos == (int)op), machineRegNum,
187 immedValue);
188 if (opType == MachineOperand::MO_VirtualRegister)
189 constantThatMustBeLoaded = true;
190 }
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000191 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000192 else
193 {
Chris Lattner04120772003-01-15 19:47:53 +0000194 assert(mop.isImmediate());
195 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000196
197 // Bit-selection flags indicate an instruction that is extracting
198 // bits from its operand so ignore this even if it is a big constant.
199 if (mop.opHiBits32() || mop.opLoBits32() ||
200 mop.opHiBits64() || mop.opLoBits64())
201 continue;
202
203 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
204 opCode, target, (immedPos == (int)op),
205 machineRegNum, immedValue);
206
Misha Brukman6fe69052003-06-07 02:34:43 +0000207 if (opType == MachineOperand::MO_SignExtendedImmed ||
208 opType == MachineOperand::MO_UnextendedImmed) {
Misha Brukmand15cd272003-06-04 04:54:06 +0000209 // The optype is an immediate value
210 // This means we need to change the opcode, e.g. ADDr -> ADDi
211 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
212 minstr->setOpcode(newOpcode);
213 }
214
Chris Lattner133f0792002-10-28 04:45:29 +0000215 if (opType == mop.getType())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000216 continue; // no change: this is the most common case
217
218 if (opType == MachineOperand::MO_VirtualRegister)
219 {
220 constantThatMustBeLoaded = true;
221 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000222 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
223 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000224 }
225 }
226
227 if (opType == MachineOperand::MO_MachineRegister)
228 minstr->SetMachineOperandReg(op, machineRegNum);
229 else if (opType == MachineOperand::MO_SignExtendedImmed ||
Misha Brukmanc740aae2003-06-03 03:18:20 +0000230 opType == MachineOperand::MO_UnextendedImmed) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000231 minstr->SetMachineOperandConst(op, opType, immedValue);
Misha Brukman6fe69052003-06-07 02:34:43 +0000232 // The optype is or has become an immediate
233 // This means we need to change the opcode, e.g. ADDr -> ADDi
234 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
235 minstr->setOpcode(newOpcode);
Misha Brukmanc740aae2003-06-03 03:18:20 +0000236 } else if (constantThatMustBeLoaded ||
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000237 (opValue && isa<GlobalValue>(opValue)))
238 { // opValue is a constant that must be explicitly loaded into a reg
239 assert(opValue);
240 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000241 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000242 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
243 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000244 }
245 }
246
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000247 // Also, check for implicit operands used by the machine instruction
248 // (no need to check those defined since they cannot be constants).
249 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000250 // -- arguments to a Call
251 // -- return value of a Return
252 // Any such operand that is a constant value needs to be fixed also.
253 // The current instructions with implicit refs (viz., Call and Return)
254 // have no immediate fields, so the constant always needs to be loaded
255 // into a register.
256 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000257 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000258 unsigned lastCallArgNum = 0; // unused if not a call
259 CallArgsDescriptor* argDesc = NULL; // unused if not a call
260 if (isCall)
261 argDesc = CallArgsDescriptor::get(minstr);
262
Vikram S. Adve6d353262001-10-17 23:57:50 +0000263 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000264 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000265 isa<GlobalValue>(minstr->getImplicitRef(i)))
266 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000267 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000268 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000269 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000270 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000271
272 if (isCall)
273 { // find and replace the argument in the CallArgsDescriptor
274 unsigned i=lastCallArgNum;
275 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
276 ++i;
277 assert(i < argDesc->getNumArgs() &&
278 "Constant operands to a call *must* be in the arg list");
279 lastCallArgNum = i;
280 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
281 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000282 }
283
Chris Lattner04120772003-01-15 19:47:53 +0000284 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000285}