1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
//===--- InstCount.cpp - Collects the count of all instructions -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This pass collects the count of all instructions and reports them
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-instcount"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/PassManager.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILVisitor.h"
#include "llvm/ADT/Statistic.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Statistics
//===----------------------------------------------------------------------===//
// Local aggregate statistics
STATISTIC(TotalInsts, "Number of instructions (of all types) in non-external "
"functions");
STATISTIC(TotalBlocks, "Number of basic blocks in non-external functions");
STATISTIC(TotalFuncs , "Number of non-external functions");
// External aggregate statistics
STATISTIC(TotalExternalFuncInsts, "Number of instructions (of all types) in "
"external functions");
STATISTIC(TotalExternalFuncBlocks, "Number of basic blocks in external "
"functions");
STATISTIC(TotalExternalFuncDefs, "Number of external funcs definitions");
STATISTIC(TotalExternalFuncDecls, "Number of external funcs declarations");
// Linkage statistics
STATISTIC(TotalPublicFuncs, "Number of public funcs");
STATISTIC(TotalPublicNonABIFuncs, "Number of public non-ABI funcs");
STATISTIC(TotalPackageFuncs, "Number of package funcs");
STATISTIC(TotalPackageNonABIFuncs, "Number of package non-ABI funcs");
STATISTIC(TotalHiddenFuncs, "Number of hidden funcs");
STATISTIC(TotalPrivateFuncs, "Number of private funcs");
STATISTIC(TotalSharedFuncs, "Number of shared funcs");
STATISTIC(TotalPublicExternalFuncs, "Number of public external funcs");
STATISTIC(TotalPackageExternalFuncs, "Number of package external funcs");
STATISTIC(TotalHiddenExternalFuncs, "Number of hidden external funcs");
// Individual instruction statistics
#define INST(Id, Parent) \
STATISTIC(Num##Id, "Number of " #Id);
#include "swift/SIL/SILNodes.def"
namespace {
struct InstCountVisitor : SILInstructionVisitor<InstCountVisitor> {
// We store these locally so that we do not continually check if the function
// is external or not. Instead, we just check once at the end and accumulate.
unsigned InstCount = 0;
unsigned BlockCount = 0;
void visitSILBasicBlock(SILBasicBlock *BB) {
++BlockCount;
SILInstructionVisitor<InstCountVisitor>::visitSILBasicBlock(BB);
}
void visitSILFunction(SILFunction *F) {
SILInstructionVisitor<InstCountVisitor>::visitSILFunction(F);
}
void visitValueBase(ValueBase *V) { }
#define INST(Id, Parent) \
void visit##Id(Id *I) { \
++Num##Id; \
++InstCount; \
}
#include "swift/SIL/SILNodes.def"
};
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// Top Level Driver
//===----------------------------------------------------------------------===//
namespace {
class InstCount : public SILFunctionTransform {
/// The entry point to the transformation.
void run() override {
SILFunction *F = getFunction();
InstCountVisitor V;
V.visitSILFunction(F);
if (F->isAvailableExternally()) {
if (F->isDefinition()) {
TotalExternalFuncInsts += V.InstCount;
TotalExternalFuncBlocks += V.BlockCount;
++TotalExternalFuncDefs;
} else {
++TotalExternalFuncDecls;
}
} else {
TotalInsts += V.InstCount;
TotalBlocks += V.BlockCount;
++TotalFuncs;
}
switch (F->getLinkage()) {
case SILLinkage::Public:
++TotalPublicFuncs;
break;
case SILLinkage::PublicNonABI:
++TotalPublicNonABIFuncs;
break;
case SILLinkage::Package:
++TotalPackageFuncs;
break;
case SILLinkage::PackageNonABI:
++TotalPackageNonABIFuncs;
break;
case SILLinkage::Hidden:
++TotalHiddenFuncs;
break;
case SILLinkage::Shared:
++TotalSharedFuncs;
break;
case SILLinkage::Private:
++TotalPrivateFuncs;
break;
case SILLinkage::PublicExternal:
++TotalPublicExternalFuncs;
break;
case SILLinkage::PackageExternal:
++TotalPackageExternalFuncs;
break;
case SILLinkage::HiddenExternal:
++TotalHiddenExternalFuncs;
break;
}
}
};
} // end anonymous namespace
SILTransform *swift::createInstCount() {
return new InstCount();
}
void swift::performSILInstCountIfNeeded(SILModule *M) {
if (!M->getOptions().PrintInstCounts)
return;
executePassPipelinePlan(
M, SILPassPipelinePlan::getInstCountPassPipeline(M->getOptions()));
}
|