Subject: [PATCH] Don't add iv_cand for iv_use with non mode-precision type.

Precisely, no iv_cand is added for iv_use if it's not integer/pointer
type, or non-mode precision type.  For such cases, iv_use converted
from other is handled specially by adding iv_cand for the source op.

gcc/
    PR tree-optimization/93674
    * tree-ssa-loop-ivopts.c (add_iv_candidate_for_use): Don't add iv_cand
    for iv_use which is not integer or pointer type, or non-mode precision
    type.  For such cases, add iv_cand for source operand if it's
    converted from other one.

gcc/testsuite/
    PR tree-optimization/93674
    * g++.dg/pr93674.C: New test.
---
 gcc/testsuite/g++.dg/pr93674.C | 16 ++++++++++++++++
 gcc/tree-ssa-loop-ivopts.c     | 26 +++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/pr93674.C

diff --git a/gcc/testsuite/g++.dg/pr93674.C b/gcc/testsuite/g++.dg/pr93674.C
new file mode 100644
index 0000000..8c59f1b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr93674.C
@@ -0,0 +1,16 @@
+// { dg-do compile }
+// { dg-options "-O3 -std=c++14 -fstrict-enums -pedantic -fdump-tree-optimized" }
+enum some_enum { x = 1000 };
+void sink(some_enum);
+
+int __attribute__((noinline)) func() {
+  int sum = 0;
+  for (int i = 0; i < 3; ++i) {
+      for (int j = 3; j >= 0; --j) {
+          sink((some_enum)(i + j));
+      }
+  }
+  return sum;
+}
+
+// { dg-final { scan-tree-dump-not "some_enum ivtmp" "optimized" } }
diff --git a/gcc/tree-ssa-loop-ivopts.c b/gcc/tree-ssa-loop-ivopts.c
index 1ce6d8b..8091a7d 100644
--- a/gcc/tree-ssa-loop-ivopts.c
+++ b/gcc/tree-ssa-loop-ivopts.c
@@ -3479,8 +3479,32 @@ add_iv_candidate_for_use (struct ivopts_data *data, struct iv_use *use)
 {
   poly_uint64 offset;
   tree base;
-  tree basetype;
   struct iv *iv = use->iv;
+  tree basetype = TREE_TYPE (iv->base);
+
+  /* Don't add candidate for iv_use with non integer, pointer or non-mode
+     precision types.  */
+  if ((TREE_CODE (basetype) != INTEGER_TYPE && !POINTER_TYPE_P (basetype))
+      || !type_has_mode_precision_p (basetype))
+    {
+      if (!CONVERT_EXPR_P (iv->base))
+	return;
+
+      /* Specially handle such iv_use if it's converted from other ones by
+	 adding candidate for the source iv.  */
+      base = TREE_OPERAND (iv->base, 0);
+      basetype = TREE_TYPE (base);
+      if (!INTEGRAL_TYPE_P (basetype))
+	return;
+
+      tree step = iv->step;
+
+      if (basetype == generic_type_for (basetype))
+	step = fold_convert (basetype, step);
+
+      add_candidate (data, base, step, false, NULL);
+      return;
+    }
 
   add_candidate (data, iv->base, iv->step, false, use);
 
-- 
1.8.3.1

