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