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 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 11 | #include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.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" |
| 18 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | struct DecomposePass : public BasicBlockPass { |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 21 | const char *getPassName() const { return "Decompose Subscripting Exps"; } |
| 22 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 23 | virtual bool runOnBasicBlock(BasicBlock *BB); |
| 24 | |
| 25 | private: |
| 26 | static void decomposeArrayRef(BasicBlock::iterator &BBI); |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | Pass *createDecomposeMultiDimRefsPass() { |
| 31 | return new DecomposePass(); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | // runOnBasicBlock - Entry point for array or structure references with multiple |
| 36 | // indices. |
| 37 | // |
| 38 | bool DecomposePass::runOnBasicBlock(BasicBlock *BB) { |
| 39 | bool Changed = false; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 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 | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 66 | void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) { |
| 67 | MemAccessInst *MAI = cast<MemAccessInst>(*BBI); |
| 68 | BasicBlock *BB = MAI->getParent(); |
| 69 | Value *LastPtr = MAI->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 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 74 | vector<Instruction*> NewInsts; |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 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 | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 78 | User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end(); |
| 79 | for (; 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. |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 84 | bool indexIsZero = isa<Constant>(*OI) && |
| 85 | cast<Constant>(*OI)->isNullValue() && |
| 86 | (*OI)->getType() == Type::UIntTy; |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 88 | // Extract the first index. If the ptr is a pointer to a structure |
| 89 | // and the next index is a structure offset (i.e., not an array offset), |
| 90 | // we need to include an initial [0] to index into the pointer. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 91 | // |
| 92 | vector<Value*> Indices; |
| 93 | PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); |
| 94 | if (isa<StructType>(PtrTy->getElementType()) |
| 95 | && !PtrTy->indexValid(*OI)) |
| 96 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 97 | Indices.push_back(*OI); |
| 98 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 99 | // Get the type obtained by applying the first index. |
| 100 | // It must be a structure or array. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 101 | const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(), |
| 102 | Indices, true); |
| 103 | assert(isa<CompositeType>(NextTy)); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 104 | |
| 105 | // 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] | 106 | const Type *NextPtrTy = |
| 107 | PointerType::get(isa<StructType>(NextTy) ? NextTy |
| 108 | : cast<ArrayType>(NextTy)->getElementType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 110 | // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 111 | // This is not needed if the index is zero. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 112 | if (!indexIsZero) { |
| 113 | LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1"); |
| 114 | NewInsts.push_back(cast<Instruction>(LastPtr)); |
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 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 117 | // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 118 | // This is not needed if the two types are identical. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 119 | // |
| 120 | if (LastPtr->getType() != NextPtrTy) { |
| 121 | LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2"); |
| 122 | NewInsts.push_back(cast<Instruction>(LastPtr)); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 123 | } |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 124 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 125 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 126 | // |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 127 | // Now create a new instruction to replace the original one |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 128 | // |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 129 | PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 130 | |
| 131 | // 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] | 132 | vector<Value*> Indices; |
| 133 | if (isa<StructType>(PtrTy->getElementType()) |
| 134 | && !PtrTy->indexValid(*OI)) |
| 135 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 136 | |
| 137 | Indices.push_back(*OI); |
| 138 | |
| 139 | Instruction *NewI = 0; |
| 140 | switch(MAI->getOpcode()) { |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 141 | case Instruction::Load: |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 142 | NewI = new LoadInst(LastPtr, Indices, MAI->getName()); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 143 | break; |
| 144 | case Instruction::Store: |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 145 | NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 146 | break; |
| 147 | case Instruction::GetElementPtr: |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 148 | NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName()); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 149 | break; |
| 150 | default: |
| 151 | assert(0 && "Unrecognized memory access instruction"); |
| 152 | } |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 153 | NewInsts.push_back(NewI); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 154 | |
| 155 | // Replace all uses of the old instruction with the new |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 156 | MAI->replaceAllUsesWith(NewI); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 157 | |
| 158 | // Now delete the old instruction... |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 159 | delete MAI; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 161 | // Insert all of the new instructions... |
Chris Lattner | f737121 | 2002-04-29 21:25:34 +0000 | [diff] [blame] | 162 | BBI = BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end()); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 164 | // Advance the iterator to the instruction following the one just inserted... |
Chris Lattner | f737121 | 2002-04-29 21:25:34 +0000 | [diff] [blame] | 165 | BBI += NewInsts.size(); |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 166 | } |