LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DecomposeMultiDimRefs.cpp

Go to the documentation of this file.
00001 //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // DecomposeMultiDimRefs - Convert multi-dimensional references consisting of
00011 // any combination of 2 or more array and structure indices into a sequence of
00012 // instructions (using getelementpr and cast) so that each instruction has at
00013 // most one index (except structure references, which need an extra leading
00014 // index of [0]).
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "SparcV9Internals.h"
00019 #include "llvm/DerivedTypes.h"
00020 #include "llvm/Constants.h"
00021 #include "llvm/Constant.h"
00022 #include "llvm/Instructions.h"
00023 #include "llvm/BasicBlock.h"
00024 #include "llvm/Pass.h"
00025 #include "llvm/ADT/Statistic.h"
00026 #include "llvm/Support/Debug.h"
00027 using namespace llvm;
00028 
00029 namespace {
00030   Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
00031 
00032   struct DecomposePass : public BasicBlockPass {
00033     virtual bool runOnBasicBlock(BasicBlock &BB);
00034   };
00035   RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
00036                                "structure/array references");
00037 }
00038 
00039 // runOnBasicBlock - Entry point for array or structure references with multiple
00040 // indices.
00041 //
00042 bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
00043   bool changed = false;
00044   for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
00045     if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
00046       if (gep->getNumIndices() >= 2)
00047         changed |= DecomposeArrayRef(gep); // always modifies II
00048   return changed;
00049 }
00050 
00051 FunctionPass *llvm::createDecomposeMultiDimRefsPass() {
00052   return new DecomposePass();
00053 }
00054 
00055 static inline bool isZeroConst (Value *V) {
00056   return isa<Constant> (V) && (cast<Constant>(V)->isNullValue());
00057 }
00058 
00059 // Function: DecomposeArrayRef()
00060 //  
00061 // For any GetElementPtrInst with 2 or more array and structure indices:
00062 // 
00063 //      opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
00064 // 
00065 // this function generates the following sequence:
00066 // 
00067 //      ptr1   = getElementPtr P,         idx1
00068 //      ptr2   = getElementPtr ptr1,   0, idx2
00069 //      ...
00070 //      ptrN-1 = getElementPtr ptrN-2, 0, idxN-1
00071 //      opCode                 ptrN-1, 0, idxN  // New-MAI
00072 // 
00073 // Then it replaces the original instruction with this sequence,
00074 // and replaces all uses of the original instruction with New-MAI.
00075 // If idx1 is 0, we simply omit the first getElementPtr instruction.
00076 // 
00077 // On return: BBI points to the instruction after the current one
00078 //            (whether or not *BBI was replaced).
00079 // 
00080 // Return value: true if the instruction was replaced; false otherwise.
00081 // 
00082 bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) {
00083   if (GEP->getNumIndices() < 2
00084       || (GEP->getNumIndices() == 2
00085           && isZeroConst(GEP->getOperand(1)))) {
00086     DEBUG (std::cerr << "DecomposeArrayRef: Skipping " << *GEP);
00087     return false;
00088   } else {
00089     DEBUG (std::cerr << "DecomposeArrayRef: Decomposing " << *GEP);
00090   }
00091 
00092   BasicBlock *BB = GEP->getParent();
00093   Value *LastPtr = GEP->getPointerOperand();
00094   Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
00095 
00096   // Process each index except the last one.
00097   User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end();
00098   for (; OI+1 != OE; ++OI) {
00099     std::vector<Value*> Indices;
00100     
00101     // If this is the first index and is 0, skip it and move on!
00102     if (OI == GEP->idx_begin()) {
00103       if (isZeroConst (*OI))
00104         continue;
00105     }
00106     else // Not the first index: include initial [0] to deref the last ptr
00107       Indices.push_back(Constant::getNullValue(Type::LongTy));
00108 
00109     Indices.push_back(*OI);
00110 
00111     // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
00112     LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
00113     ++NumAdded;
00114   }
00115 
00116   // Now create a new instruction to replace the original one
00117   //
00118   const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
00119 
00120   // Get the final index vector, including an initial [0] as before.
00121   std::vector<Value*> Indices;
00122   Indices.push_back(Constant::getNullValue(Type::LongTy));
00123   Indices.push_back(*OI);
00124 
00125   Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
00126                                         InsertPoint);
00127 
00128   // Replace all uses of the old instruction with the new
00129   GEP->replaceAllUsesWith(NewVal);
00130 
00131   // Now remove and delete the old instruction...
00132   BB->getInstList().erase(GEP);
00133 
00134   return true;
00135 }
00136