Skip to content

Commit fda8605

Browse files
C++: One Unreached per function
1 parent 56bb9dc commit fda8605

File tree

5 files changed

+52
-60
lines changed

5 files changed

+52
-60
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ cached private module Cached {
2525
not oldInstruction instanceof OldIR::PhiInstruction and
2626
hasChiNode(_, oldInstruction)
2727
} or
28-
UnreachedTag(OldInstruction oldInstruction, EdgeKind kind) {
29-
// We need an `Unreached` instruction for the destination of each infeasible edge whose
30-
// predecessor is reachable.
31-
Reachability::isInfeasibleInstructionSuccessor(oldInstruction, kind)
32-
}
28+
UnreachedTag()
3329

3430
cached class InstructionTagType extends TInstructionTag {
3531
cached final string toString() {
@@ -129,11 +125,12 @@ cached private module Cached {
129125
resultType = vvar.getType() and
130126
isGLValue = false
131127
) or
132-
exists(OldInstruction oldInstruction, EdgeKind kind |
133-
oldInstruction.getFunction() = func and
134-
tag = UnreachedTag(oldInstruction, kind) and
128+
exists(OldInstruction oldInstruction |
129+
func = oldInstruction.getFunction() and
130+
Reachability::isInfeasibleInstructionSuccessor(oldInstruction, _) and
131+
tag = UnreachedTag() and
135132
opcode instanceof Opcode::Unreached and
136-
ast = oldInstruction.getSuccessor(kind).getAST() and
133+
ast = func and
137134
resultType instanceof VoidType and
138135
isGLValue = false
139136
)
@@ -265,10 +262,12 @@ cached private module Cached {
265262
exists(OldInstruction oldInstruction |
266263
oldInstruction = getOldInstruction(instruction) and
267264
(
268-
result.getTag() = UnreachedTag(oldInstruction, kind) or
269-
(
270-
result = getNewInstruction(oldInstruction.getSuccessor(kind)) and
271-
not exists(UnreachedTag(oldInstruction, kind))
265+
if Reachability::isInfeasibleInstructionSuccessor(oldInstruction, kind) then (
266+
result.getTag() = UnreachedTag() and
267+
result.getFunction() = instruction.getFunction()
268+
)
269+
else (
270+
result = getNewInstruction(oldInstruction.getSuccessor(kind))
272271
)
273272
)
274273
) or
@@ -521,9 +520,9 @@ cached private module CachedForDebugging {
521520
instr.getTag() = PhiTag(vvar, phiBlock) and
522521
result = "Phi Block(" + phiBlock.getUniqueId() + "): " + vvar.getUniqueId()
523522
) or
524-
exists(OldInstruction oldInstr, EdgeKind kind |
525-
instr.getTag() = UnreachedTag(oldInstr, kind) and
526-
result = "Unreached(" + oldInstr.getUniqueId() + ":" + kind.toString() + ")"
523+
(
524+
instr.getTag() = UnreachedTag() and
525+
result = "Unreached"
527526
)
528527
}
529528

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/SSAConstruction.qll

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ cached private module Cached {
2525
not oldInstruction instanceof OldIR::PhiInstruction and
2626
hasChiNode(_, oldInstruction)
2727
} or
28-
UnreachedTag(OldInstruction oldInstruction, EdgeKind kind) {
29-
// We need an `Unreached` instruction for the destination of each infeasible edge whose
30-
// predecessor is reachable.
31-
Reachability::isInfeasibleInstructionSuccessor(oldInstruction, kind)
32-
}
28+
UnreachedTag()
3329

3430
cached class InstructionTagType extends TInstructionTag {
3531
cached final string toString() {
@@ -129,11 +125,12 @@ cached private module Cached {
129125
resultType = vvar.getType() and
130126
isGLValue = false
131127
) or
132-
exists(OldInstruction oldInstruction, EdgeKind kind |
133-
oldInstruction.getFunction() = func and
134-
tag = UnreachedTag(oldInstruction, kind) and
128+
exists(OldInstruction oldInstruction |
129+
func = oldInstruction.getFunction() and
130+
Reachability::isInfeasibleInstructionSuccessor(oldInstruction, _) and
131+
tag = UnreachedTag() and
135132
opcode instanceof Opcode::Unreached and
136-
ast = oldInstruction.getSuccessor(kind).getAST() and
133+
ast = func and
137134
resultType instanceof VoidType and
138135
isGLValue = false
139136
)
@@ -265,10 +262,12 @@ cached private module Cached {
265262
exists(OldInstruction oldInstruction |
266263
oldInstruction = getOldInstruction(instruction) and
267264
(
268-
result.getTag() = UnreachedTag(oldInstruction, kind) or
269-
(
270-
result = getNewInstruction(oldInstruction.getSuccessor(kind)) and
271-
not exists(UnreachedTag(oldInstruction, kind))
265+
if Reachability::isInfeasibleInstructionSuccessor(oldInstruction, kind) then (
266+
result.getTag() = UnreachedTag() and
267+
result.getFunction() = instruction.getFunction()
268+
)
269+
else (
270+
result = getNewInstruction(oldInstruction.getSuccessor(kind))
272271
)
273272
)
274273
) or
@@ -521,9 +520,9 @@ cached private module CachedForDebugging {
521520
instr.getTag() = PhiTag(vvar, phiBlock) and
522521
result = "Phi Block(" + phiBlock.getUniqueId() + "): " + vvar.getUniqueId()
523522
) or
524-
exists(OldInstruction oldInstr, EdgeKind kind |
525-
instr.getTag() = UnreachedTag(oldInstr, kind) and
526-
result = "Unreached(" + oldInstr.getUniqueId() + ":" + kind.toString() + ")"
523+
(
524+
instr.getTag() = UnreachedTag() and
525+
result = "Unreached"
527526
)
528527
}
529528

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,8 @@ ir.cpp:
12141214
# 281| v2_0(void) = NoOp :
12151215
#-----| Goto -> Block 1
12161216

1217-
# 283| Block 3
1218-
# 283| v3_0(void) = Unreached :
1217+
# 278| Block 3
1218+
# 278| v3_0(void) = Unreached :
12191219

12201220
# 285| For_Update() -> void
12211221
# 285| Block 0
@@ -1260,8 +1260,8 @@ ir.cpp:
12601260
# 294| v2_0(void) = NoOp :
12611261
#-----| Goto -> Block 1
12621262

1263-
# 296| Block 3
1264-
# 296| v3_0(void) = Unreached :
1263+
# 292| Block 3
1264+
# 292| v3_0(void) = Unreached :
12651265

12661266
# 298| For_InitUpdate() -> void
12671267
# 298| Block 0
@@ -1465,7 +1465,7 @@ ir.cpp:
14651465
# 334| r1_2(int) = Constant[10] :
14661466
# 334| r1_3(bool) = CompareLT : r1_1, r1_2
14671467
# 334| v1_4(void) = ConditionalBranch : r1_3
1468-
#-----| False -> Block 5
1468+
#-----| False -> Block 4
14691469
#-----| True -> Block 2
14701470

14711471
# 335| Block 2
@@ -1481,11 +1481,8 @@ ir.cpp:
14811481
# 334| v3_0(void) = NoOp :
14821482
#-----| Goto -> Block 1
14831483

1484-
# 336| Block 4
1485-
# 336| v4_0(void) = Unreached :
1486-
1487-
# 339| Block 5
1488-
# 339| v5_0(void) = Unreached :
1484+
# 333| Block 4
1485+
# 333| v4_0(void) = Unreached :
14891486

14901487
# 341| Dereference(int *) -> int
14911488
# 341| Block 0
@@ -3225,8 +3222,8 @@ ir.cpp:
32253222
# 724| v11_1(void) = ReturnVoid :
32263223
#-----| Goto -> Block 1
32273224

3228-
# 731| Block 12
3229-
# 731| v12_0(void) = Unreached :
3225+
# 724| Block 12
3226+
# 724| v12_0(void) = Unreached :
32303227

32313228
# 745| Base::Base(const Base &) -> void
32323229
# 745| Block 0
@@ -4101,8 +4098,8 @@ ir.cpp:
41014098
# 904| v1_9(void) = UnmodeledUse : mu*
41024099
# 904| v1_10(void) = ExitFunction :
41034100

4104-
# 906| Block 2
4105-
# 906| v2_0(void) = Unreached :
4101+
# 904| Block 2
4102+
# 904| v2_0(void) = Unreached :
41064103

41074104
# 940| OperatorNew() -> void
41084105
# 940| Block 0
@@ -4607,7 +4604,7 @@ ir.cpp:
46074604
# 1040| r4_3(int) = Load : r4_2, m0_10
46084605
# 1040| r4_4(bool) = CompareLT : r4_1, r4_3
46094606
# 1040| v4_5(void) = ConditionalBranch : r4_4
4610-
#-----| False -> Block 7
4607+
#-----| False -> Block 6
46114608
#-----| True -> Block 5
46124609

46134610
# 1041| Block 5
@@ -4616,11 +4613,8 @@ ir.cpp:
46164613
# 1041| m5_2(int) = Store : r5_0, r5_1
46174614
#-----| Goto -> Block 1
46184615

4619-
# 1033| Block 6
4620-
# 1033| v6_0(void) = Unreached :
4621-
4622-
# 1044| Block 7
4623-
# 1044| v7_0(void) = Unreached :
4616+
# 1028| Block 6
4617+
# 1028| v6_0(void) = Unreached :
46244618

46254619
# 1049| DoWhileFalse() -> int
46264620
# 1049| Block 0
@@ -4650,5 +4644,5 @@ ir.cpp:
46504644
# 1049| v1_6(void) = UnmodeledUse : mu*
46514645
# 1049| v1_7(void) = ExitFunction :
46524646

4653-
# 1052| Block 2
4654-
# 1052| v2_0(void) = Unreached :
4647+
# 1049| Block 2
4648+
# 1049| v2_0(void) = Unreached :

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
| IR: For_Break | 6 |
4545
| IR: For_Condition | 4 |
4646
| IR: For_ConditionUpdate | 4 |
47-
| IR: For_Continue_NoUpdate | 6 |
47+
| IR: For_Continue_NoUpdate | 5 |
4848
| IR: For_Continue_Update | 6 |
4949
| IR: For_Empty | 2 |
5050
| IR: For_Init | 2 |
@@ -94,7 +94,7 @@
9494
| IR: TryCatch | 13 |
9595
| IR: UninitializedVariables | 1 |
9696
| IR: UnionInit | 1 |
97-
| IR: UnreachableIf | 8 |
97+
| IR: UnreachableIf | 7 |
9898
| IR: UnreachableViaGoto | 1 |
9999
| IR: VarArgUsage | 1 |
100100
| IR: VarArgs | 1 |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,8 +3991,8 @@ ir.cpp:
39913991
# 904| v1_9(void) = UnmodeledUse : mu*
39923992
# 904| v1_10(void) = ExitFunction :
39933993

3994-
# 906| Block 2
3995-
# 906| v2_0(void) = Unreached :
3994+
# 904| Block 2
3995+
# 904| v2_0(void) = Unreached :
39963996

39973997
# 940| OperatorNew() -> void
39983998
# 940| Block 0
@@ -4515,5 +4515,5 @@ ir.cpp:
45154515
# 1049| v1_6(void) = UnmodeledUse : mu*
45164516
# 1049| v1_7(void) = ExitFunction :
45174517

4518-
# 1052| Block 2
4519-
# 1052| v2_0(void) = Unreached :
4518+
# 1049| Block 2
4519+
# 1049| v2_0(void) = Unreached :

0 commit comments

Comments
 (0)