--- /dev/null
+/* Disable warnings about __sync_nand_and_fetch. */
+/* { dg-options "-w" } */
+/* PR tree-optimization/122588 */
+
+int i;
+char c;
+
+static inline __attribute__((__always_inline__))
+void foo0 (int a)
+{
+l5:
+ __sync_nand_and_fetch (&i, 0);
+ int x = __builtin_memcmp_eq (&a, 0, 4);
+ if (__builtin_iseqsig (x, 0.))
+ goto l5;
+ if (a)
+ __builtin_unreachable ();
+ c = a;
+}
+
+int
+main ()
+{
+ foo0 (1);
+}
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fgimple" } */
+/* PR tree-optimization/122588 */
+
+/* The removal of unreachable blocks should not
+ change blocks which have already become true/false.
+ The function below was is an example of that. And
+ forwprop does not go into non-executable blocks
+ so the statement `t = _1;` was still holding the
+ old reference. */
+
+int t;
+
+__GIMPLE(ssa,startwith("forwprop4")) void g(void)
+{
+ int _1;
+ __BB(2):
+ _1 = 1;
+ if (_1 != 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ __builtin_unreachable ();
+
+ __BB(4):
+ t = _1;
+ return;
+}
stmt = gsi_stmt (gsi);
if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
{
- if (e->flags & EDGE_TRUE_VALUE)
+ /* If the condition is already true/false
+ ignore it. This can happen during copy prop of forwprop. */
+ if (gimple_cond_true_p (cond_stmt)
+ || gimple_cond_false_p (cond_stmt))
+ continue;
+ else if (e->flags & EDGE_TRUE_VALUE)
gimple_cond_make_false (cond_stmt);
else if (e->flags & EDGE_FALSE_VALUE)
gimple_cond_make_true (cond_stmt);