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