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/Statistic.h"
22 Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
24 struct DecomposePass : public BasicBlockPass {
25 virtual bool runOnBasicBlock(BasicBlock &BB);
29 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
30 "structure/array references");
33 *createDecomposeMultiDimRefsPass()
35 return new DecomposePass();
39 // runOnBasicBlock - Entry point for array or structure references with multiple
43 DecomposePass::runOnBasicBlock(BasicBlock &BB)
46 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
47 if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
48 if (gep->getNumIndices() >= 2)
49 changed |= DecomposeArrayRef(gep); // always modifies II
54 // Function: DecomposeArrayRef()
56 // For any GetElementPtrInst with 2 or more array and structure indices:
58 // opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
60 // this function generates the foll sequence:
62 // ptr1 = getElementPtr P, idx1
63 // ptr2 = getElementPtr ptr1, 0, idx2
65 // ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
66 // opCode ptrN-1, 0, idxN // New-MAI
68 // Then it replaces the original instruction with this sequence,
69 // and replaces all uses of the original instruction with New-MAI.
70 // If idx1 is 0, we simply omit the first getElementPtr instruction.
72 // On return: BBI points to the instruction after the current one
73 // (whether or not *BBI was replaced).
75 // Return value: true if the instruction was replaced; false otherwise.
78 DecomposeArrayRef(GetElementPtrInst* GEP)
80 if (GEP->getNumIndices() < 2)
83 BasicBlock *BB = GEP->getParent();
84 Value *LastPtr = GEP->getPointerOperand();
85 Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
87 // The vector of new instructions to be created
88 std::vector<Instruction*> NewInsts;
90 // Process each index except the last one.
91 User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
92 for (; OI+1 != OE; ++OI) {
93 std::vector<Value*> Indices;
95 // If this is the first index and is 0, skip it and move on!
96 if (OI == GEP->idx_begin()) {
97 if (*OI == ConstantInt::getNullValue((*OI)->getType()))
100 else // Not the first index: include initial [0] to deref the last ptr
101 Indices.push_back(Constant::getNullValue(Type::LongTy));
103 Indices.push_back(*OI);
105 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
106 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
110 // Now create a new instruction to replace the original one
112 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
114 // Get the final index vector, including an initial [0] as before.
115 std::vector<Value*> Indices;
116 Indices.push_back(Constant::getNullValue(Type::LongTy));
117 Indices.push_back(*OI);
119 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
122 // Replace all uses of the old instruction with the new
123 GEP->replaceAllUsesWith(NewVal);
125 // Now remove and delete the old instruction...
126 BB->getInstList().erase(GEP);