X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FLevelRaise.cpp;h=b76e8baa4b2295787c4748679f5d636ee569db6c;hb=b3abf9d0d8978408b3cb8d8983db8d1aaa9ce64c;hp=ef0a5fd314dbfbc33356cc43f48e732170ce05ea;hpb=fcc93d2c0aece88c79648be3b76f98ee9e29e826;p=oota-llvm.git diff --git a/lib/Transforms/LevelRaise.cpp b/lib/Transforms/LevelRaise.cpp index ef0a5fd314d..b76e8baa4b2 100644 --- a/lib/Transforms/LevelRaise.cpp +++ b/lib/Transforms/LevelRaise.cpp @@ -7,28 +7,27 @@ //===----------------------------------------------------------------------===// #include "llvm/Transforms/LevelChange.h" +#include "llvm/Transforms/Utils/Local.h" #include "TransformInternals.h" -#include "llvm/Method.h" #include "llvm/iOther.h" #include "llvm/iMemory.h" -#include "llvm/ConstantVals.h" -#include "llvm/Transforms/Scalar/DCE.h" -#include "llvm/Transforms/Scalar/ConstantHandling.h" -#include "llvm/Transforms/Scalar/ConstantProp.h" +#include "llvm/Pass.h" +#include "llvm/ConstantHandling.h" #include "llvm/Analysis/Expressions.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "Support/STLExtras.h" +#include "Support/StatisticReporter.h" #include -#include "llvm/Assembly/Writer.h" +static Statistic<> NumLoadStorePeepholes("raise\t\t- Number of load/store peepholes"); +static Statistic<> NumGEPInstFormed("raise\t\t- Number of other getelementptr's formed"); +static Statistic<> NumExprTreesConv("raise\t\t- Number of expression trees converted"); +static Statistic<> NumCastOfCast("raise\t\t- Number of cast-of-self removed"); +static Statistic<> NumDCEorCP("raise\t\t- Number of insts DCE'd or constprop'd"); -//#define DEBUG_PEEPHOLE_INSTS 1 -#ifdef DEBUG_PEEPHOLE_INSTS #define PRINT_PEEPHOLE(ID, NUM, I) \ - cerr << "Inst P/H " << ID << "[" << NUM << "] " << I; -#else -#define PRINT_PEEPHOLE(ID, NUM, I) -#endif + DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I) #define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0) #define PRINT_PEEPHOLE2(ID, I1, I2) \ @@ -50,7 +49,6 @@ static inline bool isReinterpretingCast(const CastInst *CI) { } - // Peephole optimize the following instructions: // %t1 = cast ? to x * // %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand @@ -61,6 +59,7 @@ static inline bool isReinterpretingCast(const CastInst *CI) { static bool HandleCastToPointer(BasicBlock::iterator BI, const PointerType *DestPTy) { CastInst *CI = cast(*BI); + if (CI->use_empty()) return false; // Scan all of the uses, looking for any uses that are not add // instructions. If we have non-adds, do not make this transformation. @@ -84,25 +83,37 @@ static bool HandleCastToPointer(BasicBlock::iterator BI, // If we have a getelementptr capability... transform all of the // add instruction uses into getelementptr's. - for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); - UI != E; ++UI) { - Instruction *I = cast(*UI); + while (!CI->use_empty()) { + BinaryOperator *I = cast(*CI->use_begin()); assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 && "Use is not a valid add instruction!"); // Get the value added to the cast result pointer... Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0); - BasicBlock *BB = I->getParent(); - BasicBlock::iterator AddIt = find(BB->getInstList().begin(), - BB->getInstList().end(), I); + Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName()); + PRINT_PEEPHOLE1("cast-add-to-gep:i", I); - GetElementPtrInst *GEP = new GetElementPtrInst(OtherPtr, Indices); + if (GEP->getType() == I->getType()) { + // Replace the old add instruction with the shiny new GEP inst + ReplaceInstWithInst(I, GEP); + } else { + // If the type produced by the gep instruction differs from the original + // add instruction type, insert a cast now. + // + + // Insert the GEP instruction before the old add instruction... and get an + // iterator to point at the add instruction... + BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1; + + PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); + CastInst *CI = new CastInst(GEP, I->getType()); + GEP = CI; + + // Replace the old add instruction with the shiny new GEP inst + ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP); + } - PRINT_PEEPHOLE1("cast-add-to-gep:i", I); - - // Replace the old add instruction with the shiny new GEP inst - ReplaceInstWithInst(BB->getInstList(), AddIt, GEP); PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); } return true; @@ -178,29 +189,16 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { CI->setName(""); Src->setName(Name, BB->getParent()->getSymbolTable()); } + + // DCE the instruction now, to avoid having the iterative version of DCE + // have to worry about it. + // + delete BB->getInstList().remove(BI); + + ++NumCastOfCast; return true; } - // Peephole optimize the following instructions: - // %tmp = cast %V to - // %V = cast %tmp to ; Where ty & ty2 are same size - // - // Into: cast %V to - // - if (SrcI) - if (CastInst *CSrc = dyn_cast(SrcI)) - if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) { - // We can only do c-c elimination if, at most, one cast does a - // reinterpretation of the input data. - // - // If legal, make this cast refer the the original casts argument! - // - PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc); - CI->setOperand(0, CSrc->getOperand(0)); - PRINT_PEEPHOLE1("cast-cast:out", CI); - return true; - } - // Check to see if it's a cast of an instruction that does not depend on the // specific type of the operands to do it's job. if (!isReinterpretingCast(CI)) { @@ -213,9 +211,7 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) { PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent()); -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "\nCONVERTING SRC EXPR TYPE:\n"; -#endif + DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n"); ValueMapCache ValueMap; Value *E = ConvertExpressionToType(Src, DestTy, ValueMap); if (Constant *CPV = dyn_cast(E)) @@ -223,9 +219,8 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { BI = BB->begin(); // Rescan basic block. BI might be invalidated. PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E); -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent(); -#endif + DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent()); + ++NumExprTreesConv; return true; } @@ -236,17 +231,14 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) { PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent()); -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "\nCONVERTING EXPR TYPE:\n"; -#endif + DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n"); ValueMapCache ValueMap; ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI! BI = BB->begin(); // Rescan basic block. BI might be invalidated. PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src); -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent(); -#endif + DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent()); + ++NumExprTreesConv; return true; } } @@ -258,6 +250,7 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (const PointerType *DestPTy = dyn_cast(DestTy)) { if (HandleCastToPointer(BI, DestPTy)) { BI = BB->begin(); // Rescan basic block. BI might be invalidated. + ++NumGEPInstFormed; return true; } } @@ -272,7 +265,6 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...> // %t1 = cast * %t1 to * // -#if 1 if (const CompositeType *CTy = getPointedToComposite(Src->getType())) if (const PointerType *DestPTy = dyn_cast(DestTy)) { @@ -342,13 +334,12 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { CI->setOperand(0, GEP); PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI); + ++NumGEPInstFormed; return true; } } } -#endif -#if 1 } else if (StoreInst *SI = dyn_cast(I)) { Value *Val = SI->getOperand(0); Value *Pointer = SI->getPointerOperand(); @@ -384,6 +375,47 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { ReplaceInstWithInst(BB->getInstList(), BI, SI = new StoreInst(NCI, CastSrc)); PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI); + ++NumLoadStorePeepholes; + return true; + } + + } else if (LoadInst *LI = dyn_cast(I)) { + Value *Pointer = LI->getOperand(0); + const Type *PtrElType = + cast(Pointer->getType())->getElementType(); + + // Peephole optimize the following instructions: + // %Val = cast * to * ;; If T1 is losslessly convertable to T2 + // %t = load * %P + // + // Into: + // %t = load * %P + // %Val = cast to + // + // Note: This is not taken care of by expr conversion because there might + // not be a cast available for the store to convert the incoming value of. + // This code is basically here to make sure that pointers don't have casts + // if possible. + // + if (CastInst *CI = dyn_cast(Pointer)) + if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType + if (PointerType *CSPT = dyn_cast(CastSrc->getType())) + // convertable types? + if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) && + !LI->hasIndices()) { // No subscripts yet! + PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI); + + // Create the new load instruction... loading the pre-casted value + LoadInst *NewLI = new LoadInst(CastSrc, LI->getName()); + + // Insert the new T cast instruction... stealing old T's name + CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName()); + BI = BB->getInstList().insert(BI, NewLI)+1; + + // Replace the old store with a new one! + ReplaceInstWithInst(BB->getInstList(), BI, NCI); + PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI); + ++NumLoadStorePeepholes; return true; } @@ -391,10 +423,10 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { isa(I->getOperand(1))) { if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0), - cast(I->getOperand(1)))) + cast(I->getOperand(1)))) { + ++NumGEPInstFormed; return true; - -#endif + } } return false; @@ -403,22 +435,18 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { -static bool DoRaisePass(Method *M) { +static bool DoRaisePass(Function *F) { bool Changed = false; - for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) { + for (Function::iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) { BasicBlock *BB = *MI; BasicBlock::InstListType &BIL = BB->getInstList(); for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { -#if DEBUG_PEEPHOLE_INSTS - cerr << "Processing: " << *BI; -#endif - if (DeadCodeElimination::dceInstruction(BIL, BI) || - ConstantPropogation::doConstantPropogation(BB, BI)) { + DEBUG(cerr << "Processing: " << *BI); + if (dceInstruction(BIL, BI) || doConstantPropogation(BB, BI)) { Changed = true; -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "DeadCode Elinated!\n"; -#endif + ++NumDCEorCP; + DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n"); } else if (PeepholeOptimize(BB, BI)) Changed = true; else @@ -429,15 +457,11 @@ static bool DoRaisePass(Method *M) { } - - -// RaisePointerReferences::doit - Raise a method representation to a higher +// RaisePointerReferences::doit - Raise a function representation to a higher // level. // -bool RaisePointerReferences::doit(Method *M) { -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "\n\n\nStarting to work on Method '" << M->getName() << "'\n"; -#endif +static bool doRPR(Function *F) { + DEBUG(cerr << "\n\n\nStarting to work on Function '" << F->getName()<< "'\n"); // Insert casts for all incoming pointer pointer values that are treated as // arrays... @@ -445,17 +469,33 @@ bool RaisePointerReferences::doit(Method *M) { bool Changed = false, LocalChange; do { -#ifdef DEBUG_PEEPHOLE_INSTS - cerr << "Looping: \n" << M; -#endif + DEBUG(cerr << "Looping: \n" << F); - // Iterate over the method, refining it, until it converges on a stable + // Iterate over the function, refining it, until it converges on a stable // state LocalChange = false; - while (DoRaisePass(M)) LocalChange = true; + while (DoRaisePass(F)) LocalChange = true; Changed |= LocalChange; } while (LocalChange); return Changed; } + +namespace { + struct RaisePointerReferences : public FunctionPass { + const char *getPassName() const { return "Raise Pointer References"; } + + virtual bool runOnFunction(Function *F) { return doRPR(F); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.preservesCFG(); + } + }; +} + +Pass *createRaisePointerReferencesPass() { + return new RaisePointerReferences(); +} + +