@@ -20,6 +20,16 @@ def lit_eq(op: IROperand, val: int) -> bool:
2020 return isinstance (op , IRLiteral ) and wrap256 (op .value ) == wrap256 (val )
2121
2222
23+ def lit_add (op : IROperand , val : int ) -> int :
24+ assert isinstance (op , IRLiteral )
25+ return eval_arith ("add" , [op , IRLiteral (val )])
26+
27+
28+ def lit_sub (val : int , op : IROperand ) -> int :
29+ assert isinstance (op , IRLiteral )
30+ return eval_arith ("sub" , [op , IRLiteral (val )])
31+
32+
2333def is_negative (x ):
2434 return bool (x & (1 << 255 ))
2535
@@ -137,11 +147,10 @@ def _fold_add_chain(self, inst: IRInstruction) -> bool:
137147 base_operand , literal = self ._extract_value_and_literal_operands (inst )
138148 if literal is None or base_operand is None :
139149 return False
140- total = eval_arith ( "add" , [ IRLiteral ( total ), literal ] )
150+ total = lit_add ( literal , total )
141151 else : # sub
142152 if self ._is_lit (op0 ) and not self ._is_lit (op1 ):
143- assert isinstance (op0 , IRLiteral ) # help mypy
144- total = eval_arith ("sub" , [op0 , IRLiteral (total )])
153+ total = lit_sub (total , op0 )
145154 base_operand = op1
146155 else :
147156 return False
@@ -188,7 +197,7 @@ def _trace_add_chain(self, operand: IROperand) -> tuple[IROperand, int]:
188197 break
189198
190199 assert isinstance (literal , IRLiteral ) # help mypy
191- total = eval_arith ( "add" , [ IRLiteral ( total ), literal ] )
200+ total = lit_add ( literal , total )
192201 current = value_op
193202 continue
194203
@@ -197,8 +206,7 @@ def _trace_add_chain(self, operand: IROperand) -> tuple[IROperand, int]:
197206 break
198207 op0 , op1 = producer .operands
199208 if self ._is_lit (op0 ) and not self ._is_lit (op1 ):
200- assert isinstance (op0 , IRLiteral ) # help mypy
201- total = eval_arith ("sub" , [IRLiteral (total ), op0 ])
209+ total = lit_sub (total , op0 )
202210 current = op1
203211 continue
204212 break
@@ -329,7 +337,7 @@ def _handle_inst_peephole(self, inst: IRInstruction):
329337 if inst .opcode == "xor" and lit_eq (operands [0 ], - 1 ):
330338 self .updater .update (inst , "not" , [operands [1 ]])
331339 return
332-
340+
333341 if inst .opcode in {"add" , "sub" }:
334342 if self ._fold_add_chain (inst ):
335343 return
0 commit comments