blob: 5d873cda2e1f0c90bd401fd38225069cc6dc2edc [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 Lattner7e708292002-06-25 16:13:24 +000024 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000025
26 private:
27 static void decomposeArrayRef(BasicBlock::iterator &BBI);
28 };
Chris Lattnerf6293092002-07-23 18:06:35 +000029
30RegisterPass<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
31 "structure/array references");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000032}
33
34Pass *createDecomposeMultiDimRefsPass() {
35 return new DecomposePass();
36}
37
38
39// runOnBasicBlock - Entry point for array or structure references with multiple
40// indices.
41//
Chris Lattner7e708292002-06-25 16:13:24 +000042bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +000043 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000044 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
45 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(&*II)) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +000046 if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
47 decomposeArrayRef(II);
48 Changed = true;
49 } else {
50 ++II;
51 }
52 } else {
53 ++II;
54 }
55 }
56
57 return Changed;
58}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000059
60//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000061// For any combination of 2 or more array and structure indices,
62// this function repeats the foll. until we have a one-dim. reference: {
63// ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex
64// ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] *
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000065// }
66// Then it replaces the original instruction with an equivalent one that
Vikram S. Adve98d64f82002-03-24 03:21:18 +000067// uses the last ptr2 generated in the loop and a single index.
68// If any index is (uint) 0, we omit the getElementPtr instruction.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000069//
Anand Shukla5ba99bd2002-06-25 21:07:58 +000070
Chris Lattner097632e2002-04-29 01:58:47 +000071void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +000072 MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
73 BasicBlock *BB = MAI.getParent();
74 Value *LastPtr = MAI.getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000075
76 // Remove the instruction from the stream
77 BB->getInstList().remove(BBI);
78
Anand Shukla5ba99bd2002-06-25 21:07:58 +000079 std::vector<Instruction*> NewInsts;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000080
Vikram S. Adve98d64f82002-03-24 03:21:18 +000081 // Process each index except the last one.
82 //
Anand Shukla5ba99bd2002-06-25 21:07:58 +000083
Chris Lattner7e708292002-06-25 16:13:24 +000084 User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000085 for (; OI+1 != OE; ++OI) {
86 assert(isa<PointerType>(LastPtr->getType()));
Vikram S. Adve98d64f82002-03-24 03:21:18 +000087
Chris Lattner2f6f03b2002-04-29 01:22:55 +000088 // Check for a zero index. This will need a cast instead of
89 // a getElementPtr, or it may need neither.
Chris Lattner96c466b2002-04-29 14:57:45 +000090 bool indexIsZero = isa<Constant>(*OI) &&
Chris Lattner7e708292002-06-25 16:13:24 +000091 cast<Constant>(OI->get())->isNullValue() &&
92 OI->get()->getType() == Type::UIntTy;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000093
Chris Lattner2f6f03b2002-04-29 01:22:55 +000094 // Extract the first index. If the ptr is a pointer to a structure
95 // and the next index is a structure offset (i.e., not an array offset),
96 // we need to include an initial [0] to index into the pointer.
Chris Lattner097632e2002-04-29 01:58:47 +000097 //
Anand Shukla5ba99bd2002-06-25 21:07:58 +000098
99 std::vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +0000100 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000101
Chris Lattner097632e2002-04-29 01:58:47 +0000102 if (isa<StructType>(PtrTy->getElementType())
103 && !PtrTy->indexValid(*OI))
104 Indices.push_back(Constant::getNullValue(Type::UIntTy));
105 Indices.push_back(*OI);
106
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000107 // Get the type obtained by applying the first index.
108 // It must be a structure or array.
Chris Lattner097632e2002-04-29 01:58:47 +0000109 const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
110 Indices, true);
111 assert(isa<CompositeType>(NextTy));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000112
113 // Get a pointer to the structure or to the elements of the array.
Chris Lattner097632e2002-04-29 01:58:47 +0000114 const Type *NextPtrTy =
115 PointerType::get(isa<StructType>(NextTy) ? NextTy
116 : cast<ArrayType>(NextTy)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000117
Chris Lattner097632e2002-04-29 01:58:47 +0000118 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000119 // This is not needed if the index is zero.
Chris Lattner097632e2002-04-29 01:58:47 +0000120 if (!indexIsZero) {
121 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
122 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner3dec1f22002-05-10 15:38:35 +0000123 ++NumAdded;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000124 }
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000125
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000126
Chris Lattner097632e2002-04-29 01:58:47 +0000127 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000128 // This is not needed if the two types are identical.
Chris Lattner097632e2002-04-29 01:58:47 +0000129 //
130 if (LastPtr->getType() != NextPtrTy) {
131 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
132 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner3dec1f22002-05-10 15:38:35 +0000133 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000134 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000135 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000136
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000137 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000138 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000139 //
Chris Lattner7e708292002-06-25 16:13:24 +0000140 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000141
142 // First, get the final index vector. As above, we may need an initial [0].
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000143
144 std::vector<Value*> Indices;
Chris Lattner097632e2002-04-29 01:58:47 +0000145 if (isa<StructType>(PtrTy->getElementType())
146 && !PtrTy->indexValid(*OI))
147 Indices.push_back(Constant::getNullValue(Type::UIntTy));
148
149 Indices.push_back(*OI);
150
151 Instruction *NewI = 0;
Chris Lattner7e708292002-06-25 16:13:24 +0000152 switch(MAI.getOpcode()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000153 case Instruction::Load:
Chris Lattner7e708292002-06-25 16:13:24 +0000154 NewI = new LoadInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000155 break;
156 case Instruction::Store:
Chris Lattner7e708292002-06-25 16:13:24 +0000157 NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000158 break;
159 case Instruction::GetElementPtr:
Chris Lattner7e708292002-06-25 16:13:24 +0000160 NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000161 break;
162 default:
163 assert(0 && "Unrecognized memory access instruction");
164 }
Chris Lattner097632e2002-04-29 01:58:47 +0000165 NewInsts.push_back(NewI);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000166
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000167
168 // Replace all uses of the old instruction with the new
Chris Lattner7e708292002-06-25 16:13:24 +0000169 MAI.replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000170
171 // Now delete the old instruction...
Chris Lattner7e708292002-06-25 16:13:24 +0000172 delete &MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000173
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000174 // Insert all of the new instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000175 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000176
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000177 // Advance the iterator to the instruction following the one just inserted...
Chris Lattner7e708292002-06-25 16:13:24 +0000178 BBI = NewInsts.back();
179 ++BBI;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000180}