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" |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 13 | #include "llvm/Constants.h" |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 14 | #include "llvm/Constant.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 15 | #include "llvm/iMemory.h" |
| 16 | #include "llvm/iOther.h" |
| 17 | #include "llvm/BasicBlock.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 19 | #include "Support/StatisticReporter.h" |
| 20 | |
| 21 | static Statistic<> NumAdded("lowerrefs\t\t- New instructions added"); |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | struct DecomposePass : public BasicBlockPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 25 | virtual bool runOnBasicBlock(BasicBlock &BB); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 26 | |
| 27 | private: |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 28 | static bool decomposeArrayRef(BasicBlock::iterator &BBI); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 29 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 30 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 31 | RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional " |
| 32 | "structure/array references"); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 35 | Pass |
| 36 | *createDecomposeMultiDimRefsPass() |
| 37 | { |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 38 | return new DecomposePass(); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // runOnBasicBlock - Entry point for array or structure references with multiple |
| 43 | // indices. |
| 44 | // |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 45 | bool |
| 46 | DecomposePass::runOnBasicBlock(BasicBlock &BB) |
| 47 | { |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 48 | bool Changed = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 49 | for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) { |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 50 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&*II)) |
| 51 | if (GEP->getNumIndices() >= 2) { |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 52 | Changed |= decomposeArrayRef(II); // always modifies II |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 53 | continue; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 54 | } |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 55 | ++II; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 57 | return Changed; |
| 58 | } |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 59 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 60 | // Check for a constant (uint) 0. |
| 61 | inline bool |
| 62 | IsZero(Value* idx) |
| 63 | { |
| 64 | return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue()); |
| 65 | } |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 66 | |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 67 | // For any GetElementPtrInst with 2 or more array and structure indices: |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 68 | // |
| 69 | // opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN |
| 70 | // |
| 71 | // this function generates the foll sequence: |
| 72 | // |
| 73 | // ptr1 = getElementPtr P, idx1 |
| 74 | // ptr2 = getElementPtr ptr1, 0, idx2 |
| 75 | // ... |
| 76 | // ptrN-1 = getElementPtr ptrN-2, 0, idxN-1 |
| 77 | // opCode ptrN-1, 0, idxN // New-MAI |
| 78 | // |
| 79 | // Then it replaces the original instruction with this sequence, |
| 80 | // and replaces all uses of the original instruction with New-MAI. |
| 81 | // If idx1 is 0, we simply omit the first getElementPtr instruction. |
| 82 | // |
| 83 | // On return: BBI points to the instruction after the current one |
| 84 | // (whether or not *BBI was replaced). |
| 85 | // |
| 86 | // Return value: true if the instruction was replaced; false otherwise. |
| 87 | // |
| 88 | bool |
| 89 | DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) |
| 90 | { |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 91 | GetElementPtrInst &GEP = cast<GetElementPtrInst>(*BBI); |
| 92 | BasicBlock *BB = GEP.getParent(); |
| 93 | Value *LastPtr = GEP.getPointerOperand(); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 94 | |
| 95 | // Remove the instruction from the stream |
| 96 | BB->getInstList().remove(BBI); |
| 97 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 98 | // The vector of new instructions to be created |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 99 | std::vector<Instruction*> NewInsts; |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 100 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 101 | // Process each index except the last one. |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 102 | User::const_op_iterator OI = GEP.idx_begin(), OE = GEP.idx_end(); |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 103 | for (; OI+1 != OE; ++OI) { |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 104 | std::vector<Value*> Indices; |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 105 | |
| 106 | // If this is the first index and is 0, skip it and move on! |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 107 | if (OI == GEP.idx_begin()) { |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 108 | if (IsZero(*OI)) continue; |
| 109 | } else |
| 110 | // Not the first index: include initial [0] to deref the last ptr |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 111 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 113 | Indices.push_back(*OI); |
| 114 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 115 | // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices |
| 116 | LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1"); |
| 117 | NewInsts.push_back(cast<Instruction>(LastPtr)); |
| 118 | ++NumAdded; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 119 | } |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 120 | |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 121 | // Now create a new instruction to replace the original one |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 122 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 123 | const PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 124 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 125 | // Get the final index vector, including an initial [0] as before. |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 126 | std::vector<Value*> Indices; |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 127 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 128 | Indices.push_back(*OI); |
| 129 | |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 130 | Instruction *NewI = new GetElementPtrInst(LastPtr, Indices, GEP.getName()); |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 131 | NewInsts.push_back(NewI); |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 132 | |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 133 | // Replace all uses of the old instruction with the new |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 134 | GEP.replaceAllUsesWith(NewI); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 135 | |
| 136 | // Now delete the old instruction... |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 137 | delete &GEP; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 139 | // Insert all of the new instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 140 | BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end()); |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 142 | // Advance the iterator to the instruction following the one just inserted... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 143 | BBI = NewInsts.back(); |
| 144 | ++BBI; |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 145 | return true; |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 146 | } |