]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Conditional constant propagation pass for the GNU compiler. |
1e052c19 KH |
2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 |
3 | Free Software Foundation, Inc. | |
6de9cd9a DN |
4 | Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org> |
5 | Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com> | |
6 | ||
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2, or (at your option) any | |
12 | later version. | |
13 | ||
14 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with GCC; see the file COPYING. If not, write to the Free | |
366ccddb KC |
21 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 02110-1301, USA. */ | |
6de9cd9a | 23 | |
0bca51f0 DN |
24 | /* Conditional constant propagation (CCP) is based on the SSA |
25 | propagation engine (tree-ssa-propagate.c). Constant assignments of | |
26 | the form VAR = CST are propagated from the assignments into uses of | |
27 | VAR, which in turn may generate new constants. The simulation uses | |
28 | a four level lattice to keep track of constant values associated | |
29 | with SSA names. Given an SSA name V_i, it may take one of the | |
30 | following values: | |
31 | ||
32 | UNINITIALIZED -> This is the default starting value. V_i | |
33 | has not been processed yet. | |
34 | ||
35 | UNDEFINED -> V_i is a local variable whose definition | |
36 | has not been processed yet. Therefore we | |
37 | don't yet know if its value is a constant | |
38 | or not. | |
39 | ||
40 | CONSTANT -> V_i has been found to hold a constant | |
41 | value C. | |
42 | ||
43 | VARYING -> V_i cannot take a constant value, or if it | |
44 | does, it is not possible to determine it | |
45 | at compile time. | |
46 | ||
47 | The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node: | |
48 | ||
49 | 1- In ccp_visit_stmt, we are interested in assignments whose RHS | |
50 | evaluates into a constant and conditional jumps whose predicate | |
51 | evaluates into a boolean true or false. When an assignment of | |
52 | the form V_i = CONST is found, V_i's lattice value is set to | |
53 | CONSTANT and CONST is associated with it. This causes the | |
54 | propagation engine to add all the SSA edges coming out the | |
55 | assignment into the worklists, so that statements that use V_i | |
56 | can be visited. | |
57 | ||
58 | If the statement is a conditional with a constant predicate, we | |
59 | mark the outgoing edges as executable or not executable | |
60 | depending on the predicate's value. This is then used when | |
61 | visiting PHI nodes to know when a PHI argument can be ignored. | |
62 | ||
63 | ||
64 | 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the | |
65 | same constant C, then the LHS of the PHI is set to C. This | |
66 | evaluation is known as the "meet operation". Since one of the | |
67 | goals of this evaluation is to optimistically return constant | |
68 | values as often as possible, it uses two main short cuts: | |
69 | ||
70 | - If an argument is flowing in through a non-executable edge, it | |
71 | is ignored. This is useful in cases like this: | |
72 | ||
73 | if (PRED) | |
74 | a_9 = 3; | |
75 | else | |
76 | a_10 = 100; | |
77 | a_11 = PHI (a_9, a_10) | |
78 | ||
79 | If PRED is known to always evaluate to false, then we can | |
80 | assume that a_11 will always take its value from a_10, meaning | |
81 | that instead of consider it VARYING (a_9 and a_10 have | |
82 | different values), we can consider it CONSTANT 100. | |
83 | ||
84 | - If an argument has an UNDEFINED value, then it does not affect | |
85 | the outcome of the meet operation. If a variable V_i has an | |
86 | UNDEFINED value, it means that either its defining statement | |
87 | hasn't been visited yet or V_i has no defining statement, in | |
88 | which case the original symbol 'V' is being used | |
89 | uninitialized. Since 'V' is a local variable, the compiler | |
90 | may assume any initial value for it. | |
91 | ||
92 | ||
93 | After propagation, every variable V_i that ends up with a lattice | |
94 | value of CONSTANT will have the associated constant value in the | |
95 | array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for | |
96 | final substitution and folding. | |
97 | ||
98 | ||
99 | Constant propagation in stores and loads (STORE-CCP) | |
100 | ---------------------------------------------------- | |
101 | ||
102 | While CCP has all the logic to propagate constants in GIMPLE | |
103 | registers, it is missing the ability to associate constants with | |
104 | stores and loads (i.e., pointer dereferences, structures and | |
105 | global/aliased variables). We don't keep loads and stores in | |
106 | SSA, but we do build a factored use-def web for them (in the | |
107 | virtual operands). | |
108 | ||
109 | For instance, consider the following code fragment: | |
110 | ||
111 | struct A a; | |
112 | const int B = 42; | |
113 | ||
114 | void foo (int i) | |
115 | { | |
116 | if (i > 10) | |
117 | a.a = 42; | |
118 | else | |
119 | { | |
120 | a.b = 21; | |
121 | a.a = a.b + 21; | |
122 | } | |
123 | ||
124 | if (a.a != B) | |
125 | never_executed (); | |
126 | } | |
127 | ||
128 | We should be able to deduce that the predicate 'a.a != B' is always | |
129 | false. To achieve this, we associate constant values to the SSA | |
130 | names in the V_MAY_DEF and V_MUST_DEF operands for each store. | |
131 | Additionally, since we also glob partial loads/stores with the base | |
132 | symbol, we also keep track of the memory reference where the | |
133 | constant value was stored (in the MEM_REF field of PROP_VALUE_T). | |
134 | For instance, | |
135 | ||
136 | # a_5 = V_MAY_DEF <a_4> | |
137 | a.a = 2; | |
138 | ||
139 | # VUSE <a_5> | |
140 | x_3 = a.b; | |
141 | ||
142 | In the example above, CCP will associate value '2' with 'a_5', but | |
143 | it would be wrong to replace the load from 'a.b' with '2', because | |
144 | '2' had been stored into a.a. | |
145 | ||
146 | To support STORE-CCP, it is necessary to add a new value to the | |
147 | constant propagation lattice. When evaluating a load for a memory | |
148 | reference we can no longer assume a value of UNDEFINED if we | |
149 | haven't seen a preceding store to the same memory location. | |
150 | Consider, for instance global variables: | |
151 | ||
152 | int A; | |
153 | ||
154 | foo (int i) | |
155 | { | |
156 | if (i_3 > 10) | |
157 | A_4 = 3; | |
158 | # A_5 = PHI (A_4, A_2); | |
159 | ||
160 | # VUSE <A_5> | |
161 | A.0_6 = A; | |
162 | ||
163 | return A.0_6; | |
164 | } | |
165 | ||
166 | The value of A_2 cannot be assumed to be UNDEFINED, as it may have | |
167 | been defined outside of foo. If we were to assume it UNDEFINED, we | |
168 | would erroneously optimize the above into 'return 3;'. Therefore, | |
169 | when doing STORE-CCP, we introduce a fifth lattice value | |
170 | (UNKNOWN_VAL), which overrides any other value when computing the | |
171 | meet operation in PHI nodes. | |
172 | ||
173 | Though STORE-CCP is not too expensive, it does have to do more work | |
174 | than regular CCP, so it is only enabled at -O2. Both regular CCP | |
175 | and STORE-CCP use the exact same algorithm. The only distinction | |
176 | is that when doing STORE-CCP, the boolean variable DO_STORE_CCP is | |
177 | set to true. This affects the evaluation of statements and PHI | |
178 | nodes. | |
6de9cd9a DN |
179 | |
180 | References: | |
181 | ||
182 | Constant propagation with conditional branches, | |
183 | Wegman and Zadeck, ACM TOPLAS 13(2):181-210. | |
184 | ||
185 | Building an Optimizing Compiler, | |
186 | Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9. | |
187 | ||
188 | Advanced Compiler Design and Implementation, | |
189 | Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */ | |
190 | ||
191 | #include "config.h" | |
192 | #include "system.h" | |
193 | #include "coretypes.h" | |
194 | #include "tm.h" | |
6de9cd9a | 195 | #include "tree.h" |
750628d8 | 196 | #include "flags.h" |
6de9cd9a DN |
197 | #include "rtl.h" |
198 | #include "tm_p.h" | |
750628d8 | 199 | #include "ggc.h" |
6de9cd9a | 200 | #include "basic-block.h" |
750628d8 | 201 | #include "output.h" |
750628d8 DN |
202 | #include "expr.h" |
203 | #include "function.h" | |
6de9cd9a | 204 | #include "diagnostic.h" |
750628d8 | 205 | #include "timevar.h" |
6de9cd9a | 206 | #include "tree-dump.h" |
750628d8 | 207 | #include "tree-flow.h" |
6de9cd9a | 208 | #include "tree-pass.h" |
750628d8 DN |
209 | #include "tree-ssa-propagate.h" |
210 | #include "langhooks.h" | |
ae3df618 | 211 | #include "target.h" |
6de9cd9a DN |
212 | |
213 | ||
214 | /* Possible lattice values. */ | |
215 | typedef enum | |
216 | { | |
217 | UNINITIALIZED = 0, | |
218 | UNDEFINED, | |
173b818d | 219 | UNKNOWN_VAL, |
6de9cd9a DN |
220 | CONSTANT, |
221 | VARYING | |
0bca51f0 | 222 | } ccp_lattice_t; |
6de9cd9a | 223 | |
0bca51f0 DN |
224 | /* Array of propagated constant values. After propagation, |
225 | CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If | |
226 | the constant is held in an SSA name representing a memory store | |
227 | (i.e., a V_MAY_DEF or V_MUST_DEF), CONST_VAL[I].MEM_REF will | |
228 | contain the actual memory reference used to store (i.e., the LHS of | |
229 | the assignment doing the store). */ | |
404f4351 | 230 | static prop_value_t *const_val; |
6de9cd9a | 231 | |
0bca51f0 DN |
232 | /* True if we are also propagating constants in stores and loads. */ |
233 | static bool do_store_ccp; | |
6de9cd9a | 234 | |
0bca51f0 | 235 | /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */ |
95eec0d6 DB |
236 | |
237 | static void | |
0bca51f0 | 238 | dump_lattice_value (FILE *outf, const char *prefix, prop_value_t val) |
95eec0d6 | 239 | { |
750628d8 | 240 | switch (val.lattice_val) |
95eec0d6 | 241 | { |
0bca51f0 DN |
242 | case UNINITIALIZED: |
243 | fprintf (outf, "%sUNINITIALIZED", prefix); | |
244 | break; | |
750628d8 DN |
245 | case UNDEFINED: |
246 | fprintf (outf, "%sUNDEFINED", prefix); | |
247 | break; | |
248 | case VARYING: | |
249 | fprintf (outf, "%sVARYING", prefix); | |
250 | break; | |
251 | case UNKNOWN_VAL: | |
252 | fprintf (outf, "%sUNKNOWN_VAL", prefix); | |
253 | break; | |
254 | case CONSTANT: | |
255 | fprintf (outf, "%sCONSTANT ", prefix); | |
0bca51f0 | 256 | print_generic_expr (outf, val.value, dump_flags); |
750628d8 DN |
257 | break; |
258 | default: | |
1e128c5f | 259 | gcc_unreachable (); |
750628d8 | 260 | } |
95eec0d6 | 261 | } |
6de9cd9a | 262 | |
6de9cd9a | 263 | |
0bca51f0 DN |
264 | /* Print lattice value VAL to stderr. */ |
265 | ||
266 | void debug_lattice_value (prop_value_t val); | |
267 | ||
268 | void | |
269 | debug_lattice_value (prop_value_t val) | |
270 | { | |
271 | dump_lattice_value (stderr, "", val); | |
272 | fprintf (stderr, "\n"); | |
273 | } | |
6de9cd9a | 274 | |
6de9cd9a | 275 | |
0bca51f0 DN |
276 | /* Compute a default value for variable VAR and store it in the |
277 | CONST_VAL array. The following rules are used to get default | |
278 | values: | |
95eec0d6 | 279 | |
0bca51f0 DN |
280 | 1- Global and static variables that are declared constant are |
281 | considered CONSTANT. | |
282 | ||
283 | 2- Any other value is considered UNDEFINED. This is useful when | |
750628d8 DN |
284 | considering PHI nodes. PHI arguments that are undefined do not |
285 | change the constant value of the PHI node, which allows for more | |
0bca51f0 | 286 | constants to be propagated. |
6de9cd9a | 287 | |
0bca51f0 DN |
288 | 3- If SSA_NAME_VALUE is set and it is a constant, its value is |
289 | used. | |
6de9cd9a | 290 | |
0bca51f0 DN |
291 | 4- Variables defined by statements other than assignments and PHI |
292 | nodes are considered VARYING. | |
6de9cd9a | 293 | |
0bca51f0 DN |
294 | 5- Variables that are not GIMPLE registers are considered |
295 | UNKNOWN_VAL, which is really a stronger version of UNDEFINED. | |
296 | It's used to avoid the short circuit evaluation implied by | |
297 | UNDEFINED in ccp_lattice_meet. */ | |
6de9cd9a | 298 | |
0bca51f0 DN |
299 | static prop_value_t |
300 | get_default_value (tree var) | |
301 | { | |
302 | tree sym = SSA_NAME_VAR (var); | |
303 | prop_value_t val = { UNINITIALIZED, NULL_TREE, NULL_TREE }; | |
304 | ||
305 | if (!do_store_ccp && !is_gimple_reg (var)) | |
6de9cd9a | 306 | { |
0bca51f0 DN |
307 | /* Short circuit for regular CCP. We are not interested in any |
308 | non-register when DO_STORE_CCP is false. */ | |
750628d8 | 309 | val.lattice_val = VARYING; |
6de9cd9a | 310 | } |
0bca51f0 DN |
311 | else if (SSA_NAME_VALUE (var) |
312 | && is_gimple_min_invariant (SSA_NAME_VALUE (var))) | |
750628d8 | 313 | { |
0bca51f0 DN |
314 | val.lattice_val = CONSTANT; |
315 | val.value = SSA_NAME_VALUE (var); | |
750628d8 | 316 | } |
0bca51f0 DN |
317 | else if (TREE_STATIC (sym) |
318 | && TREE_READONLY (sym) | |
319 | && DECL_INITIAL (sym) | |
320 | && is_gimple_min_invariant (DECL_INITIAL (sym))) | |
750628d8 | 321 | { |
0bca51f0 DN |
322 | /* Globals and static variables declared 'const' take their |
323 | initial value. */ | |
324 | val.lattice_val = CONSTANT; | |
325 | val.value = DECL_INITIAL (sym); | |
326 | val.mem_ref = sym; | |
750628d8 DN |
327 | } |
328 | else | |
329 | { | |
750628d8 | 330 | tree stmt = SSA_NAME_DEF_STMT (var); |
6de9cd9a | 331 | |
0bca51f0 DN |
332 | if (IS_EMPTY_STMT (stmt)) |
333 | { | |
334 | /* Variables defined by an empty statement are those used | |
335 | before being initialized. If VAR is a local variable, we | |
336 | can assume initially that it is UNDEFINED. If we are | |
337 | doing STORE-CCP, function arguments and non-register | |
338 | variables are initially UNKNOWN_VAL, because we cannot | |
339 | discard the value incoming from outside of this function | |
340 | (see ccp_lattice_meet for details). */ | |
341 | if (is_gimple_reg (sym) && TREE_CODE (sym) != PARM_DECL) | |
342 | val.lattice_val = UNDEFINED; | |
343 | else if (do_store_ccp) | |
344 | val.lattice_val = UNKNOWN_VAL; | |
345 | else | |
750628d8 DN |
346 | val.lattice_val = VARYING; |
347 | } | |
0bca51f0 DN |
348 | else if (TREE_CODE (stmt) == MODIFY_EXPR |
349 | || TREE_CODE (stmt) == PHI_NODE) | |
350 | { | |
351 | /* Any other variable defined by an assignment or a PHI node | |
352 | is considered UNDEFINED (or UNKNOWN_VAL if VAR is not a | |
353 | GIMPLE register). */ | |
354 | val.lattice_val = is_gimple_reg (sym) ? UNDEFINED : UNKNOWN_VAL; | |
355 | } | |
356 | else | |
357 | { | |
358 | /* Otherwise, VAR will never take on a constant value. */ | |
359 | val.lattice_val = VARYING; | |
360 | } | |
750628d8 | 361 | } |
6de9cd9a | 362 | |
750628d8 DN |
363 | return val; |
364 | } | |
6de9cd9a | 365 | |
6de9cd9a | 366 | |
0bca51f0 DN |
367 | /* Get the constant value associated with variable VAR. If |
368 | MAY_USE_DEFAULT_P is true, call get_default_value on variables that | |
369 | have the lattice value UNINITIALIZED. */ | |
6de9cd9a | 370 | |
0bca51f0 DN |
371 | static prop_value_t * |
372 | get_value (tree var, bool may_use_default_p) | |
373 | { | |
374 | prop_value_t *val = &const_val[SSA_NAME_VERSION (var)]; | |
375 | if (may_use_default_p && val->lattice_val == UNINITIALIZED) | |
6de9cd9a DN |
376 | *val = get_default_value (var); |
377 | ||
378 | return val; | |
379 | } | |
380 | ||
381 | ||
0bca51f0 DN |
382 | /* Set the value for variable VAR to NEW_VAL. Return true if the new |
383 | value is different from VAR's previous value. */ | |
6de9cd9a | 384 | |
750628d8 | 385 | static bool |
0bca51f0 | 386 | set_lattice_value (tree var, prop_value_t new_val) |
6de9cd9a | 387 | { |
0bca51f0 DN |
388 | prop_value_t *old_val = get_value (var, false); |
389 | ||
390 | /* Lattice transitions must always be monotonically increasing in | |
391 | value. We allow two exceptions: | |
392 | ||
393 | 1- If *OLD_VAL and NEW_VAL are the same, return false to | |
394 | inform the caller that this was a non-transition. | |
395 | ||
396 | 2- If we are doing store-ccp (i.e., DOING_STORE_CCP is true), | |
397 | allow CONSTANT->UNKNOWN_VAL. The UNKNOWN_VAL state is a | |
398 | special type of UNDEFINED state which prevents the short | |
399 | circuit evaluation of PHI arguments (see ccp_visit_phi_node | |
400 | and ccp_lattice_meet). */ | |
401 | gcc_assert (old_val->lattice_val <= new_val.lattice_val | |
402 | || (old_val->lattice_val == new_val.lattice_val | |
403 | && old_val->value == new_val.value | |
404 | && old_val->mem_ref == new_val.mem_ref) | |
405 | || (do_store_ccp | |
406 | && old_val->lattice_val == CONSTANT | |
407 | && new_val.lattice_val == UNKNOWN_VAL)); | |
408 | ||
409 | if (old_val->lattice_val != new_val.lattice_val) | |
6de9cd9a | 410 | { |
750628d8 DN |
411 | if (dump_file && (dump_flags & TDF_DETAILS)) |
412 | { | |
0bca51f0 DN |
413 | dump_lattice_value (dump_file, "Lattice value changed to ", new_val); |
414 | fprintf (dump_file, ". %sdding SSA edges to worklist.\n", | |
415 | new_val.lattice_val != UNDEFINED ? "A" : "Not a"); | |
750628d8 DN |
416 | } |
417 | ||
0bca51f0 DN |
418 | *old_val = new_val; |
419 | ||
420 | /* Transitions UNINITIALIZED -> UNDEFINED are never interesting | |
421 | for propagation purposes. In these cases return false to | |
422 | avoid doing useless work. */ | |
423 | return (new_val.lattice_val != UNDEFINED); | |
6de9cd9a | 424 | } |
750628d8 DN |
425 | |
426 | return false; | |
6de9cd9a DN |
427 | } |
428 | ||
429 | ||
0bca51f0 | 430 | /* Return the likely CCP lattice value for STMT. |
6de9cd9a | 431 | |
750628d8 | 432 | If STMT has no operands, then return CONSTANT. |
6de9cd9a | 433 | |
750628d8 | 434 | Else if any operands of STMT are undefined, then return UNDEFINED. |
6de9cd9a | 435 | |
750628d8 | 436 | Else if any operands of STMT are constants, then return CONSTANT. |
6de9cd9a | 437 | |
750628d8 | 438 | Else return VARYING. */ |
6de9cd9a | 439 | |
0bca51f0 | 440 | static ccp_lattice_t |
750628d8 DN |
441 | likely_value (tree stmt) |
442 | { | |
0bca51f0 | 443 | bool found_constant; |
750628d8 DN |
444 | stmt_ann_t ann; |
445 | tree use; | |
446 | ssa_op_iter iter; | |
6de9cd9a | 447 | |
750628d8 | 448 | ann = stmt_ann (stmt); |
0bca51f0 DN |
449 | |
450 | /* If the statement has volatile operands, it won't fold to a | |
451 | constant value. */ | |
452 | if (ann->has_volatile_ops) | |
453 | return VARYING; | |
454 | ||
455 | /* If we are not doing store-ccp, statements with loads | |
456 | and/or stores will never fold into a constant. */ | |
457 | if (!do_store_ccp | |
458 | && (ann->makes_aliased_stores | |
459 | || ann->makes_aliased_loads | |
f47c96aa | 460 | || !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))) |
750628d8 | 461 | return VARYING; |
6de9cd9a | 462 | |
0bca51f0 DN |
463 | |
464 | /* A CALL_EXPR is assumed to be varying. NOTE: This may be overly | |
465 | conservative, in the presence of const and pure calls. */ | |
750628d8 DN |
466 | if (get_call_expr_in (stmt) != NULL_TREE) |
467 | return VARYING; | |
6de9cd9a | 468 | |
0bca51f0 DN |
469 | /* Anything other than assignments and conditional jumps are not |
470 | interesting for CCP. */ | |
471 | if (TREE_CODE (stmt) != MODIFY_EXPR | |
472 | && TREE_CODE (stmt) != COND_EXPR | |
473 | && TREE_CODE (stmt) != SWITCH_EXPR) | |
474 | return VARYING; | |
475 | ||
a318e3ac SB |
476 | if (is_gimple_min_invariant (get_rhs (stmt))) |
477 | return CONSTANT; | |
478 | ||
0bca51f0 DN |
479 | found_constant = false; |
480 | FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE|SSA_OP_VUSE) | |
750628d8 | 481 | { |
0bca51f0 | 482 | prop_value_t *val = get_value (use, true); |
750628d8 | 483 | |
0bca51f0 DN |
484 | if (val->lattice_val == VARYING) |
485 | return VARYING; | |
750628d8 | 486 | |
750628d8 | 487 | if (val->lattice_val == UNKNOWN_VAL) |
0bca51f0 DN |
488 | { |
489 | /* UNKNOWN_VAL is invalid when not doing STORE-CCP. */ | |
490 | gcc_assert (do_store_ccp); | |
491 | return UNKNOWN_VAL; | |
492 | } | |
493 | ||
750628d8 | 494 | if (val->lattice_val == CONSTANT) |
0bca51f0 | 495 | found_constant = true; |
6de9cd9a | 496 | } |
750628d8 | 497 | |
0bca51f0 | 498 | if (found_constant |
f47c96aa AM |
499 | || ZERO_SSA_OPERANDS (stmt, SSA_OP_USE) |
500 | || ZERO_SSA_OPERANDS (stmt, SSA_OP_VUSE)) | |
0bca51f0 DN |
501 | return CONSTANT; |
502 | ||
503 | return UNDEFINED; | |
6de9cd9a DN |
504 | } |
505 | ||
506 | ||
750628d8 | 507 | /* Initialize local data structures for CCP. */ |
6de9cd9a DN |
508 | |
509 | static void | |
750628d8 | 510 | ccp_initialize (void) |
6de9cd9a | 511 | { |
750628d8 | 512 | basic_block bb; |
6de9cd9a | 513 | |
0bca51f0 DN |
514 | const_val = xmalloc (num_ssa_names * sizeof (*const_val)); |
515 | memset (const_val, 0, num_ssa_names * sizeof (*const_val)); | |
6de9cd9a | 516 | |
750628d8 DN |
517 | /* Initialize simulation flags for PHI nodes and statements. */ |
518 | FOR_EACH_BB (bb) | |
6de9cd9a | 519 | { |
750628d8 | 520 | block_stmt_iterator i; |
6de9cd9a | 521 | |
750628d8 DN |
522 | for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i)) |
523 | { | |
524 | bool is_varying = false; | |
525 | tree stmt = bsi_stmt (i); | |
6de9cd9a | 526 | |
0bca51f0 | 527 | if (likely_value (stmt) == VARYING) |
6de9cd9a | 528 | |
750628d8 | 529 | { |
0bca51f0 DN |
530 | tree def; |
531 | ssa_op_iter iter; | |
532 | ||
533 | /* If the statement will not produce a constant, mark | |
534 | all its outputs VARYING. */ | |
535 | FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) | |
536 | get_value (def, false)->lattice_val = VARYING; | |
537 | ||
538 | /* Never mark conditional jumps with DONT_SIMULATE_AGAIN, | |
539 | otherwise the propagator will never add the outgoing | |
540 | control edges. */ | |
541 | if (TREE_CODE (stmt) != COND_EXPR | |
542 | && TREE_CODE (stmt) != SWITCH_EXPR) | |
543 | is_varying = true; | |
750628d8 DN |
544 | } |
545 | ||
750628d8 DN |
546 | DONT_SIMULATE_AGAIN (stmt) = is_varying; |
547 | } | |
6de9cd9a DN |
548 | } |
549 | ||
750628d8 DN |
550 | /* Now process PHI nodes. */ |
551 | FOR_EACH_BB (bb) | |
6de9cd9a | 552 | { |
0bca51f0 | 553 | tree phi; |
750628d8 DN |
554 | |
555 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) | |
556 | { | |
0bca51f0 DN |
557 | int i; |
558 | tree arg; | |
559 | prop_value_t *val = get_value (PHI_RESULT (phi), false); | |
750628d8 | 560 | |
0bca51f0 | 561 | for (i = 0; i < PHI_NUM_ARGS (phi); i++) |
750628d8 | 562 | { |
0bca51f0 | 563 | arg = PHI_ARG_DEF (phi, i); |
750628d8 | 564 | |
0bca51f0 DN |
565 | if (TREE_CODE (arg) == SSA_NAME |
566 | && get_value (arg, false)->lattice_val == VARYING) | |
750628d8 | 567 | { |
0bca51f0 DN |
568 | val->lattice_val = VARYING; |
569 | break; | |
750628d8 DN |
570 | } |
571 | } | |
572 | ||
573 | DONT_SIMULATE_AGAIN (phi) = (val->lattice_val == VARYING); | |
574 | } | |
6de9cd9a | 575 | } |
750628d8 | 576 | } |
6de9cd9a | 577 | |
6de9cd9a | 578 | |
0bca51f0 DN |
579 | /* Do final substitution of propagated values, cleanup the flowgraph and |
580 | free allocated storage. */ | |
6de9cd9a | 581 | |
0bca51f0 DN |
582 | static void |
583 | ccp_finalize (void) | |
6de9cd9a | 584 | { |
0bca51f0 | 585 | /* Perform substitutions based on the known constant values. */ |
227858d1 | 586 | substitute_and_fold (const_val, false); |
6de9cd9a | 587 | |
0bca51f0 | 588 | free (const_val); |
6de9cd9a DN |
589 | } |
590 | ||
591 | ||
0bca51f0 DN |
592 | /* Compute the meet operator between *VAL1 and *VAL2. Store the result |
593 | in VAL1. | |
594 | ||
595 | any M UNDEFINED = any | |
596 | any M UNKNOWN_VAL = UNKNOWN_VAL | |
597 | any M VARYING = VARYING | |
598 | Ci M Cj = Ci if (i == j) | |
599 | Ci M Cj = VARYING if (i != j) | |
600 | ||
601 | Lattice values UNKNOWN_VAL and UNDEFINED are similar but have | |
602 | different semantics at PHI nodes. Both values imply that we don't | |
603 | know whether the variable is constant or not. However, UNKNOWN_VAL | |
604 | values override all others. For instance, suppose that A is a | |
605 | global variable: | |
606 | ||
607 | +------+ | |
608 | | | | |
609 | | / \ | |
610 | | / \ | |
611 | | | A_1 = 4 | |
612 | | \ / | |
613 | | \ / | |
614 | | A_3 = PHI (A_2, A_1) | |
615 | | ... = A_3 | |
616 | | | | |
617 | +----+ | |
618 | ||
619 | If the edge into A_2 is not executable, the first visit to A_3 will | |
620 | yield the constant 4. But the second visit to A_3 will be with A_2 | |
621 | in state UNKNOWN_VAL. We can no longer conclude that A_3 is 4 | |
622 | because A_2 may have been set in another function. If we had used | |
623 | the lattice value UNDEFINED, we would have had wrongly concluded | |
624 | that A_3 is 4. */ | |
625 | ||
6de9cd9a DN |
626 | |
627 | static void | |
0bca51f0 | 628 | ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2) |
6de9cd9a | 629 | { |
0bca51f0 | 630 | if (val1->lattice_val == UNDEFINED) |
6de9cd9a | 631 | { |
0bca51f0 DN |
632 | /* UNDEFINED M any = any */ |
633 | *val1 = *val2; | |
750628d8 | 634 | } |
0bca51f0 | 635 | else if (val2->lattice_val == UNDEFINED) |
195da47b | 636 | { |
0bca51f0 DN |
637 | /* any M UNDEFINED = any |
638 | Nothing to do. VAL1 already contains the value we want. */ | |
639 | ; | |
195da47b | 640 | } |
0bca51f0 DN |
641 | else if (val1->lattice_val == UNKNOWN_VAL |
642 | || val2->lattice_val == UNKNOWN_VAL) | |
6de9cd9a | 643 | { |
0bca51f0 DN |
644 | /* UNKNOWN_VAL values are invalid if we are not doing STORE-CCP. */ |
645 | gcc_assert (do_store_ccp); | |
6de9cd9a | 646 | |
0bca51f0 DN |
647 | /* any M UNKNOWN_VAL = UNKNOWN_VAL. */ |
648 | val1->lattice_val = UNKNOWN_VAL; | |
649 | val1->value = NULL_TREE; | |
650 | val1->mem_ref = NULL_TREE; | |
651 | } | |
652 | else if (val1->lattice_val == VARYING | |
653 | || val2->lattice_val == VARYING) | |
750628d8 | 654 | { |
0bca51f0 DN |
655 | /* any M VARYING = VARYING. */ |
656 | val1->lattice_val = VARYING; | |
657 | val1->value = NULL_TREE; | |
658 | val1->mem_ref = NULL_TREE; | |
750628d8 | 659 | } |
0bca51f0 DN |
660 | else if (val1->lattice_val == CONSTANT |
661 | && val2->lattice_val == CONSTANT | |
662 | && simple_cst_equal (val1->value, val2->value) == 1 | |
663 | && (!do_store_ccp | |
a318e3ac SB |
664 | || (val1->mem_ref && val2->mem_ref |
665 | && operand_equal_p (val1->mem_ref, val2->mem_ref, 0)))) | |
750628d8 | 666 | { |
0bca51f0 DN |
667 | /* Ci M Cj = Ci if (i == j) |
668 | Ci M Cj = VARYING if (i != j) | |
669 | ||
670 | If these two values come from memory stores, make sure that | |
671 | they come from the same memory reference. */ | |
672 | val1->lattice_val = CONSTANT; | |
673 | val1->value = val1->value; | |
674 | val1->mem_ref = val1->mem_ref; | |
750628d8 DN |
675 | } |
676 | else | |
677 | { | |
0bca51f0 DN |
678 | /* Any other combination is VARYING. */ |
679 | val1->lattice_val = VARYING; | |
680 | val1->value = NULL_TREE; | |
681 | val1->mem_ref = NULL_TREE; | |
750628d8 | 682 | } |
6de9cd9a DN |
683 | } |
684 | ||
685 | ||
750628d8 DN |
686 | /* Loop through the PHI_NODE's parameters for BLOCK and compare their |
687 | lattice values to determine PHI_NODE's lattice value. The value of a | |
0bca51f0 | 688 | PHI node is determined calling ccp_lattice_meet with all the arguments |
750628d8 | 689 | of the PHI node that are incoming via executable edges. */ |
6de9cd9a | 690 | |
750628d8 DN |
691 | static enum ssa_prop_result |
692 | ccp_visit_phi_node (tree phi) | |
6de9cd9a | 693 | { |
750628d8 | 694 | int i; |
0bca51f0 | 695 | prop_value_t *old_val, new_val; |
6de9cd9a | 696 | |
750628d8 | 697 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6de9cd9a | 698 | { |
750628d8 DN |
699 | fprintf (dump_file, "\nVisiting PHI node: "); |
700 | print_generic_expr (dump_file, phi, dump_flags); | |
6de9cd9a | 701 | } |
6de9cd9a | 702 | |
0bca51f0 | 703 | old_val = get_value (PHI_RESULT (phi), false); |
750628d8 DN |
704 | switch (old_val->lattice_val) |
705 | { | |
706 | case VARYING: | |
0bca51f0 | 707 | return SSA_PROP_VARYING; |
6de9cd9a | 708 | |
750628d8 DN |
709 | case CONSTANT: |
710 | new_val = *old_val; | |
711 | break; | |
6de9cd9a | 712 | |
750628d8 DN |
713 | case UNKNOWN_VAL: |
714 | /* To avoid the default value of UNKNOWN_VAL overriding | |
715 | that of its possible constant arguments, temporarily | |
716 | set the PHI node's default lattice value to be | |
717 | UNDEFINED. If the PHI node's old value was UNKNOWN_VAL and | |
718 | the new value is UNDEFINED, then we prevent the invalid | |
719 | transition by not calling set_lattice_value. */ | |
0bca51f0 DN |
720 | gcc_assert (do_store_ccp); |
721 | ||
722 | /* FALLTHRU */ | |
6de9cd9a | 723 | |
750628d8 DN |
724 | case UNDEFINED: |
725 | case UNINITIALIZED: | |
726 | new_val.lattice_val = UNDEFINED; | |
0bca51f0 DN |
727 | new_val.value = NULL_TREE; |
728 | new_val.mem_ref = NULL_TREE; | |
750628d8 | 729 | break; |
6de9cd9a | 730 | |
750628d8 | 731 | default: |
1e128c5f | 732 | gcc_unreachable (); |
750628d8 | 733 | } |
6de9cd9a | 734 | |
750628d8 DN |
735 | for (i = 0; i < PHI_NUM_ARGS (phi); i++) |
736 | { | |
0bca51f0 DN |
737 | /* Compute the meet operator over all the PHI arguments flowing |
738 | through executable edges. */ | |
750628d8 | 739 | edge e = PHI_ARG_EDGE (phi, i); |
6de9cd9a | 740 | |
750628d8 DN |
741 | if (dump_file && (dump_flags & TDF_DETAILS)) |
742 | { | |
743 | fprintf (dump_file, | |
744 | "\n Argument #%d (%d -> %d %sexecutable)\n", | |
745 | i, e->src->index, e->dest->index, | |
746 | (e->flags & EDGE_EXECUTABLE) ? "" : "not "); | |
747 | } | |
748 | ||
749 | /* If the incoming edge is executable, Compute the meet operator for | |
750 | the existing value of the PHI node and the current PHI argument. */ | |
751 | if (e->flags & EDGE_EXECUTABLE) | |
752 | { | |
0bca51f0 DN |
753 | tree arg = PHI_ARG_DEF (phi, i); |
754 | prop_value_t arg_val; | |
6de9cd9a | 755 | |
0bca51f0 | 756 | if (is_gimple_min_invariant (arg)) |
750628d8 | 757 | { |
0bca51f0 DN |
758 | arg_val.lattice_val = CONSTANT; |
759 | arg_val.value = arg; | |
760 | arg_val.mem_ref = NULL_TREE; | |
750628d8 DN |
761 | } |
762 | else | |
0bca51f0 | 763 | arg_val = *(get_value (arg, true)); |
6de9cd9a | 764 | |
0bca51f0 | 765 | ccp_lattice_meet (&new_val, &arg_val); |
6de9cd9a | 766 | |
750628d8 DN |
767 | if (dump_file && (dump_flags & TDF_DETAILS)) |
768 | { | |
769 | fprintf (dump_file, "\t"); | |
0bca51f0 DN |
770 | print_generic_expr (dump_file, arg, dump_flags); |
771 | dump_lattice_value (dump_file, "\tValue: ", arg_val); | |
750628d8 DN |
772 | fprintf (dump_file, "\n"); |
773 | } | |
6de9cd9a | 774 | |
750628d8 DN |
775 | if (new_val.lattice_val == VARYING) |
776 | break; | |
777 | } | |
778 | } | |
6de9cd9a DN |
779 | |
780 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
750628d8 DN |
781 | { |
782 | dump_lattice_value (dump_file, "\n PHI node value: ", new_val); | |
783 | fprintf (dump_file, "\n\n"); | |
784 | } | |
785 | ||
8c27b7d4 | 786 | /* Check for an invalid change from UNKNOWN_VAL to UNDEFINED. */ |
0bca51f0 DN |
787 | if (do_store_ccp |
788 | && old_val->lattice_val == UNKNOWN_VAL | |
750628d8 DN |
789 | && new_val.lattice_val == UNDEFINED) |
790 | return SSA_PROP_NOT_INTERESTING; | |
791 | ||
792 | /* Otherwise, make the transition to the new value. */ | |
793 | if (set_lattice_value (PHI_RESULT (phi), new_val)) | |
794 | { | |
795 | if (new_val.lattice_val == VARYING) | |
796 | return SSA_PROP_VARYING; | |
797 | else | |
798 | return SSA_PROP_INTERESTING; | |
799 | } | |
800 | else | |
801 | return SSA_PROP_NOT_INTERESTING; | |
6de9cd9a DN |
802 | } |
803 | ||
804 | ||
750628d8 DN |
805 | /* CCP specific front-end to the non-destructive constant folding |
806 | routines. | |
6de9cd9a DN |
807 | |
808 | Attempt to simplify the RHS of STMT knowing that one or more | |
809 | operands are constants. | |
810 | ||
811 | If simplification is possible, return the simplified RHS, | |
812 | otherwise return the original RHS. */ | |
813 | ||
814 | static tree | |
815 | ccp_fold (tree stmt) | |
816 | { | |
817 | tree rhs = get_rhs (stmt); | |
818 | enum tree_code code = TREE_CODE (rhs); | |
6615c446 | 819 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
6de9cd9a DN |
820 | tree retval = NULL_TREE; |
821 | ||
6de9cd9a | 822 | if (TREE_CODE (rhs) == SSA_NAME) |
0bca51f0 DN |
823 | { |
824 | /* If the RHS is an SSA_NAME, return its known constant value, | |
825 | if any. */ | |
826 | return get_value (rhs, true)->value; | |
827 | } | |
828 | else if (do_store_ccp && stmt_makes_single_load (stmt)) | |
829 | { | |
830 | /* If the RHS is a memory load, see if the VUSEs associated with | |
831 | it are a valid constant for that memory load. */ | |
832 | prop_value_t *val = get_value_loaded_by (stmt, const_val); | |
a318e3ac SB |
833 | if (val && val->mem_ref |
834 | && operand_equal_p (val->mem_ref, rhs, 0)) | |
0bca51f0 DN |
835 | return val->value; |
836 | else | |
837 | return NULL_TREE; | |
838 | } | |
6de9cd9a DN |
839 | |
840 | /* Unary operators. Note that we know the single operand must | |
841 | be a constant. So this should almost always return a | |
842 | simplified RHS. */ | |
6615c446 | 843 | if (kind == tcc_unary) |
6de9cd9a DN |
844 | { |
845 | /* Handle unary operators which can appear in GIMPLE form. */ | |
846 | tree op0 = TREE_OPERAND (rhs, 0); | |
847 | ||
848 | /* Simplify the operand down to a constant. */ | |
849 | if (TREE_CODE (op0) == SSA_NAME) | |
850 | { | |
0bca51f0 | 851 | prop_value_t *val = get_value (op0, true); |
6de9cd9a | 852 | if (val->lattice_val == CONSTANT) |
0bca51f0 | 853 | op0 = get_value (op0, true)->value; |
6de9cd9a DN |
854 | } |
855 | ||
615bc06a | 856 | return fold_unary (code, TREE_TYPE (rhs), op0); |
6de9cd9a DN |
857 | } |
858 | ||
859 | /* Binary and comparison operators. We know one or both of the | |
860 | operands are constants. */ | |
6615c446 JO |
861 | else if (kind == tcc_binary |
862 | || kind == tcc_comparison | |
6de9cd9a DN |
863 | || code == TRUTH_AND_EXPR |
864 | || code == TRUTH_OR_EXPR | |
865 | || code == TRUTH_XOR_EXPR) | |
866 | { | |
867 | /* Handle binary and comparison operators that can appear in | |
868 | GIMPLE form. */ | |
869 | tree op0 = TREE_OPERAND (rhs, 0); | |
870 | tree op1 = TREE_OPERAND (rhs, 1); | |
871 | ||
872 | /* Simplify the operands down to constants when appropriate. */ | |
873 | if (TREE_CODE (op0) == SSA_NAME) | |
874 | { | |
0bca51f0 | 875 | prop_value_t *val = get_value (op0, true); |
6de9cd9a | 876 | if (val->lattice_val == CONSTANT) |
0bca51f0 | 877 | op0 = val->value; |
6de9cd9a DN |
878 | } |
879 | ||
880 | if (TREE_CODE (op1) == SSA_NAME) | |
881 | { | |
0bca51f0 | 882 | prop_value_t *val = get_value (op1, true); |
6de9cd9a | 883 | if (val->lattice_val == CONSTANT) |
0bca51f0 | 884 | op1 = val->value; |
6de9cd9a DN |
885 | } |
886 | ||
615bc06a | 887 | return fold_binary (code, TREE_TYPE (rhs), op0, op1); |
6de9cd9a DN |
888 | } |
889 | ||
890 | /* We may be able to fold away calls to builtin functions if their | |
9cf737f8 | 891 | arguments are constants. */ |
6de9cd9a DN |
892 | else if (code == CALL_EXPR |
893 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR | |
894 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)) | |
895 | == FUNCTION_DECL) | |
896 | && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))) | |
897 | { | |
f47c96aa | 898 | if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_USE)) |
6de9cd9a | 899 | { |
f47c96aa | 900 | tree *orig, var; |
49d1af89 | 901 | tree fndecl, arglist; |
f47c96aa AM |
902 | size_t i = 0; |
903 | ssa_op_iter iter; | |
904 | use_operand_p var_p; | |
6de9cd9a DN |
905 | |
906 | /* Preserve the original values of every operand. */ | |
f47c96aa AM |
907 | orig = xmalloc (sizeof (tree) * NUM_SSA_OPERANDS (stmt, SSA_OP_USE)); |
908 | FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE) | |
909 | orig[i++] = var; | |
6de9cd9a DN |
910 | |
911 | /* Substitute operands with their values and try to fold. */ | |
0bca51f0 | 912 | replace_uses_in (stmt, NULL, const_val); |
49d1af89 KH |
913 | fndecl = get_callee_fndecl (rhs); |
914 | arglist = TREE_OPERAND (rhs, 1); | |
915 | retval = fold_builtin (fndecl, arglist, false); | |
6de9cd9a DN |
916 | |
917 | /* Restore operands to their original form. */ | |
f47c96aa AM |
918 | i = 0; |
919 | FOR_EACH_SSA_USE_OPERAND (var_p, stmt, iter, SSA_OP_USE) | |
920 | SET_USE (var_p, orig[i++]); | |
6de9cd9a DN |
921 | free (orig); |
922 | } | |
923 | } | |
924 | else | |
925 | return rhs; | |
926 | ||
927 | /* If we got a simplified form, see if we need to convert its type. */ | |
928 | if (retval) | |
a32e70c3 | 929 | return fold_convert (TREE_TYPE (rhs), retval); |
6de9cd9a DN |
930 | |
931 | /* No simplification was possible. */ | |
932 | return rhs; | |
933 | } | |
934 | ||
935 | ||
ae3df618 SB |
936 | /* Return the tree representing the element referenced by T if T is an |
937 | ARRAY_REF or COMPONENT_REF into constant aggregates. Return | |
938 | NULL_TREE otherwise. */ | |
939 | ||
940 | static tree | |
941 | fold_const_aggregate_ref (tree t) | |
942 | { | |
943 | prop_value_t *value; | |
4038c495 GB |
944 | tree base, ctor, idx, field; |
945 | unsigned HOST_WIDE_INT cnt; | |
946 | tree cfield, cval; | |
ae3df618 SB |
947 | |
948 | switch (TREE_CODE (t)) | |
949 | { | |
950 | case ARRAY_REF: | |
951 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
952 | DECL_INITIAL. If BASE is a nested reference into another | |
953 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
954 | the inner reference. */ | |
955 | base = TREE_OPERAND (t, 0); | |
956 | switch (TREE_CODE (base)) | |
957 | { | |
958 | case VAR_DECL: | |
959 | if (!TREE_READONLY (base) | |
960 | || TREE_CODE (TREE_TYPE (base)) != ARRAY_TYPE | |
961 | || !targetm.binds_local_p (base)) | |
962 | return NULL_TREE; | |
963 | ||
964 | ctor = DECL_INITIAL (base); | |
965 | break; | |
966 | ||
967 | case ARRAY_REF: | |
968 | case COMPONENT_REF: | |
969 | ctor = fold_const_aggregate_ref (base); | |
970 | break; | |
971 | ||
972 | default: | |
973 | return NULL_TREE; | |
974 | } | |
975 | ||
976 | if (ctor == NULL_TREE | |
977 | || TREE_CODE (ctor) != CONSTRUCTOR | |
978 | || !TREE_STATIC (ctor)) | |
979 | return NULL_TREE; | |
980 | ||
981 | /* Get the index. If we have an SSA_NAME, try to resolve it | |
982 | with the current lattice value for the SSA_NAME. */ | |
983 | idx = TREE_OPERAND (t, 1); | |
984 | switch (TREE_CODE (idx)) | |
985 | { | |
986 | case SSA_NAME: | |
987 | if ((value = get_value (idx, true)) | |
988 | && value->lattice_val == CONSTANT | |
989 | && TREE_CODE (value->value) == INTEGER_CST) | |
990 | idx = value->value; | |
991 | else | |
992 | return NULL_TREE; | |
993 | break; | |
994 | ||
995 | case INTEGER_CST: | |
996 | break; | |
997 | ||
998 | default: | |
999 | return NULL_TREE; | |
1000 | } | |
1001 | ||
1002 | /* Whoo-hoo! I'll fold ya baby. Yeah! */ | |
4038c495 GB |
1003 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
1004 | if (tree_int_cst_equal (cfield, idx)) | |
1005 | return cval; | |
ae3df618 SB |
1006 | break; |
1007 | ||
1008 | case COMPONENT_REF: | |
1009 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
1010 | DECL_INITIAL. If BASE is a nested reference into another | |
1011 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
1012 | the inner reference. */ | |
1013 | base = TREE_OPERAND (t, 0); | |
1014 | switch (TREE_CODE (base)) | |
1015 | { | |
1016 | case VAR_DECL: | |
1017 | if (!TREE_READONLY (base) | |
1018 | || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE | |
1019 | || !targetm.binds_local_p (base)) | |
1020 | return NULL_TREE; | |
1021 | ||
1022 | ctor = DECL_INITIAL (base); | |
1023 | break; | |
1024 | ||
1025 | case ARRAY_REF: | |
1026 | case COMPONENT_REF: | |
1027 | ctor = fold_const_aggregate_ref (base); | |
1028 | break; | |
1029 | ||
1030 | default: | |
1031 | return NULL_TREE; | |
1032 | } | |
1033 | ||
1034 | if (ctor == NULL_TREE | |
1035 | || TREE_CODE (ctor) != CONSTRUCTOR | |
1036 | || !TREE_STATIC (ctor)) | |
1037 | return NULL_TREE; | |
1038 | ||
1039 | field = TREE_OPERAND (t, 1); | |
1040 | ||
4038c495 GB |
1041 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
1042 | if (cfield == field | |
ae3df618 | 1043 | /* FIXME: Handle bit-fields. */ |
4038c495 GB |
1044 | && ! DECL_BIT_FIELD (cfield)) |
1045 | return cval; | |
ae3df618 SB |
1046 | break; |
1047 | ||
1ebd8d9a SB |
1048 | case REALPART_EXPR: |
1049 | case IMAGPART_EXPR: | |
1050 | { | |
1051 | tree c = fold_const_aggregate_ref (TREE_OPERAND (t, 0)); | |
1052 | if (c && TREE_CODE (c) == COMPLEX_CST) | |
1053 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), c); | |
1054 | break; | |
1055 | } | |
1056 | ||
ae3df618 SB |
1057 | default: |
1058 | break; | |
1059 | } | |
1060 | ||
1061 | return NULL_TREE; | |
1062 | } | |
1063 | ||
6de9cd9a DN |
1064 | /* Evaluate statement STMT. */ |
1065 | ||
0bca51f0 | 1066 | static prop_value_t |
6de9cd9a DN |
1067 | evaluate_stmt (tree stmt) |
1068 | { | |
0bca51f0 | 1069 | prop_value_t val; |
6de9cd9a | 1070 | tree simplified; |
0bca51f0 DN |
1071 | ccp_lattice_t likelyvalue = likely_value (stmt); |
1072 | ||
1073 | val.mem_ref = NULL_TREE; | |
6de9cd9a DN |
1074 | |
1075 | /* If the statement is likely to have a CONSTANT result, then try | |
1076 | to fold the statement to determine the constant value. */ | |
1077 | if (likelyvalue == CONSTANT) | |
1078 | simplified = ccp_fold (stmt); | |
1079 | /* If the statement is likely to have a VARYING result, then do not | |
1080 | bother folding the statement. */ | |
1081 | else if (likelyvalue == VARYING) | |
1082 | simplified = get_rhs (stmt); | |
ae3df618 SB |
1083 | /* If the statement is an ARRAY_REF or COMPONENT_REF into constant |
1084 | aggregates, extract the referenced constant. Otherwise the | |
1085 | statement is likely to have an UNDEFINED value, and there will be | |
1086 | nothing to do. Note that fold_const_aggregate_ref returns | |
1087 | NULL_TREE if the first case does not match. */ | |
6de9cd9a | 1088 | else |
ae3df618 | 1089 | simplified = fold_const_aggregate_ref (get_rhs (stmt)); |
6de9cd9a DN |
1090 | |
1091 | if (simplified && is_gimple_min_invariant (simplified)) | |
1092 | { | |
1093 | /* The statement produced a constant value. */ | |
1094 | val.lattice_val = CONSTANT; | |
0bca51f0 | 1095 | val.value = simplified; |
6de9cd9a DN |
1096 | } |
1097 | else | |
1098 | { | |
1099 | /* The statement produced a nonconstant value. If the statement | |
0bca51f0 DN |
1100 | had UNDEFINED operands, then the result of the statement |
1101 | should be UNDEFINED. Otherwise, the statement is VARYING. */ | |
a318e3ac SB |
1102 | if (likelyvalue == UNDEFINED || likelyvalue == UNKNOWN_VAL) |
1103 | val.lattice_val = likelyvalue; | |
1104 | else | |
1105 | val.lattice_val = VARYING; | |
1106 | ||
0bca51f0 | 1107 | val.value = NULL_TREE; |
6de9cd9a | 1108 | } |
750628d8 DN |
1109 | |
1110 | return val; | |
6de9cd9a DN |
1111 | } |
1112 | ||
1113 | ||
750628d8 | 1114 | /* Visit the assignment statement STMT. Set the value of its LHS to the |
0bca51f0 DN |
1115 | value computed by the RHS and store LHS in *OUTPUT_P. If STMT |
1116 | creates virtual definitions, set the value of each new name to that | |
1117 | of the RHS (if we can derive a constant out of the RHS). */ | |
6de9cd9a | 1118 | |
750628d8 DN |
1119 | static enum ssa_prop_result |
1120 | visit_assignment (tree stmt, tree *output_p) | |
6de9cd9a | 1121 | { |
0bca51f0 | 1122 | prop_value_t val; |
750628d8 | 1123 | tree lhs, rhs; |
0bca51f0 | 1124 | enum ssa_prop_result retval; |
6de9cd9a | 1125 | |
750628d8 DN |
1126 | lhs = TREE_OPERAND (stmt, 0); |
1127 | rhs = TREE_OPERAND (stmt, 1); | |
6de9cd9a | 1128 | |
750628d8 DN |
1129 | if (TREE_CODE (rhs) == SSA_NAME) |
1130 | { | |
1131 | /* For a simple copy operation, we copy the lattice values. */ | |
0bca51f0 | 1132 | prop_value_t *nval = get_value (rhs, true); |
750628d8 DN |
1133 | val = *nval; |
1134 | } | |
0bca51f0 | 1135 | else if (do_store_ccp && stmt_makes_single_load (stmt)) |
750628d8 | 1136 | { |
0bca51f0 DN |
1137 | /* Same as above, but the RHS is not a gimple register and yet |
1138 | has a known VUSE. If STMT is loading from the same memory | |
1139 | location that created the SSA_NAMEs for the virtual operands, | |
1140 | we can propagate the value on the RHS. */ | |
1141 | prop_value_t *nval = get_value_loaded_by (stmt, const_val); | |
1142 | ||
a318e3ac SB |
1143 | if (nval && nval->mem_ref |
1144 | && operand_equal_p (nval->mem_ref, rhs, 0)) | |
0bca51f0 DN |
1145 | val = *nval; |
1146 | else | |
1147 | val = evaluate_stmt (stmt); | |
750628d8 DN |
1148 | } |
1149 | else | |
9390c347 | 1150 | /* Evaluate the statement. */ |
750628d8 | 1151 | val = evaluate_stmt (stmt); |
6de9cd9a | 1152 | |
9390c347 | 1153 | /* If the original LHS was a VIEW_CONVERT_EXPR, modify the constant |
ae95537a | 1154 | value to be a VIEW_CONVERT_EXPR of the old constant value. |
9390c347 RK |
1155 | |
1156 | ??? Also, if this was a definition of a bitfield, we need to widen | |
750628d8 DN |
1157 | the constant value into the type of the destination variable. This |
1158 | should not be necessary if GCC represented bitfields properly. */ | |
1159 | { | |
9390c347 RK |
1160 | tree orig_lhs = TREE_OPERAND (stmt, 0); |
1161 | ||
1162 | if (TREE_CODE (orig_lhs) == VIEW_CONVERT_EXPR | |
1163 | && val.lattice_val == CONSTANT) | |
1164 | { | |
987b67bc KH |
1165 | tree w = fold_build1 (VIEW_CONVERT_EXPR, |
1166 | TREE_TYPE (TREE_OPERAND (orig_lhs, 0)), | |
1167 | val.value); | |
ae95537a | 1168 | |
3d36fb45 | 1169 | orig_lhs = TREE_OPERAND (orig_lhs, 0); |
ae95537a | 1170 | if (w && is_gimple_min_invariant (w)) |
0bca51f0 | 1171 | val.value = w; |
ae95537a RK |
1172 | else |
1173 | { | |
1174 | val.lattice_val = VARYING; | |
0bca51f0 | 1175 | val.value = NULL; |
ae95537a | 1176 | } |
9390c347 RK |
1177 | } |
1178 | ||
750628d8 | 1179 | if (val.lattice_val == CONSTANT |
9390c347 RK |
1180 | && TREE_CODE (orig_lhs) == COMPONENT_REF |
1181 | && DECL_BIT_FIELD (TREE_OPERAND (orig_lhs, 1))) | |
6de9cd9a | 1182 | { |
0bca51f0 | 1183 | tree w = widen_bitfield (val.value, TREE_OPERAND (orig_lhs, 1), |
9390c347 | 1184 | orig_lhs); |
750628d8 DN |
1185 | |
1186 | if (w && is_gimple_min_invariant (w)) | |
0bca51f0 | 1187 | val.value = w; |
750628d8 | 1188 | else |
6de9cd9a | 1189 | { |
750628d8 | 1190 | val.lattice_val = VARYING; |
0bca51f0 DN |
1191 | val.value = NULL_TREE; |
1192 | val.mem_ref = NULL_TREE; | |
6de9cd9a | 1193 | } |
6de9cd9a | 1194 | } |
750628d8 | 1195 | } |
6de9cd9a | 1196 | |
0bca51f0 | 1197 | retval = SSA_PROP_NOT_INTERESTING; |
6de9cd9a | 1198 | |
750628d8 | 1199 | /* Set the lattice value of the statement's output. */ |
0bca51f0 | 1200 | if (TREE_CODE (lhs) == SSA_NAME) |
6de9cd9a | 1201 | { |
0bca51f0 DN |
1202 | /* If STMT is an assignment to an SSA_NAME, we only have one |
1203 | value to set. */ | |
1204 | if (set_lattice_value (lhs, val)) | |
1205 | { | |
1206 | *output_p = lhs; | |
1207 | if (val.lattice_val == VARYING) | |
1208 | retval = SSA_PROP_VARYING; | |
1209 | else | |
1210 | retval = SSA_PROP_INTERESTING; | |
1211 | } | |
6de9cd9a | 1212 | } |
0bca51f0 DN |
1213 | else if (do_store_ccp && stmt_makes_single_store (stmt)) |
1214 | { | |
1215 | /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands | |
1216 | to the new constant value and mark the LHS as the memory | |
1217 | reference associated with VAL. */ | |
1218 | ssa_op_iter i; | |
1219 | tree vdef; | |
1220 | bool changed; | |
1221 | ||
1222 | /* Stores cannot take on an UNDEFINED value. */ | |
1223 | if (val.lattice_val == UNDEFINED) | |
1224 | val.lattice_val = UNKNOWN_VAL; | |
1225 | ||
1226 | /* Mark VAL as stored in the LHS of this assignment. */ | |
1227 | val.mem_ref = lhs; | |
1228 | ||
1229 | /* Set the value of every VDEF to VAL. */ | |
1230 | changed = false; | |
1231 | FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS) | |
1232 | changed |= set_lattice_value (vdef, val); | |
1233 | ||
1234 | /* Note that for propagation purposes, we are only interested in | |
1235 | visiting statements that load the exact same memory reference | |
1236 | stored here. Those statements will have the exact same list | |
1237 | of virtual uses, so it is enough to set the output of this | |
1238 | statement to be its first virtual definition. */ | |
1239 | *output_p = first_vdef (stmt); | |
1240 | if (changed) | |
1241 | { | |
1242 | if (val.lattice_val == VARYING) | |
1243 | retval = SSA_PROP_VARYING; | |
1244 | else | |
1245 | retval = SSA_PROP_INTERESTING; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | return retval; | |
6de9cd9a DN |
1250 | } |
1251 | ||
6de9cd9a | 1252 | |
750628d8 DN |
1253 | /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING |
1254 | if it can determine which edge will be taken. Otherwise, return | |
1255 | SSA_PROP_VARYING. */ | |
1256 | ||
1257 | static enum ssa_prop_result | |
1258 | visit_cond_stmt (tree stmt, edge *taken_edge_p) | |
6de9cd9a | 1259 | { |
0bca51f0 | 1260 | prop_value_t val; |
750628d8 DN |
1261 | basic_block block; |
1262 | ||
1263 | block = bb_for_stmt (stmt); | |
1264 | val = evaluate_stmt (stmt); | |
1265 | ||
1266 | /* Find which edge out of the conditional block will be taken and add it | |
1267 | to the worklist. If no single edge can be determined statically, | |
1268 | return SSA_PROP_VARYING to feed all the outgoing edges to the | |
1269 | propagation engine. */ | |
0bca51f0 | 1270 | *taken_edge_p = val.value ? find_taken_edge (block, val.value) : 0; |
750628d8 DN |
1271 | if (*taken_edge_p) |
1272 | return SSA_PROP_INTERESTING; | |
1273 | else | |
1274 | return SSA_PROP_VARYING; | |
6de9cd9a DN |
1275 | } |
1276 | ||
6de9cd9a | 1277 | |
750628d8 DN |
1278 | /* Evaluate statement STMT. If the statement produces an output value and |
1279 | its evaluation changes the lattice value of its output, return | |
1280 | SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the | |
1281 | output value. | |
1282 | ||
1283 | If STMT is a conditional branch and we can determine its truth | |
1284 | value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying | |
1285 | value, return SSA_PROP_VARYING. */ | |
6de9cd9a | 1286 | |
750628d8 DN |
1287 | static enum ssa_prop_result |
1288 | ccp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p) | |
1289 | { | |
750628d8 DN |
1290 | tree def; |
1291 | ssa_op_iter iter; | |
6de9cd9a | 1292 | |
750628d8 | 1293 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6de9cd9a | 1294 | { |
0bca51f0 DN |
1295 | fprintf (dump_file, "\nVisiting statement:\n"); |
1296 | print_generic_stmt (dump_file, stmt, dump_flags); | |
750628d8 | 1297 | fprintf (dump_file, "\n"); |
6de9cd9a | 1298 | } |
6de9cd9a | 1299 | |
0bca51f0 | 1300 | if (TREE_CODE (stmt) == MODIFY_EXPR) |
6de9cd9a | 1301 | { |
750628d8 DN |
1302 | /* If the statement is an assignment that produces a single |
1303 | output value, evaluate its RHS to see if the lattice value of | |
1304 | its output has changed. */ | |
1305 | return visit_assignment (stmt, output_p); | |
6de9cd9a | 1306 | } |
750628d8 | 1307 | else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR) |
6de9cd9a | 1308 | { |
750628d8 DN |
1309 | /* If STMT is a conditional branch, see if we can determine |
1310 | which branch will be taken. */ | |
1311 | return visit_cond_stmt (stmt, taken_edge_p); | |
6de9cd9a | 1312 | } |
6de9cd9a | 1313 | |
750628d8 DN |
1314 | /* Any other kind of statement is not interesting for constant |
1315 | propagation and, therefore, not worth simulating. */ | |
750628d8 DN |
1316 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1317 | fprintf (dump_file, "No interesting values produced. Marked VARYING.\n"); | |
6de9cd9a | 1318 | |
750628d8 DN |
1319 | /* Definitions made by statements other than assignments to |
1320 | SSA_NAMEs represent unknown modifications to their outputs. | |
1321 | Mark them VARYING. */ | |
0bca51f0 DN |
1322 | FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) |
1323 | { | |
1324 | prop_value_t v = { VARYING, NULL_TREE, NULL_TREE }; | |
1325 | set_lattice_value (def, v); | |
1326 | } | |
6de9cd9a | 1327 | |
750628d8 DN |
1328 | return SSA_PROP_VARYING; |
1329 | } | |
6de9cd9a | 1330 | |
6de9cd9a | 1331 | |
0bca51f0 | 1332 | /* Main entry point for SSA Conditional Constant Propagation. */ |
750628d8 DN |
1333 | |
1334 | static void | |
0bca51f0 | 1335 | execute_ssa_ccp (bool store_ccp) |
750628d8 | 1336 | { |
0bca51f0 | 1337 | do_store_ccp = store_ccp; |
750628d8 DN |
1338 | ccp_initialize (); |
1339 | ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node); | |
1340 | ccp_finalize (); | |
6de9cd9a DN |
1341 | } |
1342 | ||
173b818d | 1343 | |
0bca51f0 DN |
1344 | static void |
1345 | do_ssa_ccp (void) | |
1346 | { | |
1347 | execute_ssa_ccp (false); | |
1348 | } | |
1349 | ||
1350 | ||
173b818d | 1351 | static bool |
750628d8 | 1352 | gate_ccp (void) |
173b818d | 1353 | { |
750628d8 | 1354 | return flag_tree_ccp != 0; |
173b818d BB |
1355 | } |
1356 | ||
6de9cd9a | 1357 | |
750628d8 DN |
1358 | struct tree_opt_pass pass_ccp = |
1359 | { | |
1360 | "ccp", /* name */ | |
1361 | gate_ccp, /* gate */ | |
0bca51f0 | 1362 | do_ssa_ccp, /* execute */ |
750628d8 DN |
1363 | NULL, /* sub */ |
1364 | NULL, /* next */ | |
1365 | 0, /* static_pass_number */ | |
1366 | TV_TREE_CCP, /* tv_id */ | |
1367 | PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */ | |
1368 | 0, /* properties_provided */ | |
1369 | 0, /* properties_destroyed */ | |
1370 | 0, /* todo_flags_start */ | |
0bca51f0 | 1371 | TODO_cleanup_cfg | TODO_dump_func | TODO_update_ssa |
750628d8 | 1372 | | TODO_ggc_collect | TODO_verify_ssa |
9f8628ba PB |
1373 | | TODO_verify_stmts, /* todo_flags_finish */ |
1374 | 0 /* letter */ | |
750628d8 | 1375 | }; |
6de9cd9a | 1376 | |
6de9cd9a | 1377 | |
0bca51f0 DN |
1378 | static void |
1379 | do_ssa_store_ccp (void) | |
1380 | { | |
1381 | /* If STORE-CCP is not enabled, we just run regular CCP. */ | |
1382 | execute_ssa_ccp (flag_tree_store_ccp != 0); | |
1383 | } | |
1384 | ||
1385 | static bool | |
1386 | gate_store_ccp (void) | |
1387 | { | |
1388 | /* STORE-CCP is enabled only with -ftree-store-ccp, but when | |
1389 | -fno-tree-store-ccp is specified, we should run regular CCP. | |
1390 | That's why the pass is enabled with either flag. */ | |
1391 | return flag_tree_store_ccp != 0 || flag_tree_ccp != 0; | |
1392 | } | |
1393 | ||
1394 | ||
1395 | struct tree_opt_pass pass_store_ccp = | |
1396 | { | |
1397 | "store_ccp", /* name */ | |
1398 | gate_store_ccp, /* gate */ | |
1399 | do_ssa_store_ccp, /* execute */ | |
1400 | NULL, /* sub */ | |
1401 | NULL, /* next */ | |
1402 | 0, /* static_pass_number */ | |
1403 | TV_TREE_STORE_CCP, /* tv_id */ | |
1404 | PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */ | |
1405 | 0, /* properties_provided */ | |
1406 | 0, /* properties_destroyed */ | |
1407 | 0, /* todo_flags_start */ | |
1408 | TODO_dump_func | TODO_update_ssa | |
1409 | | TODO_ggc_collect | TODO_verify_ssa | |
1410 | | TODO_cleanup_cfg | |
1411 | | TODO_verify_stmts, /* todo_flags_finish */ | |
1412 | 0 /* letter */ | |
1413 | }; | |
1414 | ||
750628d8 DN |
1415 | /* Given a constant value VAL for bitfield FIELD, and a destination |
1416 | variable VAR, return VAL appropriately widened to fit into VAR. If | |
1417 | FIELD is wider than HOST_WIDE_INT, NULL is returned. */ | |
6de9cd9a | 1418 | |
750628d8 DN |
1419 | tree |
1420 | widen_bitfield (tree val, tree field, tree var) | |
6de9cd9a | 1421 | { |
750628d8 DN |
1422 | unsigned HOST_WIDE_INT var_size, field_size; |
1423 | tree wide_val; | |
1424 | unsigned HOST_WIDE_INT mask; | |
1425 | unsigned int i; | |
6de9cd9a | 1426 | |
750628d8 DN |
1427 | /* We can only do this if the size of the type and field and VAL are |
1428 | all constants representable in HOST_WIDE_INT. */ | |
1429 | if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1) | |
1430 | || !host_integerp (DECL_SIZE (field), 1) | |
1431 | || !host_integerp (val, 0)) | |
1432 | return NULL_TREE; | |
6de9cd9a | 1433 | |
750628d8 DN |
1434 | var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1); |
1435 | field_size = tree_low_cst (DECL_SIZE (field), 1); | |
6de9cd9a | 1436 | |
750628d8 DN |
1437 | /* Give up if either the bitfield or the variable are too wide. */ |
1438 | if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT) | |
1439 | return NULL_TREE; | |
6de9cd9a | 1440 | |
1e128c5f | 1441 | gcc_assert (var_size >= field_size); |
6de9cd9a | 1442 | |
750628d8 DN |
1443 | /* If the sign bit of the value is not set or the field's type is unsigned, |
1444 | just mask off the high order bits of the value. */ | |
1445 | if (DECL_UNSIGNED (field) | |
1446 | || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1)))) | |
1447 | { | |
1448 | /* Zero extension. Build a mask with the lower 'field_size' bits | |
1449 | set and a BIT_AND_EXPR node to clear the high order bits of | |
1450 | the value. */ | |
1451 | for (i = 0, mask = 0; i < field_size; i++) | |
1452 | mask |= ((HOST_WIDE_INT) 1) << i; | |
6de9cd9a | 1453 | |
f457cf40 JM |
1454 | wide_val = fold_build2 (BIT_AND_EXPR, TREE_TYPE (var), val, |
1455 | build_int_cst (TREE_TYPE (var), mask)); | |
6de9cd9a | 1456 | } |
750628d8 | 1457 | else |
173b818d | 1458 | { |
750628d8 DN |
1459 | /* Sign extension. Create a mask with the upper 'field_size' |
1460 | bits set and a BIT_IOR_EXPR to set the high order bits of the | |
1461 | value. */ | |
1462 | for (i = 0, mask = 0; i < (var_size - field_size); i++) | |
1463 | mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1); | |
1464 | ||
f457cf40 JM |
1465 | wide_val = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (var), val, |
1466 | build_int_cst (TREE_TYPE (var), mask)); | |
173b818d | 1467 | } |
6de9cd9a | 1468 | |
f457cf40 | 1469 | return wide_val; |
6de9cd9a DN |
1470 | } |
1471 | ||
750628d8 | 1472 | |
6de9cd9a DN |
1473 | /* A subroutine of fold_stmt_r. Attempts to fold *(A+O) to A[X]. |
1474 | BASE is an array type. OFFSET is a byte displacement. ORIG_TYPE | |
9cf737f8 | 1475 | is the desired result type. */ |
6de9cd9a DN |
1476 | |
1477 | static tree | |
1478 | maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type) | |
1479 | { | |
44de5aeb RK |
1480 | tree min_idx, idx, elt_offset = integer_zero_node; |
1481 | tree array_type, elt_type, elt_size; | |
1482 | ||
1483 | /* If BASE is an ARRAY_REF, we can pick up another offset (this time | |
1484 | measured in units of the size of elements type) from that ARRAY_REF). | |
1485 | We can't do anything if either is variable. | |
1486 | ||
1487 | The case we handle here is *(&A[N]+O). */ | |
1488 | if (TREE_CODE (base) == ARRAY_REF) | |
1489 | { | |
1490 | tree low_bound = array_ref_low_bound (base); | |
1491 | ||
1492 | elt_offset = TREE_OPERAND (base, 1); | |
1493 | if (TREE_CODE (low_bound) != INTEGER_CST | |
1494 | || TREE_CODE (elt_offset) != INTEGER_CST) | |
1495 | return NULL_TREE; | |
1496 | ||
1497 | elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0); | |
1498 | base = TREE_OPERAND (base, 0); | |
1499 | } | |
6de9cd9a DN |
1500 | |
1501 | /* Ignore stupid user tricks of indexing non-array variables. */ | |
1502 | array_type = TREE_TYPE (base); | |
1503 | if (TREE_CODE (array_type) != ARRAY_TYPE) | |
1504 | return NULL_TREE; | |
1505 | elt_type = TREE_TYPE (array_type); | |
1506 | if (!lang_hooks.types_compatible_p (orig_type, elt_type)) | |
1507 | return NULL_TREE; | |
1508 | ||
44de5aeb RK |
1509 | /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the |
1510 | element type (so we can use the alignment if it's not constant). | |
1511 | Otherwise, compute the offset as an index by using a division. If the | |
1512 | division isn't exact, then don't do anything. */ | |
6de9cd9a | 1513 | elt_size = TYPE_SIZE_UNIT (elt_type); |
44de5aeb RK |
1514 | if (integer_zerop (offset)) |
1515 | { | |
1516 | if (TREE_CODE (elt_size) != INTEGER_CST) | |
1517 | elt_size = size_int (TYPE_ALIGN (elt_type)); | |
6de9cd9a | 1518 | |
44de5aeb RK |
1519 | idx = integer_zero_node; |
1520 | } | |
1521 | else | |
1522 | { | |
1523 | unsigned HOST_WIDE_INT lquo, lrem; | |
1524 | HOST_WIDE_INT hquo, hrem; | |
1525 | ||
1526 | if (TREE_CODE (elt_size) != INTEGER_CST | |
1527 | || div_and_round_double (TRUNC_DIV_EXPR, 1, | |
1528 | TREE_INT_CST_LOW (offset), | |
1529 | TREE_INT_CST_HIGH (offset), | |
1530 | TREE_INT_CST_LOW (elt_size), | |
1531 | TREE_INT_CST_HIGH (elt_size), | |
1532 | &lquo, &hquo, &lrem, &hrem) | |
1533 | || lrem || hrem) | |
1534 | return NULL_TREE; | |
6de9cd9a | 1535 | |
7d60be94 | 1536 | idx = build_int_cst_wide (NULL_TREE, lquo, hquo); |
44de5aeb RK |
1537 | } |
1538 | ||
1539 | /* Assume the low bound is zero. If there is a domain type, get the | |
1540 | low bound, if any, convert the index into that type, and add the | |
1541 | low bound. */ | |
1542 | min_idx = integer_zero_node; | |
1543 | if (TYPE_DOMAIN (array_type)) | |
6de9cd9a | 1544 | { |
44de5aeb RK |
1545 | if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type))) |
1546 | min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)); | |
1547 | else | |
1548 | min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx); | |
1549 | ||
1550 | if (TREE_CODE (min_idx) != INTEGER_CST) | |
1551 | return NULL_TREE; | |
1552 | ||
1553 | idx = fold_convert (TYPE_DOMAIN (array_type), idx); | |
1554 | elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset); | |
6de9cd9a DN |
1555 | } |
1556 | ||
44de5aeb RK |
1557 | if (!integer_zerop (min_idx)) |
1558 | idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0); | |
1559 | if (!integer_zerop (elt_offset)) | |
1560 | idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0); | |
1561 | ||
1562 | return build (ARRAY_REF, orig_type, base, idx, min_idx, | |
1563 | size_int (tree_low_cst (elt_size, 1) | |
a4e9ffe5 | 1564 | / (TYPE_ALIGN_UNIT (elt_type)))); |
6de9cd9a DN |
1565 | } |
1566 | ||
750628d8 | 1567 | |
6de9cd9a DN |
1568 | /* A subroutine of fold_stmt_r. Attempts to fold *(S+O) to S.X. |
1569 | BASE is a record type. OFFSET is a byte displacement. ORIG_TYPE | |
1570 | is the desired result type. */ | |
1571 | /* ??? This doesn't handle class inheritance. */ | |
1572 | ||
1573 | static tree | |
1574 | maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset, | |
1575 | tree orig_type, bool base_is_ptr) | |
1576 | { | |
f34fa464 | 1577 | tree f, t, field_type, tail_array_field, field_offset; |
6de9cd9a DN |
1578 | |
1579 | if (TREE_CODE (record_type) != RECORD_TYPE | |
1580 | && TREE_CODE (record_type) != UNION_TYPE | |
1581 | && TREE_CODE (record_type) != QUAL_UNION_TYPE) | |
1582 | return NULL_TREE; | |
1583 | ||
1584 | /* Short-circuit silly cases. */ | |
1585 | if (lang_hooks.types_compatible_p (record_type, orig_type)) | |
1586 | return NULL_TREE; | |
1587 | ||
1588 | tail_array_field = NULL_TREE; | |
1589 | for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f)) | |
1590 | { | |
1591 | int cmp; | |
1592 | ||
1593 | if (TREE_CODE (f) != FIELD_DECL) | |
1594 | continue; | |
1595 | if (DECL_BIT_FIELD (f)) | |
1596 | continue; | |
f34fa464 ZD |
1597 | |
1598 | field_offset = byte_position (f); | |
1599 | if (TREE_CODE (field_offset) != INTEGER_CST) | |
6de9cd9a DN |
1600 | continue; |
1601 | ||
1602 | /* ??? Java creates "interesting" fields for representing base classes. | |
1603 | They have no name, and have no context. With no context, we get into | |
1604 | trouble with nonoverlapping_component_refs_p. Skip them. */ | |
1605 | if (!DECL_FIELD_CONTEXT (f)) | |
1606 | continue; | |
1607 | ||
1608 | /* The previous array field isn't at the end. */ | |
1609 | tail_array_field = NULL_TREE; | |
1610 | ||
1611 | /* Check to see if this offset overlaps with the field. */ | |
f34fa464 | 1612 | cmp = tree_int_cst_compare (field_offset, offset); |
6de9cd9a DN |
1613 | if (cmp > 0) |
1614 | continue; | |
1615 | ||
1616 | field_type = TREE_TYPE (f); | |
6de9cd9a DN |
1617 | |
1618 | /* Here we exactly match the offset being checked. If the types match, | |
1619 | then we can return that field. */ | |
53dba802 ZD |
1620 | if (cmp == 0 |
1621 | && lang_hooks.types_compatible_p (orig_type, field_type)) | |
6de9cd9a DN |
1622 | { |
1623 | if (base_is_ptr) | |
1624 | base = build1 (INDIRECT_REF, record_type, base); | |
44de5aeb | 1625 | t = build (COMPONENT_REF, field_type, base, f, NULL_TREE); |
6de9cd9a DN |
1626 | return t; |
1627 | } | |
53dba802 ZD |
1628 | |
1629 | /* Don't care about offsets into the middle of scalars. */ | |
1630 | if (!AGGREGATE_TYPE_P (field_type)) | |
1631 | continue; | |
6de9cd9a | 1632 | |
53dba802 ZD |
1633 | /* Check for array at the end of the struct. This is often |
1634 | used as for flexible array members. We should be able to | |
1635 | turn this into an array access anyway. */ | |
1636 | if (TREE_CODE (field_type) == ARRAY_TYPE) | |
1637 | tail_array_field = f; | |
1638 | ||
1639 | /* Check the end of the field against the offset. */ | |
1640 | if (!DECL_SIZE_UNIT (f) | |
1641 | || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST) | |
1642 | continue; | |
1643 | t = int_const_binop (MINUS_EXPR, offset, field_offset, 1); | |
1644 | if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f))) | |
1645 | continue; | |
6de9cd9a | 1646 | |
53dba802 ZD |
1647 | /* If we matched, then set offset to the displacement into |
1648 | this field. */ | |
1649 | offset = t; | |
6de9cd9a DN |
1650 | goto found; |
1651 | } | |
1652 | ||
1653 | if (!tail_array_field) | |
1654 | return NULL_TREE; | |
1655 | ||
1656 | f = tail_array_field; | |
1657 | field_type = TREE_TYPE (f); | |
53dba802 | 1658 | offset = int_const_binop (MINUS_EXPR, offset, byte_position (f), 1); |
6de9cd9a DN |
1659 | |
1660 | found: | |
1661 | /* If we get here, we've got an aggregate field, and a possibly | |
1ea7e6ad | 1662 | nonzero offset into them. Recurse and hope for a valid match. */ |
6de9cd9a DN |
1663 | if (base_is_ptr) |
1664 | base = build1 (INDIRECT_REF, record_type, base); | |
44de5aeb | 1665 | base = build (COMPONENT_REF, field_type, base, f, NULL_TREE); |
6de9cd9a DN |
1666 | |
1667 | t = maybe_fold_offset_to_array_ref (base, offset, orig_type); | |
1668 | if (t) | |
1669 | return t; | |
1670 | return maybe_fold_offset_to_component_ref (field_type, base, offset, | |
1671 | orig_type, false); | |
1672 | } | |
1673 | ||
750628d8 | 1674 | |
6de9cd9a DN |
1675 | /* A subroutine of fold_stmt_r. Attempt to simplify *(BASE+OFFSET). |
1676 | Return the simplified expression, or NULL if nothing could be done. */ | |
1677 | ||
1678 | static tree | |
1679 | maybe_fold_stmt_indirect (tree expr, tree base, tree offset) | |
1680 | { | |
1681 | tree t; | |
1682 | ||
1683 | /* We may well have constructed a double-nested PLUS_EXPR via multiple | |
1684 | substitutions. Fold that down to one. Remove NON_LVALUE_EXPRs that | |
1685 | are sometimes added. */ | |
1686 | base = fold (base); | |
ff7c8597 | 1687 | STRIP_TYPE_NOPS (base); |
6de9cd9a DN |
1688 | TREE_OPERAND (expr, 0) = base; |
1689 | ||
1690 | /* One possibility is that the address reduces to a string constant. */ | |
1691 | t = fold_read_from_constant_string (expr); | |
1692 | if (t) | |
1693 | return t; | |
1694 | ||
1695 | /* Add in any offset from a PLUS_EXPR. */ | |
1696 | if (TREE_CODE (base) == PLUS_EXPR) | |
1697 | { | |
1698 | tree offset2; | |
1699 | ||
1700 | offset2 = TREE_OPERAND (base, 1); | |
1701 | if (TREE_CODE (offset2) != INTEGER_CST) | |
1702 | return NULL_TREE; | |
1703 | base = TREE_OPERAND (base, 0); | |
1704 | ||
1705 | offset = int_const_binop (PLUS_EXPR, offset, offset2, 1); | |
1706 | } | |
1707 | ||
1708 | if (TREE_CODE (base) == ADDR_EXPR) | |
1709 | { | |
1710 | /* Strip the ADDR_EXPR. */ | |
1711 | base = TREE_OPERAND (base, 0); | |
1712 | ||
0534fa56 RH |
1713 | /* Fold away CONST_DECL to its value, if the type is scalar. */ |
1714 | if (TREE_CODE (base) == CONST_DECL | |
1715 | && is_gimple_min_invariant (DECL_INITIAL (base))) | |
1716 | return DECL_INITIAL (base); | |
1717 | ||
6de9cd9a DN |
1718 | /* Try folding *(&B+O) to B[X]. */ |
1719 | t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr)); | |
1720 | if (t) | |
1721 | return t; | |
1722 | ||
1723 | /* Try folding *(&B+O) to B.X. */ | |
1724 | t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset, | |
1725 | TREE_TYPE (expr), false); | |
1726 | if (t) | |
1727 | return t; | |
1728 | ||
44de5aeb RK |
1729 | /* Fold *&B to B. We can only do this if EXPR is the same type |
1730 | as BASE. We can't do this if EXPR is the element type of an array | |
1731 | and BASE is the array. */ | |
1732 | if (integer_zerop (offset) | |
1733 | && lang_hooks.types_compatible_p (TREE_TYPE (base), | |
1734 | TREE_TYPE (expr))) | |
6de9cd9a DN |
1735 | return base; |
1736 | } | |
1737 | else | |
1738 | { | |
1739 | /* We can get here for out-of-range string constant accesses, | |
1740 | such as "_"[3]. Bail out of the entire substitution search | |
1741 | and arrange for the entire statement to be replaced by a | |
aabcd309 | 1742 | call to __builtin_trap. In all likelihood this will all be |
6de9cd9a DN |
1743 | constant-folded away, but in the meantime we can't leave with |
1744 | something that get_expr_operands can't understand. */ | |
1745 | ||
1746 | t = base; | |
1747 | STRIP_NOPS (t); | |
1748 | if (TREE_CODE (t) == ADDR_EXPR | |
1749 | && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST) | |
1750 | { | |
1751 | /* FIXME: Except that this causes problems elsewhere with dead | |
0e61db61 | 1752 | code not being deleted, and we die in the rtl expanders |
6de9cd9a DN |
1753 | because we failed to remove some ssa_name. In the meantime, |
1754 | just return zero. */ | |
1755 | /* FIXME2: This condition should be signaled by | |
1756 | fold_read_from_constant_string directly, rather than | |
1757 | re-checking for it here. */ | |
1758 | return integer_zero_node; | |
1759 | } | |
1760 | ||
1761 | /* Try folding *(B+O) to B->X. Still an improvement. */ | |
1762 | if (POINTER_TYPE_P (TREE_TYPE (base))) | |
1763 | { | |
1764 | t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)), | |
1765 | base, offset, | |
1766 | TREE_TYPE (expr), true); | |
1767 | if (t) | |
1768 | return t; | |
1769 | } | |
1770 | } | |
1771 | ||
1772 | /* Otherwise we had an offset that we could not simplify. */ | |
1773 | return NULL_TREE; | |
1774 | } | |
1775 | ||
750628d8 | 1776 | |
6de9cd9a DN |
1777 | /* A subroutine of fold_stmt_r. EXPR is a PLUS_EXPR. |
1778 | ||
1779 | A quaint feature extant in our address arithmetic is that there | |
1780 | can be hidden type changes here. The type of the result need | |
1781 | not be the same as the type of the input pointer. | |
1782 | ||
1783 | What we're after here is an expression of the form | |
1784 | (T *)(&array + const) | |
1785 | where the cast doesn't actually exist, but is implicit in the | |
1786 | type of the PLUS_EXPR. We'd like to turn this into | |
1787 | &array[x] | |
1788 | which may be able to propagate further. */ | |
1789 | ||
1790 | static tree | |
1791 | maybe_fold_stmt_addition (tree expr) | |
1792 | { | |
1793 | tree op0 = TREE_OPERAND (expr, 0); | |
1794 | tree op1 = TREE_OPERAND (expr, 1); | |
1795 | tree ptr_type = TREE_TYPE (expr); | |
1796 | tree ptd_type; | |
1797 | tree t; | |
1798 | bool subtract = (TREE_CODE (expr) == MINUS_EXPR); | |
1799 | ||
1800 | /* We're only interested in pointer arithmetic. */ | |
1801 | if (!POINTER_TYPE_P (ptr_type)) | |
1802 | return NULL_TREE; | |
1803 | /* Canonicalize the integral operand to op1. */ | |
1804 | if (INTEGRAL_TYPE_P (TREE_TYPE (op0))) | |
1805 | { | |
1806 | if (subtract) | |
1807 | return NULL_TREE; | |
1808 | t = op0, op0 = op1, op1 = t; | |
1809 | } | |
1810 | /* It had better be a constant. */ | |
1811 | if (TREE_CODE (op1) != INTEGER_CST) | |
1812 | return NULL_TREE; | |
1813 | /* The first operand should be an ADDR_EXPR. */ | |
1814 | if (TREE_CODE (op0) != ADDR_EXPR) | |
1815 | return NULL_TREE; | |
1816 | op0 = TREE_OPERAND (op0, 0); | |
1817 | ||
1818 | /* If the first operand is an ARRAY_REF, expand it so that we can fold | |
1819 | the offset into it. */ | |
1820 | while (TREE_CODE (op0) == ARRAY_REF) | |
1821 | { | |
1822 | tree array_obj = TREE_OPERAND (op0, 0); | |
1823 | tree array_idx = TREE_OPERAND (op0, 1); | |
1824 | tree elt_type = TREE_TYPE (op0); | |
1825 | tree elt_size = TYPE_SIZE_UNIT (elt_type); | |
1826 | tree min_idx; | |
1827 | ||
1828 | if (TREE_CODE (array_idx) != INTEGER_CST) | |
1829 | break; | |
1830 | if (TREE_CODE (elt_size) != INTEGER_CST) | |
1831 | break; | |
1832 | ||
1833 | /* Un-bias the index by the min index of the array type. */ | |
1834 | min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj)); | |
1835 | if (min_idx) | |
1836 | { | |
1837 | min_idx = TYPE_MIN_VALUE (min_idx); | |
1838 | if (min_idx) | |
1839 | { | |
44de5aeb RK |
1840 | if (TREE_CODE (min_idx) != INTEGER_CST) |
1841 | break; | |
1842 | ||
6de9cd9a DN |
1843 | array_idx = convert (TREE_TYPE (min_idx), array_idx); |
1844 | if (!integer_zerop (min_idx)) | |
1845 | array_idx = int_const_binop (MINUS_EXPR, array_idx, | |
1846 | min_idx, 0); | |
1847 | } | |
1848 | } | |
1849 | ||
1850 | /* Convert the index to a byte offset. */ | |
1851 | array_idx = convert (sizetype, array_idx); | |
1852 | array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0); | |
1853 | ||
1854 | /* Update the operands for the next round, or for folding. */ | |
1855 | /* If we're manipulating unsigned types, then folding into negative | |
1856 | values can produce incorrect results. Particularly if the type | |
1857 | is smaller than the width of the pointer. */ | |
1858 | if (subtract | |
1859 | && TYPE_UNSIGNED (TREE_TYPE (op1)) | |
1860 | && tree_int_cst_lt (array_idx, op1)) | |
1861 | return NULL; | |
1862 | op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR, | |
1863 | array_idx, op1, 0); | |
1864 | subtract = false; | |
1865 | op0 = array_obj; | |
1866 | } | |
1867 | ||
1868 | /* If we weren't able to fold the subtraction into another array reference, | |
1869 | canonicalize the integer for passing to the array and component ref | |
1870 | simplification functions. */ | |
1871 | if (subtract) | |
1872 | { | |
1873 | if (TYPE_UNSIGNED (TREE_TYPE (op1))) | |
1874 | return NULL; | |
987b67bc | 1875 | op1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (op1), op1); |
6de9cd9a DN |
1876 | /* ??? In theory fold should always produce another integer. */ |
1877 | if (TREE_CODE (op1) != INTEGER_CST) | |
1878 | return NULL; | |
1879 | } | |
1880 | ||
1881 | ptd_type = TREE_TYPE (ptr_type); | |
1882 | ||
1883 | /* At which point we can try some of the same things as for indirects. */ | |
1884 | t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type); | |
1885 | if (!t) | |
1886 | t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1, | |
1887 | ptd_type, false); | |
1888 | if (t) | |
1889 | t = build1 (ADDR_EXPR, ptr_type, t); | |
1890 | ||
1891 | return t; | |
1892 | } | |
1893 | ||
1894 | /* Subroutine of fold_stmt called via walk_tree. We perform several | |
1895 | simplifications of EXPR_P, mostly having to do with pointer arithmetic. */ | |
1896 | ||
1897 | static tree | |
1898 | fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data) | |
1899 | { | |
1900 | bool *changed_p = data; | |
1901 | tree expr = *expr_p, t; | |
1902 | ||
1903 | /* ??? It'd be nice if walk_tree had a pre-order option. */ | |
1904 | switch (TREE_CODE (expr)) | |
1905 | { | |
1906 | case INDIRECT_REF: | |
1907 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1908 | if (t) | |
1909 | return t; | |
1910 | *walk_subtrees = 0; | |
1911 | ||
1912 | t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0), | |
1913 | integer_zero_node); | |
1914 | break; | |
1915 | ||
1916 | /* ??? Could handle ARRAY_REF here, as a variant of INDIRECT_REF. | |
1917 | We'd only want to bother decomposing an existing ARRAY_REF if | |
1918 | the base array is found to have another offset contained within. | |
1919 | Otherwise we'd be wasting time. */ | |
1920 | ||
1921 | case ADDR_EXPR: | |
1922 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1923 | if (t) | |
1924 | return t; | |
1925 | *walk_subtrees = 0; | |
1926 | ||
1927 | /* Set TREE_INVARIANT properly so that the value is properly | |
1928 | considered constant, and so gets propagated as expected. */ | |
1929 | if (*changed_p) | |
1930 | recompute_tree_invarant_for_addr_expr (expr); | |
1931 | return NULL_TREE; | |
1932 | ||
1933 | case PLUS_EXPR: | |
1934 | case MINUS_EXPR: | |
1935 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1936 | if (t) | |
1937 | return t; | |
1938 | t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL); | |
1939 | if (t) | |
1940 | return t; | |
1941 | *walk_subtrees = 0; | |
1942 | ||
1943 | t = maybe_fold_stmt_addition (expr); | |
1944 | break; | |
1945 | ||
1946 | case COMPONENT_REF: | |
1947 | t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL); | |
1948 | if (t) | |
1949 | return t; | |
1950 | *walk_subtrees = 0; | |
1951 | ||
fa27426e RH |
1952 | /* Make sure the FIELD_DECL is actually a field in the type on the lhs. |
1953 | We've already checked that the records are compatible, so we should | |
1954 | come up with a set of compatible fields. */ | |
1955 | { | |
1956 | tree expr_record = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
1957 | tree expr_field = TREE_OPERAND (expr, 1); | |
1958 | ||
1959 | if (DECL_FIELD_CONTEXT (expr_field) != TYPE_MAIN_VARIANT (expr_record)) | |
1960 | { | |
1961 | expr_field = find_compatible_field (expr_record, expr_field); | |
1962 | TREE_OPERAND (expr, 1) = expr_field; | |
1963 | } | |
1964 | } | |
6de9cd9a DN |
1965 | break; |
1966 | ||
ac182688 ZD |
1967 | case TARGET_MEM_REF: |
1968 | t = maybe_fold_tmr (expr); | |
1969 | break; | |
1970 | ||
6de9cd9a DN |
1971 | default: |
1972 | return NULL_TREE; | |
1973 | } | |
1974 | ||
1975 | if (t) | |
1976 | { | |
1977 | *expr_p = t; | |
1978 | *changed_p = true; | |
1979 | } | |
1980 | ||
1981 | return NULL_TREE; | |
1982 | } | |
1983 | ||
6de9cd9a | 1984 | |
10a0d495 JJ |
1985 | /* Return the string length, maximum string length or maximum value of |
1986 | ARG in LENGTH. | |
1987 | If ARG is an SSA name variable, follow its use-def chains. If LENGTH | |
1988 | is not NULL and, for TYPE == 0, its value is not equal to the length | |
1989 | we determine or if we are unable to determine the length or value, | |
1990 | return false. VISITED is a bitmap of visited variables. | |
1991 | TYPE is 0 if string length should be returned, 1 for maximum string | |
1992 | length and 2 for maximum value ARG can have. */ | |
6de9cd9a | 1993 | |
06a9b53f | 1994 | static bool |
10a0d495 | 1995 | get_maxval_strlen (tree arg, tree *length, bitmap visited, int type) |
6de9cd9a | 1996 | { |
750628d8 DN |
1997 | tree var, def_stmt, val; |
1998 | ||
1999 | if (TREE_CODE (arg) != SSA_NAME) | |
06a9b53f | 2000 | { |
10a0d495 JJ |
2001 | if (type == 2) |
2002 | { | |
2003 | val = arg; | |
2004 | if (TREE_CODE (val) != INTEGER_CST | |
2005 | || tree_int_cst_sgn (val) < 0) | |
2006 | return false; | |
2007 | } | |
2008 | else | |
2009 | val = c_strlen (arg, 1); | |
750628d8 | 2010 | if (!val) |
06a9b53f | 2011 | return false; |
cd709752 | 2012 | |
10a0d495 JJ |
2013 | if (*length) |
2014 | { | |
2015 | if (type > 0) | |
2016 | { | |
2017 | if (TREE_CODE (*length) != INTEGER_CST | |
2018 | || TREE_CODE (val) != INTEGER_CST) | |
2019 | return false; | |
2020 | ||
2021 | if (tree_int_cst_lt (*length, val)) | |
2022 | *length = val; | |
2023 | return true; | |
2024 | } | |
2025 | else if (simple_cst_equal (val, *length) != 1) | |
2026 | return false; | |
2027 | } | |
6de9cd9a | 2028 | |
750628d8 DN |
2029 | *length = val; |
2030 | return true; | |
6de9cd9a | 2031 | } |
06a9b53f | 2032 | |
750628d8 DN |
2033 | /* If we were already here, break the infinite cycle. */ |
2034 | if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg))) | |
2035 | return true; | |
2036 | bitmap_set_bit (visited, SSA_NAME_VERSION (arg)); | |
2037 | ||
2038 | var = arg; | |
2039 | def_stmt = SSA_NAME_DEF_STMT (var); | |
6de9cd9a | 2040 | |
750628d8 DN |
2041 | switch (TREE_CODE (def_stmt)) |
2042 | { | |
2043 | case MODIFY_EXPR: | |
2044 | { | |
10a0d495 JJ |
2045 | tree rhs; |
2046 | ||
750628d8 DN |
2047 | /* The RHS of the statement defining VAR must either have a |
2048 | constant length or come from another SSA_NAME with a constant | |
2049 | length. */ | |
2050 | rhs = TREE_OPERAND (def_stmt, 1); | |
2051 | STRIP_NOPS (rhs); | |
10a0d495 | 2052 | return get_maxval_strlen (rhs, length, visited, type); |
750628d8 | 2053 | } |
6de9cd9a | 2054 | |
750628d8 DN |
2055 | case PHI_NODE: |
2056 | { | |
2057 | /* All the arguments of the PHI node must have the same constant | |
2058 | length. */ | |
2059 | int i; | |
6de9cd9a | 2060 | |
750628d8 DN |
2061 | for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++) |
2062 | { | |
2063 | tree arg = PHI_ARG_DEF (def_stmt, i); | |
6de9cd9a | 2064 | |
750628d8 DN |
2065 | /* If this PHI has itself as an argument, we cannot |
2066 | determine the string length of this argument. However, | |
2067 | if we can find a constant string length for the other | |
2068 | PHI args then we can still be sure that this is a | |
2069 | constant string length. So be optimistic and just | |
2070 | continue with the next argument. */ | |
2071 | if (arg == PHI_RESULT (def_stmt)) | |
2072 | continue; | |
6de9cd9a | 2073 | |
10a0d495 | 2074 | if (!get_maxval_strlen (arg, length, visited, type)) |
750628d8 DN |
2075 | return false; |
2076 | } | |
6de9cd9a | 2077 | |
750628d8 | 2078 | return true; |
173b818d | 2079 | } |
6de9cd9a | 2080 | |
750628d8 DN |
2081 | default: |
2082 | break; | |
6de9cd9a DN |
2083 | } |
2084 | ||
750628d8 DN |
2085 | |
2086 | return false; | |
6de9cd9a DN |
2087 | } |
2088 | ||
2089 | ||
2090 | /* Fold builtin call FN in statement STMT. If it cannot be folded into a | |
2091 | constant, return NULL_TREE. Otherwise, return its constant value. */ | |
2092 | ||
2093 | static tree | |
2094 | ccp_fold_builtin (tree stmt, tree fn) | |
2095 | { | |
10a0d495 | 2096 | tree result, val[3]; |
a32e70c3 | 2097 | tree callee, arglist, a; |
10a0d495 | 2098 | int arg_mask, i, type; |
a32e70c3 RS |
2099 | bitmap visited; |
2100 | bool ignore; | |
6de9cd9a | 2101 | |
a32e70c3 | 2102 | ignore = TREE_CODE (stmt) != MODIFY_EXPR; |
6de9cd9a DN |
2103 | |
2104 | /* First try the generic builtin folder. If that succeeds, return the | |
2105 | result directly. */ | |
49d1af89 KH |
2106 | callee = get_callee_fndecl (fn); |
2107 | arglist = TREE_OPERAND (fn, 1); | |
2108 | result = fold_builtin (callee, arglist, ignore); | |
6de9cd9a | 2109 | if (result) |
10a0d495 JJ |
2110 | { |
2111 | if (ignore) | |
2112 | STRIP_NOPS (result); | |
2113 | return result; | |
2114 | } | |
a32e70c3 RS |
2115 | |
2116 | /* Ignore MD builtins. */ | |
a32e70c3 RS |
2117 | if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD) |
2118 | return NULL_TREE; | |
6de9cd9a DN |
2119 | |
2120 | /* If the builtin could not be folded, and it has no argument list, | |
2121 | we're done. */ | |
2122 | if (!arglist) | |
2123 | return NULL_TREE; | |
2124 | ||
2125 | /* Limit the work only for builtins we know how to simplify. */ | |
2126 | switch (DECL_FUNCTION_CODE (callee)) | |
2127 | { | |
2128 | case BUILT_IN_STRLEN: | |
2129 | case BUILT_IN_FPUTS: | |
2130 | case BUILT_IN_FPUTS_UNLOCKED: | |
10a0d495 JJ |
2131 | arg_mask = 1; |
2132 | type = 0; | |
6de9cd9a DN |
2133 | break; |
2134 | case BUILT_IN_STRCPY: | |
2135 | case BUILT_IN_STRNCPY: | |
10a0d495 JJ |
2136 | arg_mask = 2; |
2137 | type = 0; | |
2138 | break; | |
2139 | case BUILT_IN_MEMCPY_CHK: | |
2140 | case BUILT_IN_MEMPCPY_CHK: | |
2141 | case BUILT_IN_MEMMOVE_CHK: | |
2142 | case BUILT_IN_MEMSET_CHK: | |
2143 | case BUILT_IN_STRNCPY_CHK: | |
2144 | arg_mask = 4; | |
2145 | type = 2; | |
2146 | break; | |
2147 | case BUILT_IN_STRCPY_CHK: | |
2148 | case BUILT_IN_STPCPY_CHK: | |
2149 | arg_mask = 2; | |
2150 | type = 1; | |
2151 | break; | |
2152 | case BUILT_IN_SNPRINTF_CHK: | |
2153 | case BUILT_IN_VSNPRINTF_CHK: | |
2154 | arg_mask = 2; | |
2155 | type = 2; | |
6de9cd9a DN |
2156 | break; |
2157 | default: | |
2158 | return NULL_TREE; | |
2159 | } | |
2160 | ||
2161 | /* Try to use the dataflow information gathered by the CCP process. */ | |
8bdbfff5 | 2162 | visited = BITMAP_ALLOC (NULL); |
6de9cd9a | 2163 | |
10a0d495 | 2164 | memset (val, 0, sizeof (val)); |
6de9cd9a | 2165 | for (i = 0, a = arglist; |
10a0d495 JJ |
2166 | arg_mask; |
2167 | i++, arg_mask >>= 1, a = TREE_CHAIN (a)) | |
2168 | if (arg_mask & 1) | |
6de9cd9a DN |
2169 | { |
2170 | bitmap_clear (visited); | |
10a0d495 JJ |
2171 | if (!get_maxval_strlen (TREE_VALUE (a), &val[i], visited, type)) |
2172 | val[i] = NULL_TREE; | |
6de9cd9a DN |
2173 | } |
2174 | ||
8bdbfff5 | 2175 | BITMAP_FREE (visited); |
6de9cd9a | 2176 | |
a32e70c3 | 2177 | result = NULL_TREE; |
6de9cd9a DN |
2178 | switch (DECL_FUNCTION_CODE (callee)) |
2179 | { | |
2180 | case BUILT_IN_STRLEN: | |
10a0d495 | 2181 | if (val[0]) |
6de9cd9a | 2182 | { |
10a0d495 | 2183 | tree new = fold_convert (TREE_TYPE (fn), val[0]); |
6de9cd9a DN |
2184 | |
2185 | /* If the result is not a valid gimple value, or not a cast | |
2186 | of a valid gimple value, then we can not use the result. */ | |
2187 | if (is_gimple_val (new) | |
2188 | || (is_gimple_cast (new) | |
2189 | && is_gimple_val (TREE_OPERAND (new, 0)))) | |
2190 | return new; | |
6de9cd9a | 2191 | } |
a32e70c3 RS |
2192 | break; |
2193 | ||
6de9cd9a | 2194 | case BUILT_IN_STRCPY: |
10a0d495 JJ |
2195 | if (val[1] && is_gimple_val (val[1])) |
2196 | result = fold_builtin_strcpy (callee, arglist, val[1]); | |
a32e70c3 RS |
2197 | break; |
2198 | ||
6de9cd9a | 2199 | case BUILT_IN_STRNCPY: |
10a0d495 JJ |
2200 | if (val[1] && is_gimple_val (val[1])) |
2201 | result = fold_builtin_strncpy (callee, arglist, val[1]); | |
a32e70c3 RS |
2202 | break; |
2203 | ||
6de9cd9a | 2204 | case BUILT_IN_FPUTS: |
a32e70c3 RS |
2205 | result = fold_builtin_fputs (arglist, |
2206 | TREE_CODE (stmt) != MODIFY_EXPR, 0, | |
10a0d495 | 2207 | val[0]); |
a32e70c3 RS |
2208 | break; |
2209 | ||
6de9cd9a | 2210 | case BUILT_IN_FPUTS_UNLOCKED: |
a32e70c3 RS |
2211 | result = fold_builtin_fputs (arglist, |
2212 | TREE_CODE (stmt) != MODIFY_EXPR, 1, | |
10a0d495 JJ |
2213 | val[0]); |
2214 | break; | |
2215 | ||
2216 | case BUILT_IN_MEMCPY_CHK: | |
2217 | case BUILT_IN_MEMPCPY_CHK: | |
2218 | case BUILT_IN_MEMMOVE_CHK: | |
2219 | case BUILT_IN_MEMSET_CHK: | |
2220 | if (val[2] && is_gimple_val (val[2])) | |
2221 | result = fold_builtin_memory_chk (callee, arglist, val[2], ignore, | |
2222 | DECL_FUNCTION_CODE (callee)); | |
2223 | break; | |
2224 | ||
2225 | case BUILT_IN_STRCPY_CHK: | |
2226 | case BUILT_IN_STPCPY_CHK: | |
2227 | if (val[1] && is_gimple_val (val[1])) | |
2228 | result = fold_builtin_stxcpy_chk (callee, arglist, val[1], ignore, | |
2229 | DECL_FUNCTION_CODE (callee)); | |
2230 | break; | |
2231 | ||
2232 | case BUILT_IN_STRNCPY_CHK: | |
2233 | if (val[2] && is_gimple_val (val[2])) | |
2234 | result = fold_builtin_strncpy_chk (arglist, val[2]); | |
2235 | break; | |
2236 | ||
2237 | case BUILT_IN_SNPRINTF_CHK: | |
2238 | case BUILT_IN_VSNPRINTF_CHK: | |
2239 | if (val[1] && is_gimple_val (val[1])) | |
2240 | result = fold_builtin_snprintf_chk (arglist, val[1], | |
2241 | DECL_FUNCTION_CODE (callee)); | |
a32e70c3 | 2242 | break; |
6de9cd9a DN |
2243 | |
2244 | default: | |
1e128c5f | 2245 | gcc_unreachable (); |
6de9cd9a DN |
2246 | } |
2247 | ||
a32e70c3 | 2248 | if (result && ignore) |
9675412f | 2249 | result = fold_ignored_result (result); |
a32e70c3 | 2250 | return result; |
6de9cd9a DN |
2251 | } |
2252 | ||
2253 | ||
206048bd | 2254 | /* Fold the statement pointed to by STMT_P. In some cases, this function may |
750628d8 DN |
2255 | replace the whole statement with a new one. Returns true iff folding |
2256 | makes any changes. */ | |
6de9cd9a | 2257 | |
750628d8 DN |
2258 | bool |
2259 | fold_stmt (tree *stmt_p) | |
6de9cd9a | 2260 | { |
750628d8 DN |
2261 | tree rhs, result, stmt; |
2262 | bool changed = false; | |
6de9cd9a | 2263 | |
750628d8 | 2264 | stmt = *stmt_p; |
6de9cd9a | 2265 | |
750628d8 DN |
2266 | /* If we replaced constants and the statement makes pointer dereferences, |
2267 | then we may need to fold instances of *&VAR into VAR, etc. */ | |
2268 | if (walk_tree (stmt_p, fold_stmt_r, &changed, NULL)) | |
2269 | { | |
2270 | *stmt_p | |
2271 | = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], | |
2272 | NULL); | |
6de9cd9a DN |
2273 | return true; |
2274 | } | |
2275 | ||
750628d8 DN |
2276 | rhs = get_rhs (stmt); |
2277 | if (!rhs) | |
2278 | return changed; | |
2279 | result = NULL_TREE; | |
6de9cd9a | 2280 | |
750628d8 | 2281 | if (TREE_CODE (rhs) == CALL_EXPR) |
6de9cd9a | 2282 | { |
750628d8 | 2283 | tree callee; |
6de9cd9a | 2284 | |
750628d8 DN |
2285 | /* Check for builtins that CCP can handle using information not |
2286 | available in the generic fold routines. */ | |
2287 | callee = get_callee_fndecl (rhs); | |
2288 | if (callee && DECL_BUILT_IN (callee)) | |
2289 | result = ccp_fold_builtin (stmt, rhs); | |
2290 | else | |
6de9cd9a | 2291 | { |
750628d8 DN |
2292 | /* Check for resolvable OBJ_TYPE_REF. The only sorts we can resolve |
2293 | here are when we've propagated the address of a decl into the | |
2294 | object slot. */ | |
2295 | /* ??? Should perhaps do this in fold proper. However, doing it | |
2296 | there requires that we create a new CALL_EXPR, and that requires | |
2297 | copying EH region info to the new node. Easier to just do it | |
2298 | here where we can just smash the call operand. */ | |
2299 | callee = TREE_OPERAND (rhs, 0); | |
2300 | if (TREE_CODE (callee) == OBJ_TYPE_REF | |
2301 | && lang_hooks.fold_obj_type_ref | |
2302 | && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR | |
6615c446 JO |
2303 | && DECL_P (TREE_OPERAND |
2304 | (OBJ_TYPE_REF_OBJECT (callee), 0))) | |
6de9cd9a | 2305 | { |
750628d8 | 2306 | tree t; |
6de9cd9a | 2307 | |
750628d8 DN |
2308 | /* ??? Caution: Broken ADDR_EXPR semantics means that |
2309 | looking at the type of the operand of the addr_expr | |
2310 | can yield an array type. See silly exception in | |
2311 | check_pointer_types_r. */ | |
6de9cd9a | 2312 | |
750628d8 DN |
2313 | t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee))); |
2314 | t = lang_hooks.fold_obj_type_ref (callee, t); | |
2315 | if (t) | |
2316 | { | |
2317 | TREE_OPERAND (rhs, 0) = t; | |
2318 | changed = true; | |
2319 | } | |
6de9cd9a | 2320 | } |
6de9cd9a | 2321 | } |
6de9cd9a DN |
2322 | } |
2323 | ||
750628d8 DN |
2324 | /* If we couldn't fold the RHS, hand over to the generic fold routines. */ |
2325 | if (result == NULL_TREE) | |
2326 | result = fold (rhs); | |
6de9cd9a | 2327 | |
750628d8 DN |
2328 | /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that |
2329 | may have been added by fold, and "useless" type conversions that might | |
2330 | now be apparent due to propagation. */ | |
2331 | STRIP_USELESS_TYPE_CONVERSION (result); | |
2332 | ||
2333 | if (result != rhs) | |
2334 | changed |= set_rhs (stmt_p, result); | |
2335 | ||
2336 | return changed; | |
6de9cd9a DN |
2337 | } |
2338 | ||
38965eb2 ZD |
2339 | /* Perform the minimal folding on statement STMT. Only operations like |
2340 | *&x created by constant propagation are handled. The statement cannot | |
2341 | be replaced with a new one. */ | |
2342 | ||
2343 | bool | |
2344 | fold_stmt_inplace (tree stmt) | |
2345 | { | |
2346 | tree old_stmt = stmt, rhs, new_rhs; | |
2347 | bool changed = false; | |
2348 | ||
2349 | walk_tree (&stmt, fold_stmt_r, &changed, NULL); | |
2350 | gcc_assert (stmt == old_stmt); | |
2351 | ||
2352 | rhs = get_rhs (stmt); | |
2353 | if (!rhs || rhs == stmt) | |
2354 | return changed; | |
2355 | ||
2356 | new_rhs = fold (rhs); | |
43e364dd | 2357 | STRIP_USELESS_TYPE_CONVERSION (new_rhs); |
38965eb2 ZD |
2358 | if (new_rhs == rhs) |
2359 | return changed; | |
2360 | ||
2361 | changed |= set_rhs (&stmt, new_rhs); | |
2362 | gcc_assert (stmt == old_stmt); | |
2363 | ||
2364 | return changed; | |
2365 | } | |
6de9cd9a | 2366 | \f |
b28b1600 JJ |
2367 | /* Convert EXPR into a GIMPLE value suitable for substitution on the |
2368 | RHS of an assignment. Insert the necessary statements before | |
2369 | iterator *SI_P. */ | |
2370 | ||
2371 | static tree | |
2372 | convert_to_gimple_builtin (block_stmt_iterator *si_p, tree expr) | |
2373 | { | |
2374 | tree_stmt_iterator ti; | |
2375 | tree stmt = bsi_stmt (*si_p); | |
2376 | tree tmp, stmts = NULL; | |
2377 | ||
2378 | push_gimplify_context (); | |
2379 | tmp = get_initialized_tmp_var (expr, &stmts, NULL); | |
2380 | pop_gimplify_context (NULL); | |
2381 | ||
f47c96aa AM |
2382 | if (EXPR_HAS_LOCATION (stmt)) |
2383 | annotate_all_with_locus (&stmts, EXPR_LOCATION (stmt)); | |
2384 | ||
b28b1600 JJ |
2385 | /* The replacement can expose previously unreferenced variables. */ |
2386 | for (ti = tsi_start (stmts); !tsi_end_p (ti); tsi_next (&ti)) | |
2387 | { | |
f47c96aa | 2388 | tree new_stmt = tsi_stmt (ti); |
b28b1600 | 2389 | find_new_referenced_vars (tsi_stmt_ptr (ti)); |
f47c96aa AM |
2390 | bsi_insert_before (si_p, new_stmt, BSI_NEW_STMT); |
2391 | mark_new_vars_to_rename (bsi_stmt (*si_p)); | |
2392 | bsi_next (si_p); | |
b28b1600 JJ |
2393 | } |
2394 | ||
b28b1600 JJ |
2395 | return tmp; |
2396 | } | |
2397 | ||
2398 | ||
6de9cd9a DN |
2399 | /* A simple pass that attempts to fold all builtin functions. This pass |
2400 | is run after we've propagated as many constants as we can. */ | |
2401 | ||
2402 | static void | |
2403 | execute_fold_all_builtins (void) | |
2404 | { | |
a7d6ba24 | 2405 | bool cfg_changed = false; |
6de9cd9a DN |
2406 | basic_block bb; |
2407 | FOR_EACH_BB (bb) | |
2408 | { | |
2409 | block_stmt_iterator i; | |
10a0d495 | 2410 | for (i = bsi_start (bb); !bsi_end_p (i); ) |
6de9cd9a DN |
2411 | { |
2412 | tree *stmtp = bsi_stmt_ptr (i); | |
af47810a | 2413 | tree old_stmt = *stmtp; |
6de9cd9a DN |
2414 | tree call = get_rhs (*stmtp); |
2415 | tree callee, result; | |
10a0d495 | 2416 | enum built_in_function fcode; |
6de9cd9a DN |
2417 | |
2418 | if (!call || TREE_CODE (call) != CALL_EXPR) | |
10a0d495 JJ |
2419 | { |
2420 | bsi_next (&i); | |
2421 | continue; | |
2422 | } | |
6de9cd9a DN |
2423 | callee = get_callee_fndecl (call); |
2424 | if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL) | |
10a0d495 JJ |
2425 | { |
2426 | bsi_next (&i); | |
2427 | continue; | |
2428 | } | |
2429 | fcode = DECL_FUNCTION_CODE (callee); | |
6de9cd9a DN |
2430 | |
2431 | result = ccp_fold_builtin (*stmtp, call); | |
2432 | if (!result) | |
2433 | switch (DECL_FUNCTION_CODE (callee)) | |
2434 | { | |
2435 | case BUILT_IN_CONSTANT_P: | |
2436 | /* Resolve __builtin_constant_p. If it hasn't been | |
2437 | folded to integer_one_node by now, it's fairly | |
2438 | certain that the value simply isn't constant. */ | |
2439 | result = integer_zero_node; | |
2440 | break; | |
2441 | ||
2442 | default: | |
10a0d495 | 2443 | bsi_next (&i); |
6de9cd9a DN |
2444 | continue; |
2445 | } | |
2446 | ||
2447 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2448 | { | |
2449 | fprintf (dump_file, "Simplified\n "); | |
2450 | print_generic_stmt (dump_file, *stmtp, dump_flags); | |
2451 | } | |
2452 | ||
b28b1600 JJ |
2453 | if (!set_rhs (stmtp, result)) |
2454 | { | |
2455 | result = convert_to_gimple_builtin (&i, result); | |
e16acfcd NS |
2456 | if (result) |
2457 | { | |
2458 | bool ok = set_rhs (stmtp, result); | |
2459 | ||
2460 | gcc_assert (ok); | |
2461 | } | |
b28b1600 | 2462 | } |
386cfa6f | 2463 | mark_new_vars_to_rename (*stmtp); |
af47810a | 2464 | if (maybe_clean_or_replace_eh_stmt (old_stmt, *stmtp) |
a7d6ba24 JJ |
2465 | && tree_purge_dead_eh_edges (bb)) |
2466 | cfg_changed = true; | |
6de9cd9a DN |
2467 | |
2468 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2469 | { | |
2470 | fprintf (dump_file, "to\n "); | |
2471 | print_generic_stmt (dump_file, *stmtp, dump_flags); | |
2472 | fprintf (dump_file, "\n"); | |
2473 | } | |
10a0d495 JJ |
2474 | |
2475 | /* Retry the same statement if it changed into another | |
2476 | builtin, there might be new opportunities now. */ | |
2477 | call = get_rhs (*stmtp); | |
2478 | if (!call || TREE_CODE (call) != CALL_EXPR) | |
2479 | { | |
2480 | bsi_next (&i); | |
2481 | continue; | |
2482 | } | |
2483 | callee = get_callee_fndecl (call); | |
2484 | if (!callee | |
2485 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
2486 | || DECL_FUNCTION_CODE (callee) == fcode) | |
2487 | bsi_next (&i); | |
6de9cd9a DN |
2488 | } |
2489 | } | |
a7d6ba24 JJ |
2490 | |
2491 | /* Delete unreachable blocks. */ | |
2492 | if (cfg_changed) | |
2493 | cleanup_tree_cfg (); | |
6de9cd9a DN |
2494 | } |
2495 | ||
750628d8 | 2496 | |
6de9cd9a DN |
2497 | struct tree_opt_pass pass_fold_builtins = |
2498 | { | |
2499 | "fab", /* name */ | |
2500 | NULL, /* gate */ | |
2501 | execute_fold_all_builtins, /* execute */ | |
2502 | NULL, /* sub */ | |
2503 | NULL, /* next */ | |
2504 | 0, /* static_pass_number */ | |
2505 | 0, /* tv_id */ | |
c1b763fa | 2506 | PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */ |
6de9cd9a DN |
2507 | 0, /* properties_provided */ |
2508 | 0, /* properties_destroyed */ | |
2509 | 0, /* todo_flags_start */ | |
b28b1600 JJ |
2510 | TODO_dump_func |
2511 | | TODO_verify_ssa | |
0bca51f0 | 2512 | | TODO_update_ssa, /* todo_flags_finish */ |
9f8628ba | 2513 | 0 /* letter */ |
6de9cd9a | 2514 | }; |