29#define DEBUG_TYPE "func-properties-stats"
31#define FUNCTION_PROPERTY(Name, Description) \
32 STATISTIC(Num##Name, Description); \
33 STATISTIC(Num##Name##PreOptimization, Description " (before " \
35#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
36 STATISTIC(Num##Name, Description); \
37 STATISTIC(Num##Name##PreOptimization, Description " (before " \
39#include "llvm/IR/FunctionProperties.def"
42int64_t getNumBlocksFromCond(
const BasicBlock &BB) {
45 Ret += BI->getNumSuccessors();
47 Ret += (
SI->getNumCases() + (
nullptr !=
SI->getDefaultDest()));
53 return ((!
F.hasLocalLinkage()) ? 1 : 0) +
F.getNumUses();
57void FunctionPropertiesInfo::reIncludeBB(
const BasicBlock &BB) {
61void FunctionPropertiesInfo::updateForBB(
const BasicBlock &BB,
67 for (
const auto &
I : BB) {
69 const auto *
Callee = CS->getCalledFunction();
70 if (Callee && !
Callee->isIntrinsic() && !
Callee->isDeclaration())
73 if (
I.getOpcode() == Instruction::Load) {
75 }
else if (
I.getOpcode() == Instruction::Store) {
82 const AnalysisOptions &Opts = *DetailedOpts;
84 if (SuccessorCount == 1)
86 else if (SuccessorCount == 2)
88 else if (SuccessorCount > 2)
91 if (BB.hasNPredecessors(1))
93 else if (BB.hasNPredecessors(2))
95 else if (BB.hasNPredecessorsOrMore(3))
101 Opts.medium_basic_block_instruction_threshold)
109 if (SuccessorCount > 1) {
111 if (
Successor->hasNPredecessorsOrMore(2))
132 for (
const Instruction &
I : BB) {
136 if (
I.getType()->isFloatTy())
138 else if (
I.getType()->isIntegerTy())
168 if (
Call->
arg_size() > Opts.call_with_many_arguments_threshold)
171 for (
const auto &Arg :
Call->
args()) {
172 if (Arg->getType()->isPointerTy()) {
179#define COUNT_OPERAND(OPTYPE) \
180 if (isa<OPTYPE>(Operand)) { \
181 OPTYPE##OperandCount += Direction; \
185 for (
unsigned int OperandIndex = 0; OperandIndex <
I.getNumOperands();
187 Value *Operand =
I.getOperand(OperandIndex);
211 *BB.getParent(), *IR2VecVocab);
213 BB.getContext().emitError(
"Error creating IR2Vec embeddings");
216 const auto &BBEmbedding = Embedder->getBBVector(BB);
220 FunctionEmbedding -= BBEmbedding;
222 FunctionEmbedding += BBEmbedding;
226void FunctionPropertiesInfo::updateAggregateStats(
const Function &
F,
232 std::deque<const Loop *> Worklist;
234 while (!Worklist.empty()) {
235 const auto *
L = Worklist.front();
237 std::max(
MaxLoopDepth,
static_cast<int64_t
>(
L->getLoopDepth()));
238 Worklist.pop_front();
249 .getCachedResult<IR2VecVocabAnalysis>(*
F.getParent());
260 if (Opts.enable_detailed_function_properties)
261 FPI.DetailedOpts = &Opts;
262 if (Vocabulary && Vocabulary->
isValid()) {
263 FPI.IR2VecVocab = Vocabulary;
266 for (
const auto &BB :
F)
269 FPI.updateAggregateStats(
F, LI);
328 if (!FunctionEmbedding.approximatelyEquals(FPI.FunctionEmbedding))
335#define FUNCTION_PROPERTY(Name, Description) OS << #Name ": " << Name << "\n";
337#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
338 if (DetailedOpts) { \
339 OS << #Name ": " << Name << "\n"; \
342#include "llvm/IR/FunctionProperties.def"
344#undef FUNCTION_PROPERTY
345#undef DETAILED_FUNCTION_PROPERTY
359 OS <<
"Printing analysis results of CFA for function "
360 <<
"'" <<
F.getName() <<
"':"
372 if (IsPreOptimization) {
373#define FUNCTION_PROPERTY(Name, Description) \
374 Num##Name##PreOptimization += AnalysisResults.Name;
375#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
376 Num##Name##PreOptimization += AnalysisResults.Name;
377#include "llvm/IR/FunctionProperties.def"
378#undef FUNCTION_PROPERTY
379#undef DETAILED_FUNCTION_PROPERTY
381#define FUNCTION_PROPERTY(Name, Description) Num##Name += AnalysisResults.Name;
382#define DETAILED_FUNCTION_PROPERTY(Name, Description) \
383 Num##Name += AnalysisResults.Name;
384#include "llvm/IR/FunctionProperties.def"
385#undef FUNCTION_PROPERTY
386#undef DETAILED_FUNCTION_PROPERTY
401 LikelyToChangeBBs.
insert(&CallSiteBB);
404 LikelyToChangeBBs.
insert(&*Caller.begin());
413 CallUsers.erase(&CallSiteBB);
419 Successors.insert_range(
successors(&CallSiteBB));
428 if (Inserted.insert(Succ).second)
429 DomTreeUpdates.emplace_back(DominatorTree::UpdateKind::Delete,
443 const auto *UnwindDest =
II->getUnwindDest();
444 Successors.insert_range(
successors(UnwindDest));
447 if (Inserted.insert(Succ).second)
448 DomTreeUpdates.emplace_back(DominatorTree::UpdateKind::Delete,
458 Successors.erase(&CallSiteBB);
466 for (
const auto *BB : LikelyToChangeBBs)
467 FPI.updateForBB(*BB, -1);
470DominatorTree &FunctionPropertiesUpdater::getUpdatedDominatorTree(
479 if (Inserted.insert(Succ).second)
480 FinalDomTreeUpdates.push_back({DominatorTree::UpdateKind::Insert,
486 for (
auto &Upd : DomTreeUpdates)
488 FinalDomTreeUpdates.push_back(Upd);
490 DT.applyUpdates(FinalDomTreeUpdates);
491#ifdef EXPENSIVE_CHECKS
492 assert(DT.verify(DominatorTree::VerificationLevel::Full));
524 auto &DT = getUpdatedDominatorTree(
FAM);
526 if (&CallSiteBB != &*Caller.begin())
527 Reinclude.
insert(&*Caller.begin());
533 for (
const auto *Succ : Successors)
534 if (DT.isReachableFromEntry(Succ))
543 const auto IncludeSuccessorsMark = Reinclude.
size();
544 bool CSInsertion = Reinclude.
insert(&CallSiteBB);
547 for (
size_t I = 0;
I < Reinclude.
size(); ++
I) {
548 const auto *BB = Reinclude[
I];
549 FPI.reIncludeBB(*BB);
550 if (
I >= IncludeSuccessorsMark)
558 const auto AlreadyExcludedMark = Unreachable.
size();
559 for (
size_t I = 0;
I < Unreachable.
size(); ++
I) {
560 const auto *U = Unreachable[
I];
561 if (
I >= AlreadyExcludedMark)
562 FPI.updateForBB(*U, -1);
564 if (!DT.isReachableFromEntry(Succ))
569 FPI.updateAggregateStats(Caller, LI);
570#ifdef EXPENSIVE_CHECKS
575bool FunctionPropertiesUpdater::isUpdateValid(
Function &
F,
579 DominatorTree::VerificationLevel::Full))
584 .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.
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.
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>.