Skip to content

Commit

Permalink
Last bit of TargetLibraryInfo propagation. Also fixed a case for Targ…
Browse files Browse the repository at this point in the history
…etData

where it appeared beneficial to pass.
More of rdar://10500969

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@145630 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Chad Rosier committed Dec 1, 2011
1 parent dc81e5d commit 00737bd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
21 changes: 12 additions & 9 deletions lib/Analysis/ScalarEvolution.cpp
Expand Up @@ -4772,7 +4772,8 @@ static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
/// reason, return null.
static Constant *EvaluateExpression(Value *V, const Loop *L,
DenseMap<Instruction *, Constant *> &Vals,
const TargetData *TD) {
const TargetData *TD,
const TargetLibraryInfo *TLI) {
// Convenient constant check, but redundant for recursive calls.
if (Constant *C = dyn_cast<Constant>(V)) return C;
Instruction *I = dyn_cast<Instruction>(V);
Expand All @@ -4798,7 +4799,7 @@ static Constant *EvaluateExpression(Value *V, const Loop *L,
if (!Operands[i]) return 0;
continue;
}
Constant *C = EvaluateExpression(Operand, L, Vals, TD);
Constant *C = EvaluateExpression(Operand, L, Vals, TD, TLI);
Vals[Operand] = C;
if (!C) return 0;
Operands[i] = C;
Expand All @@ -4811,7 +4812,8 @@ static Constant *EvaluateExpression(Value *V, const Loop *L,
if (!LI->isVolatile())
return ConstantFoldLoadFromConstPtr(Operands[0], TD);
}
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Operands, TD);
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Operands, TD,
TLI);
}

/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
Expand Down Expand Up @@ -4866,7 +4868,8 @@ ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
// Compute the value of the PHIs for the next iteration.
// EvaluateExpression adds non-phi values to the CurrentIterVals map.
DenseMap<Instruction *, Constant *> NextIterVals;
Constant *NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
Constant *NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD,
TLI);
if (NextPHI == 0)
return 0; // Couldn't evaluate!
NextIterVals[PN] = NextPHI;
Expand All @@ -4891,7 +4894,7 @@ ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
Constant *&NextPHI = NextIterVals[PHI];
if (!NextPHI) { // Not already computed.
Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD, TLI);
}
if (NextPHI != I->second)
StoppedEvolving = false;
Expand Down Expand Up @@ -4946,8 +4949,8 @@ const SCEV *ScalarEvolution::ComputeExitCountExhaustively(const Loop *L,
unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
ConstantInt *CondVal =
dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, L,
CurrentIterVals, TD));
dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, L, CurrentIterVals,
TD, TLI));

