1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * All Rights Reserved.
5  * Copyright 2008 VMware, Inc.  All rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 
29 /**
30  * TGSI program scan utility.
31  * Used to determine which registers and instructions are used by a shader.
32  *
33  * Authors:  Brian Paul
34  */
35 
36 
37 #include "util/u_debug.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_prim.h"
41 #include "tgsi/tgsi_info.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_util.h"
44 #include "tgsi/tgsi_scan.h"
45 
46 
47 static bool
is_memory_file(unsigned file)48 is_memory_file(unsigned file)
49 {
50    return file == TGSI_FILE_SAMPLER ||
51           file == TGSI_FILE_SAMPLER_VIEW ||
52           file == TGSI_FILE_IMAGE ||
53           file == TGSI_FILE_BUFFER ||
54           file == TGSI_FILE_HW_ATOMIC;
55 }
56 
57 
58 static bool
is_mem_query_inst(enum tgsi_opcode opcode)59 is_mem_query_inst(enum tgsi_opcode opcode)
60 {
61    return opcode == TGSI_OPCODE_RESQ ||
62           opcode == TGSI_OPCODE_TXQ ||
63           opcode == TGSI_OPCODE_TXQS ||
64           opcode == TGSI_OPCODE_LODQ;
65 }
66 
67 /**
68  * Is the opcode a "true" texture instruction which samples from a
69  * texture map?
70  */
71 static bool
is_texture_inst(enum tgsi_opcode opcode)72 is_texture_inst(enum tgsi_opcode opcode)
73 {
74    return (!is_mem_query_inst(opcode) &&
75            tgsi_get_opcode_info(opcode)->is_tex);
76 }
77 
78 
79 /**
80  * Is the opcode an instruction which computes a derivative explicitly or
81  * implicitly?
82  */
83 static bool
computes_derivative(enum tgsi_opcode opcode)84 computes_derivative(enum tgsi_opcode opcode)
85 {
86    if (tgsi_get_opcode_info(opcode)->is_tex) {
87       return opcode != TGSI_OPCODE_TG4 &&
88              opcode != TGSI_OPCODE_TXD &&
89              opcode != TGSI_OPCODE_TXF &&
90              opcode != TGSI_OPCODE_TXF_LZ &&
91              opcode != TGSI_OPCODE_TEX_LZ &&
92              opcode != TGSI_OPCODE_TXL &&
93              opcode != TGSI_OPCODE_TXL2 &&
94              opcode != TGSI_OPCODE_TXQ &&
95              opcode != TGSI_OPCODE_TXQS;
96    }
97 
98    return opcode == TGSI_OPCODE_DDX || opcode == TGSI_OPCODE_DDX_FINE ||
99           opcode == TGSI_OPCODE_DDY || opcode == TGSI_OPCODE_DDY_FINE ||
100           opcode == TGSI_OPCODE_SAMPLE ||
101           opcode == TGSI_OPCODE_SAMPLE_B ||
102           opcode == TGSI_OPCODE_SAMPLE_C;
103 }
104 
105 
106 static void
scan_src_operand(struct tgsi_shader_info * info,const struct tgsi_full_instruction * fullinst,const struct tgsi_full_src_register * src,unsigned src_index,unsigned usage_mask_after_swizzle,bool is_interp_instruction,bool * is_mem_inst)107 scan_src_operand(struct tgsi_shader_info *info,
108                  const struct tgsi_full_instruction *fullinst,
109                  const struct tgsi_full_src_register *src,
110                  unsigned src_index,
111                  unsigned usage_mask_after_swizzle,
112                  bool is_interp_instruction,
113                  bool *is_mem_inst)
114 {
115    int ind = src->Register.Index;
116 
117    if (info->processor == PIPE_SHADER_COMPUTE &&
118        src->Register.File == TGSI_FILE_SYSTEM_VALUE) {
119       unsigned name, mask;
120 
121       name = info->system_value_semantic_name[src->Register.Index];
122 
123       switch (name) {
124       case TGSI_SEMANTIC_THREAD_ID:
125       case TGSI_SEMANTIC_BLOCK_ID:
126          mask = usage_mask_after_swizzle & TGSI_WRITEMASK_XYZ;
127          while (mask) {
128             unsigned i = u_bit_scan(&mask);
129 
130             if (name == TGSI_SEMANTIC_THREAD_ID)
131                info->uses_thread_id[i] = true;
132             else
133                info->uses_block_id[i] = true;
134          }
135          break;
136       case TGSI_SEMANTIC_BLOCK_SIZE:
137          /* The block size is translated to IMM with a fixed block size. */
138          if (info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] == 0)
139             info->uses_block_size = true;
140          break;
141       case TGSI_SEMANTIC_GRID_SIZE:
142          info->uses_grid_size = true;
143          break;
144       }
145    }
146 
147    /* Mark which inputs are effectively used */
148    if (src->Register.File == TGSI_FILE_INPUT) {
149       if (src->Register.Indirect) {
150          for (ind = 0; ind < info->num_inputs; ++ind) {
151             info->input_usage_mask[ind] |= usage_mask_after_swizzle;
152          }
153       } else {
154          assert(ind >= 0);
155          assert(ind < PIPE_MAX_SHADER_INPUTS);
156          info->input_usage_mask[ind] |= usage_mask_after_swizzle;
157       }
158 
159       if (info->processor == PIPE_SHADER_FRAGMENT) {
160          unsigned name, index, input;
161 
162          if (src->Register.Indirect && src->Indirect.ArrayID)
163             input = info->input_array_first[src->Indirect.ArrayID];
164          else
165             input = src->Register.Index;
166 
167          name = info->input_semantic_name[input];
168          index = info->input_semantic_index[input];
169 
170          if (name == TGSI_SEMANTIC_POSITION &&
171              usage_mask_after_swizzle & TGSI_WRITEMASK_Z)
172             info->reads_z = true;
173 
174          if (name == TGSI_SEMANTIC_COLOR)
175             info->colors_read |= usage_mask_after_swizzle << (index * 4);
176 
177          /* Process only interpolated varyings. Don't include POSITION.
178           * Don't include integer varyings, because they are not
179           * interpolated. Don't process inputs interpolated by INTERP
180           * opcodes. Those are tracked separately.
181           */
182          if ((!is_interp_instruction || src_index != 0) &&
183              (name == TGSI_SEMANTIC_GENERIC ||
184               name == TGSI_SEMANTIC_TEXCOORD ||
185               name == TGSI_SEMANTIC_COLOR ||
186               name == TGSI_SEMANTIC_BCOLOR ||
187               name == TGSI_SEMANTIC_FOG ||
188               name == TGSI_SEMANTIC_CLIPDIST)) {
189             switch (info->input_interpolate[input]) {
190             case TGSI_INTERPOLATE_COLOR:
191             case TGSI_INTERPOLATE_PERSPECTIVE:
192                switch (info->input_interpolate_loc[input]) {
193                case TGSI_INTERPOLATE_LOC_CENTER:
194                   info->uses_persp_center = TRUE;
195                   break;
196                case TGSI_INTERPOLATE_LOC_CENTROID:
197                   info->uses_persp_centroid = TRUE;
198                   break;
199                case TGSI_INTERPOLATE_LOC_SAMPLE:
200                   info->uses_persp_sample = TRUE;
201                   break;
202                }
203                break;
204             case TGSI_INTERPOLATE_LINEAR:
205                switch (info->input_interpolate_loc[input]) {
206                case TGSI_INTERPOLATE_LOC_CENTER:
207                   info->uses_linear_center = TRUE;
208                   break;
209                case TGSI_INTERPOLATE_LOC_CENTROID:
210                   info->uses_linear_centroid = TRUE;
211                   break;
212                case TGSI_INTERPOLATE_LOC_SAMPLE:
213                   info->uses_linear_sample = TRUE;
214                   break;
215                }
216                break;
217                /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
218             }
219          }
220       }
221    }
222 
223    if (info->processor == PIPE_SHADER_TESS_CTRL &&
224        src->Register.File == TGSI_FILE_OUTPUT) {
225       unsigned input;
226 
227       if (src->Register.Indirect && src->Indirect.ArrayID)
228          input = info->output_array_first[src->Indirect.ArrayID];
229       else
230          input = src->Register.Index;
231 
232       switch (info->output_semantic_name[input]) {
233       case TGSI_SEMANTIC_PATCH:
234          info->reads_perpatch_outputs = true;
235          break;
236       case TGSI_SEMANTIC_TESSINNER:
237       case TGSI_SEMANTIC_TESSOUTER:
238          info->reads_tessfactor_outputs = true;
239          break;
240       default:
241          info->reads_pervertex_outputs = true;
242       }
243    }
244 
245    /* check for indirect register reads */
246    if (src->Register.Indirect) {
247       info->indirect_files |= (1 << src->Register.File);
248       info->indirect_files_read |= (1 << src->Register.File);
249 
250       /* record indirect constant buffer indexing */
251       if (src->Register.File == TGSI_FILE_CONSTANT) {
252          if (src->Register.Dimension) {
253             if (src->Dimension.Indirect)
254                info->const_buffers_indirect = info->const_buffers_declared;
255             else
256                info->const_buffers_indirect |= 1u << src->Dimension.Index;
257          } else {
258             info->const_buffers_indirect |= 1;
259          }
260       }
261    }
262 
263    if (src->Register.Dimension && src->Dimension.Indirect)
264       info->dim_indirect_files |= 1u << src->Register.File;
265 
266    /* Texture samplers */
267    if (src->Register.File == TGSI_FILE_SAMPLER) {
268       const unsigned index = src->Register.Index;
269 
270       assert(fullinst->Instruction.Texture);
271       assert(index < PIPE_MAX_SAMPLERS);
272 
273       if (is_texture_inst(fullinst->Instruction.Opcode)) {
274          const unsigned target = fullinst->Texture.Texture;
275          assert(target < TGSI_TEXTURE_UNKNOWN);
276          /* for texture instructions, check that the texture instruction
277           * target matches the previous sampler view declaration (if there
278           * was one.)
279           */
280          if (info->sampler_targets[index] == TGSI_TEXTURE_UNKNOWN) {
281             /* probably no sampler view declaration */
282             info->sampler_targets[index] = target;
283          } else {
284             /* Make sure the texture instruction's sampler/target info
285              * agrees with the sampler view declaration.
286              */
287             assert(info->sampler_targets[index] == target);
288          }
289       }
290    }
291 
292    if (is_memory_file(src->Register.File) &&
293        !is_mem_query_inst(fullinst->Instruction.Opcode)) {
294       *is_mem_inst = true;
295 
296       if (src->Register.File == TGSI_FILE_IMAGE &&
297           (fullinst->Memory.Texture == TGSI_TEXTURE_2D_MSAA ||
298            fullinst->Memory.Texture == TGSI_TEXTURE_2D_ARRAY_MSAA)) {
299          if (src->Register.Indirect)
300             info->msaa_images_declared = info->images_declared;
301          else
302             info->msaa_images_declared |= 1 << src->Register.Index;
303       }
304 
305       if (tgsi_get_opcode_info(fullinst->Instruction.Opcode)->is_store) {
306          info->writes_memory = TRUE;
307 
308          if (src->Register.File == TGSI_FILE_IMAGE) {
309             if (src->Register.Indirect)
310                info->images_atomic = info->images_declared;
311             else
312                info->images_atomic |= 1 << src->Register.Index;
313          } else if (src->Register.File == TGSI_FILE_BUFFER) {
314             if (src->Register.Indirect)
315                info->shader_buffers_atomic = info->shader_buffers_declared;
316             else
317                info->shader_buffers_atomic |= 1 << src->Register.Index;
318          }
319       } else {
320          if (src->Register.File == TGSI_FILE_IMAGE) {
321             if (src->Register.Indirect)
322                info->images_load = info->images_declared;
323             else
324                info->images_load |= 1 << src->Register.Index;
325          } else if (src->Register.File == TGSI_FILE_BUFFER) {
326             if (src->Register.Indirect)
327                info->shader_buffers_load = info->shader_buffers_declared;
328             else
329                info->shader_buffers_load |= 1 << src->Register.Index;
330          }
331       }
332    }
333 }
334 
335 
336 static void
scan_instruction(struct tgsi_shader_info * info,const struct tgsi_full_instruction * fullinst,unsigned * current_depth)337 scan_instruction(struct tgsi_shader_info *info,
338                  const struct tgsi_full_instruction *fullinst,
339                  unsigned *current_depth)
340 {
341    unsigned i;
342    bool is_mem_inst = false;
343    bool is_interp_instruction = false;
344    unsigned sampler_src;
345 
346    assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
347    info->opcode_count[fullinst->Instruction.Opcode]++;
348 
349    switch (fullinst->Instruction.Opcode) {
350    case TGSI_OPCODE_IF:
351    case TGSI_OPCODE_UIF:
352    case TGSI_OPCODE_BGNLOOP:
353       (*current_depth)++;
354       info->max_depth = MAX2(info->max_depth, *current_depth);
355       break;
356    case TGSI_OPCODE_ENDIF:
357    case TGSI_OPCODE_ENDLOOP:
358       (*current_depth)--;
359       break;
360    case TGSI_OPCODE_TEX:
361    case TGSI_OPCODE_TEX_LZ:
362    case TGSI_OPCODE_TXB:
363    case TGSI_OPCODE_TXD:
364    case TGSI_OPCODE_TXL:
365    case TGSI_OPCODE_TXP:
366    case TGSI_OPCODE_TXQ:
367    case TGSI_OPCODE_TXQS:
368    case TGSI_OPCODE_TXF:
369    case TGSI_OPCODE_TXF_LZ:
370    case TGSI_OPCODE_TEX2:
371    case TGSI_OPCODE_TXB2:
372    case TGSI_OPCODE_TXL2:
373    case TGSI_OPCODE_TG4:
374    case TGSI_OPCODE_LODQ:
375       sampler_src = fullinst->Instruction.NumSrcRegs - 1;
376       if (fullinst->Src[sampler_src].Register.File != TGSI_FILE_SAMPLER)
377          info->uses_bindless_samplers = true;
378       break;
379    case TGSI_OPCODE_RESQ:
380       if (tgsi_is_bindless_image_file(fullinst->Src[0].Register.File))
381          info->uses_bindless_images = true;
382       break;
383    case TGSI_OPCODE_LOAD:
384       if (tgsi_is_bindless_image_file(fullinst->Src[0].Register.File)) {
385          info->uses_bindless_images = true;
386 
387          if (fullinst->Memory.Texture == TGSI_TEXTURE_BUFFER)
388             info->uses_bindless_buffer_load = true;
389          else
390             info->uses_bindless_image_load = true;
391       }
392       break;
393    case TGSI_OPCODE_ATOMUADD:
394    case TGSI_OPCODE_ATOMXCHG:
395    case TGSI_OPCODE_ATOMCAS:
396    case TGSI_OPCODE_ATOMAND:
397    case TGSI_OPCODE_ATOMOR:
398    case TGSI_OPCODE_ATOMXOR:
399    case TGSI_OPCODE_ATOMUMIN:
400    case TGSI_OPCODE_ATOMUMAX:
401    case TGSI_OPCODE_ATOMIMIN:
402    case TGSI_OPCODE_ATOMIMAX:
403    case TGSI_OPCODE_ATOMFADD:
404    case TGSI_OPCODE_ATOMINC_WRAP:
405    case TGSI_OPCODE_ATOMDEC_WRAP:
406       if (tgsi_is_bindless_image_file(fullinst->Src[0].Register.File)) {
407          info->uses_bindless_images = true;
408 
409          if (fullinst->Memory.Texture == TGSI_TEXTURE_BUFFER)
410             info->uses_bindless_buffer_atomic = true;
411          else
412             info->uses_bindless_image_atomic = true;
413       }
414       break;
415    case TGSI_OPCODE_STORE:
416       if (tgsi_is_bindless_image_file(fullinst->Dst[0].Register.File)) {
417          info->uses_bindless_images = true;
418 
419          if (fullinst->Memory.Texture == TGSI_TEXTURE_BUFFER)
420             info->uses_bindless_buffer_store = true;
421          else
422             info->uses_bindless_image_store = true;
423       }
424       break;
425    case TGSI_OPCODE_FBFETCH:
426       info->uses_fbfetch = true;
427       break;
428    default:
429       break;
430    }
431 
432    if (fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID ||
433        fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
434        fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
435       const struct tgsi_full_src_register *src0 = &fullinst->Src[0];
436       unsigned input;
437 
438       is_interp_instruction = true;
439 
440       if (src0->Register.Indirect && src0->Indirect.ArrayID)
441          input = info->input_array_first[src0->Indirect.ArrayID];
442       else
443          input = src0->Register.Index;
444 
445       /* For the INTERP opcodes, the interpolation is always
446        * PERSPECTIVE unless LINEAR is specified.
447        */
448       switch (info->input_interpolate[input]) {
449       case TGSI_INTERPOLATE_COLOR:
450       case TGSI_INTERPOLATE_CONSTANT:
451       case TGSI_INTERPOLATE_PERSPECTIVE:
452          switch (fullinst->Instruction.Opcode) {
453          case TGSI_OPCODE_INTERP_CENTROID:
454             info->uses_persp_opcode_interp_centroid = TRUE;
455             break;
456          case TGSI_OPCODE_INTERP_OFFSET:
457             info->uses_persp_opcode_interp_offset = TRUE;
458             break;
459          case TGSI_OPCODE_INTERP_SAMPLE:
460             info->uses_persp_opcode_interp_sample = TRUE;
461             break;
462          }
463          break;
464 
465       case TGSI_INTERPOLATE_LINEAR:
466          switch (fullinst->Instruction.Opcode) {
467          case TGSI_OPCODE_INTERP_CENTROID:
468             info->uses_linear_opcode_interp_centroid = TRUE;
469             break;
470          case TGSI_OPCODE_INTERP_OFFSET:
471             info->uses_linear_opcode_interp_offset = TRUE;
472             break;
473          case TGSI_OPCODE_INTERP_SAMPLE:
474             info->uses_linear_opcode_interp_sample = TRUE;
475             break;
476          }
477          break;
478       }
479    }
480 
481    if ((fullinst->Instruction.Opcode >= TGSI_OPCODE_F2D &&
482         fullinst->Instruction.Opcode <= TGSI_OPCODE_DSSG) ||
483        fullinst->Instruction.Opcode == TGSI_OPCODE_DFMA ||
484        fullinst->Instruction.Opcode == TGSI_OPCODE_DDIV ||
485        fullinst->Instruction.Opcode == TGSI_OPCODE_D2U64 ||
486        fullinst->Instruction.Opcode == TGSI_OPCODE_D2I64 ||
487        fullinst->Instruction.Opcode == TGSI_OPCODE_U642D ||
488        fullinst->Instruction.Opcode == TGSI_OPCODE_I642D)
489       info->uses_doubles = TRUE;
490 
491    for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
492       scan_src_operand(info, fullinst, &fullinst->Src[i], i,
493                        tgsi_util_get_inst_usage_mask(fullinst, i),
494                        is_interp_instruction, &is_mem_inst);
495 
496       if (fullinst->Src[i].Register.Indirect) {
497          struct tgsi_full_src_register src = {{0}};
498 
499          src.Register.File = fullinst->Src[i].Indirect.File;
500          src.Register.Index = fullinst->Src[i].Indirect.Index;
501 
502          scan_src_operand(info, fullinst, &src, -1,
503                           1 << fullinst->Src[i].Indirect.Swizzle,
504                           false, NULL);
505       }
506 
507       if (fullinst->Src[i].Register.Dimension &&
508           fullinst->Src[i].Dimension.Indirect) {
509          struct tgsi_full_src_register src = {{0}};
510 
511          src.Register.File = fullinst->Src[i].DimIndirect.File;
512          src.Register.Index = fullinst->Src[i].DimIndirect.Index;
513 
514          scan_src_operand(info, fullinst, &src, -1,
515                           1 << fullinst->Src[i].DimIndirect.Swizzle,
516                           false, NULL);
517       }
518    }
519 
520    if (fullinst->Instruction.Texture) {
521       for (i = 0; i < fullinst->Texture.NumOffsets; i++) {
522          struct tgsi_full_src_register src = {{0}};
523 
524          src.Register.File = fullinst->TexOffsets[i].File;
525          src.Register.Index = fullinst->TexOffsets[i].Index;
526 
527          /* The usage mask is suboptimal but should be safe. */
528          scan_src_operand(info, fullinst, &src, -1,
529                           (1 << fullinst->TexOffsets[i].SwizzleX) |
530                           (1 << fullinst->TexOffsets[i].SwizzleY) |
531                           (1 << fullinst->TexOffsets[i].SwizzleZ),
532                           false, &is_mem_inst);
533       }
534    }
535 
536    /* check for indirect register writes */
537    for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
538       const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
539 
540       if (dst->Register.Indirect) {
541          struct tgsi_full_src_register src = {{0}};
542 
543          src.Register.File = dst->Indirect.File;
544          src.Register.Index = dst->Indirect.Index;
545 
546          scan_src_operand(info, fullinst, &src, -1,
547                           1 << dst->Indirect.Swizzle, false, NULL);
548 
549          info->indirect_files |= (1 << dst->Register.File);
550          info->indirect_files_written |= (1 << dst->Register.File);
551       }
552 
553       if (dst->Register.Dimension && dst->Dimension.Indirect) {
554          struct tgsi_full_src_register src = {{0}};
555 
556          src.Register.File = dst->DimIndirect.File;
557          src.Register.Index = dst->DimIndirect.Index;
558 
559          scan_src_operand(info, fullinst, &src, -1,
560                           1 << dst->DimIndirect.Swizzle, false, NULL);
561 
562          info->dim_indirect_files |= 1u << dst->Register.File;
563       }
564 
565       if (is_memory_file(dst->Register.File)) {
566          assert(fullinst->Instruction.Opcode == TGSI_OPCODE_STORE);
567 
568          is_mem_inst = true;
569          info->writes_memory = TRUE;
570 
571          if (dst->Register.File == TGSI_FILE_IMAGE) {
572             if (fullinst->Memory.Texture == TGSI_TEXTURE_2D_MSAA ||
573                 fullinst->Memory.Texture == TGSI_TEXTURE_2D_ARRAY_MSAA) {
574                if (dst->Register.Indirect)
575                   info->msaa_images_declared = info->images_declared;
576                else
577                   info->msaa_images_declared |= 1 << dst->Register.Index;
578             }
579 
580             if (dst->Register.Indirect)
581                info->images_store = info->images_declared;
582             else
583                info->images_store |= 1 << dst->Register.Index;
584          } else if (dst->Register.File == TGSI_FILE_BUFFER) {
585             if (dst->Register.Indirect)
586                info->shader_buffers_store = info->shader_buffers_declared;
587             else
588                info->shader_buffers_store |= 1 << dst->Register.Index;
589          }
590       }
591    }
592 
593    if (is_mem_inst)
594       info->num_memory_instructions++;
595 
596    if (computes_derivative(fullinst->Instruction.Opcode))
597       info->uses_derivatives = true;
598 
599    info->num_instructions++;
600 }
601 
602 
603 static void
scan_declaration(struct tgsi_shader_info * info,const struct tgsi_full_declaration * fulldecl)604 scan_declaration(struct tgsi_shader_info *info,
605                  const struct tgsi_full_declaration *fulldecl)
606 {
607    const uint file = fulldecl->Declaration.File;
608    const unsigned procType = info->processor;
609    uint reg;
610 
611    if (fulldecl->Declaration.Array) {
612       unsigned array_id = fulldecl->Array.ArrayID;
613 
614       switch (file) {
615       case TGSI_FILE_INPUT:
616          assert(array_id < ARRAY_SIZE(info->input_array_first));
617          info->input_array_first[array_id] = fulldecl->Range.First;
618          info->input_array_last[array_id] = fulldecl->Range.Last;
619          break;
620       case TGSI_FILE_OUTPUT:
621          assert(array_id < ARRAY_SIZE(info->output_array_first));
622          info->output_array_first[array_id] = fulldecl->Range.First;
623          info->output_array_last[array_id] = fulldecl->Range.Last;
624          break;
625       }
626       info->array_max[file] = MAX2(info->array_max[file], array_id);
627    }
628 
629    for (reg = fulldecl->Range.First; reg <= fulldecl->Range.Last; reg++) {
630       unsigned semName = fulldecl->Semantic.Name;
631       unsigned semIndex = fulldecl->Semantic.Index +
632          (reg - fulldecl->Range.First);
633       int buffer;
634       unsigned index, target, type;
635 
636       /*
637        * only first 32 regs will appear in this bitfield, if larger
638        * bits will wrap around.
639        */
640       info->file_mask[file] |= (1u << (reg & 31));
641       info->file_count[file]++;
642       info->file_max[file] = MAX2(info->file_max[file], (int)reg);
643 
644       switch (file) {
645       case TGSI_FILE_CONSTANT:
646          buffer = 0;
647 
648          if (fulldecl->Declaration.Dimension)
649             buffer = fulldecl->Dim.Index2D;
650 
651          info->const_file_max[buffer] =
652             MAX2(info->const_file_max[buffer], (int)reg);
653          info->const_buffers_declared |= 1u << buffer;
654          break;
655 
656       case TGSI_FILE_IMAGE:
657          info->images_declared |= 1u << reg;
658          if (fulldecl->Image.Resource == TGSI_TEXTURE_BUFFER)
659             info->images_buffers |= 1 << reg;
660          break;
661 
662       case TGSI_FILE_BUFFER:
663          info->shader_buffers_declared |= 1u << reg;
664          break;
665 
666       case TGSI_FILE_HW_ATOMIC:
667          info->hw_atomic_declared |= 1u << reg;
668          break;
669 
670       case TGSI_FILE_INPUT:
671          info->input_semantic_name[reg] = (ubyte) semName;
672          info->input_semantic_index[reg] = (ubyte) semIndex;
673          info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
674          info->input_interpolate_loc[reg] = (ubyte)fulldecl->Interp.Location;
675 
676          /* Vertex shaders can have inputs with holes between them. */
677          info->num_inputs = MAX2(info->num_inputs, reg + 1);
678 
679          switch (semName) {
680          case TGSI_SEMANTIC_PRIMID:
681             info->uses_primid = true;
682             break;
683          case TGSI_SEMANTIC_POSITION:
684             info->reads_position = true;
685             break;
686          case TGSI_SEMANTIC_FACE:
687             info->uses_frontface = true;
688             break;
689          }
690          break;
691 
692       case TGSI_FILE_SYSTEM_VALUE:
693          index = fulldecl->Range.First;
694 
695          info->system_value_semantic_name[index] = semName;
696          info->num_system_values = MAX2(info->num_system_values, index + 1);
697 
698          switch (semName) {
699          case TGSI_SEMANTIC_INSTANCEID:
700             info->uses_instanceid = TRUE;
701             break;
702          case TGSI_SEMANTIC_VERTEXID:
703             info->uses_vertexid = TRUE;
704             break;
705          case TGSI_SEMANTIC_VERTEXID_NOBASE:
706             info->uses_vertexid_nobase = TRUE;
707             break;
708          case TGSI_SEMANTIC_BASEVERTEX:
709             info->uses_basevertex = TRUE;
710             break;
711          case TGSI_SEMANTIC_DRAWID:
712             info->uses_drawid = TRUE;
713             break;
714          case TGSI_SEMANTIC_PRIMID:
715             info->uses_primid = TRUE;
716             break;
717          case TGSI_SEMANTIC_INVOCATIONID:
718             info->uses_invocationid = TRUE;
719             break;
720          case TGSI_SEMANTIC_POSITION:
721             info->reads_position = TRUE;
722             break;
723          case TGSI_SEMANTIC_FACE:
724             info->uses_frontface = TRUE;
725             break;
726          case TGSI_SEMANTIC_SAMPLEMASK:
727             info->reads_samplemask = TRUE;
728             break;
729          case TGSI_SEMANTIC_TESSINNER:
730          case TGSI_SEMANTIC_TESSOUTER:
731             info->reads_tess_factors = true;
732             break;
733          }
734          break;
735 
736       case TGSI_FILE_OUTPUT:
737          info->output_semantic_name[reg] = (ubyte) semName;
738          info->output_semantic_index[reg] = (ubyte) semIndex;
739          info->output_usagemask[reg] |= fulldecl->Declaration.UsageMask;
740          info->num_outputs = MAX2(info->num_outputs, reg + 1);
741 
742          if (fulldecl->Declaration.UsageMask & TGSI_WRITEMASK_X) {
743             info->output_streams[reg] |= (ubyte)fulldecl->Semantic.StreamX;
744             info->num_stream_output_components[fulldecl->Semantic.StreamX]++;
745          }
746          if (fulldecl->Declaration.UsageMask & TGSI_WRITEMASK_Y) {
747             info->output_streams[reg] |= (ubyte)fulldecl->Semantic.StreamY << 2;
748             info->num_stream_output_components[fulldecl->Semantic.StreamY]++;
749          }
750          if (fulldecl->Declaration.UsageMask & TGSI_WRITEMASK_Z) {
751             info->output_streams[reg] |= (ubyte)fulldecl->Semantic.StreamZ << 4;
752             info->num_stream_output_components[fulldecl->Semantic.StreamZ]++;
753          }
754          if (fulldecl->Declaration.UsageMask & TGSI_WRITEMASK_W) {
755             info->output_streams[reg] |= (ubyte)fulldecl->Semantic.StreamW << 6;
756             info->num_stream_output_components[fulldecl->Semantic.StreamW]++;
757          }
758 
759          switch (semName) {
760          case TGSI_SEMANTIC_PRIMID:
761             info->writes_primid = true;
762             break;
763          case TGSI_SEMANTIC_VIEWPORT_INDEX:
764             info->writes_viewport_index = true;
765             break;
766          case TGSI_SEMANTIC_LAYER:
767             info->writes_layer = true;
768             break;
769          case TGSI_SEMANTIC_PSIZE:
770             info->writes_psize = true;
771             break;
772          case TGSI_SEMANTIC_CLIPVERTEX:
773             info->writes_clipvertex = true;
774             break;
775          case TGSI_SEMANTIC_COLOR:
776             info->colors_written |= 1 << semIndex;
777             break;
778          case TGSI_SEMANTIC_STENCIL:
779             info->writes_stencil = true;
780             break;
781          case TGSI_SEMANTIC_SAMPLEMASK:
782             info->writes_samplemask = true;
783             break;
784          case TGSI_SEMANTIC_EDGEFLAG:
785             info->writes_edgeflag = true;
786             break;
787          case TGSI_SEMANTIC_POSITION:
788             if (procType == PIPE_SHADER_FRAGMENT)
789                info->writes_z = true;
790             else
791                info->writes_position = true;
792             break;
793          }
794          break;
795 
796       case TGSI_FILE_SAMPLER:
797          STATIC_ASSERT(sizeof(info->samplers_declared) * 8 >= PIPE_MAX_SAMPLERS);
798          info->samplers_declared |= 1u << reg;
799          break;
800 
801       case TGSI_FILE_SAMPLER_VIEW:
802          target = fulldecl->SamplerView.Resource;
803          type = fulldecl->SamplerView.ReturnTypeX;
804 
805          assert(target < TGSI_TEXTURE_UNKNOWN);
806          if (info->sampler_targets[reg] == TGSI_TEXTURE_UNKNOWN) {
807             /* Save sampler target for this sampler index */
808             info->sampler_targets[reg] = target;
809             info->sampler_type[reg] = type;
810          } else {
811             /* if previously declared, make sure targets agree */
812             assert(info->sampler_targets[reg] == target);
813             assert(info->sampler_type[reg] == type);
814          }
815          break;
816       }
817    }
818 }
819 
820 
821 static void
scan_immediate(struct tgsi_shader_info * info)822 scan_immediate(struct tgsi_shader_info *info)
823 {
824    uint reg = info->immediate_count++;
825    uint file = TGSI_FILE_IMMEDIATE;
826 
827    info->file_mask[file] |= (1 << reg);
828    info->file_count[file]++;
829    info->file_max[file] = MAX2(info->file_max[file], (int)reg);
830 }
831 
832 
833 static void
scan_property(struct tgsi_shader_info * info,const struct tgsi_full_property * fullprop)834 scan_property(struct tgsi_shader_info *info,
835               const struct tgsi_full_property *fullprop)
836 {
837    unsigned name = fullprop->Property.PropertyName;
838    unsigned value = fullprop->u[0].Data;
839 
840    assert(name < ARRAY_SIZE(info->properties));
841    info->properties[name] = value;
842 
843    switch (name) {
844    case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
845       info->num_written_clipdistance = value;
846       info->clipdist_writemask |= (1 << value) - 1;
847       break;
848    case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
849       info->num_written_culldistance = value;
850       info->culldist_writemask |= (1 << value) - 1;
851       break;
852    }
853 }
854 
855 
856 /**
857  * Scan the given TGSI shader to collect information such as number of
858  * registers used, special instructions used, etc.
859  * \return info  the result of the scan
860  */
861 void
tgsi_scan_shader(const struct tgsi_token * tokens,struct tgsi_shader_info * info)862 tgsi_scan_shader(const struct tgsi_token *tokens,
863                  struct tgsi_shader_info *info)
864 {
865    uint procType, i;
866    struct tgsi_parse_context parse;
867    unsigned current_depth = 0;
868 
869    memset(info, 0, sizeof(*info));
870    for (i = 0; i < TGSI_FILE_COUNT; i++)
871       info->file_max[i] = -1;
872    for (i = 0; i < ARRAY_SIZE(info->const_file_max); i++)
873       info->const_file_max[i] = -1;
874    for (i = 0; i < ARRAY_SIZE(info->sampler_targets); i++)
875       info->sampler_targets[i] = TGSI_TEXTURE_UNKNOWN;
876 
877    /**
878     ** Setup to begin parsing input shader
879     **/
880    if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
881       debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
882       return;
883    }
884    procType = parse.FullHeader.Processor.Processor;
885    assert(procType == PIPE_SHADER_FRAGMENT ||
886           procType == PIPE_SHADER_VERTEX ||
887           procType == PIPE_SHADER_GEOMETRY ||
888           procType == PIPE_SHADER_TESS_CTRL ||
889           procType == PIPE_SHADER_TESS_EVAL ||
890           procType == PIPE_SHADER_COMPUTE);
891    info->processor = procType;
892    info->num_tokens = tgsi_num_tokens(parse.Tokens);
893 
894    if (procType == PIPE_SHADER_GEOMETRY)
895       info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = 1;
896 
897    /**
898     ** Loop over incoming program tokens/instructions
899     */
900    while (!tgsi_parse_end_of_tokens(&parse)) {
901       tgsi_parse_token( &parse );
902 
903       switch( parse.FullToken.Token.Type ) {
904       case TGSI_TOKEN_TYPE_INSTRUCTION:
905          scan_instruction(info, &parse.FullToken.FullInstruction,
906                           &current_depth);
907          break;
908       case TGSI_TOKEN_TYPE_DECLARATION:
909          scan_declaration(info, &parse.FullToken.FullDeclaration);
910          break;
911       case TGSI_TOKEN_TYPE_IMMEDIATE:
912          scan_immediate(info);
913          break;
914       case TGSI_TOKEN_TYPE_PROPERTY:
915          scan_property(info, &parse.FullToken.FullProperty);
916          break;
917       default:
918          assert(!"Unexpected TGSI token type");
919       }
920    }
921 
922    info->uses_kill = (info->opcode_count[TGSI_OPCODE_KILL_IF] ||
923                       info->opcode_count[TGSI_OPCODE_KILL]);
924 
925    /* The dimensions of the IN decleration in geometry shader have
926     * to be deduced from the type of the input primitive.
927     */
928    if (procType == PIPE_SHADER_GEOMETRY) {
929       unsigned input_primitive =
930             info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
931       int num_verts = u_vertices_per_prim(input_primitive);
932       int j;
933       info->file_count[TGSI_FILE_INPUT] = num_verts;
934       info->file_max[TGSI_FILE_INPUT] =
935             MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
936       for (j = 0; j < num_verts; ++j) {
937          info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
938       }
939    }
940 
941    tgsi_parse_free(&parse);
942 }
943 
944 /**
945  * Collect information about the arrays of a given register file.
946  *
947  * @param tokens TGSI shader
948  * @param file the register file to scan through
949  * @param max_array_id number of entries in @p arrays; should be equal to the
950  *                     highest array id, i.e. tgsi_shader_info::array_max[file].
951  * @param arrays info for array of each ID will be written to arrays[ID - 1].
952  */
953 void
tgsi_scan_arrays(const struct tgsi_token * tokens,unsigned file,unsigned max_array_id,struct tgsi_array_info * arrays)954 tgsi_scan_arrays(const struct tgsi_token *tokens,
955                  unsigned file,
956                  unsigned max_array_id,
957                  struct tgsi_array_info *arrays)
958 {
959    struct tgsi_parse_context parse;
960 
961    if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
962       debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
963       return;
964    }
965 
966    memset(arrays, 0, sizeof(arrays[0]) * max_array_id);
967 
968    while (!tgsi_parse_end_of_tokens(&parse)) {
969       struct tgsi_full_instruction *inst;
970 
971       tgsi_parse_token(&parse);
972 
973       if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
974          struct tgsi_full_declaration *decl = &parse.FullToken.FullDeclaration;
975 
976          if (decl->Declaration.Array && decl->Declaration.File == file &&
977              decl->Array.ArrayID > 0 && decl->Array.ArrayID <= max_array_id) {
978             struct tgsi_array_info *array = &arrays[decl->Array.ArrayID - 1];
979             assert(!array->declared);
980             array->declared = true;
981             array->range = decl->Range;
982          }
983       }
984 
985       if (parse.FullToken.Token.Type != TGSI_TOKEN_TYPE_INSTRUCTION)
986          continue;
987 
988       inst = &parse.FullToken.FullInstruction;
989       for (unsigned i = 0; i < inst->Instruction.NumDstRegs; i++) {
990          const struct tgsi_full_dst_register *dst = &inst->Dst[i];
991          if (dst->Register.File != file)
992             continue;
993 
994          if (dst->Register.Indirect) {
995             if (dst->Indirect.ArrayID > 0 &&
996                 dst->Indirect.ArrayID <= max_array_id) {
997                arrays[dst->Indirect.ArrayID - 1].writemask |= dst->Register.WriteMask;
998             } else {
999                /* Indirect writes without an ArrayID can write anywhere. */
1000                for (unsigned j = 0; j < max_array_id; ++j)
1001                   arrays[j].writemask |= dst->Register.WriteMask;
1002             }
1003          } else {
1004             /* Check whether the write falls into any of the arrays anyway. */
1005             for (unsigned j = 0; j < max_array_id; ++j) {
1006                struct tgsi_array_info *array = &arrays[j];
1007                if (array->declared &&
1008                    dst->Register.Index >= array->range.First &&
1009                    dst->Register.Index <= array->range.Last)
1010                   array->writemask |= dst->Register.WriteMask;
1011             }
1012          }
1013       }
1014    }
1015 
1016    tgsi_parse_free(&parse);
1017 
1018    return;
1019 }
1020 
1021 static void
check_no_subroutines(const struct tgsi_full_instruction * inst)1022 check_no_subroutines(const struct tgsi_full_instruction *inst)
1023 {
1024    switch (inst->Instruction.Opcode) {
1025    case TGSI_OPCODE_BGNSUB:
1026    case TGSI_OPCODE_ENDSUB:
1027    case TGSI_OPCODE_CAL:
1028       unreachable("subroutines unhandled");
1029    }
1030 }
1031 
1032 static unsigned
get_inst_tessfactor_writemask(const struct tgsi_shader_info * info,const struct tgsi_full_instruction * inst)1033 get_inst_tessfactor_writemask(const struct tgsi_shader_info *info,
1034                               const struct tgsi_full_instruction *inst)
1035 {
1036    unsigned writemask = 0;
1037 
1038    for (unsigned i = 0; i < inst->Instruction.NumDstRegs; i++) {
1039       const struct tgsi_full_dst_register *dst = &inst->Dst[i];
1040 
1041       if (dst->Register.File == TGSI_FILE_OUTPUT &&
1042           !dst->Register.Indirect) {
1043          unsigned name = info->output_semantic_name[dst->Register.Index];
1044 
1045          if (name == TGSI_SEMANTIC_TESSINNER)
1046             writemask |= dst->Register.WriteMask;
1047          else if (name == TGSI_SEMANTIC_TESSOUTER)
1048             writemask |= dst->Register.WriteMask << 4;
1049       }
1050    }
1051    return writemask;
1052 }
1053 
1054 static unsigned
get_block_tessfactor_writemask(const struct tgsi_shader_info * info,struct tgsi_parse_context * parse,unsigned end_opcode)1055 get_block_tessfactor_writemask(const struct tgsi_shader_info *info,
1056                                struct tgsi_parse_context *parse,
1057                                unsigned end_opcode)
1058 {
1059    struct tgsi_full_instruction *inst;
1060    unsigned writemask = 0;
1061 
1062    tgsi_parse_token(parse);
1063    assert(parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
1064    inst = &parse->FullToken.FullInstruction;
1065    check_no_subroutines(inst);
1066 
1067    while (inst->Instruction.Opcode != end_opcode) {
1068 
1069       /* Recursively process nested blocks. */
1070       switch (inst->Instruction.Opcode) {
1071       case TGSI_OPCODE_IF:
1072       case TGSI_OPCODE_UIF:
1073          writemask |=
1074             get_block_tessfactor_writemask(info, parse, TGSI_OPCODE_ENDIF);
1075          break;
1076 
1077       case TGSI_OPCODE_BGNLOOP:
1078          writemask |=
1079             get_block_tessfactor_writemask(info, parse, TGSI_OPCODE_ENDLOOP);
1080          break;
1081 
1082       case TGSI_OPCODE_BARRIER:
1083          unreachable("nested BARRIER is illegal");
1084          break;
1085 
1086       default:
1087          writemask |= get_inst_tessfactor_writemask(info, inst);
1088       }
1089 
1090       tgsi_parse_token(parse);
1091       assert(parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
1092       inst = &parse->FullToken.FullInstruction;
1093       check_no_subroutines(inst);
1094    }
1095 
1096    return writemask;
1097 }
1098 
1099 static void
get_if_block_tessfactor_writemask(const struct tgsi_shader_info * info,struct tgsi_parse_context * parse,unsigned * upper_block_tf_writemask,unsigned * cond_block_tf_writemask)1100 get_if_block_tessfactor_writemask(const struct tgsi_shader_info *info,
1101                                   struct tgsi_parse_context *parse,
1102                                   unsigned *upper_block_tf_writemask,
1103                                   unsigned *cond_block_tf_writemask)
1104 {
1105    struct tgsi_full_instruction *inst;
1106    unsigned then_tessfactor_writemask = 0;
1107    unsigned else_tessfactor_writemask = 0;
1108    unsigned writemask;
1109    bool is_then = true;
1110 
1111    tgsi_parse_token(parse);
1112    assert(parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
1113    inst = &parse->FullToken.FullInstruction;
1114    check_no_subroutines(inst);
1115 
1116    while (inst->Instruction.Opcode != TGSI_OPCODE_ENDIF) {
1117 
1118       switch (inst->Instruction.Opcode) {
1119       case TGSI_OPCODE_ELSE:
1120          is_then = false;
1121          break;
1122 
1123       /* Recursively process nested blocks. */
1124       case TGSI_OPCODE_IF:
1125       case TGSI_OPCODE_UIF:
1126          get_if_block_tessfactor_writemask(info, parse,
1127                                            is_then ? &then_tessfactor_writemask :
1128                                                      &else_tessfactor_writemask,
1129                                            cond_block_tf_writemask);
1130          break;
1131 
1132       case TGSI_OPCODE_BGNLOOP:
1133          *cond_block_tf_writemask |=
1134             get_block_tessfactor_writemask(info, parse, TGSI_OPCODE_ENDLOOP);
1135          break;
1136 
1137       case TGSI_OPCODE_BARRIER:
1138          unreachable("nested BARRIER is illegal");
1139          break;
1140       default:
1141          /* Process an instruction in the current block. */
1142          writemask = get_inst_tessfactor_writemask(info, inst);
1143 
1144          if (writemask) {
1145             if (is_then)
1146                then_tessfactor_writemask |= writemask;
1147             else
1148                else_tessfactor_writemask |= writemask;
1149          }
1150       }
1151 
1152       tgsi_parse_token(parse);
1153       assert(parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
1154       inst = &parse->FullToken.FullInstruction;
1155       check_no_subroutines(inst);
1156    }
1157 
1158    if (then_tessfactor_writemask || else_tessfactor_writemask) {
1159       /* If both statements write the same tess factor channels,
1160        * we can say that the upper block writes them too. */
1161       *upper_block_tf_writemask |= then_tessfactor_writemask &
1162                                    else_tessfactor_writemask;
1163       *cond_block_tf_writemask |= then_tessfactor_writemask |
1164                                   else_tessfactor_writemask;
1165    }
1166 }
1167 
1168 void
tgsi_scan_tess_ctrl(const struct tgsi_token * tokens,const struct tgsi_shader_info * info,struct tgsi_tessctrl_info * out)1169 tgsi_scan_tess_ctrl(const struct tgsi_token *tokens,
1170                     const struct tgsi_shader_info *info,
1171                     struct tgsi_tessctrl_info *out)
1172 {
1173    memset(out, 0, sizeof(*out));
1174 
1175    if (info->processor != PIPE_SHADER_TESS_CTRL)
1176       return;
1177 
1178    struct tgsi_parse_context parse;
1179    if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
1180       debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
1181       return;
1182    }
1183 
1184    /* The pass works as follows:
1185     * If all codepaths write tess factors, we can say that all invocations
1186     * define tess factors.
1187     *
1188     * Each tess factor channel is tracked separately.
1189     */
1190    unsigned main_block_tf_writemask = 0; /* if main block writes tess factors */
1191    unsigned cond_block_tf_writemask = 0; /* if cond block writes tess factors */
1192 
1193    /* Initial value = true. Here the pass will accumulate results from multiple
1194     * segments surrounded by barriers. If tess factors aren't written at all,
1195     * it's a shader bug and we don't care if this will be true.
1196     */
1197    out->tessfactors_are_def_in_all_invocs = true;
1198 
1199    while (!tgsi_parse_end_of_tokens(&parse)) {
1200       tgsi_parse_token(&parse);
1201 
1202       if (parse.FullToken.Token.Type != TGSI_TOKEN_TYPE_INSTRUCTION)
1203          continue;
1204 
1205       struct tgsi_full_instruction *inst = &parse.FullToken.FullInstruction;
1206       check_no_subroutines(inst);
1207 
1208       /* Process nested blocks. */
1209       switch (inst->Instruction.Opcode) {
1210       case TGSI_OPCODE_IF:
1211       case TGSI_OPCODE_UIF:
1212          get_if_block_tessfactor_writemask(info, &parse,
1213                                            &main_block_tf_writemask,
1214                                            &cond_block_tf_writemask);
1215          continue;
1216 
1217       case TGSI_OPCODE_BGNLOOP:
1218          cond_block_tf_writemask |=
1219             get_block_tessfactor_writemask(info, &parse, TGSI_OPCODE_ENDLOOP);
1220          continue;
1221 
1222       case TGSI_OPCODE_BARRIER:
1223          /* The following case must be prevented:
1224           *    gl_TessLevelInner = ...;
1225           *    barrier();
1226           *    if (gl_InvocationID == 1)
1227           *       gl_TessLevelInner = ...;
1228           *
1229           * If you consider disjoint code segments separated by barriers, each
1230           * such segment that writes tess factor channels should write the same
1231           * channels in all codepaths within that segment.
1232           */
1233          if (main_block_tf_writemask || cond_block_tf_writemask) {
1234             /* Accumulate the result: */
1235             out->tessfactors_are_def_in_all_invocs &=
1236                !(cond_block_tf_writemask & ~main_block_tf_writemask);
1237 
1238             /* Analyze the next code segment from scratch. */
1239             main_block_tf_writemask = 0;
1240             cond_block_tf_writemask = 0;
1241          }
1242          continue;
1243       }
1244 
1245       main_block_tf_writemask |= get_inst_tessfactor_writemask(info, inst);
1246    }
1247 
1248    /* Accumulate the result for the last code segment separated by a barrier. */
1249    if (main_block_tf_writemask || cond_block_tf_writemask) {
1250       out->tessfactors_are_def_in_all_invocs &=
1251          !(cond_block_tf_writemask & ~main_block_tf_writemask);
1252    }
1253 
1254    tgsi_parse_free(&parse);
1255 }
1256