Add support for printing out statistics information when -stats is added to

the command line

llvm-svn: 2601
This commit is contained in:
Chris Lattner 2002-05-10 15:38:35 +00:00
parent bad1b4dfce
commit 0b18c1d64e
17 changed files with 109 additions and 17 deletions

View File

@ -19,6 +19,9 @@
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/Pass.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumMerged("constmerge\t\t- Number of global constants merged");
// mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate
// constants, starting at global ConstantNo, and adds vars to the map if they
@ -50,6 +53,7 @@ bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
delete GList.remove(GList.begin()+ConstantNo);
--ConstantNo; // Don't skip the next constant.
++NumMerged;
MadeChanges = true;
}
}

View File

@ -27,6 +27,13 @@
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <algorithm>
#include <iostream>
#include "Support/StatisticReporter.h"
static Statistic<> NumResolved("funcresolve\t- Number of varargs functions resolved");
static Statistic<> NumTypeSymtabEntriesKilled("cleangcc\t- Number of unused typenames removed from symtab");
static Statistic<> NumCastsMoved("cleangcc\t- Number of casts removed from head of basic block");
static Statistic<> NumRefactoredPreds("cleangcc\t- Number of predecessor blocks refactored");
using std::vector;
using std::string;
using std::cerr;
@ -112,6 +119,7 @@ bool CleanupGCCOutput::doInitialization(Module *M) {
Plane.erase(PI); // Alas, GCC 2.95.3 doesn't *SIGH*
PI = Plane.begin();
#endif
++NumTypeSymtabEntriesKilled;
Changed = true;
} else {
++PI;
@ -159,6 +167,9 @@ static inline bool FixCastsAndPHIs(BasicBlock *BB) {
// Move the cast instruction to the current insert position...
--InsertPos; // New position for cast to go...
std::swap(*InsertPos, *I); // Cast goes down, PHI goes up
Changed = true;
++NumCastsMoved;
if (isa<PHINode>(Src) && // Handle case #1
cast<PHINode>(Src)->getParent() == BB) {
@ -196,6 +207,7 @@ static inline bool FixCastsAndPHIs(BasicBlock *BB) {
// Reinsert the cast right before the terminator in Pred.
Pred->getInstList().insert(Pred->end()-1, CI);
Changed = true;
}
} else {
++I;
@ -281,6 +293,7 @@ bool CleanupGCCOutput::runOnFunction(Function *M) {
for (unsigned i = 0; i < Preds.size(); ++i) {
if (SortedPreds[i] == LastOne) { // Found a duplicate.
RefactorPredecessor(BB, SortedPreds[i]);
++NumRefactoredPreds;
Changed = true;
}
LastOne = SortedPreds[i];
@ -430,6 +443,7 @@ bool FunctionResolvingPass::run(Module *M) {
delete Functions[i];
Functions.erase(Functions.begin()+i);
Changed = true;
++NumResolved;
continue;
}
}
@ -499,11 +513,13 @@ bool FunctionResolvingPass::run(Module *M) {
assert(CI->getOperand(0) == Old);
CI->setOperand(0, Concrete);
Changed = true;
++NumResolved;
} else if (CallInst *CI = dyn_cast<CallInst>(U)) {
// Can only fix up calls TO the argument, not args passed in.
if (CI->getCalledValue() == Old) {
ConvertCallTo(CI, Concrete);
Changed = true;
++NumResolved;
} else {
cerr << "Couldn't cleanup this function call, must be an"
<< " argument or something!" << CI;

View File

@ -11,6 +11,9 @@
#include "llvm/GlobalVariable.h"
#include "llvm/Analysis/CallGraph.h"
#include "Support/DepthFirstIterator.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumRemoved("globaldce\t- Number of global values removed");
static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
// Calculate which functions are reachable from the external functions in the
@ -30,6 +33,7 @@ static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
(*I)->dropAllReferences();
N->removeAllCalledMethods();
FunctionsToDelete.push_back(N);
++NumRemoved;
}
}
@ -57,6 +61,7 @@ static bool RemoveUnreachableGlobalVariables(Module *M) {
++I; // Cannot eliminate global variable
else {
delete M->getGlobalList().remove(I);
++NumRemoved;
Changed = true;
}
return Changed;

View File

@ -27,6 +27,9 @@
#include "llvm/iOther.h"
#include "llvm/Type.h"
#include "llvm/Argument.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumInlined("inline\t\t- Number of functions inlined");
#include <algorithm>
#include <iostream>
using std::cerr;
@ -258,6 +261,7 @@ static bool doFunctionInlining(Function *F) {
// Loop through now and inline instructions a basic block at a time...
for (Function::iterator I = F->begin(); I != F->end(); )
if (DoFunctionInlining(*I)) {
++NumInlined;
Changed = true;
// Iterator is now invalidated by new basic blocks inserted
I = F->begin();

View File

@ -10,6 +10,9 @@
#include "llvm/Pass.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumChanged("internalize\t- Number of functions internal'd");
class InternalizePass : public Pass {
const char *getPassName() const { return "Internalize Functions"; }
@ -28,9 +31,12 @@ class InternalizePass : public Pass {
// Found a main function, mark all functions not named main as internal.
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
if ((*I)->getName() != "main" && // Leave the main function external
!(*I)->isExternal()) // Function must be defined here
(*I)->setInternalLinkage(Changed = true);
if ((*I)->getName() != "main" && // Leave the main function external
!(*I)->isExternal()) { // Function must be defined here
(*I)->setInternalLinkage(true);
Changed = true;
++NumChanged;
}
return Changed;
}

View File

@ -13,6 +13,9 @@
#include "llvm/iMemory.h"
#include "llvm/iOther.h"
#include "llvm/Pass.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised");
namespace {
@ -91,11 +94,13 @@ bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
CI->setName("");
ReplaceInstWithInst(BIL, BI, MallocI);
Changed = true;
++NumRaised;
continue; // Skip the ++BI
} else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
Changed = true;
continue; // Skip the ++BI
++NumRaised;
}
}

View File

@ -19,6 +19,9 @@
#include "llvm/Support/InstIterator.h"
#include <set>
#include "Support/StatisticReporter.h"
static Statistic<> NumInstKilled("constprop - Number of instructions killed");
namespace {
struct ConstantPropogation : public FunctionPass {
const char *getPassName() const { return "Simple Constant Propogation"; }
@ -55,9 +58,10 @@ bool ConstantPropogation::runOnFunction(Function *F) {
// Replace all of the uses of a variable with uses of the constant.
I->replaceAllUsesWith(C);
// We made a change to the function...
Changed = true;
++NumInstKilled;
}
}
return Changed;

View File

@ -14,8 +14,12 @@
#include "llvm/Instruction.h"
#include "llvm/Pass.h"
#include "llvm/Support/InstIterator.h"
#include "Support/StatisticReporter.h"
#include <set>
static Statistic<> DIEEliminated("die\t\t- Number of insts removed");
static Statistic<> DCEEliminated("dce\t\t- Number of insts removed");
//===----------------------------------------------------------------------===//
// DeadInstElimination pass implementation
//
@ -28,9 +32,10 @@ namespace {
BasicBlock::InstListType &Vals = BB->getInstList();
bool Changed = false;
for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
if (dceInstruction(Vals, DI))
if (dceInstruction(Vals, DI)) {
Changed = true;
else
++DIEEliminated;
} else
++DI;
return Changed;
}
@ -103,6 +108,7 @@ bool DCE::runOnFunction(Function *F) {
for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); )
if (DeadInsts.count(*BI)) { // Is this instruction dead?
delete BBIL.remove(BI); // Yup, remove and delete inst
++DCEEliminated;
} else { // This instruction is not dead
++BI; // Continue on to the next one...
}

View File

@ -15,6 +15,9 @@
#include "llvm/iOther.h"
#include "llvm/BasicBlock.h"
#include "llvm/Pass.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumAdded("lowerrefs\t\t- New instructions added");
namespace {
struct DecomposePass : public BasicBlockPass {
@ -112,6 +115,7 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
if (!indexIsZero) {
LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
NewInsts.push_back(cast<Instruction>(LastPtr));
++NumAdded;
}
// Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
@ -120,6 +124,7 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
if (LastPtr->getType() != NextPtrTy) {
LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
NewInsts.push_back(cast<Instruction>(LastPtr));
++NumAdded;
}
}

View File

@ -20,8 +20,11 @@
#include "llvm/Analysis/Dominators.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/InstIterator.h"
#include "Support/StatisticReporter.h"
#include <algorithm>
static Statistic<> NumInstRemoved("gcse\t\t- Number of instructions removed");
namespace {
class GCSE : public FunctionPass, public InstVisitor<GCSE, bool> {
set<Instruction*> WorkList;
@ -131,6 +134,8 @@ void GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
WorkList.erase(Other); // Other may not actually be on the worklist anymore...
++NumInstRemoved; // Keep track of number of instructions eliminated
// Handle the easy case, where both instructions are in the same basic block
BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
if (BB1 == BB2) {

View File

@ -14,6 +14,10 @@
#include "llvm/Constants.h"
#include "llvm/Support/CFG.h"
#include "Support/STLExtras.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed");
static Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added");
#if 0
#define DEBUG
@ -38,7 +42,8 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
std::bind1st(std::ptr_fun(TransformLoop), Loops));
// Get the header node for this loop. All of the phi nodes that could be
// induction variables must live in this basic block.
BasicBlock *Header = (BasicBlock*)Loop->getBlocks().front();
//
BasicBlock *Header = Loop->getBlocks().front();
// Loop over all of the PHI nodes in the basic block, calculating the
// induction variables that they represent... stuffing the induction variable
@ -107,6 +112,7 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
"Just inserted cannonical indvar that is not cannonical!");
Cannonical = &IndVars.back();
++NumInserted;
Changed = true;
}
@ -179,20 +185,13 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
delete IV->Phi;
InsertPos--; // Deleted an instr, decrement insert position
Changed = true;
++NumRemoved;
}
}
return Changed;
}
static bool doit(Function *M, LoopInfo &Loops) {
// Induction Variables live in the header nodes of the loops of the function
return reduce_apply_bool(Loops.getTopLevelLoops().begin(),
Loops.getTopLevelLoops().end(),
std::bind1st(std::ptr_fun(TransformLoop), &Loops));
}
namespace {
struct InductionVariableSimplify : public FunctionPass {
const char *getPassName() const {
@ -200,7 +199,12 @@ namespace {
}
virtual bool runOnFunction(Function *F) {
return doit(F, getAnalysis<LoopInfo>());
LoopInfo &LI = getAnalysis<LoopInfo>();
// Induction Variables live in the header nodes of loops
return reduce_apply_bool(LI.getTopLevelLoops().begin(),
LI.getTopLevelLoops().end(),
std::bind1st(std::ptr_fun(TransformLoop), &LI));
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {

View File

@ -25,7 +25,9 @@
#include "llvm/Pass.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumCombined("instcombine\t- Number of insts combined");
namespace {
class InstCombiner : public FunctionPass,
@ -547,6 +549,7 @@ bool InstCombiner::runOnFunction(Function *F) {
// Now that we have an instruction, try combining it to simplify it...
Instruction *Result = visit(I);
if (Result) {
++NumCombined;
// Should we replace the old instruction with a new one?
if (Result != I)
ReplaceInstWithInst(I, Result);

View File

@ -15,6 +15,9 @@
#include "llvm/Constants.h"
#include "llvm/Pass.h"
#include "llvm/Target/TargetData.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
using std::vector;
namespace {
@ -81,7 +84,7 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
// Loop over all of the instructions, looking for malloc or free instructions
for (unsigned i = 0; i < BB->size(); ++i) {
for (unsigned i = 0; i != BB->size(); ++i) {
BasicBlock::InstListType &BBIL = BB->getInstList();
if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
@ -116,6 +119,7 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
MI->replaceAllUsesWith(MCast);
delete MI; // Delete the malloc inst
Changed = true;
++NumLowered;
} else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
BBIL.remove(BB->getInstList().begin()+i);
@ -132,6 +136,7 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
// Delete the old free instruction
delete FI;
Changed = true;
++NumLowered;
}
}

View File

@ -34,6 +34,9 @@
#include "llvm/iOperators.h"
#include "llvm/iPHINode.h"
#include "llvm/Support/CFG.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumInserted("pinodes\t\t- Number of Pi nodes inserted");
namespace {
struct PiNodeInserter : public FunctionPass {
@ -180,6 +183,8 @@ bool PiNodeInserter::insertPiNodeFor(Value *V, BasicBlock *Succ, Value *Rep) {
if (Rep == 0)
cast<PHINode>(Pi)->addIncoming(V, Pred);
++NumInserted;
return true;
}

View File

@ -25,6 +25,10 @@
#include "llvm/Constant.h"
#include "llvm/Support/CFG.h"
#include "Support/PostOrderIterator.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumChanged("reassociate\t- Number of insts reassociated");
static Statistic<> NumSwapped("reassociate\t- Number of insts with operands swapped");
namespace {
class Reassociate : public FunctionPass {
@ -115,6 +119,7 @@ bool Reassociate::ReassociateExpr(BinaryOperator *I) {
std::swap(LHS, RHS);
std::swap(LHSRank, RHSRank);
Changed = true;
++NumSwapped;
//cerr << "Transposed: " << I << " Result BB: " << I->getParent();
}
@ -136,6 +141,7 @@ bool Reassociate::ReassociateExpr(BinaryOperator *I) {
LHSI->setOperand(TakeOp, RHS);
I->setOperand(1, LHSI);
++NumChanged;
//cerr << "Reassociated: " << I << " Result BB: " << I->getParent();
// Since we modified the RHS instruction, make sure that we recheck it.

View File

@ -26,11 +26,14 @@
#include "llvm/Pass.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/STLExtras.h"
#include "Support/StatisticReporter.h"
#include <algorithm>
#include <set>
#include <iostream>
using std::cerr;
static Statistic<> NumInstRemoved("sccp\t\t- Number of instructions removed");
#if 0 // Enable this to get SCCP debug output
#define DEBUG_SCCP(X) X
#else
@ -315,6 +318,7 @@ bool SCCP::runOnFunction(Function *F) {
// Hey, we just changed something!
MadeChanges = true;
++NumInstRemoved;
} else {
++BI;
}

View File

@ -26,6 +26,9 @@
#include "llvm/BasicBlock.h"
#include "llvm/Constant.h"
#include "llvm/Type.h"
#include "Support/StatisticReporter.h"
static Statistic<> NumPromoted("mem2reg\t\t- Number of alloca's promoted");
using std::vector;
using std::map;
@ -187,6 +190,8 @@ bool PromotePass::runOnFunction(Function *F) {
delete I;
}
NumPromoted += Allocas.size();
// Purge data structurse so they are available the next iteration...
Allocas.clear();
AllocaLookup.clear();