|
LLVM
4.0.0
|
#include "llvm/ADT/APInt.h"#include "llvm/ADT/ArrayRef.h"#include "llvm/ADT/DenseMap.h"#include "llvm/ADT/Optional.h"#include "llvm/ADT/SetOperations.h"#include "llvm/ADT/SetVector.h"#include "llvm/ADT/SmallPtrSet.h"#include "llvm/ADT/SmallSet.h"#include "llvm/ADT/SmallVector.h"#include "llvm/ADT/Statistic.h"#include "llvm/ADT/STLExtras.h"#include "llvm/Analysis/ConstantFolding.h"#include "llvm/Analysis/EHPersonalities.h"#include "llvm/Analysis/InstructionSimplify.h"#include "llvm/Analysis/TargetTransformInfo.h"#include "llvm/Analysis/ValueTracking.h"#include "llvm/IR/BasicBlock.h"#include "llvm/IR/CallSite.h"#include "llvm/IR/CFG.h"#include "llvm/IR/Constant.h"#include "llvm/IR/ConstantRange.h"#include "llvm/IR/Constants.h"#include "llvm/IR/DataLayout.h"#include "llvm/IR/DebugInfo.h"#include "llvm/IR/DerivedTypes.h"#include "llvm/IR/GlobalValue.h"#include "llvm/IR/GlobalVariable.h"#include "llvm/IR/IRBuilder.h"#include "llvm/IR/InstrTypes.h"#include "llvm/IR/Instruction.h"#include "llvm/IR/Instructions.h"#include "llvm/IR/IntrinsicInst.h"#include "llvm/IR/Intrinsics.h"#include "llvm/IR/LLVMContext.h"#include "llvm/IR/MDBuilder.h"#include "llvm/IR/Metadata.h"#include "llvm/IR/Module.h"#include "llvm/IR/NoFolder.h"#include "llvm/IR/Operator.h"#include "llvm/IR/PatternMatch.h"#include "llvm/IR/Type.h"#include "llvm/IR/User.h"#include "llvm/IR/Value.h"#include "llvm/Support/Casting.h"#include "llvm/Support/CommandLine.h"#include "llvm/Support/Debug.h"#include "llvm/Support/ErrorHandling.h"#include "llvm/Support/MathExtras.h"#include "llvm/Support/raw_ostream.h"#include "llvm/Transforms/Utils/BasicBlockUtils.h"#include "llvm/Transforms/Utils/Local.h"#include "llvm/Transforms/Utils/ValueMapper.h"#include <algorithm>#include <cassert>#include <climits>#include <cstddef>#include <cstdint>#include <iterator>#include <map>#include <set>#include <utility>#include <vector>Go to the source code of this file.
Macros | |
| #define | DEBUG_TYPE "simplifycfg" |
Functions | |
| STATISTIC (NumBitMaps,"Number of switch instructions turned into bitmaps") | |
| STATISTIC (NumLinearMaps,"Number of switch instructions turned into linear mapping") | |
| STATISTIC (NumLookupTables,"Number of switch instructions turned into lookup tables") | |
| STATISTIC (NumLookupTablesHoles,"Number of switch instructions turned into lookup tables (holes checked)") | |
| STATISTIC (NumTableCmpReuses,"Number of reused switch table lookup compares") | |
| STATISTIC (NumSinkCommons,"Number of common instructions sunk down to the end block") | |
| STATISTIC (NumSpeculations,"Number of speculative executed instructions") | |
| static bool | SafeToMergeTerminators (TerminatorInst *SI1, TerminatorInst *SI2, SmallSetVector< BasicBlock *, 4 > *FailBlocks=nullptr) |
| Return true if it is safe to merge these two terminator instructions together. More... | |
| static bool | isProfitableToFoldUnconditional (BranchInst *SI1, BranchInst *SI2, Instruction *Cond, SmallVectorImpl< PHINode * > &PhiNodes) |
| Return true if it is safe and profitable to merge these two terminator instructions together, where SI1 is an unconditional branch. More... | |
| static void | AddPredecessorToBlock (BasicBlock *Succ, BasicBlock *NewPred, BasicBlock *ExistPred) |
| Update PHI nodes in Succ to indicate that there will now be entries in it from the 'NewPred' block. More... | |
| static unsigned | ComputeSpeculationCost (const User *I, const TargetTransformInfo &TTI) |
| Compute an abstract "cost" of speculating the given instruction, which is assumed to be safe to speculate. More... | |
| static bool | DominatesMergePoint (Value *V, BasicBlock *BB, SmallPtrSetImpl< Instruction * > *AggressiveInsts, unsigned &CostRemaining, const TargetTransformInfo &TTI, unsigned Depth=0) |
| If we have a merge point of an "if condition" as accepted above, return true if the specified value dominates the block. More... | |
| static ConstantInt * | GetConstantInt (Value *V, const DataLayout &DL) |
| Extract ConstantInt from value, looking through IntToPtr and PointerNullValue. More... | |
| static void | EraseTerminatorInstAndDCECond (TerminatorInst *TI) |
| static void | EliminateBlockCases (BasicBlock *BB, std::vector< ValueEqualityComparisonCase > &Cases) |
| Given a vector of bb/value pairs, remove any entries in the list that match the specified block. More... | |
| static bool | ValuesOverlap (std::vector< ValueEqualityComparisonCase > &C1, std::vector< ValueEqualityComparisonCase > &C2) |
| Return true if there are any keys in C1 that exist in C2 as well. More... | |
| static int | ConstantIntSortPredicate (ConstantInt *const *P1, ConstantInt *const *P2) |
| static bool | HasBranchWeights (const Instruction *I) |
| static void | GetBranchWeights (TerminatorInst *TI, SmallVectorImpl< uint64_t > &Weights) |
| Get Weights of a given TerminatorInst, the default weight is at the front of the vector. More... | |
| static void | FitWeights (MutableArrayRef< uint64_t > Weights) |
| Keep halving the weights until all can fit in uint32_t. More... | |
| static bool | isSafeToHoistInvoke (BasicBlock *BB1, BasicBlock *BB2, Instruction *I1, Instruction *I2) |
| static bool | passingValueIsAlwaysUndefined (Value *V, Instruction *I) |
| Check if passing a value to an instruction will cause undefined behavior. More... | |
| static bool | HoistThenElseCodeToIf (BranchInst *BI, const TargetTransformInfo &TTI) |
| Given a conditional branch that goes to BB1 and BB2, hoist any common code in the two blocks up into the branch block. More... | |
| static bool | canReplaceOperandWithVariable (const Instruction *I, unsigned OpIdx) |
| static bool | canSinkInstructions (ArrayRef< Instruction * > Insts, DenseMap< Instruction *, SmallVector< Value *, 4 >> &PHIOperands) |
| static bool | sinkLastInstruction (ArrayRef< BasicBlock * > Blocks) |
| static bool | SinkThenElseCodeToEnd (BranchInst *BI1) |
| Given an unconditional branch that goes to BBEnd, check whether BBEnd has only two predecessors and the other predecessor ends with an unconditional branch. More... | |
| static Value * | isSafeToSpeculateStore (Instruction *I, BasicBlock *BrBB, BasicBlock *StoreBB, BasicBlock *EndBB) |
| Determine if we can hoist sink a sole store instruction out of a conditional block. More... | |
| static bool | SpeculativelyExecuteBB (BranchInst *BI, BasicBlock *ThenBB, const TargetTransformInfo &TTI) |
| Speculate a conditional basic block flattening the CFG. More... | |
| static bool | BlockIsSimpleEnoughToThreadThrough (BasicBlock *BB) |
| Return true if we can thread a branch across this block. More... | |
| static bool | FoldCondBranchOnPHI (BranchInst *BI, const DataLayout &DL) |
| 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. More... | |
| static bool | FoldTwoEntryPHINode (PHINode *PN, const TargetTransformInfo &TTI, const DataLayout &DL) |
| Given a BB that starts with the specified two-entry PHI node, see if we can eliminate it. More... | |
| static bool | SimplifyCondBranchToTwoReturns (BranchInst *BI, IRBuilder<> &Builder) |
| 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. More... | |
| static bool | checkCSEInPredecessor (Instruction *Inst, BasicBlock *PB) |
| Return true if the given instruction is available in its predecessor block. More... | |
| static bool | extractPredSuccWeights (BranchInst *PBI, BranchInst *BI, uint64_t &PredTrueWeight, uint64_t &PredFalseWeight, uint64_t &SuccTrueWeight, uint64_t &SuccFalseWeight) |
| Return true if either PBI or BI has branch weight available, and store the weights in {Pred|Succ}{True|False}Weight. More... | |
| static StoreInst * | findUniqueStoreInBlocks (BasicBlock *BB1, BasicBlock *BB2) |
| static Value * | ensureValueAvailableInSuccessor (Value *V, BasicBlock *BB, Value *AlternativeV=nullptr) |
| static bool | mergeConditionalStoreToAddress (BasicBlock *PTB, BasicBlock *PFB, BasicBlock *QTB, BasicBlock *QFB, BasicBlock *PostBB, Value *Address, bool InvertPCond, bool InvertQCond) |
| static bool | mergeConditionalStores (BranchInst *PBI, BranchInst *QBI) |
| static bool | SimplifyCondBranchToCondBranch (BranchInst *PBI, BranchInst *BI, const DataLayout &DL) |
| If we have a conditional branch as a predecessor of another block, this function tries to simplify it. More... | |
| static bool | SimplifyTerminatorOnSelect (TerminatorInst *OldTerm, Value *Cond, BasicBlock *TrueBB, BasicBlock *FalseBB, uint32_t TrueWeight, uint32_t FalseWeight) |
| static bool | SimplifySwitchOnSelect (SwitchInst *SI, SelectInst *Select) |
| static bool | SimplifyIndirectBrOnSelect (IndirectBrInst *IBI, SelectInst *SI) |
| static bool | TryToSimplifyUncondBranchWithICmpInIt (ICmpInst *ICI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI, unsigned BonusInstThreshold, AssumptionCache *AC) |
| 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. More... | |
| static bool | SimplifyBranchOnICmpChain (BranchInst *BI, IRBuilder<> &Builder, const DataLayout &DL) |
| The specified branch is a conditional branch. More... | |
| static bool | removeEmptyCleanup (CleanupReturnInst *RI) |
| static bool | mergeCleanupPad (CleanupReturnInst *RI) |
| static bool | CasesAreContiguous (SmallVectorImpl< ConstantInt * > &Cases) |
| static bool | TurnSwitchRangeIntoICmp (SwitchInst *SI, IRBuilder<> &Builder) |
| Turn a switch with two reachable destinations into an integer range comparison and branch. More... | |
| static bool | EliminateDeadSwitchCases (SwitchInst *SI, AssumptionCache *AC, const DataLayout &DL) |
| Compute masked bits for the condition of a switch and use it to remove dead cases. More... | |
| static PHINode * | FindPHIForConditionForwarding (ConstantInt *CaseValue, BasicBlock *BB, int *PhiIndex) |
| If BB would be eligible for simplification by TryToSimplifyUncondBranchFromEmptyBlock (i.e. More... | |
| static bool | ForwardSwitchConditionToPHI (SwitchInst *SI) |
| 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. More... | |
| static bool | ValidLookupTableConstant (Constant *C, const TargetTransformInfo &TTI) |
| Return true if the backend will be able to handle initializing an array of constants like C. More... | |
| static Constant * | LookupConstant (Value *V, const SmallDenseMap< Value *, Constant * > &ConstantPool) |
| If V is a Constant, return it. More... | |
| static Constant * | ConstantFold (Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool) |
| Try to fold instruction I into a constant. More... | |
| static bool | GetCaseResults (SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest, BasicBlock **CommonDest, SmallVectorImpl< std::pair< PHINode *, Constant * >> &Res, const DataLayout &DL, const TargetTransformInfo &TTI) |
| 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. More... | |
| static void | MapCaseToResult (ConstantInt *CaseVal, SwitchCaseResultVectorTy &UniqueResults, Constant *Result) |
| static bool | InitializeUniqueCases (SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest, SwitchCaseResultVectorTy &UniqueResults, Constant *&DefaultResult, const DataLayout &DL, const TargetTransformInfo &TTI) |
| static Value * | ConvertTwoCaseSwitch (const SwitchCaseResultVectorTy &ResultVector, Constant *DefaultResult, Value *Condition, IRBuilder<> &Builder) |
| static void | RemoveSwitchAfterSelectConversion (SwitchInst *SI, PHINode *PHI, Value *SelectValue, IRBuilder<> &Builder) |
| static bool | SwitchToSelect (SwitchInst *SI, IRBuilder<> &Builder, AssumptionCache *AC, const DataLayout &DL, const TargetTransformInfo &TTI) |
| If the switch is only used to initialize one or more phi nodes in a common successor block with only two different constant values, replace the switch with select. More... | |
| static bool | ShouldBuildLookupTable (SwitchInst *SI, uint64_t TableSize, const TargetTransformInfo &TTI, const DataLayout &DL, const SmallDenseMap< PHINode *, Type * > &ResultTypes) |
| 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. More... | |
| static void | reuseTableCompare (User *PhiUser, BasicBlock *PhiBlock, BranchInst *RangeCheckBranch, Constant *DefaultValue, const SmallVectorImpl< std::pair< ConstantInt *, Constant * >> &Values) |
| Try to reuse the switch table index compare. More... | |
| static bool | SwitchToLookupTable (SwitchInst *SI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI) |
| 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. More... | |
| static bool | isSwitchDense (ArrayRef< int64_t > Values) |
| static bool | ReduceSwitchRange (SwitchInst *SI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI) |
| static bool | TryToMergeLandingPad (LandingPadInst *LPad, BranchInst *BI, BasicBlock *BB) |
| Given an block with only a single landing pad and a unconditional branch try to find another basic block which this one can be merged with. More... | |
| static BasicBlock * | allPredecessorsComeFromSameSource (BasicBlock *BB) |
| static bool | removeUndefIntroducingPredecessor (BasicBlock *BB) |
| If BB has an incoming value that will always trigger undefined behavior (eg. More... | |
Variables | |
| static cl::opt< unsigned > | PHINodeFoldingThreshold ("phi-node-folding-threshold", cl::Hidden, cl::init(2), cl::desc("Control the amount of phi node folding to perform (default = 2)")) |
| 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 precedes")) |
| static cl::opt< bool > | MergeCondStores ("simplifycfg-merge-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores even if an unconditional store does not ""precede - hoist multiple conditional stores into a single ""predicated store")) |
| static cl::opt< bool > | MergeCondStoresAggressively ("simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(false), cl::desc("When merging conditional stores, do so even if the resultant ""basic blocks are unlikely to be if-converted as a result")) |
| static cl::opt< bool > | SpeculateOneExpensiveInst ("speculate-one-expensive-inst", cl::Hidden, cl::init(true), cl::desc("Allow exactly one expensive instruction to be speculatively ""executed")) |
| static cl::opt< unsigned > | MaxSpeculationDepth ("max-speculation-depth", cl::Hidden, cl::init(10), cl::desc("Limit maximum recursion depth when calculating costs of ""speculatively executed instructions")) |
| #define DEBUG_TYPE "simplifycfg" |
Definition at line 81 of file SimplifyCFG.cpp.
|
static |
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 279 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(), SimplifyCondBranchToCondBranch(), and SwitchToLookupTable().
|
static |
Definition at line 5729 of file SimplifyCFG.cpp.
References llvm::BasicBlock::getSinglePredecessor(), and llvm::predecessors().
|
static |
Return true if we can thread a branch across this block.
Definition at line 2122 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), and llvm::Value::users().
Referenced by FoldCondBranchOnPHI(), and SimplifyCondBranchToCondBranch().
|
static |
Definition at line 1378 of file SimplifyCFG.cpp.
References llvm::Call, llvm::gep_type_begin(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::isBundleOperand(), llvm::Type::isMetadataTy(), and llvm::generic_gep_type_iterator< ItTy >::isSequential().
Referenced by canSinkInstructions().
|
static |
Definition at line 1428 of file SimplifyCFG.cpp.
References llvm::all_of(), llvm::any_of(), assert(), canReplaceOperandWithVariable(), llvm::dyn_cast(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::TerminatorInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), I, llvm::Type::isTokenTy(), and llvm::Value::user_begin().
Referenced by SinkThenElseCodeToEnd().
|
static |
Definition at line 4217 of file SimplifyCFG.cpp.
References llvm::array_pod_sort(), assert(), llvm::SmallVectorTemplateCommon< T >::begin(), ConstantIntSortPredicate(), E, llvm::SmallVectorTemplateCommon< T >::end(), I, and llvm::SmallVectorTemplateCommon< T >::size().
Referenced by TurnSwitchRangeIntoICmp().
|
static |
Return true if the given instruction is available in its predecessor block.
If yes, the instruction will be removed.
Definition at line 2497 of file SimplifyCFG.cpp.
References llvm::Instruction::eraseFromParent(), I, llvm::Instruction::isIdenticalTo(), and llvm::Value::replaceAllUsesWith().
Referenced by llvm::FoldBranchToCommonDest().
|
static |
Compute an abstract "cost" of speculating the given instruction, which is assumed to be safe to speculate.
TCC_Free means cheap, TCC_Basic means less cheap, and TCC_Expensive means prohibitively expensive.
Definition at line 293 of file SimplifyCFG.cpp.
References assert(), llvm::TargetTransformInfo::getUserCost(), and llvm::isSafeToSpeculativelyExecute().
|
static |
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 4541 of file SimplifyCFG.cpp.
References A, llvm::ConstantFoldCompareInstOperands(), llvm::ConstantFoldInstOperands(), llvm::ISD::ConstantPool, E, llvm::User::getNumOperands(), llvm::User::getOperand(), 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 925 of file SimplifyCFG.cpp.
References llvm::ConstantInt::getValue(), and llvm::APInt::ult().
Referenced by CasesAreContiguous(), and SimplifyBranchOnICmpChain().
|
static |
Definition at line 4721 of file SimplifyCFG.cpp.
References assert(), llvm::IRBuilder< T, Inserter >::CreateICmpEQ(), and llvm::IRBuilder< T, Inserter >::CreateSelect().
Referenced by SwitchToSelect().
|
static |
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 317 of file SimplifyCFG.cpp.
References C, llvm::ComputeSpeculationCost(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::Depth, llvm::dyn_cast(), llvm::SmallPtrSetImplBase::empty(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), I, i, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::BranchInst::isConditional(), llvm::isSafeToSpeculativelyExecute(), MaxSpeculationDepth, llvm::User::op_begin(), llvm::User::op_end(), and SpeculateOneExpensiveInst.
Referenced by FoldTwoEntryPHINode().
|
static |
Given a vector of bb/value pairs, remove any entries in the list that match the specified block.
Definition at line 731 of file SimplifyCFG.cpp.
References llvm::sys::fs::remove().
|
static |
Compute masked bits for the condition of a switch and use it to remove dead cases.
Definition at line 4347 of file SimplifyCFG.cpp.
References assert(), llvm::SmallVectorTemplateCommon< T >::back(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::tgtok::Bits, llvm::SwitchInst::case_default(), llvm::SwitchInst::cases(), llvm::computeKnownBits(), llvm::ComputeNumSignBits(), llvm::countPopulation(), llvm::dbgs(), DEBUG, llvm::SmallVectorBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), EraseTerminatorInstAndDCECond(), llvm::SwitchInst::findCaseValue(), llvm::BasicBlock::front(), GetBranchWeights(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseIndex(), llvm::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getCaseSuccessor(), llvm::SwitchInst::getCondition(), llvm::BasicBlock::getContext(), llvm::Value::getContext(), llvm::SwitchInst::getDefaultDest(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::Type::getIntegerBitWidth(), llvm::APInt::getMinSignedBits(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), HasBranchWeights(), 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::SwitchInst::setDefaultDest(), llvm::Instruction::setMetadata(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::SplitBlock(), llvm::SplitBlockPredecessors(), and std::swap().
|
static |
Definition at line 2848 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), assert(), llvm::BasicBlock::begin(), llvm::PHINode::Create(), llvm::BasicBlock::front(), llvm::UndefValue::get(), llvm::PHINode::getIncomingValueForBlock(), getParent(), llvm::BasicBlock::getSingleSuccessor(), llvm::Value::getType(), I, llvm::pred_begin(), llvm::pred_end(), and llvm::predecessors().
Referenced by mergeConditionalStoreToAddress().
|
static |
Definition at line 662 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::RecursivelyDeleteTriviallyDeadInstructions(), and SI.
Referenced by EliminateDeadSwitchCases(), llvm::FoldBranchToCommonDest(), HoistThenElseCodeToIf(), SimplifyBranchOnICmpChain(), SimplifyCondBranchToTwoReturns(), and SimplifyTerminatorOnSelect().
|
static |
Return true if either PBI or BI has branch weight available, and store the weights in {Pred|Succ}{True|False}Weight.
If one of PBI and BI does not have branch weight, use 1:1 as its weight.
Definition at line 2515 of file SimplifyCFG.cpp.
References llvm::Instruction::extractProfMetadata().
Referenced by llvm::FoldBranchToCommonDest(), and SimplifyCondBranchToCondBranch().
|
static |
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 4430 of file SimplifyCFG.cpp.
References assert(), llvm::BasicBlock::begin(), llvm::MCID::Branch, llvm::dyn_cast(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::BasicBlock::getSinglePredecessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), and llvm::BranchInst::isUnconditional().
Referenced by ForwardSwitchConditionToPHI().
|
static |
Definition at line 2831 of file SimplifyCFG.cpp.
Referenced by mergeConditionalStoreToAddress().
|
static |
Keep halving the weights until all can fit in uint32_t.
Definition at line 967 of file SimplifyCFG.cpp.
References llvm::MutableArrayRef< T >::begin(), llvm::countLeadingZeros(), llvm::MutableArrayRef< T >::end(), and Offset.
Referenced by llvm::FoldBranchToCommonDest(), and SimplifyCondBranchToCondBranch().
|
static |
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 2150 of file SimplifyCFG.cpp.
References AddPredecessorToBlock(), llvm::any_of(), llvm::BasicBlock::begin(), BlockIsSimpleEnoughToThreadThrough(), llvm::CallInst::cannotDuplicate(), llvm::BasicBlock::Create(), llvm::BranchInst::Create(), llvm::dyn_cast(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::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(), llvm::Value::hasOneUse(), I, i, llvm::iplist_impl< IntrusiveListT, TraitsT >::insert(), llvm::CallInst::isConvergent(), llvm::Type::isIntegerTy(), llvm::Instruction::mayHaveSideEffects(), N, llvm::User::op_begin(), llvm::User::op_end(), llvm::BasicBlock::removePredecessor(), llvm::Value::setName(), llvm::TerminatorInst::setSuccessor(), and llvm::SimplifyInstruction().
|
static |
Given a BB that starts with the specified two-entry PHI node, see if we can eliminate it.
Definition at line 2262 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::IRBuilder< T, Inserter >::CreateBr(), llvm::IRBuilder< 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(), PHINodeFoldingThreshold, llvm::pred_begin(), llvm::Value::replaceAllUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), llvm::SimplifyInstruction(), llvm::iplist_impl< IntrusiveListT, TraitsT >::splice(), llvm::Value::takeName(), and llvm::TargetTransformInfo::TCC_Basic.
|
static |
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 4463 of file SimplifyCFG.cpp.
References llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), E, FindPHIForConditionForwarding(), llvm::SwitchInst::getCondition(), I, llvm::PHINode::setIncomingValue(), and llvm::SmallVectorTemplateCommon< T, typename >::size().
|
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 946 of file SimplifyCFG.cpp.
References assert(), llvm::SmallVectorTemplateCommon< T, typename >::back(), llvm::SmallVectorTemplateCommon< T, typename >::front(), llvm::BranchInst::getCondition(), llvm::Instruction::getMetadata(), llvm::MDNode::getNumOperands(), llvm::MDNode::getOperand(), llvm::ConstantInt::getValue(), llvm::APInt::getZExtValue(), i, llvm::CmpInst::ICMP_EQ, llvm::LLVMContext::MD_prof, llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), llvm::SmallVectorTemplateCommon< T, typename >::size(), and std::swap().
Referenced by EliminateDeadSwitchCases(), SimplifySwitchOnSelect(), TryToSimplifyUncondBranchWithICmpInIt(), and TurnSwitchRangeIntoICmp().
|
static |
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 4575 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), ConstantFold(), llvm::ISD::ConstantPool, E, llvm::BasicBlock::end(), llvm::SwitchInst::getCondition(), llvm::Instruction::getParent(), llvm::Use::getUser(), I, llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::insert(), LookupConstant(), llvm::Value::uses(), and ValidLookupTableConstant().
Referenced by InitializeUniqueCases(), and SwitchToLookupTable().
|
static |
Extract ConstantInt from value, looking through IntToPtr and PointerNullValue.
Return NULL if value is not a constant int.
Definition at line 395 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::ConstantInt::get(), llvm::ConstantExpr::getIntegerCast(), llvm::DataLayout::getIntPtrType(), llvm::ConstantInt::getType(), llvm::Value::getType(), and llvm::Type::isPointerTy().
|
inlinestatic |
Definition at line 934 of file SimplifyCFG.cpp.
References llvm::Instruction::getMetadata(), llvm::MDNode::getOperand(), and llvm::LLVMContext::MD_prof.
Referenced by EliminateDeadSwitchCases(), SimplifySwitchOnSelect(), TryToSimplifyUncondBranchWithICmpInIt(), and TurnSwitchRangeIntoICmp().
|
static |
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 1220 of file SimplifyCFG.cpp.
References AddPredecessorToBlock(), llvm::Instruction::andIRFlags(), llvm::BasicBlock::begin(), llvm::Instruction::clone(), llvm::combineMetadata(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::dyn_cast(), EraseTerminatorInstAndDCECond(), llvm::BranchInst::getCondition(), llvm::Instruction::getDebugLoc(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::DILocation::getMergedLocation(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::Value::getType(), i, llvm::iplist_impl< IntrusiveListT, TraitsT >::insert(), llvm::Instruction::isIdenticalToWhenDefined(), llvm::TargetTransformInfo::isProfitableToHoist(), isSafeToHoistInvoke(), llvm::isSafeToSpeculativelyExecute(), llvm::Type::isVoidTy(), llvm::LLVMContext::MD_align, llvm::LLVMContext::MD_dereferenceable, llvm::LLVMContext::MD_dereferenceable_or_null, llvm::LLVMContext::MD_fpmath, llvm::LLVMContext::MD_invariant_group, llvm::LLVMContext::MD_invariant_load, llvm::LLVMContext::MD_mem_parallel_loop_access, llvm::LLVMContext::MD_nonnull, llvm::LLVMContext::MD_range, llvm::LLVMContext::MD_tbaa, passingValueIsAlwaysUndefined(), llvm::Value::replaceAllUsesWith(), llvm::Instruction::setDebugLoc(), llvm::PHINode::setIncomingValue(), llvm::iplist_impl< IntrusiveListT, TraitsT >::splice(), llvm::successors(), and llvm::Value::takeName().
|
static |
Definition at line 4668 of file SimplifyCFG.cpp.
References llvm::SwitchInst::cases(), GetCaseResults(), llvm::SwitchInst::getDefaultDest(), llvm::BasicBlock::getFirstNonPHIOrDbg(), MapCaseToResult(), and Results.
Referenced by SwitchToSelect().
|
static |
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 239 of file SimplifyCFG.cpp.
References assert(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValueForBlock(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BranchInst::isConditional(), llvm::BranchInst::isUnconditional(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::succ_begin(), llvm::succ_end(), and llvm::successors().
Referenced by llvm::FoldBranchToCommonDest().
|
static |
Definition at line 1199 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::PHINode::getIncomingValueForBlock(), and llvm::successors().
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 1864 of file SimplifyCFG.cpp.
References llvm::dyn_cast(), llvm::StoreInst::getPointerOperand(), I, llvm::StoreInst::isSimple(), and llvm::reverse().
Referenced by SpeculativelyExecuteBB().
Definition at line 5411 of file SimplifyCFG.cpp.
References llvm::ArrayRef< T >::back(), llvm::ArrayRef< T >::front(), and llvm::ArrayRef< T >::size().
Referenced by ReduceSwitchRange().
|
static |
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 4529 of file SimplifyCFG.cpp.
References C, and llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::lookup().
Referenced by ConstantFold(), and GetCaseResults().
|
static |
Definition at line 4651 of file SimplifyCFG.cpp.
Referenced by InitializeUniqueCases().
|
static |
Definition at line 3968 of file SimplifyCFG.cpp.
References llvm::BranchInst::Create(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::front(), llvm::CleanupReturnInst::getCleanupPad(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::CleanupReturnInst::getUnwindDest(), and llvm::Value::replaceAllUsesWith().
|
static |
Definition at line 3013 of file SimplifyCFG.cpp.
References llvm::dwarf::syntax::Address, llvm::Value::getNumUses(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BasicBlock::getSingleSuccessor(), llvm::BranchInst::getSuccessor(), llvm::SmallPtrSetImpl< PtrType >::insert(), mergeConditionalStoreToAddress(), P, llvm::set_intersect(), and std::swap().
Referenced by SimplifyCondBranchToCondBranch().
|
static |
Definition at line 2897 of file SimplifyCFG.cpp.
References E, llvm::BasicBlock::end(), ensureValueAvailableInSuccessor(), llvm::Instruction::eraseFromParent(), findUniqueStoreInBlocks(), llvm::Instruction::getAAMetadata(), llvm::BasicBlock::getFirstInsertionPt(), llvm::Operator::getOpcode(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BasicBlock::getTerminator(), llvm::StoreInst::getValueOperand(), I, llvm::StoreInst::isUnordered(), llvm::Instruction::mayReadOrWriteMemory(), MergeCondStoresAggressively, N, PHINodeFoldingThreshold, llvm::Instruction::setAAMetadata(), and llvm::SplitBlockAndInsertIfThen().
Referenced by mergeConditionalStores().
|
static |
Check if passing a value to an instruction will cause undefined behavior.
Definition at line 5858 of file SimplifyCFG.cpp.
References C, llvm::dyn_cast(), llvm::BasicBlock::end(), GEP, llvm::Instruction::getParent(), I, i, llvm::Constant::isNullValue(), llvm::Value::use_empty(), and llvm::Value::user_begin().
Referenced by HoistThenElseCodeToIf(), removeUndefIntroducingPredecessor(), and SpeculativelyExecuteBB().
|
static |
Definition at line 5430 of file SimplifyCFG.cpp.
References llvm::SmallVectorTemplateCommon< T >::begin(), C, llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), llvm::SwitchInst::cases(), llvm::IRBuilder< T, Inserter >::CreateLShr(), llvm::IRBuilder< T, Inserter >::CreateOr(), llvm::IRBuilder< T, Inserter >::CreateShl(), llvm::IRBuilder< T, Inserter >::CreateSub(), E, llvm::SmallVectorTemplateCommon< T >::end(), llvm::DataLayout::fitsInLegalInteger(), llvm::SwitchInst::getCondition(), llvm::SwitchInst::getNumCases(), llvm::APInt::getSExtValue(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::GreatestCommonDivisor64(), llvm::isPowerOf2_64(), isSwitchDense(), llvm::Log2_64(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::User::replaceUsesOfWith(), and llvm::IRBuilderBase::SetInsertPoint().
|
static |
Definition at line 3832 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), assert(), llvm::BasicBlock::begin(), llvm::dyn_cast(), E, llvm::BasicBlock::eraseFromParent(), llvm::PHINode::getBasicBlockIndex(), llvm::CleanupReturnInst::getCleanupPad(), llvm::BasicBlock::getFirstNonPHI(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::IntrinsicInst::getIntrinsicID(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::CleanupReturnInst::getUnwindDest(), llvm::Value::hasOneUse(), I, llvm::ARM_PROC::IE, llvm::Instruction::moveBefore(), pred, llvm::pred_begin(), llvm::pred_end(), llvm::predecessors(), llvm::PHINode::removeIncomingValue(), llvm::removeUnwindEdge(), llvm::User::replaceUsesOfWith(), and llvm::Value::use_empty().
|
static |
Definition at line 4752 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), llvm::IRBuilder< T, Inserter >::CreateBr(), llvm::Instruction::eraseFromParent(), llvm::PHINode::getBasicBlockIndex(), llvm::SwitchInst::getNumSuccessors(), llvm::Instruction::getParent(), llvm::SwitchInst::getSuccessor(), i, llvm::PHINode::removeIncomingValue(), and llvm::BasicBlock::removePredecessor().
Referenced by SwitchToSelect().
|
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 5908 of file SimplifyCFG.cpp.
References llvm::BasicBlock::begin(), llvm::IRBuilder< T, Inserter >::CreateBr(), llvm::IRBuilder< T, Inserter >::CreateUnreachable(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), i, llvm::BranchInst::isUnconditional(), passingValueIsAlwaysUndefined(), and llvm::BasicBlock::removePredecessor().
|
static |
Try to reuse the switch table index compare.
Following pattern:
Is optimized to:
Jump threading will then eliminate the second if(cond).
Definition at line 5112 of file SimplifyCFG.cpp.
References assert(), llvm::dyn_cast(), E, llvm::BranchInst::getCondition(), getFalse(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), getTrue(), llvm::Value::getType(), llvm::BasicBlock::getUniquePredecessor(), llvm::pred_begin(), llvm::pred_end(), and llvm::Value::replaceAllUsesWith().
Referenced by SwitchToLookupTable().
|
static |
Return true if it is safe to merge these two terminator instructions together.
Definition at line 207 of file SimplifyCFG.cpp.
References llvm::SmallPtrSetImpl< PtrType >::count(), Fail, llvm::PHINode::getIncomingValueForBlock(), llvm::Instruction::getParent(), llvm::succ_begin(), llvm::succ_end(), and llvm::successors().
Referenced by llvm::FoldBranchToCommonDest().
|
static |
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 5052 of file SimplifyCFG.cpp.
References llvm::SwitchInst::getNumCases(), and llvm::TargetTransformInfo::isTypeLegal().
Referenced by SwitchToLookupTable().
|
static |
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 3606 of file SimplifyCFG.cpp.
References llvm::SwitchInst::addCase(), llvm::PHINode::addIncoming(), AddPredecessorToBlock(), llvm::array_pod_sort(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::BasicBlock::begin(), ConstantIntSortPredicate(), llvm::IRBuilder< T, Inserter >::CreateCondBr(), llvm::IRBuilder< T, Inserter >::CreatePtrToInt(), llvm::IRBuilder< T, Inserter >::CreateSwitch(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::SmallVectorImpl< T >::erase(), llvm::Instruction::eraseFromParent(), EraseTerminatorInstAndDCECond(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValueForBlock(), llvm::DataLayout::getIntPtrType(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::Instruction::getOpcode(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), i, llvm::Type::isPointerTy(), llvm::APIntOps::Or(), llvm::IRBuilderBase::SetInsertPoint(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::BasicBlock::splitBasicBlock(), and std::swap().
|
static |
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 3116 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), AddPredecessorToBlock(), assert(), llvm::BasicBlock::begin(), BlockIsSimpleEnoughToThreadThrough(), llvm::BasicBlock::Create(), llvm::PHINode::Create(), llvm::BranchInst::Create(), llvm::IRBuilder< T, Inserter >::CreateNot(), llvm::IRBuilder< T, Inserter >::CreateOr(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), extractPredSuccWeights(), FitWeights(), llvm::StringRef::front(), llvm::ConstantInt::get(), llvm::PHINode::getBasicBlockIndex(), llvm::BranchInst::getCondition(), llvm::Value::getContext(), llvm::PHINode::getIncomingValue(), 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, mergeConditionalStores(), MergeCondStores, P, llvm::pred_begin(), llvm::pred_end(), llvm::BranchInst::setCondition(), llvm::Instruction::setMetadata(), and llvm::BranchInst::setSuccessor().
|
static |
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 2410 of file SimplifyCFG.cpp.
References assert(), llvm::IRBuilder< T, Inserter >::CreateRet(), llvm::IRBuilder< T, Inserter >::CreateRetVoid(), llvm::IRBuilder< 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 3463 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 3427 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::SwitchInst::CaseIteratorT< SwitchInstTy, ConstantIntTy, BasicBlockTy >::getSuccessorIndex(), llvm::SelectInst::getTrueValue(), HasBranchWeights(), SimplifyTerminatorOnSelect(), and llvm::SmallVectorTemplateCommon< T >::size().
|
static |
Definition at line 3362 of file SimplifyCFG.cpp.
References llvm::IRBuilder< T, Inserter >::CreateBr(), llvm::IRBuilder< T, Inserter >::CreateCondBr(), EraseTerminatorInstAndDCECond(), llvm::Value::getContext(), llvm::Instruction::getDebugLoc(), llvm::Instruction::getParent(), llvm::LLVMContext::MD_prof, llvm::BasicBlock::removePredecessor(), llvm::IRBuilderBase::SetCurrentDebugLocation(), llvm::Instruction::setMetadata(), and llvm::TerminatorInst::successors().
Referenced by SimplifyIndirectBrOnSelect(), and SimplifySwitchOnSelect().
|
static |
Definition at line 1521 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), llvm::all_of(), llvm::Instruction::andIRFlags(), llvm::any_of(), assert(), llvm::combineMetadataForCSE(), llvm::PHINode::Create(), llvm::dyn_cast(), E, llvm::SmallVectorTemplateCommon< T >::front(), llvm::BasicBlock::front(), llvm::Instruction::getDebugLoc(), llvm::DILocation::getMergedLocation(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::User::getOperandUse(), llvm::Instruction::getParent(), llvm::ilist_node_with_parent< NodeTy, ParentTy, Options >::getPrevNode(), llvm::BasicBlock::getTerminator(), llvm::Value::hasOneUse(), llvm::Instruction::moveBefore(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::Use::set(), llvm::Instruction::setDebugLoc(), llvm::SmallVectorTemplateCommon< T >::size(), and llvm::Value::user_begin().
Referenced by SinkThenElseCodeToEnd().
|
static |
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 1682 of file SimplifyCFG.cpp.
References assert(), B, canSinkInstructions(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::dbgs(), DEBUG, llvm::BranchInst::getSuccessor(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isSafeToSpeculativelyExecute(), llvm::BranchInst::isUnconditional(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), sinkLastInstruction(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::SplitBlockPredecessors(), and T.
|
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 1939 of file SimplifyCFG.cpp.
References assert(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::begin(), llvm::BasicBlock::begin(), llvm::ComputeSpeculationCost(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), E, llvm::BasicBlock::end(), llvm::PHINode::getBasicBlockIndex(), llvm::BranchInst::getCondition(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::Value::getName(), llvm::Instruction::getParent(), llvm::TerminatorInst::getSuccessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::StoreInst::getValueOperand(), HoistCondStores, I, i, isSafeToSpeculateStore(), llvm::isSafeToSpeculativelyExecute(), llvm::Instruction::mayHaveSideEffects(), llvm::User::op_begin(), llvm::User::op_end(), passingValueIsAlwaysUndefined(), PHINodeFoldingThreshold, llvm::PHINode::setIncomingValue(), llvm::User::setOperand(), llvm::iplist_impl< IntrusiveListT, TraitsT >::splice(), std::swap(), and llvm::TargetTransformInfo::TCC_Basic.
| STATISTIC | ( | NumBitMaps | , |
| "Number of switch instructions turned into bitmaps" | |||
| ) |
| STATISTIC | ( | NumLinearMaps | , |
| "Number of switch instructions turned into linear mapping" | |||
| ) |
| STATISTIC | ( | NumLookupTables | , |
| "Number of switch instructions turned into lookup tables" | |||
| ) |
| STATISTIC | ( | NumLookupTablesHoles | , |
| "Number of switch instructions turned into lookup tables (holes checked)" | |||
| ) |
| STATISTIC | ( | NumSinkCommons | , |
| "Number of common instructions sunk down to the end block" | |||
| ) |
| STATISTIC | ( | NumSpeculations | , |
| "Number of speculative executed instructions" | |||
| ) |
|
static |
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 5179 of file SimplifyCFG.cpp.
References llvm::PHINode::addIncoming(), AddPredecessorToBlock(), assert(), llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::count(), llvm::IRBuilder< T, Inserter >::CreateBr(), llvm::IRBuilder< T, Inserter >::CreateCondBr(), llvm::IRBuilder< T, Inserter >::CreateICmpULT(), llvm::IRBuilder< T, Inserter >::CreateLShr(), llvm::IRBuilder< T, Inserter >::CreateRet(), llvm::IRBuilder< T, Inserter >::CreateSub(), llvm::IRBuilder< T, Inserter >::CreateTrunc(), llvm::IRBuilder< T, Inserter >::CreateZExtOrTrunc(), E, 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::ConstantInt::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasOneUse(), I, i, llvm::NextPowerOf2(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::BasicBlock::removePredecessor(), Results, reuseTableCompare(), llvm::IRBuilderBase::SetInsertPoint(), llvm::Value::setName(), llvm::APInt::sgt(), ShouldBuildLookupTable(), llvm::TargetTransformInfo::shouldBuildLookupTables(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::size(), llvm::APInt::slt(), llvm::Instruction::user_back(), llvm::Value::user_begin(), and llvm::Value::users().
|
static |
If the switch is only used to initialize one or more phi nodes in a common successor block with only two different constant values, replace the switch with select.
Definition at line 4776 of file SimplifyCFG.cpp.
References assert(), ConvertTwoCaseSwitch(), llvm::SwitchInst::getCondition(), InitializeUniqueCases(), RemoveSwitchAfterSelectConversion(), and llvm::IRBuilderBase::SetInsertPoint().
|
static |
Given an block with only a single landing pad and a unconditional branch try to find another basic block which this one can be merged with.
This handles cases where we have multiple invokes with unique landing pads, but a shared handler.
We specifically choose to not worry about merging non-empty blocks here. That is a PRE/scheduling problem and is best solved elsewhere. In practice, the optimizer produces empty landing pad blocks quite frequently when dealing with exception dense code. (see: instcombine, gvn, if-else sinking in this file)
This is primarily a code size optimization. We need to avoid performing any transform which might inhibit optimization (such as our ability to specialize a particular handler via tail commoning). We do this by not merging any blocks which require us to introduce a phi. Since the same values are flowing through both blocks, we don't loose any ability to specialize. If anything, we make such specialization more likely.
TODO - This transformation could remove entries from a phi in the target block when the inputs in the phi are the same for the two blocks being merged. In some cases, this could result in removal of the PHI entirely.
Definition at line 5624 of file SimplifyCFG.cpp.
References assert(), llvm::IRBuilder< T, Inserter >::CreateUnreachable(), llvm::dyn_cast(), E, llvm::Instruction::eraseFromParent(), llvm::InvokeInst::getNormalDest(), llvm::BasicBlock::getTerminator(), llvm::BasicBlock::getUniqueSuccessor(), llvm::InvokeInst::getUnwindDest(), I, llvm::SmallSet< T, N, C >::insert(), llvm::Instruction::isIdenticalTo(), llvm::pred_begin(), llvm::pred_end(), llvm::predecessors(), llvm::InvokeInst::setUnwindDest(), llvm::succ_begin(), and llvm::succ_end().
|
static |
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 3496 of file SimplifyCFG.cpp.
References llvm::SwitchInst::addCase(), llvm::PHINode::addIncoming(), assert(), llvm::BasicBlock::begin(), llvm::SwitchInst::case_default(), llvm::BasicBlock::Create(), llvm::IRBuilder< 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::user_back().
|
static |
Turn a switch with two reachable destinations into an integer range comparison and branch.
Definition at line 4230 of file SimplifyCFG.cpp.
References assert(), llvm::BasicBlock::begin(), llvm::SwitchInst::cases(), CasesAreContiguous(), llvm::IRBuilder< T, Inserter >::CreateAdd(), llvm::IRBuilder< T, Inserter >::CreateCondBr(), llvm::IRBuilder< T, Inserter >::CreateICmpULT(), E, llvm::SmallVectorBase::empty(), llvm::Instruction::eraseFromParent(), llvm::ConstantInt::get(), GetBranchWeights(), llvm::SwitchInst::getCondition(), llvm::Value::getContext(), llvm::SwitchInst::getDefaultDest(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::Value::getName(), llvm::ConstantExpr::getNeg(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::SwitchInst::getSuccessor(), llvm::ConstantInt::getTrue(), llvm::Value::getType(), HasBranchWeights(), I, llvm::Constant::isNullValue(), llvm::LLVMContext::MD_prof, Offset, llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::Instruction::setMetadata(), and llvm::SmallVectorTemplateCommon< T >::size().
|
static |
Return true if the backend will be able to handle initializing an array of constants like C.
Definition at line 4502 of file SimplifyCFG.cpp.
References C, llvm::Constant::isDLLImportDependent(), llvm::Constant::isThreadDependent(), and llvm::TargetTransformInfo::shouldBuildLookupTablesForConstant().
Referenced by GetCaseResults().
|
static |
Return true if there are any keys in C1 that exist in C2 as well.
Definition at line 737 of file SimplifyCFG.cpp.
References llvm::array_pod_sort(), i, std::swap(), llvm::NVPTX::PTXLdStInstCode::V2, and llvm::Value::Value().
|
static |
|
static |
Referenced by SpeculativelyExecuteBB().
|
static |
Referenced by DominatesMergePoint().
|
static |
Referenced by SimplifyCondBranchToCondBranch().
|
static |
Referenced by mergeConditionalStoreToAddress().
|
static |
Referenced by FoldTwoEntryPHINode(), mergeConditionalStoreToAddress(), and SpeculativelyExecuteBB().
|
static |
|
static |
Referenced by DominatesMergePoint().
1.8.6