Skip to content

Commit 35d7fb5

Browse files
committed
C++: Fix TranslatedElement.getInstruction perf
This relation was almost 40x the size it needed to be on Wireshark because it lacked a restriction on the `tag` parameter. To implement that restriction efficiently, I had to split the relation in two to dictate the join order. With the fix, `getInstruction` now computes the same as `getInstructionTranslatedElementAndTag`, so the latter could be simplified. I made a corresponding change to `TranslatedElement.getTempVariable` for the sake of consistency.
1 parent fc5b9dd commit 35d7fb5

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,12 @@ class InstructionTagType extends TInstructionTag {
1515

1616
private TranslatedElement getInstructionTranslatedElement(
1717
Instruction instruction) {
18-
result = getInstructionTranslatedElementAndTag(instruction, _)
18+
instruction = result.getInstruction(_)
1919
}
2020

2121
private TranslatedElement getInstructionTranslatedElementAndTag(
2222
Instruction instruction, InstructionTag tag) {
23-
result.getAST() = instruction.getAST() and
24-
tag = instruction.getTag() and
25-
result.hasInstruction(_, tag, _, _)
26-
}
27-
28-
private TranslatedElement getTempVariableTranslatedElement(
29-
IRTempVariable var) {
30-
result.getAST() = var.getAST() and
31-
result.hasTempVariable(var.getTag(), _)
23+
instruction = result.getInstruction(tag)
3224
}
3325

3426
import Cached
@@ -179,8 +171,10 @@ cached private module Cached {
179171
import CachedForDebugging
180172
cached private module CachedForDebugging {
181173
cached string getTempVariableUniqueId(IRTempVariable var) {
182-
result = getTempVariableTranslatedElement(var).getId() + ":" +
183-
getTempVariableTagId(var.getTag())
174+
exists(TranslatedElement element |
175+
var = element.getTempVariable(_) and
176+
result = element.getId() + ":" + getTempVariableTagId(var.getTag())
177+
)
184178
}
185179

186180
cached string getInstructionUniqueId(Instruction instruction) {

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,20 +633,27 @@ abstract class TranslatedElement extends TTranslatedElement {
633633
none()
634634
}
635635

636+
pragma[noinline]
637+
private predicate hasInstructionWithTagAndAst(InstructionTag tag, Locatable ast) {
638+
hasInstruction(_, tag, _, _) and
639+
ast = getAST()
640+
}
641+
636642
/**
637643
* Gets the instruction generated by this element with tag `tag`.
638644
*/
639645
final Instruction getInstruction(InstructionTag tag) {
640-
result.getAST() = getAST() and
641-
result.getTag() = tag
646+
hasInstructionWithTagAndAst(tag, result.getAST()) and
647+
tag = result.getTag()
642648
}
643649

644650
/**
645651
* Gets the temporary variable generated by this element with tag `tag`.
646652
*/
647653
final IRTempVariable getTempVariable(TempVariableTag tag) {
648654
result.getAST() = getAST() and
649-
result.getTag() = tag
655+
result.getTag() = tag and
656+
hasTempVariable(tag, _)
650657
}
651658

652659
/**

0 commit comments

Comments
 (0)