2018-01-20 Jakub Jelinek <jakub@redhat.com>
+ PR middle-end/83945
+ * tree-emutls.c: Include gimplify.h.
+ (lower_emutls_2): New function.
+ (lower_emutls_1): If ADDR_EXPR is a gimple invariant and walk_tree
+ with lower_emutls_2 callback finds some TLS decl in it, unshare_expr
+ it before further processing.
+
PR target/83930
* simplify-rtx.c (simplify_binary_operation_1) <case UMOD>: Use
UINTVAL (trueop1) instead of INTVAL (op1).
--- /dev/null
+/* PR middle-end/83945 */
+/* { dg-do compile { target tls } } */
+/* { dg-options "-O2" } */
+
+struct S { int a[1]; };
+__thread struct T { int c; } e;
+int f;
+void bar (int);
+
+void
+foo (int f, int x)
+{
+ struct S *h = (struct S *) &e.c;
+ for (;;)
+ {
+ int *a = h->a, i;
+ for (i = x; i; i--)
+ bar (a[f]);
+ bar (a[f]);
+ }
+}
#include "gimple-walk.h"
#include "langhooks.h"
#include "tree-iterator.h"
+#include "gimplify.h"
/* Whenever a target does not support thread-local storage (TLS) natively,
we can emulate it with some run-time support in libgcc. This will in
return addr;
}
+/* Callback for lower_emutls_1, return non-NULL if there is any TLS
+ VAR_DECL in the subexpressions. */
+
+static tree
+lower_emutls_2 (tree *ptr, int *walk_subtrees, void *)
+{
+ tree t = *ptr;
+ if (TREE_CODE (t) == VAR_DECL)
+ return DECL_THREAD_LOCAL_P (t) ? t : NULL_TREE;
+ else if (!EXPR_P (t))
+ *walk_subtrees = 0;
+ return NULL_TREE;
+}
+
/* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
Given an operand *PTR within D->STMT, if the operand references a TLS
variable, then lower the reference to a call to the runtime. Insert
{
bool save_changed;
+ /* Gimple invariants are shareable trees, so before changing
+ anything in them if we will need to change anything, unshare
+ them. */
+ if (is_gimple_min_invariant (t)
+ && walk_tree (&TREE_OPERAND (t, 0), lower_emutls_2, NULL, NULL))
+ *ptr = t = unshare_expr (t);
+
/* If we're allowed more than just is_gimple_val, continue. */
if (!wi->val_only)
{