1 //! Instruction predicates/properties, shared by various analyses.
2 use crate::ir::immediates::Offset32;
3 use crate::ir::{self, Block, Function, Inst, InstructionData, Opcode, Type, Value};
4 
5 /// Test whether the given opcode is unsafe to even consider as side-effect-free.
6 #[inline(always)]
7 fn trivially_has_side_effects(opcode: Opcode) -> bool {
8     opcode.is_call()
9         || opcode.is_branch()
10         || opcode.is_terminator()
11         || opcode.is_return()
12         || opcode.can_trap()
13         || opcode.other_side_effects()
14         || opcode.can_store()
15 }
16 
17 /// Load instructions without the `notrap` flag are defined to trap when
18 /// operating on inaccessible memory, so we can't treat them as side-effect-free even if the loaded
19 /// value is unused.
20 #[inline(always)]
21 fn is_load_with_defined_trapping(opcode: Opcode, data: &InstructionData) -> bool {
22     if !opcode.can_load() {
23         return false;
24     }
25     match *data {
26         InstructionData::StackLoad { .. } => false,
27         InstructionData::Load { flags, .. } => !flags.notrap(),
28         _ => true,
29     }
30 }
31 
32 /// Does the given instruction have any side-effect that would preclude it from being removed when
33 /// its value is unused?
34 #[inline(always)]
35 fn has_side_effect(func: &Function, inst: Inst) -> bool {
36     let data = &func.dfg.insts[inst];
37     let opcode = data.opcode();
38     trivially_has_side_effects(opcode) || is_load_with_defined_trapping(opcode, data)
39 }
40 
41 /// Is the given instruction a bitcast to or from a reference type (e.g. `r64`)?
42 pub fn is_bitcast_from_ref(func: &Function, inst: Inst) -> bool {
43     let op = func.dfg.insts[inst].opcode();
44     if op != ir::Opcode::Bitcast {
45         return false;
46     }
47 
48     let arg = func.dfg.inst_args(inst)[0];
49     func.dfg.value_type(arg).is_ref()
50 }
51 
52 /// Does the given instruction behave as a "pure" node with respect to
53 /// aegraph semantics?
54 ///
55 /// - Actual pure nodes (arithmetic, etc)
56 /// - Loads with the `readonly` flag set
57 pub fn is_pure_for_egraph(func: &Function, inst: Inst) -> bool {
58     let is_readonly_load = match func.dfg.insts[inst] {
59         InstructionData::Load {
60             opcode: Opcode::Load,
61             flags,
62             ..
63         } => flags.readonly() && flags.notrap(),
64         _ => false,
65     };
66 
67     // Multi-value results do not play nicely with much of the egraph
68     // infrastructure. They are in practice used only for multi-return
69     // calls and some other odd instructions (e.g. uadd_overflow) which,
70     // for now, we can afford to leave in place as opaque
71     // side-effecting ops. So if more than one result, then the inst
72     // is "not pure". Similarly, ops with zero results can be used
73     // only for their side-effects, so are never pure. (Or if they
74     // are, we can always trivially eliminate them with no effect.)
75     let has_one_result = func.dfg.inst_results(inst).len() == 1;
76 
77     let op = func.dfg.insts[inst].opcode();
78 
79     has_one_result
80         && (is_readonly_load || (!op.can_load() && !trivially_has_side_effects(op)))
81         // Cannot optimize ref-y bitcasts, as that can interact badly with
82         // safepoints and stack maps.
83         && !is_bitcast_from_ref(func, inst)
84 }
85 
86 /// Can the given instruction be merged into another copy of itself?
87 /// These instructions may have side-effects, but as long as we retain
88 /// the first instance of the instruction, the second and further
89 /// instances are redundant if they would produce the same trap or
90 /// result.
91 pub fn is_mergeable_for_egraph(func: &Function, inst: Inst) -> bool {
92     let op = func.dfg.insts[inst].opcode();
93     // We can only merge one-result operators due to the way that GVN
94     // is structured in the egraph implementation.
95     let has_one_result = func.dfg.inst_results(inst).len() == 1;
96     has_one_result
97         // Loads/stores are handled by alias analysis and not
98         // otherwise mergeable.
99         && !op.can_load()
100         && !op.can_store()
101         // Can only have idempotent side-effects.
102         && (!has_side_effect(func, inst) || op.side_effects_idempotent())
103         // Cannot optimize ref-y bitcasts, as that can interact badly with
104         // safepoints and stack maps.
105         && !is_bitcast_from_ref(func, inst)
106 }
107 
108 /// Does the given instruction have any side-effect as per [has_side_effect], or else is a load,
109 /// but not the get_pinned_reg opcode?
110 pub fn has_lowering_side_effect(func: &Function, inst: Inst) -> bool {
111     let op = func.dfg.insts[inst].opcode();
112     op != Opcode::GetPinnedReg && (has_side_effect(func, inst) || op.can_load())
113 }
114 
115 /// Is the given instruction a constant value (`iconst`, `fconst`) that can be
116 /// represented in 64 bits?
117 pub fn is_constant_64bit(func: &Function, inst: Inst) -> Option<u64> {
118     let data = &func.dfg.insts[inst];
119     if data.opcode() == Opcode::Null {
120         return Some(0);
121     }
122     match data {
123         &InstructionData::UnaryImm { imm, .. } => Some(imm.bits() as u64),
124         &InstructionData::UnaryIeee32 { imm, .. } => Some(imm.bits() as u64),
125         &InstructionData::UnaryIeee64 { imm, .. } => Some(imm.bits()),
126         _ => None,
127     }
128 }
129 
130 /// Get the address, offset, and access type from the given instruction, if any.
131 pub fn inst_addr_offset_type(func: &Function, inst: Inst) -> Option<(Value, Offset32, Type)> {
132     let data = &func.dfg.insts[inst];
133     match data {
134         InstructionData::Load { arg, offset, .. } => {
135             let ty = func.dfg.value_type(func.dfg.inst_results(inst)[0]);
136             Some((*arg, *offset, ty))
137         }
138         InstructionData::LoadNoOffset { arg, .. } => {
139             let ty = func.dfg.value_type(func.dfg.inst_results(inst)[0]);
140             Some((*arg, 0.into(), ty))
141         }
142         InstructionData::Store { args, offset, .. } => {
143             let ty = func.dfg.value_type(args[0]);
144             Some((args[1], *offset, ty))
145         }
146         InstructionData::StoreNoOffset { args, .. } => {
147             let ty = func.dfg.value_type(args[0]);
148             Some((args[1], 0.into(), ty))
149         }
150         _ => None,
151     }
152 }
153 
154 /// Get the store data, if any, from an instruction.
155 pub fn inst_store_data(func: &Function, inst: Inst) -> Option<Value> {
156     let data = &func.dfg.insts[inst];
157     match data {
158         InstructionData::Store { args, .. } | InstructionData::StoreNoOffset { args, .. } => {
159             Some(args[0])
160         }
161         _ => None,
162     }
163 }
164 
165 /// Determine whether this opcode behaves as a memory fence, i.e.,
166 /// prohibits any moving of memory accesses across it.
167 pub fn has_memory_fence_semantics(op: Opcode) -> bool {
168     match op {
169         Opcode::AtomicRmw
170         | Opcode::AtomicCas
171         | Opcode::AtomicLoad
172         | Opcode::AtomicStore
173         | Opcode::Fence
174         | Opcode::Debugtrap => true,
175         Opcode::Call | Opcode::CallIndirect => true,
176         op if op.can_trap() => true,
177         _ => false,
178     }
179 }
180 
181 /// Visit all successors of a block with a given visitor closure. The closure
182 /// arguments are the branch instruction that is used to reach the successor,
183 /// the successor block itself, and a flag indicating whether the block is
184 /// branched to via a table entry.
185 pub(crate) fn visit_block_succs<F: FnMut(Inst, Block, bool)>(
186     f: &Function,
187     block: Block,
188     mut visit: F,
189 ) {
190     if let Some(inst) = f.layout.last_inst(block) {
191         match &f.dfg.insts[inst] {
192             ir::InstructionData::Jump {
193                 destination: dest, ..
194             } => {
195                 visit(inst, dest.block(&f.dfg.value_lists), false);
196             }
197 
198             ir::InstructionData::Brif {
199                 blocks: [block_then, block_else],
200                 ..
201             } => {
202                 visit(inst, block_then.block(&f.dfg.value_lists), false);
203                 visit(inst, block_else.block(&f.dfg.value_lists), false);
204             }
205 
206             ir::InstructionData::BranchTable { table, .. } => {
207                 let pool = &f.dfg.value_lists;
208                 let table = &f.stencil.dfg.jump_tables[*table];
209 
210                 // The default block is reached via a direct conditional branch,
211                 // so it is not part of the table. We visit the default block
212                 // first explicitly, to mirror the traversal order of
213                 // `JumpTableData::all_branches`, and transitively the order of
214                 // `InstructionData::branch_destination`.
215                 //
216                 // Additionally, this case is why we are unable to replace this
217                 // whole function with a loop over `branch_destination`: we need
218                 // to report which branch targets come from the table vs the
219                 // default.
220                 visit(inst, table.default_block().block(pool), false);
221 
222                 for dest in table.as_slice() {
223                     visit(inst, dest.block(pool), true);
224                 }
225             }
226 
227             inst => debug_assert!(!inst.opcode().is_branch()),
228         }
229     }
230 }
231