Skip to content

Commit be2504d

Browse files
committed
SSA: Extend consistency queries.
1 parent e5466a4 commit be2504d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

shared/ssa/codeql/ssa/Ssa.qll

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,56 @@ module Make<LocationSig Location, InputSig<Location> Input> {
13811381
not def.definesAt(v, getImmediateBasicBlockDominator*(bb), _)
13821382
)
13831383
}
1384+
1385+
/** Holds if the end of a basic block can be reached by multiple definitions. */
1386+
query predicate nonUniqueDefReachesEndOfBlock(Definition def, SourceVariable v, BasicBlock bb) {
1387+
ssaDefReachesEndOfBlock(bb, def, v) and
1388+
not exists(unique(Definition def0 | ssaDefReachesEndOfBlock(bb, def0, v)))
1389+
}
1390+
1391+
/** Holds if a phi node has less than two inputs. */
1392+
query predicate uselessPhiNode(PhiNode phi, int inputs) {
1393+
inputs = count(Definition inp | phiHasInputFromBlock(phi, inp, _)) and
1394+
inputs < 2
1395+
}
1396+
1397+
/** Holds if a certain read does not have a prior reference. */
1398+
query predicate readWithoutPriorRef(SourceVariable v, BasicBlock bb, int i) {
1399+
variableRead(bb, i, v, true) and
1400+
not AdjacentSsaRefs::adjacentRefRead(_, _, bb, i, v)
1401+
}
1402+
1403+
/**
1404+
* Holds if a certain read has multiple prior references. The introduction
1405+
* of phi reads should make the prior reference unique.
1406+
*/
1407+
query predicate readWithMultiplePriorRefs(
1408+
SourceVariable v, BasicBlock bb1, int i1, BasicBlock bb2, int i2
1409+
) {
1410+
AdjacentSsaRefs::adjacentRefRead(bb1, i1, bb2, i2, v) and
1411+
2 <=
1412+
strictcount(BasicBlock bb0, int i0 | AdjacentSsaRefs::adjacentRefRead(bb0, i0, bb1, i1, v))
1413+
}
1414+
1415+
/** Holds if `phi` has less than 2 immediately prior references. */
1416+
query predicate phiWithoutTwoPriorRefs(PhiNode phi, int inputRefs) {
1417+
exists(BasicBlock bbPhi, SourceVariable v |
1418+
phi.definesAt(v, bbPhi, _) and
1419+
inputRefs =
1420+
count(BasicBlock bb, int i | AdjacentSsaRefs::adjacentRefPhi(bb, i, _, bbPhi, v)) and
1421+
inputRefs < 2
1422+
)
1423+
}
1424+
1425+
/**
1426+
* Holds if the phi read for `v` at `bb` has less than 2 immediately prior
1427+
* references.
1428+
*/
1429+
query predicate phiReadWithoutTwoPriorRefs(BasicBlock bbPhi, SourceVariable v, int inputRefs) {
1430+
synthPhiRead(bbPhi, v) and
1431+
inputRefs = count(BasicBlock bb, int i | AdjacentSsaRefs::adjacentRefPhi(bb, i, _, bbPhi, v)) and
1432+
inputRefs < 2
1433+
}
13841434
}
13851435

13861436
/** Provides the input to `DataFlowIntegration`. */

0 commit comments

Comments
 (0)