bool target_firstprivatize_array_bases;
   bool add_safelen1;
   bool order_concurrent;
+  bool has_depend;
   int defaultmap[4];
 };
 
              remove = true;
              break;
            }
+         if (code == OMP_TASK)
+           ctx->has_depend = true;
          break;
 
        case OMP_CLAUSE_TO:
            return 0;
        }
       code = OMP_CLAUSE_SHARED;
+      /* Don't optimize shared into firstprivate for read-only vars
+        on tasks with depend clause, we shouldn't try to copy them
+        until the dependencies are satisfied.  */
+      if (gimplify_omp_ctxp->has_depend)
+       flags |= GOVD_WRITTEN;
     }
   else if (flags & GOVD_PRIVATE)
     code = OMP_CLAUSE_PRIVATE;
                  OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
                  OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
                }
+              if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
+                 && ctx->has_depend
+                 && DECL_P (decl))
+               n->value |= GOVD_WRITTEN;
              if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
                  && (n->value & GOVD_WRITTEN) == 0
                  && DECL_P (decl)
 
--- /dev/null
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main ()
+{
+  int x = 0, y = 0;
+  #pragma omp parallel shared(x, y)
+  #pragma omp master
+  {
+    #pragma omp task depend(out:y) shared(x, y)
+    {
+      sleep (1);
+      x = 1;
+      y = 1;
+    }
+    #pragma omp task depend(inout:y) shared(x, y)
+    {
+      if (x != 1 || y != 1)
+       abort ();
+      y++;
+    }
+  }
+  if (x != 1 || y != 2)
+    abort ();
+  x = 0;
+  y = 0;
+  #pragma omp parallel
+  #pragma omp master
+  {
+    #pragma omp task depend(out:y)
+    {
+      sleep (1);
+      x = 1;
+      y = 1;
+    }
+    #pragma omp task depend(inout:y)
+    {
+      if (x != 1 || y != 1)
+       abort ();
+      y++;
+    }
+  }
+  if (x != 1 || y != 2)
+    abort ();
+  return 0;
+}