blob: 1f6dbda199ddb23abe25000a1207bc05f224e61e [file] [log] [blame]
Chris Lattner64a26c72004-11-07 00:39:09 +00001//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Chris Lattner64a26c72004-11-07 00:39:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Chris Lattner64a26c72004-11-07 00:39:09 +00008//===----------------------------------------------------------------------===//
9//
10// DecomposeMultiDimRefs - Convert multi-dimensional references consisting of
11// any combination of 2 or more array and structure indices into a sequence of
12// instructions (using getelementpr and cast) so that each instruction has at
13// most one index (except structure references, which need an extra leading
14// index of [0]).
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner26818012004-11-07 00:43:24 +000018#include "SparcV9Internals.h"
Chris Lattner64a26c72004-11-07 00:39:09 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Constants.h"
21#include "llvm/Constant.h"
22#include "llvm/Instructions.h"
23#include "llvm/BasicBlock.h"
24#include "llvm/Pass.h"
25#include "llvm/ADT/Statistic.h"
26#include "llvm/Support/Debug.h"
27using namespace llvm;
28
29namespace {
30 Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
31
32 struct DecomposePass : public BasicBlockPass {
33 virtual bool runOnBasicBlock(BasicBlock &BB);
34 };
35 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
36 "structure/array references");
37}
38
39// runOnBasicBlock - Entry point for array or structure references with multiple
40// indices.
41//
42bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
43 bool changed = false;
44 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
45 if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
46 if (gep->getNumIndices() >= 2)
47 changed |= DecomposeArrayRef(gep); // always modifies II
48 return changed;
49}
50
51FunctionPass *llvm::createDecomposeMultiDimRefsPass() {
52 return new DecomposePass();
53}
54
55static inline bool isZeroConst (Value *V) {
56 return isa<Constant> (V) && (cast<Constant>(V)->isNullValue());
57}
58
59// Function: DecomposeArrayRef()
Misha Brukmanb5f662f2005-04-21 23:30:14 +000060//
Chris Lattner64a26c72004-11-07 00:39:09 +000061// For any GetElementPtrInst with 2 or more array and structure indices:
Misha Brukmanb5f662f2005-04-21 23:30:14 +000062//
Chris Lattner64a26c72004-11-07 00:39:09 +000063// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
Misha Brukmanb5f662f2005-04-21 23:30:14 +000064//
Chris Lattner64a26c72004-11-07 00:39:09 +000065// this function generates the following sequence:
Misha Brukmanb5f662f2005-04-21 23:30:14 +000066//
Chris Lattner64a26c72004-11-07 00:39:09 +000067// ptr1 = getElementPtr P, idx1
68// ptr2 = getElementPtr ptr1, 0, idx2
69// ...
70// ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
71// opCode ptrN-1, 0, idxN // New-MAI
Misha Brukmanb5f662f2005-04-21 23:30:14 +000072//
Chris Lattner64a26c72004-11-07 00:39:09 +000073// Then it replaces the original instruction with this sequence,
74// and replaces all uses of the original instruction with New-MAI.
75// If idx1 is 0, we simply omit the first getElementPtr instruction.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000076//
Chris Lattner64a26c72004-11-07 00:39:09 +000077// On return: BBI points to the instruction after the current one
78// (whether or not *BBI was replaced).
Misha Brukmanb5f662f2005-04-21 23:30:14 +000079//
Chris Lattner64a26c72004-11-07 00:39:09 +000080// Return value: true if the instruction was replaced; false otherwise.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000081//
Chris Lattner64a26c72004-11-07 00:39:09 +000082bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) {
83 if (GEP->getNumIndices() < 2
84 || (GEP->getNumIndices() == 2
85 && isZeroConst(GEP->getOperand(1)))) {
86 DEBUG (std::cerr << "DecomposeArrayRef: Skipping " << *GEP);
87 return false;
88 } else {
89 DEBUG (std::cerr << "DecomposeArrayRef: Decomposing " << *GEP);
90 }
91
92 BasicBlock *BB = GEP->getParent();
93 Value *LastPtr = GEP->getPointerOperand();
94 Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
95
96 // Process each index except the last one.
97 User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
98 for (; OI+1 != OE; ++OI) {
99 std::vector<Value*> Indices;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000100
Chris Lattner64a26c72004-11-07 00:39:09 +0000101 // If this is the first index and is 0, skip it and move on!
102 if (OI == GEP->idx_begin()) {
103 if (isZeroConst (*OI))
104 continue;
105 }
106 else // Not the first index: include initial [0] to deref the last ptr
107 Indices.push_back(Constant::getNullValue(Type::LongTy));
108
109 Indices.push_back(*OI);
110
111 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
112 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
113 ++NumAdded;
114 }
115
116 // Now create a new instruction to replace the original one
117 //
118 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
119
120 // Get the final index vector, including an initial [0] as before.
121 std::vector<Value*> Indices;
122 Indices.push_back(Constant::getNullValue(Type::LongTy));
123 Indices.push_back(*OI);
124
125 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
126 InsertPoint);
127
128 // Replace all uses of the old instruction with the new
129 GEP->replaceAllUsesWith(NewVal);
130
131 // Now remove and delete the old instruction...
132 BB->getInstList().erase(GEP);
133
134 return true;
135}
136