1
# DP: Fix PR middle-end/43323 wrong-code for bitfields
3
This patch fixes PR middle-end/43323, wrong-code for bitfields
4
and type casts. This is a 4.4 regression, 4.3 and 4.5 work.
6
For 4.5 the bug was fixed by r148631, Adam Nemet's STRIP_NOPS
7
conversion patch. This patch backports that patch to 4.4, and
8
adds the new PR43323 test case.
10
Bootstrapped and regression tested with all default languages
11
on {i686,x86_64,sparc64,powerpc64}-unknown-linux and
12
armv5tel-unknown-linux-gnueabi. Both Adam's original test case
13
and the new PR43323 one pass with this patch but fail without it.
15
Ok for 4.4? (I don't have svn write access.)
19
2010-03-13 Mikael Pettersson <mikpe@it.uu.se>
23
Backport from mainline:
24
2009-06-17 Adam Nemet <anemet@caviumnetworks.com>
26
* tree.h (STRIP_NOPS, STRIP_SIGN_NOPS,
27
STRIP_USELESS_TYPE_CONVERSION): Use tree_strip_nop_conversions,
28
tree_strip_sign_nop_conversions and
29
tree_ssa_strip_useless_type_conversions rather than stripping
31
(tree_strip_nop_conversions, tree_strip_sign_nop_conversions):
33
* gimple.h (tree_ssa_strip_useless_type_conversions): Declare it.
34
* tree-ssa.c (tree_ssa_strip_useless_type_conversions): New function.
35
* tree.c (tree_nop_conversion, tree_sign_nop_conversion,
36
tree_strip_nop_conversions, tree_strip_sign_nop_conversions): New
41
2010-03-13 Mikael Pettersson <mikpe@it.uu.se>
45
* gcc.c-torture/execute/pr43323.c: New test.
47
Backport from mainline:
48
2009-06-17 Adam Nemet <anemet@caviumnetworks.com>
50
* gcc.c-torture/execute/bitfld-5.c: New test.
52
diff -rupN a/src/gcc/gimple.h b/src/gcc/gimple.h
53
--- a/src/gcc/gimple.h 2009-05-18 12:13:43.000000000 +0200
54
+++ b/src/gcc/gimple.h 2010-03-13 10:59:57.000000000 +0100
55
@@ -1020,6 +1020,7 @@ extern void gimple_add_to_addresses_take
58
extern bool tree_ssa_useless_type_conversion (tree);
59
+extern tree tree_ssa_strip_useless_type_conversions (tree);
60
extern bool useless_type_conversion_p (tree, tree);
61
extern bool types_compatible_p (tree, tree);
63
diff -rupN a/src/gcc/testsuite/gcc.c-torture/execute/bitfld-5.c b/src/gcc/testsuite/gcc.c-torture/execute/bitfld-5.c
64
--- a/src/gcc/testsuite/gcc.c-torture/execute/bitfld-5.c 1970-01-01 01:00:00.000000000 +0100
65
+++ b/src/gcc/testsuite/gcc.c-torture/execute/bitfld-5.c 2010-03-13 10:59:57.000000000 +0100
67
+/* See http://gcc.gnu.org/ml/gcc/2009-06/msg00072.html. */
69
+extern void abort (void);
73
+ unsigned long long a:2;
74
+ unsigned long long b:40;
75
+ unsigned long long c:22;
78
+__attribute__ ((noinline)) void
79
+g (unsigned long long a, unsigned long long b)
86
+__attribute__ ((noinline)) void
87
+f (struct s s, unsigned long long b)
90
+ g (((unsigned long long) (s.b-8)) + 8, b);
96
+ struct s s = {1, 10, 3};
97
+ struct s t = {1, 2, 3};
99
+ f (t, 0x10000000002);
102
diff -rupN a/src/gcc/testsuite/gcc.c-torture/execute/pr43323.c b/src/gcc/testsuite/gcc.c-torture/execute/pr43323.c
103
--- a/src/gcc/testsuite/gcc.c-torture/execute/pr43323.c 1970-01-01 01:00:00.000000000 +0100
104
+++ b/src/gcc/testsuite/gcc.c-torture/execute/pr43323.c 2010-03-13 10:59:57.000000000 +0100
106
+/* PR middle-end/43323 */
108
+extern void abort (void);
112
+ struct { unsigned bar:1; } foo;
114
+ if ((unsigned char)(foo.bar * 0xfe) != 0xfeu)
116
+ if ((unsigned char)(foo.bar * 0xff) != 0xffu)
120
diff -rupN a/src/gcc/tree-ssa.c b/src/gcc/tree-ssa.c
121
--- a/src/gcc/tree-ssa.c 2009-08-03 21:27:32.000000000 +0200
122
+++ b/src/gcc/tree-ssa.c 2010-03-13 10:59:57.000000000 +0100
123
@@ -1271,6 +1271,18 @@ tree_ssa_useless_type_conversion (tree e
127
+/* Strip conversions from EXP according to
128
+ tree_ssa_useless_type_conversion and return the resulting
132
+tree_ssa_strip_useless_type_conversions (tree exp)
134
+ while (tree_ssa_useless_type_conversion (exp))
135
+ exp = TREE_OPERAND (exp, 0);
140
/* Internal helper for walk_use_def_chains. VAR, FN and DATA are as
141
described in walk_use_def_chains.
142
diff -rupN a/src/gcc/tree.c b/src/gcc/tree.c
143
--- a/src/gcc/tree.c 2009-07-20 16:39:32.000000000 +0200
144
+++ b/src/gcc/tree.c 2010-03-13 10:59:57.000000000 +0100
145
@@ -9094,6 +9094,79 @@ tree_nonartificial_location (tree exp)
146
return EXPR_LOCATION (exp);
149
+/* Return true iff conversion in EXP generates no instruction. Mark
150
+ it inline so that we fully inline into the stripping functions even
151
+ though we have two uses of this function. */
154
+tree_nop_conversion (const_tree exp)
156
+ tree outer_type, inner_type;
158
+ if (!CONVERT_EXPR_P (exp)
159
+ && TREE_CODE (exp) != NON_LVALUE_EXPR)
161
+ if (TREE_OPERAND (exp, 0) == error_mark_node)
164
+ outer_type = TREE_TYPE (exp);
165
+ inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
167
+ /* Use precision rather then machine mode when we can, which gives
168
+ the correct answer even for submode (bit-field) types. */
169
+ if ((INTEGRAL_TYPE_P (outer_type)
170
+ || POINTER_TYPE_P (outer_type)
171
+ || TREE_CODE (outer_type) == OFFSET_TYPE)
172
+ && (INTEGRAL_TYPE_P (inner_type)
173
+ || POINTER_TYPE_P (inner_type)
174
+ || TREE_CODE (inner_type) == OFFSET_TYPE))
175
+ return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
177
+ /* Otherwise fall back on comparing machine modes (e.g. for
178
+ aggregate types, floats). */
179
+ return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
182
+/* Return true iff conversion in EXP generates no instruction. Don't
183
+ consider conversions changing the signedness. */
186
+tree_sign_nop_conversion (const_tree exp)
188
+ tree outer_type, inner_type;
190
+ if (!tree_nop_conversion (exp))
193
+ outer_type = TREE_TYPE (exp);
194
+ inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
196
+ return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
197
+ && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
200
+/* Strip conversions from EXP according to tree_nop_conversion and
201
+ return the resulting expression. */
204
+tree_strip_nop_conversions (tree exp)
206
+ while (tree_nop_conversion (exp))
207
+ exp = TREE_OPERAND (exp, 0);
211
+/* Strip conversions from EXP according to tree_sign_nop_conversion
212
+ and return the resulting expression. */
215
+tree_strip_sign_nop_conversions (tree exp)
217
+ while (tree_sign_nop_conversion (exp))
218
+ exp = TREE_OPERAND (exp, 0);
223
/* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
225
diff -rupN a/src/gcc/tree.h b/src/gcc/tree.h
226
--- a/src/gcc/tree.h 2009-06-02 09:18:16.000000000 +0200
227
+++ b/src/gcc/tree.h 2010-03-13 10:59:57.000000000 +0100
228
@@ -974,30 +974,17 @@ extern void omp_clause_range_check_faile
232
-/* Given an expression as a tree, strip any NON_LVALUE_EXPRs and NOP_EXPRs
233
- that don't change the machine mode. */
234
+/* Given an expression as a tree, strip any conversion that generates
235
+ no instruction. Accepts both tree and const_tree arguments since
236
+ we are not modifying the tree itself. */
238
-#define STRIP_NOPS(EXP) \
239
- while ((CONVERT_EXPR_P (EXP) \
240
- || TREE_CODE (EXP) == NON_LVALUE_EXPR) \
241
- && TREE_OPERAND (EXP, 0) != error_mark_node \
242
- && (TYPE_MODE (TREE_TYPE (EXP)) \
243
- == TYPE_MODE (TREE_TYPE (TREE_OPERAND (EXP, 0))))) \
244
- (EXP) = TREE_OPERAND (EXP, 0)
245
+#define STRIP_NOPS(EXP) \
246
+ (EXP) = tree_strip_nop_conversions (CONST_CAST_TREE (EXP))
248
/* Like STRIP_NOPS, but don't let the signedness change either. */
250
#define STRIP_SIGN_NOPS(EXP) \
251
- while ((CONVERT_EXPR_P (EXP) \
252
- || TREE_CODE (EXP) == NON_LVALUE_EXPR) \
253
- && TREE_OPERAND (EXP, 0) != error_mark_node \
254
- && (TYPE_MODE (TREE_TYPE (EXP)) \
255
- == TYPE_MODE (TREE_TYPE (TREE_OPERAND (EXP, 0)))) \
256
- && (TYPE_UNSIGNED (TREE_TYPE (EXP)) \
257
- == TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (EXP, 0)))) \
258
- && (POINTER_TYPE_P (TREE_TYPE (EXP)) \
259
- == POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (EXP, 0))))) \
260
- (EXP) = TREE_OPERAND (EXP, 0)
261
+ (EXP) = tree_strip_sign_nop_conversions (CONST_CAST_TREE (EXP))
263
/* Like STRIP_NOPS, but don't alter the TREE_TYPE either. */
265
@@ -1012,9 +999,8 @@ extern void omp_clause_range_check_faile
266
/* Remove unnecessary type conversions according to
267
tree_ssa_useless_type_conversion. */
269
-#define STRIP_USELESS_TYPE_CONVERSION(EXP) \
270
- while (tree_ssa_useless_type_conversion (EXP)) \
271
- EXP = TREE_OPERAND (EXP, 0)
272
+#define STRIP_USELESS_TYPE_CONVERSION(EXP) \
273
+ (EXP) = tree_ssa_strip_useless_type_conversions (EXP)
275
/* Nonzero if TYPE represents an integral type. Note that we do not
276
include COMPLEX types here. Keep these checks in ascending code
277
@@ -4702,6 +4688,8 @@ extern bool stdarg_p (tree);
278
extern bool prototype_p (tree);
279
extern int function_args_count (tree);
280
extern bool auto_var_in_fn_p (const_tree, const_tree);
281
+extern tree tree_strip_nop_conversions (tree);
282
+extern tree tree_strip_sign_nop_conversions (tree);
285
extern tree unshare_expr (tree);