From de1e0c405e3e3a04e292f15e1ebdd120fa9dad09 Mon Sep 17 00:00:00 2001 From: nickysn Date: Mon, 1 May 2017 18:16:38 +0000 Subject: [PATCH] + optimize '0 shl x' and '0 shr x' to 0 git-svn-id: trunk@36041 - --- compiler/nmat.pas | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/compiler/nmat.pas b/compiler/nmat.pas index a4a33e7b9d..75079d4956 100644 --- a/compiler/nmat.pas +++ b/compiler/nmat.pas @@ -702,6 +702,28 @@ implementation result:=left; left:=nil; end; + end + else if is_constintnode(left) then + begin + lvalue:=tordconstnode(left).value; + { shl/shr are unsigned operations, so cut off upper bits } + case resultdef.size of + 1: + lvalue:=tordconstnode(left).value and byte($ff); + 2: + lvalue:=tordconstnode(left).value and word($ffff); + 4: + lvalue:=tordconstnode(left).value and dword($ffffffff); + 8: + lvalue:=tordconstnode(left).value and qword($ffffffffffffffff); + else + internalerror(2013122301); + end; + { '0 shl x' and '0 shr x' are 0 } + if (lvalue=0) and + ((cs_opt_level4 in current_settings.optimizerswitches) or + not might_have_sideeffects(right)) then + result:=cordconstnode.create(0,resultdef,true); end; end;