blob: 95ab6ee3db5bd4714bc48afba87ebca9b01ca88a [file] [log] [blame]
Dinakar Dhurjatie5554602002-11-13 18:22:13 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Dinakar Dhurjatie5554602002-11-13 18:22:13 +00009//
Misha Brukman2b37d7c2005-04-21 21:13:18 +000010// This pass collects the count of all instructions and reports them
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000011//
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000012//===----------------------------------------------------------------------===//
13
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/ADT/Statistic.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000015#include "llvm/Analysis/Passes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Function.h"
Chandler Carruth67f6bf72014-03-06 03:23:41 +000017#include "llvm/IR/InstVisitor.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000018#include "llvm/Pass.h"
David Greene270862d2009-12-23 20:34:27 +000019#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattnerbdff5482009-08-23 04:37:46 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner6ae7e982005-03-22 03:55:10 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chandler Carruth4da25372014-04-22 02:48:03 +000024#define DEBUG_TYPE "instcount"
25
Chris Lattner3b27d682006-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 Lattnera1af8bd2002-12-07 23:24:24 +000029
Chris Lattner149a5202002-12-03 19:40:16 +000030#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner3b27d682006-12-19 22:30:33 +000031 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000032
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/Instruction.def"
Chris Lattner149a5202002-12-03 19:40:16 +000034
Chris Lattner3b27d682006-12-19 22:30:33 +000035namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000036 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencere26057a2004-11-16 06:58:55 +000037 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000038
Chris Lattnera1af8bd2002-12-07 23:24:24 +000039 void visitFunction (Function &F) { ++TotalFuncs; }
40 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
41
Chris Lattner149a5202002-12-03 19:40:16 +000042#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000043 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000044
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000045#include "llvm/IR/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000046
Argyrios Kyrtzidis8c8b9ee2010-08-15 10:27:23 +000047 void visitInstruction(Instruction &I) {
David Greeneb64ca132009-12-23 23:29:28 +000048 errs() << "Instruction Count does not know about " << I;
Craig Topper570e52c2014-04-15 04:59:12 +000049 llvm_unreachable(nullptr);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000050 }
51 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000052 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000053 InstCount() : FunctionPass(ID) {
54 initializeInstCountPass(*PassRegistry::getPassRegistry());
55 }
Devang Patel794fd752007-05-01 21:15:47 +000056
Craig Topperc37e6c02014-03-05 07:30:04 +000057 bool runOnFunction(Function &F) override;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000058
Craig Topperc37e6c02014-03-05 07:30:04 +000059 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000060 AU.setPreservesAll();
61 }
Craig Topperc37e6c02014-03-05 07:30:04 +000062 void print(raw_ostream &O, const Module *M) const override {}
Chris Lattner149a5202002-12-03 19:40:16 +000063
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000064 };
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +000065}
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000066
Dan Gohman844731a2008-05-13 00:00:25 +000067char InstCount::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000068INITIALIZE_PASS(InstCount, "instcount",
Owen Andersonce665bd2010-10-07 22:25:06 +000069 "Counts the various types of Instructions", false, true)
Dan Gohman844731a2008-05-13 00:00:25 +000070
Chris Lattner4fb1b212005-10-24 01:00:45 +000071FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
72
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000073// InstCount::run - This is the main Analysis entry point for a
74// function.
75//
Chris Lattner6666a042003-08-29 14:43:17 +000076bool InstCount::runOnFunction(Function &F) {
77 visit(F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000078 return false;
79}