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