diff --git a/gcc/shrink-wrap.c b/gcc/shrink-wrap.c
index 5e60f34f74927d8c99e5abbe6723e2c618e2bb04..35149ecd84c65487f1afdae056e5d68b486c2542 100644
--- a/gcc/shrink-wrap.c
+++ b/gcc/shrink-wrap.c
@@ -781,7 +781,7 @@ try_shrink_wrapping (edge *entry_edge, rtx_insn *prologue_seq)
   unsigned max_grow_size = get_uncond_jump_length ();
   max_grow_size *= param_max_grow_copy_bb_insns;
 
-  while (!vec.is_empty () && pro != entry)
+  while (pro != entry)
     {
       while (pro != entry && !can_get_prologue (pro, prologue_clobbered))
 	{
@@ -791,6 +791,9 @@ try_shrink_wrapping (edge *entry_edge, rtx_insn *prologue_seq)
 	    vec.quick_push (pro);
 	}
 
+      if (vec.is_empty ())
+	break;
+
       basic_block bb = vec.pop ();
       if (!can_dup_for_shrink_wrapping (bb, pro, max_grow_size))
 	while (!dominated_by_p (CDI_DOMINATORS, bb, pro))
diff --git a/gcc/testsuite/gcc.dg/pr103860.c b/gcc/testsuite/gcc.dg/pr103860.c
new file mode 100644
index 0000000000000000000000000000000000000000..15eee558083dfe9b3f781be4592d5419f3f2f99f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr103860.c
@@ -0,0 +1,31 @@
+/* PR rtl-optimization/103860 */
+/* { dg-do run } */
+/* { dg-options "-O3" } */
+/* { dg-additional-options "-fPIC" { target fpic } } */
+
+static int d, *e;
+int f;
+
+__attribute__((noinline)) signed char
+foo (signed char b, signed char c)
+{
+  return b + c;
+}
+
+int
+main ()
+{
+  signed char l;
+  for (l = -1; l; l = foo (l, 1))
+    {
+      while (d < 0)
+	;
+      if (d > 0)
+	{
+	  f = 0;
+	  *e = 0;
+	}
+    }
+  d = 0;
+  return 0;
+}