X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FLevelRaise.cpp;h=881cfd9789d0a06ef53a1e326e7d2222678e1b27;hb=2a010afefc507cfcd6734435e6a404639a7f81d9;hp=a5bdce758d3b39fecc920679ff651f18adf58945;hpb=6806f5614d2ec260fda954c951d33f58e77ed610;p=oota-llvm.git diff --git a/lib/Transforms/LevelRaise.cpp b/lib/Transforms/LevelRaise.cpp index a5bdce758d3..881cfd9789d 100644 --- a/lib/Transforms/LevelRaise.cpp +++ b/lib/Transforms/LevelRaise.cpp @@ -1,4 +1,11 @@ -//===- LevelRaise.cpp - Code to change LLVM to higher level -----------------=// +//===- LevelRaise.cpp - Code to change LLVM to higher level ---------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// // // This file implements the 'raising' part of the LevelChange API. This is // useful because, in general, it makes the LLVM code terser and easier to @@ -6,21 +13,18 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Transforms/RaisePointerReferences.h" +#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/Local.h" #include "TransformInternals.h" -#include "llvm/iOther.h" -#include "llvm/iMemory.h" +#include "llvm/Instructions.h" #include "llvm/Pass.h" -#include "llvm/ConstantHandling.h" -#include "llvm/Analysis/Expressions.h" -#include "llvm/Analysis/Verifier.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "Support/CommandLine.h" #include "Support/Debug.h" #include "Support/Statistic.h" #include "Support/STLExtras.h" #include +using namespace llvm; // StartInst - This enables the -raise-start-inst=foo option to cause the level // raising pass to start at instruction "foo", which is immensely useful for @@ -48,7 +52,6 @@ NumDCEorCP("raise", "Number of insts DCEd or constprop'd"); static Statistic<> NumVarargCallChanges("raise", "Number of vararg call peepholes"); - #define PRINT_PEEPHOLE(ID, NUM, I) \ DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I) @@ -79,12 +82,12 @@ namespace { RegisterOpt X("raise", "Raise Pointer References"); } -Pass *createRaisePointerReferencesPass() { + +Pass *llvm::createRaisePointerReferencesPass() { return new RPR(); } - // isReinterpretingCast - Return true if the cast instruction specified will // cause the operand to be "reinterpreted". A value is reinterpreted if the // cast instruction would cause the underlying bits to change. @@ -133,7 +136,7 @@ static bool HandleCastToPointer(BasicBlock::iterator BI, // Cannot handle subtracts if there is more than one index required... if (HasSubUse && Indices.size() != 1) return false; - PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI); + PRINT_PEEPHOLE2("cast-add-to-gep:in", *Src, CI); // If we have a getelementptr capability... transform all of the // add instruction uses into getelementptr's. @@ -147,7 +150,7 @@ static bool HandleCastToPointer(BasicBlock::iterator BI, Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0); Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName()); - PRINT_PEEPHOLE1("cast-add-to-gep:i", I); + PRINT_PEEPHOLE1("cast-add-to-gep:i", *I); // If the instruction is actually a subtract, we are guaranteed to only have // one index (from code above), so we just need to negate the pointer index @@ -169,14 +172,14 @@ static bool HandleCastToPointer(BasicBlock::iterator BI, // Insert the GEP instruction before the old add instruction... I->getParent()->getInstList().insert(I, GEP); - PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); + PRINT_PEEPHOLE1("cast-add-to-gep:o", *GEP); GEP = new CastInst(GEP, I->getType()); // Replace the old add instruction with the shiny new GEP inst ReplaceInstWithInst(I, GEP); } - PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); + PRINT_PEEPHOLE1("cast-add-to-gep:o", *GEP); } return true; } @@ -216,9 +219,9 @@ static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI, return false; // Not convertible... perhaps next time if (getPointedToComposite(AddOp1->getType())) { // case 1 - PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI); + PRINT_PEEPHOLE2("add-to-gep1:in", *AddOp2, *BI); } else { - PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI); + PRINT_PEEPHOLE3("add-to-gep2:in", *AddOp1, *AddOp2, *BI); } GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices, @@ -226,7 +229,7 @@ static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *NCI = new CastInst(GEP, AddOp1->getType()); ReplaceInstWithInst(BB->getInstList(), BI, NCI); - PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI); + PRINT_PEEPHOLE2("add-to-gep:out", *GEP, *NCI); return true; } @@ -245,7 +248,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Into: // if (DestTy == Src->getType()) { // Check for a cast to same type as src!! - PRINT_PEEPHOLE1("cast-of-self-ty", CI); + PRINT_PEEPHOLE1("cast-of-self-ty", *CI); CI->replaceAllUsesWith(Src); if (!Src->hasName() && CI->hasName()) { std::string Name = CI->getName(); @@ -272,7 +275,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change if (ExpressionConvertibleToType(Src, DestTy, ConvertedTypes, TD)) { - PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent()); + PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", *Src, *CI, *BB->getParent()); DEBUG(std::cerr << "\nCONVERTING SRC EXPR TYPE:\n"); { // ValueMap must be destroyed before function verified! @@ -282,13 +285,11 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (Constant *CPV = dyn_cast(E)) CI->replaceAllUsesWith(CPV); - PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E); + PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", *E); DEBUG(std::cerr << "DONE CONVERTING SRC EXPR TYPE: \n" - << BB->getParent()); + << *BB->getParent()); } - DEBUG(assert(verifyFunction(*BB->getParent()) == false && - "Function broken!")); BI = BB->begin(); // Rescan basic block. BI might be invalidated. ++NumExprTreesConv; return true; @@ -301,7 +302,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Make sure the source doesn't change type ConvertedTypes[Src] = Src->getType(); if (ValueConvertibleToType(CI, Src->getType(), ConvertedTypes, TD)) { - PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent()); + PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", *Src, *CI, *BB->getParent()); DEBUG(std::cerr << "\nCONVERTING EXPR TYPE:\n"); { // ValueMap must be destroyed before function verified! @@ -309,11 +310,10 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { ConvertValueToNewType(CI, Src, ValueMap, TD); // This will delete CI! } - PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src); - DEBUG(std::cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent()); + PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", *Src); + DEBUG(std::cerr << "DONE CONVERTING EXPR TYPE: \n\n" << + *BB->getParent()); - DEBUG(assert(verifyFunction(*BB->getParent()) == false && - "Function broken!")); BI = BB->begin(); // Rescan basic block. BI might be invalidated. ++NumExprTreesConv; return true; @@ -369,22 +369,23 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Build the index vector, full of all zeros std::vector Indices; - Indices.push_back(ConstantSInt::get(Type::LongTy, 0)); + + Indices.push_back(Constant::getNullValue(Type::UIntTy)); while (CurCTy && !isa(CurCTy)) { if (const StructType *CurSTy = dyn_cast(CurCTy)) { // Check for a zero element struct type... if we have one, bail. - if (CurSTy->getElementTypes().size() == 0) break; + if (CurSTy->getNumElements() == 0) break; // Grab the first element of the struct type, which must lie at // offset zero in the struct. // - ElTy = CurSTy->getElementTypes()[0]; + ElTy = CurSTy->getElementType(0); } else { ElTy = cast(CurCTy)->getElementType(); } // Insert a zero to index through this type... - Indices.push_back(Constant::getNullValue(CurCTy->getIndexType())); + Indices.push_back(Constant::getNullValue(Type::UIntTy)); // Did we find what we're looking for? if (ElTy->isLosslesslyConvertibleTo(DestPointedTy)) break; @@ -397,7 +398,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Did we find what we were looking for? If so, do the transformation if (ElTy) { - PRINT_PEEPHOLE1("cast-for-first:in", CI); + PRINT_PEEPHOLE1("cast-for-first:in", *CI); std::string Name = CI->getName(); CI->setName(""); @@ -410,7 +411,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // CI->setOperand(0, GEP); - PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI); + PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI); ++NumGEPInstFormed; return true; } @@ -439,7 +440,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (const PointerType *CSPT = dyn_cast(CastSrc->getType())) // convertible types? if (Val->getType()->isLosslesslyConvertibleTo(CSPT->getElementType())) { - PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI); + PRINT_PEEPHOLE3("st-src-cast:in ", *Pointer, *Val, *SI); // Insert the new T cast instruction... stealing old T's name std::string Name(CI->getName()); CI->setName(""); @@ -449,7 +450,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Replace the old store with a new one! ReplaceInstWithInst(BB->getInstList(), BI, SI = new StoreInst(NCI, CastSrc)); - PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI); + PRINT_PEEPHOLE3("st-src-cast:out", *NCI, *CastSrc, *SI); ++NumLoadStorePeepholes; return true; } @@ -477,7 +478,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (const PointerType *CSPT = dyn_cast(CastSrc->getType())) // convertible types? if (PtrElType->isLosslesslyConvertibleTo(CSPT->getElementType())) { - PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI); + 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(), BI); @@ -487,7 +488,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Replace the old store with a new one! ReplaceInstWithInst(BB->getInstList(), BI, NCI); - PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI); + PRINT_PEEPHOLE3("load-src-cast:out", *NCI, *CastSrc, *NewLI); ++NumLoadStorePeepholes; return true; } @@ -526,8 +527,6 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { Constant *ConstantCallSrc = 0; if (Constant *CS = dyn_cast(CI->getCalledValue())) ConstantCallSrc = CS; - else if (GlobalValue *GV = dyn_cast(CI->getCalledValue())) - ConstantCallSrc = ConstantPointerRef::get(GV); if (ConstantCallSrc) NewCast = ConstantExpr::getCast(ConstantCallSrc, NewPFunTy); @@ -557,7 +556,7 @@ bool RPR::DoRaisePass(Function &F) { bool Changed = false; for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { - DEBUG(std::cerr << "Processing: " << *BI); + DEBUG(std::cerr << "LevelRaising: " << *BI); if (dceInstruction(BI) || doConstantPropagation(BI)) { Changed = true; ++NumDCEorCP; @@ -610,3 +609,4 @@ bool RPR::runOnFunction(Function &F) { return Changed; } +