Skip to content

Commit

Permalink
C++: Improve IRGuads logic for 'unlikely' expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasVP committed Jan 15, 2025
1 parent 540f4e9 commit d204810
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,14 @@ private module Cached {
compares_eq(test.(BuiltinExpectCallValueNumber).getCondition(), left, right, k, areEqual, value)
}

private predicate isConvertedBool(Instruction instr) {
instr.getResultIRType() instanceof IRBooleanType
or
isConvertedBool(instr.(ConvertInstruction).getUnary())
or
isConvertedBool(instr.(BuiltinExpectCallInstruction).getCondition())
}

/**
* Holds if `op == k` is `areEqual` given that `test` is equal to `value`.
*/
Expand Down Expand Up @@ -982,6 +990,20 @@ private module Cached {
k = k1 + k2
)
or
exists(CompareValueNumber cmp, Operand left, Operand right, AbstractValue v |
test = cmp and
cmp.hasOperands(left, right) and
isConvertedBool(left.getDef()) and
int_value(right.getDef()) = 0 and
unary_compares_eq(valueNumberOfOperand(left), op, k, areEqual, v)
|
cmp instanceof CompareNEValueNumber and
v = value
or
cmp instanceof CompareEQValueNumber and
v.getDualValue() = value
)
or
unary_compares_eq(test.(BuiltinExpectCallValueNumber).getCondition(), op, k, areEqual, value)
}

Expand Down

0 comments on commit d204810

Please sign in to comment.