Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 1 | //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===// |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 2 | // |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 3 | // DecomposeMultiDimRefs - |
| 4 | // Convert multi-dimensional references consisting of any combination |
| 5 | // of 2 or more array and structure indices into a sequence of |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 6 | // instructions (using getelementpr and cast) so that each instruction |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 7 | // has at most one index (except structure references, |
| 8 | // which need an extra leading index of [0]). |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 11 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 12 | #include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 13 | #include "llvm/Constants.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 14 | #include "llvm/iMemory.h" |
| 15 | #include "llvm/iOther.h" |
| 16 | #include "llvm/BasicBlock.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
| 19 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 20 | namespace { |
| 21 | struct DecomposePass : public BasicBlockPass { |
| 22 | virtual bool runOnBasicBlock(BasicBlock *BB); |
| 23 | |
| 24 | private: |
| 25 | static void decomposeArrayRef(BasicBlock::iterator &BBI); |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | Pass *createDecomposeMultiDimRefsPass() { |
| 30 | return new DecomposePass(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | // runOnBasicBlock - Entry point for array or structure references with multiple |
| 35 | // indices. |
| 36 | // |
| 37 | bool DecomposePass::runOnBasicBlock(BasicBlock *BB) { |
| 38 | bool Changed = false; |
| 39 | |
| 40 | for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ) { |
| 41 | if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) { |
| 42 | if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) { |
| 43 | decomposeArrayRef(II); |
| 44 | Changed = true; |
| 45 | } else { |
| 46 | ++II; |
| 47 | } |
| 48 | } else { |
| 49 | ++II; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return Changed; |
| 54 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 55 | |
| 56 | // |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 57 | // For any combination of 2 or more array and structure indices, |
| 58 | // this function repeats the foll. until we have a one-dim. reference: { |
| 59 | // ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex |
| 60 | // ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] * |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 61 | // } |
| 62 | // Then it replaces the original instruction with an equivalent one that |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 63 | // uses the last ptr2 generated in the loop and a single index. |
| 64 | // If any index is (uint) 0, we omit the getElementPtr instruction. |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 65 | // |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 66 | void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI){ |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 67 | MemAccessInst *memI = cast<MemAccessInst>(*BBI); |
| 68 | BasicBlock* BB = memI->getParent(); |
| 69 | Value* lastPtr = memI->getPointerOperand(); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 70 | |
| 71 | // Remove the instruction from the stream |
| 72 | BB->getInstList().remove(BBI); |
| 73 | |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 74 | vector<Instruction*> newIvec; |
| 75 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 76 | // Process each index except the last one. |
| 77 | // |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 78 | User::const_op_iterator OI = memI->idx_begin(), OE = memI->idx_end(); |
| 79 | for (; OI != OE && OI+1 != OE; ++OI) { |
| 80 | assert(isa<PointerType>(lastPtr->getType())); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 82 | // Check for a zero index. This will need a cast instead of |
| 83 | // a getElementPtr, or it may need neither. |
| 84 | bool indexIsZero = isa<ConstantUInt>(*OI) && |
| 85 | cast<Constant>(*OI)->isNullValue(); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 87 | // Extract the first index. If the ptr is a pointer to a structure |
| 88 | // and the next index is a structure offset (i.e., not an array offset), |
| 89 | // we need to include an initial [0] to index into the pointer. |
| 90 | vector<Value*> idxVec(1, *OI); |
| 91 | PointerType* ptrType = cast<PointerType>(lastPtr->getType()); |
| 92 | if (isa<StructType>(ptrType->getElementType()) |
| 93 | && ! ptrType->indexValid(*OI)) |
| 94 | idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0)); |
| 95 | |
| 96 | // Get the type obtained by applying the first index. |
| 97 | // It must be a structure or array. |
| 98 | const Type* nextType = MemAccessInst::getIndexedType(lastPtr->getType(), |
| 99 | idxVec, true); |
| 100 | assert(isa<StructType>(nextType) || isa<ArrayType>(nextType)); |
| 101 | |
| 102 | // Get a pointer to the structure or to the elements of the array. |
| 103 | const Type* nextPtrType = |
| 104 | PointerType::get(isa<StructType>(nextType) ? nextType |
| 105 | : cast<ArrayType>(nextType)->getElementType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 107 | // Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec |
| 108 | // This is not needed if the index is zero. |
| 109 | Value *gepValue; |
| 110 | if (indexIsZero) |
| 111 | gepValue = lastPtr; |
| 112 | else { |
| 113 | gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1"); |
| 114 | newIvec.push_back(cast<Instruction>(gepValue)); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 116 | |
| 117 | // Instruction 2: nextPtr2 = cast nextPtr1 to nextPtrType |
| 118 | // This is not needed if the two types are identical. |
| 119 | Value *castInst; |
| 120 | if (gepValue->getType() == nextPtrType) |
| 121 | castInst = gepValue; |
| 122 | else { |
| 123 | castInst = new CastInst(gepValue, nextPtrType, "ptr2"); |
| 124 | newIvec.push_back(cast<Instruction>(castInst)); |
| 125 | } |
| 126 | |
| 127 | lastPtr = castInst; |
| 128 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 129 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 130 | // |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 131 | // Now create a new instruction to replace the original one |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 132 | // |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 133 | PointerType *ptrType = cast<PointerType>(lastPtr->getType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 134 | |
| 135 | // First, get the final index vector. As above, we may need an initial [0]. |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 136 | vector<Value*> idxVec(1, *OI); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 137 | if (isa<StructType>(ptrType->getElementType()) |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 138 | && !ptrType->indexValid(*OI)) |
| 139 | idxVec.insert(idxVec.begin(), Constant::getNullValue(Type::UIntTy)); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 140 | |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 141 | Instruction* newInst = NULL; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 142 | switch(memI->getOpcode()) { |
| 143 | case Instruction::Load: |
| 144 | newInst = new LoadInst(lastPtr, idxVec, memI->getName()); |
| 145 | break; |
| 146 | case Instruction::Store: |
| 147 | newInst = new StoreInst(memI->getOperand(0), lastPtr, idxVec); |
| 148 | break; |
| 149 | case Instruction::GetElementPtr: |
| 150 | newInst = new GetElementPtrInst(lastPtr, idxVec, memI->getName()); |
| 151 | break; |
| 152 | default: |
| 153 | assert(0 && "Unrecognized memory access instruction"); |
| 154 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 155 | newIvec.push_back(newInst); |
| 156 | |
| 157 | // Replace all uses of the old instruction with the new |
| 158 | memI->replaceAllUsesWith(newInst); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 159 | |
| 160 | // Now delete the old instruction... |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 161 | delete memI; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 162 | |
| 163 | // Convert our iterator into an index... that cannot get invalidated |
| 164 | unsigned ItOffs = BBI-BB->begin(); |
| 165 | |
| 166 | // Insert all of the new instructions... |
| 167 | BB->getInstList().insert(BBI, newIvec.begin(), newIvec.end()); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame^] | 169 | // Advance the iterator to the instruction following the one just inserted... |
| 170 | BBI = BB->begin() + (ItOffs+newIvec.size()); |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 171 | } |