diff --git a/compiler/optcse.pas b/compiler/optcse.pas
index 79b52e7afa..ef65859fe6 100644
--- a/compiler/optcse.pas
+++ b/compiler/optcse.pas
@@ -234,6 +234,17 @@ unit optcse;
               DFASetExclude(plists(arg)^.avail,i);
             result:=fen_norecurse_false;
           end;
+{$ifdef cpuhighleveltarget}
+          { The high level targets use the functionality from ncgnstld for
+            nested accesses, and that one stores the complete location of the
+            nested variable in tloadnode.left rather than only the location of
+            the parent context containing it. This causes problems with the
+            CSE in case the nested variable is used as an lvalue, so disable
+            CSE in that case
+          }
+          if (n.nodetype=loadn) and assigned(tloadnode(n).left) then
+            result:=fen_norecurse_false;
+{$endif}
        end;
 
 
diff --git a/tests/test/jvm/tnestproc.pp b/tests/test/jvm/tnestproc.pp
index 4a86aaf636..28262b9653 100644
--- a/tests/test/jvm/tnestproc.pp
+++ b/tests/test/jvm/tnestproc.pp
@@ -34,14 +34,14 @@ procedure outer(var para: byte);
   
     procedure doubleinner;
       begin
-        b:=3;
+        b:=b+1;
       end;
       
     begin
+      b:=2;
       doubleinner;
       if b<>3 then
         raise JLException.Create('b');
-      inner;
     end;
 
   begin
@@ -52,6 +52,7 @@ procedure outer(var para: byte);
       raise JLException.Create('a2');
     if para<>3 then
       raise JLException.Create('para2');
+    inner2;
   end;
 
 var