blob: 6c1c860d2c6561cd37a18f4fe02f99ec43fff76e [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"
Chris Lattner18153832003-07-23 14:55:59 +000019#include "../../Target/Sparc/SparcInstrSelectionSupport.h" // FIXME!
Vikram S. Advea1d14f32001-10-10 20:50:43 +000020
Vikram S. Advea1d14f32001-10-10 20:50:43 +000021
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000022// Generate code to load the constant into a TmpInstruction (virtual reg) and
23// returns the virtual register.
24//
Vikram S. Adve6d353262001-10-17 23:57:50 +000025static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000027 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000028 Instruction* vmInstr,
Chris Lattner18153832003-07-23 14:55:59 +000029 std::vector<MachineInstr*>& loadConstVec,
Vikram S. Adve6d353262001-10-17 23:57:50 +000030 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000031{
Vikram S. Adve6d353262001-10-17 23:57:50 +000032 // Create a tmp virtual register to hold the constant.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000033 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
Vikram S. Advef3d3ca12003-05-31 07:41:24 +000034 TmpInstruction* tmpReg = new TmpInstruction(mcfi, opValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000035
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000036 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
37 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000038
39 // Record the mapping from the tmp VM instruction to machine instruction.
40 // Do this for all machine instructions that were not mapped to any
41 // other temp values created by
42 // tmpReg->addMachineInstruction(loadConstVec.back());
43
44 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000045}
46
47
Vikram S. Advea1d14f32001-10-10 20:50:43 +000048MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +000049ChooseRegOrImmed(int64_t intValue,
50 bool isSigned,
51 MachineOpCode opCode,
52 const TargetMachine& target,
53 bool canUseImmed,
54 unsigned int& getMachineRegNum,
55 int64_t& getImmedValue)
56{
57 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
58 getMachineRegNum = 0;
59 getImmedValue = 0;
60
61 if (canUseImmed &&
Chris Lattner795ba6c2003-01-15 21:36:50 +000062 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
Vikram S. Advefd0ec802002-09-16 15:15:57 +000063 {
64 opType = isSigned? MachineOperand::MO_SignExtendedImmed
65 : MachineOperand::MO_UnextendedImmed;
66 getImmedValue = intValue;
67 }
68 else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
69 {
70 opType = MachineOperand::MO_MachineRegister;
71 getMachineRegNum = target.getRegInfo().getZeroRegNum();
72 }
73
74 return opType;
75}
76
77
78MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +000079ChooseRegOrImmed(Value* val,
80 MachineOpCode opCode,
81 const TargetMachine& target,
82 bool canUseImmed,
83 unsigned int& getMachineRegNum,
84 int64_t& getImmedValue)
85{
Vikram S. Advea1d14f32001-10-10 20:50:43 +000086 getMachineRegNum = 0;
87 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +000088
89 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Chris Lattnere9bb2df2001-12-03 22:26:30 +000090 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Adve97539fc2003-07-06 20:33:21 +000091 if (CPV == NULL
92 || CPV->isConstantExpr()
93 || (! CPV->getType()->isIntegral() &&
94 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
Vikram S. Advefd0ec802002-09-16 15:15:57 +000095 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000096
Vikram S. Advea1d14f32001-10-10 20:50:43 +000097 // Now get the constant value and check if it fits in the IMMED field.
98 // Take advantage of the fact that the max unsigned value will rarely
99 // fit into any IMMED field and ignore that case (i.e., cast smaller
100 // unsigned constants to signed).
101 //
102 int64_t intValue;
Chris Lattner9b625032002-05-06 16:15:30 +0000103 if (isa<PointerType>(CPV->getType()))
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000104 intValue = 0; // We checked above that it is NULL
105 else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
106 intValue = (int64_t) CB->getValue();
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000107 else if (CPV->getType()->isSigned())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000108 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000109 else
Vikram S. Adve1c10f172002-09-27 14:26:20 +0000110 { // get the int value and sign-extend if original was less than 64 bits
111 intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
112 switch(CPV->getType()->getPrimitiveID())
113 {
114 case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
115 case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
116 case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
117 default: break;
118 }
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000119 }
120
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000121 return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
122 opCode, target, canUseImmed,
123 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000124}
125
Vikram S. Adve6d353262001-10-17 23:57:50 +0000126
Chris Lattner04120772003-01-15 19:47:53 +0000127
Vikram S. Adve6d353262001-10-17 23:57:50 +0000128//---------------------------------------------------------------------------
129// Function: FixConstantOperandsForInstr
130//
131// Purpose:
132// Special handling for constant operands of a machine instruction
133// -- if the constant is 0, use the hardwired 0 register, if any;
134// -- if the constant fits in the IMMEDIATE field, use that field;
135// -- else create instructions to put the constant into a register, either
136// directly or by loading explicitly from the constant pool.
137//
138// In the first 2 cases, the operand of `minstr' is modified in place.
139// Returns a vector of machine instructions generated for operands that
140// fall under case 3; these must be inserted before `minstr'.
141//---------------------------------------------------------------------------
142
Chris Lattner18153832003-07-23 14:55:59 +0000143std::vector<MachineInstr*>
Vikram S. Adve6d353262001-10-17 23:57:50 +0000144FixConstantOperandsForInstr(Instruction* vmInstr,
145 MachineInstr* minstr,
146 TargetMachine& target)
147{
Chris Lattner18153832003-07-23 14:55:59 +0000148 std::vector<MachineInstr*> MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000149
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000150 MachineOpCode opCode = minstr->getOpCode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000151 const TargetInstrInfo& instrInfo = target.getInstrInfo();
Chris Lattner8f780272002-10-29 17:25:41 +0000152 int resultPos = instrInfo.getResultPos(opCode);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000153 int immedPos = instrInfo.getImmedConstantPos(opCode);
154
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000155 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000156
Vikram S. Adve6d353262001-10-17 23:57:50 +0000157 for (unsigned op=0; op < minstr->getNumOperands(); op++)
158 {
159 const MachineOperand& mop = minstr->getOperand(op);
160
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000161 // Skip the result position, preallocated machine registers, or operands
162 // that cannot be constants (CC regs or PC-relative displacements)
Chris Lattner8f780272002-10-29 17:25:41 +0000163 if (resultPos == (int)op ||
Chris Lattner133f0792002-10-28 04:45:29 +0000164 mop.getType() == MachineOperand::MO_MachineRegister ||
165 mop.getType() == MachineOperand::MO_CCRegister ||
166 mop.getType() == MachineOperand::MO_PCRelativeDisp)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000167 continue;
168
Vikram S. Adve6d353262001-10-17 23:57:50 +0000169 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000170 unsigned int machineRegNum = 0;
171 int64_t immedValue = 0;
172 Value* opValue = NULL;
173 MachineOperand::MachineOperandType opType =
174 MachineOperand::MO_VirtualRegister;
175
176 // Operand may be a virtual register or a compile-time constant
Chris Lattner133f0792002-10-28 04:45:29 +0000177 if (mop.getType() == MachineOperand::MO_VirtualRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000178 {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000179 assert(mop.getVRegValue() != NULL);
Chris Lattnerc7c7b7a2003-01-15 20:32:15 +0000180 opValue = mop.getVRegValue();
181 if (Constant *opConst = dyn_cast<Constant>(opValue)) {
Chris Lattner04120772003-01-15 19:47:53 +0000182 opType = ChooseRegOrImmed(opConst, opCode, target,
183 (immedPos == (int)op), machineRegNum,
184 immedValue);
185 if (opType == MachineOperand::MO_VirtualRegister)
186 constantThatMustBeLoaded = true;
187 }
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000188 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000189 else
190 {
Chris Lattner04120772003-01-15 19:47:53 +0000191 assert(mop.isImmediate());
192 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000193
194 // Bit-selection flags indicate an instruction that is extracting
195 // bits from its operand so ignore this even if it is a big constant.
196 if (mop.opHiBits32() || mop.opLoBits32() ||
197 mop.opHiBits64() || mop.opLoBits64())
198 continue;
199
200 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
201 opCode, target, (immedPos == (int)op),
202 machineRegNum, immedValue);
203
Misha Brukman6fe69052003-06-07 02:34:43 +0000204 if (opType == MachineOperand::MO_SignExtendedImmed ||
205 opType == MachineOperand::MO_UnextendedImmed) {
Misha Brukmand15cd272003-06-04 04:54:06 +0000206 // The optype is an immediate value
207 // This means we need to change the opcode, e.g. ADDr -> ADDi
208 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
209 minstr->setOpcode(newOpcode);
210 }
211
Chris Lattner133f0792002-10-28 04:45:29 +0000212 if (opType == mop.getType())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000213 continue; // no change: this is the most common case
214
215 if (opType == MachineOperand::MO_VirtualRegister)
216 {
217 constantThatMustBeLoaded = true;
218 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000219 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
220 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000221 }
222 }
223
224 if (opType == MachineOperand::MO_MachineRegister)
225 minstr->SetMachineOperandReg(op, machineRegNum);
226 else if (opType == MachineOperand::MO_SignExtendedImmed ||
Misha Brukmanc740aae2003-06-03 03:18:20 +0000227 opType == MachineOperand::MO_UnextendedImmed) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000228 minstr->SetMachineOperandConst(op, opType, immedValue);
Misha Brukman6fe69052003-06-07 02:34:43 +0000229 // The optype is or has become an immediate
230 // This means we need to change the opcode, e.g. ADDr -> ADDi
231 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
232 minstr->setOpcode(newOpcode);
Misha Brukmanc740aae2003-06-03 03:18:20 +0000233 } else if (constantThatMustBeLoaded ||
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000234 (opValue && isa<GlobalValue>(opValue)))
235 { // opValue is a constant that must be explicitly loaded into a reg
236 assert(opValue);
237 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000238 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000239 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
240 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000241 }
242 }
243
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000244 // Also, check for implicit operands used by the machine instruction
245 // (no need to check those defined since they cannot be constants).
246 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000247 // -- arguments to a Call
248 // -- return value of a Return
249 // Any such operand that is a constant value needs to be fixed also.
250 // The current instructions with implicit refs (viz., Call and Return)
251 // have no immediate fields, so the constant always needs to be loaded
252 // into a register.
253 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000254 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000255 unsigned lastCallArgNum = 0; // unused if not a call
256 CallArgsDescriptor* argDesc = NULL; // unused if not a call
257 if (isCall)
258 argDesc = CallArgsDescriptor::get(minstr);
259
Vikram S. Adve6d353262001-10-17 23:57:50 +0000260 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000261 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000262 isa<GlobalValue>(minstr->getImplicitRef(i)))
263 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000264 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000265 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000266 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000267 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000268
269 if (isCall)
270 { // find and replace the argument in the CallArgsDescriptor
271 unsigned i=lastCallArgNum;
272 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
273 ++i;
274 assert(i < argDesc->getNumArgs() &&
275 "Constant operands to a call *must* be in the arg list");
276 lastCallArgNum = i;
277 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
278 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000279 }
280
Chris Lattner04120772003-01-15 19:47:53 +0000281 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000282}