Skip to content

Commit a88d8b2

Browse files
committed
C++: Only ignore constant folding for certain binary operations.
1 parent a97891c commit a88d8b2

File tree

6 files changed

+268
-317
lines changed

6 files changed

+268
-317
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,25 @@ IRTempVariable getIRTempVariable(Locatable ast, TempVariableTag tag) {
4040
result.getTag() = tag
4141
}
4242

43-
/** Gets an operand of `binOp`. */
44-
private Expr getAnOperand(BinaryOperation binOp) { result = binOp.getAnOperand() }
43+
/** Gets an operand of `op`. */
44+
private Expr getAnOperand(Operation op) { result = op.getAnOperand() }
4545

4646
/**
47-
* Gets the number of nested operands of `binOp`. For example,
47+
* Gets the number of nested operands of `op`. For example,
4848
* `getNumberOfNestedBinaryOperands((1 + 2) + 3))` is `3`.
4949
*/
50-
private int getNumberOfNestedBinaryOperands(BinaryOperation binOp) {
51-
result = count(getAnOperand*(binOp))
50+
private int getNumberOfNestedBinaryOperands(Operation op) { result = count(getAnOperand*(op)) }
51+
52+
/**
53+
* Holds if `op` should not be translated to a `ConstantInstruction` as part of
54+
* IR generation, even if the value of `op` is constant.
55+
*/
56+
private predicate ignoreConstantValue(Operation op) {
57+
op instanceof BitwiseAndExpr
58+
or
59+
op instanceof BitwiseOrExpr
60+
or
61+
op instanceof BitwiseXorExpr
5262
}
5363

5464
/**
@@ -58,14 +68,14 @@ private int getNumberOfNestedBinaryOperands(BinaryOperation binOp) {
5868
*/
5969
predicate isIRConstant(Expr expr) {
6070
exists(expr.getValue()) and
61-
// We avoid constant folding binary operations since it's often useful to
71+
// We avoid constant folding certain operations since it's often useful to
6272
// mark one of those as a source in dataflow, and if the operation is
6373
// constant folded it's not possible to mark its operands as a source (or
6474
// sink).
6575
// But to avoid creating an outrageous amount of IR from very large
6676
// constant expressions we fall back to constant folding if the operation
6777
// has more than 50 operands (i.e., 1 + 2 + 3 + 4 + ... + 50)
68-
if expr instanceof BinaryOperation then getNumberOfNestedBinaryOperands(expr) > 50 else any()
78+
if ignoreConstantValue(expr) then getNumberOfNestedBinaryOperands(expr) > 50 else any()
6979
}
7080

7181
// Pulled out for performance. See

0 commit comments

Comments
 (0)