toolchain/gcc: fix an ICE on ARM (PR58595)
[openwrt.git] / toolchain / gcc / patches / 4.8-linaro / 951-arm_bug_58595.patch
1 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58595
2
3 --- /dev/null
4 +++ b/gcc/testsuite/gcc.dg/tls/pr58595.c
5 @@ -0,0 +1,28 @@
6 +/* PR target/58595 */
7 +/* { dg-do run } */
8 +/* { dg-options "-O2" } */
9 +/* { dg-additional-options "-fpic" { target fpic } } */
10 +/* { dg-require-effective-target tls } */
11 +/* { dg-require-effective-target sync_int_long } */
12 +
13 +struct S { unsigned long a, b; };
14 +__thread struct S s;
15 +void bar (unsigned long *);
16 +
17 +__attribute__((noinline)) void
18 +foo (void)
19 +{
20 +  int i;
21 +  for (i = 0; i < 10; i++)
22 +    __sync_fetch_and_add (&s.b, 1L);
23 +}
24 +
25 +int
26 +main ()
27 +{
28 +  s.b = 12;
29 +  foo ();
30 +  if (s.b != 22)
31 +    __builtin_abort ();
32 +  return 0;
33 +}
34 --- a/gcc/config/arm/arm.c
35 +++ b/gcc/config/arm/arm.c
36 @@ -231,7 +231,6 @@ static tree arm_gimplify_va_arg_expr (tr
37  static void arm_option_override (void);
38  static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
39  static bool arm_cannot_copy_insn_p (rtx);
40 -static bool arm_tls_symbol_p (rtx x);
41  static int arm_issue_rate (void);
42  static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
43  static bool arm_output_addr_const_extra (FILE *, rtx);
44 @@ -6665,6 +6664,32 @@ legitimize_tls_address (rtx x, rtx reg)
45  rtx
46  arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
47  {
48 +  if (arm_tls_referenced_p (x))
49 +    {
50 +      rtx addend = NULL;
51 +
52 +      if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS)
53 +       {
54 +         addend = XEXP (XEXP (x, 0), 1);
55 +         x = XEXP (XEXP (x, 0), 0);
56 +       }
57 +
58 +      if (GET_CODE (x) != SYMBOL_REF)
59 +       return x;
60 +
61 +      gcc_assert (SYMBOL_REF_TLS_MODEL (x) != 0);
62 +
63 +      x = legitimize_tls_address (x, NULL_RTX);
64 +
65 +      if (addend)
66 +       {
67 +         x = gen_rtx_PLUS (SImode, x, addend);
68 +         orig_x = x;
69 +       }
70 +      else
71 +       return x;
72 +    }
73 +
74    if (!TARGET_ARM)
75      {
76        /* TODO: legitimize_address for Thumb2.  */
77 @@ -6673,9 +6698,6 @@ arm_legitimize_address (rtx x, rtx orig_
78        return thumb_legitimize_address (x, orig_x, mode);
79      }
80  
81 -  if (arm_tls_symbol_p (x))
82 -    return legitimize_tls_address (x, NULL_RTX);
83 -
84    if (GET_CODE (x) == PLUS)
85      {
86        rtx xop0 = XEXP (x, 0);
87 @@ -6787,9 +6809,6 @@ arm_legitimize_address (rtx x, rtx orig_
88  rtx
89  thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
90  {
91 -  if (arm_tls_symbol_p (x))
92 -    return legitimize_tls_address (x, NULL_RTX);
93 -
94    if (GET_CODE (x) == PLUS
95        && CONST_INT_P (XEXP (x, 1))
96        && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
97 @@ -7080,20 +7099,6 @@ thumb_legitimize_reload_address (rtx *x_
98  
99  /* Test for various thread-local symbols.  */
100  
101 -/* Return TRUE if X is a thread-local symbol.  */
102 -
103 -static bool
104 -arm_tls_symbol_p (rtx x)
105 -{
106 -  if (! TARGET_HAVE_TLS)
107 -    return false;
108 -
109 -  if (GET_CODE (x) != SYMBOL_REF)
110 -    return false;
111 -
112 -  return SYMBOL_REF_TLS_MODEL (x) != 0;
113 -}
114 -
115  /* Helper for arm_tls_referenced_p.  */
116  
117  static int