blob: b1f8f2a01ab4f1e426aeacc6871564538d68dec6 [file] [log] [blame]
Chris Lattner2f6f03b2002-04-29 01:22:55 +00001//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +00009//
Chris Lattner097632e2002-04-29 01:58:47 +000010// 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]).
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000015//
Chris Lattnerf57b8452002-04-27 06:56:12 +000016//===----------------------------------------------------------------------===//
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000017
Chris Lattner022103b2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner72a1d4e2002-04-29 18:48:30 +000019#include "llvm/DerivedTypes.h"
Vikram S. Adve900fd632002-08-03 13:21:15 +000020#include "llvm/Constants.h"
Chris Lattner96c466b2002-04-29 14:57:45 +000021#include "llvm/Constant.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000022#include "llvm/Instructions.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000023#include "llvm/BasicBlock.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000024#include "llvm/Pass.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000025#include "Support/Statistic.h"
Brian Gaeke10585d92004-07-06 18:15:39 +000026#include "Support/Debug.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner2f6f03b2002-04-29 01:22:55 +000029namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000030 Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000031
Vikram S. Advec7a6d242002-09-16 16:40:07 +000032 struct DecomposePass : public BasicBlockPass {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000033 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000034 };
Chris Lattnerd7456022004-01-09 06:02:20 +000035 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
36 "structure/array references");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000037}
38
Chris Lattner2f6f03b2002-04-29 01:22:55 +000039// runOnBasicBlock - Entry point for array or structure references with multiple
40// indices.
41//
Chris Lattnerd7456022004-01-09 06:02:20 +000042bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Advec7a6d242002-09-16 16:40:07 +000043 bool changed = false;
44 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
Chris Lattnere408e252003-04-23 16:37:45 +000045 if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
Vikram S. Advec7a6d242002-09-16 16:40:07 +000046 if (gep->getNumIndices() >= 2)
47 changed |= DecomposeArrayRef(gep); // always modifies II
48 return changed;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000049}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000050
Chris Lattnerd7456022004-01-09 06:02:20 +000051FunctionPass *llvm::createDecomposeMultiDimRefsPass() {
Brian Gaeked0fde302003-11-11 22:41:34 +000052 return new DecomposePass();
53}
Vikram S. Advec7a6d242002-09-16 16:40:07 +000054
Brian Gaeke10585d92004-07-06 18:15:39 +000055static inline bool isZeroConst (Value *V) {
56 return isa<Constant> (V) && (cast<Constant>(V)->isNullValue());
57}
58
Vikram S. Advec7a6d242002-09-16 16:40:07 +000059// Function: DecomposeArrayRef()
60//
Chris Lattnercc63f1c2002-08-22 23:37:20 +000061// For any GetElementPtrInst with 2 or more array and structure indices:
Vikram S. Adve900fd632002-08-03 13:21:15 +000062//
63// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
64//
Brian Gaeke1c4b6de42004-07-02 05:30:01 +000065// this function generates the following sequence:
Vikram S. Adve900fd632002-08-03 13:21:15 +000066//
67// 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
72//
73// 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.
76//
77// On return: BBI points to the instruction after the current one
78// (whether or not *BBI was replaced).
79//
80// Return value: true if the instruction was replaced; false otherwise.
81//
Chris Lattnerd7456022004-01-09 06:02:20 +000082bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) {
Brian Gaeke10585d92004-07-06 18:15:39 +000083 if (GEP->getNumIndices() < 2
84 || (GEP->getNumIndices() == 2
Brian Gaeke08cc64e2004-07-06 19:24:47 +000085 && isZeroConst(GEP->getOperand(1)))) {
Brian Gaeke10585d92004-07-06 18:15:39 +000086 DEBUG (std::cerr << "DecomposeArrayRef: Skipping " << *GEP);
Vikram S. Advec7a6d242002-09-16 16:40:07 +000087 return false;
Brian Gaeke10585d92004-07-06 18:15:39 +000088 } else {
89 DEBUG (std::cerr << "DecomposeArrayRef: Decomposing " << *GEP);
90 }
Vikram S. Advec7a6d242002-09-16 16:40:07 +000091
92 BasicBlock *BB = GEP->getParent();
93 Value *LastPtr = GEP->getPointerOperand();
94 Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
95
Vikram S. Adve900fd632002-08-03 13:21:15 +000096 // Process each index except the last one.
Vikram S. Advec7a6d242002-09-16 16:40:07 +000097 User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000098 for (; OI+1 != OE; ++OI) {
Anand Shukla5ba99bd2002-06-25 21:07:58 +000099 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000100
101 // If this is the first index and is 0, skip it and move on!
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000102 if (OI == GEP->idx_begin()) {
Brian Gaeke10585d92004-07-06 18:15:39 +0000103 if (isZeroConst (*OI))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000104 continue;
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000105 }
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000106 else // Not the first index: include initial [0] to deref the last ptr
107 Indices.push_back(Constant::getNullValue(Type::LongTy));
Vikram S. Adve900fd632002-08-03 13:21:15 +0000108
Chris Lattner097632e2002-04-29 01:58:47 +0000109 Indices.push_back(*OI);
110
Vikram S. Adve900fd632002-08-03 13:21:15 +0000111 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000112 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
Vikram S. Adve900fd632002-08-03 13:21:15 +0000113 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000114 }
Vikram S. Adve900fd632002-08-03 13:21:15 +0000115
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000116 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000117 //
Chris Lattner7e708292002-06-25 16:13:24 +0000118 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000119
Vikram S. Adve900fd632002-08-03 13:21:15 +0000120 // Get the final index vector, including an initial [0] as before.
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000121 std::vector<Value*> Indices;
Chris Lattner3cac88a2002-09-11 01:21:33 +0000122 Indices.push_back(Constant::getNullValue(Type::LongTy));
Chris Lattner097632e2002-04-29 01:58:47 +0000123 Indices.push_back(*OI);
124
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000125 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000126 InsertPoint);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000127
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000128 // Replace all uses of the old instruction with the new
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000129 GEP->replaceAllUsesWith(NewVal);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000130
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000131 // Now remove and delete the old instruction...
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000132 BB->getInstList().erase(GEP);
133
Vikram S. Adve900fd632002-08-03 13:21:15 +0000134 return true;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000135}
Brian Gaeked0fde302003-11-11 22:41:34 +0000136