Implement InstCombine/add.ll:test20
[oota-llvm.git] / lib / Transforms / Scalar / DecomposeMultiDimRefs.cpp
1 //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
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 //===----------------------------------------------------------------------===//
9 //
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]).
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Constant.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/iOther.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/Pass.h"
26 #include "Support/Statistic.h"
27 using namespace llvm;
28
29 namespace {
30   Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
31
32   struct DecomposePass : public BasicBlockPass {
33     virtual bool runOnBasicBlock(BasicBlock &BB);
34   };
35   RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
36                                "structure/array references");
37 }
38
39 // runOnBasicBlock - Entry point for array or structure references with multiple
40 // indices.
41 //
42 bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
43   bool changed = false;
44   for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
45     if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
46       if (gep->getNumIndices() >= 2)
47         changed |= DecomposeArrayRef(gep); // always modifies II
48   return changed;
49 }
50
51 FunctionPass *llvm::createDecomposeMultiDimRefsPass() {
52   return new DecomposePass();
53 }
54
55 // Function: DecomposeArrayRef()
56 //  
57 // For any GetElementPtrInst with 2 or more array and structure indices:
58 // 
59 //      opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
60 // 
61 // this function generates the foll sequence:
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 // 
78 bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) {
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
86   // The vector of new instructions to be created
87   std::vector<Instruction*> NewInsts;
88
89   // Process each index except the last one.
90   User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
91   for (; OI+1 != OE; ++OI) {
92     std::vector<Value*> Indices;
93     
94     // If this is the first index and is 0, skip it and move on!
95     if (OI == GEP->idx_begin()) {
96       if (*OI == ConstantInt::getNullValue((*OI)->getType()))
97         continue;
98     }
99     else // Not the first index: include initial [0] to deref the last ptr
100       Indices.push_back(Constant::getNullValue(Type::LongTy));
101
102     Indices.push_back(*OI);
103
104     // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
105     LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
106     ++NumAdded;
107   }
108
109   // Now create a new instruction to replace the original one
110   //
111   const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
112
113   // Get the final index vector, including an initial [0] as before.
114   std::vector<Value*> Indices;
115   Indices.push_back(Constant::getNullValue(Type::LongTy));
116   Indices.push_back(*OI);
117
118   Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
119                                         InsertPoint);
120
121   // Replace all uses of the old instruction with the new
122   GEP->replaceAllUsesWith(NewVal);
123
124   // Now remove and delete the old instruction...
125   BB->getInstList().erase(GEP);
126
127   return true;
128 }
129