31#define DEBUG_TYPE "func-properties-stats"
33#define FUNCTION_PROPERTY(Name, Description) \
34 STATISTIC(Num##Name, Description); \
35 STATISTIC(Num##Name##PreOptimization, Description " (before " \
37#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
38 STATISTIC(Num##Name, Description); \
39 STATISTIC(Num##Name##PreOptimization, Description " (before " \
41#include "llvm/IR/FunctionProperties.def"
46 cl::desc(
"Whether or not to compute detailed function properties."));
50int64_t getNumBlocksFromCond(
const BasicBlock &BB) {
53 Ret += BI->getNumSuccessors();
55 Ret += (
SI->getNumCases() + (
nullptr !=
SI->getDefaultDest()));
61 return ((!
F.hasLocalLinkage()) ? 1 : 0) +
F.getNumUses();
65void FunctionPropertiesInfo::reIncludeBB(
const BasicBlock &BB) {
69void FunctionPropertiesInfo::updateForBB(
const BasicBlock &BB,
75 for (
const auto &
I : BB) {
77 const auto *
Callee = CS->getCalledFunction();
78 if (Callee && !
Callee->isIntrinsic() && !
Callee->isDeclaration())
81 if (
I.getOpcode() == Instruction::Load) {
83 }
else if (
I.getOpcode() == Instruction::Store) {
92 if (SuccessorCount == 1)
94 else if (SuccessorCount == 2)
96 else if (SuccessorCount > 2)
99 if (BB.hasNPredecessors(1))
101 else if (BB.hasNPredecessors(2))
103 else if (BB.hasNPredecessorsOrMore(3))
109 Opts.medium_basic_block_instruction_threshold)
117 if (SuccessorCount > 1) {
119 if (
Successor->hasNPredecessorsOrMore(2))
140 for (
const Instruction &
I : BB) {
144 if (
I.getType()->isFloatTy())
146 else if (
I.getType()->isIntegerTy())
176 if (
Call->
arg_size() > Opts.call_with_many_arguments_threshold)
179 for (
const auto &Arg :
Call->
args()) {
180 if (Arg->getType()->isPointerTy()) {
187#define COUNT_OPERAND(OPTYPE) \
188 if (isa<OPTYPE>(Operand)) { \
189 OPTYPE##OperandCount += Direction; \
193 for (
unsigned int OperandIndex = 0; OperandIndex <
I.getNumOperands();
195 Value *Operand =
I.getOperand(OperandIndex);
219 *BB.getParent(), *IR2VecVocab);
221 BB.getContext().emitError(
"Error creating IR2Vec embeddings");
224 const auto &BBEmbedding = Embedder->getBBVector(BB);
228 FunctionEmbedding -= BBEmbedding;
230 FunctionEmbedding += BBEmbedding;
234void FunctionPropertiesInfo::updateAggregateStats(
const Function &
F,
240 std::deque<const Loop *> Worklist;
242 while (!Worklist.empty()) {
243 const auto *
L = Worklist.front();
245 std::max(
MaxLoopDepth,
static_cast<int64_t
>(
L->getLoopDepth()));
246 Worklist.pop_front();
257 .getCachedResult<IR2VecVocabAnalysis>(*
F.getParent());
267 if (Vocabulary && Vocabulary->
isValid()) {
268 FPI.IR2VecVocab = Vocabulary;
271 for (
const auto &BB :
F)
274 FPI.updateAggregateStats(
F, LI);
333 if (!FunctionEmbedding.approximatelyEquals(FPI.FunctionEmbedding))
340#define FUNCTION_PROPERTY(Name, Description) OS << #Name ": " << Name << "\n";
342#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
343 if (EnableDetailedFunctionProperties) { \
344 OS << #Name ": " << Name << "\n"; \
347#include "llvm/IR/FunctionProperties.def"
349#undef FUNCTION_PROPERTY
350#undef DETAILED_FUNCTION_PROPERTY
364 OS <<
"Printing analysis results of CFA for function "
365 <<
"'" <<
F.getName() <<
"':"
377 if (IsPreOptimization) {
378#define FUNCTION_PROPERTY(Name, Description) \
379 Num##Name##PreOptimization += AnalysisResults.Name;
380#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
381 Num##Name##PreOptimization += AnalysisResults.Name;
382#include "llvm/IR/FunctionProperties.def"
383#undef FUNCTION_PROPERTY
384#undef DETAILED_FUNCTION_PROPERTY
386#define FUNCTION_PROPERTY(Name, Description) Num##Name += AnalysisResults.Name;
387#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
388 Num##Name += AnalysisResults.Name;
389#include "llvm/IR/FunctionProperties.def"
390#undef FUNCTION_PROPERTY
391#undef DETAILED_FUNCTION_PROPERTY
406 LikelyToChangeBBs.
insert(&CallSiteBB);
409 LikelyToChangeBBs.
insert(&*Caller.begin());
418 CallUsers.erase(&CallSiteBB);
424 Successors.insert_range(
successors(&CallSiteBB));
433 if (Inserted.insert(Succ).second)
434 DomTreeUpdates.emplace_back(DominatorTree::UpdateKind::Delete,
448 const auto *UnwindDest =
II->getUnwindDest();
449 Successors.insert_range(
successors(UnwindDest));
452 if (Inserted.insert(Succ).second)
453 DomTreeUpdates.emplace_back(DominatorTree::UpdateKind::Delete,
463 Successors.erase(&CallSiteBB);
471 for (
const auto *BB : LikelyToChangeBBs)
472 FPI.updateForBB(*BB, -1);
475DominatorTree &FunctionPropertiesUpdater::getUpdatedDominatorTree(
484 if (Inserted.insert(Succ).second)
485 FinalDomTreeUpdates.push_back({DominatorTree::UpdateKind::Insert,
491 for (
auto &Upd : DomTreeUpdates)
493 FinalDomTreeUpdates.push_back(Upd);
495 DT.applyUpdates(FinalDomTreeUpdates);
496#ifdef EXPENSIVE_CHECKS
497 assert(DT.verify(DominatorTree::VerificationLevel::Full));
529 auto &DT = getUpdatedDominatorTree(
FAM);
531 if (&CallSiteBB != &*Caller.begin())
532 Reinclude.
insert(&*Caller.begin());
538 for (
const auto *Succ : Successors)
539 if (DT.isReachableFromEntry(Succ))
548 const auto IncludeSuccessorsMark = Reinclude.
size();
549 bool CSInsertion = Reinclude.
insert(&CallSiteBB);
552 for (
size_t I = 0;
I < Reinclude.
size(); ++
I) {
553 const auto *BB = Reinclude[
I];
554 FPI.reIncludeBB(*BB);
555 if (
I >= IncludeSuccessorsMark)
563 const auto AlreadyExcludedMark = Unreachable.
size();
564 for (
size_t I = 0;
I < Unreachable.
size(); ++
I) {
565 const auto *U = Unreachable[
I];
566 if (
I >= AlreadyExcludedMark)
567 FPI.updateForBB(*U, -1);
569 if (!DT.isReachableFromEntry(Succ))
574 FPI.updateAggregateStats(Caller, LI);
575#ifdef EXPENSIVE_CHECKS
580bool FunctionPropertiesUpdater::isUpdateValid(
Function &
F,
584 DominatorTree::VerificationLevel::Full))
589 .getCachedResult<IR2VecVocabAnalysis>(*
F.getParent());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const Function * getParent(const Value *V)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define COUNT_OPERAND(OPTYPE)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Loop::LoopBounds::Direction Direction
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
bool doesNotReturn() const
Determine if the call cannot return.
unsigned arg_size() const
Implements a dense probed hash-table based set.
Analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static LLVM_ABI AnalysisKey Key
LLVM_ABI FunctionPropertiesInfo run(Function &F, FunctionAnalysisManager &FAM)
int64_t BasicBlocksWithMoreThanTwoSuccessors
int64_t BasicBlocksWithSinglePredecessor
int64_t CallReturnsVectorPointerCount
int64_t CallReturnsPointerCount
int64_t CallWithManyArgumentsCount
int64_t CallReturnsVectorIntCount
int64_t CallReturnsVectorFloatCount
int64_t BranchSuccessorCount
int64_t CastInstructionCount
int64_t BasicBlockCount
Number of basic blocks.
int64_t CriticalEdgeCount
int64_t Uses
Number of uses of this function, plus 1 if the function is callable outside the module.
int64_t InlineAsmOperandCount
int64_t ConstantFPOperandCount
int64_t BasicBlocksWithTwoSuccessors
int64_t NoReturnCallCount
int64_t InstructionOperandCount
int64_t CallWithPointerArgumentCount
int64_t FloatingPointInstructionCount
int64_t TopLevelLoopCount
int64_t CallReturnsIntegerCount
int64_t BlocksReachedFromConditionalInstruction
Number of blocks reached from a conditional instruction, or that are 'cases' of a SwitchInstr.
int64_t ConditionalBranchCount
int64_t GlobalValueOperandCount
int64_t UnconditionalBranchCount
int64_t ArgumentOperandCount
int64_t BasicBlocksWithSingleSuccessor
LLVM_ABI bool operator==(const FunctionPropertiesInfo &FPI) const
int64_t BasicBlockOperandCount
int64_t ControlFlowEdgeCount
int64_t SwitchInstructionCount
int64_t BranchInstructionCount
int64_t BasicBlocksWithTwoPredecessors
int64_t MediumBasicBlocks
int64_t IntegerInstructionCount
static LLVM_ABI FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F, const DominatorTree &DT, const LoopInfo &LI, const ir2vec::Vocabulary *Vocabulary)
int64_t CallReturnsFloatCount
LLVM_ABI void print(raw_ostream &OS) const
int64_t SwitchSuccessorCount
int64_t TotalInstructionCount
int64_t BasicBlocksWithMoreThanTwoPredecessors
int64_t ConstantOperandCount
int64_t ConstantIntOperandCount
int64_t UnknownOperandCount
int64_t DirectCallsToDefinedFunctions
Number of direct calls made from this function to other functions defined in this module.
int64_t IndirectCallCount
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
LLVM_ABI FunctionPropertiesUpdater(FunctionPropertiesInfo &FPI, CallBase &CB)
LLVM_ABI void finish(FunctionAnalysisManager &FAM) const
iterator_range< user_iterator > users()
Analysis pass that exposes the LoopInfo for a function.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
A vector that has set insertion semantics.
size_type size() const
Determine the number of elements in the SetVector.
void insert_range(Range &&R)
bool insert(const value_type &X)
Insert a new element into the SetVector.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
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.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isPointerTy() const
True if this is an instance of PointerType.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Type * getType() const
All values are typed, get the type of this value.
static LLVM_ABI std::unique_ptr< Embedder > create(IR2VecKind Mode, const Function &F, const Vocabulary &Vocab)
Factory method to create an Embedder object.
Class for storing and accessing the IR2Vec vocabulary.
unsigned getDimension() const
This class implements an extremely fast bulk output stream that can only output to a stream.
initializer< Ty > init(const Ty &Val)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto successors(const MachineBasicBlock *BB)
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI cl::opt< bool > EnableDetailedFunctionProperties("enable-detailed-function-properties", cl::Hidden, cl::init(false), cl::desc("Whether or not to compute detailed function properties."))
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto succ_size(const MachineBasicBlock *BB)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Embedding is a datatype that wraps std::vector<double>.