1 //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
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
9 //===----------------------------------------------------------------------===//
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"
19 struct DecomposePass : public BasicBlockPass {
20 const char *getPassName() const { return "Decompose Subscripting Exps"; }
22 virtual bool runOnBasicBlock(BasicBlock *BB);
25 static void decomposeArrayRef(BasicBlock::iterator &BBI);
29 Pass *createDecomposeMultiDimRefsPass() {
30 return new DecomposePass();
34 // runOnBasicBlock - Entry point for array or structure references with multiple
37 bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
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);
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] *
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.
65 void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
66 MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
67 BasicBlock *BB = MAI->getParent();
68 Value *LastPtr = MAI->getPointerOperand();
70 // Remove the instruction from the stream
71 BB->getInstList().remove(BBI);
73 vector<Instruction*> NewInsts;
75 // Process each index except the last one.
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()));
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;
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.
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);
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(),
102 assert(isa<CompositeType>(NextTy));
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());
109 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
110 // This is not needed if the index is zero.
112 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
113 NewInsts.push_back(cast<Instruction>(LastPtr));
116 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
117 // This is not needed if the two types are identical.
119 if (LastPtr->getType() != NextPtrTy) {
120 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
121 NewInsts.push_back(cast<Instruction>(LastPtr));
126 // Now create a new instruction to replace the original one
128 PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
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));
136 Indices.push_back(*OI);
138 Instruction *NewI = 0;
139 switch(MAI->getOpcode()) {
140 case Instruction::Load:
141 NewI = new LoadInst(LastPtr, Indices, MAI->getName());
143 case Instruction::Store:
144 NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
146 case Instruction::GetElementPtr:
147 NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
150 assert(0 && "Unrecognized memory access instruction");
152 NewInsts.push_back(NewI);
154 // Replace all uses of the old instruction with the new
155 MAI->replaceAllUsesWith(NewI);
157 // Now delete the old instruction...
160 // Convert our iterator into an index... that cannot get invalidated
161 unsigned ItOffs = BBI-BB->begin();
163 // Insert all of the new instructions...
164 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
166 // Advance the iterator to the instruction following the one just inserted...
167 BBI = BB->begin() + ItOffs + NewInsts.size();