blob: 4637273a46df6e14bbd5130b371b6b3158dde650 [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 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"
18
Chris Lattner2f6f03b2002-04-29 01:22:55 +000019namespace {
20 struct DecomposePass : public BasicBlockPass {
Chris Lattner96c466b2002-04-29 14:57:45 +000021 const char *getPassName() const { return "Decompose Subscripting Exps"; }
22
Chris Lattner2f6f03b2002-04-29 01:22:55 +000023 virtual bool runOnBasicBlock(BasicBlock *BB);
24
25 private:
26 static void decomposeArrayRef(BasicBlock::iterator &BBI);
27 };
28}
29
30Pass *createDecomposeMultiDimRefsPass() {
31 return new DecomposePass();
32}
33
34
35// runOnBasicBlock - Entry point for array or structure references with multiple
36// indices.
37//
38bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
39 bool Changed = false;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000040 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 Lattner097632e2002-04-29 01:58:47 +000066void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
67 MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
68 BasicBlock *BB = MAI->getParent();
69 Value *LastPtr = MAI->getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000070
71 // Remove the instruction from the stream
72 BB->getInstList().remove(BBI);
73
Chris Lattner097632e2002-04-29 01:58:47 +000074 vector<Instruction*> NewInsts;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000075
Vikram S. Adve98d64f82002-03-24 03:21:18 +000076 // Process each index except the last one.
77 //
Chris Lattner097632e2002-04-29 01:58:47 +000078 User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end();
79 for (; 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.
Chris Lattner96c466b2002-04-29 14:57:45 +000084 bool indexIsZero = isa<Constant>(*OI) &&
85 cast<Constant>(*OI)->isNullValue() &&
86 (*OI)->getType() == Type::UIntTy;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000087
Chris Lattner2f6f03b2002-04-29 01:22:55 +000088 // Extract the first index. If the ptr is a pointer to a structure
89 // and the next index is a structure offset (i.e., not an array offset),
90 // we need to include an initial [0] to index into the pointer.
Chris Lattner097632e2002-04-29 01:58:47 +000091 //
92 vector<Value*> Indices;
93 PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
94 if (isa<StructType>(PtrTy->getElementType())
95 && !PtrTy->indexValid(*OI))
96 Indices.push_back(Constant::getNullValue(Type::UIntTy));
97 Indices.push_back(*OI);
98
Chris Lattner2f6f03b2002-04-29 01:22:55 +000099 // Get the type obtained by applying the first index.
100 // It must be a structure or array.
Chris Lattner097632e2002-04-29 01:58:47 +0000101 const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
102 Indices, true);
103 assert(isa<CompositeType>(NextTy));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000104
105 // Get a pointer to the structure or to the elements of the array.
Chris Lattner097632e2002-04-29 01:58:47 +0000106 const Type *NextPtrTy =
107 PointerType::get(isa<StructType>(NextTy) ? NextTy
108 : cast<ArrayType>(NextTy)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000109
Chris Lattner097632e2002-04-29 01:58:47 +0000110 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000111 // This is not needed if the index is zero.
Chris Lattner097632e2002-04-29 01:58:47 +0000112 if (!indexIsZero) {
113 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
114 NewInsts.push_back(cast<Instruction>(LastPtr));
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000115 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000116
Chris Lattner097632e2002-04-29 01:58:47 +0000117 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000118 // This is not needed if the two types are identical.
Chris Lattner097632e2002-04-29 01:58:47 +0000119 //
120 if (LastPtr->getType() != NextPtrTy) {
121 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
122 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000123 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000124 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000125
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000126 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000127 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000128 //
Chris Lattner097632e2002-04-29 01:58:47 +0000129 PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000130
131 // First, get the final index vector. As above, we may need an initial [0].
Chris Lattner097632e2002-04-29 01:58:47 +0000132 vector<Value*> Indices;
133 if (isa<StructType>(PtrTy->getElementType())
134 && !PtrTy->indexValid(*OI))
135 Indices.push_back(Constant::getNullValue(Type::UIntTy));
136
137 Indices.push_back(*OI);
138
139 Instruction *NewI = 0;
140 switch(MAI->getOpcode()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000141 case Instruction::Load:
Chris Lattner097632e2002-04-29 01:58:47 +0000142 NewI = new LoadInst(LastPtr, Indices, MAI->getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000143 break;
144 case Instruction::Store:
Chris Lattner097632e2002-04-29 01:58:47 +0000145 NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000146 break;
147 case Instruction::GetElementPtr:
Chris Lattner097632e2002-04-29 01:58:47 +0000148 NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000149 break;
150 default:
151 assert(0 && "Unrecognized memory access instruction");
152 }
Chris Lattner097632e2002-04-29 01:58:47 +0000153 NewInsts.push_back(NewI);
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000154
155 // Replace all uses of the old instruction with the new
Chris Lattner097632e2002-04-29 01:58:47 +0000156 MAI->replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000157
158 // Now delete the old instruction...
Chris Lattner097632e2002-04-29 01:58:47 +0000159 delete MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000160
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000161 // Insert all of the new instructions...
Chris Lattnerf7371212002-04-29 21:25:34 +0000162 BBI = BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000163
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000164 // Advance the iterator to the instruction following the one just inserted...
Chris Lattnerf7371212002-04-29 21:25:34 +0000165 BBI += NewInsts.size();
Chris Lattnerf57b8452002-04-27 06:56:12 +0000166}