Skip to content

Commit 567eee1

Browse files
author
Robert Marsh
committed
C++: allow phi nodes to self-bound
1 parent b2cd9a2 commit 567eee1

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

cpp/ql/src/semmle/code/cpp/rangeanalysis/RangeAnalysis.qll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private import RangeAnalysisCache
118118
import RangeAnalysisPublic
119119

120120
/**
121-
* Gets a condition that tests whether `op` equals `bound + delta`.
121+
* Gets a condition that tests whether `vn` equals `bound + delta`.
122122
*
123123
* If the condition evaluates to `testIsTrue`:
124124
* - `isEq = true` : `i == bound + delta`
@@ -151,11 +151,11 @@ private predicate boundFlowStepSsa(
151151
}
152152

153153
/**
154-
* Gets a condition that tests whether `op` is bounded by `bound + delta`.
154+
* Gets a condition that tests whether `vn` is bounded by `bound + delta`.
155155
*
156156
* If the condition evaluates to `testIsTrue`:
157-
* - `upper = true` : `op <= bound + delta`
158-
* - `upper = false` : `op >= bound + delta`
157+
* - `upper = true` : `vn <= bound + delta`
158+
* - `upper = false` : `vn >= bound + delta`
159159
*/
160160
private IRGuardCondition boundFlowCond(ValueNumber vn, NonPhiOperand bound, int delta, boolean upper,
161161
boolean testIsTrue)
@@ -549,7 +549,6 @@ private predicate boundedInstruction(
549549
boundedPhiCandValidForEdge(i, b, delta, upper, fromBackEdge, origdelta, reason, op)
550550
)
551551
or
552-
not i instanceof PhiInstruction and
553552
i = b.getInstruction(delta) and
554553
(upper = true or upper = false) and
555554
fromBackEdge = false and

cpp/ql/test/library-tests/rangeanalysis/rangeanalysis/RangeAnalysis.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@
3535
| test.cpp:97:10:97:10 | Load: x | file://:0:0:0:0 | 0 | 1 | false | CompareLT: ... < ... | test.cpp:94:7:94:11 | test.cpp:94:7:94:11 |
3636
| test.cpp:100:10:100:10 | Load: x | file://:0:0:0:0 | 0 | 1 | true | CompareLE: ... <= ... | test.cpp:99:7:99:12 | test.cpp:99:7:99:12 |
3737
| test.cpp:102:10:102:10 | Load: x | file://:0:0:0:0 | 0 | 2 | false | CompareLE: ... <= ... | test.cpp:99:7:99:12 | test.cpp:99:7:99:12 |
38+
| test.cpp:106:5:106:10 | Phi: test10 | test.cpp:113:3:113:6 | Phi: call to sink | -1 | true | CompareLT: ... < ... | test.cpp:114:18:114:22 | test.cpp:114:18:114:22 |
39+
| test.cpp:106:5:106:10 | Phi: test10 | test.cpp:114:18:114:18 | Phi: i | 0 | false | NoReason | file://:0:0:0:0 | file://:0:0:0:0 |
40+
| test.cpp:106:5:106:10 | Phi: test10 | test.cpp:114:18:114:18 | Phi: i | 0 | true | NoReason | file://:0:0:0:0 | file://:0:0:0:0 |

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,16 @@ void test9(int x) {
102102
sink(x);
103103
}
104104
}
105+
106+
int test10(int y, int z, bool use_y) {
107+
int x;
108+
if(use_y) {
109+
x = y;
110+
} else {
111+
x = z;
112+
}
113+
sink();
114+
for(int i = 0; i < x; i++) {
115+
return i;
116+
}
117+
}

0 commit comments

Comments
 (0)