angr.analyses.decompiler.peephole_optimizations.cmp_sub_const

class angr.analyses.decompiler.peephole_optimizations.cmp_sub_const.CmpSubConst

Bases: PeepholeOptimizationExprBase

Canonicalize equality/inequality comparisons against a constant where one side is a constant add/subtract, by folding the inner constant into the compared constant. This undoes the compiler’s strength-reduced sub/dec-cascade lowering of switch-on-value statements, turning relative constants back into absolute ones:

(A - C1) == C2      ==>  A == (C1 + C2)
(A - C1) != C2      ==>  A != (C1 + C2)
(C1 - A) == C2      ==>  A == (C1 - C2)
(A + C1) == C2      ==>  A == (C2 - C1)

Note that this rule is only applied to CmpEQ/CmpNE. Over the modular integers Z/2^n, A - C1 == C2 iff A == C1 + C2 and A + C1 == C2 iff A == C2 - C1 hold unconditionally, independent of signedness and regardless of any intermediate overflow. This rule is NOT applied to ordered comparisons (e.g., CmpLT), for which folding across a subtract is unsound because of wraparound.

NAME = '(A - C1) cmp C2 => A cmp (C1 + C2)'
expr_classes = (<class 'angr.ailment.expression.BinaryOp'>,)
optimize(expr, **kwargs)
Parameters:

expr (BinaryOp)