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