Skip to content

Commit 9963270

Browse files
committed
C++: Annotate back edges in IR debug output
1 parent 62509ff commit 9963270

File tree

7 files changed

+72
-62
lines changed

7 files changed

+72
-62
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key,
287287
(
288288
(
289289
key = "semmle.label" and
290-
value = kind.toString()
290+
if predBlock.getBackEdgeSuccessor(kind) = succBlock
291+
then value = kind.toString() + " (back edge)"
292+
else value = kind.toString()
291293
) or
292294
(
293295
key = "semmle.order" and

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/PrintIR.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key,
287287
(
288288
(
289289
key = "semmle.label" and
290-
value = kind.toString()
290+
if predBlock.getBackEdgeSuccessor(kind) = succBlock
291+
then value = kind.toString() + " (back edge)"
292+
else value = kind.toString()
291293
) or
292294
(
293295
key = "semmle.order" and

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ cached private module Cached {
112112
// - Restrict to edges that originate within the loop (/ 2).
113113
pragma[noopt]
114114
cached Instruction getInstructionBackEdgeSuccessor(Instruction instruction, EdgeKind kind) {
115+
// While loop:
115116
// Any edge from within the body of the loop to the condition of the loop
116117
// is a back edge. This includes edges from `continue` and the fall-through
117118
// edge(s) after the last instruction(s) in the body.
@@ -125,6 +126,7 @@ cached private module Cached {
125126
)
126127
)
127128
or
129+
// Do-while loop:
128130
// The back edge should be the edge(s) from the condition to the
129131
// body. This ensures that it's the back edge that will be pruned in a `do
130132
// { ... } while (0)` statement. Note that all `continue` statements in a
@@ -141,11 +143,12 @@ cached private module Cached {
141143
)
142144
)
143145
or
146+
// For loop:
144147
// Any edge from within the body or update of the loop to the condition of
145-
// the loop is a back edge. This includes edges from `continue` and the
146-
// fall-through edge(s) after the last instruction(s) in the body. A `for`
147-
// loop may not have a condition, in which case
148-
// `getFirstConditionInstruction` returns the body instead.
148+
// the loop is a back edge. When there is no loop update expression, this
149+
// includes edges from `continue` and the fall-through edge(s) after the
150+
// last instruction(s) in the body. A for loop may not have a condition, in
151+
// which case `getFirstConditionInstruction` returns the body instead.
149152
exists(TranslatedForStmt s |
150153
s instanceof TranslatedForStmt and
151154
result = s.getFirstConditionInstruction() and
@@ -162,6 +165,7 @@ cached private module Cached {
162165
)
163166
)
164167
or
168+
// Goto statement:
165169
// As a conservative approximation, any edge out of `goto` is a back edge
166170
// unless it goes strictly forward in the program text. A `goto` whose
167171
// source and target are both inside a macro will be seen as having the

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/PrintIR.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ query predicate edges(PrintableIRBlock pred, PrintableIRBlock succ, string key,
287287
(
288288
(
289289
key = "semmle.label" and
290-
value = kind.toString()
290+
if predBlock.getBackEdgeSuccessor(kind) = succBlock
291+
then value = kind.toString() + " (back edge)"
292+
else value = kind.toString()
291293
) or
292294
(
293295
key = "semmle.order" and

cpp/ql/test/library-tests/ir/ir/aliased_ssa_ir.expected

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ ir.cpp:
11161116
# 255| r1_2(int) = Load : r1_1, m3_0
11171117
# 255| r1_3(int) = Sub : r1_2, r1_0
11181118
# 255| m1_4(int) = Store : r1_1, r1_3
1119-
#-----| Goto -> Block 3
1119+
#-----| Goto (back edge) -> Block 3
11201120

11211121
# 257| Block 2
11221122
# 257| v2_0(void) = NoOp :
@@ -1156,7 +1156,7 @@ ir.cpp:
11561156
# 262| r1_9(bool) = CompareGT : r1_7, r1_8
11571157
# 262| v1_10(void) = ConditionalBranch : r1_9
11581158
#-----| False -> Block 2
1159-
#-----| True -> Block 1
1159+
#-----| True (back edge) -> Block 1
11601160

11611161
# 263| Block 2
11621162
# 263| v2_0(void) = NoOp :
@@ -1175,7 +1175,7 @@ ir.cpp:
11751175

11761176
# 268| Block 1
11771177
# 268| v1_0(void) = NoOp :
1178-
#-----| Goto -> Block 1
1178+
#-----| Goto (back edge) -> Block 1
11791179

11801180
# 272| For_Init() -> void
11811181
# 272| Block 0
@@ -1189,7 +1189,7 @@ ir.cpp:
11891189

11901190
# 274| Block 1
11911191
# 274| v1_0(void) = NoOp :
1192-
#-----| Goto -> Block 1
1192+
#-----| Goto (back edge) -> Block 1
11931193

11941194
# 278| For_Condition() -> void
11951195
# 278| Block 0
@@ -1212,7 +1212,7 @@ ir.cpp:
12121212

12131213
# 281| Block 2
12141214
# 281| v2_0(void) = NoOp :
1215-
#-----| Goto -> Block 1
1215+
#-----| Goto (back edge) -> Block 1
12161216

12171217
# 278| Block 3
12181218
# 278| v3_0(void) = Unreached :
@@ -1235,7 +1235,7 @@ ir.cpp:
12351235
# 287| r1_4(int) = Load : r1_3, m1_0
12361236
# 287| r1_5(int) = Add : r1_4, r1_2
12371237
# 287| m1_6(int) = Store : r1_3, r1_5
1238-
#-----| Goto -> Block 1
1238+
#-----| Goto (back edge) -> Block 1
12391239

12401240
# 292| For_InitCondition() -> void
12411241
# 292| Block 0
@@ -1258,7 +1258,7 @@ ir.cpp:
12581258

12591259
# 294| Block 2
12601260
# 294| v2_0(void) = NoOp :
1261-
#-----| Goto -> Block 1
1261+
#-----| Goto (back edge) -> Block 1
12621262

12631263
# 292| Block 3
12641264
# 292| v3_0(void) = Unreached :
@@ -1281,7 +1281,7 @@ ir.cpp:
12811281
# 299| r1_4(int) = Load : r1_3, m1_0
12821282
# 299| r1_5(int) = Add : r1_4, r1_2
12831283
# 299| m1_6(int) = Store : r1_3, r1_5
1284-
#-----| Goto -> Block 1
1284+
#-----| Goto (back edge) -> Block 1
12851285

12861286
# 304| For_ConditionUpdate() -> void
12871287
# 304| Block 0
@@ -1310,7 +1310,7 @@ ir.cpp:
13101310
# 306| r2_3(int) = Load : r2_2, m1_0
13111311
# 306| r2_4(int) = Add : r2_3, r2_1
13121312
# 306| m2_5(int) = Store : r2_2, r2_4
1313-
#-----| Goto -> Block 1
1313+
#-----| Goto (back edge) -> Block 1
13141314

13151315
# 309| Block 3
13161316
# 309| v3_0(void) = NoOp :
@@ -1345,7 +1345,7 @@ ir.cpp:
13451345
# 312| r2_3(int) = Load : r2_2, m1_0
13461346
# 312| r2_4(int) = Add : r2_3, r2_1
13471347
# 312| m2_5(int) = Store : r2_2, r2_4
1348-
#-----| Goto -> Block 1
1348+
#-----| Goto (back edge) -> Block 1
13491349

13501350
# 315| Block 3
13511351
# 315| v3_0(void) = NoOp :
@@ -1379,7 +1379,7 @@ ir.cpp:
13791379
# 318| r2_2(int) = Load : r2_1, m1_0
13801380
# 318| r2_3(int) = Add : r2_2, r2_0
13811381
# 318| m2_4(int) = Store : r2_1, r2_3
1382-
#-----| Goto -> Block 1
1382+
#-----| Goto (back edge) -> Block 1
13831383

13841384
# 319| Block 3
13851385
# 319| r3_0(glval<int>) = VariableAddress[i] :
@@ -1441,7 +1441,7 @@ ir.cpp:
14411441
# 326| r4_3(int) = Load : r4_2, m1_0
14421442
# 326| r4_4(int) = Add : r4_3, r4_1
14431443
# 326| m4_5(int) = Store : r4_2, r4_4
1444-
#-----| Goto -> Block 1
1444+
#-----| Goto (back edge) -> Block 1
14451445

14461446
# 331| Block 5
14471447
# 331| v5_0(void) = NoOp :
@@ -1479,7 +1479,7 @@ ir.cpp:
14791479

14801480
# 334| Block 3
14811481
# 334| v3_0(void) = NoOp :
1482-
#-----| Goto -> Block 1
1482+
#-----| Goto (back edge) -> Block 1
14831483

14841484
# 333| Block 4
14851485
# 333| v4_0(void) = Unreached :
@@ -1547,7 +1547,7 @@ ir.cpp:
15471547
# 356| r3_2(int) = Load : r3_1, m5_0
15481548
# 356| r3_3(int) = Sub : r3_2, r3_0
15491549
# 356| m3_4(int) = Store : r3_1, r3_3
1550-
#-----| Goto -> Block 5
1550+
#-----| Goto (back edge) -> Block 5
15511551

15521552
# 357| Block 4
15531553
# 357| v4_0(void) = NoOp :
@@ -1606,7 +1606,7 @@ ir.cpp:
16061606
# 366| r4_5(bool) = CompareGT : r4_3, r4_4
16071607
# 366| v4_6(void) = ConditionalBranch : r4_5
16081608
#-----| False -> Block 5
1609-
#-----| True -> Block 1
1609+
#-----| True (back edge) -> Block 1
16101610

16111611
# 367| Block 5
16121612
# 367| v5_0(void) = NoOp :
@@ -4395,7 +4395,7 @@ ir.cpp:
43954395

43964396
# 979| Block 1
43974397
# 979| v1_0(void) = NoOp :
4398-
#-----| Goto -> Block 7
4398+
#-----| Goto (back edge) -> Block 7
43994399

44004400
# 981| Block 2
44014401
# 981| r2_0(glval<int>) = VariableAddress[z] :
@@ -4415,7 +4415,7 @@ ir.cpp:
44154415

44164416
# 981| Block 3
44174417
# 981| v3_0(void) = NoOp :
4418-
#-----| Goto -> Block 2
4418+
#-----| Goto (back edge) -> Block 2
44194419

44204420
# 983| Block 4
44214421
# 983| r4_0(glval<int *>) = VariableAddress[p] :
@@ -4431,7 +4431,7 @@ ir.cpp:
44314431

44324432
# 983| Block 5
44334433
# 983| v5_0(void) = NoOp :
4434-
#-----| Goto -> Block 4
4434+
#-----| Goto (back edge) -> Block 4
44354435

44364436
# 985| Block 6
44374437
# 985| v6_0(void) = NoOp :

0 commit comments

Comments
 (0)