blob: 1eb582ebc4c79f1161d2847b7d030f15dbd35a00 [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//
Chris Lattner097632e2002-04-29 01:58:47 +00003// 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]).
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +00008//
Chris Lattnerf57b8452002-04-27 06:56:12 +00009//===----------------------------------------------------------------------===//
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000010
Vikram S. Adve98d64f82002-03-24 03:21:18 +000011#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
Chris Lattner96c466b2002-04-29 14:57:45 +000012#include "llvm/Constant.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000013#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
15#include "llvm/BasicBlock.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000016#include "llvm/Pass.h"
17
Chris Lattner2f6f03b2002-04-29 01:22:55 +000018namespace {
19 struct DecomposePass : public BasicBlockPass {
Chris Lattner96c466b2002-04-29 14:57:45 +000020 const char *getPassName() const { return "Decompose Subscripting Exps"; }
21
Chris Lattner2f6f03b2002-04-29 01:22:55 +000022 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;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000039 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}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000054
55//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000056// 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] *
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000060// }
61// Then it replaces the original instruction with an equivalent one that
Vikram S. Adve98d64f82002-03-24 03:21:18 +000062// uses the last ptr2 generated in the loop and a single index.
63// If any index is (uint) 0, we omit the getElementPtr instruction.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000064//
Chris Lattner097632e2002-04-29 01:58:47 +000065void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
66 MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
67 BasicBlock *BB = MAI->getParent();
68 Value *LastPtr = MAI->getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000069
70 // Remove the instruction from the stream
71 BB->getInstList().remove(BBI);
72
Chris Lattner097632e2002-04-29 01:58:47 +000073 vector<Instruction*> NewInsts;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000074
Vikram S. Adve98d64f82002-03-24 03:21:18 +000075 // Process each index except the last one.
76 //
Chris Lattner097632e2002-04-29 01:58:47 +000077 User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end();
78 for (; OI+1 != OE; ++OI) {
79 assert(isa<PointerType>(LastPtr->getType()));
Vikram S. Adve98d64f82002-03-24 03:21:18 +000080
Chris Lattner2f6f03b2002-04-29 01:22:55 +000081 // Check for a zero index. This will need a cast instead of
82 // a getElementPtr, or it may need neither.
Chris Lattner96c466b2002-04-29 14:57:45 +000083 bool indexIsZero = isa<Constant>(*OI) &&
84 cast<Constant>(*OI)->isNullValue() &&
85 (*OI)->getType() == Type::UIntTy;
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.
Chris Lattner097632e2002-04-29 01:58:47 +000090 //
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
Chris Lattner2f6f03b2002-04-29 01:22:55 +000098 // Get the type obtained by applying the first index.
99 // It must be a structure or array.
Chris Lattner097632e2002-04-29 01:58:47 +0000100 const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
101 Indices, true);
102 assert(isa<CompositeType>(NextTy));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000103
104 // Get a pointer to the structure or to the elements of the array.
Chris Lattner097632e2002-04-29 01:58:47 +0000105 const Type *NextPtrTy =
106 PointerType::get(isa<StructType>(NextTy) ? NextTy
107 : cast<ArrayType>(NextTy)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000108
Chris Lattner097632e2002-04-29 01:58:47 +0000109 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000110 // This is not needed if the index is zero.
Chris Lattner097632e2002-04-29 01:58:47 +0000111 if (!indexIsZero) {
112 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
113 NewInsts.push_back(cast<Instruction>(LastPtr));
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000114 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000115
Chris Lattner097632e2002-04-29 01:58:47 +0000116 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000117 // This is not needed if the two types are identical.
Chris Lattner097632e2002-04-29 01:58:47 +0000118 //
119 if (LastPtr->getType() != NextPtrTy) {
120 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
121 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000122 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000123 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000124
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000125 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000126 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000127 //
Chris Lattner097632e2002-04-29 01:58:47 +0000128 PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000129
130 // First, get the final index vector. As above, we may need an initial [0].
Chris Lattner097632e2002-04-29 01:58:47 +0000131 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()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000140 case Instruction::Load:
Chris Lattner097632e2002-04-29 01:58:47 +0000141 NewI = new LoadInst(LastPtr, Indices, MAI->getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000142 break;
143 case Instruction::Store:
Chris Lattner097632e2002-04-29 01:58:47 +0000144 NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000145 break;
146 case Instruction::GetElementPtr:
Chris Lattner097632e2002-04-29 01:58:47 +0000147 NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000148 break;
149 default:
150 assert(0 && "Unrecognized memory access instruction");
151 }
Chris Lattner097632e2002-04-29 01:58:47 +0000152 NewInsts.push_back(NewI);
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000153
154 // Replace all uses of the old instruction with the new
Chris Lattner097632e2002-04-29 01:58:47 +0000155 MAI->replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000156
157 // Now delete the old instruction...
Chris Lattner097632e2002-04-29 01:58:47 +0000158 delete MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000159
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...
Chris Lattner097632e2002-04-29 01:58:47 +0000164 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000165
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000166 // Advance the iterator to the instruction following the one just inserted...
Chris Lattner097632e2002-04-29 01:58:47 +0000167 BBI = BB->begin() + ItOffs + NewInsts.size();
Chris Lattnerf57b8452002-04-27 06:56:12 +0000168}