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