Skip to content

Commit e32e28d

Browse files
authored
Merge pull request github#13035 from MathiasVP/ranked-phi-hack-for-bounded-phi
C++: Work around poor codegen for `forex` in IR-based range analysis
2 parents 3c74c8b + f94eb74 commit e32e28d

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,6 @@ module ModulusAnalysis<DeltaSig D, BoundSig<D> Bounds, UtilSig<D> U> {
120120
)
121121
}
122122

123-
/**
124-
* Holds if `rix` is the number of input edges to `phi`.
125-
*/
126-
private predicate maxPhiInputRank(SemSsaPhiNode phi, int rix) {
127-
rix = max(int r | rankedPhiInput(phi, _, _, r))
128-
}
129-
130123
/**
131124
* Gets the remainder of `val` modulo `mod`.
132125
*
@@ -322,20 +315,4 @@ module ModulusAnalysis<DeltaSig D, BoundSig<D> Bounds, UtilSig<D> U> {
322315
semExprModulus(rarg, b, val, mod) and isLeft = false
323316
)
324317
}
325-
326-
/**
327-
* Holds if `inp` is an input to `phi` along `edge` and this input has index `r`
328-
* in an arbitrary 1-based numbering of the input edges to `phi`.
329-
*/
330-
private predicate rankedPhiInput(
331-
SemSsaPhiNode phi, SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge, int r
332-
) {
333-
edge.phiInput(phi, inp) and
334-
edge =
335-
rank[r](SemSsaReadPositionPhiInputEdge e |
336-
e.phiInput(phi, _)
337-
|
338-
e order by e.getOrigBlock().getUniqueId()
339-
)
340-
}
341318
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,22 @@ module RangeStage<
877877
)
878878
}
879879

880+
pragma[assume_small_delta]
881+
pragma[nomagic]
882+
private predicate boundedPhiRankStep(
883+
SemSsaPhiNode phi, SemBound b, D::Delta delta, boolean upper, boolean fromBackEdge,
884+
D::Delta origdelta, SemReason reason, int rix
885+
) {
886+
exists(SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge |
887+
Utils::rankedPhiInput(phi, inp, edge, rix) and
888+
boundedPhiCandValidForEdge(phi, b, delta, upper, fromBackEdge, origdelta, reason, inp, edge)
889+
|
890+
if rix = 1
891+
then any()
892+
else boundedPhiRankStep(phi, b, delta, upper, fromBackEdge, origdelta, reason, rix - 1)
893+
)
894+
}
895+
880896
/**
881897
* Holds if `b + delta` is a valid bound for `phi`.
882898
* - `upper = true` : `phi <= b + delta`
@@ -886,8 +902,9 @@ module RangeStage<
886902
SemSsaPhiNode phi, SemBound b, D::Delta delta, boolean upper, boolean fromBackEdge,
887903
D::Delta origdelta, SemReason reason
888904
) {
889-
forex(SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge | edge.phiInput(phi, inp) |
890-
boundedPhiCandValidForEdge(phi, b, delta, upper, fromBackEdge, origdelta, reason, inp, edge)
905+
exists(int r |
906+
Utils::maxPhiInputRank(phi, r) and
907+
boundedPhiRankStep(phi, b, delta, upper, fromBackEdge, origdelta, reason, r)
891908
)
892909
}
893910

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

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

0 commit comments

Comments
 (0)