blob: a5a3662e937c062dcb7896b169be1012d4c2984f [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.
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000036 TmpInstruction* tmpReg = new TmpInstruction(opValue);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000037 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
38 mcfi.addTemp(tmpReg);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000039
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000040 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
41 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000042
43 // Record the mapping from the tmp VM instruction to machine instruction.
44 // Do this for all machine instructions that were not mapped to any
45 // other temp values created by
46 // tmpReg->addMachineInstruction(loadConstVec.back());
47
48 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000049}
50
51
Vikram S. Advea1d14f32001-10-10 20:50:43 +000052MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +000053ChooseRegOrImmed(int64_t intValue,
54 bool isSigned,
55 MachineOpCode opCode,
56 const TargetMachine& target,
57 bool canUseImmed,
58 unsigned int& getMachineRegNum,
59 int64_t& getImmedValue)
60{
61 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
62 getMachineRegNum = 0;
63 getImmedValue = 0;
64
65 if (canUseImmed &&
Chris Lattner795ba6c2003-01-15 21:36:50 +000066 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
Vikram S. Advefd0ec802002-09-16 15:15:57 +000067 {
68 opType = isSigned? MachineOperand::MO_SignExtendedImmed
69 : MachineOperand::MO_UnextendedImmed;
70 getImmedValue = intValue;
71 }
72 else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
73 {
74 opType = MachineOperand::MO_MachineRegister;
75 getMachineRegNum = target.getRegInfo().getZeroRegNum();
76 }
77
78 return opType;
79}
80
81
82MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +000083ChooseRegOrImmed(Value* val,
84 MachineOpCode opCode,
85 const TargetMachine& target,
86 bool canUseImmed,
87 unsigned int& getMachineRegNum,
88 int64_t& getImmedValue)
89{
Vikram S. Advea1d14f32001-10-10 20:50:43 +000090 getMachineRegNum = 0;
91 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +000092
93 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Chris Lattnere9bb2df2001-12-03 22:26:30 +000094 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Advefd0ec802002-09-16 15:15:57 +000095 if (CPV == NULL ||
96 (! CPV->getType()->isIntegral() &&
97 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
98 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;
Misha Brukman88ba2542003-05-30 20:36:27 +0000190 else {
191 // The optype has changed from being a register to an immediate
192 // This means we need to change the opcode, e.g. ADDr -> ADDi
193 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
194 minstr->setOpcode(newOpcode);
195 }
Chris Lattner04120772003-01-15 19:47:53 +0000196 }
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000197 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000198 else
199 {
Chris Lattner04120772003-01-15 19:47:53 +0000200 assert(mop.isImmediate());
201 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000202
203 // Bit-selection flags indicate an instruction that is extracting
204 // bits from its operand so ignore this even if it is a big constant.
205 if (mop.opHiBits32() || mop.opLoBits32() ||
206 mop.opHiBits64() || mop.opLoBits64())
207 continue;
208
209 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
210 opCode, target, (immedPos == (int)op),
211 machineRegNum, immedValue);
212
Chris Lattner133f0792002-10-28 04:45:29 +0000213 if (opType == mop.getType())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000214 continue; // no change: this is the most common case
215
216 if (opType == MachineOperand::MO_VirtualRegister)
217 {
218 constantThatMustBeLoaded = true;
219 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000220 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
221 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000222 }
Misha Brukman88ba2542003-05-30 20:36:27 +0000223 else
224 {
225 // The optype has changed from being a register to an immediate
226 // This means we need to change the opcode, e.g. ADDr -> ADDi
227 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
228 minstr->setOpcode(newOpcode);
229 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000230 }
231
232 if (opType == MachineOperand::MO_MachineRegister)
233 minstr->SetMachineOperandReg(op, machineRegNum);
234 else if (opType == MachineOperand::MO_SignExtendedImmed ||
235 opType == MachineOperand::MO_UnextendedImmed)
236 minstr->SetMachineOperandConst(op, opType, immedValue);
237 else if (constantThatMustBeLoaded ||
238 (opValue && isa<GlobalValue>(opValue)))
239 { // opValue is a constant that must be explicitly loaded into a reg
240 assert(opValue);
241 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000242 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000243 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
244 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000245 }
246 }
247
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000248 // Also, check for implicit operands used by the machine instruction
249 // (no need to check those defined since they cannot be constants).
250 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000251 // -- arguments to a Call
252 // -- return value of a Return
253 // Any such operand that is a constant value needs to be fixed also.
254 // The current instructions with implicit refs (viz., Call and Return)
255 // have no immediate fields, so the constant always needs to be loaded
256 // into a register.
257 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000258 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000259 unsigned lastCallArgNum = 0; // unused if not a call
260 CallArgsDescriptor* argDesc = NULL; // unused if not a call
261 if (isCall)
262 argDesc = CallArgsDescriptor::get(minstr);
263
Vikram S. Adve6d353262001-10-17 23:57:50 +0000264 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000265 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000266 isa<GlobalValue>(minstr->getImplicitRef(i)))
267 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000268 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000269 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000270 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000271 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000272
273 if (isCall)
274 { // find and replace the argument in the CallArgsDescriptor
275 unsigned i=lastCallArgNum;
276 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
277 ++i;
278 assert(i < argDesc->getNumArgs() &&
279 "Constant operands to a call *must* be in the arg list");
280 lastCallArgNum = i;
281 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
282 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000283 }
284
Chris Lattner04120772003-01-15 19:47:53 +0000285 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000286}