i965: Factor out push locations.
[mesa.git] / src / intel / compiler / intel_asm_annotation.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_cfg.h"
25 #include "brw_eu.h"
26 #include "common/gen_debug.h"
27 #include "intel_asm_annotation.h"
28 #include "compiler/nir/nir.h"
29
30 __attribute__((weak)) void nir_print_instr(const nir_instr *instr, FILE *fp) {}
31
32 void
33 dump_assembly(void *assembly, int num_annotations, struct annotation *annotation,
34 const struct gen_device_info *devinfo)
35 {
36 const char *last_annotation_string = NULL;
37 const void *last_annotation_ir = NULL;
38
39 for (int i = 0; i < num_annotations; i++) {
40 int start_offset = annotation[i].offset;
41 int end_offset = annotation[i + 1].offset;
42
43 if (annotation[i].block_start) {
44 fprintf(stderr, " START B%d", annotation[i].block_start->num);
45 foreach_list_typed(struct bblock_link, predecessor_link, link,
46 &annotation[i].block_start->parents) {
47 struct bblock_t *predecessor_block = predecessor_link->block;
48 fprintf(stderr, " <-B%d", predecessor_block->num);
49 }
50 fprintf(stderr, " (%u cycles)\n", annotation[i].block_start->cycle_count);
51 }
52
53 if (last_annotation_ir != annotation[i].ir) {
54 last_annotation_ir = annotation[i].ir;
55 if (last_annotation_ir) {
56 fprintf(stderr, " ");
57 nir_print_instr(annotation[i].ir, stderr);
58 fprintf(stderr, "\n");
59 }
60 }
61
62 if (last_annotation_string != annotation[i].annotation) {
63 last_annotation_string = annotation[i].annotation;
64 if (last_annotation_string)
65 fprintf(stderr, " %s\n", last_annotation_string);
66 }
67
68 brw_disassemble(devinfo, assembly, start_offset, end_offset, stderr);
69
70 if (annotation[i].error) {
71 fputs(annotation[i].error, stderr);
72 }
73
74 if (annotation[i].block_end) {
75 fprintf(stderr, " END B%d", annotation[i].block_end->num);
76 foreach_list_typed(struct bblock_link, successor_link, link,
77 &annotation[i].block_end->children) {
78 struct bblock_t *successor_block = successor_link->block;
79 fprintf(stderr, " ->B%d", successor_block->num);
80 }
81 fprintf(stderr, "\n");
82 }
83 }
84 fprintf(stderr, "\n");
85 }
86
87 static bool
88 annotation_array_ensure_space(struct annotation_info *annotation)
89 {
90 if (annotation->ann_size <= annotation->ann_count) {
91 int old_size = annotation->ann_size;
92 annotation->ann_size = MAX2(1024, annotation->ann_size * 2);
93 annotation->ann = reralloc(annotation->mem_ctx, annotation->ann,
94 struct annotation, annotation->ann_size);
95 if (!annotation->ann)
96 return false;
97
98 memset(annotation->ann + old_size, 0,
99 (annotation->ann_size - old_size) * sizeof(struct annotation));
100 }
101
102 return true;
103 }
104
105 void annotate(const struct gen_device_info *devinfo,
106 struct annotation_info *annotation, const struct cfg_t *cfg,
107 struct backend_instruction *inst, unsigned offset)
108 {
109 if (annotation->mem_ctx == NULL)
110 annotation->mem_ctx = ralloc_context(NULL);
111
112 if (!annotation_array_ensure_space(annotation))
113 return;
114
115 struct annotation *ann = &annotation->ann[annotation->ann_count++];
116 ann->offset = offset;
117 if ((INTEL_DEBUG & DEBUG_ANNOTATION) != 0) {
118 ann->ir = inst->ir;
119 ann->annotation = inst->annotation;
120 }
121
122 if (bblock_start(cfg->blocks[annotation->cur_block]) == inst) {
123 ann->block_start = cfg->blocks[annotation->cur_block];
124 }
125
126 /* There is no hardware DO instruction on Gen6+, so since DO always
127 * starts a basic block, we need to set the .block_start of the next
128 * instruction's annotation with a pointer to the bblock started by
129 * the DO.
130 *
131 * There's also only complication from emitting an annotation without
132 * a corresponding hardware instruction to disassemble.
133 */
134 if (devinfo->gen >= 6 && inst->opcode == BRW_OPCODE_DO) {
135 annotation->ann_count--;
136 }
137
138 if (bblock_end(cfg->blocks[annotation->cur_block]) == inst) {
139 ann->block_end = cfg->blocks[annotation->cur_block];
140 annotation->cur_block++;
141 }
142 }
143
144 void
145 annotation_finalize(struct annotation_info *annotation,
146 unsigned next_inst_offset)
147 {
148 if (!annotation->ann_count)
149 return;
150
151 if (annotation->ann_count == annotation->ann_size) {
152 annotation->ann = reralloc(annotation->mem_ctx, annotation->ann,
153 struct annotation, annotation->ann_size + 1);
154 }
155 annotation->ann[annotation->ann_count].offset = next_inst_offset;
156 }
157
158 void
159 annotation_insert_error(struct annotation_info *annotation, unsigned offset,
160 const char *error)
161 {
162 struct annotation *ann;
163
164 if (!annotation->ann_count)
165 return;
166
167 /* We may have to split an annotation, so ensure we have enough space
168 * allocated for that case up front.
169 */
170 if (!annotation_array_ensure_space(annotation))
171 return;
172
173 assume(annotation->ann_count > 0);
174
175 for (int i = 0; i < annotation->ann_count; i++) {
176 struct annotation *cur = &annotation->ann[i];
177 struct annotation *next = &annotation->ann[i + 1];
178 ann = cur;
179
180 if (next->offset <= offset)
181 continue;
182
183 if (offset + sizeof(brw_inst) != next->offset) {
184 memmove(next, cur,
185 (annotation->ann_count - i + 2) * sizeof(struct annotation));
186 cur->error = NULL;
187 cur->error_length = 0;
188 cur->block_end = NULL;
189 next->offset = offset + sizeof(brw_inst);
190 next->block_start = NULL;
191 annotation->ann_count++;
192 }
193 break;
194 }
195
196 if (ann->error)
197 ralloc_strcat(&ann->error, error);
198 else
199 ann->error = ralloc_strdup(annotation->mem_ctx, error);
200 }