LLVM API Documentation

SimplifyCFG.cpp File Reference

#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/Type.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include <algorithm>
#include <functional>
#include <set>
#include <map>

Include dependency graph for SimplifyCFG.cpp:

Go to the source code of this file.

Defines

#define DEBUG_TYPE   "simplifycfg"

Functions

 STATISTIC (NumSpeculations,"Number of speculative executed instructions")
static bool SafeToMergeTerminators (TerminatorInst *SI1, TerminatorInst *SI2)
static void AddPredecessorToBlock (BasicBlock *Succ, BasicBlock *NewPred, BasicBlock *ExistPred)
static bool CanPropagatePredecessorsForPHIs (BasicBlock *BB, BasicBlock *Succ)
static bool TryToSimplifyUncondBranchFromEmptyBlock (BasicBlock *BB, BasicBlock *Succ)
static Value * GetIfCondition (BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
static bool DominatesMergePoint (Value *V, BasicBlock *BB, std::set< Instruction * > *AggressiveInsts)
static Value * GatherConstantSetEQs (Value *V, std::vector< ConstantInt * > &Values)
static Value * GatherConstantSetNEs (Value *V, std::vector< ConstantInt * > &Values)
static bool GatherValueComparisons (Instruction *Cond, Value *&CompVal, std::vector< ConstantInt * > &Values)
static void EraseTerminatorInstAndDCECond (TerminatorInst *TI)
static Value * isValueEqualityComparison (TerminatorInst *TI)
static BasicBlock * GetValueEqualityComparisonCases (TerminatorInst *TI, std::vector< std::pair< ConstantInt *, BasicBlock * > > &Cases)
static void EliminateBlockCases (BasicBlock *BB, std::vector< std::pair< ConstantInt *, BasicBlock * > > &Cases)
static bool ValuesOverlap (std::vector< std::pair< ConstantInt *, BasicBlock * > > &C1, std::vector< std::pair< ConstantInt *, BasicBlock * > > &C2)
static bool SimplifyEqualityComparisonWithOnlyPredecessor (TerminatorInst *TI, BasicBlock *Pred)
static bool FoldValueComparisonIntoPredecessors (TerminatorInst *TI)
static bool HoistThenElseCodeToIf (BranchInst *BI)
static bool SpeculativelyExecuteBB (BranchInst *BI, BasicBlock *BB1)
static bool BlockIsSimpleEnoughToThreadThrough (BasicBlock *BB)
static bool FoldCondBranchOnPHI (BranchInst *BI)
static bool FoldTwoEntryPHINode (PHINode *PN)
static bool SimplifyCondBranchToTwoReturns (BranchInst *BI)
static bool FoldBranchToCommonDest (BranchInst *BI)
static bool SimplifyCondBranchToCondBranch (BranchInst *PBI, BranchInst *BI)
bool llvm::SimplifyCFG (BasicBlock *BB)


Define Documentation

#define DEBUG_TYPE   "simplifycfg"

Definition at line 14 of file SimplifyCFG.cpp.


Function Documentation

static void AddPredecessorToBlock ( BasicBlock *  Succ,
BasicBlock *  NewPred,
BasicBlock *  ExistPred 
) [static]

AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will now be entries in it from the 'NewPred' block. The values that will be flowing into the PHI nodes will be the same as those coming in from ExistPred, an existing predecessor of Succ.

Definition at line 65 of file SimplifyCFG.cpp.

References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), llvm::dyn_cast(), llvm::PHINode::getIncomingValueForBlock(), llvm::succ_begin(), and llvm::succ_end().

Referenced by FoldBranchToCommonDest(), FoldValueComparisonIntoPredecessors(), and HoistThenElseCodeToIf().

static bool BlockIsSimpleEnoughToThreadThrough ( BasicBlock *  BB  )  [static]

BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch across this block.

Definition at line 1081 of file SimplifyCFG.cpp.

References BBI, llvm::BasicBlock::begin(), E, llvm::Instruction::getParent(), and llvm::BasicBlock::getTerminator().

Referenced by FoldCondBranchOnPHI(), and SimplifyCondBranchToCondBranch().

static bool CanPropagatePredecessorsForPHIs ( BasicBlock *  BB,
BasicBlock *  Succ 
) [static]

CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an almost-empty BB ending in an unconditional branch to Succ, into succ.

Assumption: Succ is the single successor for BB.

