Skip to content

Commit f255fc2

Browse files
committed
C++: Drive-by join order fix. Before:
``` Evaluated relational algebra for predicate SsaInternals::getDefImpl/1#1ed4f567@65628fbv with tuple counts: 4935102 ~5% {4} r1 = SCAN `SsaInternals::SsaImpl::Definition.definesAt/3#dispred#7eea4c8f` OUTPUT In.2, In.3, In.0, In.1 104274503 ~1% {3} | JOIN WITH `SsaInternals::DefImpl.hasIndexInBlock/2#dispred#30a6c29f_120#join_rhs` ON FIRST 2 OUTPUT Rhs.2, Lhs.3, Lhs.2 4921319 ~2% {2} | JOIN WITH `SsaInternals::DefImpl.getSourceVariable/0#dispred#72437659` ON FIRST 2 OUTPUT Lhs.2, Lhs.0 return r1 ``` After: ``` Evaluated relational algebra for predicate SsaInternals::SsaImpl::Definition.definesAt/3#dispred#7eea4c8f_1230#join_rhs@b280fb5h with tuple counts: 4935102 ~3% {4} r1 = SCAN `SsaInternals::SsaImpl::Definition.definesAt/3#dispred#7eea4c8f` OUTPUT In.1, In.2, In.3, In.0 return r1 Evaluated relational algebra for predicate SsaInternals::DefImpl.hasIndexInBlock/3#dispred#31d295aa_1230#join_rhs@2be655s4 with tuple counts: 5634706 ~1% {4} r1 = SCAN `SsaInternals::DefImpl.hasIndexInBlock/3#dispred#31d295aa` OUTPUT In.1, In.2, In.3, In.0 return r1 Evaluated relational algebra for predicate SsaInternals::getDefImpl/1#1ed4f567@8afa36uu with tuple counts: 4921319 ~2% {2} r1 = JOIN `SsaInternals::SsaImpl::Definition.definesAt/3#dispred#7eea4c8f_1230#join_rhs` WITH `SsaInternals::DefImpl.hasIndexInBlock/3#dispred#31d295aa_1230#join_rhs` ON FIRST 3 OUTPUT Lhs.3, Rhs.3 return r1 ```
1 parent 0836f0b commit f255fc2

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ private int countNumberOfBranchesUsingParameter(SwitchInstruction switch, Parame
15671567
|
15681568
exists(Ssa::UseImpl use | use.hasIndexInBlock(useblock, _, sv))
15691569
or
1570-
exists(Ssa::DefImpl def | def.hasIndexInBlock(useblock, _, sv))
1570+
exists(Ssa::DefImpl def | def.hasIndexInBlock(sv, useblock, _))
15711571
)
15721572
)
15731573
)
@@ -1814,7 +1814,7 @@ module IteratorFlow {
18141814
*/
18151815
private predicate isIteratorWrite(Instruction write, Operand address) {
18161816
exists(Ssa::DefImpl writeDef, IRBlock bb, int i |
1817-
writeDef.hasIndexInBlock(bb, i, _) and
1817+
writeDef.hasIndexInBlock(_, bb, i) and
18181818
bb.getInstruction(i) = write and
18191819
address = writeDef.getAddressOperand()
18201820
)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ abstract class DefImpl extends TDefImpl {
191191
* Holds if this definition (or use) has index `index` in block `block`,
192192
* and is a definition (or use) of the variable `sv`
193193
*/
194-
final predicate hasIndexInBlock(IRBlock block, int index, SourceVariable sv) {
194+
final predicate hasIndexInBlock(SourceVariable sv, IRBlock block, int index) {
195195
this.hasIndexInBlock(block, index) and
196196
sv = this.getSourceVariable()
197197
}
@@ -891,12 +891,12 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
891891
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
892892
DataFlowImplCommon::forceCachingInSameStage() and
893893
(
894-
exists(DefImpl def | def.hasIndexInBlock(bb, i, v) |
894+
exists(DefImpl def | def.hasIndexInBlock(v, bb, i) |
895895
if def.isCertain() then certain = true else certain = false
896896
)
897897
or
898898
exists(GlobalDefImpl global |
899-
global.hasIndexInBlock(bb, i, v) and
899+
global.hasIndexInBlock(v, bb, i) and
900900
certain = true
901901
)
902902
)
@@ -934,10 +934,11 @@ module SsaCached {
934934
}
935935

936936
/** Gets the `DefImpl` corresponding to `def`. */
937+
pragma[nomagic]
937938
private DefImpl getDefImpl(SsaImpl::Definition def) {
938939
exists(SourceVariable sv, IRBlock bb, int i |
939940
def.definesAt(sv, bb, i) and
940-
result.hasIndexInBlock(bb, i, sv)
941+
result.hasIndexInBlock(sv, bb, i)
941942
)
942943
}
943944

0 commit comments

Comments
 (0)