Skip to content

Commit 67a0112

Browse files
authored
Merge pull request #14115 from MathiasVP/fix-incorrect-load-of-constant
C++: Don't generate `Load`s for constant expressions
2 parents da64ea4 + 72d9812 commit 67a0112

File tree

7 files changed

+67
-10
lines changed

7 files changed

+67
-10
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,9 @@ predicate hasTranslatedLoad(Expr expr) {
415415
not ignoreExpr(expr) and
416416
not isNativeCondition(expr) and
417417
not isFlexibleCondition(expr) and
418-
not ignoreLoad(expr)
418+
not ignoreLoad(expr) and
419+
// don't insert a load since we'll just substitute the constant value.
420+
not isIRConstant(expr)
419421
}
420422

421423
/**

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15716,6 +15716,30 @@ ir.cpp:
1571615716
# 2065| Type = [PointerType] Derived2 *
1571715717
# 2065| ValueCategory = prvalue(load)
1571815718
# 2066| getStmt(6): [ReturnStmt] return ...
15719+
# 2068| [TopLevelFunction] void test_constant_folding_use(int)
15720+
# 2068| <params>:
15721+
# 2068| getParameter(0): [Parameter] (unnamed parameter 0)
15722+
# 2068| Type = [IntType] int
15723+
# 2070| [TopLevelFunction] void test_constant_folding()
15724+
# 2070| <params>:
15725+
# 2070| getEntryPoint(): [BlockStmt] { ... }
15726+
# 2071| getStmt(0): [DeclStmt] declaration
15727+
# 2071| getDeclarationEntry(0): [VariableDeclarationEntry] definition of x
15728+
# 2071| Type = [SpecifiedType] const int
15729+
# 2071| getVariable().getInitializer(): [Initializer] initializer for x
15730+
# 2071| getExpr(): [Literal] 116
15731+
# 2071| Type = [IntType] int
15732+
# 2071| Value = [Literal] 116
15733+
# 2071| ValueCategory = prvalue
15734+
# 2072| getStmt(1): [ExprStmt] ExprStmt
15735+
# 2072| getExpr(): [FunctionCall] call to test_constant_folding_use
15736+
# 2072| Type = [VoidType] void
15737+
# 2072| ValueCategory = prvalue
15738+
# 2072| getArgument(0): [VariableAccess] x
15739+
# 2072| Type = [IntType] int
15740+
# 2072| Value = [VariableAccess] 116
15741+
# 2072| ValueCategory = prvalue(load)
15742+
# 2073| getStmt(2): [ReturnStmt] return ...
1571915743
perf-regression.cpp:
1572015744
# 4| [CopyAssignmentOperator] Big& Big::operator=(Big const&)
1572115745
# 4| <params>:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,5 +2065,11 @@ int virtual_delete()
20652065
delete d;
20662066
}
20672067

2068+
void test_constant_folding_use(int);
2069+
2070+
void test_constant_folding() {
2071+
const int x = 116;
2072+
test_constant_folding_use(x);
2073+
}
20682074

20692075
// semmle-extractor-options: -std=c++17 --clang

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9796,6 +9796,16 @@
97969796
| ir.cpp:2065:12:2065:12 | Address | &:r2065_2 |
97979797
| ir.cpp:2065:12:2065:12 | Arg(0) | 0:r2065_3 |
97989798
| ir.cpp:2065:12:2065:12 | Load | m2064_15 |
9799+
| ir.cpp:2070:6:2070:26 | ChiPartial | partial:m2070_3 |
9800+
| ir.cpp:2070:6:2070:26 | ChiTotal | total:m2070_2 |
9801+
| ir.cpp:2070:6:2070:26 | SideEffect | ~m2072_5 |
9802+
| ir.cpp:2071:13:2071:13 | Address | &:r2071_1 |
9803+
| ir.cpp:2071:16:2071:19 | StoreValue | r2071_2 |
9804+
| ir.cpp:2072:3:2072:27 | CallTarget | func:r2072_1 |
9805+
| ir.cpp:2072:3:2072:27 | ChiPartial | partial:m2072_4 |
9806+
| ir.cpp:2072:3:2072:27 | ChiTotal | total:m2070_4 |
9807+
| ir.cpp:2072:3:2072:27 | SideEffect | ~m2070_4 |
9808+
| ir.cpp:2072:29:2072:29 | Arg(0) | 0:r2072_2 |
97999809
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
98009810
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
98019811
| perf-regression.cpp:6:3:6:5 | Address | &:r6_7 |

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11303,6 +11303,23 @@ ir.cpp:
1130311303
# 2056| v2056_6(void) = AliasedUse : ~m?
1130411304
# 2056| v2056_7(void) = ExitFunction :
1130511305

11306+
# 2070| void test_constant_folding()
11307+
# 2070| Block 0
11308+
# 2070| v2070_1(void) = EnterFunction :
11309+
# 2070| mu2070_2(unknown) = AliasedDefinition :
11310+
# 2070| mu2070_3(unknown) = InitializeNonLocal :
11311+
# 2071| r2071_1(glval<int>) = VariableAddress[x] :
11312+
# 2071| r2071_2(int) = Constant[116] :
11313+
# 2071| mu2071_3(int) = Store[x] : &:r2071_1, r2071_2
11314+
# 2072| r2072_1(glval<unknown>) = FunctionAddress[test_constant_folding_use] :
11315+
# 2072| r2072_2(int) = Constant[116] :
11316+
# 2072| v2072_3(void) = Call[test_constant_folding_use] : func:r2072_1, 0:r2072_2
11317+
# 2072| mu2072_4(unknown) = ^CallSideEffect : ~m?
11318+
# 2073| v2073_1(void) = NoOp :
11319+
# 2070| v2070_4(void) = ReturnVoid :
11320+
# 2070| v2070_5(void) = AliasedUse : ~m?
11321+
# 2070| v2070_6(void) = ExitFunction :
11322+
1130611323
perf-regression.cpp:
1130711324
# 6| void Big::Big()
1130811325
# 6| Block 0

cpp/ql/test/library-tests/ir/modulus-analysis/test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void m(int i, bool cond, int x, int y) {
1010

1111
int seven = 7;
1212
if (mul % c2 == seven) {
13-
mod(mul); // congruent 3 mod 42, 7 mod 43
13+
mod(mul); // $ mod=0,3,42
1414
}
1515

1616
int j = cond
@@ -19,13 +19,11 @@ void m(int i, bool cond, int x, int y) {
1919
mod(j); // $ mod=0,3,4
2020

2121
if (x % c1 == 3 && y % c1 == 7) {
22-
// Need implies_v2
23-
mod(x + y); // $ MISSING: 0,10,42
22+
mod(x + y); // $ mod=0,10,42
2423
}
2524

2625
if (x % c1 == 3 && y % c1 == 7) {
27-
// Need implies_v2
28-
mod(x - y); // $ MISSING: mod=0,38,42
26+
mod(x - y); // $ mod=0,38,42
2927
}
3028

3129
if (cond) {

cpp/ql/test/library-tests/ir/range-analysis/SimpleRangeAnalysis_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,12 @@ void test(int x) {
10081008

10091009
void test_overflow() {
10101010
const int x = 2147483647; // 2^31-1
1011-
range(x);
1011+
range(x); // $ range===2147483647
10121012
const int y = 256;
1013-
range(y);
1013+
range(y); // $ range===256
10141014
if ((x + y) <= 512) {
1015-
range(x);
1016-
range(y);
1015+
range(x); // $ range===2147483647
1016+
range(y); // $ range===256
10171017
range(x + y); // $ range===-2147483393
10181018
}
10191019
}

0 commit comments

Comments
 (0)