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 | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 12 | #include "llvm/Constant.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iOther.h" |
| 15 | #include "llvm/BasicBlock.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
| 17 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | struct DecomposePass : public BasicBlockPass { |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 20 | const char *getPassName() const { return "Decompose Subscripting Exps"; } |
| 21 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 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; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 39 | for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ) { |
| 40 | if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) { |
| 41 | if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) { |
| 42 | decomposeArrayRef(II); |
| 43 | Changed = true; |
| 44 | } else { |
| 45 | ++II; |
| 46 | } |
| 47 | } else { |
| 48 | ++II; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return Changed; |
| 53 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 54 | |
| 55 | // |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 56 | // For any combination of 2 or more array and structure indices, |
| 57 | // this function repeats the foll. until we have a one-dim. reference: { |
| 58 | // ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex |
| 59 | // ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] * |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 60 | // } |
| 61 | // Then it replaces the original instruction with an equivalent one that |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 62 | // uses the last ptr2 generated in the loop and a single index. |
| 63 | // If any index is (uint) 0, we omit the getElementPtr instruction. |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 64 | // |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 65 | void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) { |
| 66 | MemAccessInst *MAI = cast<MemAccessInst>(*BBI); |
| 67 | BasicBlock *BB = MAI->getParent(); |
| 68 | Value *LastPtr = MAI->getPointerOperand(); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 69 | |
| 70 | // Remove the instruction from the stream |
| 71 | BB->getInstList().remove(BBI); |
| 72 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 73 | vector<Instruction*> NewInsts; |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 74 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 75 | // Process each index except the last one. |
| 76 | // |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 77 | User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end(); |
| 78 | for (; OI+1 != OE; ++OI) { |
| 79 | assert(isa<PointerType>(LastPtr->getType())); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 81 | // Check for a zero index. This will need a cast instead of |
| 82 | // a getElementPtr, or it may need neither. |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 83 | bool indexIsZero = isa<Constant>(*OI) && |
| 84 | cast<Constant>(*OI)->isNullValue() && |
| 85 | (*OI)->getType() == Type::UIntTy; |
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. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 90 | // |
| 91 | vector<Value*> Indices; |
| 92 | PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); |
| 93 | if (isa<StructType>(PtrTy->getElementType()) |
| 94 | && !PtrTy->indexValid(*OI)) |
| 95 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 96 | Indices.push_back(*OI); |
| 97 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 98 | // Get the type obtained by applying the first index. |
| 99 | // It must be a structure or array. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 100 | const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(), |
| 101 | Indices, true); |
| 102 | assert(isa<CompositeType>(NextTy)); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 103 | |
| 104 | // 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] | 105 | const Type *NextPtrTy = |
| 106 | PointerType::get(isa<StructType>(NextTy) ? NextTy |
| 107 | : cast<ArrayType>(NextTy)->getElementType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 109 | // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 110 | // This is not needed if the index is zero. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 111 | if (!indexIsZero) { |
| 112 | LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1"); |
| 113 | NewInsts.push_back(cast<Instruction>(LastPtr)); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 116 | // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 117 | // This is not needed if the two types are identical. |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 118 | // |
| 119 | if (LastPtr->getType() != NextPtrTy) { |
| 120 | LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2"); |
| 121 | NewInsts.push_back(cast<Instruction>(LastPtr)); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 123 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 124 | |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 125 | // |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 126 | // Now create a new instruction to replace the original one |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 127 | // |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 128 | PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 129 | |
| 130 | // 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] | 131 | vector<Value*> Indices; |
| 132 | if (isa<StructType>(PtrTy->getElementType()) |
| 133 | && !PtrTy->indexValid(*OI)) |
| 134 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 135 | |
| 136 | Indices.push_back(*OI); |
| 137 | |
| 138 | Instruction *NewI = 0; |
| 139 | switch(MAI->getOpcode()) { |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 140 | case Instruction::Load: |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 141 | NewI = new LoadInst(LastPtr, Indices, MAI->getName()); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 142 | break; |
| 143 | case Instruction::Store: |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 144 | NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 145 | break; |
| 146 | case Instruction::GetElementPtr: |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 147 | NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName()); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 148 | break; |
| 149 | default: |
| 150 | assert(0 && "Unrecognized memory access instruction"); |
| 151 | } |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 152 | NewInsts.push_back(NewI); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 153 | |
| 154 | // Replace all uses of the old instruction with the new |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 155 | MAI->replaceAllUsesWith(NewI); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 156 | |
| 157 | // Now delete the old instruction... |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 158 | delete MAI; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 159 | |
| 160 | // Convert our iterator into an index... that cannot get invalidated |
| 161 | unsigned ItOffs = BBI-BB->begin(); |
| 162 | |
| 163 | // Insert all of the new instructions... |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 164 | BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end()); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 166 | // Advance the iterator to the instruction following the one just inserted... |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 167 | BBI = BB->begin() + ItOffs + NewInsts.size(); |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 168 | } |