Definition at line 82 of file SimplifyCFG.cpp.

References BBI, llvm::BasicBlock::begin(), DOUT, llvm::dyn_cast(), E, llvm::PHINode::getIncomingValueForBlock(), llvm::Value::getNameStart(), llvm::BasicBlock::getSinglePredecessor(), PI, llvm::pred_begin(), llvm::pred_end(), llvm::succ_begin(), and Val.

Referenced by TryToSimplifyUncondBranchFromEmptyBlock().

static bool DominatesMergePoint ( Value *  V,
BasicBlock *  BB,
std::set< Instruction * > *  AggressiveInsts 
) [static]

DominatesMergePoint - If we have a merge point of an "if condition" as accepted above, return true if the specified value dominates the block. We don't handle the true generality of domination here, just a special case which works well enough for us.

If AggressiveInsts is non-null, and if V does not dominate BB, we check to see if V (which must be an instruction) is cheap to compute and is non-trapping. If both are true, the instruction is inserted into the set and true is returned.

Definition at line 359 of file SimplifyCFG.cpp.

References Add(), And(), llvm::BasicBlock::begin(), llvm::CallingConv::C, llvm::dyn_cast(), FCmp, llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::Type::isFPOrFPVector(), Load, LShr, llvm::User::op_begin(), llvm::User::op_end(), llvm::APIntOps::Or(), and llvm::APIntOps::Xor().

Referenced by FoldTwoEntryPHINode().

static void EliminateBlockCases ( BasicBlock *  BB,
std::vector< std::pair< ConstantInt *, BasicBlock * > > &  Cases 
) [static]

EliminateBlockCases - Given a vector of bb/value pairs, remove any entries in the list that match the specified block.

Definition at line 557 of file SimplifyCFG.cpp.

References second.

Referenced by SimplifyEqualityComparisonWithOnlyPredecessor().

static void EraseTerminatorInstAndDCECond ( TerminatorInst *  TI  )  [static]

static bool FoldBranchToCommonDest ( BranchInst *  BI  )  [static]

static bool FoldCondBranchOnPHI ( BranchInst *  BI  )  [static]

static bool FoldTwoEntryPHINode ( PHINode *  PN  )  [static]

static bool FoldValueComparisonIntoPredecessors ( TerminatorInst *  TI  )  [static]

static Value* GatherConstantSetEQs ( Value *  V,
std::vector< ConstantInt * > &  Values 
) [static]

GatherConstantSetEQs - Given a potentially 'or'd together collection of icmp_eq instructions that compare a value against a constant, return the value being compared, and stick the constant into the Values vector.

Definition at line 432 of file SimplifyCFG.cpp.

References llvm::CallingConv::C, llvm::Instruction::getOpcode(), llvm::User::getOperand(), Inst, LHS, llvm::APIntOps::Or(), and RHS.

Referenced by GatherValueComparisons().

static Value* GatherConstantSetNEs ( Value *  V,
std::vector< ConstantInt * > &  Values 
) [static]

GatherConstantSetNEs - Given a potentially 'and'd together collection of setne instructions that compare a value against a constant, return the value being compared, and stick the constant into the Values vector.

Definition at line 456 of file SimplifyCFG.cpp.

References And(), llvm::CallingConv::C, llvm::Instruction::getOpcode(), llvm::User::getOperand(), Inst, LHS, and RHS.

Referenced by GatherValueComparisons().

static bool GatherValueComparisons ( Instruction *  Cond,
Value *&  CompVal,
std::vector< ConstantInt * > &  Values 
) [static]

GatherValueComparisons - If the specified Cond is an 'and' or 'or' of a bunch of comparisons of one value against constants, return the value and the constants being compared.

Definition at line 480 of file SimplifyCFG.cpp.

References And(), GatherConstantSetEQs(), GatherConstantSetNEs(), llvm::Instruction::getOpcode(), and llvm::APIntOps::Or().

Referenced by llvm::SimplifyCFG().

static Value* GetIfCondition ( BasicBlock *  BB,
BasicBlock *&  IfTrue,
BasicBlock *&  IfFalse 
) [static]

GetIfCondition - Given a basic block (BB) with two predecessors (and presumably PHI nodes in it), check to see if the merge at this block is due to an "if condition". If so, return the boolean condition that determines which entry into BB will be taken. Also, return by references the block that will be entered from if the condition is true, and the block that will be entered if the condition is false.

