blob: d0f47d8b8069a1f580a1b00438c495ea2ce32356 [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 Lattnera92f6962002-10-01 22:38:41 +000019#include "Support/Statistic.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000020
Chris Lattner2f6f03b2002-04-29 01:22:55 +000021namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000022 Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000023
Vikram S. Advec7a6d242002-09-16 16:40:07 +000024 struct DecomposePass : public BasicBlockPass {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000025 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000026 };
27}
28
Vikram S. Advec7a6d242002-09-16 16:40:07 +000029RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
30 "structure/array references");
31
Vikram S. Adve900fd632002-08-03 13:21:15 +000032Pass
33*createDecomposeMultiDimRefsPass()
34{
Chris Lattner2f6f03b2002-04-29 01:22:55 +000035 return new DecomposePass();
36}
37
38
39// runOnBasicBlock - Entry point for array or structure references with multiple
40// indices.
41//
Vikram S. Adve900fd632002-08-03 13:21:15 +000042bool
43DecomposePass::runOnBasicBlock(BasicBlock &BB)
44{
Vikram S. Advec7a6d242002-09-16 16:40:07 +000045 bool changed = false;
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
50 return changed;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000051}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000052
Vikram S. Advec7a6d242002-09-16 16:40:07 +000053
54// Function: DecomposeArrayRef()
55//
Chris Lattnercc63f1c2002-08-22 23:37:20 +000056// For any GetElementPtrInst with 2 or more array and structure indices:
Vikram S. Adve900fd632002-08-03 13:21:15 +000057//
58// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
59//
60// this function generates the foll sequence:
61//
62// ptr1 = getElementPtr P, idx1
63// ptr2 = getElementPtr ptr1, 0, idx2
64// ...
65// ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
66// opCode ptrN-1, 0, idxN // New-MAI
67//
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.
71//
72// On return: BBI points to the instruction after the current one
73// (whether or not *BBI was replaced).
74//
75// Return value: true if the instruction was replaced; false otherwise.
76//
77bool
Vikram S. Advec7a6d242002-09-16 16:40:07 +000078DecomposeArrayRef(GetElementPtrInst* GEP)
Vikram S. Adve900fd632002-08-03 13:21:15 +000079{
Vikram S. Advec7a6d242002-09-16 16:40:07 +000080 if (GEP->getNumIndices() < 2)
81 return false;
82
83 BasicBlock *BB = GEP->getParent();
84 Value *LastPtr = GEP->getPointerOperand();
85 Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
86
87 // The vector of new instructions to be created
88 std::vector<Instruction*> NewInsts;
Anand Shukla5ba99bd2002-06-25 21:07:58 +000089
Vikram S. Adve900fd632002-08-03 13:21:15 +000090 // Process each index except the last one.
Vikram S. Advec7a6d242002-09-16 16:40:07 +000091 User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000092 for (; OI+1 != OE; ++OI) {
Anand Shukla5ba99bd2002-06-25 21:07:58 +000093 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +000094
95 // If this is the first index and is 0, skip it and move on!
Vikram S. Advec7a6d242002-09-16 16:40:07 +000096 if (OI == GEP->idx_begin()) {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000097 if (*OI == ConstantInt::getNullValue((*OI)->getType()))
98 continue;
Chris Lattner2a7c23e2002-09-10 17:04:02 +000099 }
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000100 else // Not the first index: include initial [0] to deref the last ptr
101 Indices.push_back(Constant::getNullValue(Type::LongTy));
Vikram S. Adve900fd632002-08-03 13:21:15 +0000102
Chris Lattner097632e2002-04-29 01:58:47 +0000103 Indices.push_back(*OI);
104
Vikram S. Adve900fd632002-08-03 13:21:15 +0000105 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000106 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
Vikram S. Adve900fd632002-08-03 13:21:15 +0000107 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000108 }
Vikram S. Adve900fd632002-08-03 13:21:15 +0000109
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000110 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000111 //
Chris Lattner7e708292002-06-25 16:13:24 +0000112 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000113
Vikram S. Adve900fd632002-08-03 13:21:15 +0000114 // Get the final index vector, including an initial [0] as before.
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000115 std::vector<Value*> Indices;
Chris Lattner3cac88a2002-09-11 01:21:33 +0000116 Indices.push_back(Constant::getNullValue(Type::LongTy));
Chris Lattner097632e2002-04-29 01:58:47 +0000117 Indices.push_back(*OI);
118
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000119 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000120 InsertPoint);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000121
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000122 // Replace all uses of the old instruction with the new
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000123 GEP->replaceAllUsesWith(NewVal);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000124
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000125 // Now remove and delete the old instruction...
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000126 BB->getInstList().erase(GEP);
127
Vikram S. Adve900fd632002-08-03 13:21:15 +0000128 return true;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000129}