blob: ab6059a88f66122023ca3ff49807e5bb6a257618 [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//
Chris Lattner097632e2002-04-29 01:58:47 +000069void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +000070 MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
71 BasicBlock *BB = MAI.getParent();
72 Value *LastPtr = MAI.getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000073
74 // Remove the instruction from the stream
75 BB->getInstList().remove(BBI);
76
Chris Lattner097632e2002-04-29 01:58:47 +000077 vector<Instruction*> NewInsts;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000078
Vikram S. Adve98d64f82002-03-24 03:21:18 +000079 // Process each index except the last one.
80 //
Chris Lattner7e708292002-06-25 16:13:24 +000081 User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000082 for (; OI+1 != OE; ++OI) {
83 assert(isa<PointerType>(LastPtr->getType()));
Vikram S. Adve98d64f82002-03-24 03:21:18 +000084
Chris Lattner2f6f03b2002-04-29 01:22:55 +000085 // Check for a zero index. This will need a cast instead of
86 // a getElementPtr, or it may need neither.
Chris Lattner96c466b2002-04-29 14:57:45 +000087 bool indexIsZero = isa<Constant>(*OI) &&
Chris Lattner7e708292002-06-25 16:13:24 +000088 cast<Constant>(OI->get())->isNullValue() &&
89 OI->get()->getType() == Type::UIntTy;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000090
Chris Lattner2f6f03b2002-04-29 01:22:55 +000091 // Extract the first index. If the ptr is a pointer to a structure
92 // and the next index is a structure offset (i.e., not an array offset),
93 // we need to include an initial [0] to index into the pointer.
Chris Lattner097632e2002-04-29 01:58:47 +000094 //
95 vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +000096 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Chris Lattner097632e2002-04-29 01:58:47 +000097 if (isa<StructType>(PtrTy->getElementType())
98 && !PtrTy->indexValid(*OI))
99 Indices.push_back(Constant::getNullValue(Type::UIntTy));
100 Indices.push_back(*OI);
101
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000102 // Get the type obtained by applying the first index.
103 // It must be a structure or array.
Chris Lattner097632e2002-04-29 01:58:47 +0000104 const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
105 Indices, true);
106 assert(isa<CompositeType>(NextTy));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000107
108 // Get a pointer to the structure or to the elements of the array.
Chris Lattner097632e2002-04-29 01:58:47 +0000109 const Type *NextPtrTy =
110 PointerType::get(isa<StructType>(NextTy) ? NextTy
111 : cast<ArrayType>(NextTy)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000112
Chris Lattner097632e2002-04-29 01:58:47 +0000113 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000114 // This is not needed if the index is zero.
Chris Lattner097632e2002-04-29 01:58:47 +0000115 if (!indexIsZero) {
116 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
117 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner3dec1f22002-05-10 15:38:35 +0000118 ++NumAdded;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000119 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000120
Chris Lattner097632e2002-04-29 01:58:47 +0000121 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000122 // This is not needed if the two types are identical.
Chris Lattner097632e2002-04-29 01:58:47 +0000123 //
124 if (LastPtr->getType() != NextPtrTy) {
125 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
126 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner3dec1f22002-05-10 15:38:35 +0000127 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000128 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000129 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000130
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000131 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000132 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000133 //
Chris Lattner7e708292002-06-25 16:13:24 +0000134 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000135
136 // First, get the final index vector. As above, we may need an initial [0].
Chris Lattner097632e2002-04-29 01:58:47 +0000137 vector<Value*> Indices;
138 if (isa<StructType>(PtrTy->getElementType())
139 && !PtrTy->indexValid(*OI))
140 Indices.push_back(Constant::getNullValue(Type::UIntTy));
141
142 Indices.push_back(*OI);
143
144 Instruction *NewI = 0;
Chris Lattner7e708292002-06-25 16:13:24 +0000145 switch(MAI.getOpcode()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000146 case Instruction::Load:
Chris Lattner7e708292002-06-25 16:13:24 +0000147 NewI = new LoadInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000148 break;
149 case Instruction::Store:
Chris Lattner7e708292002-06-25 16:13:24 +0000150 NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000151 break;
152 case Instruction::GetElementPtr:
Chris Lattner7e708292002-06-25 16:13:24 +0000153 NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000154 break;
155 default:
156 assert(0 && "Unrecognized memory access instruction");
157 }
Chris Lattner097632e2002-04-29 01:58:47 +0000158 NewInsts.push_back(NewI);
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000159
160 // Replace all uses of the old instruction with the new
Chris Lattner7e708292002-06-25 16:13:24 +0000161 MAI.replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000162
163 // Now delete the old instruction...
Chris Lattner7e708292002-06-25 16:13:24 +0000164 delete &MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000165
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000166 // Insert all of the new instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000167 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.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...
Chris Lattner7e708292002-06-25 16:13:24 +0000170 BBI = NewInsts.back();
171 ++BBI;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000172}