Browse Source

* do not copy the full while loop body when converting while loops into if/repeat loops to avoid
the hazzle of copying labels, resolves #41440

florian 1 day ago
parent
commit
5e44cfa486
2 changed files with 26 additions and 5 deletions
  1. 6 5
      compiler/nflw.pas
  2. 20 0
      tests/webtbs/tw41440.pp

+ 6 - 5
compiler/nflw.pas

@@ -1368,17 +1368,18 @@ implementation
                   taddnode(left).left.isequal(tcallparanode(tinlinenode(p).left).left) and
                   taddnode(left).left.isequal(tcallparanode(tinlinenode(p).left).left) and
                   not(assigned(tcallparanode(tinlinenode(p).left).right)) then
                   not(assigned(tcallparanode(tinlinenode(p).left).right)) then
                   begin
                   begin
-                    result:=cifnode.create_internal(left.getcopy,getcopy,nil);
-                    include(twhilerepeatnode(tifnode(result).right).loopflags,lnf_checknegate);
-                    exclude(twhilerepeatnode(tifnode(result).right).loopflags,lnf_testatbegin);
+                    result:=cifnode.create_internal(left.getcopy,cwhilerepeatnode.create(left,right,false,true),nil);
+                    left:=nil;
+                    right:=nil;
                     twhilerepeatnode(tifnode(result).right).left.nodetype:=equaln;
                     twhilerepeatnode(tifnode(result).right).left.nodetype:=equaln;
                   end;
                   end;
               end
               end
             else if not(cs_opt_size in current_settings.optimizerswitches) and
             else if not(cs_opt_size in current_settings.optimizerswitches) and
               (node_complexity(left)<=3) then
               (node_complexity(left)<=3) then
               begin
               begin
-                result:=cifnode.create_internal(left.getcopy,getcopy,nil);
-                exclude(twhilerepeatnode(tifnode(result).right).loopflags,lnf_testatbegin);
+                result:=cifnode.create_internal(left.getcopy,cwhilerepeatnode.create(left,right,false,false),nil);
+                left:=nil;
+                right:=nil;
               end;
               end;
           end;
           end;
       end;
       end;

+ 20 - 0
tests/webtbs/tw41440.pp

@@ -0,0 +1,20 @@
+{ %OPT=-O3 -Sg }
+program app;
+
+procedure kek(value: UInt32);
+label
+  mylabel;
+begin
+  while value shr 24 = 0 do
+  begin
+mylabel:
+  end;
+
+  if Random>0.0 then
+  begin
+    goto mylabel;
+  end;
+end;
+
+begin
+end.