• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* _nir_foreach_def() needs to be ALWAYS_INLINE so that it can inline the
2  * callback if it was declared with ALWAYS_INLINE.
3  */
4 static ALWAYS_INLINE bool
_nir_foreach_def(nir_instr * instr,nir_foreach_def_cb cb,void * state)5 _nir_foreach_def(nir_instr *instr, nir_foreach_def_cb cb, void *state)
6 {
7    switch (instr->type) {
8    case nir_instr_type_alu:
9       return cb(&nir_instr_as_alu(instr)->def, state);
10    case nir_instr_type_deref:
11       return cb(&nir_instr_as_deref(instr)->def, state);
12    case nir_instr_type_intrinsic: {
13       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
14       if (nir_intrinsic_infos[intrin->intrinsic].has_dest)
15          return cb(&intrin->def, state);
16       return true;
17    }
18    case nir_instr_type_tex:
19       return cb(&nir_instr_as_tex(instr)->def, state);
20    case nir_instr_type_phi:
21       return cb(&nir_instr_as_phi(instr)->def, state);
22    case nir_instr_type_parallel_copy: {
23       nir_foreach_parallel_copy_entry(entry, nir_instr_as_parallel_copy(instr)) {
24          if (!entry->dest_is_reg && !cb(&entry->dest.def, state))
25             return false;
26       }
27       return true;
28    }
29 
30    case nir_instr_type_load_const:
31       return cb(&nir_instr_as_load_const(instr)->def, state);
32    case nir_instr_type_undef:
33       return cb(&nir_instr_as_undef(instr)->def, state);
34 
35    case nir_instr_type_call:
36    case nir_instr_type_jump:
37       return true;
38 
39    default:
40       unreachable("Invalid instruction type");
41    }
42 }
43 
44 static ALWAYS_INLINE bool
_nir_visit_src(nir_src * src,nir_foreach_src_cb cb,void * state)45 _nir_visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
46 {
47    if (!cb(src, state))
48       return false;
49    return true;
50 }
51 
52 static inline bool
nir_foreach_def(nir_instr * instr,nir_foreach_def_cb cb,void * state)53 nir_foreach_def(nir_instr *instr, nir_foreach_def_cb cb, void *state)
54 {
55    return _nir_foreach_def(instr, cb, state);
56 }
57 
58 static inline bool
nir_foreach_src(nir_instr * instr,nir_foreach_src_cb cb,void * state)59 nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
60 {
61    switch (instr->type) {
62    case nir_instr_type_alu: {
63       nir_alu_instr *alu = nir_instr_as_alu(instr);
64       for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++)
65          if (!_nir_visit_src(&alu->src[i].src, cb, state))
66             return false;
67       break;
68    }
69    case nir_instr_type_deref: {
70       nir_deref_instr *deref = nir_instr_as_deref(instr);
71 
72       if (deref->deref_type != nir_deref_type_var) {
73          if (!_nir_visit_src(&deref->parent, cb, state))
74             return false;
75       }
76 
77       if (deref->deref_type == nir_deref_type_array ||
78           deref->deref_type == nir_deref_type_ptr_as_array) {
79          if (!_nir_visit_src(&deref->arr.index, cb, state))
80             return false;
81       }
82       break;
83    }
84    case nir_instr_type_intrinsic: {
85       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
86       unsigned num_srcs = nir_intrinsic_infos[intrin->intrinsic].num_srcs;
87       for (unsigned i = 0; i < num_srcs; i++) {
88          if (!_nir_visit_src(&intrin->src[i], cb, state))
89             return false;
90       }
91       break;
92    }
93    case nir_instr_type_tex: {
94       nir_tex_instr *tex = nir_instr_as_tex(instr);
95       for (unsigned i = 0; i < tex->num_srcs; i++) {
96          if (!_nir_visit_src(&tex->src[i].src, cb, state))
97             return false;
98       }
99       break;
100    }
101    case nir_instr_type_call: {
102       nir_call_instr *call = nir_instr_as_call(instr);
103       for (unsigned i = 0; i < call->num_params; i++) {
104          if (!_nir_visit_src(&call->params[i], cb, state))
105             return false;
106       }
107       break;
108    }
109    case nir_instr_type_phi: {
110       nir_phi_instr *phi = nir_instr_as_phi(instr);
111       nir_foreach_phi_src(src, phi) {
112          if (!_nir_visit_src(&src->src, cb, state))
113             return false;
114       }
115       break;
116    }
117    case nir_instr_type_parallel_copy: {
118       nir_parallel_copy_instr *pc = nir_instr_as_parallel_copy(instr);
119       nir_foreach_parallel_copy_entry(entry, pc) {
120          if (!_nir_visit_src(&entry->src, cb, state))
121             return false;
122          if (entry->dest_is_reg && !_nir_visit_src(&entry->dest.reg, cb, state))
123             return false;
124       }
125       break;
126    }
127    case nir_instr_type_jump: {
128       nir_jump_instr *jump = nir_instr_as_jump(instr);
129 
130       if (jump->type == nir_jump_goto_if && !_nir_visit_src(&jump->condition, cb, state))
131          return false;
132       return true;
133    }
134 
135    case nir_instr_type_load_const:
136    case nir_instr_type_undef:
137       return true;
138 
139    default:
140       unreachable("Invalid instruction type");
141       break;
142    }
143 
144    return true;
145 }
146