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