2 //***************************************************************************
4 // InstrSelectionSupport.h
7 // Target-independent instruction selection code.
8 // See SparcInstrSelection.cpp for usage.
11 // 10/10/01 - Vikram Adve - Created
12 //**************************************************************************/
14 #include "llvm/CodeGen/InstrSelectionSupport.h"
15 #include "llvm/CodeGen/InstrSelection.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/MachineRegInfo.h"
19 #include "llvm/ConstPoolVals.h"
20 #include "llvm/Instruction.h"
21 #include "llvm/Type.h"
22 #include "llvm/iMemory.h"
25 //*************************** Local Functions ******************************/
28 GetSignedIntConstantValue(Value* val, bool& isValidConstant)
31 isValidConstant = false;
33 if (val->getValueType() == Value::ConstantVal)
35 switch(val->getType()->getPrimitiveID())
38 intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
39 isValidConstant = true;
45 intValue = ((ConstPoolSInt*) val)->getValue();
46 isValidConstant = true;
57 GetUnsignedIntConstantValue(Value* val, bool& isValidConstant)
59 uint64_t intValue = 0;
60 isValidConstant = false;
62 if (val->getValueType() == Value::ConstantVal)
64 switch(val->getType()->getPrimitiveID())
67 intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
68 isValidConstant = true;
71 case Type::UShortTyID:
74 intValue = ((ConstPoolUInt*) val)->getValue();
75 isValidConstant = true;
87 GetConstantValueAsSignedInt(Value* val, bool& isValidConstant)
91 if (val->getType()->isSigned())
93 intValue = GetSignedIntConstantValue(val, isValidConstant);
95 else // non-numeric types will fall here
97 uint64_t uintValue = GetUnsignedIntConstantValue(val, isValidConstant);
98 if (isValidConstant && uintValue < INT64_MAX) // safe to use signed
99 intValue = (int64_t) uintValue;
101 isValidConstant = false;
108 //---------------------------------------------------------------------------
109 // Function: FoldGetElemChain
112 // Fold a chain of GetElementPtr instructions into an equivalent
113 // (Pointer, IndexVector) pair. Returns the pointer Value, and
114 // stores the resulting IndexVector in argument chainIdxVec.
115 //---------------------------------------------------------------------------
118 FoldGetElemChain(const InstructionNode* getElemInstrNode,
119 vector<ConstPoolVal*>& chainIdxVec)
121 MemAccessInst* getElemInst = (MemAccessInst*)
122 getElemInstrNode->getInstruction();
124 // Initialize return values from the incoming instruction
125 Value* ptrVal = getElemInst->getPtrOperand();
126 chainIdxVec = getElemInst->getIndexVec(); // copies index vector values
128 // Now chase the chain of getElementInstr instructions, if any
129 InstrTreeNode* ptrChild = getElemInstrNode->leftChild();
130 while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
131 ptrChild->getOpLabel() == GetElemPtrIdx)
133 // Child is a GetElemPtr instruction
134 getElemInst = (MemAccessInst*)
135 ((InstructionNode*) ptrChild)->getInstruction();
136 const vector<ConstPoolVal*>& idxVec = getElemInst->getIndexVec();
138 // Get the pointer value out of ptrChild and *prepend* its index vector
139 ptrVal = getElemInst->getPtrOperand();
140 chainIdxVec.insert(chainIdxVec.begin(), idxVec.begin(), idxVec.end());
142 ptrChild = ptrChild->leftChild();
149 //------------------------------------------------------------------------
150 // Function Set2OperandsFromInstr
151 // Function Set3OperandsFromInstr
153 // For the common case of 2- and 3-operand arithmetic/logical instructions,
154 // set the m/c instr. operands directly from the VM instruction's operands.
155 // Check whether the first or second operand is 0 and can use a dedicated "0"
157 // Check whether the second operand should use an immediate field or register.
158 // (First and third operands are never immediates for such instructions.)
161 // canDiscardResult: Specifies that the result operand can be discarded
162 // by using the dedicated "0"
164 // op1position, op2position and resultPosition: Specify in which position
165 // in the machine instruction the 3 operands (arg1, arg2
166 // and result) should go.
168 // RETURN VALUE: unsigned int flags, where
169 // flags & 0x01 => operand 1 is constant and needs a register
170 // flags & 0x02 => operand 2 is constant and needs a register
171 //------------------------------------------------------------------------
174 Set2OperandsFromInstr(MachineInstr* minstr,
175 InstructionNode* vmInstrNode,
176 const TargetMachine& target,
177 bool canDiscardResult,
181 Set3OperandsFromInstr(minstr, vmInstrNode, target,
182 canDiscardResult, op1Position,
183 /*op2Position*/ -1, resultPosition);
186 #undef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
187 #ifdef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
189 Set3OperandsFromInstrJUNK(MachineInstr* minstr,
190 InstructionNode* vmInstrNode,
191 const TargetMachine& target,
192 bool canDiscardResult,
197 assert(op1Position >= 0);
198 assert(resultPosition >= 0);
200 unsigned returnFlags = 0x0;
202 // Check if operand 1 is 0. If so, try to use a hardwired 0 register.
203 Value* op1Value = vmInstrNode->leftChild()->getValue();
204 bool isValidConstant;
205 int64_t intValue = GetConstantValueAsSignedInt(op1Value, isValidConstant);
206 if (isValidConstant && intValue == 0 && target.zeroRegNum >= 0)
207 minstr->SetMachineOperand(op1Position, /*regNum*/ target.zeroRegNum);
210 if (isa<ConstPoolVal>(op1Value))
212 // value is constant and must be loaded from constant pool
213 returnFlags = returnFlags | (1 << op1Position);
215 minstr->SetMachineOperand(op1Position, MachineOperand::MO_VirtualRegister,
219 // Check if operand 2 (if any) fits in the immed. field of the instruction,
220 // or if it is 0 and can use a dedicated machine register
221 if (op2Position >= 0)
223 Value* op2Value = vmInstrNode->rightChild()->getValue();
225 unsigned int machineRegNum;
227 MachineOperand::MachineOperandType
228 op2type = ChooseRegOrImmed(op2Value, minstr->getOpCode(), target,
229 /*canUseImmed*/ true,
230 machineRegNum, immedValue);
232 if (op2type == MachineOperand::MO_MachineRegister)
233 minstr->SetMachineOperand(op2Position, machineRegNum);
234 else if (op2type == MachineOperand::MO_VirtualRegister)
236 if (isa<ConstPoolVal>(op2Value))
238 // value is constant and must be loaded from constant pool
239 returnFlags = returnFlags | (1 << op2Position);
241 minstr->SetMachineOperand(op2Position, op2type, op2Value);
245 assert(op2type != MO_CCRegister);
246 minstr->SetMachineOperand(op2Position, op2type, immedValue);
250 // If operand 3 (result) can be discarded, use a dead register if one exists
251 if (canDiscardResult && target.zeroRegNum >= 0)
252 minstr->SetMachineOperand(resultPosition, target.zeroRegNum);
254 minstr->SetMachineOperand(resultPosition,
255 MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
263 Set3OperandsFromInstr(MachineInstr* minstr,
264 InstructionNode* vmInstrNode,
265 const TargetMachine& target,
266 bool canDiscardResult,
271 assert(op1Position >= 0);
272 assert(resultPosition >= 0);
275 minstr->SetMachineOperand(op1Position, MachineOperand::MO_VirtualRegister,
276 vmInstrNode->leftChild()->getValue());
278 // operand 2 (if any)
279 if (op2Position >= 0)
280 minstr->SetMachineOperand(op2Position, MachineOperand::MO_VirtualRegister,
281 vmInstrNode->rightChild()->getValue());
283 // result operand: if it can be discarded, use a dead register if one exists
284 if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0)
285 minstr->SetMachineOperand(resultPosition,
286 target.getRegInfo().getZeroRegNum());
288 minstr->SetMachineOperand(resultPosition,
289 MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
293 MachineOperand::MachineOperandType
294 ChooseRegOrImmed(Value* val,
295 MachineOpCode opCode,
296 const TargetMachine& target,
298 unsigned int& getMachineRegNum,
299 int64_t& getImmedValue)
301 MachineOperand::MachineOperandType opType =
302 MachineOperand::MO_VirtualRegister;
303 getMachineRegNum = 0;
306 // Check for the common case first: argument is not constant
308 ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(val);
309 if (!CPV) return opType;
311 if (CPV->getType() == Type::BoolTy)
313 ConstPoolBool *CPB = (ConstPoolBool*)CPV;
314 if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0)
316 getMachineRegNum = target.getRegInfo().getZeroRegNum();
317 return MachineOperand::MO_MachineRegister;
321 return MachineOperand::MO_SignExtendedImmed;
324 if (!CPV->getType()->isIntegral()) return opType;
326 // Now get the constant value and check if it fits in the IMMED field.
327 // Take advantage of the fact that the max unsigned value will rarely
328 // fit into any IMMED field and ignore that case (i.e., cast smaller
329 // unsigned constants to signed).
332 if (CPV->getType()->isSigned())
334 intValue = ((ConstPoolSInt*)CPV)->getValue();
338 uint64_t V = ((ConstPoolUInt*)CPV)->getValue();
339 if (V >= INT64_MAX) return opType;
340 intValue = (int64_t)V;
343 if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
345 opType = MachineOperand::MO_MachineRegister;
346 getMachineRegNum = target.getRegInfo().getZeroRegNum();
348 else if (canUseImmed &&
349 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
351 opType = MachineOperand::MO_SignExtendedImmed;
352 getImmedValue = intValue;