blob: a6d7e37cdb6f34235480d70ee9aab35059cbe797 [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
Chris Lattner2f6f03b2002-04-29 01:22:55 +000021namespace {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000022 Statistic<> NumAdded("lowerrefs\t\t- # of getelementptr instructions added");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000023
Chris Lattner2a7c23e2002-09-10 17:04:02 +000024 class DecomposePass : public BasicBlockPass {
25 static bool decomposeArrayRef(GetElementPtrInst &GEP);
26 public:
27 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000028 };
Chris Lattnerf6293092002-07-23 18:06:35 +000029
Chris Lattnera6275cc2002-07-26 21:12:46 +000030 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
31 "structure/array references");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000032}
33
Vikram S. Adve900fd632002-08-03 13:21:15 +000034Pass
35*createDecomposeMultiDimRefsPass()
36{
Chris Lattner2f6f03b2002-04-29 01:22:55 +000037 return new DecomposePass();
38}
39
40
41// runOnBasicBlock - Entry point for array or structure references with multiple
42// indices.
43//
Vikram S. Adve900fd632002-08-03 13:21:15 +000044bool
45DecomposePass::runOnBasicBlock(BasicBlock &BB)
46{
Chris Lattner2f6f03b2002-04-29 01:22:55 +000047 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000048 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000049 Instruction *I = II;
Vikram S. Adve900fd632002-08-03 13:21:15 +000050 ++II;
Chris Lattner2a7c23e2002-09-10 17:04:02 +000051 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
52 if (GEP->getNumIndices() >= 2)
53 Changed |= decomposeArrayRef(*GEP); // always modifies II
Chris Lattner2f6f03b2002-04-29 01:22:55 +000054 }
Chris Lattner2f6f03b2002-04-29 01:22:55 +000055 return Changed;
56}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000057
Chris Lattnercc63f1c2002-08-22 23:37:20 +000058// For any GetElementPtrInst with 2 or more array and structure indices:
Vikram S. Adve900fd632002-08-03 13:21:15 +000059//
60// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
61//
62// this function generates the foll sequence:
63//
64// ptr1 = getElementPtr P, idx1
65// ptr2 = getElementPtr ptr1, 0, idx2
66// ...
67// ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
68// opCode ptrN-1, 0, idxN // New-MAI
69//
70// Then it replaces the original instruction with this sequence,
71// and replaces all uses of the original instruction with New-MAI.
72// If idx1 is 0, we simply omit the first getElementPtr instruction.
73//
74// On return: BBI points to the instruction after the current one
75// (whether or not *BBI was replaced).
76//
77// Return value: true if the instruction was replaced; false otherwise.
78//
79bool
Chris Lattner2a7c23e2002-09-10 17:04:02 +000080DecomposePass::decomposeArrayRef(GetElementPtrInst &GEP)
Vikram S. Adve900fd632002-08-03 13:21:15 +000081{
Chris Lattnercc63f1c2002-08-22 23:37:20 +000082 BasicBlock *BB = GEP.getParent();
83 Value *LastPtr = GEP.getPointerOperand();
Chris Lattner2a7c23e2002-09-10 17:04:02 +000084 Instruction *InsertPoint = GEP.getNext(); // Insert before the next insn
Anand Shukla5ba99bd2002-06-25 21:07:58 +000085
Vikram S. Adve900fd632002-08-03 13:21:15 +000086 // Process each index except the last one.
Chris Lattnercc63f1c2002-08-22 23:37:20 +000087 User::const_op_iterator OI = GEP.idx_begin(), OE = GEP.idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000088 for (; OI+1 != OE; ++OI) {
Anand Shukla5ba99bd2002-06-25 21:07:58 +000089 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +000090
91 // If this is the first index and is 0, skip it and move on!
Chris Lattnercc63f1c2002-08-22 23:37:20 +000092 if (OI == GEP.idx_begin()) {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000093 if (*OI == ConstantInt::getNullValue((*OI)->getType()))
94 continue;
95 } else {
Vikram S. Adve900fd632002-08-03 13:21:15 +000096 // Not the first index: include initial [0] to deref the last ptr
Chris Lattner097632e2002-04-29 01:58:47 +000097 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattner2a7c23e2002-09-10 17:04:02 +000098 }
Vikram S. Adve900fd632002-08-03 13:21:15 +000099
Chris Lattner097632e2002-04-29 01:58:47 +0000100 Indices.push_back(*OI);
101
Vikram S. Adve900fd632002-08-03 13:21:15 +0000102 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000103 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
Vikram S. Adve900fd632002-08-03 13:21:15 +0000104 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000105 }
Vikram S. Adve900fd632002-08-03 13:21:15 +0000106
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000107 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000108 //
Chris Lattner7e708292002-06-25 16:13:24 +0000109 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000110
Vikram S. Adve900fd632002-08-03 13:21:15 +0000111 // Get the final index vector, including an initial [0] as before.
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000112 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000113 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattner097632e2002-04-29 01:58:47 +0000114 Indices.push_back(*OI);
115
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000116 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP.getName(),
117 InsertPoint);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000118
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000119 // Replace all uses of the old instruction with the new
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000120 GEP.replaceAllUsesWith(NewVal);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000121
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000122 // Now remove and delete the old instruction...
123 BB->getInstList().erase(&GEP);
Vikram S. Adve900fd632002-08-03 13:21:15 +0000124 return true;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000125}