Definition at line 268 of file SimplifyCFG.cpp.

References llvm::BranchInst::getCondition(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isConditional(), llvm::pred_begin(), llvm::pred_end(), and std::swap().

Referenced by FoldTwoEntryPHINode().

static BasicBlock* GetValueEqualityComparisonCases ( TerminatorInst *  TI,
std::vector< std::pair< ConstantInt *, BasicBlock * > > &  Cases 
) [static]

GetValueEqualityComparisonCases - Given a value comparison instruction, decode all of the 'cases' that it represents and return the 'default' block.

Definition at line 536 of file SimplifyCFG.cpp.

References llvm::BranchInst::getCondition(), and llvm::BranchInst::getSuccessor().

Referenced by FoldValueComparisonIntoPredecessors(), and SimplifyEqualityComparisonWithOnlyPredecessor().

static bool HoistThenElseCodeToIf ( BranchInst *  BI  )  [static]

static Value* isValueEqualityComparison ( TerminatorInst *  TI  )  [static]

isValueEqualityComparison - Return true if the specified terminator checks to see if a value is equal to constant integer value.

Definition at line 513 of file SimplifyCFG.cpp.

References llvm::pred_begin(), and llvm::pred_end().

Referenced by FoldValueComparisonIntoPredecessors(), llvm::SimplifyCFG(), and SimplifyEqualityComparisonWithOnlyPredecessor().

static bool SafeToMergeTerminators ( TerminatorInst *  SI1,
TerminatorInst *  SI2 
) [static]

SafeToMergeTerminators - Return true if it is safe to merge these two terminator instructions together.

Definition at line 38 of file SimplifyCFG.cpp.

References BBI, llvm::SmallPtrSet< PtrType, SmallSize >::count(), E, llvm::PHINode::getIncomingValueForBlock(), llvm::Instruction::getParent(), llvm::succ_begin(), and llvm::succ_end().

Referenced by FoldBranchToCommonDest(), and FoldValueComparisonIntoPredecessors().

static bool SimplifyCondBranchToCondBranch ( BranchInst *  PBI,
BranchInst *  BI 
) [static]

static bool SimplifyCondBranchToTwoReturns ( BranchInst *  BI  )  [static]

SimplifyCondBranchToTwoReturns - If we found a conditional branch that goes to two returning blocks, try to merge them together into one return, introducing a select if the return values disagree.

Definition at line 1324 of file SimplifyCFG.cpp.

References BBI, llvm::BasicBlock::begin(), BrCond, DOUT, EraseTerminatorInstAndDCECond(), llvm::BranchInst::getCondition(), llvm::Instruction::getParent(), llvm::ReturnInst::getReturnValue(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isConditional(), and llvm::BasicBlock::removePredecessor().

Referenced by llvm::SimplifyCFG().

static bool SimplifyEqualityComparisonWithOnlyPredecessor ( TerminatorInst *  TI,
BasicBlock *  Pred 
) [static]

SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a terminator instruction and its block is known to only have a single predecessor block, check to see if that predecessor is also a value comparison with the same value, and if that comparison determines the outcome of this comparison. If so, simplify TI. This does a very limited form of jump threading.

Definition at line 607 of file SimplifyCFG.cpp.

References DOUT, EliminateBlockCases(), EraseTerminatorInstAndDCECond(), first, llvm::SwitchInst::getCaseValue(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::SwitchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), GetValueEqualityComparisonCases(), isValueEqualityComparison(), llvm::SwitchInst::removeCase(), llvm::BasicBlock::removePredecessor(), second, llvm::succ_begin(), llvm::succ_end(), and ValuesOverlap().

Referenced by llvm::SimplifyCFG().

static bool SpeculativelyExecuteBB ( BranchInst *  BI,
BasicBlock *  BB1 
) [static]

STATISTIC ( NumSpeculations  ,
"Number of speculative executed instructions"   
)

static bool TryToSimplifyUncondBranchFromEmptyBlock ( BasicBlock *  BB,
BasicBlock *  Succ 
) [static]

static bool ValuesOverlap ( std::vector< std::pair< ConstantInt *, BasicBlock * > > &  C1,
std::vector< std::pair< ConstantInt *, BasicBlock * > > &  C2 
) [static]

ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as well.

Definition at line 569 of file SimplifyCFG.cpp.

References C2, first, std::swap(), and V1.

Referenced by SimplifyEqualityComparisonWithOnlyPredecessor().