blob: 0ec72c65e011a088e858790521236ca57c9c9368 [file] [log] [blame]
Chris Lattnerf57b8452002-04-27 06:56:12 +00001//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D ---=//
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +00002//
Vikram S. Adve98d64f82002-03-24 03:21:18 +00003// DecomposeMultiDimRefs -
4// Convert multi-dimensional references consisting of any combination
5// of 2 or more array and structure indices into a sequence of
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +00006// instructions (using getelementpr and cast) so that each instruction
Vikram S. Adve98d64f82002-03-24 03:21:18 +00007// has at most one index (except structure references,
8// which need an extra leading index of [0]).
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +00009//
Chris Lattnerf57b8452002-04-27 06:56:12 +000010//===----------------------------------------------------------------------===//
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000011
Vikram S. Adve98d64f82002-03-24 03:21:18 +000012#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
13#include "llvm/ConstantVals.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000014#include "llvm/iMemory.h"
15#include "llvm/iOther.h"
16#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000018#include "llvm/Pass.h"
19
20
21//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000022// 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] *
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000026// }
27// Then it replaces the original instruction with an equivalent one that
Vikram S. Adve98d64f82002-03-24 03:21:18 +000028// uses the last ptr2 generated in the loop and a single index.
29// If any index is (uint) 0, we omit the getElementPtr instruction.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000030//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000031static BasicBlock::iterator
32decomposeArrayRef(BasicBlock::iterator& BBI)
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000033{
34 MemAccessInst *memI = cast<MemAccessInst>(*BBI);
35 BasicBlock* BB = memI->getParent();
36 Value* lastPtr = memI->getPointerOperand();
37 vector<Instruction*> newIvec;
38
Vikram S. Adve98d64f82002-03-24 03:21:18 +000039 // Process each index except the last one.
40 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000041 MemAccessInst::const_op_iterator OI = memI->idx_begin();
Vikram S. Adve98d64f82002-03-24 03:21:18 +000042 MemAccessInst::const_op_iterator OE = memI->idx_end();
43 for ( ; OI != OE; ++OI)
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000044 {
Vikram S. Adve98d64f82002-03-24 03:21:18 +000045 assert(isa<PointerType>(lastPtr->getType()));
46
47 if (OI+1 == OE) // stop before the last operand
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000048 break;
49
Vikram S. Adve98d64f82002-03-24 03:21:18 +000050 // 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);
54
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.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000058 vector<Value*> idxVec(1, *OI);
Vikram S. Adve98d64f82002-03-24 03:21:18 +000059 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));
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000063
Vikram S. Adve98d64f82002-03-24 03:21:18 +000064 // 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(),
67 idxVec, true);
68 assert(isa<StructType>(nextType) || isa<ArrayType>(nextType));
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000069
Vikram S. Adve98d64f82002-03-24 03:21:18 +000070 // 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());
74
75 // Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec
76 // This is not needed if the index is zero.
77 Value* gepValue;
78 if (indexIsZero)
79 gepValue = lastPtr;
80 else
81 {
82 gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1");
83 newIvec.push_back(cast<Instruction>(gepValue));
84 }
85
86 // Instruction 2: nextPtr2 = cast nextPtr1 to nextPtrType
87 // This is not needed if the two types are identical.
88 Value* castInst;
89 if (gepValue->getType() == nextPtrType)
90 castInst = gepValue;
91 else
92 {
93 castInst = new CastInst(gepValue, nextPtrType, "ptr2");
94 newIvec.push_back(cast<Instruction>(castInst));
95 }
96
97 lastPtr = castInst;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000098 }
99
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000100 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000101 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000102 //
103 PointerType* ptrType = cast<PointerType>(lastPtr->getType());
104 assert(ptrType);
105
106 // First, get the final index vector. As above, we may need an initial [0].
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000107 vector<Value*> idxVec(1, *OI);
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000108 if (isa<StructType>(ptrType->getElementType())
109 && ! ptrType->indexValid(*OI))
110 idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
111
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000112 const std::string newInstName = memI->hasName()? memI->getName()
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000113 : string("finalRef");
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000114 Instruction* newInst = NULL;
115
116 switch(memI->getOpcode())
117 {
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;
123 break;
124 case Instruction::GetElementPtr:
125 newInst = new GetElementPtrInst(lastPtr, idxVec /*, newInstName */); break;
126 default:
127 assert(0 && "Unrecognized memory access instruction"); break;
128 }
129
130 newIvec.push_back(newInst);
131
132 // Replace all uses of the old instruction with the new
133 memI->replaceAllUsesWith(newInst);
134
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000135 BasicBlock::iterator newI = BBI;;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000136 for (int i = newIvec.size()-1; i >= 0; i--)
137 newI = BB->getInstList().insert(newI, newIvec[i]);
138
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000139 // Now delete the old instruction and return a pointer to the last new one
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000140 BB->getInstList().remove(memI);
141 delete memI;
142
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000143 return newI + newIvec.size() - 1; // pointer to last new instr
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000144}
145
146
147//---------------------------------------------------------------------------
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000148// Entry point for array or structure references with multiple indices.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000149//---------------------------------------------------------------------------
150
151static bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000152doDecomposeMultiDimRefs(Function *F)
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000153{
154 bool changed = false;
155
Chris Lattner237e6d12002-04-08 22:03:00 +0000156 for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000157 for (BasicBlock::iterator newI, II = (*BI)->begin();
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000158 II != (*BI)->end(); II = ++newI)
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000159 {
160 newI = II;
161 if (MemAccessInst *memI = dyn_cast<MemAccessInst>(*II))
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000162 if (memI->getNumOperands() > 1 + memI->getFirstIndexOperandNumber())
163 {
164 newI = decomposeArrayRef(II);
165 changed = true;
166 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000167 }
168
169 return changed;
170}
171
172
173namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000174 struct DecomposeMultiDimRefsPass : public FunctionPass {
175 virtual bool runOnFunction(Function *F) {
176 return doDecomposeMultiDimRefs(F);
177 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000178 };
179}
180
Chris Lattnerf57b8452002-04-27 06:56:12 +0000181Pass *createDecomposeMultiDimRefsPass() {
182 return new DecomposeMultiDimRefsPass();
183}