blob: b50c4fb5bf8c97efcea32f2992479a9447685dba [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(); ) {
Chris Lattnercc63f1c2002-08-22 23:37:20 +000050 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&*II))
51 if (GEP->getNumIndices() >= 2) {
Chris Lattner24ea74e2002-08-22 22:49:05 +000052 Changed |= decomposeArrayRef(II); // always modifies II
Vikram S. Adve900fd632002-08-03 13:21:15 +000053 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
Chris Lattnercc63f1c2002-08-22 23:37:20 +000067// For any GetElementPtrInst with 2 or more array and structure indices:
Vikram S. Adve900fd632002-08-03 13:21:15 +000068//
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 Lattnercc63f1c2002-08-22 23:37:20 +000091 GetElementPtrInst &GEP = cast<GetElementPtrInst>(*BBI);
92 BasicBlock *BB = GEP.getParent();
93 Value *LastPtr = GEP.getPointerOperand();
Chris Lattner2f6f03b2002-04-29 01:22:55 +000094
95 // Remove the instruction from the stream
96 BB->getInstList().remove(BBI);
97
Vikram S. Adve900fd632002-08-03 13:21:15 +000098 // The vector of new instructions to be created
Anand Shukla5ba99bd2002-06-25 21:07:58 +000099 std::vector<Instruction*> NewInsts;
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000100
Vikram S. Adve900fd632002-08-03 13:21:15 +0000101 // Process each index except the last one.
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000102 User::const_op_iterator OI = GEP.idx_begin(), OE = GEP.idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +0000103 for (; OI+1 != OE; ++OI) {
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000104 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000105
106 // If this is the first index and is 0, skip it and move on!
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000107 if (OI == GEP.idx_begin()) {
Vikram S. Adve900fd632002-08-03 13:21:15 +0000108 if (IsZero(*OI)) continue;
109 } else
110 // Not the first index: include initial [0] to deref the last ptr
Chris Lattner097632e2002-04-29 01:58:47 +0000111 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Vikram S. Adve900fd632002-08-03 13:21:15 +0000112
Chris Lattner097632e2002-04-29 01:58:47 +0000113 Indices.push_back(*OI);
114
Vikram S. Adve900fd632002-08-03 13:21:15 +0000115 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
116 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
117 NewInsts.push_back(cast<Instruction>(LastPtr));
118 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000119 }
Vikram S. Adve900fd632002-08-03 13:21:15 +0000120
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000121 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000122 //
Chris Lattner7e708292002-06-25 16:13:24 +0000123 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000124
Vikram S. Adve900fd632002-08-03 13:21:15 +0000125 // Get the final index vector, including an initial [0] as before.
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000126 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000127 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattner097632e2002-04-29 01:58:47 +0000128 Indices.push_back(*OI);
129
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000130 Instruction *NewI = new GetElementPtrInst(LastPtr, Indices, GEP.getName());
Chris Lattner097632e2002-04-29 01:58:47 +0000131 NewInsts.push_back(NewI);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000132
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000133 // Replace all uses of the old instruction with the new
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000134 GEP.replaceAllUsesWith(NewI);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000135
136 // Now delete the old instruction...
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000137 delete &GEP;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000138
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000139 // Insert all of the new instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000140 BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
Vikram S. Adve900fd632002-08-03 13:21:15 +0000141
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000142 // Advance the iterator to the instruction following the one just inserted...
Chris Lattner7e708292002-06-25 16:13:24 +0000143 BBI = NewInsts.back();
144 ++BBI;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000145 return true;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000146}