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