print_rtx_function: integrate dumping of the CFG into the insn chain
[official-gcc.git] / gcc / print-rtl-function.c
blob4f9b4efdb2e3cd0a25f82799aa657210e15a8081
1 /* Print RTL functions for GCC.
2 Copyright (C) 2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "alias.h"
26 #include "tree.h"
27 #include "cfg.h"
28 #include "flags.h"
29 #include "predict.h"
30 #include "function.h"
31 #include "basic-block.h"
32 #include "print-rtl.h"
33 #include "langhooks.h"
34 #include "emit-rtl.h"
36 /* Print an "(edge-from)" or "(edge-to)" directive describing E
37 to OUTFILE. */
39 static void
40 print_edge (FILE *outfile, edge e, bool from)
42 fprintf (outfile, " (%s ", from ? "edge-from" : "edge-to");
43 basic_block bb = from ? e->src : e->dest;
44 gcc_assert (bb);
45 switch (bb->index)
47 case ENTRY_BLOCK:
48 fprintf (outfile, "entry");
49 break;
50 case EXIT_BLOCK:
51 fprintf (outfile, "exit");
52 break;
53 default:
54 fprintf (outfile, "%i", bb->index);
55 break;
58 /* Express edge flags as a string with " | " separator.
59 e.g. (flags "FALLTHRU | DFS_BACK"). */
60 fprintf (outfile, " (flags \"");
61 bool seen_flag = false;
62 #define DEF_EDGE_FLAG(NAME,IDX) \
63 do { \
64 if (e->flags & EDGE_##NAME) \
65 { \
66 if (seen_flag) \
67 fprintf (outfile, " | "); \
68 fprintf (outfile, "%s", (#NAME)); \
69 seen_flag = true; \
70 } \
71 } while (0);
72 #include "cfg-flags.def"
73 #undef DEF_EDGE_FLAG
75 fprintf (outfile, "\"))\n");
78 /* If BB is non-NULL, print the start of a "(block)" directive for it
79 to OUTFILE, otherwise do nothing. */
81 static void
82 begin_any_block (FILE *outfile, basic_block bb)
84 if (!bb)
85 return;
87 edge e;
88 edge_iterator ei;
90 fprintf (outfile, " (block %i\n", bb->index);
91 FOR_EACH_EDGE (e, ei, bb->preds)
92 print_edge (outfile, e, true);
95 /* If BB is non-NULL, print the end of a "(block)" directive for it
96 to OUTFILE, otherwise do nothing. */
98 static void
99 end_any_block (FILE *outfile, basic_block bb)
101 if (!bb)
102 return;
104 edge e;
105 edge_iterator ei;
107 FOR_EACH_EDGE (e, ei, bb->succs)
108 print_edge (outfile, e, false);
109 fprintf (outfile, " ) ;; block %i\n", bb->index);
112 /* Determine if INSN is of a kind that can have a basic block. */
114 static bool
115 can_have_basic_block_p (const rtx_insn *insn)
117 rtx_code code = GET_CODE (insn);
118 if (code == BARRIER)
119 return false;
120 gcc_assert (GET_RTX_FORMAT (code)[2] == 'B');
121 return true;
124 /* Write FN to OUTFILE in a form suitable for parsing, with indentation
125 and comments to make the structure easy for a human to grok. Track
126 the basic blocks of insns in the chain, wrapping those that are within
127 blocks within "(block)" directives.
129 Example output:
131 (function "times_two"
132 (insn-chain
133 (note 1 0 4 (nil) NOTE_INSN_DELETED)
134 (block 2
135 (edge-from entry (flags "FALLTHRU"))
136 (note 4 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
137 (insn 2 4 3 2 (set (mem/c:SI (plus:DI (reg/f:DI 82 virtual-stack-vars)
138 (const_int -4 [0xfffffffffffffffc])) [1 i+0 S4 A32])
139 (reg:SI 5 di [ i ])) t.c:2 -1
140 (nil))
141 (note 3 2 6 2 NOTE_INSN_FUNCTION_BEG)
142 (insn 6 3 7 2 (set (reg:SI 89)
143 (mem/c:SI (plus:DI (reg/f:DI 82 virtual-stack-vars)
144 (const_int -4 [0xfffffffffffffffc])) [1 i+0 S4 A32])) t.c:3 -1
145 (nil))
146 (insn 7 6 10 2 (parallel [
147 (set (reg:SI 87 [ _2 ])
148 (ashift:SI (reg:SI 89)
149 (const_int 1 [0x1])))
150 (clobber (reg:CC 17 flags))
151 ]) t.c:3 -1
152 (expr_list:REG_EQUAL (ashift:SI (mem/c:SI (plus:DI (reg/f:DI 82 virtual-stack-vars)
153 (const_int -4 [0xfffffffffffffffc])) [1 i+0 S4 A32])
154 (const_int 1 [0x1]))
155 (nil)))
156 (insn 10 7 14 2 (set (reg:SI 88 [ <retval> ])
157 (reg:SI 87 [ _2 ])) t.c:3 -1
158 (nil))
159 (insn 14 10 15 2 (set (reg/i:SI 0 ax)
160 (reg:SI 88 [ <retval> ])) t.c:4 -1
161 (nil))
162 (insn 15 14 0 2 (use (reg/i:SI 0 ax)) t.c:4 -1
163 (nil))
164 (edge-to exit (flags "FALLTHRU"))
165 ) ;; block 2
166 ) ;; insn-chain
167 (crtl
168 (return_rtx
169 (reg/i:SI 0 ax)
170 ) ;; return_rtx
171 ) ;; crtl
172 ) ;; function "times_two"
175 DEBUG_FUNCTION void
176 print_rtx_function (FILE *outfile, function *fn)
178 tree fdecl = fn->decl;
180 const char *dname = lang_hooks.decl_printable_name (fdecl, 2);
182 fprintf (outfile, "(function \"%s\"\n", dname);
184 /* The instruction chain. */
185 fprintf (outfile, " (insn-chain\n");
186 basic_block curr_bb = NULL;
187 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
189 basic_block insn_bb;
190 if (can_have_basic_block_p (insn))
191 insn_bb = BLOCK_FOR_INSN (insn);
192 else
193 insn_bb = NULL;
194 if (curr_bb != insn_bb)
196 end_any_block (outfile, curr_bb);
197 curr_bb = insn_bb;
198 begin_any_block (outfile, curr_bb);
200 print_rtl_single_with_indent (outfile, insn, curr_bb ? 6 : 4);
202 end_any_block (outfile, curr_bb);
203 fprintf (outfile, " ) ;; insn-chain\n");
205 /* Additional RTL state. */
206 fprintf (outfile, " (crtl\n");
207 fprintf (outfile, " (return_rtx \n");
208 print_rtl_single_with_indent (outfile, crtl->return_rtx, 6);
209 fprintf (outfile, " ) ;; return_rtx\n");
210 fprintf (outfile, " ) ;; crtl\n");
212 fprintf (outfile, ") ;; function \"%s\"\n", dname);