From 3e2aff15fbdcb0ee4041cf8949b8984a8a162a80 Mon Sep 17 00:00:00 2001 From: florian Date: Wed, 4 Jan 2017 16:33:27 +0000 Subject: [PATCH] * short boolean evaluation is too expensive for simple nodes with no side effects, do full evaluation in this case git-svn-id: trunk@35229 - --- compiler/nadd.pas | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/compiler/nadd.pas b/compiler/nadd.pas index 6bdb54f4d0..b97581339a 100644 --- a/compiler/nadd.pas +++ b/compiler/nadd.pas @@ -909,21 +909,21 @@ implementation end; { slow simplifications } - if (cs_opt_level2 in current_settings.optimizerswitches) then + if cs_opt_level2 in current_settings.optimizerswitches then begin { the comparison is might be expensive and the nodes are usually only equal if some previous optimizations were done so don't check this simplification always } - if is_boolean(left.resultdef) and is_boolean(right.resultdef) and - { even when short circuit boolean evaluation is active, this - optimization cannot be performed in case the node has - side effects, because this can change the result (e.g., in an - or-node that calls the same function twice and first returns - false and then true because of a global state change } - not might_have_sideeffects(left) then + if is_boolean(left.resultdef) and is_boolean(right.resultdef) then begin - if left.isequal(right) then + { even when short circuit boolean evaluation is active, this + optimization cannot be performed in case the node has + side effects, because this can change the result (e.g., in an + or-node that calls the same function twice and first returns + false and then true because of a global state change } + if not might_have_sideeffects(left) and + left.isequal(right) then begin case nodetype of andn,orn: @@ -940,8 +940,25 @@ implementation end; } end; - end; - end; + end + { short to full boolean evalution possible and useful? } + else if not(might_have_sideeffects(right)) and not(cs_full_boolean_eval in localswitches) then + begin + case nodetype of + andn,orn: + { full boolean evaluation is only useful if the nodes are not too complex and if no flags/jumps must be converted, + further, we need to know the expectloc } + if (node_complexity(right)<=2) and + not(left.expectloc in [LOC_FLAGS,LOC_JUMP,LOC_INVALID]) and not(right.expectloc in [LOC_FLAGS,LOC_JUMP,LOC_INVALID]) then + begin + { we need to copy the whole tree to force another pass_1 } + include(localswitches,cs_full_boolean_eval); + result:=getcopy; + exit; + end; + end; + end + end; { using sqr(x) for reals instead of x*x might reduces register pressure and/or memory accesses while sqr() has no drawback }