2002-04-29 01:22:55 +00:00
|
|
|
//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-23 20:43:39 +00:00
|
|
|
//
|
2002-04-29 01:58:47 +00:00
|
|
|
// DecomposeMultiDimRefs - Convert multi-dimensional references consisting of
|
|
|
|
// any combination of 2 or more array and structure indices into a sequence of
|
|
|
|
// instructions (using getelementpr and cast) so that each instruction has at
|
|
|
|
// most one index (except structure references, which need an extra leading
|
|
|
|
// index of [0]).
|
2002-03-23 20:43:39 +00:00
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-23 20:43:39 +00:00
|
|
|
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-04-29 18:48:30 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-08-03 13:21:15 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-04-29 14:57:45 +00:00
|
|
|
#include "llvm/Constant.h"
|
2002-03-23 20:43:39 +00:00
|
|
|
#include "llvm/iMemory.h"
|
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Pass.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-04-29 01:22:55 +00:00
|
|
|
namespace {
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
|
2002-04-29 01:22:55 +00:00
|
|
|
|
2002-09-16 16:40:07 +00:00
|
|
|
struct DecomposePass : public BasicBlockPass {
|
2002-09-10 17:04:02 +00:00
|
|
|
virtual bool runOnBasicBlock(BasicBlock &BB);
|
2002-04-29 01:22:55 +00:00
|
|
|
};
|
2004-01-09 06:02:20 +00:00
|
|
|
RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
|
|
|
|
"structure/array references");
|
2002-04-29 01:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runOnBasicBlock - Entry point for array or structure references with multiple
|
|
|
|
// indices.
|
|
|
|
//
|
2004-01-09 06:02:20 +00:00
|
|
|
bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
|
2002-09-16 16:40:07 +00:00
|
|
|
bool changed = false;
|
|
|
|
for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
|
2003-04-23 16:37:45 +00:00
|
|
|
if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
|
2002-09-16 16:40:07 +00:00
|
|
|
if (gep->getNumIndices() >= 2)
|
|
|
|
changed |= DecomposeArrayRef(gep); // always modifies II
|
|
|
|
return changed;
|
2002-04-29 01:22:55 +00:00
|
|
|
}
|
2002-03-23 20:43:39 +00:00
|
|
|
|
2004-01-09 06:02:20 +00:00
|
|
|
FunctionPass *llvm::createDecomposeMultiDimRefsPass() {
|
2003-11-11 22:41:34 +00:00
|
|
|
return new DecomposePass();
|
|
|
|
}
|
2002-09-16 16:40:07 +00:00
|
|
|
|
|
|
|
// Function: DecomposeArrayRef()
|
|
|
|
//
|
2002-08-22 23:37:20 +00:00
|
|
|
// For any GetElementPtrInst with 2 or more array and structure indices:
|
2002-03-23 20:43:39 +00:00
|
|
|
//
|
2002-08-03 13:21:15 +00:00
|
|
|
// opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
|
2002-03-23 20:43:39 +00:00
|
|
|
//
|
2004-07-02 05:30:01 +00:00
|
|
|
// this function generates the following sequence:
|
2002-08-03 13:21:15 +00:00
|
|
|
//
|
|
|
|
// ptr1 = getElementPtr P, idx1
|
|
|
|
// ptr2 = getElementPtr ptr1, 0, idx2
|
|
|
|
// ...
|
|
|
|
// ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
|
|
|
|
// opCode ptrN-1, 0, idxN // New-MAI
|
|
|
|
//
|
|
|
|
// Then it replaces the original instruction with this sequence,
|
|
|
|
// and replaces all uses of the original instruction with New-MAI.
|
|
|
|
// If idx1 is 0, we simply omit the first getElementPtr instruction.
|
|
|
|
//
|
|
|
|
// On return: BBI points to the instruction after the current one
|
|
|
|
// (whether or not *BBI was replaced).
|
|
|
|
//
|
|
|
|
// Return value: true if the instruction was replaced; false otherwise.
|
|
|
|
//
|
2004-01-09 06:02:20 +00:00
|
|
|
bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) {
|
2002-09-16 16:40:07 +00:00
|
|
|
if (GEP->getNumIndices() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BasicBlock *BB = GEP->getParent();
|
|
|
|
Value *LastPtr = GEP->getPointerOperand();
|
|
|
|
Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
|
|
|
|
|
2002-08-03 13:21:15 +00:00
|
|
|
// Process each index except the last one.
|
2002-09-16 16:40:07 +00:00
|
|
|
User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
|
2002-04-29 01:58:47 +00:00
|
|
|
for (; OI+1 != OE; ++OI) {
|
2002-06-25 21:07:58 +00:00
|
|
|
std::vector<Value*> Indices;
|
2002-08-03 13:21:15 +00:00
|
|
|
|
|
|
|
// If this is the first index and is 0, skip it and move on!
|
2002-09-16 16:40:07 +00:00
|
|
|
if (OI == GEP->idx_begin()) {
|
2002-09-10 17:04:02 +00:00
|
|
|
if (*OI == ConstantInt::getNullValue((*OI)->getType()))
|
|
|
|
continue;
|
|
|
|
}
|
2002-09-16 16:40:07 +00:00
|
|
|
else // Not the first index: include initial [0] to deref the last ptr
|
|
|
|
Indices.push_back(Constant::getNullValue(Type::LongTy));
|
2002-08-03 13:21:15 +00:00
|
|
|
|
2002-04-29 01:58:47 +00:00
|
|
|
Indices.push_back(*OI);
|
|
|
|
|
2002-08-03 13:21:15 +00:00
|
|
|
// New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
|
2002-09-10 17:04:02 +00:00
|
|
|
LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
|
2002-08-03 13:21:15 +00:00
|
|
|
++NumAdded;
|
2002-04-29 01:22:55 +00:00
|
|
|
}
|
2002-08-03 13:21:15 +00:00
|
|
|
|
2002-03-23 20:43:39 +00:00
|
|
|
// Now create a new instruction to replace the original one
|
2002-03-24 03:21:18 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
|
2002-03-24 03:21:18 +00:00
|
|
|
|
2002-08-03 13:21:15 +00:00
|
|
|
// Get the final index vector, including an initial [0] as before.
|
2002-06-25 21:07:58 +00:00
|
|
|
std::vector<Value*> Indices;
|
2002-09-11 01:21:33 +00:00
|
|
|
Indices.push_back(Constant::getNullValue(Type::LongTy));
|
2002-04-29 01:58:47 +00:00
|
|
|
Indices.push_back(*OI);
|
|
|
|
|
2002-09-16 16:40:07 +00:00
|
|
|
Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertPoint);
|
2002-06-25 21:07:58 +00:00
|
|
|
|
2002-03-23 20:43:39 +00:00
|
|
|
// Replace all uses of the old instruction with the new
|
2002-09-16 16:40:07 +00:00
|
|
|
GEP->replaceAllUsesWith(NewVal);
|
2002-08-03 13:21:15 +00:00
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
// Now remove and delete the old instruction...
|
2002-09-16 16:40:07 +00:00
|
|
|
BB->getInstList().erase(GEP);
|
|
|
|
|
2002-08-03 13:21:15 +00:00
|
|
|
return true;
|
2002-04-27 06:56:12 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|