diff --git a/compiler/optconstprop.pas b/compiler/optconstprop.pas
index 505d67ac76..b56fa1abbb 100644
--- a/compiler/optconstprop.pas
+++ b/compiler/optconstprop.pas
@@ -50,7 +50,7 @@ unit optconstprop;
 
       will not result in any constant propagation.
     }
-    function do_optconstpropagate(var rootnode : tnode) : tnode;
+    function do_optconstpropagate(var rootnode : tnode;var changed: boolean) : tnode;
 
   implementation
 
@@ -136,7 +136,9 @@ unit optconstprop;
                 { play safe and set the result which is check below }
                 result:=replaceBasicAssign(tfornode(n).t1, arg, tree_modified2);
                 tree_modified:=tree_modified or tree_modified2;
-                if result and (pi_dfaavailable in current_procinfo.flags) then
+                if result and (pi_dfaavailable in current_procinfo.flags) and
+                  { play safe }
+                  assigned(tfornode(n).t2.optinfo) and assigned(tassignmentnode(arg).left.optinfo) then
                   begin
                     CalcDefSum(tfornode(n).t2);
                     { the constant can propagete if is is not the counter variable ... }
@@ -146,9 +148,8 @@ unit optconstprop;
                       { and no definition in the loop? }
                       not(DFASetIn(tfornode(n).t2.optinfo^.defsum,tassignmentnode(arg).left.optinfo^.index)) then
                       begin
-                        replaceBasicAssign(tfornode(n).t2, arg, tree_modified3);
+                        result:=replaceBasicAssign(tfornode(n).t2, arg, tree_modified3);
                         tree_modified:=tree_modified or tree_modified3;
-                        result:=false;
                       end
                     else
                       result:=false;
@@ -383,22 +384,15 @@ unit optconstprop;
       end;
 
 
-    function do_optconstpropagate(var rootnode: tnode): tnode;
-      var
-        changed: boolean;
-        runsimplify : Boolean;
+    function do_optconstpropagate(var rootnode: tnode;var changed: boolean): tnode;
       begin
 {$ifdef DEBUG_CONSTPROP}
         writeln('************************ before constant propagation ***************************');
         printnode(rootnode);
 {$endif DEBUG_CONSTPROP}
-        runsimplify:=false;
-        repeat
-          changed:=false;
-          foreachnodestatic(pm_postandagain, rootnode, @propagate, @changed);
-          runsimplify:=runsimplify or changed;
-        until changed=false;
-        if runsimplify then
+        changed:=false;
+        foreachnodestatic(pm_postandagain, rootnode, @propagate, @changed);
+        if changed then
           doinlinesimplify(rootnode);
 {$ifdef DEBUG_CONSTPROP}
         writeln('************************ after constant propagation ***************************');
diff --git a/compiler/psub.pas b/compiler/psub.pas
index 6404040584..62aa910313 100644
--- a/compiler/psub.pas
+++ b/compiler/psub.pas
@@ -1199,7 +1199,7 @@ implementation
     procedure tcgprocinfo.OptimizeNodeTree;
       var
         i : integer;
-        RedoDFA: Boolean;
+        RedoDFA, changed: Boolean;
         {RedoDFA : boolean;}
       begin
        { do this before adding the entry code else the tail recursion recognition won't work,
@@ -1210,7 +1210,12 @@ implementation
          do_opttail(code,procdef);
 
        if cs_opt_constant_propagate in current_settings.optimizerswitches then
-         do_optconstpropagate(code);
+         begin
+           changed:=false;
+           repeat
+             do_optconstpropagate(code,changed);
+           until not(changed);
+         end;
 
        if (cs_opt_nodedfa in current_settings.optimizerswitches) and
          { creating dfa is not always possible }
@@ -1222,10 +1227,14 @@ implementation
            RedoDFA:=false;
 
            if cs_opt_constant_propagate in current_settings.optimizerswitches then
-             do_optconstpropagate(code);
-
-           if RedoDFA then
-             dfabuilder.redodfainfo(code);
+             begin
+               changed:=false;
+               repeat
+                 do_optconstpropagate(code,changed);
+                 if changed then
+                   dfabuilder.redodfainfo(code);
+               until not(changed);
+             end;
 
            if (cs_opt_loopstrength in current_settings.optimizerswitches)
              { our induction variable strength reduction doesn't like