Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/InstCount.cpp
35233 views
//===-- InstCount.cpp - Collects the count of all instructions ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This pass collects the count of all instructions and reports them9//10//===----------------------------------------------------------------------===//1112#include "llvm/Analysis/InstCount.h"13#include "llvm/ADT/Statistic.h"14#include "llvm/Analysis/Passes.h"15#include "llvm/IR/Function.h"16#include "llvm/IR/InstVisitor.h"17#include "llvm/Support/Debug.h"18#include "llvm/Support/ErrorHandling.h"19#include "llvm/Support/raw_ostream.h"20using namespace llvm;2122#define DEBUG_TYPE "instcount"2324STATISTIC(TotalInsts, "Number of instructions (of all types)");25STATISTIC(TotalBlocks, "Number of basic blocks");26STATISTIC(TotalFuncs, "Number of non-external functions");2728#define HANDLE_INST(N, OPCODE, CLASS) \29STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");3031#include "llvm/IR/Instruction.def"3233namespace {34class InstCount : public InstVisitor<InstCount> {35friend class InstVisitor<InstCount>;3637void visitFunction(Function &F) { ++TotalFuncs; }38void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }3940#define HANDLE_INST(N, OPCODE, CLASS) \41void visit##OPCODE(CLASS &) { \42++Num##OPCODE##Inst; \43++TotalInsts; \44}4546#include "llvm/IR/Instruction.def"4748void visitInstruction(Instruction &I) {49errs() << "Instruction Count does not know about " << I;50llvm_unreachable(nullptr);51}52};53} // namespace5455PreservedAnalyses InstCountPass::run(Function &F,56FunctionAnalysisManager &FAM) {57LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()58<< "\n");59InstCount().visit(F);6061return PreservedAnalyses::all();62}636465