LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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:

Include dependency graph

Go to the source code of this file.

Classes

struct  ConstantIntOrdering

Defines

#define DEBUG_TYPE   "simplifycfg"

Functions

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


Define Documentation

#define DEBUG_TYPE   "simplifycfg"
 

Definition at line 14 of file SimplifyCFG.cpp.


Function Documentation

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::PHINode::getIncomingValueForBlock(), llvm::succ_begin(), and llvm::succ_end().

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

bool BlockIsSimpleEnoughToThreadThrough BasicBlock BB  )  [static]
 

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

Definition at line 1085 of file SimplifyCFG.cpp.

References llvm::BasicBlock::begin(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), and llvm::Value::use_iterator.

Referenced by FoldCondBranchOnPHI(), and SimplifyCondBranchToCondBranch().

bool CanPropagatePredecessorsForPHIs BasicBlock BB,
BasicBlock Succ
[static]
 

Definition at line 82 of file SimplifyCFG.cpp.

References llvm::BasicBlock::begin(), DOUT, llvm::PHINode::getIncomingValueForBlock(), llvm::Value::getNameStart(), llvm::Instruction::getParent(), llvm::pred_begin(), llvm::pred_end(), llvm::pred_iterator, and llvm::succ_begin().

Referenced by TryToSimplifyUncondBranchFromEmptyBlock().

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

Definition at line 357 of file SimplifyCFG.cpp.

References llvm::BasicBlock::begin(), llvm::Constant::canTrap(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::Type::isFPOrFPVector(), llvm::BranchInst::isUnconditional(), llvm::BasicBlock::iterator, llvm::User::op_begin(), llvm::User::op_end(), and llvm::User::op_iterator.

Referenced by FoldTwoEntryPHINode().

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

Definition at line 575 of file SimplifyCFG.cpp.

Referenced by SimplifyEqualityComparisonWithOnlyPredecessor().

void ErasePossiblyDeadInstructionTree Instruction I  )  [static]
 

ErasePossiblyDeadInstructionTree - If the specified instruction is dead and has no side effects, nuke it. If it uses any instructions that become dead because the instruction is now gone, nuke them too.

Definition at line 500 of file SimplifyCFG.cpp.

References llvm::SmallVectorImpl< T >::back(), llvm::SmallVectorImpl< T >::begin(), llvm::SmallVectorImpl< T >::empty(), llvm::SmallVectorImpl< T >::erase(), llvm::Instruction::eraseFromParent(), llvm::isInstructionTriviallyDead(), llvm::User::op_begin(), llvm::User::op_end(), llvm::User::op_iterator, llvm::SmallVectorImpl< T >::pop_back(), llvm::SmallVectorImpl< T >::push_back(), and llvm::SmallVectorImpl< T >::size().

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

bool FoldBranchToCommonDest BranchInst BI  )  [static]
 

FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch, and if a predecessor branches to us and one of our successors, fold the setcc into the predecessor and use logical operations to pick the right destination.

Definition at line 1422 of file SimplifyCFG.cpp.

References AddPredecessorToBlock(), llvm::Instruction::clone(), llvm::BinaryOperator::Create(), llvm::BinaryOperator::CreateNot(), llvm::BasicBlock::front(), llvm::BranchInst::getCondition(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::hasOneUse(), llvm::BranchInst::isUnconditional(), llvm::pred_begin(), llvm::pred_end(), llvm::pred_iterator, SafeToMergeTerminators(), llvm::BranchInst::setCondition(), llvm::Value::setName(), llvm::BranchInst::setSuccessor(), and llvm::Value::takeName().

Referenced by llvm::SimplifyCFG().

bool FoldCondBranchOnPHI BranchInst BI  )  [static]
 

FoldCondBranchOnPHI - If we have a conditional branch on a PHI node value that is defined in the same block as the branch and if any PHI entries are constants, thread edges corresponding to that entry to be branches to their ultimate destination.

Definition at line 1112 of file SimplifyCFG.cpp.

References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), BlockIsSimpleEnoughToThreadThrough(), llvm::ConstantFoldInstruction(), llvm::BranchInst::Create(), llvm::BasicBlock::Create(), llvm::Instruction::eraseFromParent(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::TerminatorInst::getNumSuccessors(), llvm::BasicBlock::getParent(), llvm::Instruction::getParent(), llvm::TerminatorInst::getSuccessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getType(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), llvm::Value::hasOneUse(), llvm::User::op_begin(), llvm::User::op_end(), llvm::User::op_iterator, llvm::BasicBlock::removePredecessor(), llvm::Value::replaceAllUsesWith(), llvm::Value::setName(), and llvm::TerminatorInst::setSuccessor().

Referenced by llvm::SimplifyCFG().

bool FoldTwoEntryPHINode PHINode PN  )  [static]
 

FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry PHI node, see if we can eliminate it.

Definition at line 1214 of file SimplifyCFG.cpp.

References llvm::BasicBlock::begin(), llvm::SelectInst::Create(), DominatesMergePoint(), DOUT, GetIfCondition(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::pred_begin(), llvm::Value::replaceAllUsesWith(), and llvm::Value::takeName().

Referenced by llvm::SimplifyCFG().

bool FoldValueComparisonIntoPredecessors TerminatorInst TI  )  [static]
 

Definition at line 747 of file SimplifyCFG.cpp.

References llvm::SwitchInst::addCase(), AddPredecessorToBlock(), llvm::SmallVectorImpl< T >::back(), llvm::BranchInst::Create(), llvm::BasicBlock::Create(), llvm::SwitchInst::Create(), llvm::SmallVectorImpl< T >::empty(), llvm::SmallVectorImpl< T >::erase(), ErasePossiblyDeadInstructionTree(), llvm::BranchInst::getCondition(), llvm::BasicBlock::getInstList(), llvm::SwitchInst::getNumSuccessors(), llvm::BasicBlock::getParent(), llvm::Instruction::getParent(), llvm::SwitchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), GetValueEqualityComparisonCases(), llvm::SmallVectorImpl< T >::insert(), isValueEqualityComparison(), llvm::SmallVectorImpl< T >::pop_back(), llvm::pred_begin(), llvm::pred_end(), llvm::SmallVectorImpl< T >::push_back(), llvm::BasicBlock::removePredecessor(), SafeToMergeTerminators(), llvm::SwitchInst::setSuccessor(), llvm::BasicBlock::size(), llvm::SmallVectorImpl< T >::size(), and std::swap().

Referenced by llvm::SimplifyCFG().

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

Definition at line 429 of file SimplifyCFG.cpp.

References llvm::Instruction::getOpcode(), and llvm::User::getOperand().

Referenced by GatherValueComparisons().

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

Definition at line 453 of file SimplifyCFG.cpp.

References llvm::Instruction::getOpcode(), and llvm::User::getOperand().

Referenced by GatherValueComparisons().

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 479 of file SimplifyCFG.cpp.

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

Referenced by llvm::SimplifyCFG().

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 266 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().

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

Definition at line 554 of file SimplifyCFG.cpp.

References llvm::SwitchInst::getCaseValue(), llvm::BranchInst::getCondition(), llvm::SwitchInst::getDefaultDest(), llvm::SwitchInst::getNumCases(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::BranchInst::getSuccessor(), and llvm::SwitchInst::getSuccessor().

Referenced by FoldValueComparisonIntoPredecessors(), and SimplifyEqualityComparisonWithOnlyPredecessor().

bool HoistThenElseCodeToIf BranchInst BI  )  [static]
 

HoistThenElseCodeToIf - Given a conditional branch that goes to BB1 and BB2, hoist any common code in the two blocks up into the branch block. The caller of this function guarantees that BI's block dominates BB1 and BB2.

Definition at line 879 of file SimplifyCFG.cpp.

