1 //===-- InstrSelectionSupport.cpp -----------------------------------------===//
3 // Target-independent instruction selection code. See SparcInstrSelection.cpp
6 //===----------------------------------------------------------------------===//
8 #include "llvm/CodeGen/InstrSelectionSupport.h"
9 #include "llvm/CodeGen/InstrSelection.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/CodeGen/MachineInstrAnnot.h"
12 #include "llvm/CodeGen/MachineCodeForInstruction.h"
13 #include "llvm/CodeGen/MachineCodeForMethod.h"
14 #include "llvm/CodeGen/InstrForest.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/Target/MachineRegInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/Type.h"
20 #include "llvm/iMemory.h"
23 //*************************** Local Functions ******************************/
26 // Generate code to load the constant into a TmpInstruction (virtual reg) and
27 // returns the virtual register.
29 static TmpInstruction*
30 InsertCodeToLoadConstant(Function *F,
33 vector<MachineInstr*>& loadConstVec,
34 TargetMachine& target)
36 // Create a tmp virtual register to hold the constant.
37 TmpInstruction* tmpReg = new TmpInstruction(opValue);
38 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
41 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
44 // Record the mapping from the tmp VM instruction to machine instruction.
45 // Do this for all machine instructions that were not mapped to any
46 // other temp values created by
47 // tmpReg->addMachineInstruction(loadConstVec.back());
53 //---------------------------------------------------------------------------
54 // Function GetConstantValueAsUnsignedInt
55 // Function GetConstantValueAsSignedInt
57 // Convenience functions to get the value of an integral constant, for an
58 // appropriate integer or non-integer type that can be held in a signed
59 // or unsigned integer respectively. The type of the argument must be
61 // Signed or unsigned integer
65 // isValidConstant is set to true if a valid constant was found.
66 //---------------------------------------------------------------------------
69 GetConstantValueAsUnsignedInt(const Value *V,
70 bool &isValidConstant)
72 isValidConstant = true;
75 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
76 return (int64_t)CB->getValue();
77 else if (const ConstantSInt *CS = dyn_cast<ConstantSInt>(V))
78 return (uint64_t)CS->getValue();
79 else if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(V))
80 return CU->getValue();
82 isValidConstant = false;
87 GetConstantValueAsSignedInt(const Value *V,
88 bool &isValidConstant)
90 uint64_t C = GetConstantValueAsUnsignedInt(V, isValidConstant);
91 if (isValidConstant) {
92 if (V->getType()->isSigned() || C < INT64_MAX) // safe to cast to signed
95 isValidConstant = false;
101 //---------------------------------------------------------------------------
102 // Function: FoldGetElemChain
105 // Fold a chain of GetElementPtr instructions containing only
106 // constant offsets into an equivalent (Pointer, IndexVector) pair.
107 // Returns the pointer Value, and stores the resulting IndexVector
108 // in argument chainIdxVec. This is a helper function for
109 // FoldConstantIndices that does the actual folding.
110 //---------------------------------------------------------------------------
113 // Check for a constant 0.
117 return (idx == ConstantSInt::getNullValue(idx->getType()));
121 FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec,
122 bool lastInstHasLeadingNonZero)
124 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
125 GetElementPtrInst* gepInst =
126 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
128 // ptr value is not computed in this tree or ptr value does not come from GEP
133 // Return NULL if we don't fold any instructions in.
134 Value* ptrVal = NULL;
136 // Now chase the chain of getElementInstr instructions, if any.
137 // Check for any non-constant indices and stop there.
138 // Also, stop if the first index of child is a non-zero array index
139 // and the last index of the current node is a non-array index:
140 // in that case, a non-array declared type is being accessed as an array
141 // which is not type-safe, but could be legal.
143 InstructionNode* ptrChild = gepNode;
144 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
145 ptrChild->getOpLabel() == GetElemPtrIdx))
147 // Child is a GetElemPtr instruction
148 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
149 User::op_iterator OI, firstIdx = gepInst->idx_begin();
150 User::op_iterator lastIdx = gepInst->idx_end();
151 bool allConstantOffsets = true;
153 // The first index of every GEP must be an array index.
154 assert((*firstIdx)->getType() == Type::LongTy &&
155 "INTERNAL ERROR: Structure index for a pointer type!");
157 // If the last instruction had a leading non-zero index,
158 // check if the current one ends with an array index. If not,
159 // the code is not type-safe and we would create an illegal GEP
160 // by folding them, so don't fold any more instructions.
162 if (lastInstHasLeadingNonZero)
163 if (firstIdx != lastIdx && (*(lastIdx-1))->getType() != Type::LongTy)
164 break; // cannot fold in any preceding getElementPtr instrs.
166 // Check that all offsets are constant for this instruction
167 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
168 allConstantOffsets = isa<ConstantInt>(*OI);
170 if (allConstantOffsets)
171 { // Get pointer value out of ptrChild.
172 ptrVal = gepInst->getPointerOperand();
174 // Remember if it has leading zero index: it will be discarded later.
175 lastInstHasLeadingNonZero = ! IsZero(*firstIdx);
177 // Insert its index vector at the start, skipping any leading [0]
178 chainIdxVec.insert(chainIdxVec.begin(),
179 firstIdx + !lastInstHasLeadingNonZero, lastIdx);
181 // Mark the folded node so no code is generated for it.
182 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
184 // Get the previous GEP instruction and continue trying to fold
185 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
187 else // cannot fold this getElementPtr instr. or any preceding ones
191 // If the first getElementPtr instruction had a leading [0], add it back.
192 // Note that this instruction is the *last* one successfully folded above.
193 if (ptrVal && ! lastInstHasLeadingNonZero)
194 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
200 //---------------------------------------------------------------------------
201 // Function: GetMemInstArgs
204 // Get the pointer value and the index vector for a memory operation
205 // (GetElementPtr, Load, or Store). If all indices of the given memory
206 // operation are constant, fold in constant indices in a chain of
207 // preceding GetElementPtr instructions (if any), and return the
208 // pointer value of the first instruction in the chain.
209 // All folded instructions are marked so no code is generated for them.
212 // Returns the pointer Value to use.
213 // Returns the resulting IndexVector in idxVec.
214 // Returns true/false in allConstantIndices if all indices are/aren't const.
215 //---------------------------------------------------------------------------
218 GetMemInstArgs(const InstructionNode* memInstrNode,
219 vector<Value*>& idxVec,
220 bool& allConstantIndices)
222 allConstantIndices = true;
223 Instruction* memInst = memInstrNode->getInstruction();
224 assert(idxVec.size() == 0 && "Need empty vector to return indices");
226 // If there is a GetElemPtr instruction to fold in to this instr,
227 // it must be in the left child for Load and GetElemPtr, and in the
228 // right child for Store instructions.
229 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
230 ? memInstrNode->rightChild()
231 : memInstrNode->leftChild());
233 // Default pointer is the one from the current instruction.
234 Value* ptrVal = ptrChild->getValue();
236 // GEP is the only indexed memory instruction. Extract its index vector.
237 // Also, if all indices are constant and first index is zero, try to fold
238 // in preceding GEPs with all constant indices.
239 GetElementPtrInst* gepI = dyn_cast<GetElementPtrInst>(memInst);
241 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
242 allConstantIndices && OI != OE; ++OI)
243 if (! isa<Constant>(*OI))
244 allConstantIndices = false; // note: this also terminates loop!
246 // If we have only constant indices, fold chains of constant indices
247 // in this and any preceding GetElemPtr instructions.
248 bool foldedGEPs = false;
249 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
250 if (allConstantIndices)
251 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx))
257 // Append the index vector of the current instruction, if any.
258 // Skip the leading [0] index if preceding GEPs were folded into this.
260 idxVec.insert(idxVec.end(),
261 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
267 //------------------------------------------------------------------------
268 // Function Set2OperandsFromInstr
269 // Function Set3OperandsFromInstr
271 // For the common case of 2- and 3-operand arithmetic/logical instructions,
272 // set the m/c instr. operands directly from the VM instruction's operands.
273 // Check whether the first or second operand is 0 and can use a dedicated "0"
275 // Check whether the second operand should use an immediate field or register.
276 // (First and third operands are never immediates for such instructions.)
279 // canDiscardResult: Specifies that the result operand can be discarded
280 // by using the dedicated "0"
282 // op1position, op2position and resultPosition: Specify in which position
283 // in the machine instruction the 3 operands (arg1, arg2
284 // and result) should go.
286 //------------------------------------------------------------------------
289 Set2OperandsFromInstr(MachineInstr* minstr,
290 InstructionNode* vmInstrNode,
291 const TargetMachine& target,
292 bool canDiscardResult,
296 Set3OperandsFromInstr(minstr, vmInstrNode, target,
297 canDiscardResult, op1Position,
298 /*op2Position*/ -1, resultPosition);
303 Set3OperandsFromInstr(MachineInstr* minstr,
304 InstructionNode* vmInstrNode,
305 const TargetMachine& target,
306 bool canDiscardResult,
311 assert(op1Position >= 0);
312 assert(resultPosition >= 0);
315 minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister,
316 vmInstrNode->leftChild()->getValue());
318 // operand 2 (if any)
319 if (op2Position >= 0)
320 minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister,
321 vmInstrNode->rightChild()->getValue());
323 // result operand: if it can be discarded, use a dead register if one exists
324 if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0)
325 minstr->SetMachineOperandReg(resultPosition,
326 target.getRegInfo().getZeroRegNum());
328 minstr->SetMachineOperandVal(resultPosition,
329 MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
333 MachineOperand::MachineOperandType
334 ChooseRegOrImmed(int64_t intValue,
336 MachineOpCode opCode,
337 const TargetMachine& target,
339 unsigned int& getMachineRegNum,
340 int64_t& getImmedValue)
342 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
343 getMachineRegNum = 0;
347 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
349 opType = isSigned? MachineOperand::MO_SignExtendedImmed
350 : MachineOperand::MO_UnextendedImmed;
351 getImmedValue = intValue;
353 else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
355 opType = MachineOperand::MO_MachineRegister;
356 getMachineRegNum = target.getRegInfo().getZeroRegNum();
363 MachineOperand::MachineOperandType
364 ChooseRegOrImmed(Value* val,
365 MachineOpCode opCode,
366 const TargetMachine& target,
368 unsigned int& getMachineRegNum,
369 int64_t& getImmedValue)
371 getMachineRegNum = 0;
374 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
375 Constant *CPV = dyn_cast<Constant>(val);
377 (! CPV->getType()->isIntegral() &&
378 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
379 return MachineOperand::MO_VirtualRegister;
381 // Now get the constant value and check if it fits in the IMMED field.
382 // Take advantage of the fact that the max unsigned value will rarely
383 // fit into any IMMED field and ignore that case (i.e., cast smaller
384 // unsigned constants to signed).
387 if (isa<PointerType>(CPV->getType()))
388 intValue = 0; // We checked above that it is NULL
389 else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
390 intValue = (int64_t) CB->getValue();
391 else if (CPV->getType()->isSigned())
392 intValue = cast<ConstantSInt>(CPV)->getValue();
394 { // get the int value and sign-extend if original was less than 64 bits
395 intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
396 switch(CPV->getType()->getPrimitiveID())
398 case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
399 case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
400 case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
405 return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
406 opCode, target, canUseImmed,
407 getMachineRegNum, getImmedValue);
411 //---------------------------------------------------------------------------
412 // Function: FixConstantOperandsForInstr
415 // Special handling for constant operands of a machine instruction
416 // -- if the constant is 0, use the hardwired 0 register, if any;
417 // -- if the constant fits in the IMMEDIATE field, use that field;
418 // -- else create instructions to put the constant into a register, either
419 // directly or by loading explicitly from the constant pool.
421 // In the first 2 cases, the operand of `minstr' is modified in place.
422 // Returns a vector of machine instructions generated for operands that
423 // fall under case 3; these must be inserted before `minstr'.
424 //---------------------------------------------------------------------------
426 vector<MachineInstr*>
427 FixConstantOperandsForInstr(Instruction* vmInstr,
428 MachineInstr* minstr,
429 TargetMachine& target)
431 vector<MachineInstr*> loadConstVec;
433 MachineOpCode opCode = minstr->getOpCode();
434 const MachineInstrInfo& instrInfo = target.getInstrInfo();
435 const MachineInstrDescriptor& instrDesc = instrInfo.getDescriptor(opCode);
436 int immedPos = instrInfo.getImmedConstantPos(opCode);
438 Function *F = vmInstr->getParent()->getParent();
440 for (unsigned op=0; op < minstr->getNumOperands(); op++)
442 const MachineOperand& mop = minstr->getOperand(op);
444 // Skip the result position, preallocated machine registers, or operands
445 // that cannot be constants (CC regs or PC-relative displacements)
446 if (instrDesc.resultPos == (int) op ||
447 mop.getOperandType() == MachineOperand::MO_MachineRegister ||
448 mop.getOperandType() == MachineOperand::MO_CCRegister ||
449 mop.getOperandType() == MachineOperand::MO_PCRelativeDisp)
452 bool constantThatMustBeLoaded = false;
453 unsigned int machineRegNum = 0;
454 int64_t immedValue = 0;
455 Value* opValue = NULL;
456 MachineOperand::MachineOperandType opType =
457 MachineOperand::MO_VirtualRegister;
459 // Operand may be a virtual register or a compile-time constant
460 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister)
462 assert(mop.getVRegValue() != NULL);
463 opValue = mop.getVRegValue();
464 if (Constant *opConst = dyn_cast<Constant>(opValue))
466 opType = ChooseRegOrImmed(opConst, opCode, target,
467 (immedPos == (int)op), machineRegNum, immedValue);
468 if (opType == MachineOperand::MO_VirtualRegister)
469 constantThatMustBeLoaded = true;
474 assert(mop.getOperandType() == MachineOperand::MO_SignExtendedImmed ||
475 mop.getOperandType() == MachineOperand::MO_UnextendedImmed);
477 bool isSigned = (mop.getOperandType() ==
478 MachineOperand::MO_SignExtendedImmed);
480 // Bit-selection flags indicate an instruction that is extracting
481 // bits from its operand so ignore this even if it is a big constant.
482 if (mop.opHiBits32() || mop.opLoBits32() ||
483 mop.opHiBits64() || mop.opLoBits64())
486 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
487 opCode, target, (immedPos == (int)op),
488 machineRegNum, immedValue);
490 if (opType == mop.getOperandType())
491 continue; // no change: this is the most common case
493 if (opType == MachineOperand::MO_VirtualRegister)
495 constantThatMustBeLoaded = true;
497 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
498 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
502 if (opType == MachineOperand::MO_MachineRegister)
503 minstr->SetMachineOperandReg(op, machineRegNum);
504 else if (opType == MachineOperand::MO_SignExtendedImmed ||
505 opType == MachineOperand::MO_UnextendedImmed)
506 minstr->SetMachineOperandConst(op, opType, immedValue);
507 else if (constantThatMustBeLoaded ||
508 (opValue && isa<GlobalValue>(opValue)))
509 { // opValue is a constant that must be explicitly loaded into a reg
511 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
512 loadConstVec, target);
513 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
518 // Also, check for implicit operands used by the machine instruction
519 // (no need to check those defined since they cannot be constants).
521 // -- arguments to a Call
522 // -- return value of a Return
523 // Any such operand that is a constant value needs to be fixed also.
524 // The current instructions with implicit refs (viz., Call and Return)
525 // have no immediate fields, so the constant always needs to be loaded
528 bool isCall = instrInfo.isCall(opCode);
529 unsigned lastCallArgNum = 0; // unused if not a call
530 CallArgsDescriptor* argDesc = NULL; // unused if not a call
532 argDesc = CallArgsDescriptor::get(minstr);
534 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
535 if (isa<Constant>(minstr->getImplicitRef(i)) ||
536 isa<GlobalValue>(minstr->getImplicitRef(i)))
538 Value* oldVal = minstr->getImplicitRef(i);
539 TmpInstruction* tmpReg =
540 InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, target);
541 minstr->setImplicitRef(i, tmpReg);
544 { // find and replace the argument in the CallArgsDescriptor
545 unsigned i=lastCallArgNum;
546 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
548 assert(i < argDesc->getNumArgs() &&
549 "Constant operands to a call *must* be in the arg list");
551 argDesc->getArgInfo(i).replaceArgVal(tmpReg);