1 //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -----=//
3 // DecomposeMultiDimRefs -
4 // Convert multi-dimensional references consisting of any combination
5 // of 2 or more array and structure indices into a sequence of
6 // instructions (using getelementpr and cast) so that each instruction
7 // has at most one index (except structure references,
8 // which need an extra leading index of [0]).
10 //===---------------------------------------------------------------------===//
12 #include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
13 #include "llvm/ConstantVals.h"
14 #include "llvm/iMemory.h"
15 #include "llvm/iOther.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/Function.h"
18 #include "llvm/Pass.h"
22 // For any combination of 2 or more array and structure indices,
23 // this function repeats the foll. until we have a one-dim. reference: {
24 // ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex
25 // ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] *
27 // Then it replaces the original instruction with an equivalent one that
28 // uses the last ptr2 generated in the loop and a single index.
29 // If any index is (uint) 0, we omit the getElementPtr instruction.
31 static BasicBlock::iterator
32 decomposeArrayRef(BasicBlock::iterator& BBI)
34 MemAccessInst *memI = cast<MemAccessInst>(*BBI);
35 BasicBlock* BB = memI->getParent();
36 Value* lastPtr = memI->getPointerOperand();
37 vector<Instruction*> newIvec;
39 // Process each index except the last one.
41 MemAccessInst::const_op_iterator OI = memI->idx_begin();
42 MemAccessInst::const_op_iterator OE = memI->idx_end();
43 for ( ; OI != OE; ++OI)
45 assert(isa<PointerType>(lastPtr->getType()));
47 if (OI+1 == OE) // stop before the last operand
50 // Check for a zero index. This will need a cast instead of
51 // a getElementPtr, or it may need neither.
52 bool indexIsZero = bool(isa<ConstantUInt>(*OI) &&
53 cast<ConstantUInt>(*OI)->getValue() == 0);
55 // Extract the first index. If the ptr is a pointer to a structure
56 // and the next index is a structure offset (i.e., not an array offset),
57 // we need to include an initial [0] to index into the pointer.
58 vector<Value*> idxVec(1, *OI);
59 PointerType* ptrType = cast<PointerType>(lastPtr->getType());
60 if (isa<StructType>(ptrType->getElementType())
61 && ! ptrType->indexValid(*OI))
62 idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
64 // Get the type obtained by applying the first index.
65 // It must be a structure or array.
66 const Type* nextType = MemAccessInst::getIndexedType(lastPtr->getType(),
68 assert(isa<StructType>(nextType) || isa<ArrayType>(nextType));
70 // Get a pointer to the structure or to the elements of the array.
71 const Type* nextPtrType =
72 PointerType::get(isa<StructType>(nextType)? nextType
73 : cast<ArrayType>(nextType)->getElementType());
75 // Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec
76 // This is not needed if the index is zero.
82 gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1");
83 newIvec.push_back(cast<Instruction>(gepValue));
86 // Instruction 2: nextPtr2 = cast nextPtr1 to nextPtrType
87 // This is not needed if the two types are identical.
89 if (gepValue->getType() == nextPtrType)
93 castInst = new CastInst(gepValue, nextPtrType, "ptr2");
94 newIvec.push_back(cast<Instruction>(castInst));
101 // Now create a new instruction to replace the original one
103 PointerType* ptrType = cast<PointerType>(lastPtr->getType());
106 // First, get the final index vector. As above, we may need an initial [0].
107 vector<Value*> idxVec(1, *OI);
108 if (isa<StructType>(ptrType->getElementType())
109 && ! ptrType->indexValid(*OI))
110 idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
112 const std::string newInstName = memI->hasName()? memI->getName()
113 : string("finalRef");
114 Instruction* newInst = NULL;
116 switch(memI->getOpcode())
118 case Instruction::Load:
119 newInst = new LoadInst(lastPtr, idxVec /*, newInstName */); break;
120 case Instruction::Store:
121 newInst = new StoreInst(memI->getOperand(0),
122 lastPtr, idxVec /*, newInstName */); break;
124 case Instruction::GetElementPtr:
125 newInst = new GetElementPtrInst(lastPtr, idxVec /*, newInstName */); break;
127 assert(0 && "Unrecognized memory access instruction"); break;
130 newIvec.push_back(newInst);
132 // Replace all uses of the old instruction with the new
133 memI->replaceAllUsesWith(newInst);
135 BasicBlock::iterator newI = BBI;;
136 for (int i = newIvec.size()-1; i >= 0; i--)
137 newI = BB->getInstList().insert(newI, newIvec[i]);
139 // Now delete the old instruction and return a pointer to the last new one
140 BB->getInstList().remove(memI);
143 return newI + newIvec.size() - 1; // pointer to last new instr
147 //---------------------------------------------------------------------------
148 // Entry point for array or structure references with multiple indices.
149 //---------------------------------------------------------------------------
152 doDecomposeMultiDimRefs(Function *F)
154 bool changed = false;
156 for (Method::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
157 for (BasicBlock::iterator newI, II = (*BI)->begin();
158 II != (*BI)->end(); II = ++newI)
161 if (MemAccessInst *memI = dyn_cast<MemAccessInst>(*II))
162 if (memI->getNumOperands() > 1 + memI->getFirstIndexOperandNumber())
164 newI = decomposeArrayRef(II);
174 struct DecomposeMultiDimRefsPass : public MethodPass {
175 virtual bool runOnMethod(Function *F) { return doDecomposeMultiDimRefs(F); }
179 Pass *createDecomposeMultiDimRefsPass() { return new DecomposeMultiDimRefsPass(); }