diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 910eb398c9f41f231135b212b165e33712d91474..1e0a0f48f74ca59a7c46255ec8129bbe2b8091cc 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2005-03-14  Alexandre Oliva  <aoliva@redhat.com>
+
+	PR middle-end/18628
+	* cse.c (fold_rtx_mem): Don't fold a load from a jumptable into a
+	register.
+
 2005-03-14  Alexandre Oliva  <aoliva@redhat.com>
 
 	PR c++/20280
diff --git a/gcc/cse.c b/gcc/cse.c
index d7f3027e2bb48129eb776669609d7cef31fdbd8c..fd5e21ac2ee13f3b9a2a9fe78afb64e5303c46c2 100644
--- a/gcc/cse.c
+++ b/gcc/cse.c
@@ -3515,8 +3515,30 @@ fold_rtx_mem (rtx x, rtx insn)
 	    if (offset >= 0
 		&& (offset / GET_MODE_SIZE (GET_MODE (table))
 		    < XVECLEN (table, 0)))
-	      return XVECEXP (table, 0,
-			      offset / GET_MODE_SIZE (GET_MODE (table)));
+	      {
+		rtx label = XVECEXP
+		  (table, 0, offset / GET_MODE_SIZE (GET_MODE (table)));
+		rtx set;
+
+		/* If we have an insn that loads the label from the
+		   jumptable into a reg, we don't want to set the reg
+		   to the label, because this may cause a reference to
+		   the label to remain after the label is removed in
+		   some very obscure cases (PR middle-end/18628).  */
+		if (!insn)
+		  return label;
+
+		set = single_set (insn);
+
+		if (! set || SET_SRC (set) != x)
+		  return x;
+
+		/* If it's a jump, it's safe to reference the label.  */
+		if (SET_DEST (set) == pc_rtx)
+		  return label;
+
+		return x;
+	      }
 	  }
 	if (table_insn && JUMP_P (table_insn)
 	    && GET_CODE (PATTERN (table_insn)) == ADDR_DIFF_VEC)
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4e18492dbab3af61415066e5dc928187608f2e7c..3053e823e8f68ec112ecb2667e400ca7b05b2ae5 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2005-03-14  Alexandre Oliva  <aoliva@redhat.com>
+
+	* gcc.dg/pr18628.c: New.
+
 2005-03-14  Alexandre Oliva  <aoliva@redhat.com>
 
 	PR c++/20280
diff --git a/gcc/testsuite/gcc.dg/pr18628.c b/gcc/testsuite/gcc.dg/pr18628.c
new file mode 100644
index 0000000000000000000000000000000000000000..d365075b7291a3bac626879892f15b4d17eebe3d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr18628.c
@@ -0,0 +1,31 @@
+/* { dg-do link } */
+/* { dg-options "-O2" } */
+
+/* PR middle-end/18628 exposed a problem in which cse folded a load
+   from a jump table into the label that was the target of the branch.
+   Unfortunately, the indirect jump was moved to a different basic
+   block, and the LABEL_REF copied to the register wasn't enough to
+   keep the cfg from optimizing the otherwise-unused label away.  So
+   we ended up with a dangling reference to the label.  */
+
+int i;
+
+int main()
+{
+  for (;;)
+  {
+    switch (i)
+    {
+      case 0:
+      case 1:
+        return 1;
+
+      case 2:
+      case 3:
+        return 0;
+
+      case 5:
+        --i;
+    }
+  }
+}