blob: 0367348ef5d81f62dac0218721456719198ae7ec [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 Lattner31bcdb82002-04-28 19:55:58 +000012#include "llvm/Constants.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000013#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
15#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000016#include "llvm/Function.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 {
21 virtual bool runOnBasicBlock(BasicBlock *BB);
22
23 private:
24 static void decomposeArrayRef(BasicBlock::iterator &BBI);
25 };
26}
27
28Pass *createDecomposeMultiDimRefsPass() {
29 return new DecomposePass();
30}
31
32
33// runOnBasicBlock - Entry point for array or structure references with multiple
34// indices.
35//
36bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
37 bool Changed = false;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000038 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ) {
39 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) {
40 if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
41 decomposeArrayRef(II);
42 Changed = true;
43 } else {
44 ++II;
45 }
46 } else {
47 ++II;
48 }
49 }
50
51 return Changed;
52}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000053
54//
Vikram S. Adve98d64f82002-03-24 03:21:18 +000055// For any combination of 2 or more array and structure indices,
56// this function repeats the foll. until we have a one-dim. reference: {
57// ptr1 = getElementPtr [CompositeType-N] * lastPtr, uint firstIndex
58// ptr2 = cast [CompositeType-N] * ptr1 to [CompositeType-N] *
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000059// }
60// Then it replaces the original instruction with an equivalent one that
Vikram S. Adve98d64f82002-03-24 03:21:18 +000061// uses the last ptr2 generated in the loop and a single index.
62// If any index is (uint) 0, we omit the getElementPtr instruction.
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000063//
Chris Lattner097632e2002-04-29 01:58:47 +000064void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
65 MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
66 BasicBlock *BB = MAI->getParent();
67 Value *LastPtr = MAI->getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000068
69 // Remove the instruction from the stream
70 BB->getInstList().remove(BBI);
71
Chris Lattner097632e2002-04-29 01:58:47 +000072 vector<Instruction*> NewInsts;
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000073
Vikram S. Adve98d64f82002-03-24 03:21:18 +000074 // Process each index except the last one.
75 //
Chris Lattner097632e2002-04-29 01:58:47 +000076 User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end();
77 for (; OI+1 != OE; ++OI) {
78 assert(isa<PointerType>(LastPtr->getType()));
Vikram S. Adve98d64f82002-03-24 03:21:18 +000079
Chris Lattner2f6f03b2002-04-29 01:22:55 +000080 // Check for a zero index. This will need a cast instead of
81 // a getElementPtr, or it may need neither.
82 bool indexIsZero = isa<ConstantUInt>(*OI) &&
83 cast<Constant>(*OI)->isNullValue();
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000084
Chris Lattner2f6f03b2002-04-29 01:22:55 +000085 // Extract the first index. If the ptr is a pointer to a structure
86 // and the next index is a structure offset (i.e., not an array offset),
87 // we need to include an initial [0] to index into the pointer.
Chris Lattner097632e2002-04-29 01:58:47 +000088 //
89 vector<Value*> Indices;
90 PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
91 if (isa<StructType>(PtrTy->getElementType())
92 && !PtrTy->indexValid(*OI))
93 Indices.push_back(Constant::getNullValue(Type::UIntTy));
94 Indices.push_back(*OI);
95
Chris Lattner2f6f03b2002-04-29 01:22:55 +000096 // Get the type obtained by applying the first index.
97 // It must be a structure or array.
Chris Lattner097632e2002-04-29 01:58:47 +000098 const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
99 Indices, true);
100 assert(isa<CompositeType>(NextTy));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000101
102 // Get a pointer to the structure or to the elements of the array.
Chris Lattner097632e2002-04-29 01:58:47 +0000103 const Type *NextPtrTy =
104 PointerType::get(isa<StructType>(NextTy) ? NextTy
105 : cast<ArrayType>(NextTy)->getElementType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000106
Chris Lattner097632e2002-04-29 01:58:47 +0000107 // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000108 // This is not needed if the index is zero.
Chris Lattner097632e2002-04-29 01:58:47 +0000109 if (!indexIsZero) {
110 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
111 NewInsts.push_back(cast<Instruction>(LastPtr));
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000112 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000113
Chris Lattner097632e2002-04-29 01:58:47 +0000114 // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000115 // This is not needed if the two types are identical.
Chris Lattner097632e2002-04-29 01:58:47 +0000116 //
117 if (LastPtr->getType() != NextPtrTy) {
118 LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
119 NewInsts.push_back(cast<Instruction>(LastPtr));
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000120 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000121 }
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000122
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000123 //
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000124 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000125 //
Chris Lattner097632e2002-04-29 01:58:47 +0000126 PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000127
128 // First, get the final index vector. As above, we may need an initial [0].
Chris Lattner097632e2002-04-29 01:58:47 +0000129 vector<Value*> Indices;
130 if (isa<StructType>(PtrTy->getElementType())
131 && !PtrTy->indexValid(*OI))
132 Indices.push_back(Constant::getNullValue(Type::UIntTy));
133
134 Indices.push_back(*OI);
135
136 Instruction *NewI = 0;
137 switch(MAI->getOpcode()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000138 case Instruction::Load:
Chris Lattner097632e2002-04-29 01:58:47 +0000139 NewI = new LoadInst(LastPtr, Indices, MAI->getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000140 break;
141 case Instruction::Store:
Chris Lattner097632e2002-04-29 01:58:47 +0000142 NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000143 break;
144 case Instruction::GetElementPtr:
Chris Lattner097632e2002-04-29 01:58:47 +0000145 NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000146 break;
147 default:
148 assert(0 && "Unrecognized memory access instruction");
149 }
Chris Lattner097632e2002-04-29 01:58:47 +0000150 NewInsts.push_back(NewI);
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000151
152 // Replace all uses of the old instruction with the new
Chris Lattner097632e2002-04-29 01:58:47 +0000153 MAI->replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000154
155 // Now delete the old instruction...
Chris Lattner097632e2002-04-29 01:58:47 +0000156 delete MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000157
158 // Convert our iterator into an index... that cannot get invalidated
159 unsigned ItOffs = BBI-BB->begin();
160
161 // Insert all of the new instructions...
Chris Lattner097632e2002-04-29 01:58:47 +0000162 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 Lattner097632e2002-04-29 01:58:47 +0000165 BBI = BB->begin() + ItOffs + NewInsts.size();
Chris Lattnerf57b8452002-04-27 06:56:12 +0000166}