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