References AddPredecessorToBlock(), llvm::BasicBlock::begin(), llvm::Instruction::clone(), llvm::SelectInst::Create(), llvm::Instruction::eraseFromParent(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getOpcode(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::Value::getType(), llvm::Instruction::isIdenticalTo(), llvm::Value::replaceAllUsesWith(), llvm::PHINode::setIncomingValue(), llvm::succ_begin(), llvm::succ_end(), llvm::succ_iterator, and llvm::Value::takeName().

Referenced by llvm::SimplifyCFG().

Value* isValueEqualityComparison TerminatorInst TI  )  [static]
 

Definition at line 531 of file SimplifyCFG.cpp.

References llvm::BranchInst::getCondition(), llvm::SwitchInst::getCondition(), llvm::SwitchInst::getNumSuccessors(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), llvm::Value::hasOneUse(), llvm::BranchInst::isConditional(), llvm::pred_begin(), and llvm::pred_end().

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

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 llvm::SmallPtrSet< PtrType, SmallSize >::count(), llvm::PHINode::getIncomingValueForBlock(), llvm::Instruction::getParent(), llvm::succ_begin(), llvm::succ_end(), and llvm::succ_iterator.

Referenced by FoldBranchToCommonDest(), and FoldValueComparisonIntoPredecessors().

bool SimplifyCondBranchToCondBranch BranchInst PBI,
BranchInst BI
[static]
 

SimplifyCondBranchToCondBranch - If we have a conditional branch as a predecessor of another block, this function tries to simplify it. We know that PBI and BI are both conditional branches, and BI is in one of the successor blocks of PBI - PBI branches to BI.

Definition at line 1509 of file SimplifyCFG.cpp.

References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), BlockIsSimpleEnoughToThreadThrough(), llvm::SelectInst::Create(), llvm::BranchInst::Create(), llvm::BasicBlock::Create(), llvm::PHINode::Create(), llvm::BinaryOperator::CreateNot(), DOUT, llvm::BasicBlock::front(), llvm::PHINode::getBasicBlockIndex(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getIncomingValueForBlock(), llvm::Value::getName(), llvm::BasicBlock::getParent(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BranchInst::getSuccessor(), llvm::BranchInst::isConditional(), llvm::pred_begin(), llvm::pred_end(), llvm::pred_iterator, llvm::BranchInst::setCondition(), llvm::PHINode::setIncomingValue(), and llvm::BranchInst::setSuccessor().

Referenced by llvm::SimplifyCFG().

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 1332 of file SimplifyCFG.cpp.

References llvm::BasicBlock::begin(), llvm::Constant::canTrap(), llvm::SelectInst::Create(), llvm::ReturnInst::Create(), DOUT, llvm::Instruction::eraseFromParent(), ErasePossiblyDeadInstructionTree(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValueForBlock(), llvm::User::getNumOperands(), llvm::Instruction::getParent(), llvm::ReturnInst::getReturnValue(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isConditional(), and llvm::BasicBlock::removePredecessor().

Referenced by llvm::SimplifyCFG().

bool SimplifyEqualityComparisonWithOnlyPredecessor TerminatorInst TI,
BasicBlock Pred
[static]
 

Definition at line 625 of file SimplifyCFG.cpp.

References llvm::SmallPtrSet< PtrType, SmallSize >::count(), llvm::BranchInst::Create(), DOUT, EliminateBlockCases(), llvm::Instruction::eraseFromParent(), ErasePossiblyDeadInstructionTree(), llvm::SwitchInst::getCaseValue(), llvm::BranchInst::getCondition(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::SwitchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), GetValueEqualityComparisonCases(), llvm::SmallPtrSet< PtrType, SmallSize >::insert(), isValueEqualityComparison(), llvm::SwitchInst::removeCase(), llvm::BasicBlock::removePredecessor(), llvm::succ_begin(), llvm::succ_end(), llvm::succ_iterator, and ValuesOverlap().

Referenced by llvm::SimplifyCFG().

bool SpeculativelyExecuteBB BranchInst BI,
BasicBlock BB1
[static]
 

SpeculativelyExecuteBB - Given a conditional branch that goes to BB1 and an BB2 and the only successor of BB1 is BB2, hoist simple code (for now, restricted to a single instruction that's side effect free) from the BB1 into the branch block to speculatively execute it.

Definition at line 964 of file SimplifyCFG.cpp.

References llvm::BasicBlock::begin(), llvm::SelectInst::Create(), llvm::BasicBlock::end(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::Value::getType(), llvm::Type::isInteger(), llvm::Value::isUsedInBasicBlock(), llvm::User::op_begin(), llvm::User::op_end(), llvm::User::op_iterator, llvm::SmallVectorImpl< T >::push_back(), llvm::PHINode::setIncomingValue(), llvm::SmallVectorImpl< T >::size(), llvm::Value::use_begin(), llvm::Value::use_end(), and llvm::Value::use_iterator.

Referenced by llvm::SimplifyCFG().

STATISTIC NumSpeculations  ,
"Number of speculative executed instructions" 
 

bool TryToSimplifyUncondBranchFromEmptyBlock BasicBlock BB,
BasicBlock Succ
[static]
 

TryToSimplifyUncondBranchFromEmptyBlock - BB contains an unconditional branch to Succ, and contains no instructions other than PHI nodes and the branch. If possible, eliminate BB.

Definition at line 178 of file SimplifyCFG.cpp.

References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), CanPropagatePredecessorsForPHIs(), DOUT, llvm::BasicBlock::eraseFromParent(), llvm::Instruction::eraseFromParent(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::BasicBlock::getInstList(), llvm::PHINode::getNumIncomingValues(), llvm::Value::hasName(), llvm::pred_begin(), llvm::pred_end(), llvm::PHINode::removeIncomingValue(), llvm::Value::replaceAllUsesWith(), llvm::SmallVectorImpl< T >::size(), llvm::Value::takeName(), and llvm::Value::use_empty().

Referenced by llvm::SimplifyCFG().

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

Definition at line 587 of file SimplifyCFG.cpp.

References llvm::BasicBlock::size(), and std::swap().

Referenced by SimplifyEqualityComparisonWithOnlyPredecessor().




This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.