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