Add new optional getPassName() virtual function that a Pass can override
[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/DecomposeMultiDimRefs.h"
12 #include "llvm/Constant.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/iOther.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Pass.h"
17
18 namespace {
19   struct DecomposePass : public BasicBlockPass {
20     const char *getPassName() const { return "Decompose Subscripting Exps"; }
21
22     virtual bool runOnBasicBlock(BasicBlock *BB);
23
24   private:
25     static void decomposeArrayRef(BasicBlock::iterator &BBI);
26   };
27 }
28
29 Pass *createDecomposeMultiDimRefsPass() {
30   return new DecomposePass();
31 }
32
33
34 // runOnBasicBlock - Entry point for array or structure references with multiple
35 // indices.
36 //
37 bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
38   bool Changed = false;
39   for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ) {
40     if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) {
41       if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
42         decomposeArrayRef(II);
43         Changed = true;
44       } else {
45         ++II;
46       }
47     } else {
48       ++II;
49     }
50   }
51   
52   return Changed;
53 }
54
55 // 
56 // For any combination of 2 or more array and structure indices,
57 // this function repeats the foll. until we have a one-dim. reference: {
58 //      ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex
59 //      ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] *
60 // }
61 // Then it replaces the original instruction with an equivalent one that
62 // uses the last ptr2 generated in the loop and a single index.
63 // If any index is (uint) 0, we omit the getElementPtr instruction.
64 // 
65 void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
66   MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
67   BasicBlock *BB = MAI->getParent();
68   Value *LastPtr = MAI->getPointerOperand();
69
70   // Remove the instruction from the stream
71   BB->getInstList().remove(BBI);
72
73   vector<Instruction*> NewInsts;
74   
75   // Process each index except the last one.
76   // 
77   User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end();
78   for (; OI+1 != OE; ++OI) {
79     assert(isa<PointerType>(LastPtr->getType()));
80       
81     // Check for a zero index.  This will need a cast instead of
82     // a getElementPtr, or it may need neither.
83     bool indexIsZero = isa<Constant>(*OI) && 
84                        cast<Constant>(*OI)->isNullValue() &&
85                        (*OI)->getType() == Type::UIntTy;
86       
87     // Extract the first index.  If the ptr is a pointer to a structure
88     // and the next index is a structure offset (i.e., not an array offset), 
89     // we need to include an initial [0] to index into the pointer.
90     //
91     vector<Value*> Indices;
92     PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
93     if (isa<StructType>(PtrTy->getElementType())
94         && !PtrTy->indexValid(*OI))
95       Indices.push_back(Constant::getNullValue(Type::UIntTy));
96     Indices.push_back(*OI);
97
98     // Get the type obtained by applying the first index.
99     // It must be a structure or array.
100     const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
101                                                        Indices, true);
102     assert(isa<CompositeType>(NextTy));
103     
104     // Get a pointer to the structure or to the elements of the array.
105     const Type *NextPtrTy =
106       PointerType::get(isa<StructType>(NextTy) ? NextTy
107                        : cast<ArrayType>(NextTy)->getElementType());
108       
109     // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
110     // This is not needed if the index is zero.
111     if (!indexIsZero) {
112       LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
113       NewInsts.push_back(cast<Instruction>(LastPtr));
114     }
115       
116     // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
117     // This is not needed if the two types are identical.
118     //
119     if (LastPtr->getType() != NextPtrTy) {
120       LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
121       NewInsts.push_back(cast<Instruction>(LastPtr));
122     }
123   }
124   
125   // 
126   // Now create a new instruction to replace the original one
127   //
128   PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
129
130   // First, get the final index vector.  As above, we may need an initial [0].
131   vector<Value*> Indices;
132   if (isa<StructType>(PtrTy->getElementType())
133       && !PtrTy->indexValid(*OI))
134     Indices.push_back(Constant::getNullValue(Type::UIntTy));
135
136   Indices.push_back(*OI);
137
138   Instruction *NewI = 0;
139   switch(MAI->getOpcode()) {
140   case Instruction::Load:
141     NewI = new LoadInst(LastPtr, Indices, MAI->getName());
142     break;
143   case Instruction::Store:
144     NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
145     break;
146   case Instruction::GetElementPtr:
147     NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
148     break;
149   default:
150     assert(0 && "Unrecognized memory access instruction");
151   }
152   NewInsts.push_back(NewI);
153   
154   // Replace all uses of the old instruction with the new
155   MAI->replaceAllUsesWith(NewI);
156
157   // Now delete the old instruction...
158   delete MAI;
159
160   // Convert our iterator into an index... that cannot get invalidated
161   unsigned ItOffs = BBI-BB->begin();
162
163   // Insert all of the new instructions...
164   BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
165   
166   // Advance the iterator to the instruction following the one just inserted...
167   BBI = BB->begin() + ItOffs + NewInsts.size();
168 }