blob: 7bfa329b57ca8637caa4314610c079511d2d3b5c [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
Chris Lattner022103b2002-05-07 20:03:00 +000011#include "llvm/Transforms/Scalar.h"
Chris Lattner72a1d4e2002-04-29 18:48:30 +000012#include "llvm/DerivedTypes.h"
Vikram S. Adve900fd632002-08-03 13:21:15 +000013#include "llvm/Constants.h"
Chris Lattner96c466b2002-04-29 14:57:45 +000014#include "llvm/Constant.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000015#include "llvm/iMemory.h"
16#include "llvm/iOther.h"
17#include "llvm/BasicBlock.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000018#include "llvm/Pass.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000019#include "Support/StatisticReporter.h"
20
21static Statistic<> NumAdded("lowerrefs\t\t- New instructions added");
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000022
Chris Lattner2f6f03b2002-04-29 01:22:55 +000023namespace {
24 struct DecomposePass : public BasicBlockPass {
Chris Lattner7e708292002-06-25 16:13:24 +000025 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000026
27 private:
Vikram S. Adve900fd632002-08-03 13:21:15 +000028 static bool decomposeArrayRef(BasicBlock::iterator &BBI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000029 };
Chris Lattnerf6293092002-07-23 18:06:35 +000030
Chris Lattnera6275cc2002-07-26 21:12:46 +000031 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
32 "structure/array references");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000033}
34
Vikram S. Adve900fd632002-08-03 13:21:15 +000035Pass
36*createDecomposeMultiDimRefsPass()
37{
Chris Lattner2f6f03b2002-04-29 01:22:55 +000038 return new DecomposePass();
39}
40
41
42// runOnBasicBlock - Entry point for array or structure references with multiple
43// indices.
44//
Vikram S. Adve900fd632002-08-03 13:21:15 +000045bool
46DecomposePass::runOnBasicBlock(BasicBlock &BB)
47{
Chris Lattner2f6f03b2002-04-29 01:22:55 +000048 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000049 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
Vikram S. Adve900fd632002-08-03 13:21:15 +000050 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(&*II))
51 if (MAI->getNumIndices() >= 2) {
52 Changed = decomposeArrayRef(II) || Changed; // always modifies II
53 continue;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000054 }
Vikram S. Adve900fd632002-08-03 13:21:15 +000055 ++II;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000056 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +000057 return Changed;
58}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000059
Vikram S. Adve900fd632002-08-03 13:21:15 +000060// Check for a constant (uint) 0.
61inline bool
62IsZero(Value* idx)
63{
64 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
65}
Anand Shukla5ba99bd2002-06-25 21:07:58 +000066
Vikram S. Adve900fd632002-08-03 13:21:15 +000067// For any MemAccessInst with 2 or more array and structure indices:
68//
69// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
70//
71// this function generates the foll sequence:
72//
73// ptr1 = getElementPtr P, idx1
74// ptr2 = getElementPtr ptr1, 0, idx2
75// ...
76// ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
77// opCode ptrN-1, 0, idxN // New-MAI
78//
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.
82//
83// On return: BBI points to the instruction after the current one
84// (whether or not *BBI was replaced).
85//
86// Return value: true if the instruction was replaced; false otherwise.
87//
88bool
89DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
90{
Chris Lattner905641b2002-08-21 22:10:06 +000091 // FIXME: If condition below
Chris Lattner7e708292002-06-25 16:13:24 +000092 MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
Chris Lattner905641b2002-08-21 22:10:06 +000093 // FIXME: If condition below
Vikram S. Adve900fd632002-08-03 13:21:15 +000094
Chris Lattner905641b2002-08-21 22:10:06 +000095 // If this instr has no indexes, then the decomposed version is identical to
96 // the instruction itself. FIXME: this should go away once GEP is the only
97 // MAI
98 //
99 if (MAI.getNumIndices() == 0) {
Vikram S. Adve900fd632002-08-03 13:21:15 +0000100 ++BBI;
101 return false;
102 }
103
Chris Lattner7e708292002-06-25 16:13:24 +0000104 BasicBlock *BB = MAI.getParent();
105 Value *LastPtr = MAI.getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000106
107 // Remove the instruction from the stream
108 BB->getInstList().remove(BBI);
109
Vikram S. Adve900fd632002-08-03 13:21:15 +0000110 // The vector of new instructions to be created
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000111 std::vector<Instruction*> NewInsts;
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000112
Vikram S. Adve900fd632002-08-03 13:21:15 +0000113 // Process each index except the last one.
Chris Lattner7e708292002-06-25 16:13:24 +0000114 User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +0000115 for (; OI+1 != OE; ++OI) {
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000116 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000117
118 // If this is the first index and is 0, skip it and move on!
119 if (OI == MAI.idx_begin()) {
120 if (IsZero(*OI)) continue;
121 } else
122 // Not the first index: include initial [0] to deref the last ptr
Chris Lattner097632e2002-04-29 01:58:47 +0000123 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Vikram S. Adve900fd632002-08-03 13:21:15 +0000124
Chris Lattner097632e2002-04-29 01:58:47 +0000125 Indices.push_back(*OI);
126
Vikram S. Adve900fd632002-08-03 13:21:15 +0000127 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
128 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
129 NewInsts.push_back(cast<Instruction>(LastPtr));
130 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000131 }
Vikram S. Adve900fd632002-08-03 13:21:15 +0000132
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000133 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000134 //
Chris Lattner7e708292002-06-25 16:13:24 +0000135 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000136
Vikram S. Adve900fd632002-08-03 13:21:15 +0000137 // Get the final index vector, including an initial [0] as before.
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000138 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000139 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattner097632e2002-04-29 01:58:47 +0000140 Indices.push_back(*OI);
141
142 Instruction *NewI = 0;
Chris Lattner7e708292002-06-25 16:13:24 +0000143 switch(MAI.getOpcode()) {
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000144 case Instruction::Load:
Chris Lattner7e708292002-06-25 16:13:24 +0000145 NewI = new LoadInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000146 break;
147 case Instruction::Store:
Chris Lattner7e708292002-06-25 16:13:24 +0000148 NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000149 break;
150 case Instruction::GetElementPtr:
Chris Lattner7e708292002-06-25 16:13:24 +0000151 NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000152 break;
153 default:
154 assert(0 && "Unrecognized memory access instruction");
155 }
Chris Lattner097632e2002-04-29 01:58:47 +0000156 NewInsts.push_back(NewI);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000157
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000158 // Replace all uses of the old instruction with the new
Chris Lattner7e708292002-06-25 16:13:24 +0000159 MAI.replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000160
161 // Now delete the old instruction...
Chris Lattner7e708292002-06-25 16:13:24 +0000162 delete &MAI;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000163
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000164 // Insert all of the new instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000165 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
Vikram S. Adve900fd632002-08-03 13:21:15 +0000166
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000167 // Advance the iterator to the instruction following the one just inserted...
Chris Lattner7e708292002-06-25 16:13:24 +0000168 BBI = NewInsts.back();
169 ++BBI;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000170 return true;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000171}