Skip to content

Commit ff6b8c4

Browse files
authored
Merge pull request #14721 from aschackmull/shared/ssareadpos-share
Java/C++/RangeAnalysis: Move SsaReadPosition to shared qlpack.
2 parents cfa47a6 + f9132c5 commit ff6b8c4

File tree

12 files changed

+227
-258
lines changed

12 files changed

+227
-258
lines changed

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/SemanticExprSpecific.qll

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -198,75 +198,15 @@ module SemanticExprConfig {
198198
result = v.asOperand().getUse().getBlock()
199199
}
200200

201-
private newtype TReadPosition =
202-
TReadPositionBlock(IR::IRBlock block) or
203-
TReadPositionPhiInputEdge(IR::IRBlock pred, IR::IRBlock succ) {
204-
exists(IR::PhiInputOperand input |
205-
pred = input.getPredecessorBlock() and
206-
succ = input.getUse().getBlock()
207-
)
208-
}
209-
210-
class SsaReadPosition extends TReadPosition {
211-
string toString() { none() }
212-
213-
Location getLocation() { none() }
214-
215-
predicate hasRead(SsaVariable v) { none() }
216-
}
217-
218-
private class SsaReadPositionBlock extends SsaReadPosition, TReadPositionBlock {
219-
IR::IRBlock block;
220-
221-
SsaReadPositionBlock() { this = TReadPositionBlock(block) }
222-
223-
final override string toString() { result = block.toString() }
224-
225-
final override Location getLocation() { result = block.getLocation() }
226-
227-
final override predicate hasRead(SsaVariable v) {
228-
exists(IR::Operand operand | operand.getDef() = v.asInstruction() |
229-
not operand instanceof IR::PhiInputOperand and
230-
operand.getUse().getBlock() = block
231-
)
232-
}
233-
}
234-
235-
private class SsaReadPositionPhiInputEdge extends SsaReadPosition, TReadPositionPhiInputEdge {
236-
IR::IRBlock pred;
237-
IR::IRBlock succ;
238-
239-
SsaReadPositionPhiInputEdge() { this = TReadPositionPhiInputEdge(pred, succ) }
240-
241-
final override string toString() { result = pred.toString() + "->" + succ.toString() }
242-
243-
final override Location getLocation() { result = succ.getLocation() }
244-
245-
final override predicate hasRead(SsaVariable v) {
246-
exists(IR::PhiInputOperand operand | operand.getDef() = v.asInstruction() |
247-
operand.getPredecessorBlock() = pred and
248-
operand.getUse().getBlock() = succ
249-
)
250-
}
251-
}
252-
253-
predicate hasReadOfSsaVariable(SsaReadPosition pos, SsaVariable v) { pos.hasRead(v) }
254-
255-
predicate readBlock(SsaReadPosition pos, BasicBlock block) { pos = TReadPositionBlock(block) }
256-
257-
predicate phiInputEdge(SsaReadPosition pos, BasicBlock origBlock, BasicBlock phiBlock) {
258-
pos = TReadPositionPhiInputEdge(origBlock, phiBlock)
259-
}
260-
261-
predicate phiInput(SsaReadPosition pos, SsaVariable phi, SsaVariable input) {
201+
/** Holds if `inp` is an input to the phi node along the edge originating in `bb`. */
202+
predicate phiInputFromBlock(SsaVariable phi, SsaVariable inp, BasicBlock bb) {
262203
exists(IR::PhiInputOperand operand |
263-
pos = TReadPositionPhiInputEdge(operand.getPredecessorBlock(), operand.getUse().getBlock())
264-
|
204+
bb = operand.getPredecessorBlock() and
265205
phi.asInstruction() = operand.getUse() and
266206
(
267-
input.asInstruction() = operand.getDef()
207+
inp.asInstruction() = operand.getDef()
268208
or
269-
input.asOperand() = operand
209+
inp.asOperand() = operand
270210
)
271211
)
272212
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/SemanticSSA.qll

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,8 @@ class SemSsaPhiNode extends SemSsaVariable {
3131
SemSsaPhiNode() { Specific::phi(this) }
3232

3333
final SemSsaVariable getAPhiInput() { result = Specific::getAPhiInput(this) }
34-
}
35-
36-
class SemSsaReadPosition instanceof Specific::SsaReadPosition {
37-
final string toString() { result = super.toString() }
38-
39-
final Specific::Location getLocation() { result = super.getLocation() }
40-
41-
final predicate hasReadOfVar(SemSsaVariable var) { Specific::hasReadOfSsaVariable(this, var) }
42-
}
43-
44-
class SemSsaReadPositionPhiInputEdge extends SemSsaReadPosition {
45-
SemBasicBlock origBlock;
46-
SemBasicBlock phiBlock;
47-
48-
SemSsaReadPositionPhiInputEdge() { Specific::phiInputEdge(this, origBlock, phiBlock) }
49-
50-
predicate phiInput(SemSsaPhiNode phi, SemSsaVariable inp) { Specific::phiInput(this, phi, inp) }
51-
52-
SemBasicBlock getOrigBlock() { result = origBlock }
53-
54-
SemBasicBlock getPhiBlock() { result = phiBlock }
55-
}
56-
57-
class SemSsaReadPositionBlock extends SemSsaReadPosition {
58-
SemBasicBlock block;
59-
60-
SemSsaReadPositionBlock() { Specific::readBlock(this, block) }
61-
62-
SemBasicBlock getBlock() { result = block }
6334

64-
SemExpr getAnExpr() { result = this.getBlock().getAnExpr() }
35+
final predicate hasInputFromBlock(SemSsaVariable inp, SemBasicBlock bb) {
36+
Specific::phiInputFromBlock(this, inp, bb)
37+
}
6538
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisImpl.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ module Sem implements Semantic {
7474

7575
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
7676

77+
int getBlockId1(BasicBlock bb) { result = bb.getUniqueId() }
78+
7779
class Guard = SemGuard;
7880

7981
predicate implies_v2 = semImplies_v2/4;
@@ -92,12 +94,6 @@ module Sem implements Semantic {
9294

9395
class SsaExplicitUpdate = SemSsaExplicitUpdate;
9496

95-
class SsaReadPosition = SemSsaReadPosition;
96-
97-
class SsaReadPositionPhiInputEdge = SemSsaReadPositionPhiInputEdge;
98-
99-
class SsaReadPositionBlock = SemSsaReadPositionBlock;
100-
10197
predicate conversionCannotOverflow(Type fromType, Type toType) {
10298
SemanticType::conversionCannotOverflow(fromType, toType)
10399
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeUtils.qll

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,33 +133,4 @@ module RangeUtil<DeltaSig D, LangSig<Sem, D> Lang> implements UtilSig<Sem, D> {
133133
or
134134
not exists(Lang::getAlternateTypeForSsaVariable(var)) and result = var.getType()
135135
}
136-
137-
import Ranking
138-
}
139-
140-
import Ranking
141-
142-
module Ranking {
143-
/**
144-
* Holds if `rix` is the number of input edges to `phi`.
145-
*/
146-
predicate maxPhiInputRank(SemSsaPhiNode phi, int rix) {
147-
rix = max(int r | rankedPhiInput(phi, _, _, r))
148-
}
149-
150-
/**
151-
* Holds if `inp` is an input to `phi` along `edge` and this input has index `r`
152-
* in an arbitrary 1-based numbering of the input edges to `phi`.
153-
*/
154-
predicate rankedPhiInput(
155-
SemSsaPhiNode phi, SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge, int r
156-
) {
157-
edge.phiInput(phi, inp) and
158-
edge =
159-
rank[r](SemSsaReadPositionPhiInputEdge e |
160-
e.phiInput(phi, _)
161-
|
162-
e order by e.getOrigBlock().getUniqueId()
163-
)
164-
}
165136
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/SignAnalysisCommon.qll

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
4545
/** An SSA Phi definition, whose sign is the union of the signs of its inputs. */
4646
private class PhiSignDef extends FlowSignDef instanceof SemSsaPhiNode {
4747
final override Sign getSign() {
48-
exists(SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge |
48+
exists(SemSsaVariable inp, SsaReadPositionPhiInputEdge edge |
4949
edge.phiInput(this, inp) and
5050
result = semSsaSign(inp, edge)
5151
)
@@ -170,11 +170,11 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
170170
override Sign getSignRestriction() {
171171
// Propagate via SSA
172172
// Propagate the sign from the def of `v`, incorporating any inference from guards.
173-
result = semSsaSign(v, any(SemSsaReadPositionBlock bb | bb.getAnExpr() = this))
173+
result = semSsaSign(v, any(SsaReadPositionBlock bb | bb.getBlock().getAnExpr() = this))
174174
or
175175
// No block for this read. Just use the sign of the def.
176176
// REVIEW: How can this happen?
177-
not exists(SemSsaReadPositionBlock bb | bb.getAnExpr() = this) and
177+
not exists(SsaReadPositionBlock bb | bb.getBlock().getAnExpr() = this) and
178178
result = semSsaDefSign(v)
179179
}
180180
}
@@ -290,7 +290,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
290290
* to only include bounds for which we might determine a sign.
291291
*/
292292
private predicate lowerBound(
293-
SemExpr lowerbound, SemSsaVariable v, SemSsaReadPosition pos, boolean isStrict
293+
SemExpr lowerbound, SemSsaVariable v, SsaReadPosition pos, boolean isStrict
294294
) {
295295
exists(boolean testIsTrue, SemRelationalExpr comp |
296296
pos.hasReadOfVar(v) and
@@ -314,7 +314,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
314314
* to only include bounds for which we might determine a sign.
315315
*/
316316
private predicate upperBound(
317-
SemExpr upperbound, SemSsaVariable v, SemSsaReadPosition pos, boolean isStrict
317+
SemExpr upperbound, SemSsaVariable v, SsaReadPosition pos, boolean isStrict
318318
) {
319319
exists(boolean testIsTrue, SemRelationalExpr comp |
320320
pos.hasReadOfVar(v) and
@@ -340,7 +340,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
340340
* - `isEq = true` : `v = eqbound`
341341
* - `isEq = false` : `v != eqbound`
342342
*/
343-
private predicate eqBound(SemExpr eqbound, SemSsaVariable v, SemSsaReadPosition pos, boolean isEq) {
343+
private predicate eqBound(SemExpr eqbound, SemSsaVariable v, SsaReadPosition pos, boolean isEq) {
344344
exists(SemGuard guard, boolean testIsTrue, boolean polarity, SemExpr e |
345345
pos.hasReadOfVar(pragma[only_bind_into](v)) and
346346
guardControlsSsaRead(guard, pragma[only_bind_into](pos), testIsTrue) and
@@ -355,7 +355,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
355355
* Holds if `bound` is a bound for `v` at `pos` that needs to be positive in
356356
* order for `v` to be positive.
357357
*/
358-
private predicate posBound(SemExpr bound, SemSsaVariable v, SemSsaReadPosition pos) {
358+
private predicate posBound(SemExpr bound, SemSsaVariable v, SsaReadPosition pos) {
359359
upperBound(bound, v, pos, _) or
360360
eqBound(bound, v, pos, true)
361361
}
@@ -364,7 +364,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
364364
* Holds if `bound` is a bound for `v` at `pos` that needs to be negative in
365365
* order for `v` to be negative.
366366
*/
367-
private predicate negBound(SemExpr bound, SemSsaVariable v, SemSsaReadPosition pos) {
367+
private predicate negBound(SemExpr bound, SemSsaVariable v, SsaReadPosition pos) {
368368
lowerBound(bound, v, pos, _) or
369369
eqBound(bound, v, pos, true)
370370
}
@@ -373,24 +373,24 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
373373
* Holds if `bound` is a bound for `v` at `pos` that can restrict whether `v`
374374
* can be zero.
375375
*/
376-
private predicate zeroBound(SemExpr bound, SemSsaVariable v, SemSsaReadPosition pos) {
376+
private predicate zeroBound(SemExpr bound, SemSsaVariable v, SsaReadPosition pos) {
377377
lowerBound(bound, v, pos, _) or
378378
upperBound(bound, v, pos, _) or
379379
eqBound(bound, v, pos, _)
380380
}
381381

382382
/** Holds if `bound` allows `v` to be positive at `pos`. */
383-
private predicate posBoundOk(SemExpr bound, SemSsaVariable v, SemSsaReadPosition pos) {
383+
private predicate posBoundOk(SemExpr bound, SemSsaVariable v, SsaReadPosition pos) {
384384
posBound(bound, v, pos) and TPos() = semExprSign(bound)
385385
}
386386

387387
/** Holds if `bound` allows `v` to be negative at `pos`. */
388-
private predicate negBoundOk(SemExpr bound, SemSsaVariable v, SemSsaReadPosition pos) {
388+
private predicate negBoundOk(SemExpr bound, SemSsaVariable v, SsaReadPosition pos) {
389389
negBound(bound, v, pos) and TNeg() = semExprSign(bound)
390390
}
391391

392392
/** Holds if `bound` allows `v` to be zero at `pos`. */
393-
private predicate zeroBoundOk(SemExpr bound, SemSsaVariable v, SemSsaReadPosition pos) {
393+
private predicate zeroBoundOk(SemExpr bound, SemSsaVariable v, SsaReadPosition pos) {
394394
lowerBound(bound, v, pos, _) and TNeg() = semExprSign(bound)
395395
or
396396
lowerBound(bound, v, pos, false) and TZero() = semExprSign(bound)
@@ -408,7 +408,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
408408
* Holds if there is a bound that might restrict whether `v` has the sign `s`
409409
* at `pos`.
410410
*/
411-
private predicate hasGuard(SemSsaVariable v, SemSsaReadPosition pos, Sign s) {
411+
private predicate hasGuard(SemSsaVariable v, SsaReadPosition pos, Sign s) {
412412
s = TPos() and posBound(_, v, pos)
413413
or
414414
s = TNeg() and negBound(_, v, pos)
@@ -421,7 +421,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
421421
* might be ruled out by a guard.
422422
*/
423423
pragma[noinline]
424-
private Sign guardedSsaSign(SemSsaVariable v, SemSsaReadPosition pos) {
424+
private Sign guardedSsaSign(SemSsaVariable v, SsaReadPosition pos) {
425425
result = semSsaDefSign(v) and
426426
pos.hasReadOfVar(v) and
427427
hasGuard(v, pos, result)
@@ -432,7 +432,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
432432
* can rule it out.
433433
*/
434434
pragma[noinline]
435-
private Sign unguardedSsaSign(SemSsaVariable v, SemSsaReadPosition pos) {
435+
private Sign unguardedSsaSign(SemSsaVariable v, SsaReadPosition pos) {
436436
result = semSsaDefSign(v) and
437437
pos.hasReadOfVar(v) and
438438
not hasGuard(v, pos, result)
@@ -443,7 +443,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
443443
* ruled out the sign but does not.
444444
* This does not check that the definition of `v` also allows the sign.
445445
*/
446-
private Sign guardedSsaSignOk(SemSsaVariable v, SemSsaReadPosition pos) {
446+
private Sign guardedSsaSignOk(SemSsaVariable v, SsaReadPosition pos) {
447447
result = TPos() and
448448
forex(SemExpr bound | posBound(bound, v, pos) | posBoundOk(bound, v, pos))
449449
or
@@ -455,7 +455,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
455455
}
456456

457457
/** Gets a possible sign for `v` at `pos`. */
458-
private Sign semSsaSign(SemSsaVariable v, SemSsaReadPosition pos) {
458+
private Sign semSsaSign(SemSsaVariable v, SsaReadPosition pos) {
459459
result = unguardedSsaSign(v, pos)
460460
or
461461
result = guardedSsaSign(v, pos) and

0 commit comments

Comments
 (0)