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 | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 112 | |
| 113 | // Check for a constant 0. |
| 114 | inline bool |
| 115 | IsZero(Value* idx) |
| 116 | { |
| 117 | return (idx == ConstantSInt::getNullValue(idx->getType())); |
| 118 | } |
| 119 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 120 | static Value* |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 121 | FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec, |
| 122 | bool lastInstHasLeadingNonZero) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 123 | { |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 124 | InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode); |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 125 | GetElementPtrInst* gepInst = |
Chris Lattner | 597f81f | 2002-09-12 20:27:10 +0000 | [diff] [blame] | 126 | dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0); |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 127 | |
| 128 | // ptr value is not computed in this tree or ptr value does not come from GEP |
| 129 | // instruction |
| 130 | if (gepInst == NULL) |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 133 | // Return NULL if we don't fold any instructions in. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 134 | Value* ptrVal = NULL; |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 135 | |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 136 | // Now chase the chain of getElementInstr instructions, if any. |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 137 | // Check for any non-constant indices and stop there. |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 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. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 142 | // |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 143 | InstructionNode* ptrChild = gepNode; |
| 144 | while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 145 | ptrChild->getOpLabel() == GetElemPtrIdx)) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 146 | { |
| 147 | // Child is a GetElemPtr instruction |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 148 | gepInst = cast<GetElementPtrInst>(ptrChild->getValue()); |
| 149 | User::op_iterator OI, firstIdx = gepInst->idx_begin(); |
| 150 | User::op_iterator lastIdx = gepInst->idx_end(); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 151 | bool allConstantOffsets = true; |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 152 | |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 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!"); |
| 156 | |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 157 | // If the last instruction had a leading non-zero index, check if the |
| 158 | // current one references a sequential (i.e., indexable) type. |
| 159 | // If not, the code is not type-safe and we would create an illegal GEP |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 160 | // by folding them, so don't fold any more instructions. |
| 161 | // |
| 162 | if (lastInstHasLeadingNonZero) |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 163 | if (! isa<SequentialType>(gepInst->getType()->getElementType())) |
| 164 | break; // cannot fold in any preceding getElementPtr instrs. |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 165 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 166 | // Check that all offsets are constant for this instruction |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 167 | for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI) |
| 168 | allConstantOffsets = isa<ConstantInt>(*OI); |
| 169 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 170 | if (allConstantOffsets) |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 171 | { // Get pointer value out of ptrChild. |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 172 | ptrVal = gepInst->getPointerOperand(); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 173 | |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 174 | // Remember if it has leading zero index: it will be discarded later. |
| 175 | lastInstHasLeadingNonZero = ! IsZero(*firstIdx); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 176 | |
| 177 | // Insert its index vector at the start, skipping any leading [0] |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 178 | chainIdxVec.insert(chainIdxVec.begin(), |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 179 | firstIdx + !lastInstHasLeadingNonZero, lastIdx); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 180 | |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 181 | // Mark the folded node so no code is generated for it. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 182 | ((InstructionNode*) ptrChild)->markFoldedIntoParent(); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 183 | |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 184 | // Get the previous GEP instruction and continue trying to fold |
| 185 | ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild()); |
| 186 | } |
| 187 | else // cannot fold this getElementPtr instr. or any preceding ones |
| 188 | break; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 189 | } |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 190 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 191 | // 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] | 192 | // Note that this instruction is the *last* one successfully folded above. |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 193 | if (ptrVal && ! lastInstHasLeadingNonZero) |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 194 | chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0)); |
Vikram S. Adve | 1b51b1b | 2002-08-04 20:49:49 +0000 | [diff] [blame] | 195 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 196 | return ptrVal; |
| 197 | } |
| 198 | |
| 199 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 200 | //--------------------------------------------------------------------------- |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 201 | // Function: GetGEPInstArgs |
| 202 | // |
| 203 | // Purpose: |
| 204 | // Helper function for GetMemInstArgs that handles the final getElementPtr |
| 205 | // instruction used by (or same as) the memory operation. |
| 206 | // Extracts the indices of the current instruction and tries to fold in |
| 207 | // preceding ones if all indices of the current one are constant. |
| 208 | //--------------------------------------------------------------------------- |
| 209 | |
| 210 | Value* |
| 211 | GetGEPInstArgs(InstructionNode* gepNode, |
| 212 | vector<Value*>& idxVec, |
| 213 | bool& allConstantIndices) |
| 214 | { |
| 215 | allConstantIndices = true; |
| 216 | GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction()); |
| 217 | |
| 218 | // Default pointer is the one from the current instruction. |
| 219 | Value* ptrVal = gepI->getPointerOperand(); |
| 220 | InstrTreeNode* ptrChild = gepNode->leftChild(); |
| 221 | |
| 222 | // Extract the index vector of the GEP instructin. |
| 223 | // If all indices are constant and first index is zero, try to fold |
| 224 | // in preceding GEPs with all constant indices. |
| 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 | bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin()); |
| 234 | if (allConstantIndices) |
| 235 | if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) |
| 236 | { |
| 237 | ptrVal = newPtr; |
| 238 | foldedGEPs = true; |
| 239 | } |
| 240 | |
| 241 | // Append the index vector of the current instruction. |
| 242 | // Skip the leading [0] index if preceding GEPs were folded into this. |
| 243 | idxVec.insert(idxVec.end(), |
| 244 | gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx), |
| 245 | gepI->idx_end()); |
| 246 | |
| 247 | return ptrVal; |
| 248 | } |
| 249 | |
| 250 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 251 | // Function: GetMemInstArgs |
| 252 | // |
| 253 | // Purpose: |
| 254 | // Get the pointer value and the index vector for a memory operation |
| 255 | // (GetElementPtr, Load, or Store). If all indices of the given memory |
| 256 | // operation are constant, fold in constant indices in a chain of |
| 257 | // preceding GetElementPtr instructions (if any), and return the |
| 258 | // pointer value of the first instruction in the chain. |
| 259 | // All folded instructions are marked so no code is generated for them. |
| 260 | // |
| 261 | // Return values: |
| 262 | // Returns the pointer Value to use. |
| 263 | // Returns the resulting IndexVector in idxVec. |
| 264 | // Returns true/false in allConstantIndices if all indices are/aren't const. |
| 265 | //--------------------------------------------------------------------------- |
| 266 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 267 | Value* |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 268 | GetMemInstArgs(InstructionNode* memInstrNode, |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 269 | vector<Value*>& idxVec, |
| 270 | bool& allConstantIndices) |
| 271 | { |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 272 | allConstantIndices = false; |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 273 | Instruction* memInst = memInstrNode->getInstruction(); |
Vikram S. Adve | aebdbe6 | 2002-09-29 22:55:05 +0000 | [diff] [blame] | 274 | assert(idxVec.size() == 0 && "Need empty vector to return indices"); |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 275 | |
| 276 | // If there is a GetElemPtr instruction to fold in to this instr, |
| 277 | // it must be in the left child for Load and GetElemPtr, and in the |
| 278 | // right child for Store instructions. |
| 279 | InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store |
| 280 | ? memInstrNode->rightChild() |
| 281 | : memInstrNode->leftChild()); |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 282 | |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 283 | // Default pointer is the one from the current instruction. |
| 284 | Value* ptrVal = ptrChild->getValue(); |
| 285 | |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 286 | // Find the "last" GetElemPtr instruction: this one or the immediate child. |
| 287 | // There will be none if this is a load or a store from a scalar pointer. |
| 288 | InstructionNode* gepNode = NULL; |
| 289 | if (isa<GetElementPtrInst>(memInst)) |
| 290 | gepNode = memInstrNode; |
| 291 | else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) |
| 292 | { // Child of load/store is a GEP and memInst is its only use. |
| 293 | // Use its indices and mark it as folded. |
| 294 | gepNode = cast<InstructionNode>(ptrChild); |
| 295 | gepNode->markFoldedIntoParent(); |
| 296 | } |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 297 | |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 298 | // If there are no indices, return the current pointer. |
| 299 | // Else extract the pointer from the GEP and fold the indices. |
| 300 | return (gepNode)? GetGEPInstArgs(gepNode, idxVec, allConstantIndices) |
| 301 | : ptrVal; |
Vikram S. Adve | 6851333 | 2002-08-24 21:00:08 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Vikram S. Adve | cf911de | 2002-10-14 16:30:55 +0000 | [diff] [blame] | 304 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 305 | //------------------------------------------------------------------------ |
| 306 | // Function Set2OperandsFromInstr |
| 307 | // Function Set3OperandsFromInstr |
| 308 | // |
| 309 | // For the common case of 2- and 3-operand arithmetic/logical instructions, |
| 310 | // set the m/c instr. operands directly from the VM instruction's operands. |
| 311 | // Check whether the first or second operand is 0 and can use a dedicated "0" |
| 312 | // register. |
| 313 | // Check whether the second operand should use an immediate field or register. |
| 314 | // (First and third operands are never immediates for such instructions.) |
| 315 | // |
| 316 | // Arguments: |
| 317 | // canDiscardResult: Specifies that the result operand can be discarded |
| 318 | // by using the dedicated "0" |
| 319 | // |
| 320 | // op1position, op2position and resultPosition: Specify in which position |
| 321 | // in the machine instruction the 3 operands (arg1, arg2 |
| 322 | // and result) should go. |
| 323 | // |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 324 | //------------------------------------------------------------------------ |
| 325 | |
| 326 | void |
| 327 | Set2OperandsFromInstr(MachineInstr* minstr, |
| 328 | InstructionNode* vmInstrNode, |
| 329 | const TargetMachine& target, |
| 330 | bool canDiscardResult, |
| 331 | int op1Position, |
| 332 | int resultPosition) |
| 333 | { |
| 334 | Set3OperandsFromInstr(minstr, vmInstrNode, target, |
| 335 | canDiscardResult, op1Position, |
| 336 | /*op2Position*/ -1, resultPosition); |
| 337 | } |
| 338 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 339 | |
| 340 | void |
| 341 | Set3OperandsFromInstr(MachineInstr* minstr, |
| 342 | InstructionNode* vmInstrNode, |
| 343 | const TargetMachine& target, |
| 344 | bool canDiscardResult, |
| 345 | int op1Position, |
| 346 | int op2Position, |
| 347 | int resultPosition) |
| 348 | { |
| 349 | assert(op1Position >= 0); |
| 350 | assert(resultPosition >= 0); |
| 351 | |
| 352 | // operand 1 |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 353 | minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 354 | vmInstrNode->leftChild()->getValue()); |
| 355 | |
| 356 | // operand 2 (if any) |
| 357 | if (op2Position >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 358 | minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 359 | vmInstrNode->rightChild()->getValue()); |
| 360 | |
| 361 | // result operand: if it can be discarded, use a dead register if one exists |
| 362 | if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 363 | minstr->SetMachineOperandReg(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 364 | target.getRegInfo().getZeroRegNum()); |
| 365 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 366 | minstr->SetMachineOperandVal(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 367 | MachineOperand::MO_VirtualRegister, vmInstrNode->getValue()); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | MachineOperand::MachineOperandType |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 372 | ChooseRegOrImmed(int64_t intValue, |
| 373 | bool isSigned, |
| 374 | MachineOpCode opCode, |
| 375 | const TargetMachine& target, |
| 376 | bool canUseImmed, |
| 377 | unsigned int& getMachineRegNum, |
| 378 | int64_t& getImmedValue) |
| 379 | { |
| 380 | MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister; |
| 381 | getMachineRegNum = 0; |
| 382 | getImmedValue = 0; |
| 383 | |
| 384 | if (canUseImmed && |
| 385 | target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) |
| 386 | { |
| 387 | opType = isSigned? MachineOperand::MO_SignExtendedImmed |
| 388 | : MachineOperand::MO_UnextendedImmed; |
| 389 | getImmedValue = intValue; |
| 390 | } |
| 391 | else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) |
| 392 | { |
| 393 | opType = MachineOperand::MO_MachineRegister; |
| 394 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 395 | } |
| 396 | |
| 397 | return opType; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | MachineOperand::MachineOperandType |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 402 | ChooseRegOrImmed(Value* val, |
| 403 | MachineOpCode opCode, |
| 404 | const TargetMachine& target, |
| 405 | bool canUseImmed, |
| 406 | unsigned int& getMachineRegNum, |
| 407 | int64_t& getImmedValue) |
| 408 | { |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 409 | getMachineRegNum = 0; |
| 410 | getImmedValue = 0; |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 411 | |
| 412 | // To use reg or immed, constant needs to be integer, bool, or a NULL pointer |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 413 | Constant *CPV = dyn_cast<Constant>(val); |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 414 | if (CPV == NULL || |
| 415 | (! CPV->getType()->isIntegral() && |
| 416 | ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue()))) |
| 417 | return MachineOperand::MO_VirtualRegister; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 418 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 419 | // Now get the constant value and check if it fits in the IMMED field. |
| 420 | // Take advantage of the fact that the max unsigned value will rarely |
| 421 | // fit into any IMMED field and ignore that case (i.e., cast smaller |
| 422 | // unsigned constants to signed). |
| 423 | // |
| 424 | int64_t intValue; |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 425 | if (isa<PointerType>(CPV->getType())) |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 426 | intValue = 0; // We checked above that it is NULL |
| 427 | else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV)) |
| 428 | intValue = (int64_t) CB->getValue(); |
Vikram S. Adve | 9e29f78 | 2001-11-14 17:55:02 +0000 | [diff] [blame] | 429 | else if (CPV->getType()->isSigned()) |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 430 | intValue = cast<ConstantSInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 431 | else |
Vikram S. Adve | 1c10f17 | 2002-09-27 14:26:20 +0000 | [diff] [blame] | 432 | { // get the int value and sign-extend if original was less than 64 bits |
| 433 | intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue(); |
| 434 | switch(CPV->getType()->getPrimitiveID()) |
| 435 | { |
| 436 | case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break; |
| 437 | case Type::UShortTyID: intValue = (int64_t) (short) intValue; break; |
| 438 | case Type::UIntTyID: intValue = (int64_t) (int) intValue; break; |
| 439 | default: break; |
| 440 | } |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 443 | return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(), |
| 444 | opCode, target, canUseImmed, |
| 445 | getMachineRegNum, getImmedValue); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 448 | |
| 449 | //--------------------------------------------------------------------------- |
| 450 | // Function: FixConstantOperandsForInstr |
| 451 | // |
| 452 | // Purpose: |
| 453 | // Special handling for constant operands of a machine instruction |
| 454 | // -- if the constant is 0, use the hardwired 0 register, if any; |
| 455 | // -- if the constant fits in the IMMEDIATE field, use that field; |
| 456 | // -- else create instructions to put the constant into a register, either |
| 457 | // directly or by loading explicitly from the constant pool. |
| 458 | // |
| 459 | // In the first 2 cases, the operand of `minstr' is modified in place. |
| 460 | // Returns a vector of machine instructions generated for operands that |
| 461 | // fall under case 3; these must be inserted before `minstr'. |
| 462 | //--------------------------------------------------------------------------- |
| 463 | |
| 464 | vector<MachineInstr*> |
| 465 | FixConstantOperandsForInstr(Instruction* vmInstr, |
| 466 | MachineInstr* minstr, |
| 467 | TargetMachine& target) |
| 468 | { |
| 469 | vector<MachineInstr*> loadConstVec; |
| 470 | |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 471 | MachineOpCode opCode = minstr->getOpCode(); |
| 472 | const MachineInstrInfo& instrInfo = target.getInstrInfo(); |
| 473 | const MachineInstrDescriptor& instrDesc = instrInfo.getDescriptor(opCode); |
| 474 | int immedPos = instrInfo.getImmedConstantPos(opCode); |
| 475 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 476 | Function *F = vmInstr->getParent()->getParent(); |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 477 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 478 | for (unsigned op=0; op < minstr->getNumOperands(); op++) |
| 479 | { |
| 480 | const MachineOperand& mop = minstr->getOperand(op); |
| 481 | |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 482 | // Skip the result position, preallocated machine registers, or operands |
| 483 | // that cannot be constants (CC regs or PC-relative displacements) |
| 484 | if (instrDesc.resultPos == (int) op || |
| 485 | mop.getOperandType() == MachineOperand::MO_MachineRegister || |
| 486 | mop.getOperandType() == MachineOperand::MO_CCRegister || |
| 487 | mop.getOperandType() == MachineOperand::MO_PCRelativeDisp) |
| 488 | continue; |
| 489 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 490 | bool constantThatMustBeLoaded = false; |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 491 | unsigned int machineRegNum = 0; |
| 492 | int64_t immedValue = 0; |
| 493 | Value* opValue = NULL; |
| 494 | MachineOperand::MachineOperandType opType = |
| 495 | MachineOperand::MO_VirtualRegister; |
| 496 | |
| 497 | // Operand may be a virtual register or a compile-time constant |
| 498 | if (mop.getOperandType() == MachineOperand::MO_VirtualRegister) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 499 | { |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 500 | assert(mop.getVRegValue() != NULL); |
| 501 | opValue = mop.getVRegValue(); |
| 502 | if (Constant *opConst = dyn_cast<Constant>(opValue)) |
| 503 | { |
| 504 | opType = ChooseRegOrImmed(opConst, opCode, target, |
| 505 | (immedPos == (int)op), machineRegNum, immedValue); |
| 506 | if (opType == MachineOperand::MO_VirtualRegister) |
| 507 | constantThatMustBeLoaded = true; |
| 508 | } |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 509 | } |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 510 | else |
| 511 | { |
| 512 | assert(mop.getOperandType() == MachineOperand::MO_SignExtendedImmed || |
| 513 | mop.getOperandType() == MachineOperand::MO_UnextendedImmed); |
| 514 | |
| 515 | bool isSigned = (mop.getOperandType() == |
| 516 | MachineOperand::MO_SignExtendedImmed); |
| 517 | |
| 518 | // Bit-selection flags indicate an instruction that is extracting |
| 519 | // bits from its operand so ignore this even if it is a big constant. |
| 520 | if (mop.opHiBits32() || mop.opLoBits32() || |
| 521 | mop.opHiBits64() || mop.opLoBits64()) |
| 522 | continue; |
| 523 | |
| 524 | opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned, |
| 525 | opCode, target, (immedPos == (int)op), |
| 526 | machineRegNum, immedValue); |
| 527 | |
| 528 | if (opType == mop.getOperandType()) |
| 529 | continue; // no change: this is the most common case |
| 530 | |
| 531 | if (opType == MachineOperand::MO_VirtualRegister) |
| 532 | { |
| 533 | constantThatMustBeLoaded = true; |
| 534 | opValue = isSigned |
Chris Lattner | 82f05d8 | 2002-09-17 17:23:09 +0000 | [diff] [blame] | 535 | ? (Value*)ConstantSInt::get(Type::LongTy, immedValue) |
| 536 | : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue); |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | if (opType == MachineOperand::MO_MachineRegister) |
| 541 | minstr->SetMachineOperandReg(op, machineRegNum); |
| 542 | else if (opType == MachineOperand::MO_SignExtendedImmed || |
| 543 | opType == MachineOperand::MO_UnextendedImmed) |
| 544 | minstr->SetMachineOperandConst(op, opType, immedValue); |
| 545 | else if (constantThatMustBeLoaded || |
| 546 | (opValue && isa<GlobalValue>(opValue))) |
| 547 | { // opValue is a constant that must be explicitly loaded into a reg |
| 548 | assert(opValue); |
| 549 | TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr, |
| 550 | loadConstVec, target); |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 551 | minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister, |
| 552 | tmpReg); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 556 | // Also, check for implicit operands used by the machine instruction |
| 557 | // (no need to check those defined since they cannot be constants). |
| 558 | // These include: |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 559 | // -- arguments to a Call |
| 560 | // -- return value of a Return |
| 561 | // Any such operand that is a constant value needs to be fixed also. |
| 562 | // The current instructions with implicit refs (viz., Call and Return) |
| 563 | // have no immediate fields, so the constant always needs to be loaded |
| 564 | // into a register. |
| 565 | // |
Vikram S. Adve | fd0ec80 | 2002-09-16 15:15:57 +0000 | [diff] [blame] | 566 | bool isCall = instrInfo.isCall(opCode); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 567 | unsigned lastCallArgNum = 0; // unused if not a call |
| 568 | CallArgsDescriptor* argDesc = NULL; // unused if not a call |
| 569 | if (isCall) |
| 570 | argDesc = CallArgsDescriptor::get(minstr); |
| 571 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 572 | for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 573 | if (isa<Constant>(minstr->getImplicitRef(i)) || |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 574 | isa<GlobalValue>(minstr->getImplicitRef(i))) |
| 575 | { |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 576 | Value* oldVal = minstr->getImplicitRef(i); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 577 | TmpInstruction* tmpReg = |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 578 | InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, target); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 579 | minstr->setImplicitRef(i, tmpReg); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame] | 580 | |
| 581 | if (isCall) |
| 582 | { // find and replace the argument in the CallArgsDescriptor |
| 583 | unsigned i=lastCallArgNum; |
| 584 | while (argDesc->getArgInfo(i).getArgVal() != oldVal) |
| 585 | ++i; |
| 586 | assert(i < argDesc->getNumArgs() && |
| 587 | "Constant operands to a call *must* be in the arg list"); |
| 588 | lastCallArgNum = i; |
| 589 | argDesc->getArgInfo(i).replaceArgVal(tmpReg); |
| 590 | } |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | return loadConstVec; |
| 594 | } |
| 595 | |
| 596 | |