blob: 943a99a5f46dec3916dab791367609a5ecbacda2 [file] [log] [blame]
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00008//
Misha Brukman01808ca2005-04-21 21:13:18 +00009// This pass collects the count of all instructions and reports them
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000010//
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000011//===----------------------------------------------------------------------===//
12
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Analysis/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Function.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000016#include "llvm/IR/InstVisitor.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000017#include "llvm/Pass.h"
David Greenea7b92ee2009-12-23 20:34:27 +000018#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000019#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb25de3f2009-08-23 04:37:46 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerf0068402005-03-22 03:55:10 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chandler Carruthf1221bd2014-04-22 02:48:03 +000023#define DEBUG_TYPE "instcount"
24
Chris Lattner57ef9422006-12-19 22:30:33 +000025STATISTIC(TotalInsts , "Number of instructions (of all types)");
26STATISTIC(TotalBlocks, "Number of basic blocks");
27STATISTIC(TotalFuncs , "Number of non-external functions");
Chris Lattner3935d2b2002-12-07 23:24:24 +000028
Chris Lattner5e058172002-12-03 19:40:16 +000029#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner57ef9422006-12-19 22:30:33 +000030 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000031
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Instruction.def"
Chris Lattner5e058172002-12-03 19:40:16 +000033
Chris Lattner57ef9422006-12-19 22:30:33 +000034namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000035 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencerf5e3cfe2004-11-16 06:58:55 +000036 friend class InstVisitor<InstCount>;
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000037
Chris Lattner3935d2b2002-12-07 23:24:24 +000038 void visitFunction (Function &F) { ++TotalFuncs; }
39 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
40
Chris Lattner5e058172002-12-03 19:40:16 +000041#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner3935d2b2002-12-07 23:24:24 +000042 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000043
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/Instruction.def"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000045
Argyrios Kyrtzidisd0fcc9a2010-08-15 10:27:23 +000046 void visitInstruction(Instruction &I) {
David Greene1495b5f2009-12-23 23:29:28 +000047 errs() << "Instruction Count does not know about " << I;
Craig Topper9f008862014-04-15 04:59:12 +000048 llvm_unreachable(nullptr);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000049 }
50 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000051 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000052 InstCount() : FunctionPass(ID) {
53 initializeInstCountPass(*PassRegistry::getPassRegistry());
54 }
Devang Patel09f162c2007-05-01 21:15:47 +000055
Craig Toppere9ba7592014-03-05 07:30:04 +000056 bool runOnFunction(Function &F) override;
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000057
Craig Toppere9ba7592014-03-05 07:30:04 +000058 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000059 AU.setPreservesAll();
60 }
Craig Toppere9ba7592014-03-05 07:30:04 +000061 void print(raw_ostream &O, const Module *M) const override {}
Chris Lattner5e058172002-12-03 19:40:16 +000062
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000063 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000064}
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000065
Dan Gohmand78c4002008-05-13 00:00:25 +000066char InstCount::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000067INITIALIZE_PASS(InstCount, "instcount",
Owen Andersondf7a4f22010-10-07 22:25:06 +000068 "Counts the various types of Instructions", false, true)
Dan Gohmand78c4002008-05-13 00:00:25 +000069
Chris Lattnerca014ad2005-10-24 01:00:45 +000070FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
71
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000072// InstCount::run - This is the main Analysis entry point for a
73// function.
74//
Chris Lattner5f9be672003-08-29 14:43:17 +000075bool InstCount::runOnFunction(Function &F) {
76 visit(F);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000077 return false;
78}