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