blob: b18095027bd4fe314d2128e1a8e00436fea41e79 [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
Chris Lattner022103b2002-05-07 20:03:00 +000011#include "llvm/Transforms/Scalar.h"
Chris Lattner72a1d4e2002-04-29 18:48:30 +000012#include "llvm/DerivedTypes.h"
Chris Lattner96c466b2002-04-29 14:57:45 +000013#include "llvm/Constant.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000014#include "llvm/iMemory.h"
15#include "llvm/iOther.h"
16#include "llvm/BasicBlock.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000017#include "llvm/Pass.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000018#include "Support/StatisticReporter.h"
19
20static Statistic<> NumAdded("lowerrefs\t\t- New instructions added");
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000021
Chris Lattner2f6f03b2002-04-29 01:22:55 +000022namespace {
23 struct DecomposePass : public BasicBlockPass {
Chris Lattner96c466b2002-04-29 14:57:45 +000024 const char *getPassName() const { return "Decompose Subscripting Exps"; }
25
Chris Lattner7e708292002-06-25 16:13:24 +000026 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000027
28 private:
29 static void decomposeArrayRef(BasicBlock::iterator &BBI);
30 };
31}
32
33Pass *createDecomposeMultiDimRefsPass() {
34 return new DecomposePass();
35}
36
37
38// runOnBasicBlock - Entry point for array or structure references with multiple
39// indices.
40//
Chris Lattner7e708292002-06-25 16:13:24 +000041bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +000042 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000043 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
44 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(&*II)) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +000045 if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
46 decomposeArrayRef(II);
47 Changed = true;
48 } else {
49 ++II;
50 }
51 } else {
52 ++II;
53 }
54 }
55
56 return Changed;
57}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000058
59//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000060// For any combination of 2 or more array and structure indices,
61// this function repeats the foll. until we have a one-dim. reference: {
62// ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex
63// ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] *
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000064// }
65// Then it replaces the original instruction with an equivalent one that
Vikram S. Adve98d64f82002-03-24 03:21:18 +000066// uses the last ptr2 generated in the loop and a single index.
67// If any index is (uint) 0, we omit the getElementPtr instruction.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000068//
Anand Shukla5ba99bd2002-06-25 21:07:58 +000069
Chris Lattner097632e2002-04-29 01:58:47 +000070void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +000071 MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
72 BasicBlock *BB = MAI.getParent();
73 Value *LastPtr = MAI.getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000074
75 // Remove the instruction from the stream
76 BB->getInstList().remove(BBI);
77
Anand Shukla5ba99bd2002-06-25 21:07:58 +000078 std::vector<Instruction*> NewInsts;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000079
Vikram S. Adve98d64f82002-03-24 03:21:18 +000080 // Process each index except the last one.
81 //
Anand Shukla5ba99bd2002-06-25 21:07:58 +000082
Chris Lattner7e708292002-06-25 16:13:24 +000083 User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000084 for (; OI+1 != OE; ++OI) {
85 assert(isa<PointerType>(LastPtr->getType()));
Vikram S. Adve98d64f82002-03-24 03:21:18 +000086
Chris Lattner2f6f03b2002-04-29 01:22:55 +000087 // Check for a zero index. This will need a cast instead of
88 // a getElementPtr, or it may need neither.
Chris Lattner96c466b2002-04-29 14:57:45 +000089 bool indexIsZero = isa<Constant>(*OI) &&
Chris Lattner7e708292002-06-25 16:13:24 +000090 cast<Constant>(OI->get())->isNullValue() &&
91 OI->get()->getType() == Type::UIntTy;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000092
Chris Lattner2f6f03b2002-04-29 01:22:55 +000093 // Extract the first index. If the ptr is a pointer to a structure
94 // and the next index is a structure offset (i.e., not an array offset),
95 // we need to include an initial [0] to index into the pointer.
Chris Lattner097632e2002-04-29 01:58:47 +000096 //
Anand Shukla5ba99bd2002-06-25 21:07:58 +000097
98 std::vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +000099 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000100
Chris Lattner097632e2002-04-29 01:58:47 +0000101 if (isa<StructType>(PtrTy->getElementType())
102 && !PtrTy->indexValid(*OI))
103 Indices.push_back(Constant::getNullValue(Type::UIntTy));
104 Indices.push_back(*OI);
105
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000106 // Get the type obtained by applying the first index.
107 // It must be a structure or array.
Chris Lattner097632e2002-04-29 01:58:47 +0000108 const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
109 Indices, true);
110 assert(isa<CompositeType>(NextTy));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000111
112 // Get a pointer to the structure or to the elements of the array.
Chris Lattner097632e2002-04-29 01:58:47 +0000113 const Type *NextPtrTy =
114 PointerType::get(isa<StructType>(NextTy) ? NextTy
115 : cast<ArrayType>(NextTy)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000116
Chris Lattner097632e2002-04-29 01:58:47 +0000117 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000118 // This is not needed if the index is zero.
Chris Lattner097632e2002-04-29 01:58:47 +0000119 if (!indexIsZero) {
120 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
121 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner3dec1f22002-05-10 15:38:35 +0000122 ++NumAdded;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000123 }
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000124
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000125
Chris Lattner097632e2002-04-29 01:58:47 +0000126 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000127 // This is not needed if the two types are identical.
Chris Lattner097632e2002-04-29 01:58:47 +0000128 //
129 if (LastPtr->getType() != NextPtrTy) {
130 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
131 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner3dec1f22002-05-10 15:38:35 +0000132 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000133 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000134 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000135
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000136 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000137 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000138 //
Chris Lattner7e708292002-06-25 16:13:24 +0000139 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000140
141 // First, get the final index vector. As above, we may need an initial [0].
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000142
143 std::vector<Value*> Indices;
Chris Lattner097632e2002-04-29 01:58:47 +0000144 if (isa<StructType>(PtrTy->getElementType())
145 && !PtrTy->indexValid(*OI))
146 Indices.push_back(Constant::getNullValue(Type::UIntTy));
147
148 Indices.push_back(*OI);
149
150 Instruction *NewI = 0;
Chris Lattner7e708292002-06-25 16:13:24 +0000151 switch(MAI.getOpcode()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000152 case Instruction::Load:
Chris Lattner7e708292002-06-25 16:13:24 +0000153 NewI = new LoadInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000154 break;
155 case Instruction::Store:
Chris Lattner7e708292002-06-25 16:13:24 +0000156 NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000157 break;
158 case Instruction::GetElementPtr:
Chris Lattner7e708292002-06-25 16:13:24 +0000159 NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000160 break;
161 default:
162 assert(0 && "Unrecognized memory access instruction");
163 }
Chris Lattner097632e2002-04-29 01:58:47 +0000164 NewInsts.push_back(NewI);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000165
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000166
167 // Replace all uses of the old instruction with the new
Chris Lattner7e708292002-06-25 16:13:24 +0000168 MAI.replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000169
170 // Now delete the old instruction...
Chris Lattner7e708292002-06-25 16:13:24 +0000171 delete &MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000172
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000173 // Insert all of the new instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000174 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000175
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000176 // Advance the iterator to the instruction following the one just inserted...
Chris Lattner7e708292002-06-25 16:13:24 +0000177 BBI = NewInsts.back();
178 ++BBI;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000179}