1 //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
3 // 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
9 //===----------------------------------------------------------------------===//
11 #include "llvm/Transforms/Scalar.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/Constants.h"
14 #include "llvm/Constant.h"
15 #include "llvm/iMemory.h"
16 #include "llvm/iOther.h"
17 #include "llvm/BasicBlock.h"
18 #include "llvm/Pass.h"
19 #include "Support/StatisticReporter.h"
21 static Statistic<> NumAdded("lowerrefs\t\t- New instructions added");
24 struct DecomposePass : public BasicBlockPass {
25 virtual bool runOnBasicBlock(BasicBlock &BB);
28 static bool decomposeArrayRef(BasicBlock::iterator &BBI);
31 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
32 "structure/array references");
36 *createDecomposeMultiDimRefsPass()
38 return new DecomposePass();
42 // runOnBasicBlock - Entry point for array or structure references with multiple
46 DecomposePass::runOnBasicBlock(BasicBlock &BB)
49 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
50 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(&*II))
51 if (MAI->getNumIndices() >= 2) {
52 Changed = decomposeArrayRef(II) || Changed; // always modifies II
60 // Check for a constant (uint) 0.
64 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
67 // For any MemAccessInst with 2 or more array and structure indices:
69 // opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
71 // this function generates the foll sequence:
73 // ptr1 = getElementPtr P, idx1
74 // ptr2 = getElementPtr ptr1, 0, idx2
76 // ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
77 // opCode ptrN-1, 0, idxN // New-MAI
79 // Then it replaces the original instruction with this sequence,
80 // and replaces all uses of the original instruction with New-MAI.
81 // If idx1 is 0, we simply omit the first getElementPtr instruction.
83 // On return: BBI points to the instruction after the current one
84 // (whether or not *BBI was replaced).
86 // Return value: true if the instruction was replaced; false otherwise.
89 DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
91 MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
93 // If this instr two or fewer arguments and the first argument is 0,
94 // the decomposed version is identical to the instruction itself.
95 // This is common enough that it is worth checking for explicitly...
96 if (MAI.getNumIndices() == 0 ||
97 (MAI.getNumIndices() <= 2 && IsZero(*MAI.idx_begin()))) {
102 BasicBlock *BB = MAI.getParent();
103 Value *LastPtr = MAI.getPointerOperand();
105 // Remove the instruction from the stream
106 BB->getInstList().remove(BBI);
108 // The vector of new instructions to be created
109 std::vector<Instruction*> NewInsts;
111 // Process each index except the last one.
112 User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
113 for (; OI+1 != OE; ++OI) {
114 std::vector<Value*> Indices;
116 // If this is the first index and is 0, skip it and move on!
117 if (OI == MAI.idx_begin()) {
118 if (IsZero(*OI)) continue;
120 // Not the first index: include initial [0] to deref the last ptr
121 Indices.push_back(Constant::getNullValue(Type::UIntTy));
123 Indices.push_back(*OI);
125 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
126 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
127 NewInsts.push_back(cast<Instruction>(LastPtr));
131 // Now create a new instruction to replace the original one
133 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
135 // Get the final index vector, including an initial [0] as before.
136 std::vector<Value*> Indices;
137 Indices.push_back(Constant::getNullValue(Type::UIntTy));
138 Indices.push_back(*OI);
140 Instruction *NewI = 0;
141 switch(MAI.getOpcode()) {
142 case Instruction::Load:
143 NewI = new LoadInst(LastPtr, Indices, MAI.getName());
145 case Instruction::Store:
146 NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
148 case Instruction::GetElementPtr:
149 NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
152 assert(0 && "Unrecognized memory access instruction");
154 NewInsts.push_back(NewI);
156 // Replace all uses of the old instruction with the new
157 MAI.replaceAllUsesWith(NewI);
159 // Now delete the old instruction...
162 // Insert all of the new instructions...
163 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
165 // Advance the iterator to the instruction following the one just inserted...
166 BBI = NewInsts.back();