Skip to content

Commit 36ad6b3

Browse files
authored
Merge pull request #5001 from hvitved/csharp/ssa/read-kind
C#: Remove `ReadKind` from the shared SSA library
2 parents 0ba610f + e7b43e5 commit 36ad6b3

File tree

4 files changed

+42
-46
lines changed

4 files changed

+42
-46
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/SSA.qll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,11 @@ module Ssa {
233233
* node between lines 9 and 10.
234234
*/
235235
final AssignableRead getAReadAtNode(ControlFlow::Node cfn) {
236-
exists(ControlFlow::BasicBlock bb, int i |
237-
SsaImpl::ssaDefReachesRead(_, this, bb, i, SsaImpl::ActualRead()) and
236+
exists(SourceVariable v, ControlFlow::BasicBlock bb, int i |
237+
SsaImpl::ssaDefReachesRead(v, this, bb, i) and
238238
cfn = bb.getNode(i) and
239-
result.getAControlFlowNode() = cfn
239+
cfn = result.getAControlFlowNode() and
240+
result = v.getAnAccess().(AssignableRead)
240241
)
241242
}
242243

csharp/ql/src/semmle/code/csharp/dataflow/internal/SsaImpl.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,12 @@ private module Cached {
11141114
Ssa::ExplicitDefinition def, Ssa::ImplicitEntryDefinition edef,
11151115
ControlFlow::Nodes::ElementNode c, boolean additionalCalls
11161116
) {
1117-
exists(Definition def0, ControlFlow::BasicBlock bb, int i |
1118-
capturedReadIn(_, _, def.getSourceVariable(), edef.getSourceVariable(), c, additionalCalls) and
1117+
exists(Ssa::SourceVariable v, Definition def0, ControlFlow::BasicBlock bb, int i |
1118+
v = def.getSourceVariable() and
1119+
capturedReadIn(_, _, v, edef.getSourceVariable(), c, additionalCalls) and
11191120
def = def0.getAnUltimateDefinition() and
1120-
ssaDefReachesRead(_, def0, bb, i, CapturedVarCallRead()) and
1121+
ssaDefReachesRead(_, def0, bb, i) and
1122+
variableRead(bb, i, v, CapturedVarCallRead()) and
11211123
c = bb.getNode(i)
11221124
)
11231125
}

csharp/ql/src/semmle/code/csharp/dataflow/internal/SsaImplCommon.qll

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ private module Cached {
1919
* (certain or uncertain) writes.
2020
*/
2121
private newtype TRefKind =
22-
Read(ReadKind rk) or
22+
Read() or
2323
Write(boolean certain) { certain = true or certain = false }
2424

2525
private class RefKind extends TRefKind {
2626
string toString() {
27-
exists(ReadKind rk | this = Read(rk) and result = "read (" + rk + ")")
27+
this = Read() and result = "read"
2828
or
2929
exists(boolean certain | this = Write(certain) and result = "write (" + certain + ")")
3030
}
3131

3232
int getOrder() {
33-
this = Read(_) and
33+
this = Read() and
3434
result = 0
3535
or
3636
this = Write(_) and
@@ -42,7 +42,7 @@ private module Cached {
4242
* Holds if the `i`th node of basic block `bb` is a reference to `v` of kind `k`.
4343
*/
4444
private predicate ref(BasicBlock bb, int i, SourceVariable v, RefKind k) {
45-
exists(ReadKind rk | variableRead(bb, i, v, rk) | k = Read(rk))
45+
variableRead(bb, i, v) and k = Read()
4646
or
4747
exists(boolean certain | variableWrite(bb, i, v, certain) | k = Write(certain))
4848
}
@@ -94,40 +94,38 @@ private module Cached {
9494

9595
/**
9696
* Holds if source variable `v` is live at the beginning of basic block `bb`.
97-
* The read that witnesses the liveness of `v` is of kind `rk`.
9897
*/
99-
predicate liveAtEntry(BasicBlock bb, SourceVariable v, ReadKind rk) {
98+
predicate liveAtEntry(BasicBlock bb, SourceVariable v) {
10099
// The first read or certain write to `v` inside `bb` is a read
101-
refRank(bb, _, v, Read(rk)) = firstReadOrCertainWrite(bb, v)
100+
refRank(bb, _, v, Read()) = firstReadOrCertainWrite(bb, v)
102101
or
103102
// There is no certain write to `v` inside `bb`, but `v` is live at entry
104103
// to a successor basic block of `bb`
105104
not exists(firstReadOrCertainWrite(bb, v)) and
106-
liveAtExit(bb, v, rk)
105+
liveAtExit(bb, v)
107106
}
108107

109108
/**
110109
* Holds if source variable `v` is live at the end of basic block `bb`.
111-
* The read that witnesses the liveness of `v` is of kind `rk`.
112110
*/
113-
predicate liveAtExit(BasicBlock bb, SourceVariable v, ReadKind rk) {
114-
liveAtEntry(getABasicBlockSuccessor(bb), v, rk)
111+
predicate liveAtExit(BasicBlock bb, SourceVariable v) {
112+
liveAtEntry(getABasicBlockSuccessor(bb), v)
115113
}
116114

117115
/**
118116
* Holds if variable `v` is live in basic block `bb` at index `i`.
119117
* The rank of `i` is `rnk` as defined by `refRank()`.
120118
*/
121-
private predicate liveAtRank(BasicBlock bb, int i, SourceVariable v, int rnk, ReadKind rk) {
119+
private predicate liveAtRank(BasicBlock bb, int i, SourceVariable v, int rnk) {
122120
exists(RefKind kind | rnk = refRank(bb, i, v, kind) |
123121
rnk = maxRefRank(bb, v) and
124-
liveAtExit(bb, v, rk)
122+
liveAtExit(bb, v)
125123
or
126124
ref(bb, i, v, kind) and
127-
kind = Read(rk)
125+
kind = Read()
128126
or
129127
exists(RefKind nextKind |
130-
liveAtRank(bb, _, v, rnk + 1, rk) and
128+
liveAtRank(bb, _, v, rnk + 1) and
131129
rnk + 1 = refRank(bb, _, v, nextKind) and
132130
nextKind != Write(true)
133131
)
@@ -136,11 +134,10 @@ private module Cached {
136134

137135
/**
138136
* Holds if variable `v` is live after the (certain or uncertain) write at
139-
* index `i` inside basic block `bb`. The read that witnesses the liveness of
140-
* `v` is of kind `rk`.
137+
* index `i` inside basic block `bb`.
141138
*/
142-
predicate liveAfterWrite(BasicBlock bb, int i, SourceVariable v, ReadKind rk) {
143-
exists(int rnk | rnk = refRank(bb, i, v, Write(_)) | liveAtRank(bb, i, v, rnk, rk))
139+
predicate liveAfterWrite(BasicBlock bb, int i, SourceVariable v) {
140+
exists(int rnk | rnk = refRank(bb, i, v, Write(_)) | liveAtRank(bb, i, v, rnk))
144141
}
145142
}
146143

@@ -182,11 +179,11 @@ private module Cached {
182179
newtype TDefinition =
183180
TWriteDef(SourceVariable v, BasicBlock bb, int i) {
184181
variableWrite(bb, i, v, _) and
185-
liveAfterWrite(bb, i, v, _)
182+
liveAfterWrite(bb, i, v)
186183
} or
187184
TPhiNode(SourceVariable v, BasicBlock bb) {
188185
inDefDominanceFrontier(bb, v) and
189-
liveAtEntry(bb, v, _)
186+
liveAtEntry(bb, v)
190187
}
191188

192189
private module SsaDefReaches {
@@ -223,7 +220,7 @@ private module Cached {
223220
* Unlike `Liveness::ref`, this includes `phi` nodes.
224221
*/
225222
predicate ssaRef(BasicBlock bb, int i, SourceVariable v, SsaRefKind k) {
226-
variableRead(bb, i, v, _) and
223+
variableRead(bb, i, v) and
227224
k = SsaRead()
228225
or
229226
exists(Definition def | def.definesAt(v, bb, i)) and
@@ -287,16 +284,12 @@ private module Cached {
287284
/**
288285
* Holds if the SSA definition of `v` at `def` reaches index `i` in the same
289286
* basic block `bb`, without crossing another SSA definition of `v`.
290-
*
291-
* The read at `i` is of kind `rk`.
292287
*/
293-
predicate ssaDefReachesReadWithinBlock(
294-
SourceVariable v, Definition def, BasicBlock bb, int i, ReadKind rk
295-
) {
288+
predicate ssaDefReachesReadWithinBlock(SourceVariable v, Definition def, BasicBlock bb, int i) {
296289
exists(int rnk |
297290
ssaDefReachesRank(bb, def, rnk, v) and
298291
rnk = ssaRefRank(bb, i, v, SsaRead()) and
299-
variableRead(bb, i, v, rk)
292+
variableRead(bb, i, v)
300293
)
301294
}
302295

@@ -321,7 +314,7 @@ private module Cached {
321314
v = def.getSourceVariable() and
322315
result = ssaRefRank(bb, i, v, k) and
323316
(
324-
ssaDefReachesRead(_, def, bb, i, _)
317+
ssaDefReachesRead(_, def, bb, i)
325318
or
326319
def.definesAt(_, bb, i)
327320
)
@@ -362,7 +355,7 @@ private module Cached {
362355
predicate defAdjacentRead(Definition def, BasicBlock bb1, BasicBlock bb2, int i2) {
363356
varBlockReaches(def, bb1, bb2) and
364357
ssaRefRank(bb2, i2, def.getSourceVariable(), SsaRead()) = 1 and
365-
variableRead(bb2, i2, _, _)
358+
variableRead(bb2, i2, _)
366359
}
367360
}
368361

@@ -390,11 +383,11 @@ private module Cached {
390383
predicate ssaDefReachesEndOfBlock(BasicBlock bb, Definition def, SourceVariable v) {
391384
exists(int last | last = maxSsaRefRank(bb, v) |
392385
ssaDefReachesRank(bb, def, last, v) and
393-
liveAtExit(bb, v, _)
386+
liveAtExit(bb, v)
394387
)
395388
or
396389
ssaDefReachesEndOfBlockRec(bb, def, v) and
397-
liveAtExit(bb, v, _) and
390+
liveAtExit(bb, v) and
398391
not ssaRef(bb, _, v, SsaDef())
399392
}
400393

@@ -404,12 +397,12 @@ private module Cached {
404397
* is of kind `rk`.
405398
*/
406399
cached
407-
predicate ssaDefReachesRead(SourceVariable v, Definition def, BasicBlock bb, int i, ReadKind rk) {
408-
ssaDefReachesReadWithinBlock(v, def, bb, i, rk)
400+
predicate ssaDefReachesRead(SourceVariable v, Definition def, BasicBlock bb, int i) {
401+
ssaDefReachesReadWithinBlock(v, def, bb, i)
409402
or
410-
variableRead(bb, i, v, rk) and
403+
variableRead(bb, i, v) and
411404
ssaDefReachesEndOfBlock(getABasicBlockPredecessor(bb), def, v) and
412-
not ssaDefReachesReadWithinBlock(v, _, bb, i, _)
405+
not ssaDefReachesReadWithinBlock(v, _, bb, i)
413406
}
414407

415408
/**
@@ -439,7 +432,7 @@ private module Cached {
439432
exists(int rnk |
440433
rnk = ssaDefRank(def, _, bb1, i1, _) and
441434
rnk + 1 = ssaDefRank(def, _, bb1, i2, SsaRead()) and
442-
variableRead(bb1, i2, _, _) and
435+
variableRead(bb1, i2, _) and
443436
bb2 = bb1
444437
)
445438
or

csharp/ql/src/semmle/code/csharp/dataflow/internal/SsaImplSpecific.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor()
1212

1313
class ExitBasicBlock = ControlFlow::BasicBlocks::ExitBlock;
1414

15-
class ReadKind = SsaImpl::ReadKind;
16-
1715
class SourceVariable = SsaImpl::TSourceVariable;
1816

1917
predicate variableWrite = SsaImpl::variableWrite/4;
2018

21-
predicate variableRead = SsaImpl::variableRead/4;
19+
predicate variableRead(BasicBlock bb, int i, SourceVariable v) {
20+
SsaImpl::variableRead(bb, i, v, _)
21+
}

0 commit comments

Comments
 (0)