13
13
#ifndef SWIFT_SIL_PRINTCONTEXT_H
14
14
#define SWIFT_SIL_PRINTCONTEXT_H
15
15
16
+ #include " swift/SIL/SILDebugScope.h"
17
+ #include " swift/SIL/SILValue.h"
16
18
#include " llvm/ADT/DenseMap.h"
17
19
#include " llvm/Support/raw_ostream.h"
18
20
@@ -21,10 +23,42 @@ namespace swift {
21
23
class SILDebugScope ;
22
24
class SILInstruction ;
23
25
class SILFunction ;
26
+ class SILBasicBlock ;
24
27
25
28
// / Used as context for the SIL print functions.
26
29
class SILPrintContext {
30
+ public:
31
+ struct ID {
32
+ enum ID_Kind { SILBasicBlock, SILUndef, SSAValue } Kind;
33
+ unsigned Number;
34
+
35
+ // A stable ordering of ID objects.
36
+ bool operator <(ID Other) const {
37
+ if (unsigned (Kind) < unsigned (Other.Kind ))
38
+ return true ;
39
+ if (Number < Other.Number )
40
+ return true ;
41
+ return false ;
42
+ }
43
+
44
+ void print (raw_ostream &OS);
45
+ };
46
+
27
47
protected:
48
+ // Cache block and value identifiers for this function. This is useful in
49
+ // general for identifying entities, not just emitting textual SIL.
50
+ //
51
+ // TODO: It would be more discplined for the caller to provide a function
52
+ // context. That way it would be impossible for IDs to change meaning within
53
+ // the caller's scope.
54
+ struct SILPrintFunctionContext {
55
+ const SILFunction *F = nullptr ;
56
+ llvm::DenseMap<const SILBasicBlock *, unsigned > BlocksToIDMap;
57
+ llvm::DenseMap<const ValueBase *, unsigned > ValueToIDMap;
58
+ };
59
+
60
+ SILPrintFunctionContext FuncCtx;
61
+
28
62
llvm::raw_ostream &OutStream;
29
63
30
64
llvm::DenseMap<const SILDebugScope *, unsigned > ScopeToIDMap;
@@ -42,6 +76,11 @@ class SILPrintContext {
42
76
43
77
virtual ~SILPrintContext ();
44
78
79
+ SILPrintFunctionContext &getFuncContext (const SILFunction *F);
80
+
81
+ // Initialized block IDs from the order provided in `blocks`.
82
+ void initBlockIDs (ArrayRef<const SILBasicBlock *> Blocks);
83
+
45
84
// / Returns the output stream for printing.
46
85
llvm::raw_ostream &OS () const { return OutStream; }
47
86
@@ -51,6 +90,10 @@ class SILPrintContext {
51
90
// / Returns true if verbose SIL should be printed.
52
91
bool printVerbose () const { return Verbose; }
53
92
93
+ SILPrintContext::ID getID (const SILBasicBlock *Block);
94
+
95
+ SILPrintContext::ID getID (SILValue V);
96
+
54
97
// / Returns true if the \p Scope has and ID assigned.
55
98
bool hasScopeID (const SILDebugScope *Scope) const {
56
99
return ScopeToIDMap.count (Scope) != 0 ;
@@ -74,6 +117,8 @@ class SILPrintContext {
74
117
virtual void printInstructionCallBack (const SILInstruction *I);
75
118
};
76
119
120
+ raw_ostream &operator <<(raw_ostream &OS, SILPrintContext::ID i);
121
+
77
122
} // end namespace swift
78
123
79
124
#endif /* SWIFT_SIL_PRINTCONTEXT_H */
0 commit comments