LLVM API Documentation
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/NoFolder.h"
#include "llvm/Support/PatternMatch.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <algorithm>
#include <map>
#include <set>
Go to the source code of this file.
Macros | |
#define | DEBUG_TYPE "simplifycfg" |
Variables | |
static cl::opt< unsigned > | PHINodeFoldingThreshold ("phi-node-folding-threshold", cl::Hidden, cl::init(1), cl::desc("Control the amount of phi node folding to perform (default = 1)")) |
static cl::opt< bool > | DupRet ("simplifycfg-dup-ret", cl::Hidden, cl::init(false), cl::desc("Duplicate return instructions into unconditional branches")) |
static cl::opt< bool > | SinkCommon ("simplifycfg-sink-common", cl::Hidden, cl::init(true), cl::desc("Sink common instructions down to the end block")) |
static cl::opt< bool > | HoistCondStores ("simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores if an unconditional store preceeds")) |
#define DEBUG_TYPE "simplifycfg" |
Definition at line 14 of file SimplifyCFG.cpp.
|
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 189 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), llvm::dyn_cast(), llvm::PHINode::getIncomingValueForBlock(), and I.
Referenced by llvm::FoldBranchToCommonDest(), FoldCondBranchOnPHI(), HoistThenElseCodeToIf(), SimplifyBranchOnICmpChain(), and SimplifyCondBranchToCondBranch().
|
static |
BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch across this block.
Definition at line 1580 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::Instruction::getParent(), and llvm::BasicBlock::getTerminator().
Referenced by FoldCondBranchOnPHI(), and SimplifyCondBranchToCondBranch().
|
static |
checkCSEInPredecessor - Return true if the given instruction is available in its predecessor block. If yes, the instruction will be removed.
Definition at line 1949 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), I, llvm::Instruction::isIdenticalTo(), and llvm::Value::replaceAllUsesWith().
Referenced by llvm::FoldBranchToCommonDest().
ComputeSpeculationCost - Compute an abstract "cost" of speculating the given instruction, which is assumed to be safe to speculate. 1 means cheap, 2 means less cheap, and UINT_MAX means prohibitively expensive.
Definition at line 202 of file SimplifyCFG.cpp.
References llvm::APIntOps::And(), llvm::Call, llvm::Operator::getOpcode(), llvm::isSafeToSpeculativelyExecute(), llvm::SPII::Load, llvm::APIntOps::Or(), llvm::MCID::Select, and llvm::APIntOps::Xor().
Referenced by DominatesMergePoint(), and SpeculativelyExecuteBB().
|
static |
ConstantFold - Try to fold instruction I into a constant. This works for simple instructions such as binary operations where both operands are constant or can be replaced by constants from the ConstantPool. Returns the resulting constant on success, 0 otherwise.
Definition at line 3333 of file SimplifyCFG.cpp.
References llvm::ARM_PROC::A, llvm::ConstantFoldCompareInstOperands(), llvm::ConstantFoldInstOperands(), llvm::ISD::ConstantPool, llvm::User::getNumOperands(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Constant::isAllOnesValue(), llvm::Constant::isNullValue(), LookupConstant(), N, llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), and llvm::MCID::Select.
Referenced by GetCaseResults().
|
static |
Definition at line 707 of file SimplifyCFG.cpp.
References llvm::ConstantInt::getValue(), and llvm::APInt::ult().
Referenced by SimplifyBranchOnICmpChain(), and TurnSwitchRangeIntoICmp().
|
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) and its recursive operands that do not dominate BB have a combined cost lower than CostRemaining and are non-trapping. If both are true, the instruction is inserted into the set and true is returned.
The cost for most non-trapping instructions is defined as 1 except for Select whose cost is 2.
After this function returns, CostRemaining is decreased by the cost of V plus its non-dominating operands. If that cost is greater than CostRemaining, false is returned and CostRemaining is undefined.
Definition at line 252 of file SimplifyCFG.cpp.
References llvm::CallingConv::C, ComputeSpeculationCost(), llvm::SmallPtrSet< PtrType, SmallSize >::count(), llvm::dyn_cast(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), I, llvm::SmallPtrSet< PtrType, SmallSize >::insert(), llvm::BranchInst::isConditional(), llvm::isSafeToSpeculativelyExecute(), llvm::User::op_begin(), and llvm::User::op_end().
Referenced by FoldTwoEntryPHINode().
|
static |
EliminateBlockCases - Given a vector of bb/value pairs, remove any entries in the list that match the specified block.
Definition at line 515 of file SimplifyCFG.cpp.
References llvm::LibFunc::remove.
|
static |
EliminateDeadSwitchCases - Compute masked bits for the condition of a switch and use it to remove dead cases.
Definition at line 3187 of file SimplifyCFG.cpp.
References llvm::SmallVectorTemplateCommon< T >::back(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::tgtok::Bits, llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_default(), llvm::SwitchInst::case_end(), llvm::ComputeMaskedBits(), llvm::dbgs(), DEBUG, llvm::SmallVectorBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::SwitchInst::findCaseValue(), GetBranchWeights(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseIndex(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseSuccessor(), llvm::SwitchInst::getCondition(), llvm::BasicBlock::getContext(), llvm::Type::getIntegerBitWidth(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::Value::getType(), HasBranchWeights(), I, llvm::LLVMContext::MD_prof, llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::pop_back(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::SwitchInst::removeCase(), llvm::BasicBlock::removePredecessor(), llvm::Instruction::setMetadata(), llvm::SmallVectorTemplateCommon< T >::size(), and std::swap().
|
static |
Definition at line 447 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), and llvm::RecursivelyDeleteTriviallyDeadInstructions().
Referenced by llvm::FoldBranchToCommonDest(), HoistThenElseCodeToIf(), SimplifyBranchOnICmpChain(), SimplifyCondBranchToTwoReturns(), and SimplifyTerminatorOnSelect().
|
static |
ExtractBranchMetadata - Given a conditional BranchInstruction, retrieve the probabilities of the branch taking each edge. Fills in the two APInt parameters and return true, or returns false if no or invalid metadata was found.
Definition at line 1932 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::Instruction::getMetadata(), llvm::ConstantInt::getValue(), llvm::APInt::getZExtValue(), llvm::BranchInst::isConditional(), and llvm::LLVMContext::MD_prof.
Referenced by llvm::FoldBranchToCommonDest(), and SimplifyCondBranchToCondBranch().
|
static |
FindPHIForConditionForwarding - If BB would be eligible for simplification by TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated by an unconditional branch), look at the phi node for BB in the successor block and see if the incoming value is equal to CaseValue. If so, return the phi node, and set PhiIndex to BB's index in the phi node.
Definition at line 3240 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::MCID::Branch, llvm::dyn_cast(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::BasicBlock::getSinglePredecessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isUnconditional(), and llvm::TargetOpcode::PHI.
Referenced by ForwardSwitchConditionToPHI().
|
static |
Sees if any of the weights are too big for a uint32_t, and halves all the weights if any are.
Definition at line 753 of file SimplifyCFG.cpp.
References llvm::ArrayRef< T >::size().
Referenced by llvm::FoldBranchToCommonDest(), and SimplifyCondBranchToCondBranch().
|
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 1608 of file SimplifyCFG.cpp.
References AddPredecessorToBlock(), llvm::BasicBlock::begin(), BlockIsSimpleEnoughToThreadThrough(), llvm::BasicBlock::Create(), llvm::BranchInst::Create(), llvm::dyn_cast(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT >, KeyT, ValueT, KeyInfoT >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT >, KeyT, ValueT, KeyInfoT >::find(), llvm::FoldSingleEntryPHINodes(), llvm::BranchInst::getCondition(), llvm::BasicBlock::getContext(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::TerminatorInst::getNumSuccessors(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::TerminatorInst::getSuccessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getType(), llvm::ConstantInt::getZExtValue(), HasNoDuplicateCall(), llvm::Value::hasOneUse(), llvm::iplist< NodeTy, Traits >::insert(), llvm::Type::isIntegerTy(), N, llvm::User::op_begin(), llvm::User::op_end(), llvm::BasicBlock::removePredecessor(), llvm::Value::setName(), llvm::TerminatorInst::setSuccessor(), and llvm::SimplifyInstruction().
|
static |
FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry PHI node, see if we can eliminate it.
Definition at line 1706 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::SmallPtrSet< PtrType, SmallSize >::count(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSelect(), llvm::dbgs(), DEBUG, DominatesMergePoint(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::GetIfCondition(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), I, llvm::Type::isIntegerTy(), llvm::A64CC::NV, PHINodeFoldingThreshold, llvm::pred_begin(), llvm::Value::replaceAllUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), llvm::SimplifyInstruction(), llvm::iplist< NodeTy, Traits >::splice(), and llvm::Value::takeName().
|
static |
ForwardSwitchConditionToPHI - Try to forward the condition of a switch instruction to a phi node dominated by the switch, if that would mean that some of the destination blocks of the switch can be folded away. Returns true if a change is made.
Definition at line 3273 of file SimplifyCFG.cpp.
References llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), FindPHIForConditionForwarding(), llvm::SwitchInst::getCondition(), I, llvm::TargetOpcode::PHI, llvm::PHINode::setIncomingValue(), and llvm::SmallVectorTemplateCommon< T, typename >::size().
|
static |
GatherConstantCompares - Given a potentially 'or'd or 'and'd together collection of icmp eq/ne instructions that compare a value against a constant, return the value being compared, and stick the constant into the Values vector.
Definition at line 342 of file SimplifyCFG.cpp.
References llvm::APIntOps::And(), llvm::CallingConv::C, llvm::dyn_cast(), llvm::ConstantInt::get(), GetConstantInt(), llvm::Value::getContext(), llvm::ConstantRange::getLower(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::ConstantRange::getSetSize(), llvm::ConstantRange::getUpper(), llvm::ConstantInt::getValue(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::ConstantRange::inverse(), llvm::ConstantRange::isEmptySet(), llvm::APInt::isPowerOf2(), llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Value(), llvm::ConstantRange::makeICmpRegion(), llvm::PatternMatch::match(), llvm::APIntOps::Not(), llvm::APIntOps::Or(), llvm::ConstantRange::subtract(), TD, and llvm::APInt::ugt().
Referenced by SimplifyBranchOnICmpChain().
|
static |
Get Weights of a given TerminatorInst, the default weight is at the front of the vector. If TI is a conditional eq, we need to swap the branch-weight metadata.
Definition at line 730 of file SimplifyCFG.cpp.
References llvm::SmallVectorTemplateCommon< T >::back(), llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T >::front(), llvm::BranchInst::getCondition(), llvm::Instruction::getMetadata(), llvm::MDNode::getNumOperands(), llvm::MDNode::getOperand(), llvm::ConstantInt::getValue(), llvm::APInt::getZExtValue(), llvm::CmpInst::ICMP_EQ, llvm::LLVMContext::MD_prof, llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::SmallVectorTemplateCommon< T >::size(), and std::swap().
Referenced by EliminateDeadSwitchCases(), SimplifySwitchOnSelect(), TryToSimplifyUncondBranchWithICmpInIt(), and TurnSwitchRangeIntoICmp().
|
static |
GetCaseResults - Try to determine the resulting constant values in phi nodes at the common destination basic block, *CommonDest, for one of the case destionations CaseDest corresponding to value CaseVal (0 for the default case), of a switch instruction SI.
Definition at line 3367 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::CallingConv::C, ConstantFold(), llvm::ISD::ConstantPool, llvm::BasicBlock::end(), llvm::SwitchInst::getCondition(), llvm::Instruction::getParent(), I, llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT >, KeyT, ValueT, KeyInfoT >::insert(), LookupConstant(), llvm::TargetOpcode::PHI, and ValidLookupTableConstant().
Referenced by SwitchToLookupTable().
|
static |
GetConstantInt - Extract ConstantInt from value, looking through IntToPtr and PointerNullValue. Return NULL if value is not a constant int.
Definition at line 309 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::ConstantInt::get(), llvm::ConstantExpr::getIntegerCast(), llvm::DataLayout::getIntPtrType(), llvm::Value::getType(), llvm::ConstantInt::getType(), and llvm::Type::isPointerTy().
Referenced by GatherConstantCompares().
|
inlinestatic |
Definition at line 718 of file SimplifyCFG.cpp.
References llvm::Instruction::getMetadata(), llvm::MDNode::getOperand(), and llvm::LLVMContext::MD_prof.
Referenced by EliminateDeadSwitchCases(), SimplifySwitchOnSelect(), TryToSimplifyUncondBranchWithICmpInIt(), and TurnSwitchRangeIntoICmp().
|
static |
Definition at line 1567 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::CallInst::cannotDuplicate(), llvm::dyn_cast(), llvm::BasicBlock::end(), and I.
Referenced by FoldCondBranchOnPHI().
|
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 1002 of file SimplifyCFG.cpp.
References AddPredecessorToBlock(), llvm::BasicBlock::begin(), llvm::Instruction::clone(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSelect(), llvm::dyn_cast(), EraseTerminatorInstAndDCECond(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::Value::getType(), llvm::iplist< NodeTy, Traits >::insert(), llvm::Value::intersectOptionalDataWith(), llvm::Instruction::isIdenticalToWhenDefined(), isSafeToHoistInvoke(), llvm::isSafeToSpeculativelyExecute(), llvm::Type::isVoidTy(), llvm::Value::replaceAllUsesWith(), llvm::PHINode::setIncomingValue(), llvm::iplist< NodeTy, Traits >::splice(), llvm::succ_begin(), llvm::succ_end(), and llvm::Value::takeName().
|
static |
isProfitableToFoldUnconditional - Return true if it is safe and profitable to merge these two terminator instructions together, where SI1 is an unconditional branch. PhiNodes will store all PHI nodes in common successors.
Definition at line 149 of file SimplifyCFG.cpp.
References llvm::SmallPtrSet< PtrType, SmallSize >::count(), llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValueForBlock(), llvm::User::getOperand(), llvm::Instruction::getParent(), I, llvm::BranchInst::isConditional(), llvm::BranchInst::isUnconditional(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::succ_begin(), and llvm::succ_end().
Referenced by llvm::FoldBranchToCommonDest().
|
static |
Definition at line 983 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::PHINode::getIncomingValueForBlock(), llvm::succ_begin(), and llvm::succ_end().
Referenced by HoistThenElseCodeToIf().
|
static |
Determine if we can hoist sink a sole store instruction out of a conditional block.
We are looking for code like the following: BrBB: store i32 add, i32* arrayidx2 ... // No other stores or function calls (we could be calling a memory ... // function). cmp = icmp ult x, y br i1 cmp, label EndBB, label ThenBB ThenBB: store i32 add5, i32* arrayidx2 br label EndBB EndBB: ... We are going to transform this into: BrBB: store i32 add, i32* arrayidx2 ... // cmp = icmp ult x, y add.add5 = select i1 cmp, i32 add, add5 store i32 add.add5, i32* arrayidx2 ...
Definition at line 1324 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::StoreInst::getPointerOperand(), llvm::StoreInst::getValueOperand(), I, llvm::StoreInst::isSimple(), llvm::Instruction::mayHaveSideEffects(), llvm::BasicBlock::rbegin(), and llvm::BasicBlock::rend().
Referenced by SpeculativelyExecuteBB().
|
static |
LookupConstant - If V is a Constant, return it. Otherwise, try to look up its constant value in ConstantPool, returning 0 if it's not there.
Definition at line 3321 of file SimplifyCFG.cpp.
References llvm::CallingConv::C, and llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT >, KeyT, ValueT, KeyInfoT >::lookup().
Referenced by ConstantFold(), and GetCaseResults().
|
static |
Check if passing a value to an instruction will cause undefined behavior.
Definition at line 4016 of file SimplifyCFG.cpp.
References llvm::CallingConv::C, llvm::dyn_cast(), llvm::BasicBlock::end(), llvm::Instruction::getParent(), I, llvm::Constant::isNullValue(), LI, llvm::Value::use_begin(), and llvm::Value::use_empty().
Referenced by removeUndefIntroducingPredecessor().
|
static |
If BB has an incoming value that will always trigger undefined behavior (eg. null pointer dereference), remove the branch leading here.
Definition at line 4058 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateUnreachable(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isUnconditional(), passingValueIsAlwaysUndefined(), llvm::TargetOpcode::PHI, and llvm::BasicBlock::removePredecessor().
|
static |
SafeToMergeTerminators - Return true if it is safe to merge these two terminator instructions together.
Definition at line 121 of file SimplifyCFG.cpp.
References llvm::SmallPtrSet< PtrType, SmallSize >::count(), llvm::PHINode::getIncomingValueForBlock(), llvm::Instruction::getParent(), I, llvm::succ_begin(), and llvm::succ_end().
Referenced by llvm::FoldBranchToCommonDest().
|
static |
ShouldBuildLookupTable - Determine whether a lookup table should be built for this switch, based on the number of cases, size of the table and the types of the results.
Definition at line 3621 of file SimplifyCFG.cpp.
References llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT >, KeyT, ValueT, KeyInfoT >::begin(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT >, KeyT, ValueT, KeyInfoT >::end(), llvm::SwitchInst::getNumCases(), I, and llvm::TargetTransformInfo::isTypeLegal().
Referenced by SwitchToLookupTable().
|
static |
SimplifyBranchOnICmpChain - The specified branch is a conditional branch. Check to see if it is branching on an or/and chain of icmp instructions, and fold it into a switch instruction if so.
Definition at line 2733 of file SimplifyCFG.cpp.
References llvm::SwitchInst::addCase(), llvm::PHINode::addIncoming(), AddPredecessorToBlock(), llvm::APIntOps::And(), llvm::array_pod_sort(), llvm::BasicBlock::begin(), ConstantIntSortPredicate(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateCondBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreatePtrToInt(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSwitch(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), EraseTerminatorInstAndDCECond(), GatherConstantCompares(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValueForBlock(), llvm::DataLayout::getIntPtrType(), llvm::Instruction::getOpcode(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::Type::isPointerTy(), llvm::APIntOps::Or(), llvm::IRBuilderBase::SetInsertPoint(), llvm::BasicBlock::size(), llvm::BasicBlock::splitBasicBlock(), and std::swap().
|
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 2290 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), AddPredecessorToBlock(), llvm::StringRef::begin(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::BasicBlock::begin(), BlockIsSimpleEnoughToThreadThrough(), llvm::BasicBlock::Create(), llvm::PHINode::Create(), llvm::BranchInst::Create(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateNot(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateOr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSelect(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T >::end(), ExtractBranchMetadata(), FitWeights(), llvm::ConstantInt::get(), llvm::BranchInst::getCondition(), llvm::Value::getContext(), llvm::PHINode::getIncomingValueForBlock(), llvm::Type::getInt1Ty(), llvm::Value::getName(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isConditional(), llvm::LLVMContext::MD_prof, llvm::A64CC::NV, P, llvm::pred_begin(), llvm::pred_end(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::BranchInst::setCondition(), llvm::Instruction::setMetadata(), and llvm::BranchInst::setSuccessor().
|
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 1843 of file SimplifyCFG.cpp.
References llvm::IRBuilder< preserveNames, T, Inserter >::CreateRet(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateRetVoid(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSelect(), llvm::dbgs(), DEBUG, EraseTerminatorInstAndDCECond(), llvm::BranchInst::getCondition(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::Instruction::getParent(), llvm::ReturnInst::getReturnValue(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::BranchInst::isConditional(), llvm::Instruction::isTerminator(), llvm::BasicBlock::removePredecessor(), and llvm::IRBuilderBase::SetInsertPoint().
|
static |
Definition at line 2593 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::BlockAddress::getBasicBlock(), llvm::SelectInst::getCondition(), llvm::SelectInst::getFalseValue(), llvm::SelectInst::getTrueValue(), and SimplifyTerminatorOnSelect().
|
static |
Definition at line 2557 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::SwitchInst::findCaseValue(), GetBranchWeights(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseSuccessor(), llvm::SelectInst::getCondition(), llvm::SelectInst::getFalseValue(), llvm::SwitchInst::getNumCases(), llvm::SelectInst::getTrueValue(), HasBranchWeights(), SimplifyTerminatorOnSelect(), and llvm::SmallVectorTemplateCommon< T >::size().
|
static |
Definition at line 2492 of file SimplifyCFG.cpp.
References llvm::IRBuilder< preserveNames, T, Inserter >::CreateBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateCondBr(), EraseTerminatorInstAndDCECond(), llvm::Value::getContext(), llvm::Instruction::getDebugLoc(), llvm::TerminatorInst::getNumSuccessors(), llvm::Instruction::getParent(), llvm::TerminatorInst::getSuccessor(), I, llvm::LLVMContext::MD_prof, llvm::BasicBlock::removePredecessor(), llvm::IRBuilderBase::SetCurrentDebugLocation(), and llvm::Instruction::setMetadata().
Referenced by SimplifyIndirectBrOnSelect(), and SimplifySwitchOnSelect().
|
static |
SinkThenElseCodeToEnd - Given an unconditional branch that goes to BBEnd, check whether BBEnd has only two predecessors and the other predecessor ends with an unconditional branch. If it is true, sink any common code in the two predecessors to BBEnd.
Definition at line 1133 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), llvm::PHINode::Create(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), llvm::Value::hasOneUse(), I, llvm::Value::intersectOptionalDataWith(), llvm::Instruction::isSameOperationAs(), llvm::BranchInst::isUnconditional(), llvm::Instruction::mayHaveSideEffects(), llvm::Instruction::mayReadOrWriteMemory(), llvm::pred_begin(), llvm::pred_end(), llvm::iplist< NodeTy, Traits >::rbegin(), llvm::iplist< NodeTy, Traits >::rend(), llvm::Value::replaceAllUsesWith(), llvm::User::setOperand(), llvm::iplist< NodeTy, Traits >::splice(), and llvm::Value::use_empty().
|
static |
Speculate a conditional basic block flattening the CFG.
Note that this is a very risky transform currently. Speculating instructions like this is most often not desirable. Instead, there is an MI pass which can do it with full awareness of the resource constraints. However, some cases are "obvious" and we should do directly. An example of this is speculating a single, reasonably cheap instruction.
There is only one distinct advantage to flattening the CFG at the IR level: it makes very common but simplistic optimizations such as are common in instcombine and the DAG combiner more powerful by removing CFG edges and modeling their effects with easier to reason about SSA value graphs.
An illustration of this transform is turning this IR:
Into this IR:
Definition at line 1395 of file SimplifyCFG.cpp.
References llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT >::begin(), llvm::BasicBlock::begin(), ComputeSpeculationCost(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSelect(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::BasicBlock::end(), llvm::PHINode::getBasicBlockIndex(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::Value::getName(), llvm::Instruction::getParent(), llvm::TerminatorInst::getSuccessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::StoreInst::getValueOperand(), HoistCondStores, I, isSafeToSpeculateStore(), llvm::isSafeToSpeculativelyExecute(), llvm::Instruction::mayHaveSideEffects(), llvm::User::op_begin(), llvm::User::op_end(), PHINodeFoldingThreshold, llvm::prior(), llvm::PHINode::setIncomingValue(), llvm::User::setOperand(), llvm::iplist< NodeTy, Traits >::splice(), and std::swap().
STATISTIC | ( | NumBitMaps | , |
"Number of switch instructions turned into bitmaps" | |||
) |
STATISTIC | ( | NumLookupTables | , |
"Number of switch instructions turned into lookup tables" | |||
) |
STATISTIC | ( | NumSinkCommons | , |
"Number of common instructions sunk down to the end block" | |||
) |
STATISTIC | ( | NumSpeculations | , |
"Number of speculative executed instructions" | |||
) |
|
static |
SwitchToLookupTable - If the switch is only used to initialize one or more phi nodes in a common successor block with different constant values, replace the switch with lookup tables.
Definition at line 3666 of file SimplifyCFG.cpp.
References llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT >, KeyT, ValueT, KeyInfoT >::count(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateCondBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateICmpULT(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateRet(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateSub(), llvm::Instruction::eraseFromParent(), GetCaseResults(), llvm::SwitchInst::getCondition(), llvm::Module::getContext(), llvm::SwitchInst::getDefaultDest(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::APInt::getLimitedValue(), llvm::SwitchInst::getNumCases(), llvm::SwitchInst::getNumSuccessors(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::GlobalValue::getParent(), llvm::Type::getPrimitiveSizeInBits(), llvm::SwitchInst::getSuccessor(), llvm::Value::getType(), llvm::ConstantInt::getType(), llvm::ConstantInt::getValue(), I, llvm::TargetOpcode::PHI, llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::BasicBlock::removePredecessor(), llvm::IRBuilderBase::SetInsertPoint(), llvm::APInt::sgt(), ShouldBuildLookupTable(), llvm::TargetTransformInfo::shouldBuildLookupTables(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::APInt::slt(), and TD.
|
static |
TryToSimplifyUncondBranchWithICmpInIt - This is called when we find an icmp instruction (a seteq/setne with a constant) as the only instruction in a block that ends with an uncond branch. We are looking for a very specific pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In this case, we merge the first two "or's of icmp" into a switch, but then the default value goes to an uncond block with a seteq in it, we get something like:
switch i8 A, label DEFAULT [ i8 1, label end i8 2, label end ] DEFAULT: tmp = icmp eq i8 A, 92 br label end end: ... = phi i1 [ true, entry ], [ tmp, DEFAULT ], [ true, entry ]
We prefer to split the edge to 'end' so that there is a true/false entry to the PHI, merging the third icmp into the switch.
Definition at line 2626 of file SimplifyCFG.cpp.
References llvm::SwitchInst::addCase(), llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), llvm::SwitchInst::case_default(), llvm::BasicBlock::Create(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateBr(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::SwitchInst::findCaseDest(), llvm::SwitchInst::findCaseValue(), llvm::BasicBlock::front(), GetBranchWeights(), llvm::SwitchInst::getCondition(), llvm::BasicBlock::getContext(), llvm::Value::getContext(), llvm::Instruction::getDebugLoc(), llvm::SwitchInst::getDefaultDest(), llvm::ConstantInt::getFalse(), llvm::SwitchInst::getNumCases(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::CmpInst::getPredicate(), llvm::BasicBlock::getSinglePredecessor(), llvm::TerminatorInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getTrue(), HasBranchWeights(), llvm::Value::hasOneUse(), llvm::CmpInst::ICMP_EQ, llvm::LLVMContext::MD_prof, llvm::Value::replaceAllUsesWith(), llvm::IRBuilderBase::SetCurrentDebugLocation(), llvm::IRBuilderBase::SetInsertPoint(), llvm::Instruction::setMetadata(), llvm::User::setOperand(), llvm::SimplifyCFG(), llvm::SimplifyInstruction(), std::swap(), and llvm::Instruction::use_back().
|
static |
TurnSwitchRangeIntoICmp - Turns a switch with that contains only a integer range comparison into a sub, an icmp and a branch.
Definition at line 3118 of file SimplifyCFG.cpp.
References llvm::array_pod_sort(), llvm::BasicBlock::begin(), llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), ConstantIntSortPredicate(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateAdd(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateCondBr(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateICmpULT(), llvm::Instruction::eraseFromParent(), llvm::ConstantInt::get(), GetBranchWeights(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseSuccessor(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseValue(), llvm::SwitchInst::getCondition(), llvm::Value::getContext(), llvm::SwitchInst::getDefaultDest(), llvm::Value::getName(), llvm::ConstantExpr::getNeg(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::ConstantInt::getTrue(), llvm::Value::getType(), HasBranchWeights(), I, llvm::Constant::isNullValue(), llvm::LLVMContext::MD_prof, llvm::Instruction::setMetadata(), and llvm::SmallVectorTemplateCommon< T >::size().
ValidLookupTableConstant - Return true if the backend will be able to handle initializing an array of constants like C.
Definition at line 3308 of file SimplifyCFG.cpp.
References llvm::CallingConv::C.
Referenced by GetCaseResults().
|
static |
ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as well.
Definition at line 523 of file SimplifyCFG.cpp.
References llvm::array_pod_sort(), std::swap(), llvm::NVPTX::PTXLdStInstCode::V2, and llvm::Value::Value().
|
static |
|
static |
Referenced by SpeculativelyExecuteBB().
|
static |
Referenced by FoldTwoEntryPHINode(), and SpeculativelyExecuteBB().