68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 /* Post-reload compare elimination.
|
145
|
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 This file is part of GCC.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 GCC is free software; you can redistribute it and/or modify it under
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 the terms of the GNU General Public License as published by the Free
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 Software Foundation; either version 3, or (at your option) any later
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 version.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 for more details.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 You should have received a copy of the GNU General Public License
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 along with GCC; see the file COPYING3. If not see
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 <http://www.gnu.org/licenses/>. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 /* There is a set of targets whose general-purpose move or addition
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 instructions clobber the flags. These targets cannot split their
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 CBRANCH/CSTORE etc patterns before reload is complete, lest reload
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 itself insert these instructions in between the flags setter and user.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 Because these targets cannot split the compare from the use, they
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 cannot make use of the comparison elimination offered by the combine pass.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 This is a small pass intended to provide comparison elimination similar to
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 what is available via NOTICE_UPDATE_CC for cc0 targets. This should help
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 encourage cc0 targets to convert to an explicit post-reload representation
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 of the flags.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 This pass assumes:
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 (1) All comparison patterns are represented as
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37
|
111
|
38 [(set (reg:CC) (compare:CC (reg) (reg_or_immediate)))]
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 (2) All insn patterns that modify the flags are represented as
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 [(set (reg) (operation)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 (clobber (reg:CC))]
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 (3) If an insn of form (2) can usefully set the flags, there is
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 another pattern of the form
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47
|
111
|
48 [(set (reg:CCM) (compare:CCM (operation) (immediate)))
|
|
49 (set (reg) (operation)]
|
|
50
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 The mode CCM will be chosen as if by SELECT_CC_MODE.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 This could be handled as a future enhancement.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 #include "config.h"
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 #include "system.h"
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 #include "coretypes.h"
|
111
|
60 #include "backend.h"
|
|
61 #include "target.h"
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 #include "rtl.h"
|
111
|
63 #include "df.h"
|
|
64 #include "memmodel.h"
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 #include "tm_p.h"
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 #include "insn-config.h"
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 #include "recog.h"
|
111
|
68 #include "emit-rtl.h"
|
|
69 #include "cfgrtl.h"
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 #include "tree-pass.h"
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 #include "domwalk.h"
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 /* These structures describe a comparison and how it is used. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 /* The choice of maximum 3 uses comes from wanting to eliminate the two
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 duplicate compares from a three-way branch on the sign of a value.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 This is also sufficient to eliminate the duplicate compare against the
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 high-part of a double-word comparison. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 #define MAX_CMP_USE 3
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 struct comparison_use
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 /* The instruction in which the result of the compare is used. */
|
111
|
85 rtx_insn *insn;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 /* The location of the flags register within the use. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 rtx *loc;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 /* The comparison code applied against the flags register. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 enum rtx_code code;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 };
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 struct comparison
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 /* The comparison instruction. */
|
111
|
95 rtx_insn *insn;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 /* The insn prior to the comparison insn that clobbers the flags. */
|
111
|
98 rtx_insn *prev_clobber;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99
|
131
|
100 /* The insn prior to the comparison insn that sets in_a REG. */
|
|
101 rtx_insn *in_a_setter;
|
|
102
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 /* The two values being compared. These will be either REGs or
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 constants. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105 rtx in_a, in_b;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106
|
111
|
107 /* The REG_EH_REGION of the comparison. */
|
|
108 rtx eh_note;
|
|
109
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 /* Information about how this comparison is used. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111 struct comparison_use uses[MAX_CMP_USE];
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113 /* The original CC_MODE for this comparison. */
|
111
|
114 machine_mode orig_mode;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 /* The number of uses identified for this comparison. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
117 unsigned short n_uses;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 /* True if not all uses of this comparison have been identified.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 This can happen either for overflowing the array above, or if
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
121 the flags register is used in some unusual context. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 bool missing_uses;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
123
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 /* True if its inputs are still valid at the end of the block. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125 bool inputs_valid;
|
145
|
126
|
|
127 /* Whether IN_A is wrapped in a NOT before being compared. */
|
|
128 bool not_in_a;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 };
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130
|
111
|
131 static vec<comparison *> all_compares;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132
|
145
|
133 /* Return whether X is a NOT unary expression. */
|
|
134
|
|
135 static bool
|
|
136 is_not (rtx x)
|
|
137 {
|
|
138 return GET_CODE (x) == NOT;
|
|
139 }
|
|
140
|
|
141 /* Strip a NOT unary expression around X, if any. */
|
|
142
|
|
143 static rtx
|
|
144 strip_not (rtx x)
|
|
145 {
|
|
146 if (is_not (x))
|
|
147 return XEXP (x, 0);
|
|
148
|
|
149 return x;
|
|
150 }
|
|
151
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 /* Look for a "conforming" comparison, as defined above. If valid, return
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 the rtx for the COMPARE itself. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
154
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 static rtx
|
111
|
156 conforming_compare (rtx_insn *insn)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 rtx set, src, dest;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 set = single_set (insn);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 if (set == NULL)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 src = SET_SRC (set);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 if (GET_CODE (src) != COMPARE)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 dest = SET_DEST (set);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171
|
145
|
172 if (!REG_P (strip_not (XEXP (src, 0))))
|
111
|
173 return NULL;
|
|
174
|
|
175 if (CONSTANT_P (XEXP (src, 1)) || REG_P (XEXP (src, 1)))
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 return src;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177
|
111
|
178 if (GET_CODE (XEXP (src, 1)) == UNSPEC)
|
|
179 {
|
|
180 for (int i = 0; i < XVECLEN (XEXP (src, 1), 0); i++)
|
|
181 if (!REG_P (XVECEXP (XEXP (src, 1), 0, i)))
|
|
182 return NULL;
|
|
183 return src;
|
|
184 }
|
|
185
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 /* Look for a pattern of the "correct" form for an insn with a flags clobber
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190 for which we may be able to eliminate a compare later. We're not looking
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191 to validate any inputs at this time, merely see that the basic shape is
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
192 correct. The term "arithmetic" may be somewhat misleading... */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194 static bool
|
111
|
195 arithmetic_flags_clobber_p (rtx_insn *insn)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 rtx pat, x;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
199 if (!NONJUMP_INSN_P (insn))
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201 pat = PATTERN (insn);
|
111
|
202 if (asm_noperands (pat) >= 0)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207 x = XVECEXP (pat, 0, 0);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208 if (GET_CODE (x) != SET)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
210 x = SET_DEST (x);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
211 if (!REG_P (x))
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
212 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
213
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
214 x = XVECEXP (pat, 0, 1);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215 if (GET_CODE (x) == CLOBBER)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 x = XEXP (x, 0);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218 if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 return true;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
222
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
226 /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
227 it in CMP; otherwise indicate that we've missed a use. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
228
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
229 static void
|
111
|
230 find_flags_uses_in_insn (struct comparison *cmp, rtx_insn *insn)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
231 {
|
111
|
232 df_ref use;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
233
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
234 /* If we've already lost track of uses, don't bother collecting more. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
235 if (cmp->missing_uses)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
236 return;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
237
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
238 /* Find a USE of the flags register. */
|
111
|
239 FOR_EACH_INSN_USE (use, insn)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
240 if (DF_REF_REGNO (use) == targetm.flags_regnum)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
241 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
242 rtx x, *loc;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
243
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
244 /* If this is an unusual use, quit. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
245 if (DF_REF_TYPE (use) != DF_REF_REG_USE)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
246 goto fail;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
247
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
248 /* If we've run out of slots to record uses, quit. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
249 if (cmp->n_uses == MAX_CMP_USE)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
250 goto fail;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
251
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
252 /* Unfortunately the location of the flags register, while present
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
253 in the reference structure, doesn't help. We need to find the
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
254 comparison code that is outer to the actual flags use. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
255 loc = DF_REF_LOC (use);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
256 x = PATTERN (insn);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
257 if (GET_CODE (x) == PARALLEL)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
258 x = XVECEXP (x, 0, 0);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
259 x = SET_SRC (x);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
260 if (GET_CODE (x) == IF_THEN_ELSE)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
261 x = XEXP (x, 0);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
262 if (COMPARISON_P (x)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
263 && loc == &XEXP (x, 0)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
264 && XEXP (x, 1) == const0_rtx)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
265 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
266 /* We've found a use of the flags that we understand. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
267 struct comparison_use *cuse = &cmp->uses[cmp->n_uses++];
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
268 cuse->insn = insn;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
269 cuse->loc = loc;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
270 cuse->code = GET_CODE (x);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
271 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
272 else
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
273 goto fail;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
274 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
275 return;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
276
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
277 fail:
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
278 /* We failed to recognize this use of the flags register. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
279 cmp->missing_uses = true;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
280 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
281
|
111
|
282 class find_comparison_dom_walker : public dom_walker
|
|
283 {
|
|
284 public:
|
|
285 find_comparison_dom_walker (cdi_direction direction)
|
|
286 : dom_walker (direction) {}
|
|
287
|
|
288 virtual edge before_dom_children (basic_block);
|
|
289 };
|
|
290
|
|
291 /* Return true if conforming COMPARE with EH_NOTE is redundant with comparison
|
|
292 CMP and can thus be eliminated. */
|
|
293
|
|
294 static bool
|
|
295 can_eliminate_compare (rtx compare, rtx eh_note, struct comparison *cmp)
|
|
296 {
|
|
297 /* Take care that it's in the same EH region. */
|
|
298 if (cfun->can_throw_non_call_exceptions
|
|
299 && !rtx_equal_p (eh_note, cmp->eh_note))
|
|
300 return false;
|
|
301
|
|
302 /* Make sure the compare is redundant with the previous. */
|
145
|
303 if (!rtx_equal_p (strip_not (XEXP (compare, 0)), cmp->in_a)
|
111
|
304 || !rtx_equal_p (XEXP (compare, 1), cmp->in_b))
|
|
305 return false;
|
|
306
|
145
|
307 if (is_not (XEXP (compare, 0)) != cmp->not_in_a)
|
|
308 return false;
|
|
309
|
111
|
310 /* New mode must be compatible with the previous compare mode. */
|
|
311 machine_mode new_mode
|
|
312 = targetm.cc_modes_compatible (GET_MODE (compare), cmp->orig_mode);
|
|
313
|
|
314 if (new_mode == VOIDmode)
|
|
315 return false;
|
|
316
|
|
317 if (cmp->orig_mode != new_mode)
|
|
318 {
|
|
319 /* Generate new comparison for substitution. */
|
|
320 rtx flags = gen_rtx_REG (new_mode, targetm.flags_regnum);
|
|
321 rtx x = gen_rtx_COMPARE (new_mode, cmp->in_a, cmp->in_b);
|
|
322 x = gen_rtx_SET (flags, x);
|
|
323
|
|
324 if (!validate_change (cmp->insn, &PATTERN (cmp->insn), x, false))
|
|
325 return false;
|
|
326
|
|
327 cmp->orig_mode = new_mode;
|
|
328 }
|
|
329
|
|
330 return true;
|
|
331 }
|
|
332
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
333 /* Identify comparison instructions within BB. If the flags from the last
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
334 compare in the BB is live at the end of the block, install the compare
|
111
|
335 in BB->AUX. Called via dom_walker.walk (). */
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
336
|
111
|
337 edge
|
|
338 find_comparison_dom_walker::before_dom_children (basic_block bb)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
339 {
|
131
|
340 rtx_insn *insn, *next;
|
111
|
341 bool need_purge = false;
|
131
|
342 rtx_insn *last_setter[FIRST_PSEUDO_REGISTER];
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
343
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
344 /* The last comparison that was made. Will be reset to NULL
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
345 once the flags are clobbered. */
|
131
|
346 struct comparison *last_cmp = NULL;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
347
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
348 /* True iff the last comparison has not been clobbered, nor
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
349 have its inputs. Used to eliminate duplicate compares. */
|
131
|
350 bool last_cmp_valid = false;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
351
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
352 /* The last insn that clobbered the flags, if that insn is of
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
353 a form that may be valid for eliminating a following compare.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
354 To be reset to NULL once the flags are set otherwise. */
|
131
|
355 rtx_insn *last_clobber = NULL;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
356
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
357 /* Propagate the last live comparison throughout the extended basic block. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
358 if (single_pred_p (bb))
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
359 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
360 last_cmp = (struct comparison *) single_pred (bb)->aux;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
361 if (last_cmp)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
362 last_cmp_valid = last_cmp->inputs_valid;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
363 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
364
|
131
|
365 memset (last_setter, 0, sizeof (last_setter));
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
366 for (insn = BB_HEAD (bb); insn; insn = next)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
367 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
368 rtx src;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
369
|
111
|
370 next = (insn == BB_END (bb) ? NULL : NEXT_INSN (insn));
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
371 if (!NONDEBUG_INSN_P (insn))
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
372 continue;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
373
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
374 src = conforming_compare (insn);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
375 if (src)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
376 {
|
111
|
377 rtx eh_note = NULL;
|
|
378
|
|
379 if (cfun->can_throw_non_call_exceptions)
|
|
380 eh_note = find_reg_note (insn, REG_EH_REGION, NULL);
|
|
381
|
|
382 if (last_cmp_valid && can_eliminate_compare (src, eh_note, last_cmp))
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
383 {
|
111
|
384 if (eh_note)
|
|
385 need_purge = true;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
386 delete_insn (insn);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
387 continue;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
388 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
389
|
111
|
390 last_cmp = XCNEW (struct comparison);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
391 last_cmp->insn = insn;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
392 last_cmp->prev_clobber = last_clobber;
|
145
|
393 last_cmp->in_a = strip_not (XEXP (src, 0));
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
394 last_cmp->in_b = XEXP (src, 1);
|
145
|
395 last_cmp->not_in_a = is_not (XEXP (src, 0));
|
111
|
396 last_cmp->eh_note = eh_note;
|
|
397 last_cmp->orig_mode = GET_MODE (src);
|
131
|
398 if (last_cmp->in_b == const0_rtx
|
|
399 && last_setter[REGNO (last_cmp->in_a)])
|
|
400 {
|
|
401 rtx set = single_set (last_setter[REGNO (last_cmp->in_a)]);
|
|
402 if (set && rtx_equal_p (SET_DEST (set), last_cmp->in_a))
|
|
403 last_cmp->in_a_setter = last_setter[REGNO (last_cmp->in_a)];
|
|
404 }
|
111
|
405 all_compares.safe_push (last_cmp);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
406
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
407 /* It's unusual, but be prepared for comparison patterns that
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
408 also clobber an input, or perhaps a scratch. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
409 last_clobber = NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
410 last_cmp_valid = true;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
411 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
412
|
111
|
413 else
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
414 {
|
111
|
415 /* Notice if this instruction uses the flags register. */
|
|
416 if (last_cmp)
|
|
417 find_flags_uses_in_insn (last_cmp, insn);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
418
|
111
|
419 /* Notice if this instruction kills the flags register. */
|
131
|
420 df_ref def;
|
|
421 FOR_EACH_INSN_DEF (def, insn)
|
|
422 if (DF_REF_REGNO (def) == targetm.flags_regnum)
|
|
423 {
|
|
424 /* See if this insn could be the "clobber" that eliminates
|
|
425 a future comparison. */
|
|
426 last_clobber = (arithmetic_flags_clobber_p (insn)
|
|
427 ? insn : NULL);
|
111
|
428
|
131
|
429 /* In either case, the previous compare is no longer valid. */
|
|
430 last_cmp = NULL;
|
|
431 last_cmp_valid = false;
|
|
432 break;
|
|
433 }
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
434 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
435
|
131
|
436 /* Notice if any of the inputs to the comparison have changed
|
|
437 and remember last insn that sets each register. */
|
|
438 df_ref def;
|
|
439 FOR_EACH_INSN_DEF (def, insn)
|
|
440 {
|
|
441 if (last_cmp_valid
|
|
442 && (DF_REF_REGNO (def) == REGNO (last_cmp->in_a)
|
|
443 || (REG_P (last_cmp->in_b)
|
|
444 && DF_REF_REGNO (def) == REGNO (last_cmp->in_b))))
|
|
445 last_cmp_valid = false;
|
|
446 last_setter[DF_REF_REGNO (def)] = insn;
|
|
447 }
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
448 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
449
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
450 /* Remember the live comparison for subsequent members of
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
451 the extended basic block. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
452 if (last_cmp)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
453 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
454 bb->aux = last_cmp;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
455 last_cmp->inputs_valid = last_cmp_valid;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
456
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
457 /* Look to see if the flags register is live outgoing here, and
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
458 incoming to any successor not part of the extended basic block. */
|
111
|
459 if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
460 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
461 edge e;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
462 edge_iterator ei;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
463
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
464 FOR_EACH_EDGE (e, ei, bb->succs)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
465 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
466 basic_block dest = e->dest;
|
111
|
467 if (bitmap_bit_p (df_get_live_in (bb), targetm.flags_regnum)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
468 && !single_pred_p (dest))
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
469 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
470 last_cmp->missing_uses = true;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
471 break;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
472 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
473 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
474 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
475 }
|
111
|
476
|
|
477 /* If we deleted a compare with a REG_EH_REGION note, we may need to
|
|
478 remove EH edges. */
|
|
479 if (need_purge)
|
|
480 purge_dead_edges (bb);
|
|
481
|
|
482 return NULL;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
483 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
484
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
485 /* Find all comparisons in the function. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
486
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
487 static void
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
488 find_comparisons (void)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
489 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
490 calculate_dominance_info (CDI_DOMINATORS);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
491
|
111
|
492 find_comparison_dom_walker (CDI_DOMINATORS)
|
|
493 .walk (cfun->cfg->x_entry_block_ptr);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
494
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
495 clear_aux_for_blocks ();
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
496 free_dominance_info (CDI_DOMINATORS);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
497 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
498
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
499 /* Select an alternate CC_MODE for a comparison insn comparing A and B.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
500 Note that inputs are almost certainly different than the IN_A and IN_B
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
501 stored in CMP -- we're called while attempting to eliminate the compare
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
502 after all. Return the new FLAGS rtx if successful, else return NULL.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
503 Note that this function may start a change group. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
504
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
505 static rtx
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
506 maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
507 rtx b ATTRIBUTE_UNUSED)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
508 {
|
111
|
509 machine_mode sel_mode;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
510 const int n = cmp->n_uses;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
511 rtx flags = NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
512
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
513 #ifndef SELECT_CC_MODE
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
514 /* Minimize code differences when this target macro is undefined. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
515 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
516 #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
517 #endif
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
518
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
519 /* If we don't have access to all of the uses, we can't validate. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
520 if (cmp->missing_uses || n == 0)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
521 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
522
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
523 /* Find a new mode that works for all of the uses. Special case the
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
524 common case of exactly one use. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
525 if (n == 1)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
526 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
527 sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
528 if (sel_mode != cmp->orig_mode)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
529 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
530 flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
531 validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
532 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
533 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
534 else
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
535 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
536 int i;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
537
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
538 sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
539 for (i = 1; i < n; ++i)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
540 {
|
111
|
541 machine_mode new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
542 if (new_mode != sel_mode)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
543 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
544 sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
545 if (sel_mode == VOIDmode)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
546 return NULL;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
547 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
548 }
|
111
|
549
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
550 if (sel_mode != cmp->orig_mode)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
551 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
552 flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
553 for (i = 0; i < n; ++i)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
554 validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
555 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
556 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
557
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
558 return flags;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
559 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
560
|
111
|
561 /* Return a register RTX holding the same value at START as REG at END, or
|
|
562 NULL_RTX if there is none. */
|
|
563
|
|
564 static rtx
|
|
565 equivalent_reg_at_start (rtx reg, rtx_insn *end, rtx_insn *start)
|
|
566 {
|
|
567 machine_mode orig_mode = GET_MODE (reg);
|
|
568 rtx_insn *bb_head = BB_HEAD (BLOCK_FOR_INSN (end));
|
|
569
|
|
570 for (rtx_insn *insn = PREV_INSN (end);
|
|
571 insn != start;
|
|
572 insn = PREV_INSN (insn))
|
|
573 {
|
|
574 const int abnormal_flags
|
|
575 = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
|
|
576 | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
|
|
577 | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
|
|
578 | DF_REF_PRE_POST_MODIFY);
|
|
579 df_ref def;
|
|
580
|
|
581 /* Note that the BB_HEAD is always either a note or a label, but in
|
|
582 any case it means that REG is defined outside the block. */
|
|
583 if (insn == bb_head)
|
|
584 return NULL_RTX;
|
|
585 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
|
|
586 continue;
|
|
587
|
|
588 /* Find a possible def of REG in INSN. */
|
|
589 FOR_EACH_INSN_DEF (def, insn)
|
|
590 if (DF_REF_REGNO (def) == REGNO (reg))
|
|
591 break;
|
|
592
|
|
593 /* No definitions of REG; continue searching. */
|
|
594 if (def == NULL)
|
|
595 continue;
|
|
596
|
|
597 /* Bail if this is not a totally normal set of REG. */
|
|
598 if (DF_REF_IS_ARTIFICIAL (def))
|
|
599 return NULL_RTX;
|
|
600 if (DF_REF_FLAGS (def) & abnormal_flags)
|
|
601 return NULL_RTX;
|
|
602
|
|
603 /* We've found an insn between the compare and the clobber that sets
|
|
604 REG. Given that pass_cprop_hardreg has not yet run, we still find
|
|
605 situations in which we can usefully look through a copy insn. */
|
|
606 rtx x = single_set (insn);
|
|
607 if (x == NULL_RTX)
|
|
608 return NULL_RTX;
|
|
609 reg = SET_SRC (x);
|
|
610 if (!REG_P (reg))
|
|
611 return NULL_RTX;
|
|
612 }
|
|
613
|
|
614 if (GET_MODE (reg) != orig_mode)
|
|
615 return NULL_RTX;
|
|
616
|
|
617 return reg;
|
|
618 }
|
|
619
|
|
620 /* Return true if it is okay to merge the comparison CMP_INSN with
|
|
621 the instruction ARITH_INSN. Both instructions are assumed to be in the
|
|
622 same basic block with ARITH_INSN appearing before CMP_INSN. This checks
|
|
623 that there are no uses or defs of the condition flags or control flow
|
|
624 changes between the two instructions. */
|
|
625
|
|
626 static bool
|
|
627 can_merge_compare_into_arith (rtx_insn *cmp_insn, rtx_insn *arith_insn)
|
|
628 {
|
|
629 for (rtx_insn *insn = PREV_INSN (cmp_insn);
|
|
630 insn && insn != arith_insn;
|
|
631 insn = PREV_INSN (insn))
|
|
632 {
|
|
633 if (!NONDEBUG_INSN_P (insn))
|
|
634 continue;
|
|
635 /* Bail if there are jumps or calls in between. */
|
|
636 if (!NONJUMP_INSN_P (insn))
|
|
637 return false;
|
|
638
|
|
639 /* Bail on old-style asm statements because they lack
|
|
640 data flow information. */
|
|
641 if (GET_CODE (PATTERN (insn)) == ASM_INPUT)
|
|
642 return false;
|
|
643
|
|
644 df_ref ref;
|
|
645 /* Find a USE of the flags register. */
|
|
646 FOR_EACH_INSN_USE (ref, insn)
|
|
647 if (DF_REF_REGNO (ref) == targetm.flags_regnum)
|
|
648 return false;
|
|
649
|
|
650 /* Find a DEF of the flags register. */
|
|
651 FOR_EACH_INSN_DEF (ref, insn)
|
|
652 if (DF_REF_REGNO (ref) == targetm.flags_regnum)
|
|
653 return false;
|
|
654 }
|
|
655 return true;
|
|
656 }
|
|
657
|
|
658 /* Given two SET expressions, SET_A and SET_B determine whether they form
|
|
659 a recognizable pattern when emitted in parallel. Return that parallel
|
|
660 if so. Otherwise return NULL. */
|
|
661
|
|
662 static rtx
|
|
663 try_validate_parallel (rtx set_a, rtx set_b)
|
|
664 {
|
131
|
665 rtx par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_a, set_b));
|
|
666 rtx_insn *insn = make_insn_raw (par);
|
111
|
667
|
131
|
668 if (insn_invalid_p (insn, false))
|
|
669 {
|
|
670 crtl->emit.x_cur_insn_uid--;
|
|
671 return NULL_RTX;
|
|
672 }
|
111
|
673
|
131
|
674 SET_PREV_INSN (insn) = NULL_RTX;
|
|
675 SET_NEXT_INSN (insn) = NULL_RTX;
|
|
676 INSN_LOCATION (insn) = 0;
|
|
677 return insn;
|
111
|
678 }
|
|
679
|
|
680 /* For a comparison instruction described by CMP check if it compares a
|
|
681 register with zero i.e. it is of the form CC := CMP R1, 0.
|
|
682 If it is, find the instruction defining R1 (say I1) and try to create a
|
|
683 PARALLEL consisting of I1 and the comparison, representing a flag-setting
|
|
684 arithmetic instruction. Example:
|
|
685 I1: R1 := R2 + R3
|
|
686 <instructions that don't read the condition register>
|
|
687 I2: CC := CMP R1 0
|
|
688 I2 can be merged with I1 into:
|
131
|
689 I1: { CC := CMP (R2 + R3) 0 ; R1 := R2 + R3 }
|
111
|
690 This catches cases where R1 is used between I1 and I2 and therefore
|
|
691 combine and other RTL optimisations will not try to propagate it into
|
|
692 I2. Return true if we succeeded in merging CMP. */
|
|
693
|
|
694 static bool
|
|
695 try_merge_compare (struct comparison *cmp)
|
|
696 {
|
|
697 rtx_insn *cmp_insn = cmp->insn;
|
|
698
|
131
|
699 if (cmp->in_b != const0_rtx || cmp->in_a_setter == NULL)
|
111
|
700 return false;
|
|
701 rtx in_a = cmp->in_a;
|
|
702 df_ref use;
|
|
703
|
|
704 FOR_EACH_INSN_USE (use, cmp_insn)
|
|
705 if (DF_REF_REGNO (use) == REGNO (in_a))
|
|
706 break;
|
|
707 if (!use)
|
|
708 return false;
|
|
709
|
131
|
710 rtx_insn *def_insn = cmp->in_a_setter;
|
111
|
711 rtx set = single_set (def_insn);
|
|
712 if (!set)
|
|
713 return false;
|
|
714
|
|
715 if (!can_merge_compare_into_arith (cmp_insn, def_insn))
|
|
716 return false;
|
|
717
|
|
718 rtx src = SET_SRC (set);
|
131
|
719
|
|
720 /* If the source uses addressing modes with side effects, we can't
|
|
721 do the merge because we'd end up with a PARALLEL that has two
|
|
722 instances of that side effect in it. */
|
|
723 if (side_effects_p (src))
|
|
724 return false;
|
|
725
|
111
|
726 rtx flags = maybe_select_cc_mode (cmp, src, CONST0_RTX (GET_MODE (src)));
|
|
727 if (!flags)
|
|
728 {
|
|
729 /* We may already have a change group going through maybe_select_cc_mode.
|
|
730 Discard it properly. */
|
|
731 cancel_changes (0);
|
|
732 return false;
|
|
733 }
|
|
734
|
|
735 rtx flag_set
|
|
736 = gen_rtx_SET (flags, gen_rtx_COMPARE (GET_MODE (flags),
|
|
737 copy_rtx (src),
|
|
738 CONST0_RTX (GET_MODE (src))));
|
|
739 rtx arith_set = copy_rtx (PATTERN (def_insn));
|
|
740 rtx par = try_validate_parallel (flag_set, arith_set);
|
|
741 if (!par)
|
|
742 {
|
|
743 /* We may already have a change group going through maybe_select_cc_mode.
|
|
744 Discard it properly. */
|
|
745 cancel_changes (0);
|
|
746 return false;
|
|
747 }
|
|
748 if (!apply_change_group ())
|
|
749 return false;
|
|
750 emit_insn_after (par, def_insn);
|
|
751 delete_insn (def_insn);
|
|
752 delete_insn (cmp->insn);
|
|
753 return true;
|
|
754 }
|
|
755
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
756 /* Attempt to replace a comparison with a prior arithmetic insn that can
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
757 compute the same flags value as the comparison itself. Return true if
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
758 successful, having made all rtl modifications necessary. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
759
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
760 static bool
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
761 try_eliminate_compare (struct comparison *cmp)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
762 {
|
145
|
763 rtx flags, in_a, in_b, cmp_a, cmp_b;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
764
|
111
|
765 if (try_merge_compare (cmp))
|
|
766 return true;
|
|
767
|
|
768 /* We must have found an interesting "clobber" preceding the compare. */
|
|
769 if (cmp->prev_clobber == NULL)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
770 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
771
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
772 /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
773 Given that this target requires this pass, we can assume that most
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
774 insns do clobber the flags, and so the distance between the compare
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
775 and the clobber is likely to be small. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
776 /* ??? This is one point at which one could argue that DF_REF_CHAIN would
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
777 be useful, but it is thought to be too heavy-weight a solution here. */
|
111
|
778 in_a = equivalent_reg_at_start (cmp->in_a, cmp->insn, cmp->prev_clobber);
|
|
779 if (!in_a)
|
|
780 return false;
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
781
|
111
|
782 /* Likewise for IN_B if need be. */
|
|
783 if (CONSTANT_P (cmp->in_b))
|
|
784 in_b = cmp->in_b;
|
|
785 else if (REG_P (cmp->in_b))
|
|
786 {
|
|
787 in_b = equivalent_reg_at_start (cmp->in_b, cmp->insn, cmp->prev_clobber);
|
|
788 if (!in_b)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
789 return false;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
790 }
|
111
|
791 else if (GET_CODE (cmp->in_b) == UNSPEC)
|
|
792 {
|
|
793 const int len = XVECLEN (cmp->in_b, 0);
|
|
794 rtvec v = rtvec_alloc (len);
|
|
795 for (int i = 0; i < len; i++)
|
|
796 {
|
|
797 rtx r = equivalent_reg_at_start (XVECEXP (cmp->in_b, 0, i),
|
|
798 cmp->insn, cmp->prev_clobber);
|
|
799 if (!r)
|
|
800 return false;
|
|
801 RTVEC_ELT (v, i) = r;
|
|
802 }
|
|
803 in_b = gen_rtx_UNSPEC (GET_MODE (cmp->in_b), v, XINT (cmp->in_b, 1));
|
|
804 }
|
|
805 else
|
|
806 gcc_unreachable ();
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
807
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
808 /* We've reached PREV_CLOBBER without finding a modification of IN_A.
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
809 Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
810 recall that we've already validated the shape of PREV_CLOBBER. */
|
111
|
811 rtx_insn *insn = cmp->prev_clobber;
|
|
812
|
|
813 rtx x = XVECEXP (PATTERN (insn), 0, 0);
|
|
814 if (rtx_equal_p (SET_DEST (x), in_a))
|
145
|
815 cmp_a = SET_SRC (x);
|
111
|
816
|
|
817 /* Also check operations with implicit extensions, e.g.:
|
|
818 [(set (reg:DI)
|
|
819 (zero_extend:DI (plus:SI (reg:SI) (reg:SI))))
|
|
820 (set (reg:CCZ flags)
|
|
821 (compare:CCZ (plus:SI (reg:SI) (reg:SI))
|
|
822 (const_int 0)))] */
|
|
823 else if (REG_P (SET_DEST (x))
|
|
824 && REG_P (in_a)
|
|
825 && REGNO (SET_DEST (x)) == REGNO (in_a)
|
|
826 && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND
|
|
827 || GET_CODE (SET_SRC (x)) == SIGN_EXTEND)
|
|
828 && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a))
|
145
|
829 cmp_a = XEXP (SET_SRC (x), 0);
|
111
|
830
|
|
831 /* Also check fully redundant comparisons, e.g.:
|
|
832 [(set (reg:SI)
|
|
833 (minus:SI (reg:SI) (reg:SI))))
|
|
834 (set (reg:CC flags)
|
|
835 (compare:CC (reg:SI) (reg:SI)))] */
|
|
836 else if (REG_P (in_b)
|
|
837 && GET_CODE (SET_SRC (x)) == MINUS
|
|
838 && rtx_equal_p (XEXP (SET_SRC (x), 0), in_a)
|
|
839 && rtx_equal_p (XEXP (SET_SRC (x), 1), in_b))
|
145
|
840 cmp_a = in_a;
|
111
|
841
|
|
842 else
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
843 return false;
|
111
|
844
|
131
|
845 /* If the source uses addressing modes with side effects, we can't
|
|
846 do the merge because we'd end up with a PARALLEL that has two
|
|
847 instances of that side effect in it. */
|
145
|
848 if (side_effects_p (cmp_a))
|
|
849 return false;
|
|
850
|
|
851 if (in_a == in_b)
|
|
852 cmp_b = cmp_a;
|
|
853 else if (rtx_equal_p (SET_DEST (x), in_b))
|
|
854 cmp_b = SET_SRC (x);
|
|
855 else
|
|
856 cmp_b = in_b;
|
|
857 if (side_effects_p (cmp_b))
|
131
|
858 return false;
|
|
859
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
860 /* Determine if we ought to use a different CC_MODE here. */
|
145
|
861 flags = maybe_select_cc_mode (cmp, cmp_a, cmp_b);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
862 if (flags == NULL)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
863 flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
864
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
865 /* Generate a new comparison for installation in the setter. */
|
145
|
866 rtx y = cmp->not_in_a
|
|
867 ? gen_rtx_NOT (GET_MODE (cmp_a), copy_rtx (cmp_a))
|
|
868 : copy_rtx (cmp_a);
|
|
869 y = gen_rtx_COMPARE (GET_MODE (flags), y, copy_rtx (cmp_b));
|
111
|
870 y = gen_rtx_SET (flags, y);
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
871
|
111
|
872 /* Canonicalize instruction to:
|
|
873 [(set (reg:CCM) (compare:CCM (operation) (immediate)))
|
|
874 (set (reg) (operation)] */
|
|
875
|
|
876 rtvec v = rtvec_alloc (2);
|
|
877 RTVEC_ELT (v, 0) = y;
|
|
878 RTVEC_ELT (v, 1) = x;
|
|
879
|
|
880 rtx pat = gen_rtx_PARALLEL (VOIDmode, v);
|
|
881
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
882 /* Succeed if the new instruction is valid. Note that we may have started
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
883 a change group within maybe_select_cc_mode, therefore we must continue. */
|
111
|
884 validate_change (insn, &PATTERN (insn), pat, true);
|
|
885
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
886 if (!apply_change_group ())
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
887 return false;
|
111
|
888
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
889 /* Success. Delete the compare insn... */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
890 delete_insn (cmp->insn);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
891
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
892 /* ... and any notes that are now invalid due to multiple sets. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
893 x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
894 if (x)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
895 remove_note (insn, x);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
896 x = find_reg_note (insn, REG_EQUAL, NULL);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
897 if (x)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
898 remove_note (insn, x);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
899 x = find_reg_note (insn, REG_EQUIV, NULL);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
900 if (x)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
901 remove_note (insn, x);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
902
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
903 return true;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
904 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
905
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
906 /* Main entry point to the pass. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
907
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
908 static unsigned int
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
909 execute_compare_elim_after_reload (void)
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
910 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
911 df_analyze ();
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
912
|
111
|
913 gcc_checking_assert (!all_compares.exists ());
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
914
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
915 /* Locate all comparisons and their uses, and eliminate duplicates. */
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
916 find_comparisons ();
|
111
|
917 if (all_compares.exists ())
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
918 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
919 struct comparison *cmp;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
920 size_t i;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
921
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
922 /* Eliminate comparisons that are redundant with flags computation. */
|
111
|
923 FOR_EACH_VEC_ELT (all_compares, i, cmp)
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
924 {
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
925 try_eliminate_compare (cmp);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
926 XDELETE (cmp);
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
927 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
928
|
111
|
929 all_compares.release ();
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
930 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
931
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
932 return 0;
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
933 }
|
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
934
|
111
|
935 namespace {
|
|
936
|
|
937 const pass_data pass_data_compare_elim_after_reload =
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
938 {
|
111
|
939 RTL_PASS, /* type */
|
|
940 "cmpelim", /* name */
|
|
941 OPTGROUP_NONE, /* optinfo_flags */
|
|
942 TV_NONE, /* tv_id */
|
|
943 0, /* properties_required */
|
|
944 0, /* properties_provided */
|
|
945 0, /* properties_destroyed */
|
|
946 0, /* todo_flags_start */
|
|
947 ( TODO_df_finish | TODO_df_verify ), /* todo_flags_finish */
|
|
948 };
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
949
|
111
|
950 class pass_compare_elim_after_reload : public rtl_opt_pass
|
68
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
951 {
|
111
|
952 public:
|
|
953 pass_compare_elim_after_reload (gcc::context *ctxt)
|
|
954 : rtl_opt_pass (pass_data_compare_elim_after_reload, ctxt)
|
|
955 {}
|
|
956
|
|
957 /* opt_pass methods: */
|
|
958 virtual bool gate (function *)
|
|
959 {
|
|
960 /* Setting this target hook value is how a backend indicates the need. */
|
|
961 if (targetm.flags_regnum == INVALID_REGNUM)
|
|
962 return false;
|
|
963 return flag_compare_elim_after_reload;
|
|
964 }
|
|
965
|
|
966 virtual unsigned int execute (function *)
|
|
967 {
|
|
968 return execute_compare_elim_after_reload ();
|
|
969 }
|
|
970
|
|
971 }; // class pass_compare_elim_after_reload
|
|
972
|
|
973 } // anon namespace
|
|
974
|
|
975 rtl_opt_pass *
|
|
976 make_pass_compare_elim_after_reload (gcc::context *ctxt)
|
|
977 {
|
|
978 return new pass_compare_elim_after_reload (ctxt);
|
|
979 }
|