Skip to content

Commit d5682f1

Browse files
authored
Merge pull request #7525 from MathiasVP/remove-rank-in-ssa-internals
C++: Remove `rank` aggregate in `SsaInternals`
2 parents 6d62227 + 6719540 commit d5682f1

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ private newtype TDefOrUse =
5151
TExplicitUse(Operand op) { isExplicitUse(op) } or
5252
TReturnParamIndirection(Operand op) { returnParameterIndirection(op, _) }
5353

54-
pragma[nomagic]
55-
private int getRank(DefOrUse defOrUse, IRBlock block) {
56-
defOrUse =
57-
rank[result](int i, DefOrUse cand |
58-
block.getInstruction(i) = toInstruction(cand)
59-
|
60-
cand order by i
61-
)
62-
}
63-
6454
private class DefOrUse extends TDefOrUse {
6555
/** Gets the instruction associated with this definition, if any. */
6656
Instruction asDef() { none() }
@@ -74,9 +64,10 @@ private class DefOrUse extends TDefOrUse {
7464
/** Gets the block of this definition or use. */
7565
abstract IRBlock getBlock();
7666

77-
/** Holds if this definition or use has rank `rank` in block `block`. */
78-
cached
79-
final predicate hasRankInBlock(IRBlock block, int rnk) { rnk = getRank(this, block) }
67+
/** Holds if this definition or use has index `index` in block `block`. */
68+
final predicate hasIndexInBlock(IRBlock block, int index) {
69+
block.getInstruction(index) = toInstruction(this)
70+
}
8071

8172
/** Gets the location of this element. */
8273
abstract Cpp::Location getLocation();
@@ -313,8 +304,8 @@ cached
313304
private module Cached {
314305
private predicate defUseFlow(Node nodeFrom, Node nodeTo) {
315306
exists(IRBlock bb1, int i1, IRBlock bb2, int i2, DefOrUse defOrUse, Use use |
316-
defOrUse.hasRankInBlock(bb1, i1) and
317-
use.hasRankInBlock(bb2, i2) and
307+
defOrUse.hasIndexInBlock(bb1, i1) and
308+
use.hasIndexInBlock(bb2, i2) and
318309
adjacentDefRead(_, bb1, i1, bb2, i2) and
319310
nodeFrom.asInstruction() = toInstruction(defOrUse) and
320311
flowOutOfAddressStep(use.getOperand(), nodeTo)
@@ -326,9 +317,9 @@ private module Cached {
326317
exists(IRBlock bb1, int i1, IRBlock bb2, int i2, Def def, Use use |
327318
nodeFrom.isTerminal() and
328319
def.getInstruction() = nodeFrom.getStoreInstruction() and
329-
def.hasRankInBlock(bb1, i1) and
320+
def.hasIndexInBlock(bb1, i1) and
330321
adjacentDefRead(_, bb1, i1, bb2, i2) and
331-
use.hasRankInBlock(bb2, i2) and
322+
use.hasIndexInBlock(bb2, i2) and
332323
flowOutOfAddressStep(use.getOperand(), nodeTo)
333324
)
334325
or
@@ -359,8 +350,8 @@ private module Cached {
359350

360351
private predicate fromReadNode(ReadNode nodeFrom, Node nodeTo) {
361352
exists(IRBlock bb1, int i1, IRBlock bb2, int i2, Use use1, Use use2 |
362-
use1.hasRankInBlock(bb1, i1) and
363-
use2.hasRankInBlock(bb2, i2) and
353+
use1.hasIndexInBlock(bb1, i1) and
354+
use2.hasIndexInBlock(bb2, i2) and
364355
use1.getOperand().getDef() = nodeFrom.getInstruction() and
365356
adjacentDefRead(_, bb1, i1, bb2, i2) and
366357
flowOutOfAddressStep(use2.getOperand(), nodeTo)
@@ -371,15 +362,15 @@ private module Cached {
371362
exists(PhiNode phi, Use use, IRBlock block, int rnk |
372363
phi = nodeFrom.getPhiNode() and
373364
adjacentDefRead(phi, _, _, block, rnk) and
374-
use.hasRankInBlock(block, rnk) and
365+
use.hasIndexInBlock(block, rnk) and
375366
flowOutOfAddressStep(use.getOperand(), nodeTo)
376367
)
377368
}
378369

379370
private predicate toPhiNode(Node nodeFrom, SsaPhiNode nodeTo) {
380371
// Flow to phi nodes
381372
exists(Def def, IRBlock block, int rnk |
382-
def.hasRankInBlock(block, rnk) and
373+
def.hasIndexInBlock(block, rnk) and
383374
nodeTo.hasInputAtRankInBlock(block, rnk)
384375
|
385376
exists(StoreNodeInstr storeNode |
@@ -512,8 +503,8 @@ private module Cached {
512503
|
513504
store = def.getInstruction() and
514505
store.getSourceValueOperand() = operand and
515-
def.hasRankInBlock(block1, rnk1) and
516-
use.hasRankInBlock(block2, rnk2) and
506+
def.hasIndexInBlock(block1, rnk1) and
507+
use.hasIndexInBlock(block2, rnk2) and
517508
adjacentDefRead(_, block1, rnk1, block2, rnk2)
518509
|
519510
// The shared SSA library has determined that `use` is the next use of the operand
@@ -543,12 +534,12 @@ private module Cached {
543534
not operand = getSourceAddressOperand(_) and
544535
exists(Use use1, Use use2, IRBlock block1, int rnk1, IRBlock block2, int rnk2 |
545536
use1.getOperand() = operand and
546-
use1.hasRankInBlock(block1, rnk1) and
537+
use1.hasIndexInBlock(block1, rnk1) and
547538
// Don't flow to the next use if this use is part of a store operation that totally
548539
// overrides a variable.
549540
not explicitWrite(true, _, use1.getOperand().getDef()) and
550541
adjacentDefRead(_, block1, rnk1, block2, rnk2) and
551-
use2.hasRankInBlock(block2, rnk2) and
542+
use2.hasIndexInBlock(block2, rnk2) and
552543
flowOutOfAddressStep(use2.getOperand(), nodeTo)
553544
)
554545
or
@@ -620,7 +611,7 @@ import Cached
620611
predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
621612
DataFlowImplCommon::forceCachingInSameStage() and
622613
exists(Def def |
623-
def.hasRankInBlock(bb, i) and
614+
def.hasIndexInBlock(bb, i) and
624615
v = def.getSourceVariable() and
625616
(if def.isCertain() then certain = true else certain = false)
626617
)
@@ -632,7 +623,7 @@ predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
632623
*/
633624
predicate variableRead(IRBlock bb, int i, SourceVariable v, boolean certain) {
634625
exists(Use use |
635-
use.hasRankInBlock(bb, i) and
626+
use.hasIndexInBlock(bb, i) and
636627
v = use.getSourceVariable() and
637628
certain = true
638629
)

0 commit comments

Comments
 (0)