blob: f4bb51a02d1fbac66a7a64d1a03c6a8fd3183731 [file] [log] [blame]
Chris Lattner2f6f03b2002-04-29 01:22:55 +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"
Chris Lattner31bcdb82002-04-28 19:55:58 +000013#include "llvm/Constants.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
Chris Lattner2f6f03b2002-04-29 01:22:55 +000020namespace {
21 struct DecomposePass : public BasicBlockPass {
22 virtual bool runOnBasicBlock(BasicBlock *BB);
23
24 private:
25 static void decomposeArrayRef(BasicBlock::iterator &BBI);
26 };
27}
28
29Pass *createDecomposeMultiDimRefsPass() {
30 return new DecomposePass();
31}
32
33
34// runOnBasicBlock - Entry point for array or structure references with multiple
35// indices.
36//
37bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
38 bool Changed = false;
39
40 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ) {
41 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) {
42 if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
43 decomposeArrayRef(II);
44 Changed = true;
45 } else {
46 ++II;
47 }
48 } else {
49 ++II;
50 }
51 }
52
53 return Changed;
54}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000055
56//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000057// For any combination of 2 or more array and structure indices,
58// this function repeats the foll. until we have a one-dim. reference: {
59// ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex
60// ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] *
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000061// }
62// Then it replaces the original instruction with an equivalent one that
Vikram S. Adve98d64f82002-03-24 03:21:18 +000063// uses the last ptr2 generated in the loop and a single index.
64// If any index is (uint) 0, we omit the getElementPtr instruction.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000065//
Chris Lattner2f6f03b2002-04-29 01:22:55 +000066void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI){
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000067 MemAccessInst *memI = cast<MemAccessInst>(*BBI);
68 BasicBlock* BB = memI->getParent();
69 Value* lastPtr = memI->getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000070
71 // Remove the instruction from the stream
72 BB->getInstList().remove(BBI);
73
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000074 vector<Instruction*> newIvec;
75
Vikram S. Adve98d64f82002-03-24 03:21:18 +000076 // Process each index except the last one.
77 //
Chris Lattner2f6f03b2002-04-29 01:22:55 +000078 User::const_op_iterator OI = memI->idx_begin(), OE = memI->idx_end();
79 for (; OI != OE && OI+1 != OE; ++OI) {
80 assert(isa<PointerType>(lastPtr->getType()));
Vikram S. Adve98d64f82002-03-24 03:21:18 +000081
Chris Lattner2f6f03b2002-04-29 01:22:55 +000082 // Check for a zero index. This will need a cast instead of
83 // a getElementPtr, or it may need neither.
84 bool indexIsZero = isa<ConstantUInt>(*OI) &&
85 cast<Constant>(*OI)->isNullValue();
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000086
Chris Lattner2f6f03b2002-04-29 01:22:55 +000087 // 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 vector<Value*> idxVec(1, *OI);
91 PointerType* ptrType = cast<PointerType>(lastPtr->getType());
92 if (isa<StructType>(ptrType->getElementType())
93 && ! ptrType->indexValid(*OI))
94 idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
95
96 // Get the type obtained by applying the first index.
97 // It must be a structure or array.
98 const Type* nextType = MemAccessInst::getIndexedType(lastPtr->getType(),
99 idxVec, true);
100 assert(isa<StructType>(nextType) || isa<ArrayType>(nextType));
101
102 // Get a pointer to the structure or to the elements of the array.
103 const Type* nextPtrType =
104 PointerType::get(isa<StructType>(nextType) ? nextType
105 : cast<ArrayType>(nextType)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000106
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000107 // Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec
108 // This is not needed if the index is zero.
109 Value *gepValue;
110 if (indexIsZero)
111 gepValue = lastPtr;
112 else {
113 gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1");
114 newIvec.push_back(cast<Instruction>(gepValue));
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000115 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000116
117 // Instruction 2: nextPtr2 = cast nextPtr1 to nextPtrType
118 // This is not needed if the two types are identical.
119 Value *castInst;
120 if (gepValue->getType() == nextPtrType)
121 castInst = gepValue;
122 else {
123 castInst = new CastInst(gepValue, nextPtrType, "ptr2");
124 newIvec.push_back(cast<Instruction>(castInst));
125 }
126
127 lastPtr = castInst;
128 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000129
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000130 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000131 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000132 //
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000133 PointerType *ptrType = cast<PointerType>(lastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000134
135 // First, get the final index vector. As above, we may need an initial [0].
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000136 vector<Value*> idxVec(1, *OI);
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000137 if (isa<StructType>(ptrType->getElementType())
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000138 && !ptrType->indexValid(*OI))
139 idxVec.insert(idxVec.begin(), Constant::getNullValue(Type::UIntTy));
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000140
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000141 Instruction* newInst = NULL;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000142 switch(memI->getOpcode()) {
143 case Instruction::Load:
144 newInst = new LoadInst(lastPtr, idxVec, memI->getName());
145 break;
146 case Instruction::Store:
147 newInst = new StoreInst(memI->getOperand(0), lastPtr, idxVec);
148 break;
149 case Instruction::GetElementPtr:
150 newInst = new GetElementPtrInst(lastPtr, idxVec, memI->getName());
151 break;
152 default:
153 assert(0 && "Unrecognized memory access instruction");
154 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000155 newIvec.push_back(newInst);
156
157 // Replace all uses of the old instruction with the new
158 memI->replaceAllUsesWith(newInst);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000159
160 // Now delete the old instruction...
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000161 delete memI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000162
163 // Convert our iterator into an index... that cannot get invalidated
164 unsigned ItOffs = BBI-BB->begin();
165
166 // Insert all of the new instructions...
167 BB->getInstList().insert(BBI, newIvec.begin(), newIvec.end());
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000168
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000169 // Advance the iterator to the instruction following the one just inserted...
170 BBI = BB->begin() + (ItOffs+newIvec.size());
Chris Lattnerf57b8452002-04-27 06:56:12 +0000171}