diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index 21843d7870b..b3e7792a8fa 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -945,6 +945,9 @@ gimple_extract (gimple *stmt, gimple_match_op *res_op,
 		|| code == VIEW_CONVERT_EXPR)
 	      {
 		tree op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
+		/* op0 needs to be a SSA name or an min invariant. */
+		if (TREE_CODE (op0) != SSA_NAME && !is_gimple_min_invariant (op0))
+		  return false;
 		res_op->set_op (code, type, valueize_op (op0));
 		return true;
 	      }
@@ -952,6 +955,9 @@ gimple_extract (gimple *stmt, gimple_match_op *res_op,
 	      {
 		tree rhs1 = gimple_assign_rhs1 (stmt);
 		tree op0 = valueize_op (TREE_OPERAND (rhs1, 0));
+		/* op0 needs to be a SSA name or an min invariant. */
+		if (TREE_CODE (op0) != SSA_NAME && !is_gimple_min_invariant (op0))
+		  return false;
 		res_op->set_op (code, type, op0,
 				TREE_OPERAND (rhs1, 1),
 				TREE_OPERAND (rhs1, 2),
diff --git a/gcc/testsuite/gcc.dg/torture/pr116412-1.c b/gcc/testsuite/gcc.dg/torture/pr116412-1.c
new file mode 100644
index 00000000000..3bc26ecd8b8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr116412-1.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+double f(_Complex double a, _Complex double *b, int c)
+{
+  if (c) return __real__ a;
+  return __real__ *b;
+}
