46 #define DEBUG_TYPE "simplifycfg" 50 cl::desc(
"Control the number of bonus instructions (default = 1)"));
54 cl::desc(
"Preserve canonical loop structure (default = true)"));
58 cl::desc(
"Convert switches to lookup tables (default = false)"));
62 cl::desc(
"Forward switch condition to phi ops (default = false)"));
66 cl::desc(
"Sink common instructions (default = false)"));
69 STATISTIC(NumSimpl,
"Number of blocks simplified");
88 if (Ret != &BB.
front()) {
93 while (isa<DbgInfoIntrinsic>(I) && I != BB.
begin())
95 if (!isa<DbgInfoIntrinsic>(I) &&
115 cast<ReturnInst>(RetBlock->
getTerminator())->getOperand(0)) {
127 std::distance(PB, PE),
"merge",
150 bool Changed =
false;
151 bool LocalChange =
true;
156 for (
unsigned i = 0, e = Edges.
size(); i != e; ++i)
157 LoopHeaders.
insert(const_cast<BasicBlock *>(Edges[i].second));
159 while (LocalChange) {
164 if (
simplifyCFG(&*BBIt++, TTI, Options, &LoopHeaders)) {
169 Changed |= LocalChange;
181 if (!EverChanged)
return false;
194 }
while (EverChanged);
233 std::function<bool(const Function &)> PredicateFtor;
235 CFGSimplifyPass(
unsigned Threshold = 1,
bool ForwardSwitchCond =
false,
236 bool ConvertSwitch =
false,
bool KeepLoops =
true,
265 if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F)))
268 Options.
AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
269 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
291 bool ConvertSwitch,
bool KeepLoops,
294 return new CFGSimplifyPass(Threshold, ForwardSwitchCond, ConvertSwitch,
295 KeepLoops, SinkCommon, std::move(Ftor));
Legacy wrapper pass to provide the GlobalsAAResult object.
This file provides the interface for the pass responsible for both simplifying and canonicalizing the...
Return a value (possibly void), from a function.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static cl::opt< bool > UserSwitchToLookup("switch-to-lookup", cl::Hidden, cl::init(false), cl::desc("Convert switches to lookup tables (default = false)"))
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
SimplifyCFGPass()
The default constructor sets the pass options to create canonical IR, rather than optimal IR...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
This is the interface for a simple mod/ref and alias analysis over globals.
bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI=nullptr, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
An immutable pass that tracks lazily created AssumptionCache objects.
static cl::opt< unsigned > UserBonusInstThreshold("bonus-inst-threshold", cl::Hidden, cl::init(1), cl::desc("Control the number of bonus instructions (default = 1)"))
Analysis pass providing the TargetTransformInfo.
STATISTIC(NumFunctions, "Total number of functions")
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
iterator begin()
Instruction iterator methods.
FunctionPass * createCFGSimplificationPass(unsigned Threshold=1, bool ForwardSwitchCond=false, bool ConvertSwitch=false, bool KeepLoops=true, bool SinkCommon=false, std::function< bool(const Function &)> Ftor=nullptr)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI, const SimplifyCFGOptions &Options)
Call SimplifyCFG on all the blocks in the function, iterating until no more changes are made...
This file contains the simple types necessary to represent the attributes associated with functions a...
Type * getType() const
All values are typed, get the type of this value.
static cl::opt< bool > SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true), cl::desc("Sink common instructions down to the end block"))
bool ForwardSwitchCondToPhi
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Value * getOperand(unsigned i) const
static cl::opt< bool > UserForwardSwitchCond("forward-switch-cond", cl::Hidden, cl::init(false), cl::desc("Forward switch condition to phi ops (default = false)"))
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
static cl::opt< bool > UserSinkCommonInsts("sink-common-insts", cl::Hidden, cl::init(false), cl::desc("Sink common instructions (default = false)"))
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
bool ConvertSwitchToLookupTable
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
A function analysis which provides an AssumptionCache.
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setOperand(unsigned i, Value *Val)
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI, const SimplifyCFGOptions &Options)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, false) INITIALIZE_PASS_END(CFGSimplifyPass
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static cl::opt< bool > UserKeepLoops("keep-loops", cl::Hidden, cl::init(true), cl::desc("Preserve canonical loop structure (default = true)"))
void preserve()
Mark an analysis as preserved.
void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock *> > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them...
static bool mergeEmptyReturnBlocks(Function &F)
If we have more than one empty (other than phi node) return blocks, merge them together to promote re...
void initializeCFGSimplifyPassPass(PassRegistry &)
LLVM Value Representation.
print Print MemDeps of function
A container for analyses that lazily runs them and caches their results.
bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, const SimplifyCFGOptions &Options={}, SmallPtrSetImpl< BasicBlock *> *LoopHeaders=nullptr)
This function is used to do simplification of a CFG.
A set of parameters used to control the transforms in the SimplifyCFG pass.