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.
This commit is contained in:
Sei Lisa 2017-08-02 01:05:17 +02:00
parent 61565470a2
commit 3f61e6f7bf

View file

@ -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