re PR debug/54551 (DF resets some DEBUG_INSNs unnecessarily)
[gcc.git] / gcc / valtrack.c
1 /* Infrastructure for tracking user variable locations and values
2 throughout compilation.
3 Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "valtrack.h"
28 #include "function.h"
29 #include "regs.h"
30 #include "emit-rtl.h"
31
32 /* Replace auto-increment addressing modes with explicit operations to access
33 the same addresses without modifying the corresponding registers. */
34
35 static rtx
36 cleanup_auto_inc_dec (rtx src, enum machine_mode mem_mode ATTRIBUTE_UNUSED)
37 {
38 rtx x = src;
39 #ifdef AUTO_INC_DEC
40 const RTX_CODE code = GET_CODE (x);
41 int i;
42 const char *fmt;
43
44 switch (code)
45 {
46 case REG:
47 CASE_CONST_ANY:
48 case SYMBOL_REF:
49 case CODE_LABEL:
50 case PC:
51 case CC0:
52 case SCRATCH:
53 /* SCRATCH must be shared because they represent distinct values. */
54 return x;
55 case CLOBBER:
56 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
57 return x;
58 break;
59
60 case CONST:
61 if (shared_const_p (x))
62 return x;
63 break;
64
65 case MEM:
66 mem_mode = GET_MODE (x);
67 break;
68
69 case PRE_INC:
70 case PRE_DEC:
71 gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
72 return gen_rtx_PLUS (GET_MODE (x),
73 cleanup_auto_inc_dec (XEXP (x, 0), mem_mode),
74 GEN_INT (code == PRE_INC
75 ? GET_MODE_SIZE (mem_mode)
76 : -GET_MODE_SIZE (mem_mode)));
77
78 case POST_INC:
79 case POST_DEC:
80 case PRE_MODIFY:
81 case POST_MODIFY:
82 return cleanup_auto_inc_dec (code == PRE_MODIFY
83 ? XEXP (x, 1) : XEXP (x, 0),
84 mem_mode);
85
86 default:
87 break;
88 }
89
90 /* Copy the various flags, fields, and other information. We assume
91 that all fields need copying, and then clear the fields that should
92 not be copied. That is the sensible default behavior, and forces
93 us to explicitly document why we are *not* copying a flag. */
94 x = shallow_copy_rtx (x);
95
96 /* We do not copy the USED flag, which is used as a mark bit during
97 walks over the RTL. */
98 RTX_FLAG (x, used) = 0;
99
100 /* We do not copy FRAME_RELATED for INSNs. */
101 if (INSN_P (x))
102 RTX_FLAG (x, frame_related) = 0;
103
104 fmt = GET_RTX_FORMAT (code);
105 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
106 if (fmt[i] == 'e')
107 XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), mem_mode);
108 else if (fmt[i] == 'E' || fmt[i] == 'V')
109 {
110 int j;
111 XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
112 for (j = 0; j < XVECLEN (x, i); j++)
113 XVECEXP (x, i, j)
114 = cleanup_auto_inc_dec (XVECEXP (src, i, j), mem_mode);
115 }
116
117 #else /* !AUTO_INC_DEC */
118 x = copy_rtx (x);
119 #endif /* !AUTO_INC_DEC */
120
121 return x;
122 }
123
124 /* Auxiliary data structure for propagate_for_debug_stmt. */
125
126 struct rtx_subst_pair
127 {
128 rtx to;
129 bool adjusted;
130 };
131
132 /* DATA points to an rtx_subst_pair. Return the value that should be
133 substituted. */
134
135 static rtx
136 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
137 {
138 struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
139
140 if (!rtx_equal_p (from, old_rtx))
141 return NULL_RTX;
142 if (!pair->adjusted)
143 {
144 pair->adjusted = true;
145 pair->to = cleanup_auto_inc_dec (pair->to, VOIDmode);
146 pair->to = make_compound_operation (pair->to, SET);
147 return pair->to;
148 }
149 return copy_rtx (pair->to);
150 }
151
152 /* Replace all the occurrences of DEST with SRC in DEBUG_INSNs between INSN
153 and LAST, not including INSN, but including LAST. Also stop at the end
154 of THIS_BASIC_BLOCK. */
155
156 void
157 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src,
158 basic_block this_basic_block)
159 {
160 rtx next, loc, end = NEXT_INSN (BB_END (this_basic_block));
161
162 struct rtx_subst_pair p;
163 p.to = src;
164 p.adjusted = false;
165
166 next = NEXT_INSN (insn);
167 last = NEXT_INSN (last);
168 while (next != last && next != end)
169 {
170 insn = next;
171 next = NEXT_INSN (insn);
172 if (DEBUG_INSN_P (insn))
173 {
174 loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
175 dest, propagate_for_debug_subst, &p);
176 if (loc == INSN_VAR_LOCATION_LOC (insn))
177 continue;
178 INSN_VAR_LOCATION_LOC (insn) = loc;
179 df_insn_rescan (insn);
180 }
181 }
182 }
183
184 /* Initialize DEBUG to an empty list, and clear USED, if given. */
185
186 void
187 dead_debug_global_init (struct dead_debug_global *debug, bitmap used)
188 {
189 debug->used = used;
190 if (used)
191 bitmap_clear (used);
192 }
193
194 /* Initialize DEBUG to an empty list, and clear USED, if given. Link
195 back to GLOBAL, if given, and bring in used bits from it. */
196
197 void
198 dead_debug_local_init (struct dead_debug_local *debug, bitmap used,
199 struct dead_debug_global *global)
200 {
201 if (!used && global && global->used)
202 used = BITMAP_ALLOC (NULL);
203
204 debug->head = NULL;
205 debug->global = global;
206 debug->used = used;
207 debug->to_rescan = NULL;
208
209 if (used)
210 {
211 if (global && global->used)
212 bitmap_copy (used, global->used);
213 else
214 bitmap_clear (used);
215 }
216 }
217
218 /* Locate the entry for REG in GLOBAL->htab. */
219
220 static dead_debug_global_entry *
221 dead_debug_global_find (struct dead_debug_global *global, rtx reg)
222 {
223 dead_debug_global_entry temp_entry;
224 temp_entry.reg = reg;
225
226 dead_debug_global_entry *entry = global->htab.find (&temp_entry);
227 gcc_checking_assert (entry && entry->reg == temp_entry.reg);
228 gcc_checking_assert (entry->dtemp);
229
230 return entry;
231 }
232
233 /* Insert an entry mapping REG to DTEMP in GLOBAL->htab. */
234
235 static void
236 dead_debug_global_insert (struct dead_debug_global *global, rtx reg, rtx dtemp)
237 {
238 dead_debug_global_entry temp_entry;
239 temp_entry.reg = reg;
240 temp_entry.dtemp = dtemp;
241
242 if (!global->htab.is_created ())
243 global->htab.create (31);
244
245 dead_debug_global_entry **slot = global->htab.find_slot (&temp_entry, INSERT);
246 gcc_checking_assert (!*slot);
247 *slot = XNEW (dead_debug_global_entry);
248 **slot = temp_entry;
249 }
250
251 /* If UREGNO, referenced by USE, is a pseudo marked as used in GLOBAL,
252 replace it with with a USE of the debug temp recorded for it, and
253 return TRUE. Otherwise, just return FALSE.
254
255 If PTO_RESCAN is given, instead of rescanning modified INSNs right
256 away, add their UIDs to the bitmap, allocating one of *PTO_RESCAN
257 is NULL. */
258
259 static bool
260 dead_debug_global_replace_temp (struct dead_debug_global *global,
261 df_ref use, unsigned int uregno,
262 bitmap *pto_rescan)
263 {
264 if (!global || uregno < FIRST_PSEUDO_REGISTER
265 || !global->used
266 || !bitmap_bit_p (global->used, uregno))
267 return false;
268
269 gcc_checking_assert (REGNO (*DF_REF_REAL_LOC (use)) == uregno);
270
271 dead_debug_global_entry *entry
272 = dead_debug_global_find (global, *DF_REF_REAL_LOC (use));
273 gcc_checking_assert (GET_CODE (entry->reg) == REG
274 && REGNO (entry->reg) == uregno);
275
276 *DF_REF_REAL_LOC (use) = entry->dtemp;
277 if (!pto_rescan)
278 df_insn_rescan (DF_REF_INSN (use));
279 else
280 {
281 if (!*pto_rescan)
282 *pto_rescan = BITMAP_ALLOC (NULL);
283 bitmap_set_bit (*pto_rescan, INSN_UID (DF_REF_INSN (use)));
284 }
285
286 return true;
287 }
288
289 /* Reset all debug uses in HEAD, and clear DEBUG->to_rescan bits of
290 each reset insn. DEBUG is not otherwise modified. If HEAD is
291 DEBUG->head, DEBUG->head will be set to NULL at the end.
292 Otherwise, entries from DEBUG->head that pertain to reset insns
293 will be removed, and only then rescanned. */
294
295 static void
296 dead_debug_reset_uses (struct dead_debug_local *debug,
297 struct dead_debug_use *head)
298 {
299 bool got_head = (debug->head == head);
300 bitmap rescan;
301 struct dead_debug_use **tailp = &debug->head;
302 struct dead_debug_use *cur;
303 bitmap_iterator bi;
304 unsigned int uid;
305
306 if (got_head)
307 rescan = NULL;
308 else
309 rescan = BITMAP_ALLOC (NULL);
310
311 while (head)
312 {
313 struct dead_debug_use *next = head->next;
314 rtx insn;
315
316 insn = DF_REF_INSN (head->use);
317 if (!next || DF_REF_INSN (next->use) != insn)
318 {
319 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
320 if (got_head)
321 df_insn_rescan_debug_internal (insn);
322 else
323 bitmap_set_bit (rescan, INSN_UID (insn));
324 if (debug->to_rescan)
325 bitmap_clear_bit (debug->to_rescan, INSN_UID (insn));
326 }
327 XDELETE (head);
328 head = next;
329 }
330
331 if (got_head)
332 {
333 debug->head = NULL;
334 return;
335 }
336
337 while ((cur = *tailp))
338 if (bitmap_bit_p (rescan, INSN_UID (DF_REF_INSN (cur->use))))
339 {
340 *tailp = cur->next;
341 XDELETE (cur);
342 }
343 else
344 tailp = &cur->next;
345
346 EXECUTE_IF_SET_IN_BITMAP (rescan, 0, uid, bi)
347 {
348 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
349 if (insn_info)
350 df_insn_rescan_debug_internal (insn_info->insn);
351 }
352
353 BITMAP_FREE (rescan);
354 }
355
356 /* Promote pending local uses of pseudos in DEBUG to global
357 substitutions. Uses of non-pseudos are left alone for
358 resetting. */
359
360 static void
361 dead_debug_promote_uses (struct dead_debug_local *debug)
362 {
363 for (struct dead_debug_use *head = debug->head, **headp = &debug->head;
364 head; head = *headp)
365 {
366 rtx reg = *DF_REF_REAL_LOC (head->use);
367
368 if (GET_CODE (reg) != REG
369 || REGNO (reg) < FIRST_PSEUDO_REGISTER)
370 {
371 headp = &head->next;
372 continue;
373 }
374
375 if (!debug->global->used)
376 debug->global->used = BITMAP_ALLOC (NULL);
377
378 if (bitmap_set_bit (debug->global->used, REGNO (reg)))
379 dead_debug_global_insert (debug->global, reg,
380 make_debug_expr_from_rtl (reg));
381
382 if (!dead_debug_global_replace_temp (debug->global, head->use,
383 REGNO (reg), &debug->to_rescan))
384 {
385 headp = &head->next;
386 continue;
387 }
388
389 *headp = head->next;
390 XDELETE (head);
391 }
392 }
393
394 /* Reset all debug insns with pending uses. Release the bitmap in it,
395 unless it is USED. USED must be the same bitmap passed to
396 dead_debug_local_init. */
397
398 void
399 dead_debug_local_finish (struct dead_debug_local *debug, bitmap used)
400 {
401 if (debug->used != used)
402 BITMAP_FREE (debug->used);
403
404 if (debug->global)
405 dead_debug_promote_uses (debug);
406
407 dead_debug_reset_uses (debug, debug->head);
408
409 if (debug->to_rescan)
410 {
411 bitmap_iterator bi;
412 unsigned int uid;
413
414 EXECUTE_IF_SET_IN_BITMAP (debug->to_rescan, 0, uid, bi)
415 {
416 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
417 if (insn_info)
418 df_insn_rescan (insn_info->insn);
419 }
420 BITMAP_FREE (debug->to_rescan);
421 }
422 }
423
424 /* Release GLOBAL->used unless it is the same as USED. Release the
425 mapping hash table if it was initialized. */
426
427 void
428 dead_debug_global_finish (struct dead_debug_global *global, bitmap used)
429 {
430 if (global->used != used)
431 BITMAP_FREE (global->used);
432
433 if (global->htab.is_created ())
434 global->htab.dispose ();
435 }
436
437 /* Add USE to DEBUG, or substitute it right away if it's a pseudo in
438 the global substitution list. USE must be a dead reference to
439 UREGNO in a debug insn. Create a bitmap for DEBUG as needed. */
440
441 void
442 dead_debug_add (struct dead_debug_local *debug, df_ref use, unsigned int uregno)
443 {
444 if (dead_debug_global_replace_temp (debug->global, use, uregno,
445 &debug->to_rescan))
446 return;
447
448 struct dead_debug_use *newddu = XNEW (struct dead_debug_use);
449
450 newddu->use = use;
451 newddu->next = debug->head;
452 debug->head = newddu;
453
454 if (!debug->used)
455 debug->used = BITMAP_ALLOC (NULL);
456
457 /* ??? If we dealt with split multi-registers below, we should set
458 all registers for the used mode in case of hardware
459 registers. */
460 bitmap_set_bit (debug->used, uregno);
461 }
462
463 /* If UREGNO is referenced by any entry in DEBUG, emit a debug insn
464 before or after INSN (depending on WHERE), that binds a (possibly
465 global) debug temp to the widest-mode use of UREGNO, if WHERE is
466 *_WITH_REG, or the value stored in UREGNO by INSN otherwise, and
467 replace all uses of UREGNO in DEBUG with uses of the debug temp.
468 INSN must be where UREGNO dies, if WHERE is *_BEFORE_*, or where it
469 is set otherwise. Return the number of debug insns emitted. */
470
471 int
472 dead_debug_insert_temp (struct dead_debug_local *debug, unsigned int uregno,
473 rtx insn, enum debug_temp_where where)
474 {
475 struct dead_debug_use **tailp = &debug->head;
476 struct dead_debug_use *cur;
477 struct dead_debug_use *uses = NULL;
478 struct dead_debug_use **usesp = &uses;
479 rtx reg = NULL_RTX;
480 rtx breg;
481 rtx dval = NULL_RTX;
482 rtx bind;
483 bool global;
484
485 if (!debug->used)
486 return 0;
487
488 global = (debug->global && debug->global->used
489 && bitmap_bit_p (debug->global->used, uregno));
490
491 if (!global && !bitmap_clear_bit (debug->used, uregno))
492 return 0;
493
494 /* Move all uses of uregno from debug->head to uses, setting mode to
495 the widest referenced mode. */
496 while ((cur = *tailp))
497 {
498 if (DF_REF_REGNO (cur->use) == uregno)
499 {
500 /* If this loc has been changed e.g. to debug_expr already
501 as part of a multi-register use, just drop it. */
502 if (!REG_P (*DF_REF_REAL_LOC (cur->use)))
503 {
504 *tailp = cur->next;
505 XDELETE (cur);
506 continue;
507 }
508 *usesp = cur;
509 usesp = &cur->next;
510 *tailp = cur->next;
511 cur->next = NULL;
512 if (!reg
513 || (GET_MODE_BITSIZE (GET_MODE (reg))
514 < GET_MODE_BITSIZE (GET_MODE (*DF_REF_REAL_LOC (cur->use)))))
515 reg = *DF_REF_REAL_LOC (cur->use);
516 }
517 else
518 tailp = &(*tailp)->next;
519 }
520
521 /* We may have dangling bits in debug->used for registers that were part
522 of a multi-register use, one component of which has been reset. */
523 if (reg == NULL)
524 {
525 gcc_checking_assert (!uses);
526 if (!global)
527 return 0;
528 }
529
530 if (global)
531 {
532 if (!reg)
533 reg = regno_reg_rtx[uregno];
534 dead_debug_global_entry *entry
535 = dead_debug_global_find (debug->global, reg);
536 gcc_checking_assert (entry->reg == reg);
537 dval = entry->dtemp;
538 }
539
540 gcc_checking_assert (uses || global);
541
542 breg = reg;
543 /* Recover the expression INSN stores in REG. */
544 if (where == DEBUG_TEMP_BEFORE_WITH_VALUE)
545 {
546 rtx set = single_set (insn);
547 rtx dest, src;
548
549 if (set)
550 {
551 dest = SET_DEST (set);
552 src = SET_SRC (set);
553 /* Lose if the REG-setting insn is a CALL. */
554 if (GET_CODE (src) == CALL)
555 {
556 while (uses)
557 {
558 cur = uses->next;
559 XDELETE (uses);
560 uses = cur;
561 }
562 return 0;
563 }
564 }
565
566 /* ??? Should we try to extract it from a PARALLEL? */
567 if (!set)
568 breg = NULL;
569 /* Cool, it's the same REG, we can use SRC. */
570 else if (dest == reg)
571 breg = cleanup_auto_inc_dec (src, VOIDmode);
572 else if (REG_P (dest))
573 {
574 /* Hmm... Something's fishy, we should be setting REG here. */
575 if (REGNO (dest) != REGNO (reg))
576 breg = NULL;
577 /* If we're not overwriting all the hardware registers that
578 setting REG in its mode would, we won't know what to bind
579 the debug temp to. ??? We could bind the debug_expr to a
580 CONCAT or PARALLEL with the split multi-registers, and
581 replace them as we found the corresponding sets. */
582 else if (REGNO (reg) < FIRST_PSEUDO_REGISTER
583 && (hard_regno_nregs[REGNO (reg)][GET_MODE (reg)]
584 != hard_regno_nregs[REGNO (reg)][GET_MODE (dest)]))
585 breg = NULL;
586 /* Ok, it's the same (hardware) REG, but with a different
587 mode, so SUBREG it. */
588 else
589 breg = lowpart_subreg (GET_MODE (reg),
590 cleanup_auto_inc_dec (src, VOIDmode),
591 GET_MODE (dest));
592 }
593 else if (GET_CODE (dest) == SUBREG)
594 {
595 /* We should be setting REG here. Lose. */
596 if (REGNO (SUBREG_REG (dest)) != REGNO (reg))
597 breg = NULL;
598 /* Lose if we're setting something other than the lowpart of
599 REG. */
600 else if (!subreg_lowpart_p (dest))
601 breg = NULL;
602 /* If we're not overwriting all the hardware registers that
603 setting REG in its mode would, we won't know what to bind
604 the debug temp to. */
605 else if (REGNO (reg) < FIRST_PSEUDO_REGISTER
606 && (hard_regno_nregs[REGNO (reg)][GET_MODE (reg)]
607 != hard_regno_nregs[REGNO (reg)][GET_MODE (dest)]))
608 breg = NULL;
609 /* Yay, we can use SRC, just adjust its mode. */
610 else
611 breg = lowpart_subreg (GET_MODE (reg),
612 cleanup_auto_inc_dec (src, VOIDmode),
613 GET_MODE (dest));
614 }
615 /* Oh well, we're out of luck. */
616 else
617 breg = NULL;
618
619 /* We couldn't figure out the value stored in REG, so reset all
620 of its pending debug uses. */
621 if (!breg)
622 {
623 dead_debug_reset_uses (debug, uses);
624 return 0;
625 }
626 }
627
628 /* If there's a single (debug) use of an otherwise unused REG, and
629 the debug use is not part of a larger expression, then it
630 probably doesn't make sense to introduce a new debug temp. */
631 if (where == DEBUG_TEMP_AFTER_WITH_REG && !uses->next)
632 {
633 rtx next = DF_REF_INSN (uses->use);
634
635 if (DEBUG_INSN_P (next) && reg == INSN_VAR_LOCATION_LOC (next))
636 {
637 XDELETE (uses);
638 return 0;
639 }
640 }
641
642 if (!global)
643 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
644 dval = make_debug_expr_from_rtl (reg);
645
646 /* Emit a debug bind insn before the insn in which reg dies. */
647 bind = gen_rtx_VAR_LOCATION (GET_MODE (reg),
648 DEBUG_EXPR_TREE_DECL (dval), breg,
649 VAR_INIT_STATUS_INITIALIZED);
650
651 if (where == DEBUG_TEMP_AFTER_WITH_REG)
652 bind = emit_debug_insn_after (bind, insn);
653 else
654 bind = emit_debug_insn_before (bind, insn);
655 df_insn_rescan (bind);
656
657 /* Adjust all uses. */
658 while ((cur = uses))
659 {
660 if (GET_MODE (*DF_REF_REAL_LOC (cur->use)) == GET_MODE (reg))
661 *DF_REF_REAL_LOC (cur->use) = dval;
662 else
663 *DF_REF_REAL_LOC (cur->use)
664 = gen_lowpart_SUBREG (GET_MODE (*DF_REF_REAL_LOC (cur->use)), dval);
665 /* ??? Should we simplify subreg of subreg? */
666 if (debug->to_rescan == NULL)
667 debug->to_rescan = BITMAP_ALLOC (NULL);
668 bitmap_set_bit (debug->to_rescan, INSN_UID (DF_REF_INSN (cur->use)));
669 uses = cur->next;
670 XDELETE (cur);
671 }
672
673 return 1;
674 }