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 (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&*II))
51 if (GEP->getNumIndices() >= 2) {
52 Changed |= decomposeArrayRef(II); // always modifies II
60 // Check for a constant (uint) 0.
64 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
67 // For any GetElementPtrInst 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 GetElementPtrInst &GEP = cast<GetElementPtrInst>(*BBI);
92 BasicBlock *BB = GEP.getParent();
93 Value *LastPtr = GEP.getPointerOperand();
95 // Remove the instruction from the stream
96 BB->getInstList().remove(BBI);
98 // The vector of new instructions to be created
99 std::vector<Instruction*> NewInsts;
101 // Process each index except the last one.
102 User::const_op_iterator OI = GEP.idx_begin(), OE = GEP.idx_end();
103 for (; OI+1 != OE; ++OI) {
104 std::vector<Value*> Indices;
106 // If this is the first index and is 0, skip it and move on!
107 if (OI == GEP.idx_begin()) {
108 if (IsZero(*OI)) continue;
110 // Not the first index: include initial [0] to deref the last ptr
111 Indices.push_back(Constant::getNullValue(Type::UIntTy));
113 Indices.push_back(*OI);
115 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
116 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
117 NewInsts.push_back(cast<Instruction>(LastPtr));
121 // Now create a new instruction to replace the original one
123 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
125 // Get the final index vector, including an initial [0] as before.
126 std::vector<Value*> Indices;
127 Indices.push_back(Constant::getNullValue(Type::UIntTy));
128 Indices.push_back(*OI);
130 Instruction *NewI = new GetElementPtrInst(LastPtr, Indices, GEP.getName());
131 NewInsts.push_back(NewI);
133 // Replace all uses of the old instruction with the new
134 GEP.replaceAllUsesWith(NewI);
136 // Now delete the old instruction...
139 // Insert all of the new instructions...
140 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
142 // Advance the iterator to the instruction following the one just inserted...
143 BBI = NewInsts.back();