// Couldn't symbolically evaluate.
if (!CondVal) return getCouldNotCompute();
Expand Down Expand Up @@ -4977,7 +4980,7 @@ const SCEV *ScalarEvolution::ComputeExitCountExhaustively(const Loop *L,
if (NextPHI) continue; // Already computed!

Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD, TLI);
}
CurrentIterVals.swap(NextIterVals);
}
Expand Down Expand Up @@ -5175,7 +5178,7 @@ const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
C = ConstantFoldLoadFromConstPtr(Operands[0], TD);
} else
C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Operands, TD);
Operands, TD, TLI);
if (!C) return V;
return getSCEV(C);
}
Expand Down
26 changes: 17 additions & 9 deletions lib/Transforms/IPO/GlobalOpt.cpp
Expand Up @@ -26,6 +26,7 @@
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -61,6 +62,7 @@ namespace {
struct GlobalStatus;
struct GlobalOpt : public ModulePass {
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetLibraryInfo>();
}
static char ID; // Pass identification, replacement for typeid
GlobalOpt() : ModulePass(ID) {
Expand All @@ -84,7 +86,10 @@ namespace {
}

char GlobalOpt::ID = 0;
INITIALIZE_PASS(GlobalOpt, "globalopt",
INITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt",
"Global Variable Optimizer", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
INITIALIZE_PASS_END(GlobalOpt, "globalopt",
"Global Variable Optimizer", false, false)

ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
Expand Down Expand Up @@ -2304,7 +2309,8 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
DenseMap<Constant*, Constant*> &MutatedMemory,
std::vector<GlobalVariable*> &AllocaTmps,
SmallPtrSet<Constant*, 8> &SimpleConstants,
const TargetData *TD) {
const TargetData *TD,
const TargetLibraryInfo *TLI) {
// Check to see if this function is already executing (recursion). If so,
// bail out. TODO: we might want to accept limited recursion.
if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
Expand Down Expand Up @@ -2461,7 +2467,7 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,

if (Callee->isDeclaration()) {
// If this is a function we can constant fold, do it.
if (Constant *C = ConstantFoldCall(Callee, Formals)) {
if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
InstResult = C;
} else {
return false;
Expand All @@ -2473,7 +2479,8 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
Constant *RetVal;
// Execute the call, if successful, use the return value.
if (!EvaluateFunction(Callee, RetVal, Formals, CallStack,
MutatedMemory, AllocaTmps, SimpleConstants, TD))
MutatedMemory, AllocaTmps, SimpleConstants, TD,
TLI))
return false;
InstResult = RetVal;
}
Expand Down Expand Up @@ -2547,7 +2554,8 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,

/// EvaluateStaticConstructor - Evaluate static constructors in the function, if
/// we can. Return true if we can, false otherwise.
static bool EvaluateStaticConstructor(Function *F, const TargetData *TD) {
static bool EvaluateStaticConstructor(Function *F, const TargetData *TD,
const TargetLibraryInfo *TLI) {
/// MutatedMemory - For each store we execute, we update this map. Loads
/// check this to get the most up-to-date value. If evaluation is successful,
/// this state is committed to the process.
Expand All @@ -2572,7 +2580,7 @@ static bool EvaluateStaticConstructor(Function *F, const TargetData *TD) {
bool EvalSuccess = EvaluateFunction(F, RetValDummy,
SmallVector<Constant*, 0>(), CallStack,
MutatedMemory, AllocaTmps,
SimpleConstants, TD);
SimpleConstants, TD, TLI);

if (EvalSuccess) {
// We succeeded at evaluation: commit the result.
Expand Down Expand Up @@ -2601,8 +2609,6 @@ static bool EvaluateStaticConstructor(Function *F, const TargetData *TD) {
return EvalSuccess;
}



/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
/// Return true if anything changed.
bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
Expand All @@ -2611,6 +2617,8 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
if (Ctors.empty()) return false;

const TargetData *TD = getAnalysisIfAvailable<TargetData>();
const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();

// Loop over global ctors, optimizing them when we can.
for (unsigned i = 0; i != Ctors.size(); ++i) {
Function *F = Ctors[i];
Expand All @@ -2628,7 +2636,7 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
if (F->empty()) continue;

// If we can evaluate the ctor at compile time, do.
if (EvaluateStaticConstructor(F, TD)) {
if (EvaluateStaticConstructor(F, TD, TLI)) {
Ctors.erase(Ctors.begin()+i);
MadeChange = true;
--i;
Expand Down
4 changes: 3 additions & 1 deletion lib/Transforms/InstCombine/InstCombine.h
Expand Up @@ -22,6 +22,7 @@
namespace llvm {
class CallSite;
class TargetData;
class TargetLibraryInfo;
class DbgDeclareInst;
class MemIntrinsic;
class MemSetInst;
Expand Down Expand Up @@ -71,6 +72,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
: public FunctionPass,
public InstVisitor<InstCombiner, Instruction*> {
TargetData *TD;
TargetLibraryInfo *TLI;
bool MadeIRChange;
public:
/// Worklist - All of the instructions that need to be simplified.
Expand All @@ -92,7 +94,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
bool DoOneIteration(Function &F, unsigned ItNum);

virtual void getAnalysisUsage(AnalysisUsage &AU) const;

TargetData *getTargetData() const { return TD; }

// Visitation implementation - Implement instruction combining for different
Expand Down
9 changes: 3 additions & 6 deletions lib/Transforms/InstCombine/InstCombineCasts.cpp
Expand Up @@ -148,8 +148,6 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
return ReplaceInstUsesWith(CI, New);
}



/// EvaluateInDifferentType - Given an expression that
/// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually
/// insert the code to evaluate the expression.
Expand All @@ -159,7 +157,7 @@ Value *InstCombiner::EvaluateInDifferentType(Value *V, Type *Ty,
C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
// If we got a constantexpr back, try to simplify it with TD info.
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
C = ConstantFoldConstantExpression(CE, TD);
C = ConstantFoldConstantExpression(CE, TD, TLI);
return C;
}

Expand Down Expand Up @@ -1212,10 +1210,9 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
}

// Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x)
const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfo>();
CallInst *Call = dyn_cast<CallInst>(CI.getOperand(0));
if (Call && Call->getCalledFunction() && TLI.has(LibFunc::sqrtf) &&
Call->getCalledFunction()->getName() == TLI.getName(LibFunc::sqrt) &&
if (Call && Call->getCalledFunction() && TLI->has(LibFunc::sqrtf) &&
Call->getCalledFunction()->getName() == TLI->getName(LibFunc::sqrt) &&
Call->getNumArgOperands() == 1 &&
Call->hasOneUse()) {
CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0));
Expand Down
17 changes: 11 additions & 6 deletions lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -75,7 +75,10 @@ void LLVMInitializeInstCombine(LLVMPassRegistryRef R) {
}

char InstCombiner::ID = 0;
INITIALIZE_PASS(InstCombiner, "instcombine",
INITIALIZE_PASS_BEGIN(InstCombiner, "instcombine",
"Combine redundant instructions", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
INITIALIZE_PASS_END(InstCombiner, "instcombine",
"Combine redundant instructions", false, false)

void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
Expand Down Expand Up @@ -1800,7 +1803,8 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
static bool AddReachableCodeToWorklist(BasicBlock *BB,
SmallPtrSet<BasicBlock*, 64> &Visited,
InstCombiner &IC,
const TargetData *TD) {
const TargetData *TD,
const TargetLibraryInfo *TLI) {
bool MadeIRChange = false;
SmallVector<BasicBlock*, 256> Worklist;
Worklist.push_back(BB);
Expand All @@ -1827,7 +1831,7 @@ static bool AddReachableCodeToWorklist(BasicBlock *BB,

// ConstantProp instruction if trivially constant.
if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0)))
if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
if (Constant *C = ConstantFoldInstruction(Inst, TD, TLI)) {
DEBUG(errs() << "IC: ConstFold to: " << *C << " from: "
<< *Inst << '\n');
Inst->replaceAllUsesWith(C);
Expand Down Expand Up @@ -1911,7 +1915,8 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
// the reachable instructions. Ignore blocks that are not reachable. Keep
// track of which blocks we visit.
SmallPtrSet<BasicBlock*, 64> Visited;
MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD,
TLI);

// Do a quick scan over the function. If we find any blocks that are
// unreachable, remove any instructions inside of them. This prevents
Expand Down Expand Up @@ -1956,7 +1961,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {

// Instruction isn't dead, see if we can constant propagate it.
if (!I->use_empty() && isa<Constant>(I->getOperand(0)))
if (Constant *C = ConstantFoldInstruction(I, TD)) {
if (Constant *C = ConstantFoldInstruction(I, TD, TLI)) {
DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');

// Add operands to the worklist.
Expand Down Expand Up @@ -2064,7 +2069,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {

bool InstCombiner::runOnFunction(Function &F) {
TD = getAnalysisIfAvailable<TargetData>();

TLI = &getAnalysis<TargetLibraryInfo>();

/// Builder - This is an IRBuilder that automatically inserts new
/// instructions into the worklist when they are created.
Expand Down
13 changes: 10 additions & 3 deletions lib/Transforms/Scalar/ConstantProp.cpp
Expand Up @@ -24,6 +24,8 @@
#include "llvm/Constant.h"
#include "llvm/Instruction.h"
#include "llvm/Pass.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/ADT/Statistic.h"
#include <set>
Expand All @@ -42,33 +44,38 @@ namespace {

virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired<TargetLibraryInfo>();
}
};
}

char ConstantPropagation::ID = 0;
INITIALIZE_PASS(ConstantPropagation, "constprop",
INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
"Simple constant propagation", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
INITIALIZE_PASS_END(ConstantPropagation, "constprop",
"Simple constant propagation", false, false)

FunctionPass *llvm::createConstantPropagationPass() {
return new ConstantPropagation();
}


bool ConstantPropagation::runOnFunction(Function &F) {
// Initialize the worklist to all of the instructions ready to process...
std::set<Instruction*> WorkList;
for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
WorkList.insert(&*i);
}
bool Changed = false;
TargetData *TD = getAnalysisIfAvailable<TargetData>();
TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();

while (!WorkList.empty()) {
Instruction *I = *WorkList.begin();
WorkList.erase(WorkList.begin()); // Get an element from the worklist...

if (!I->use_empty()) // Don't muck with dead instructions...
if (Constant *C = ConstantFoldInstruction(I)) {
if (Constant *C = ConstantFoldInstruction(I, TD, TLI)) {
// Add all of the users of this instruction to the worklist, they might
// be constant propagatable now...
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
Expand Down

0 comments on commit 00737bd

Please sign in to comment.