blob: 91230230e3b9d41ffd78c5856a20910174a09655 [file] [log] [blame]
Vikram S. Advea1d14f32001-10-10 20:50:43 +00001//***************************************************************************
2// File:
3// InstrSelectionSupport.h
4//
5// Purpose:
6// Target-independent instruction selection code.
7// See SparcInstrSelection.cpp for usage.
8//
9// History:
10// 10/10/01 - Vikram Adve - Created
11//**************************************************************************/
12
13#include "llvm/CodeGen/InstrSelectionSupport.h"
14#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000015#include "llvm/CodeGen/MachineInstr.h"
16#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
18#include "llvm/CodeGen/MachineCodeForMethod.h"
19#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/MachineRegInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Vikram S. Adve94e40ef2001-10-28 21:46:23 +000024#include "llvm/BasicBlock.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000025#include "llvm/Type.h"
26#include "llvm/iMemory.h"
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::vector;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000028
29//*************************** Local Functions ******************************/
30
Vikram S. Advea1d14f32001-10-10 20:50:43 +000031
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000032// Generate code to load the constant into a TmpInstruction (virtual reg) and
33// returns the virtual register.
34//
Vikram S. Adve6d353262001-10-17 23:57:50 +000035static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000036InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000037 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000038 Instruction* vmInstr,
39 vector<MachineInstr*>& loadConstVec,
40 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000041{
Vikram S. Adve6d353262001-10-17 23:57:50 +000042 // Create a tmp virtual register to hold the constant.
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000043 TmpInstruction* tmpReg = new TmpInstruction(opValue);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000044 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
45 mcfi.addTemp(tmpReg);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000046
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000047 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
48 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000049
50 // Record the mapping from the tmp VM instruction to machine instruction.
51 // Do this for all machine instructions that were not mapped to any
52 // other temp values created by
53 // tmpReg->addMachineInstruction(loadConstVec.back());
54
55 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000056}
57
58
Vikram S. Adve6d353262001-10-17 23:57:50 +000059//---------------------------------------------------------------------------
60// Function GetConstantValueAsSignedInt
61//
62// Convenience function to get the value of an integer constant, for an
63// appropriate integer or non-integer type that can be held in an integer.
64// The type of the argument must be the following:
65// Signed or unsigned integer
66// Boolean
67// Pointer
68//
69// isValidConstant is set to true if a valid constant was found.
70//---------------------------------------------------------------------------
71
72int64_t
73GetConstantValueAsSignedInt(const Value *V,
74 bool &isValidConstant)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000075{
Chris Lattnere9bb2df2001-12-03 22:26:30 +000076 if (!isa<Constant>(V))
Vikram S. Advea1d14f32001-10-10 20:50:43 +000077 {
Vikram S. Adve6d353262001-10-17 23:57:50 +000078 isValidConstant = false;
79 return 0;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000080 }
81
Vikram S. Adve6d353262001-10-17 23:57:50 +000082 isValidConstant = true;
83
84 if (V->getType() == Type::BoolTy)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000085 return (int64_t) cast<ConstantBool>(V)->getValue();
Vikram S. Adve6d353262001-10-17 23:57:50 +000086
87 if (V->getType()->isIntegral())
88 {
89 if (V->getType()->isSigned())
Chris Lattnere9bb2df2001-12-03 22:26:30 +000090 return cast<ConstantSInt>(V)->getValue();
Vikram S. Adve6d353262001-10-17 23:57:50 +000091
92 assert(V->getType()->isUnsigned());
Chris Lattnere9bb2df2001-12-03 22:26:30 +000093 uint64_t Val = cast<ConstantUInt>(V)->getValue();
Vikram S. Adve6d353262001-10-17 23:57:50 +000094 if (Val < INT64_MAX) // then safe to cast to signed
95 return (int64_t)Val;
96 }
Vikram S. Adve17927792002-03-31 18:56:51 +000097
Vikram S. Adve6d353262001-10-17 23:57:50 +000098 isValidConstant = false;
99 return 0;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000100}
101
102
103//---------------------------------------------------------------------------
104// Function: FoldGetElemChain
105//
106// Purpose:
Vikram S. Advec941b872002-03-24 03:37:53 +0000107// Fold a chain of GetElementPtr instructions containing only
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000108// constant offsets into an equivalent (Pointer, IndexVector) pair.
Vikram S. Advec941b872002-03-24 03:37:53 +0000109// Returns the pointer Value, and stores the resulting IndexVector
110// in argument chainIdxVec.
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000111//---------------------------------------------------------------------------
112
113Value*
114FoldGetElemChain(const InstructionNode* getElemInstrNode,
Vikram S. Advefa248972001-12-15 00:36:32 +0000115 vector<Value*>& chainIdxVec)
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000116{
117 MemAccessInst* getElemInst = (MemAccessInst*)
118 getElemInstrNode->getInstruction();
119
Vikram S. Adve17927792002-03-31 18:56:51 +0000120 // Return NULL if we don't fold any instructions in.
Vikram S. Advec941b872002-03-24 03:37:53 +0000121 Value* ptrVal = NULL;
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000122
123 // Remember if the last instruction had a leading [0] index.
124 bool hasLeadingZero = false;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000125
Vikram S. Advec941b872002-03-24 03:37:53 +0000126 // Now chase the chain of getElementInstr instructions, if any.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000127 // Check for any non-constant indices and stop there.
Vikram S. Advec941b872002-03-24 03:37:53 +0000128 //
129 const InstrTreeNode* ptrChild = getElemInstrNode;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000130 while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
131 ptrChild->getOpLabel() == GetElemPtrIdx)
132 {
133 // Child is a GetElemPtr instruction
134 getElemInst = (MemAccessInst*)
135 ((InstructionNode*) ptrChild)->getInstruction();
Vikram S. Advefa248972001-12-15 00:36:32 +0000136 const vector<Value*>& idxVec = getElemInst->copyIndices();
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000137 bool allConstantOffsets = true;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000138
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000139 // Check for a leading [0] index, if any. It will be discarded later.
140 ConstantUInt* CV = dyn_cast<ConstantUInt>(idxVec[0]);
141 hasLeadingZero = bool(CV && CV->getType() == Type::UIntTy &&
142 (CV->getValue() == 0));
Vikram S. Advec941b872002-03-24 03:37:53 +0000143
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000144 // Check that all offsets are constant for this instruction
145 for (unsigned int i=0; i < idxVec.size(); i++)
146 if (! isa<ConstantUInt>(idxVec[i]))
147 {
148 allConstantOffsets = false;
149 break;
150 }
Vikram S. Advec941b872002-03-24 03:37:53 +0000151
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000152 if (allConstantOffsets)
Vikram S. Adve17927792002-03-31 18:56:51 +0000153 { // Get pointer value out of ptrChild.
Vikram S. Advec941b872002-03-24 03:37:53 +0000154 ptrVal = getElemInst->getPointerOperand();
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000155
156 // Insert its index vector at the start.
157 chainIdxVec.insert(chainIdxVec.begin(),
158 idxVec.begin() + (hasLeadingZero? 1:0),
159 idxVec.end());
Vikram S. Adve17927792002-03-31 18:56:51 +0000160
161 // Mark the folded node so no code is generated for it.
Vikram S. Advec941b872002-03-24 03:37:53 +0000162 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Vikram S. Advec941b872002-03-24 03:37:53 +0000163 }
164 else // cannot fold this getElementPtr instr. or any further ones
165 break;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000166
167 ptrChild = ptrChild->leftChild();
168 }
169
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000170 // If the first getElementPtr instruction had a leading [0], add it back.
171 // Note that this instruction is the *last* one handled above.
172 if (hasLeadingZero)
173 chainIdxVec.insert(chainIdxVec.begin(), ConstantUInt::get(Type::UIntTy,0));
174
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000175 return ptrVal;
176}
177
178
179//------------------------------------------------------------------------
180// Function Set2OperandsFromInstr
181// Function Set3OperandsFromInstr
182//
183// For the common case of 2- and 3-operand arithmetic/logical instructions,
184// set the m/c instr. operands directly from the VM instruction's operands.
185// Check whether the first or second operand is 0 and can use a dedicated "0"
186// register.
187// Check whether the second operand should use an immediate field or register.
188// (First and third operands are never immediates for such instructions.)
189//
190// Arguments:
191// canDiscardResult: Specifies that the result operand can be discarded
192// by using the dedicated "0"
193//
194// op1position, op2position and resultPosition: Specify in which position
195// in the machine instruction the 3 operands (arg1, arg2
196// and result) should go.
197//
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000198//------------------------------------------------------------------------
199
200void
201Set2OperandsFromInstr(MachineInstr* minstr,
202 InstructionNode* vmInstrNode,
203 const TargetMachine& target,
204 bool canDiscardResult,
205 int op1Position,
206 int resultPosition)
207{
208 Set3OperandsFromInstr(minstr, vmInstrNode, target,
209 canDiscardResult, op1Position,
210 /*op2Position*/ -1, resultPosition);
211}
212
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000213
214void
215Set3OperandsFromInstr(MachineInstr* minstr,
216 InstructionNode* vmInstrNode,
217 const TargetMachine& target,
218 bool canDiscardResult,
219 int op1Position,
220 int op2Position,
221 int resultPosition)
222{
223 assert(op1Position >= 0);
224 assert(resultPosition >= 0);
225
226 // operand 1
Vikram S. Adve42f63202002-03-18 03:33:43 +0000227 minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000228 vmInstrNode->leftChild()->getValue());
229
230 // operand 2 (if any)
231 if (op2Position >= 0)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000232 minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000233 vmInstrNode->rightChild()->getValue());
234
235 // result operand: if it can be discarded, use a dead register if one exists
236 if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000237 minstr->SetMachineOperandReg(resultPosition,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000238 target.getRegInfo().getZeroRegNum());
239 else
Vikram S. Adve42f63202002-03-18 03:33:43 +0000240 minstr->SetMachineOperandVal(resultPosition,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000241 MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
242}
243
244
245MachineOperand::MachineOperandType
246ChooseRegOrImmed(Value* val,
247 MachineOpCode opCode,
248 const TargetMachine& target,
249 bool canUseImmed,
250 unsigned int& getMachineRegNum,
251 int64_t& getImmedValue)
252{
253 MachineOperand::MachineOperandType opType =
254 MachineOperand::MO_VirtualRegister;
255 getMachineRegNum = 0;
256 getImmedValue = 0;
257
258 // Check for the common case first: argument is not constant
259 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000260 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000261 if (!CPV) return opType;
262
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000263 if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV))
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000264 {
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000265 if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0)
266 {
267 getMachineRegNum = target.getRegInfo().getZeroRegNum();
268 return MachineOperand::MO_MachineRegister;
269 }
270
271 getImmedValue = 1;
272 return MachineOperand::MO_SignExtendedImmed;
273 }
274
Vikram S. Advec8117452001-11-14 17:24:49 +0000275 // Otherwise it needs to be an integer or a NULL pointer
276 if (! CPV->getType()->isIntegral() &&
Chris Lattner9b625032002-05-06 16:15:30 +0000277 ! (isa<PointerType>(CPV->getType()) &&
Vikram S. Advec8117452001-11-14 17:24:49 +0000278 CPV->isNullValue()))
279 return opType;
280
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000281 // Now get the constant value and check if it fits in the IMMED field.
282 // Take advantage of the fact that the max unsigned value will rarely
283 // fit into any IMMED field and ignore that case (i.e., cast smaller
284 // unsigned constants to signed).
285 //
286 int64_t intValue;
Chris Lattner9b625032002-05-06 16:15:30 +0000287 if (isa<PointerType>(CPV->getType()))
Vikram S. Advec8117452001-11-14 17:24:49 +0000288 {
289 intValue = 0;
290 }
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000291 else if (CPV->getType()->isSigned())
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000292 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000294 }
295 else
296 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000297 uint64_t V = cast<ConstantUInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000298 if (V >= INT64_MAX) return opType;
299 intValue = (int64_t)V;
300 }
301
302 if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
303 {
304 opType = MachineOperand::MO_MachineRegister;
305 getMachineRegNum = target.getRegInfo().getZeroRegNum();
306 }
307 else if (canUseImmed &&
308 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
309 {
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000310 opType = CPV->getType()->isSigned()
311 ? MachineOperand::MO_SignExtendedImmed
312 : MachineOperand::MO_UnextendedImmed;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000313 getImmedValue = intValue;
314 }
315
316 return opType;
317}
318
Vikram S. Adve6d353262001-10-17 23:57:50 +0000319
320//---------------------------------------------------------------------------
321// Function: FixConstantOperandsForInstr
322//
323// Purpose:
324// Special handling for constant operands of a machine instruction
325// -- if the constant is 0, use the hardwired 0 register, if any;
326// -- if the constant fits in the IMMEDIATE field, use that field;
327// -- else create instructions to put the constant into a register, either
328// directly or by loading explicitly from the constant pool.
329//
330// In the first 2 cases, the operand of `minstr' is modified in place.
331// Returns a vector of machine instructions generated for operands that
332// fall under case 3; these must be inserted before `minstr'.
333//---------------------------------------------------------------------------
334
335vector<MachineInstr*>
336FixConstantOperandsForInstr(Instruction* vmInstr,
337 MachineInstr* minstr,
338 TargetMachine& target)
339{
340 vector<MachineInstr*> loadConstVec;
341
342 const MachineInstrDescriptor& instrDesc =
343 target.getInstrInfo().getDescriptor(minstr->getOpCode());
344
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000345 Function *F = vmInstr->getParent()->getParent();
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000346
Vikram S. Adve6d353262001-10-17 23:57:50 +0000347 for (unsigned op=0; op < minstr->getNumOperands(); op++)
348 {
349 const MachineOperand& mop = minstr->getOperand(op);
350
351 // skip the result position (for efficiency below) and any other
352 // positions already marked as not a virtual register
353 if (instrDesc.resultPos == (int) op ||
354 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
355 mop.getVRegValue() == NULL)
356 {
357 continue;
358 }
359
360 Value* opValue = mop.getVRegValue();
361 bool constantThatMustBeLoaded = false;
362
Vikram S. Adve42f63202002-03-18 03:33:43 +0000363 if (Constant *opConst = dyn_cast<Constant>(opValue))
364 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000365 unsigned int machineRegNum;
366 int64_t immedValue;
367 MachineOperand::MachineOperandType opType =
368 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
Vikram S. Adve42f63202002-03-18 03:33:43 +0000369 (target.getInstrInfo().getImmedConstantPos(minstr->getOpCode()) == (int) op),
Vikram S. Adve6d353262001-10-17 23:57:50 +0000370 machineRegNum, immedValue);
Vikram S. Adveecd58132001-11-14 18:49:45 +0000371
Vikram S. Adve6d353262001-10-17 23:57:50 +0000372 if (opType == MachineOperand::MO_MachineRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000373 minstr->SetMachineOperandReg(op, machineRegNum);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000374 else if (opType == MachineOperand::MO_VirtualRegister)
375 constantThatMustBeLoaded = true; // load is generated below
376 else
Vikram S. Adve42f63202002-03-18 03:33:43 +0000377 minstr->SetMachineOperandConst(op, opType, immedValue);
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000378 }
379
Vikram S. Adve6d353262001-10-17 23:57:50 +0000380 if (constantThatMustBeLoaded || isa<GlobalValue>(opValue))
381 { // opValue is a constant that must be explicitly loaded into a reg.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000382 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue,vmInstr,
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000383 loadConstVec,
384 target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000385 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
386 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000387 }
388 }
389
390 //
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000391 // Also, check for implicit operands used by the machine instruction
392 // (no need to check those defined since they cannot be constants).
393 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000394 // -- arguments to a Call
395 // -- return value of a Return
396 // Any such operand that is a constant value needs to be fixed also.
397 // The current instructions with implicit refs (viz., Call and Return)
398 // have no immediate fields, so the constant always needs to be loaded
399 // into a register.
400 //
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000401 bool isCall = target.getInstrInfo().isCall(minstr->getOpCode());
402 unsigned lastCallArgNum = 0; // unused if not a call
403 CallArgsDescriptor* argDesc = NULL; // unused if not a call
404 if (isCall)
405 argDesc = CallArgsDescriptor::get(minstr);
406
Vikram S. Adve6d353262001-10-17 23:57:50 +0000407 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000408 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000409 isa<GlobalValue>(minstr->getImplicitRef(i)))
410 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000411 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000412 TmpInstruction* tmpReg =
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000413 InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000414 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000415
416 if (isCall)
417 { // find and replace the argument in the CallArgsDescriptor
418 unsigned i=lastCallArgNum;
419 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
420 ++i;
421 assert(i < argDesc->getNumArgs() &&
422 "Constant operands to a call *must* be in the arg list");
423 lastCallArgNum = i;
424 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
425 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000426 }
427
428 return loadConstVec;
429}
430
431