1 //===- PassTimingInfo.h - pass execution timing -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This header defines classes/functions to handle pass execution timing
12 /// information with interfaces for both pass managers.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_PASSTIMINGINFO_H
17 #define LLVM_IR_PASSTIMINGINFO_H
19 #include "llvm/ADT/Any.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/Timer.h"
24 #include "llvm/Support/TypeName.h"
29 class PassInstrumentationCallbacks;
31 /// If -time-passes has been specified, report the timings immediately and then
32 /// reset the timers to zero.
33 void reportAndResetTimings();
35 /// Request the timer for this legacy-pass-manager's pass instance.
36 Timer *getPassTimer(Pass *);
38 /// If the user specifies the -time-passes argument on an LLVM tool command line
39 /// then the value of this boolean will be true, otherwise false.
40 /// This is the storage for the -time-passes option.
41 extern bool TimePassesIsEnabled;
43 /// This class implements -time-passes functionality for new pass manager.
44 /// It provides the pass-instrumentation callbacks that measure the pass
45 /// execution time. They collect timing info into individual timers as
46 /// passes are being run. At the end of its life-time it prints the resulting
48 class TimePassesHandler {
49 /// Value of this type is capable of uniquely identifying pass invocations.
50 /// It is a pair of string Pass-Identifier (which for now is common
51 /// to all the instance of a given pass) + sequential invocation counter.
52 using PassInvocationID = std::pair<StringRef, unsigned>;
54 /// A group of all pass-timing timers.
57 /// Map of timers for pass invocations
58 DenseMap<PassInvocationID, std::unique_ptr<Timer>> TimingData;
60 /// Map that counts invocations of passes, for use in UniqPassID construction.
61 StringMap<unsigned> PassIDCountMap;
63 /// Stack of currently active timers.
64 SmallVector<Timer *, 8> TimerStack;
69 TimePassesHandler(bool Enabled = TimePassesIsEnabled);
71 /// Destructor handles the print action if it has not been handled before.
72 ~TimePassesHandler() {
73 // First destroying the timers from TimingData, which deploys all their
74 // collected data into the TG time group member, which later prints itself
75 // when being destroyed.
79 /// Prints out timing information and then resets the timers.
82 // We intend this to be unique per-compilation, thus no copies.
83 TimePassesHandler(const TimePassesHandler &) = delete;
84 void operator=(const TimePassesHandler &) = delete;
86 void registerCallbacks(PassInstrumentationCallbacks &PIC);
89 /// Dumps information for running/triggered timers, useful for debugging
90 LLVM_DUMP_METHOD void dump() const;
92 /// Returns the new timer for each new run of the pass.
93 Timer &getPassTimer(StringRef PassID);
95 /// Returns the incremented counter for the next invocation of \p PassID.
96 unsigned nextPassID(StringRef PassID) { return ++PassIDCountMap[PassID]; }
98 void startTimer(StringRef PassID);
99 void stopTimer(StringRef PassID);
101 // Implementation of pass instrumentation callbacks.
102 bool runBeforePass(StringRef PassID);
103 void runAfterPass(StringRef PassID);