blob: 72e537a7dcd49f44bf6f0f2e953269dfa47e39b0 [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"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000022#include "llvm/iMemory.h"
23#include "llvm/iOther.h"
24#include "llvm/BasicBlock.h"
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000025#include "llvm/Pass.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000026#include "Support/Statistic.h"
Brian Gaeke10585d92004-07-06 18:15:39 +000027#include "Support/Debug.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner2f6f03b2002-04-29 01:22:55 +000030namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000031 Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000032
Vikram S. Advec7a6d242002-09-16 16:40:07 +000033 struct DecomposePass : public BasicBlockPass {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000034 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner2f6f03b2002-04-29 01:22:55 +000035 };
Chris Lattnerd7456022004-01-09 06:02:20 +000036 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
37 "structure/array references");
Chris Lattner2f6f03b2002-04-29 01:22:55 +000038}
39
Chris Lattner2f6f03b2002-04-29 01:22:55 +000040// runOnBasicBlock - Entry point for array or structure references with multiple
41// indices.
42//
Chris Lattnerd7456022004-01-09 06:02:20 +000043bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Advec7a6d242002-09-16 16:40:07 +000044 bool changed = false;
45 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
Chris Lattnere408e252003-04-23 16:37:45 +000046 if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
Vikram S. Advec7a6d242002-09-16 16:40:07 +000047 if (gep->getNumIndices() >= 2)
48 changed |= DecomposeArrayRef(gep); // always modifies II
49 return changed;
Chris Lattner2f6f03b2002-04-29 01:22:55 +000050}
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +000051
Chris Lattnerd7456022004-01-09 06:02:20 +000052FunctionPass *llvm::createDecomposeMultiDimRefsPass() {
Brian Gaeked0fde302003-11-11 22:41:34 +000053 return new DecomposePass();
54}
Vikram S. Advec7a6d242002-09-16 16:40:07 +000055
Brian Gaeke10585d92004-07-06 18:15:39 +000056static inline bool isZeroConst (Value *V) {
57 return isa<Constant> (V) && (cast<Constant>(V)->isNullValue());
58}
59
Vikram S. Advec7a6d242002-09-16 16:40:07 +000060// Function: DecomposeArrayRef()
61//
Chris Lattnercc63f1c2002-08-22 23:37:20 +000062// For any GetElementPtrInst with 2 or more array and structure indices:
Vikram S. Adve900fd632002-08-03 13:21:15 +000063//
64// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
65//
Brian Gaeke1c4b6de42004-07-02 05:30:01 +000066// this function generates the following sequence:
Vikram S. Adve900fd632002-08-03 13:21:15 +000067//
68// ptr1 = getElementPtr P, idx1
69// ptr2 = getElementPtr ptr1, 0, idx2
70// ...
71// ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
72// opCode ptrN-1, 0, idxN // New-MAI
73//
74// Then it replaces the original instruction with this sequence,
75// and replaces all uses of the original instruction with New-MAI.
76// If idx1 is 0, we simply omit the first getElementPtr instruction.
77//
78// On return: BBI points to the instruction after the current one
79// (whether or not *BBI was replaced).
80//
81// Return value: true if the instruction was replaced; false otherwise.
82//
Chris Lattnerd7456022004-01-09 06:02:20 +000083bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) {
Brian Gaeke10585d92004-07-06 18:15:39 +000084 if (GEP->getNumIndices() < 2
85 || (GEP->getNumIndices() == 2
Brian Gaeke08cc64e2004-07-06 19:24:47 +000086 && isZeroConst(GEP->getOperand(1)))) {
Brian Gaeke10585d92004-07-06 18:15:39 +000087 DEBUG (std::cerr << "DecomposeArrayRef: Skipping " << *GEP);
Vikram S. Advec7a6d242002-09-16 16:40:07 +000088 return false;
Brian Gaeke10585d92004-07-06 18:15:39 +000089 } else {
90 DEBUG (std::cerr << "DecomposeArrayRef: Decomposing " << *GEP);
91 }
Vikram S. Advec7a6d242002-09-16 16:40:07 +000092
93 BasicBlock *BB = GEP->getParent();
94 Value *LastPtr = GEP->getPointerOperand();
95 Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
96
Vikram S. Adve900fd632002-08-03 13:21:15 +000097 // Process each index except the last one.
Vikram S. Advec7a6d242002-09-16 16:40:07 +000098 User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
Chris Lattner097632e2002-04-29 01:58:47 +000099 for (; OI+1 != OE; ++OI) {
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000100 std::vector<Value*> Indices;
Vikram S. Adve900fd632002-08-03 13:21:15 +0000101
102 // If this is the first index and is 0, skip it and move on!
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000103 if (OI == GEP->idx_begin()) {
Brian Gaeke10585d92004-07-06 18:15:39 +0000104 if (isZeroConst (*OI))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000105 continue;
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000106 }
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000107 else // Not the first index: include initial [0] to deref the last ptr
108 Indices.push_back(Constant::getNullValue(Type::LongTy));
Vikram S. Adve900fd632002-08-03 13:21:15 +0000109
Chris Lattner097632e2002-04-29 01:58:47 +0000110 Indices.push_back(*OI);
111
Vikram S. Adve900fd632002-08-03 13:21:15 +0000112 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000113 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
Vikram S. Adve900fd632002-08-03 13:21:15 +0000114 ++NumAdded;
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000115 }
Vikram S. Adve900fd632002-08-03 13:21:15 +0000116
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000117 // Now create a new instruction to replace the original one
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000118 //
Chris Lattner7e708292002-06-25 16:13:24 +0000119 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
Vikram S. Adve98d64f82002-03-24 03:21:18 +0000120
Vikram S. Adve900fd632002-08-03 13:21:15 +0000121 // Get the final index vector, including an initial [0] as before.
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000122 std::vector<Value*> Indices;
Chris Lattner3cac88a2002-09-11 01:21:33 +0000123 Indices.push_back(Constant::getNullValue(Type::LongTy));
Chris Lattner097632e2002-04-29 01:58:47 +0000124 Indices.push_back(*OI);
125
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000126 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000127 InsertPoint);
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000128
Vikram S. Advedfbbf7a2002-03-23 20:43:39 +0000129 // Replace all uses of the old instruction with the new
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000130 GEP->replaceAllUsesWith(NewVal);
Chris Lattner2f6f03b2002-04-29 01:22:55 +0000131
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000132 // Now remove and delete the old instruction...
Vikram S. Advec7a6d242002-09-16 16:40:07 +0000133 BB->getInstList().erase(GEP);
134
Vikram S. Adve900fd632002-08-03 13:21:15 +0000135 return true;
Chris Lattnerf57b8452002-04-27 06:56:12 +0000136}
Brian Gaeked0fde302003-11-11 22:41:34 +0000137