Debian Codegraph @

src/nouveau/compiler/nak/opt_dce.rs

raw
1 // Copyright © 2022 Collabora, Ltd.
2 // SPDX-License-Identifier: MIT
3
4 use crate::{
5 api::{GetDebugFlags, DEBUG},
6 ir::*,
7 };
8 use compiler::bitset::BitSet;
9
10 struct DeadCodePass {
11 any_dead: bool,
12 new_live: bool,
13 live_ssa: BitSet<SSAValue>,
14 live_phi: BitSet<Phi>,
15 }
16
17 impl DeadCodePass {
18 pub fn new() -> DeadCodePass {
19 DeadCodePass {
20 any_dead: false,
21 new_live: false,
22 live_ssa: Default::default(),
23 live_phi: Default::default(),
24 }
25 }
26
27 fn mark_ssa_live(&mut self, ssa: &SSAValue) {
28 self.new_live |= self.live_ssa.insert(*ssa);
29 }
30
31 fn mark_src_live(&mut self, src: &Src) {
32 for ssa in src.iter_ssa() {
33 self.mark_ssa_live(ssa);
34 }
35 }
36
37 fn mark_phi_live(&mut self, phi: Phi) {
38 self.new_live |= self.live_phi.insert(phi);
39 }
40
41 fn is_dst_live(&self, dst: &Dst) -> bool {
42 match dst {
43 Dst::SSA(ssa) => {
44 for val in ssa.iter() {
45 if self.live_ssa.contains(*val) {
46 return true;
47 }
48 }
49 false
50 }
51 Dst::None => false,
52 _ => panic!("Invalid SSA destination"),
53 }
54 }
55
56 fn is_phi_live(&self, phi: Phi) -> bool {
57 self.live_phi.contains(phi)
58 }
59
60 fn is_instr_live(&self, instr: &Instr) -> bool {
61 if instr.pred.is_false() {
62 return false;
63 }
64
65 if !instr.can_eliminate() {
66 return true;
67 }
68
69 for dst in instr.dsts() {
70 if self.is_dst_live(dst) {
71 return true;
72 }
73 }
74
75 false
76 }
77
78 fn mark_instr(&mut self, instr: &Instr) {
79 match &instr.op {
80 Op::PhiSrcs(phi) => {
81 assert!(instr.pred.is_true());
82 for (phi, src) in phi.srcs.iter() {
83 if self.is_phi_live(*phi) {
84 self.mark_src_live(src);
85 } else {
86 self.any_dead = true;
87 }
88 }
89 }
90 Op::PhiDsts(phi) => {
91 assert!(instr.pred.is_true());
92 for (phi, dst) in phi.dsts.iter() {
93 if self.is_dst_live(dst) {
94 self.mark_phi_live(*phi);
95 } else {
96 self.any_dead = true;
97 }
98 }
99 }
100 Op::ParCopy(pcopy) => {
101 assert!(instr.pred.is_true());
102 for (dst, src) in pcopy.dsts_srcs.iter() {
103 if self.is_dst_live(dst) {
104 self.mark_src_live(src);
105 } else {
106 self.any_dead = true;
107 }
108 }
109 }
110 _ => {
111 if self.is_instr_live(instr) {
112 if let PredRef::SSA(ssa) = &instr.pred.pred_ref {
113 self.mark_ssa_live(ssa);
114 }
115
116 for src in instr.srcs() {
117 self.mark_src_live(src);
118 }
119 } else {
120 self.any_dead = true;
121 }
122 }
123 }
124 }
125
126 fn map_instr(&self, mut instr: Instr) -> MappedInstrs {
127 let is_live = match &mut instr.op {
128 Op::PhiSrcs(phi) => {
129 phi.srcs.retain(|phi, _| self.is_phi_live(*phi));
130 !phi.srcs.is_empty()
131 }
132 Op::PhiDsts(phi) => {
133 phi.dsts.retain(|_, dst| self.is_dst_live(dst));
134 !phi.dsts.is_empty()
135 }
136 Op::ParCopy(pcopy) => {
137 pcopy.dsts_srcs.retain(|dst, _| self.is_dst_live(dst));
138 !pcopy.dsts_srcs.is_empty()
139 }
140 _ => self.is_instr_live(&instr),
141 };
142
143 if is_live {
144 MappedInstrs::One(instr)
145 } else {
146 if DEBUG.annotate() {
147 MappedInstrs::One(Instr::new(OpAnnotate {
148 annotation: "killed by dce".into(),
149 }))
150 } else {
151 MappedInstrs::None
152 }
153 }
154 }
155
156 pub fn run(&mut self, f: &mut Function) {
157 loop {
158 self.new_live = false;
159 self.any_dead = false;
160
161 for b in f.blocks.iter().rev() {
162 for instr in b.instrs.iter().rev() {
163 self.mark_instr(instr);
164 }
165 }
166
167 if !self.new_live {
168 break;
169 }
170 }
171
172 if self.any_dead {
173 f.map_instrs(|instr, _| self.map_instr(instr));
174 }
175 }
176 }
177
178 impl Function {
179 pub fn opt_dce(&mut self) {
180 DeadCodePass::new().run(self);
181 }
182 }
183
184 impl Shader<'_> {
185 pub fn opt_dce(&mut self) {
186 for f in &mut self.functions {
187 f.opt_dce();
188 }
189 }
190 }