Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 1 | //===-- InstrSelectionSupport.cpp -----------------------------------------===// |
| 2 | // |
| 3 | // Target-independent instruction selection code. See SparcInstrSelection.cpp |
| 4 | // for usage. |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 5 | // |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 6 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 7 | |
| 8 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
| 9 | #include "llvm/CodeGen/InstrSelection.h" |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineInstr.h" |
| 11 | #include "llvm/CodeGen/MachineInstrAnnot.h" |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 13 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
| 14 | #include "llvm/CodeGen/InstrForest.h" |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetMachine.h" |
| 16 | #include "llvm/Target/MachineRegInfo.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/iMemory.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 21 | using std::vector; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 22 | |
| 23 | //*************************** Local Functions ******************************/ |
| 24 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 25 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 26 | // Generate code to load the constant into a TmpInstruction (virtual reg) and |
| 27 | // returns the virtual register. |
| 28 | // |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 29 | static TmpInstruction* |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 30 | InsertCodeToLoadConstant(Function *F, |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 31 | Value* opValue, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 32 | Instruction* vmInstr, |
| 33 | vector<MachineInstr*>& loadConstVec, |
| 34 | TargetMachine& target) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 35 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 36 | // Create a tmp virtual register to hold the constant. |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 37 | TmpInstruction* tmpReg = new TmpInstruction(opValue); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 38 | MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr); |
| 39 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 40 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 41 | target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg, |
| 42 | loadConstVec, mcfi); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 43 | |
| 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()); |
| 48 | |
| 49 | return tmpReg; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 53 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 54 | // Function GetConstantValueAsUnsignedInt |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 55 | // Function GetConstantValueAsSignedInt |
| 56 | // |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 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 |
| 60 | // the following: |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 61 | // Signed or unsigned integer |
| 62 | // Boolean |
| 63 | // Pointer |
| 64 | // |
| 65 | // isValidConstant is set to true if a valid constant was found. |
| 66 | //--------------------------------------------------------------------------- |
| 67 | |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 68 | uint64_t |
| 69 | GetConstantValueAsUnsignedInt(const Value *V, |
| 70 | bool &isValidConstant) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 71 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 72 | isValidConstant = true; |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 73 | |
| 74 | if (isa<Constant>(V)) |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 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(); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 81 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 82 | isValidConstant = false; |
| 83 | return 0; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 86 | int64_t |
| 87 | GetConstantValueAsSignedInt(const Value *V, |
| 88 | bool &isValidConstant) |
| 89 | { |
| 90 | uint64_t C = GetConstantValueAsUnsignedInt(V, isValidConstant); |
| 91 | if (isValidConstant) { |
| 92 | if (V->getType()->isSigned() || C < INT64_MAX) // safe to cast to signed |
| 93 | return (int64_t) C; |
| 94 | else |
| 95 | isValidConstant = false; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 99 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 100 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 101 | //--------------------------------------------------------------------------- |
| 102 | // Function: FoldGetElemChain |
| 103 | // |
| 104 | // Purpose: |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 105 | // Fold a chain of GetElementPtr instructions containing only |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 106 | // constant offsets into an equivalent (Pointer, IndexVector) pair. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 107 | // Returns the pointer Value, and stores the resulting IndexVector |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 108 | // in argument chainIdxVec. This is a helper function for |
| 109 | // FoldConstantIndices that does the actual folding. |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 110 | //--------------------------------------------------------------------------- |
| 111 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 112 | static Value* |
| 113 | FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 114 | { |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 115 | InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode); |
| 116 | if (gepNode == NULL) |
| 117 | return NULL; // ptr value is not computed in this tree |
| 118 | |
| 119 | GetElementPtrInst* gepInst = |
| 120 | dyn_cast<GetElementPtrInst>(gepNode->getInstruction()); |
| 121 | if (gepInst == NULL) // ptr value does not come from GEP instruction |
| 122 | return NULL; |
| 123 | |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 124 | // Return NULL if we don't fold any instructions in. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 125 | Value* ptrVal = NULL; |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 126 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 127 | // Remember if the last instruction had a leading [0] index. |
| 128 | bool hasLeadingZero = false; |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 129 | |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 130 | // Now chase the chain of getElementInstr instructions, if any. |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 131 | // Check for any non-constant indices and stop there. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 132 | // |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 133 | InstructionNode* ptrChild = gepNode; |
| 134 | while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 135 | ptrChild->getOpLabel() == GetElemPtrIdx)) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 136 | { |
| 137 | // Child is a GetElemPtr instruction |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 138 | gepInst = cast<GetElementPtrInst>(ptrChild->getValue()); |
| 139 | User::op_iterator OI, firstIdx = gepInst->idx_begin(); |
| 140 | User::op_iterator lastIdx = gepInst->idx_end(); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 141 | bool allConstantOffsets = true; |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 142 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 143 | // Check that all offsets are constant for this instruction |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 144 | for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI) |
| 145 | allConstantOffsets = isa<ConstantInt>(*OI); |
| 146 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 147 | if (allConstantOffsets) |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 148 | { // Get pointer value out of ptrChild. |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 149 | ptrVal = gepInst->getPointerOperand(); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 150 | |
| 151 | // Check for a leading [0] index, if any. It will be discarded later. |
| 152 | ConstantUInt* CV = dyn_cast<ConstantUInt>((Value*) *firstIdx); |
| 153 | hasLeadingZero = bool(CV && CV->getValue() == 0); |
| 154 | |
| 155 | // Insert its index vector at the start, skipping any leading [0] |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 156 | chainIdxVec.insert(chainIdxVec.begin(), |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 157 | firstIdx + hasLeadingZero, lastIdx); |
| 158 | |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 159 | // Mark the folded node so no code is generated for it. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 160 | ((InstructionNode*) ptrChild)->markFoldedIntoParent(); |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 161 | } |
| 162 | else // cannot fold this getElementPtr instr. or any further ones |
| 163 | break; |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 164 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 165 | ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild()); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 166 | } |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 167 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 168 | // If the first getElementPtr instruction had a leading [0], add it back. |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 169 | // Note that this instruction is the *last* one successfully folded above. |
| 170 | if (ptrVal && hasLeadingZero) |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 171 | chainIdxVec.insert(chainIdxVec.begin(), ConstantUInt::get(Type::UIntTy,0)); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 172 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 173 | return ptrVal; |
| 174 | } |
| 175 | |
| 176 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 177 | //--------------------------------------------------------------------------- |
| 178 | // Function: GetMemInstArgs |
| 179 | // |
| 180 | // Purpose: |
| 181 | // Get the pointer value and the index vector for a memory operation |
| 182 | // (GetElementPtr, Load, or Store). If all indices of the given memory |
| 183 | // operation are constant, fold in constant indices in a chain of |
| 184 | // preceding GetElementPtr instructions (if any), and return the |
| 185 | // pointer value of the first instruction in the chain. |
| 186 | // All folded instructions are marked so no code is generated for them. |
| 187 | // |
| 188 | // Return values: |
| 189 | // Returns the pointer Value to use. |
| 190 | // Returns the resulting IndexVector in idxVec. |
| 191 | // Returns true/false in allConstantIndices if all indices are/aren't const. |
| 192 | //--------------------------------------------------------------------------- |
| 193 | |
| 194 | |
| 195 | // Check for a constant (uint) 0. |
| 196 | inline bool |
| 197 | IsZero(Value* idx) |
| 198 | { |
| 199 | return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue()); |
| 200 | } |
| 201 | |
| 202 | Value* |
| 203 | GetMemInstArgs(const InstructionNode* memInstrNode, |
| 204 | vector<Value*>& idxVec, |
| 205 | bool& allConstantIndices) |
| 206 | { |
| 207 | allConstantIndices = true; |
| 208 | Instruction* memInst = memInstrNode->getInstruction(); |
| 209 | |
| 210 | // If there is a GetElemPtr instruction to fold in to this instr, |
| 211 | // it must be in the left child for Load and GetElemPtr, and in the |
| 212 | // right child for Store instructions. |
| 213 | InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store |
| 214 | ? memInstrNode->rightChild() |
| 215 | : memInstrNode->leftChild()); |
| 216 | |
| 217 | // Default pointer is the one from the current instruction. |
| 218 | Value* ptrVal = ptrChild->getValue(); |
| 219 | |
| 220 | // GEP is the only indexed memory instruction. gepI is used below. |
| 221 | GetElementPtrInst* gepI = dyn_cast<GetElementPtrInst>(memInst); |
| 222 | |
| 223 | // If memInst is a GEP, check if all indices are constant for this instruction |
| 224 | if (gepI) |
| 225 | for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end(); |
| 226 | allConstantIndices && OI != OE; ++OI) |
| 227 | if (! isa<Constant>(*OI)) |
| 228 | allConstantIndices = false; // note: this also terminates loop! |
| 229 | |
| 230 | // If we have only constant indices, fold chains of constant indices |
| 231 | // in this and any preceding GetElemPtr instructions. |
| 232 | bool foldedGEPs = false; |
| 233 | if (allConstantIndices) |
| 234 | if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec)) |
| 235 | { |
| 236 | ptrVal = newPtr; |
| 237 | foldedGEPs = true; |
| 238 | assert((!gepI || IsZero(*gepI->idx_begin())) && "1st index not 0"); |
| 239 | } |
| 240 | |
| 241 | // Append the index vector of the current instruction, if any. |
| 242 | // Skip the leading [0] index if preceding GEPs were folded into this. |
| 243 | if (gepI) |
| 244 | idxVec.insert(idxVec.end(), gepI->idx_begin() +foldedGEPs, gepI->idx_end()); |
| 245 | |
| 246 | return ptrVal; |
| 247 | } |
| 248 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 249 | //------------------------------------------------------------------------ |
| 250 | // Function Set2OperandsFromInstr |
| 251 | // Function Set3OperandsFromInstr |
| 252 | // |
| 253 | // For the common case of 2- and 3-operand arithmetic/logical instructions, |
| 254 | // set the m/c instr. operands directly from the VM instruction's operands. |
| 255 | // Check whether the first or second operand is 0 and can use a dedicated "0" |
| 256 | // register. |
| 257 | // Check whether the second operand should use an immediate field or register. |
| 258 | // (First and third operands are never immediates for such instructions.) |
| 259 | // |
| 260 | // Arguments: |
| 261 | // canDiscardResult: Specifies that the result operand can be discarded |
| 262 | // by using the dedicated "0" |
| 263 | // |
| 264 | // op1position, op2position and resultPosition: Specify in which position |
| 265 | // in the machine instruction the 3 operands (arg1, arg2 |
| 266 | // and result) should go. |
| 267 | // |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 268 | //------------------------------------------------------------------------ |
| 269 | |
| 270 | void |
| 271 | Set2OperandsFromInstr(MachineInstr* minstr, |
| 272 | InstructionNode* vmInstrNode, |
| 273 | const TargetMachine& target, |
| 274 | bool canDiscardResult, |
| 275 | int op1Position, |
| 276 | int resultPosition) |
| 277 | { |
| 278 | Set3OperandsFromInstr(minstr, vmInstrNode, target, |
| 279 | canDiscardResult, op1Position, |
| 280 | /*op2Position*/ -1, resultPosition); |
| 281 | } |
| 282 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 283 | |
| 284 | void |
| 285 | Set3OperandsFromInstr(MachineInstr* minstr, |
| 286 | InstructionNode* vmInstrNode, |
| 287 | const TargetMachine& target, |
| 288 | bool canDiscardResult, |
| 289 | int op1Position, |
| 290 | int op2Position, |
| 291 | int resultPosition) |
| 292 | { |
| 293 | assert(op1Position >= 0); |
| 294 | assert(resultPosition >= 0); |
| 295 | |
| 296 | // operand 1 |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 297 | minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 298 | vmInstrNode->leftChild()->getValue()); |
| 299 | |
| 300 | // operand 2 (if any) |
| 301 | if (op2Position >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 302 | minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 303 | vmInstrNode->rightChild()->getValue()); |
| 304 | |
| 305 | // result operand: if it can be discarded, use a dead register if one exists |
| 306 | if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 307 | minstr->SetMachineOperandReg(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 308 | target.getRegInfo().getZeroRegNum()); |
| 309 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 310 | minstr->SetMachineOperandVal(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 311 | MachineOperand::MO_VirtualRegister, vmInstrNode->getValue()); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | MachineOperand::MachineOperandType |
| 316 | ChooseRegOrImmed(Value* val, |
| 317 | MachineOpCode opCode, |
| 318 | const TargetMachine& target, |
| 319 | bool canUseImmed, |
| 320 | unsigned int& getMachineRegNum, |
| 321 | int64_t& getImmedValue) |
| 322 | { |
| 323 | MachineOperand::MachineOperandType opType = |
| 324 | MachineOperand::MO_VirtualRegister; |
| 325 | getMachineRegNum = 0; |
| 326 | getImmedValue = 0; |
| 327 | |
| 328 | // Check for the common case first: argument is not constant |
| 329 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 330 | Constant *CPV = dyn_cast<Constant>(val); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 331 | if (!CPV) return opType; |
| 332 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 333 | if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV)) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 334 | { |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 335 | if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0) |
| 336 | { |
| 337 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 338 | return MachineOperand::MO_MachineRegister; |
| 339 | } |
| 340 | |
| 341 | getImmedValue = 1; |
| 342 | return MachineOperand::MO_SignExtendedImmed; |
| 343 | } |
| 344 | |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 345 | // Otherwise it needs to be an integer or a NULL pointer |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 346 | if (! CPV->getType()->isInteger() && |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 347 | ! (isa<PointerType>(CPV->getType()) && |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 348 | CPV->isNullValue())) |
| 349 | return opType; |
| 350 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 351 | // Now get the constant value and check if it fits in the IMMED field. |
| 352 | // Take advantage of the fact that the max unsigned value will rarely |
| 353 | // fit into any IMMED field and ignore that case (i.e., cast smaller |
| 354 | // unsigned constants to signed). |
| 355 | // |
| 356 | int64_t intValue; |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 357 | if (isa<PointerType>(CPV->getType())) |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 358 | { |
| 359 | intValue = 0; |
| 360 | } |
Vikram S. Adve | 9e29f78 | 2001-11-14 17:55:02 +0000 | [diff] [blame] | 361 | else if (CPV->getType()->isSigned()) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 362 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 363 | intValue = cast<ConstantSInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 364 | } |
| 365 | else |
| 366 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 367 | uint64_t V = cast<ConstantUInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 368 | if (V >= INT64_MAX) return opType; |
| 369 | intValue = (int64_t)V; |
| 370 | } |
| 371 | |
| 372 | if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) |
| 373 | { |
| 374 | opType = MachineOperand::MO_MachineRegister; |
| 375 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 376 | } |
| 377 | else if (canUseImmed && |
| 378 | target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) |
| 379 | { |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 380 | opType = CPV->getType()->isSigned() |
| 381 | ? MachineOperand::MO_SignExtendedImmed |
| 382 | : MachineOperand::MO_UnextendedImmed; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 383 | getImmedValue = intValue; |
| 384 | } |
| 385 | |
| 386 | return opType; |
| 387 | } |
| 388 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 389 | |
| 390 | //--------------------------------------------------------------------------- |
| 391 | // Function: FixConstantOperandsForInstr |
| 392 | // |
| 393 | // Purpose: |
| 394 | // Special handling for constant operands of a machine instruction |
| 395 | // -- if the constant is 0, use the hardwired 0 register, if any; |
| 396 | // -- if the constant fits in the IMMEDIATE field, use that field; |
| 397 | // -- else create instructions to put the constant into a register, either |
| 398 | // directly or by loading explicitly from the constant pool. |
| 399 | // |
| 400 | // In the first 2 cases, the operand of `minstr' is modified in place. |
| 401 | // Returns a vector of machine instructions generated for operands that |
| 402 | // fall under case 3; these must be inserted before `minstr'. |
| 403 | //--------------------------------------------------------------------------- |
| 404 | |
| 405 | vector<MachineInstr*> |
| 406 | FixConstantOperandsForInstr(Instruction* vmInstr, |
| 407 | MachineInstr* minstr, |
| 408 | TargetMachine& target) |
| 409 | { |
| 410 | vector<MachineInstr*> loadConstVec; |
| 411 | |
| 412 | const MachineInstrDescriptor& instrDesc = |
| 413 | target.getInstrInfo().getDescriptor(minstr->getOpCode()); |
| 414 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 415 | Function *F = vmInstr->getParent()->getParent(); |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 416 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 417 | for (unsigned op=0; op < minstr->getNumOperands(); op++) |
| 418 | { |
| 419 | const MachineOperand& mop = minstr->getOperand(op); |
| 420 | |
| 421 | // skip the result position (for efficiency below) and any other |
| 422 | // positions already marked as not a virtual register |
| 423 | if (instrDesc.resultPos == (int) op || |
| 424 | mop.getOperandType() != MachineOperand::MO_VirtualRegister || |
| 425 | mop.getVRegValue() == NULL) |
| 426 | { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | Value* opValue = mop.getVRegValue(); |
| 431 | bool constantThatMustBeLoaded = false; |
| 432 | |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 433 | if (Constant *opConst = dyn_cast<Constant>(opValue)) |
| 434 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 435 | unsigned int machineRegNum; |
| 436 | int64_t immedValue; |
| 437 | MachineOperand::MachineOperandType opType = |
| 438 | ChooseRegOrImmed(opValue, minstr->getOpCode(), target, |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 439 | (target.getInstrInfo().getImmedConstantPos(minstr->getOpCode()) == (int) op), |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 440 | machineRegNum, immedValue); |
Vikram S. Adve | ecd5813 | 2001-11-14 18:49:45 +0000 | [diff] [blame] | 441 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 442 | if (opType == MachineOperand::MO_MachineRegister) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 443 | minstr->SetMachineOperandReg(op, machineRegNum); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 444 | else if (opType == MachineOperand::MO_VirtualRegister) |
| 445 | constantThatMustBeLoaded = true; // load is generated below |
| 446 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 447 | minstr->SetMachineOperandConst(op, opType, immedValue); |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 450 | if (constantThatMustBeLoaded || isa<GlobalValue>(opValue)) |
| 451 | { // opValue is a constant that must be explicitly loaded into a reg. |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 452 | TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue,vmInstr, |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 453 | loadConstVec, |
| 454 | target); |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 455 | minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister, |
| 456 | tmpReg); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | // |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 461 | // Also, check for implicit operands used by the machine instruction |
| 462 | // (no need to check those defined since they cannot be constants). |
| 463 | // These include: |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 464 | // -- arguments to a Call |
| 465 | // -- return value of a Return |
| 466 | // Any such operand that is a constant value needs to be fixed also. |
| 467 | // The current instructions with implicit refs (viz., Call and Return) |
| 468 | // have no immediate fields, so the constant always needs to be loaded |
| 469 | // into a register. |
| 470 | // |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 471 | bool isCall = target.getInstrInfo().isCall(minstr->getOpCode()); |
| 472 | unsigned lastCallArgNum = 0; // unused if not a call |
| 473 | CallArgsDescriptor* argDesc = NULL; // unused if not a call |
| 474 | if (isCall) |
| 475 | argDesc = CallArgsDescriptor::get(minstr); |
| 476 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 477 | for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 478 | if (isa<Constant>(minstr->getImplicitRef(i)) || |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 479 | isa<GlobalValue>(minstr->getImplicitRef(i))) |
| 480 | { |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 481 | Value* oldVal = minstr->getImplicitRef(i); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 482 | TmpInstruction* tmpReg = |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 483 | InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, target); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 484 | minstr->setImplicitRef(i, tmpReg); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 485 | |
| 486 | if (isCall) |
| 487 | { // find and replace the argument in the CallArgsDescriptor |
| 488 | unsigned i=lastCallArgNum; |
| 489 | while (argDesc->getArgInfo(i).getArgVal() != oldVal) |
| 490 | ++i; |
| 491 | assert(i < argDesc->getNumArgs() && |
| 492 | "Constant operands to a call *must* be in the arg list"); |
| 493 | lastCallArgNum = i; |
| 494 | argDesc->getArgInfo(i).replaceArgVal(tmpReg); |
| 495 | } |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | return loadConstVec; |
| 499 | } |
| 500 | |
| 501 | |