From 3f61e6f7bff0ba5698a91356e9dc4cac178d58fa Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Wed, 2 Aug 2017 01:05:17 +0200 Subject: [PATCH] Make IsBool smarter when finding whether an expression is boolean. It was failing with a && b && c where a, b, c were known to be bools. It managed to simplify them to a & b & -c but that's not optimal. Now it recurses on some operators that may return bool when used with bool operands: &, |, ^, * (in the case of &, it's bool if either operand is; for the rest, it's bool if both are). As a result, the above now simplifies to a & b & c, which is optimal. --- lslopt/lslfoldconst.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lslopt/lslfoldconst.py b/lslopt/lslfoldconst.py index 5f45907..c108450 100644 --- a/lslopt/lslfoldconst.py +++ b/lslopt/lslfoldconst.py @@ -103,6 +103,8 @@ class foldconst(object): nt = node['nt'] if nt in ('<', '!', '>', '<=', '>=', '==', '||', '&&') \ or nt == '!=' and node['ch'][0]['t'] != 'list' \ + or nt == '&' and (self.IsBool(node['ch'][0]) or self.IsBool(node['ch'][1])) \ + or nt in ('|', '^', '*') and self.IsBool(node['ch'][0]) and self.IsBool(node['ch'][1]) \ or nt == 'CONST' and node['t'] == 'integer' and node['value'] in (0, 1): return True