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" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 23 | #include "llvm/BasicBlock.h" |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame^] | 25 | #include "llvm/ADT/Statistic.h" |
| 26 | #include "llvm/Support/Debug.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 | |
Brian Gaeke | 10585d9 | 2004-07-06 18:15:39 +0000 | [diff] [blame] | 55 | static inline bool isZeroConst (Value *V) { |
| 56 | return isa<Constant> (V) && (cast<Constant>(V)->isNullValue()); |
| 57 | } |
| 58 | |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 59 | // Function: DecomposeArrayRef() |
| 60 | // |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 61 | // For any GetElementPtrInst with 2 or more array and structure indices: |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 62 | // |
| 63 | // opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN |
| 64 | // |
Brian Gaeke | 1c4b6de4 | 2004-07-02 05:30:01 +0000 | [diff] [blame] | 65 | // this function generates the following sequence: |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 66 | // |
| 67 | // ptr1 = getElementPtr P, idx1 |
| 68 | // ptr2 = getElementPtr ptr1, 0, idx2 |
| 69 | // ... |
| 70 | // ptrN-1 = getElementPtr ptrN-2, 0, idxN-1 |
| 71 | // opCode ptrN-1, 0, idxN // New-MAI |
| 72 | // |
| 73 | // Then it replaces the original instruction with this sequence, |
| 74 | // and replaces all uses of the original instruction with New-MAI. |
| 75 | // If idx1 is 0, we simply omit the first getElementPtr instruction. |
| 76 | // |
| 77 | // On return: BBI points to the instruction after the current one |
| 78 | // (whether or not *BBI was replaced). |
| 79 | // |
| 80 | // Return value: true if the instruction was replaced; false otherwise. |
| 81 | // |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 82 | bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) { |
Brian Gaeke | 10585d9 | 2004-07-06 18:15:39 +0000 | [diff] [blame] | 83 | if (GEP->getNumIndices() < 2 |
| 84 | || (GEP->getNumIndices() == 2 |
Brian Gaeke | 08cc64e | 2004-07-06 19:24:47 +0000 | [diff] [blame] | 85 | && isZeroConst(GEP->getOperand(1)))) { |
Brian Gaeke | 10585d9 | 2004-07-06 18:15:39 +0000 | [diff] [blame] | 86 | DEBUG (std::cerr << "DecomposeArrayRef: Skipping " << *GEP); |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 87 | return false; |
Brian Gaeke | 10585d9 | 2004-07-06 18:15:39 +0000 | [diff] [blame] | 88 | } else { |
| 89 | DEBUG (std::cerr << "DecomposeArrayRef: Decomposing " << *GEP); |
| 90 | } |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 91 | |
| 92 | BasicBlock *BB = GEP->getParent(); |
| 93 | Value *LastPtr = GEP->getPointerOperand(); |
| 94 | Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn |
| 95 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 96 | // Process each index except the last one. |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 97 | User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end(); |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 98 | for (; OI+1 != OE; ++OI) { |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 99 | std::vector<Value*> Indices; |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 100 | |
| 101 | // 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] | 102 | if (OI == GEP->idx_begin()) { |
Brian Gaeke | 10585d9 | 2004-07-06 18:15:39 +0000 | [diff] [blame] | 103 | if (isZeroConst (*OI)) |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 104 | continue; |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 105 | } |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 106 | else // Not the first index: include initial [0] to deref the last ptr |
| 107 | Indices.push_back(Constant::getNullValue(Type::LongTy)); |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 109 | Indices.push_back(*OI); |
| 110 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 111 | // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 112 | LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint); |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 113 | ++NumAdded; |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 114 | } |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 115 | |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 116 | // Now create a new instruction to replace the original one |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 117 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 118 | const PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); |
Vikram S. Adve | 98d64f8 | 2002-03-24 03:21:18 +0000 | [diff] [blame] | 119 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 120 | // Get the final index vector, including an initial [0] as before. |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 121 | std::vector<Value*> Indices; |
Chris Lattner | 3cac88a | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 122 | Indices.push_back(Constant::getNullValue(Type::LongTy)); |
Chris Lattner | 097632e | 2002-04-29 01:58:47 +0000 | [diff] [blame] | 123 | Indices.push_back(*OI); |
| 124 | |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 125 | Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(), |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 126 | InsertPoint); |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 127 | |
Vikram S. Adve | dfbbf7a | 2002-03-23 20:43:39 +0000 | [diff] [blame] | 128 | // Replace all uses of the old instruction with the new |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 129 | GEP->replaceAllUsesWith(NewVal); |
Chris Lattner | 2f6f03b | 2002-04-29 01:22:55 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 131 | // Now remove and delete the old instruction... |
Vikram S. Adve | c7a6d24 | 2002-09-16 16:40:07 +0000 | [diff] [blame] | 132 | BB->getInstList().erase(GEP); |
| 133 | |
Vikram S. Adve | 900fd63 | 2002-08-03 13:21:15 +0000 | [diff] [blame] | 134 | return true; |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 135 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 136 | |