#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
-#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/MemoryBuiltins.h"
/// Constant Propagation.
///
class SCCPSolver : public InstVisitor<SCCPSolver> {
- LLVMContext *Context;
DenseSet<BasicBlock*> BBExecutable;// The basic blocks that are executable
std::map<Value*, LatticeVal> ValueState; // The state each value is in.
typedef std::pair<BasicBlock*, BasicBlock*> Edge;
DenseSet<Edge> KnownFeasibleEdges;
public:
- void setContext(LLVMContext *C) { Context = C; }
/// MarkBlockExecutable - This method can be used by clients to mark all of
/// the blocks that are known to be intrinsically live in the processed unit.
if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
if (BI->isUnconditional()) {
Succs[0] = true;
- } else {
- LatticeVal &BCValue = getValueState(BI->getCondition());
- if (BCValue.isOverdefined() ||
- (BCValue.isConstant() && !isa<ConstantInt>(BCValue.getConstant()))) {
- // Overdefined condition variables, and branches on unfoldable constant
- // conditions, mean the branch could go either way.
- Succs[0] = Succs[1] = true;
- } else if (BCValue.isConstant()) {
- // Constant condition variables mean the branch can only go a single way
- Succs[BCValue.getConstant() == ConstantInt::getFalse(*Context)] = true;
- }
+ return;
+ }
+
+ LatticeVal &BCValue = getValueState(BI->getCondition());
+ if (BCValue.isOverdefined() ||
+ (BCValue.isConstant() && !isa<ConstantInt>(BCValue.getConstant()))) {
+ // Overdefined condition variables, and branches on unfoldable constant
+ // conditions, mean the branch could go either way.
+ Succs[0] = Succs[1] = true;
+ return;
}
+
+ // Constant condition variables mean the branch can only go a single way.
+ Succs[cast<ConstantInt>(BCValue.getConstant())->isZero()] = true;
return;
}
return true;
LatticeVal &BCValue = getValueState(BI->getCondition());
- if (BCValue.isOverdefined()) {
- // Overdefined condition variables mean the branch could go either way.
- return true;
- } else if (BCValue.isConstant()) {
- // Not branching on an evaluatable constant?
- if (!isa<ConstantInt>(BCValue.getConstant())) return true;
- // Constant condition variables mean the branch can only go a single way
- return BI->getSuccessor(BCValue.getConstant() ==
- ConstantInt::getFalse(*Context)) == To;
- }
- return false;
+ // Overdefined condition variables mean the branch could go either way,
+ // undef conditions mean that neither edge is feasible yet.
+ if (!BCValue.isConstant())
+ return BCValue.isOverdefined();
+
+ // Not branching on an evaluatable constant?
+ if (!isa<ConstantInt>(BCValue.getConstant())) return true;
+
+ // Constant condition variables mean the branch can only go a single way.
+ bool CondIsFalse = cast<ConstantInt>(BCValue.getConstant())->isZero();
+ return BI->getSuccessor(CondIsFalse) == To;
}
// Invoke instructions successors are always executable.
if (!getValueState(BI->getCondition()).isUndefined())
continue;
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
- if (SI->getNumSuccessors()<2) // no cases
+ if (SI->getNumSuccessors() < 2) // no cases
continue;
if (!getValueState(SI->getCondition()).isUndefined())
continue;
// as undef, then further analysis could think the undef went another way
// leading to an inconsistent set of conclusions.
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
- BI->setCondition(ConstantInt::getFalse(*Context));
+ BI->setCondition(ConstantInt::getFalse(BI->getContext()));
} else {
SwitchInst *SI = cast<SwitchInst>(TI);
SI->setCondition(SI->getCaseValue(1));
bool SCCP::runOnFunction(Function &F) {
DEBUG(errs() << "SCCP on function '" << F.getName() << "'\n");
SCCPSolver Solver;
- Solver.setContext(&F.getContext());
// Mark the first block of the function as being executable.
Solver.MarkBlockExecutable(F.begin());
}
bool IPSCCP::runOnModule(Module &M) {
- LLVMContext *Context = &M.getContext();
-
SCCPSolver Solver;
- Solver.setContext(Context);
// Loop over all functions, marking arguments to those with their addresses
// taken or that are external as overdefined.