--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiprop1-details" } */
+
+/* PR tree-optimization/60183 */
+
+unsigned char c;
+extern unsigned char d;
+int j = 2;
+
+unsigned long
+foo (void)
+{
+ unsigned char *y = &c;
+ int i;
+ unsigned w = 0;
+ for (i = 0; i < j; i++)
+ {
+ w = *y;
+ y = &d;
+ }
+ return w;
+}
+/* the load from c/d does not trap so we should able to remove the phi. */
+/* { dg-final { scan-tree-dump "Removing dead stmt:" "phiprop1"} } */
+/* { dg-final { scan-tree-dump "Inserting PHI for result of load" "phiprop1"} } */
--- /dev/null
+/* { dg-do compile { target { weak_undefined } } } */
+/* { dg-options "-O1 -fdump-tree-phiprop1-details" } */
+/* { dg-add-options weak_undefined } */
+
+/* PR tree-optimization/60183 */
+
+unsigned char c;
+extern unsigned char d __attribute__((weak));
+int j = 2;
+
+unsigned long
+foo (void)
+{
+ unsigned char *y = &c;
+ int i;
+ unsigned w = 0;
+ for (i = 0; i < j; i++)
+ {
+ w = *y;
+ y = &d;
+ }
+ return w;
+}
+/* the load from d can trap so this should not cause a phiprop. */
+/* { dg-final { scan-tree-dump-not "Removing dead stmt:" "phiprop1"} } */
+/* { dg-final { scan-tree-dump-not "Inserting PHI for result of load" "phiprop1"} } */
return false;
tree up_vuse = NULL_TREE;
+ bool canpossible_trap = false;
/* Check if we can "cheaply" dereference all phi arguments. */
FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
{
arg = gimple_assign_rhs1 (def_stmt);
}
if (TREE_CODE (arg) == ADDR_EXPR)
- ;
+ {
+ tree decl = TREE_OPERAND (arg, 0);
+ if (!canpossible_trap)
+ canpossible_trap = tree_could_trap_p (decl);
+ }
/* When we have an SSA name see if we previously encountered a
dereference of it. */
else if (TREE_CODE (arg) == SSA_NAME
calculate_dominance_info (CDI_POST_DOMINATORS);
/* Only replace loads in blocks that post-dominate the PHI node. That
- makes sure we don't end up speculating loads. */
- if (!dominated_by_p (CDI_POST_DOMINATORS,
- bb, gimple_bb (use_stmt)))
+ makes sure we don't end up speculating trapping loads. */
+ if (canpossible_trap
+ && !dominated_by_p (CDI_POST_DOMINATORS,
+ bb, gimple_bb (use_stmt)))
continue;
/* Check whether this is a load of *ptr. */