Skip to content

Commit 71cd507

Browse files
authored
Merge pull request #8298 from MathiasVP/filter-bad-conversions-in-cpp-gvn
C++: Fix `GVN` performance on more invalid IR
2 parents 169f655 + 155502c commit 71cd507

File tree

5 files changed

+60
-45
lines changed

5 files changed

+60
-45
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/gvn/internal/ValueNumberingInternal.qll

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ private predicate filteredNumberableInstruction(Instruction instr) {
106106
or
107107
instr instanceof FieldAddressInstruction and
108108
count(instr.(FieldAddressInstruction).getField()) != 1
109+
or
110+
instr instanceof InheritanceConversionInstruction and
111+
(
112+
count(instr.(InheritanceConversionInstruction).getBaseClass()) != 1 or
113+
count(instr.(InheritanceConversionInstruction).getDerivedClass()) != 1
114+
)
109115
}
110116

111117
private predicate variableAddressValueNumber(
@@ -115,8 +121,7 @@ private predicate variableAddressValueNumber(
115121
// The underlying AST element is used as value-numbering key instead of the
116122
// `IRVariable` to work around a problem where a variable or expression with
117123
// multiple types gives rise to multiple `IRVariable`s.
118-
instr.getIRVariable().getAST() = ast and
119-
strictcount(instr.getIRVariable().getAST()) = 1
124+
unique( | | instr.getIRVariable().getAST()) = ast
120125
}
121126

122127
private predicate initializeParameterValueNumber(
@@ -133,8 +138,7 @@ private predicate constantValueNumber(
133138
ConstantInstruction instr, IRFunction irFunc, IRType type, string value
134139
) {
135140
instr.getEnclosingIRFunction() = irFunc and
136-
strictcount(instr.getResultIRType()) = 1 and
137-
instr.getResultIRType() = type and
141+
unique( | | instr.getResultIRType()) = type and
138142
instr.getValue() = value
139143
}
140144

@@ -151,8 +155,7 @@ private predicate fieldAddressValueNumber(
151155
TValueNumber objectAddress
152156
) {
153157
instr.getEnclosingIRFunction() = irFunc and
154-
instr.getField() = field and
155-
strictcount(instr.getField()) = 1 and
158+
unique( | | instr.getField()) = field and
156159
tvalueNumber(instr.getObjectAddress()) = objectAddress
157160
}
158161

@@ -195,9 +198,9 @@ private predicate inheritanceConversionValueNumber(
195198
) {
196199
instr.getEnclosingIRFunction() = irFunc and
197200
instr.getOpcode() = opcode and
198-
instr.getBaseClass() = baseClass and
199-
instr.getDerivedClass() = derivedClass and
200-
tvalueNumber(instr.getUnary()) = operand
201+
tvalueNumber(instr.getUnary()) = operand and
202+
unique( | | instr.getBaseClass()) = baseClass and
203+
unique( | | instr.getDerivedClass()) = derivedClass
201204
}
202205

203206
private predicate loadTotalOverlapValueNumber(

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/gvn/internal/ValueNumberingInternal.qll

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ private predicate filteredNumberableInstruction(Instruction instr) {
106106
or
107107
instr instanceof FieldAddressInstruction and
108108
count(instr.(FieldAddressInstruction).getField()) != 1
109+
or
110+
instr instanceof InheritanceConversionInstruction and
111+
(
112+
count(instr.(InheritanceConversionInstruction).getBaseClass()) != 1 or
113+
count(instr.(InheritanceConversionInstruction).getDerivedClass()) != 1
114+
)
109115
}
110116

111117
private predicate variableAddressValueNumber(
@@ -115,8 +121,7 @@ private predicate variableAddressValueNumber(
115121
// The underlying AST element is used as value-numbering key instead of the
116122
// `IRVariable` to work around a problem where a variable or expression with
117123
// multiple types gives rise to multiple `IRVariable`s.
118-
instr.getIRVariable().getAST() = ast and
119-
strictcount(instr.getIRVariable().getAST()) = 1
124+
unique( | | instr.getIRVariable().getAST()) = ast
120125
}
121126

122127
private predicate initializeParameterValueNumber(
@@ -133,8 +138,7 @@ private predicate constantValueNumber(
133138
ConstantInstruction instr, IRFunction irFunc, IRType type, string value
134139
) {
135140
instr.getEnclosingIRFunction() = irFunc and
136-
strictcount(instr.getResultIRType()) = 1 and
137-
instr.getResultIRType() = type and
141+
unique( | | instr.getResultIRType()) = type and
138142
instr.getValue() = value
139143
}
140144

@@ -151,8 +155,7 @@ private predicate fieldAddressValueNumber(
151155
TValueNumber objectAddress
152156
) {
153157
instr.getEnclosingIRFunction() = irFunc and
154-
instr.getField() = field and
155-
strictcount(instr.getField()) = 1 and
158+
unique( | | instr.getField()) = field and
156159
tvalueNumber(instr.getObjectAddress()) = objectAddress
157160
}
158161

@@ -195,9 +198,9 @@ private predicate inheritanceConversionValueNumber(
195198
) {
196199
instr.getEnclosingIRFunction() = irFunc and
197200
instr.getOpcode() = opcode and
198-
instr.getBaseClass() = baseClass and
199-
instr.getDerivedClass() = derivedClass and
200-
tvalueNumber(instr.getUnary()) = operand
201+
tvalueNumber(instr.getUnary()) = operand and
202+
unique( | | instr.getBaseClass()) = baseClass and
203+
unique( | | instr.getDerivedClass()) = derivedClass
201204
}
202205

203206
private predicate loadTotalOverlapValueNumber(

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/gvn/internal/ValueNumberingInternal.qll

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ private predicate filteredNumberableInstruction(Instruction instr) {
106106
or
107107
instr instanceof FieldAddressInstruction and
108108
count(instr.(FieldAddressInstruction).getField()) != 1
109+
or
110+
instr instanceof InheritanceConversionInstruction and
111+
(
112+
count(instr.(InheritanceConversionInstruction).getBaseClass()) != 1 or
113+
count(instr.(InheritanceConversionInstruction).getDerivedClass()) != 1
114+
)
109115
}
110116

111117
private predicate variableAddressValueNumber(
@@ -115,8 +121,7 @@ private predicate variableAddressValueNumber(
115121
// The underlying AST element is used as value-numbering key instead of the
116122
// `IRVariable` to work around a problem where a variable or expression with
117123
// multiple types gives rise to multiple `IRVariable`s.
118-
instr.getIRVariable().getAST() = ast and
119-
strictcount(instr.getIRVariable().getAST()) = 1
124+
unique( | | instr.getIRVariable().getAST()) = ast
120125
}
121126

122127
private predicate initializeParameterValueNumber(
@@ -133,8 +138,7 @@ private predicate constantValueNumber(
133138
ConstantInstruction instr, IRFunction irFunc, IRType type, string value
134139
) {
135140
instr.getEnclosingIRFunction() = irFunc and
136-
strictcount(instr.getResultIRType()) = 1 and
137-
instr.getResultIRType() = type and
141+
unique( | | instr.getResultIRType()) = type and
138142
instr.getValue() = value
139143
}
140144

@@ -151,8 +155,7 @@ private predicate fieldAddressValueNumber(
151155
TValueNumber objectAddress
152156
) {
153157
instr.getEnclosingIRFunction() = irFunc and
154-
instr.getField() = field and
155-
strictcount(instr.getField()) = 1 and
158+
unique( | | instr.getField()) = field and
156159
tvalueNumber(instr.getObjectAddress()) = objectAddress
157160
}
158161

@@ -195,9 +198,9 @@ private predicate inheritanceConversionValueNumber(
195198
) {
196199
instr.getEnclosingIRFunction() = irFunc and
197200
instr.getOpcode() = opcode and
198-
instr.getBaseClass() = baseClass and
199-
instr.getDerivedClass() = derivedClass and
200-
tvalueNumber(instr.getUnary()) = operand
201+
tvalueNumber(instr.getUnary()) = operand and
202+
unique( | | instr.getBaseClass()) = baseClass and
203+
unique( | | instr.getDerivedClass()) = derivedClass
201204
}
202205

203206
private predicate loadTotalOverlapValueNumber(

csharp/ql/src/experimental/ir/implementation/raw/gvn/internal/ValueNumberingInternal.qll

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ private predicate filteredNumberableInstruction(Instruction instr) {
106106
or
107107
instr instanceof FieldAddressInstruction and
108108
count(instr.(FieldAddressInstruction).getField()) != 1
109+
or
110+
instr instanceof InheritanceConversionInstruction and
111+
(
112+
count(instr.(InheritanceConversionInstruction).getBaseClass()) != 1 or
113+
count(instr.(InheritanceConversionInstruction).getDerivedClass()) != 1
114+
)
109115
}
110116

111117
private predicate variableAddressValueNumber(
@@ -115,8 +121,7 @@ private predicate variableAddressValueNumber(
115121
// The underlying AST element is used as value-numbering key instead of the
116122
// `IRVariable` to work around a problem where a variable or expression with
117123
// multiple types gives rise to multiple `IRVariable`s.
118-
instr.getIRVariable().getAST() = ast and
119-
strictcount(instr.getIRVariable().getAST()) = 1
124+
unique( | | instr.getIRVariable().getAST()) = ast
120125
}
121126

122127
private predicate initializeParameterValueNumber(
@@ -133,8 +138,7 @@ private predicate constantValueNumber(
133138
ConstantInstruction instr, IRFunction irFunc, IRType type, string value
134139
) {
135140
instr.getEnclosingIRFunction() = irFunc and
136-
strictcount(instr.getResultIRType()) = 1 and
137-
instr.getResultIRType() = type and
141+
unique( | | instr.getResultIRType()) = type and
138142
instr.getValue() = value
139143
}
140144

@@ -151,8 +155,7 @@ private predicate fieldAddressValueNumber(
151155
TValueNumber objectAddress
152156
) {
153157
instr.getEnclosingIRFunction() = irFunc and
154-
instr.getField() = field and
155-
strictcount(instr.getField()) = 1 and
158+
unique( | | instr.getField()) = field and
156159
tvalueNumber(instr.getObjectAddress()) = objectAddress
157160
}
158161

@@ -195,9 +198,9 @@ private predicate inheritanceConversionValueNumber(
195198
) {
196199
instr.getEnclosingIRFunction() = irFunc and
197200
instr.getOpcode() = opcode and
198-
instr.getBaseClass() = baseClass and
199-
instr.getDerivedClass() = derivedClass and
200-
tvalueNumber(instr.getUnary()) = operand
201+
tvalueNumber(instr.getUnary()) = operand and
202+
unique( | | instr.getBaseClass()) = baseClass and
203+
unique( | | instr.getDerivedClass()) = derivedClass
201204
}
202205

203206
private predicate loadTotalOverlapValueNumber(

csharp/ql/src/experimental/ir/implementation/unaliased_ssa/gvn/internal/ValueNumberingInternal.qll

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ private predicate filteredNumberableInstruction(Instruction instr) {
106106
or
107107
instr instanceof FieldAddressInstruction and
108108
count(instr.(FieldAddressInstruction).getField()) != 1
109+
or
110+
instr instanceof InheritanceConversionInstruction and
111+
(
112+
count(instr.(InheritanceConversionInstruction).getBaseClass()) != 1 or
113+
count(instr.(InheritanceConversionInstruction).getDerivedClass()) != 1
114+
)
109115
}
110116

111117
private predicate variableAddressValueNumber(
@@ -115,8 +121,7 @@ private predicate variableAddressValueNumber(
115121
// The underlying AST element is used as value-numbering key instead of the
116122
// `IRVariable` to work around a problem where a variable or expression with
117123
// multiple types gives rise to multiple `IRVariable`s.
118-
instr.getIRVariable().getAST() = ast and
119-
strictcount(instr.getIRVariable().getAST()) = 1
124+
unique( | | instr.getIRVariable().getAST()) = ast
120125
}
121126

122127
private predicate initializeParameterValueNumber(
@@ -133,8 +138,7 @@ private predicate constantValueNumber(
133138
ConstantInstruction instr, IRFunction irFunc, IRType type, string value
134139
) {
135140
instr.getEnclosingIRFunction() = irFunc and
136-
strictcount(instr.getResultIRType()) = 1 and
137-
instr.getResultIRType() = type and
141+
unique( | | instr.getResultIRType()) = type and
138142
instr.getValue() = value
139143
}
140144

@@ -151,8 +155,7 @@ private predicate fieldAddressValueNumber(
151155
TValueNumber objectAddress
152156
) {
153157
instr.getEnclosingIRFunction() = irFunc and
154-
instr.getField() = field and
155-
strictcount(instr.getField()) = 1 and
158+
unique( | | instr.getField()) = field and
156159
tvalueNumber(instr.getObjectAddress()) = objectAddress
157160
}
158161

@@ -195,9 +198,9 @@ private predicate inheritanceConversionValueNumber(
195198
) {
196199
instr.getEnclosingIRFunction() = irFunc and
197200
instr.getOpcode() = opcode and
198-
instr.getBaseClass() = baseClass and
199-
instr.getDerivedClass() = derivedClass and
200-
tvalueNumber(instr.getUnary()) = operand
201+
tvalueNumber(instr.getUnary()) = operand and
202+
unique( | | instr.getBaseClass()) = baseClass and
203+
unique( | | instr.getDerivedClass()) = derivedClass
201204
}
202205

203206
private predicate loadTotalOverlapValueNumber(

0 commit comments

Comments
 (0)