فهرست منبع

* simplify integer operations with constnat 0 or 1

git-svn-id: trunk@7009 -
peter 18 سال پیش
والد
کامیت
ab22fd42d1
3فایلهای تغییر یافته به همراه70 افزوده شده و 0 حذف شده
  1. 1 0
      .gitattributes
  2. 46 0
      compiler/nadd.pas
  3. 23 0
      tests/webtbs/tw8573.pp

+ 1 - 0
.gitattributes

@@ -8132,6 +8132,7 @@ tests/webtbs/tw8434.pp svneol=native#text/plain
 tests/webtbs/tw8462.pp svneol=native#text/plain
 tests/webtbs/tw8513.pp svneol=native#text/plain
 tests/webtbs/tw8525.pp svneol=native#text/plain
+tests/webtbs/tw8573.pp svneol=native#text/plain
 tests/webtbs/ub1873.pp svneol=native#text/plain
 tests/webtbs/ub1883.pp svneol=native#text/plain
 tests/webtbs/uw0555.pp svneol=native#text/plain

+ 46 - 0
compiler/nadd.pas

@@ -384,6 +384,52 @@ implementation
              exit;
           end;
 
+        { Add,Sub,Mul with constant 0 or 1?  }
+        if is_constintnode(right) then
+          begin
+            if tordconstnode(right).value = 0 then
+              begin
+                case nodetype of
+                  addn,subn:
+                   result := left.getcopy;
+                  muln:
+                   result:=cordconstnode.create(0,left.resultdef,true);
+                end;
+              end
+            else if tordconstnode(right).value = 1 then
+              begin
+                case nodetype of
+                  muln:
+                   result := left.getcopy;
+                end;
+              end;
+            if assigned(result) then
+              exit;
+          end;
+        if is_constintnode(left) then
+          begin
+            if tordconstnode(left).value = 0 then
+              begin
+                case nodetype of
+                  addn:
+                   result := right.getcopy;
+                  subn:
+                   result := cunaryminusnode.create(right.getcopy);
+                  muln:
+                   result:=cordconstnode.create(0,right.resultdef,true);
+                end;
+              end
+            else if tordconstnode(left).value = 1 then
+              begin
+                case nodetype of
+                  muln:
+                   result := right.getcopy;
+                end;
+              end;
+            if assigned(result) then
+              exit;
+          end;
+
       { both real constants ? }
         if (lt=realconstn) and (rt=realconstn) then
           begin

+ 23 - 0
tests/webtbs/tw8573.pp

@@ -0,0 +1,23 @@
+program overflowbug;
+
+{$mode objfpc}{$Q+}
+
+const
+  zero=0;
+  one=1;
+
+var
+  x,y,z: cardinal;
+
+begin
+  x := 0;
+  y := one + x;
+
+  // the next line sets the carry flag, so a overflow error will be generated
+  if x>y then;
+  // here the overflow error will be generated.
+  // the addition of zero is optimized away, but the check for the carry flag
+  // is not removed, so it is using the result of the compile in line 17
+  z := zero + y;
+end.
+