llvm-project
63 строки · 2.2 Кб
1//===-- InstCount.cpp - Collects the count of all instructions ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass collects the count of all instructions and reports them
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Analysis/InstCount.h"14#include "llvm/ADT/Statistic.h"15#include "llvm/Analysis/Passes.h"16#include "llvm/IR/Function.h"17#include "llvm/IR/InstVisitor.h"18#include "llvm/Support/Debug.h"19#include "llvm/Support/ErrorHandling.h"20#include "llvm/Support/raw_ostream.h"21using namespace llvm;22
23#define DEBUG_TYPE "instcount"24
25STATISTIC(TotalInsts, "Number of instructions (of all types)");26STATISTIC(TotalBlocks, "Number of basic blocks");27STATISTIC(TotalFuncs, "Number of non-external functions");28
29#define HANDLE_INST(N, OPCODE, CLASS) \30STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");31
32#include "llvm/IR/Instruction.def"33
34namespace {35class InstCount : public InstVisitor<InstCount> {36friend class InstVisitor<InstCount>;37
38void visitFunction(Function &F) { ++TotalFuncs; }39void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }40
41#define HANDLE_INST(N, OPCODE, CLASS) \42void visit##OPCODE(CLASS &) { \43++Num##OPCODE##Inst; \44++TotalInsts; \45}46
47#include "llvm/IR/Instruction.def"48
49void visitInstruction(Instruction &I) {50errs() << "Instruction Count does not know about " << I;51llvm_unreachable(nullptr);52}53};54} // namespace55
56PreservedAnalyses InstCountPass::run(Function &F,57FunctionAnalysisManager &FAM) {58LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()59<< "\n");60InstCount().visit(F);61
62return PreservedAnalyses::all();63}
64