浏览代码

* simplify "div -1" to unaryminusn, not just for performance reasons but
also because "div -1" is not handled correctly by the div-to-mul/shift
transformation on x86 (mantis #28702)

git-svn-id: trunk@32619 -

Jonas Maebe 9 年之前
父节点
当前提交
ed03286be5
共有 3 个文件被更改,包括 19 次插入0 次删除
  1. 1 0
      .gitattributes
  2. 10 0
      compiler/nmat.pas
  3. 8 0
      tests/webtbs/tw28702.pp

+ 1 - 0
.gitattributes

@@ -14858,6 +14858,7 @@ tests/webtbs/tw28641.pp svneol=native#text/plain
 tests/webtbs/tw2865.pp svneol=native#text/plain
 tests/webtbs/tw28650.pp svneol=native#text/pascal
 tests/webtbs/tw28674.pp svneol=native#text/pascal
+tests/webtbs/tw28702.pp svneol=native#text/plain
 tests/webtbs/tw28713.pp svneol=native#text/pascal
 tests/webtbs/tw28713b.pp svneol=native#text/pascal
 tests/webtbs/tw28718a.pp svneol=native#text/plain

+ 10 - 0
compiler/nmat.pas

@@ -133,6 +133,16 @@ implementation
                 { recover }
                 tordconstnode(right).value := 1;
               end;
+            { the following simplification is also required for correctness
+              on x86, as its transformation of divisions by constants to
+              multiplications and shifts does not handle -1 correctly }
+            if (rv=-1) and
+               (nodetype=divn) then
+              begin
+                result:=cunaryminusnode.create(left);
+                left:=nil;
+                exit;
+              end;
             if (nf_isomod in flags) and
               (rv<=0) then
                begin

+ 8 - 0
tests/webtbs/tw28702.pp

@@ -0,0 +1,8 @@
+var
+  l: longint;
+begin
+  l:=5;
+  { dummy random to prevent constant propagation }
+  if ((l+random(1))div -1)<>-5 then
+    halt(1);
+end.