19
19
#include " llvm/ADT/SmallVector.h"
20
20
#include " llvm/ADT/StringRef.h"
21
21
#include " llvm/ADT/StringSet.h"
22
- #include " llvm/CodeGen/DroppedVariableStats.h"
23
22
#include " llvm/CodeGen/MachineBasicBlock.h"
24
23
#include " llvm/IR/BasicBlock.h"
25
24
#include " llvm/IR/DebugInfoMetadata.h"
@@ -580,6 +579,83 @@ class PrintCrashIRInstrumentation {
580
579
static void SignalHandler (void *);
581
580
};
582
581
582
+ // / A class to collect and print dropped debug information variable statistics.
583
+ // / After every LLVM IR pass is run, it will print how many #dbg_values were
584
+ // / dropped due to that pass.
585
+ class DroppedVariableStats {
586
+ public:
587
+ DroppedVariableStats (bool DroppedVarStatsEnabled) {
588
+ if (DroppedVarStatsEnabled)
589
+ llvm::outs ()
590
+ << " Pass Level, Pass Name, Num of Dropped Variables, Func or "
591
+ " Module Name\n " ;
592
+ };
593
+ // We intend this to be unique per-compilation, thus no copies.
594
+ DroppedVariableStats (const DroppedVariableStats &) = delete ;
595
+ void operator =(const DroppedVariableStats &) = delete ;
596
+
597
+ void registerCallbacks (PassInstrumentationCallbacks &PIC);
598
+ void runBeforePass (StringRef PassID, Any IR);
599
+ void runAfterPass (StringRef PassID, Any IR, const PreservedAnalyses &PA);
600
+ void runAfterPassInvalidated (StringRef PassID, const PreservedAnalyses &PA);
601
+ bool getPassDroppedVariables () { return PassDroppedVariables; }
602
+
603
+ private:
604
+ bool PassDroppedVariables = false ;
605
+ // / A unique key that represents a #dbg_value.
606
+ using VarID =
607
+ std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
608
+
609
+ struct DebugVariables {
610
+ // / DenseSet of VarIDs before an optimization pass has run.
611
+ DenseSet<VarID> DebugVariablesBefore;
612
+ // / DenseSet of VarIDs after an optimization pass has run.
613
+ DenseSet<VarID> DebugVariablesAfter;
614
+ };
615
+
616
+ // / A stack of a DenseMap, that maps DebugVariables for every pass to an
617
+ // / llvm::Function. A stack is used because an optimization pass can call
618
+ // / other passes.
619
+ SmallVector<DenseMap<const Function *, DebugVariables>> DebugVariablesStack;
620
+
621
+ // / A DenseSet tracking whether a scope was visited before.
622
+ DenseSet<const DIScope *> VisitedScope;
623
+ // / A stack of DenseMaps, which map the name of an llvm::Function to a
624
+ // / DenseMap of VarIDs and their inlinedAt locations before an optimization
625
+ // / pass has run.
626
+ SmallVector<DenseMap<StringRef, DenseMap<VarID, DILocation *>>> InlinedAts;
627
+
628
+ // / Iterate over all Functions in a Module and report any dropped debug
629
+ // / information. Will call calculateDroppedVarStatsOnFunction on every
630
+ // / Function.
631
+ void calculateDroppedVarStatsOnModule (const Module *M, StringRef PassID,
632
+ std::string FuncOrModName,
633
+ std::string PassLevel);
634
+ // / Iterate over all Instructions in a Function and report any dropped debug
635
+ // / information.
636
+ void calculateDroppedVarStatsOnFunction (const Function *F, StringRef PassID,
637
+ std::string FuncOrModName,
638
+ std::string PassLevel);
639
+ // / Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
640
+ // / after a pass has run to facilitate dropped variable calculation for an
641
+ // / llvm::Function.
642
+ void runOnFunction (const Function *F, bool Before);
643
+ // / Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
644
+ // / after a pass has run to facilitate dropped variable calculation for an
645
+ // / llvm::Module. Calls runOnFunction on every Function in the Module.
646
+ void runOnModule (const Module *M, bool Before);
647
+ // / Remove a dropped #dbg_value VarID from all Sets in the
648
+ // / DroppedVariablesBefore stack.
649
+ void removeVarFromAllSets (VarID Var, const Function *F);
650
+ // / Return true if \p Scope is the same as \p DbgValScope or a child scope of
651
+ // / \p DbgValScope, return false otherwise.
652
+ bool isScopeChildOfOrEqualTo (DIScope *Scope, const DIScope *DbgValScope);
653
+ // / Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
654
+ // / the InlinedAt chain, return false otherwise.
655
+ bool isInlinedAtChildOfOrEqualTo (const DILocation *InlinedAt,
656
+ const DILocation *DbgValInlinedAt);
657
+ };
658
+
583
659
// / This class provides an interface to register all the standard pass
584
660
// / instrumentations and manages their state (if any).
585
661
class StandardInstrumentations {
@@ -597,7 +673,7 @@ class StandardInstrumentations {
597
673
PrintCrashIRInstrumentation PrintCrashIR;
598
674
IRChangedTester ChangeTester;
599
675
VerifyInstrumentation Verify;
600
- DroppedVariableStatsIR DroppedStatsIR ;
676
+ DroppedVariableStats DroppedStats ;
601
677
602
678
bool VerifyEach;
603
679
0 commit comments