* simplify logical operations with constant booleans if the expresions involve no side effects

git-svn-id: trunk@35230 -
This commit is contained in:
florian 2017-01-04 16:33:29 +00:00
parent 3e2aff15fb
commit 331bb95c5e

View File

@ -908,6 +908,60 @@ implementation
exit;
end;
{ in case of expressions having no side effect, we can simplify boolean expressions
containing constants }
if is_boolean(left.resultdef) and is_boolean(right.resultdef) then
begin
if is_constboolnode(left) and not(might_have_sideeffects(right)) then
begin
if ((nodetype=andn) and (tordconstnode(left).value<>0)) or
((nodetype=orn) and (tordconstnode(left).value=0)) or
((nodetype=xorn) and (tordconstnode(left).value=0)) then
begin
result:=right;
right:=nil;
exit;
end
else if ((nodetype=orn) and (tordconstnode(left).value<>0)) or
((nodetype=andn) and (tordconstnode(left).value=0)) then
begin
result:=left;
left:=nil;
exit;
end
else if ((nodetype=xorn) and (tordconstnode(left).value<>0)) then
begin
result:=cnotnode.create(right);
right:=nil;
exit;
end
end
else if is_constboolnode(right) and not(might_have_sideeffects(left)) then
begin
if ((nodetype=andn) and (tordconstnode(right).value<>0)) or
((nodetype=orn) and (tordconstnode(right).value=0)) or
((nodetype=xorn) and (tordconstnode(right).value=0)) then
begin
result:=left;
left:=nil;
exit;
end
else if ((nodetype=orn) and (tordconstnode(right).value<>0)) or
((nodetype=andn) and (tordconstnode(right).value=0)) then
begin
result:=right;
right:=nil;
exit;
end
else if ((nodetype=xorn) and (tordconstnode(right).value<>0)) then
begin
result:=cnotnode.create(left);
left:=nil;
exit;
end
end;
end;
{ slow simplifications }
if cs_opt_level2 in current_settings.optimizerswitches then
begin