LLVM API Documentation
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 #include <iostream> 00028 using namespace llvm; 00029 00030 namespace { 00031 Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added"); 00032 00033 struct DecomposePass : public BasicBlockPass { 00034 virtual bool runOnBasicBlock(BasicBlock &BB); 00035 }; 00036 RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional " 00037 "structure/array references"); 00038 } 00039 00040 // runOnBasicBlock - Entry point for array or structure references with multiple 00041 // indices. 00042 // 00043 bool DecomposePass::runOnBasicBlock(BasicBlock &BB) { 00044 bool changed = false; 00045 for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) 00046 if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc 00047 if (gep->getNumIndices() >= 2) 00048 changed |= DecomposeArrayRef(gep); // always modifies II 00049 return changed; 00050 } 00051 00052 FunctionPass *llvm::createDecomposeMultiDimRefsPass() { 00053 return new DecomposePass(); 00054 } 00055 00056 static inline bool isZeroConst (Value *V) { 00057 return isa<Constant> (V) && (cast<Constant>(V)->isNullValue()); 00058 } 00059 00060 // Function: DecomposeArrayRef() 00061 // 00062 // For any GetElementPtrInst with 2 or more array and structure indices: 00063 // 00064 // opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN 00065 // 00066 // this function generates the following sequence: 00067 // 00068 // ptr1 = getElementPtr P, idx1 00069 // ptr2 = getElementPtr ptr1, 0, idx2 00070 // ... 00071 // ptrN-1 = getElementPtr ptrN-2, 0, idxN-1 00072 // opCode ptrN-1, 0, idxN // New-MAI 00073 // 00074 // Then it replaces the original instruction with this sequence, 00075 // and replaces all uses of the original instruction with New-MAI. 00076 // If idx1 is 0, we simply omit the first getElementPtr instruction. 00077 // 00078 // On return: BBI points to the instruction after the current one 00079 // (whether or not *BBI was replaced). 00080 // 00081 // Return value: true if the instruction was replaced; false otherwise. 00082 // 00083 bool llvm::DecomposeArrayRef(GetElementPtrInst* GEP) { 00084 if (GEP->getNumIndices() < 2 00085 || (GEP->getNumIndices() == 2 00086 && isZeroConst(GEP->getOperand(1)))) { 00087 DEBUG (std::cerr << "DecomposeArrayRef: Skipping " << *GEP); 00088 return false; 00089 } else { 00090 DEBUG (std::cerr << "DecomposeArrayRef: Decomposing " << *GEP); 00091 } 00092 00093 BasicBlock *BB = GEP->getParent(); 00094 Value *LastPtr = GEP->getPointerOperand(); 00095 Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn 00096 00097 // Process each index except the last one. 00098 User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end(); 00099 for (; OI+1 != OE; ++OI) { 00100 std::vector<Value*> Indices; 00101 00102 // If this is the first index and is 0, skip it and move on! 00103 if (OI == GEP->idx_begin()) { 00104 if (isZeroConst (*OI)) 00105 continue; 00106 } 00107 else // Not the first index: include initial [0] to deref the last ptr 00108 Indices.push_back(Constant::getNullValue(Type::LongTy)); 00109 00110 Indices.push_back(*OI); 00111 00112 // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices 00113 LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint); 00114 ++NumAdded; 00115 } 00116 00117 // Now create a new instruction to replace the original one 00118 // 00119 const PointerType *PtrTy = cast<PointerType>(LastPtr->getType()); 00120 00121 // Get the final index vector, including an initial [0] as before. 00122 std::vector<Value*> Indices; 00123 Indices.push_back(Constant::getNullValue(Type::LongTy)); 00124 Indices.push_back(*OI); 00125 00126 Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(), 00127 InsertPoint); 00128 00129 // Replace all uses of the old instruction with the new 00130 GEP->replaceAllUsesWith(NewVal); 00131 00132 // Now remove and delete the old instruction... 00133 BB->getInstList().erase(GEP); 00134 00135 return true; 00136 } 00137