Skip to content

Commit c24e603

Browse files
committed
XPR: add checks for loopcounter; extend ssa-value recording
1 parent 2bda409 commit c24e603

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

chb/invariants/XSymbol.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def name(self) -> str:
6363
def attrs(self) -> List[str]:
6464
return self.tags[1:]
6565

66+
def has_attribute(self, attr: str) -> bool:
67+
return attr in self.attrs
68+
6669
@property
6770
def seqnr(self) -> int:
6871
return self.args[0]

chb/invariants/XVariable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def type(self) -> str:
8282
def is_tmp(self) -> bool:
8383
return (self.seqnr == -1)
8484

85+
@property
86+
def is_loop_counter(self) -> bool:
87+
return self.symbol.has_attribute("lc")
88+
8589
@property
8690
def is_constant_value_variable(self) -> bool:
8791
return self.has_denotation() and self.denotation.is_auxiliary_variable

chb/invariants/XXpr.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ def has_variables_with_property(
294294
self, p: Callable[[XVariable], bool]) -> bool:
295295
return False
296296

297+
def all_variables_with_property(
298+
self, p: Callable[[XVariable], bool]) -> bool:
299+
return False
300+
301+
@property
302+
def is_loop_counter_expression(self) -> bool:
303+
return self.all_variables_with_property(lambda v: v.is_loop_counter)
304+
297305
def negated_value(self) -> int:
298306
raise UF.CHBError("Get_negated_value not supported for " + str(self))
299307

@@ -349,6 +357,10 @@ def __init__(
349357
def variable(self) -> XVariable:
350358
return self.xd.variable(self.args[0])
351359

360+
@property
361+
def is_loop_counter(self) -> bool:
362+
return self.variable.is_loop_counter
363+
352364
@property
353365
def is_constant_value_variable(self) -> bool:
354366
return self.variable.is_constant_value_variable
@@ -447,6 +459,10 @@ def has_variables_with_property(
447459
self, p: Callable[[XVariable], bool]) -> bool:
448460
return p(self.variable)
449461

462+
def all_variables_with_property(
463+
self, p: Callable[[XVariable], bool]) -> bool:
464+
return p(self.variable)
465+
450466
def argument_index(self) -> int:
451467
if self.is_argument_value:
452468
return self.variable.denotation.argument_index()
@@ -577,6 +593,10 @@ def intvalue(self) -> int:
577593
raise UF.CHBError(
578594
"Constant is not an integer constant: " + str(self))
579595

596+
def all_variables_with_property(
597+
self, p: Callable[[XVariable], bool]) -> bool:
598+
return True
599+
580600
@property
581601
def is_constant(self) -> bool:
582602
return True
@@ -759,6 +779,10 @@ def has_variables_with_property(
759779
self, p: Callable[[XVariable], bool]) -> bool:
760780
return any([op.has_variables_with_property(p) for op in self.operands])
761781

782+
def all_variables_with_property(
783+
self, p: Callable[[XVariable], bool]) -> bool:
784+
return all([op.all_variables_with_property(p) for op in self.operands])
785+
762786
@property
763787
def is_stack_address(self) -> bool:
764788
args = self.operands

chb/invariants/XXprUtil.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,12 +1848,26 @@ def xvariable_to_ast_lval(
18481848
bctype = xdata.function.register_lhs_type(iaddr, str(xv))
18491849
if bctype is not None:
18501850
ctype = bctype.convert(astree.typconverter)
1851+
1852+
# Note: Ideally the rhs parameter should be replaced with an astrhs
1853+
# parameter supplied by all of the instructions. The replacement would
1854+
# avoid having to compute the astrhs twice. Furthermore, for some
1855+
# instructions the rhs may be computed in a non-standard way, causing
1856+
# the assigned value to diverge from the value computed here.
18511857
if (
18521858
rhs is not None
18531859
and (rhs.is_constant
1854-
or (rhs.is_constant_value_variable))):
1860+
or (rhs.is_constant_value_variable)
1861+
or (rhs.is_loop_counter_expression))):
18551862
astrhs: Optional[AST.ASTExpr] = xxpr_to_ast_def_expr(
18561863
rhs, xdata, iaddr, astree, anonymous=anonymous)
1864+
1865+
elif rhs is not None:
1866+
astrhs = xxpr_to_ast_def_expr(
1867+
rhs, xdata, iaddr, astree, anonymous=anonymous)
1868+
if not astrhs.is_constant_value_expression:
1869+
astrhs = None
1870+
18571871
else:
18581872
astrhs = None
18591873

0 commit comments

Comments
 (0)