Skip to content

Commit 4d04391

Browse files
committed
C++: Keep all phi input back edges.
1 parent ae47339 commit 4d04391

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ private module DataFlowIntegrationInput implements SsaImpl::DataFlowIntegrationI
10101010
predicate guardControlsBlock(Guard guard, SsaInput::BasicBlock bb, boolean branch) {
10111011
guard.(IRGuards::IRGuardCondition).controls(bb, branch)
10121012
}
1013+
1014+
predicate keepAllPhiInputBackEdges() { any() }
10131015
}
10141016

10151017
private module DataFlowIntegrationImpl = SsaImpl::DataFlowIntegration<DataFlowIntegrationInput>;

shared/ssa/codeql/ssa/Ssa.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,14 @@ module Make<LocationSig Location, InputSig<Location> Input> {
15081508
* nodes. Disable this only if barrier guards are not going to be used.
15091509
*/
15101510
default predicate supportBarrierGuardsOnPhiEdges() { any() }
1511+
1512+
/**
1513+
* Holds if all phi input back edges should be kept in the data flow graph.
1514+
*
1515+
* This is ordinarily not necessary and causes the retention of superfluous
1516+
* nodes.
1517+
*/
1518+
default predicate keepAllPhiInputBackEdges() { none() }
15111519
}
15121520

15131521
/**
@@ -1539,11 +1547,26 @@ module Make<LocationSig Location, InputSig<Location> Input> {
15391547
)
15401548
}
15411549

1550+
/**
1551+
* Holds if the phi input edge from `input` to `phi` is a back edge and
1552+
* must be kept.
1553+
*/
1554+
private predicate relevantBackEdge(SsaPhiExt phi, BasicBlock input) {
1555+
exists(BasicBlock bbPhi |
1556+
DfInput::keepAllPhiInputBackEdges() and
1557+
exists(getAPhiInputDef(phi, input)) and
1558+
phi.getBasicBlock() = bbPhi and
1559+
getImmediateBasicBlockDominator+(input) = bbPhi
1560+
)
1561+
}
1562+
15421563
/**
15431564
* Holds if the input to `phi` from the block `input` might be relevant for
15441565
* barrier guards as a separately synthesized `TSsaInputNode`.
15451566
*/
15461567
private predicate relevantPhiInputNode(SsaPhiExt phi, BasicBlock input) {
1568+
relevantBackEdge(phi, input)
1569+
or
15471570
DfInput::supportBarrierGuardsOnPhiEdges() and
15481571
// If the input isn't explicitly read then a guard cannot check it.
15491572
exists(DfInput::getARead(getAPhiInputDef(phi, input))) and
@@ -1605,6 +1628,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
16051628
* flow edges.
16061629
*/
16071630
private predicate phiHasUniqNextNode(SsaPhiExt phi) {
1631+
not relevantBackEdge(phi, _) and
16081632
exists(int nextPhiInput, int nextPhi, int nextRef |
16091633
1 = nextPhiInput + nextPhi + nextRef and
16101634
nextPhiInput =

0 commit comments

Comments
 (0)