cranelift_codegen/legalizer/
globalvalue.rs1use crate::cursor::{Cursor, FuncCursor};
7use crate::ir::{self, InstBuilder};
8use crate::isa::TargetIsa;
9
10pub fn expand_global_value(
12 inst: ir::Inst,
13 func: &mut ir::Function,
14 isa: &dyn TargetIsa,
15 global_value: ir::GlobalValue,
16) {
17 crate::trace!(
18 "expanding global value: {:?}: {}",
19 inst,
20 func.dfg.display_inst(inst)
21 );
22
23 match func.global_values[global_value] {
24 ir::GlobalValueData::VMContext => vmctx_addr(inst, func),
25 ir::GlobalValueData::IAddImm {
26 base,
27 offset,
28 global_type,
29 } => iadd_imm_addr(inst, func, base, offset.into(), global_type),
30 ir::GlobalValueData::Load {
31 base,
32 offset,
33 global_type,
34 readonly,
35 } => load_addr(inst, func, base, offset, global_type, readonly, isa),
36 ir::GlobalValueData::Symbol { tls, .. } => symbol(inst, func, global_value, isa, tls),
37 ir::GlobalValueData::DynScaleTargetConst { vector_type } => {
38 const_vector_scale(inst, func, vector_type, isa)
39 }
40 }
41}
42
43fn const_vector_scale(inst: ir::Inst, func: &mut ir::Function, ty: ir::Type, isa: &dyn TargetIsa) {
44 assert!(ty.bytes() <= 16);
45
46 let base_bytes = std::cmp::max(ty.bytes(), 16);
48 let scale = (isa.dynamic_vector_bytes(ty) / base_bytes) as i64;
49 assert!(scale > 0);
50 let pos = FuncCursor::new(func).at_inst(inst);
51 pos.func.dfg.replace(inst).iconst(isa.pointer_type(), scale);
52}
53
54fn vmctx_addr(inst: ir::Inst, func: &mut ir::Function) {
56 let vmctx = func
58 .special_param(ir::ArgumentPurpose::VMContext)
59 .expect("Missing vmctx parameter");
60
61 let result = func.dfg.first_result(inst);
63 func.dfg.clear_results(inst);
64 func.dfg.change_to_alias(result, vmctx);
65 func.layout.remove_inst(inst);
66}
67
68fn iadd_imm_addr(
70 inst: ir::Inst,
71 func: &mut ir::Function,
72 base: ir::GlobalValue,
73 offset: i64,
74 global_type: ir::Type,
75) {
76 let mut pos = FuncCursor::new(func).at_inst(inst);
77
78 let lhs = if let ir::GlobalValueData::VMContext = pos.func.global_values[base] {
81 pos.func
82 .special_param(ir::ArgumentPurpose::VMContext)
83 .expect("Missing vmctx parameter")
84 } else {
85 pos.ins().global_value(global_type, base)
86 };
87
88 pos.func.dfg.replace(inst).iadd_imm(lhs, offset);
90}
91
92fn load_addr(
94 inst: ir::Inst,
95 func: &mut ir::Function,
96 base: ir::GlobalValue,
97 offset: ir::immediates::Offset32,
98 global_type: ir::Type,
99 readonly: bool,
100 isa: &dyn TargetIsa,
101) {
102 let ptr_ty = isa.pointer_type();
106 let mut pos = FuncCursor::new(func).at_inst(inst);
107 pos.use_srcloc(inst);
108
109 let base_addr = if let ir::GlobalValueData::VMContext = pos.func.global_values[base] {
112 pos.func
113 .special_param(ir::ArgumentPurpose::VMContext)
114 .expect("Missing vmctx parameter")
115 } else {
116 pos.ins().global_value(ptr_ty, base)
117 };
118
119 let mut mflags = ir::MemFlags::trusted();
121 if readonly {
122 mflags.set_readonly();
123 }
124
125 pos.func
127 .dfg
128 .replace(inst)
129 .load(global_type, mflags, base_addr, offset);
130}
131
132fn symbol(
134 inst: ir::Inst,
135 func: &mut ir::Function,
136 gv: ir::GlobalValue,
137 isa: &dyn TargetIsa,
138 tls: bool,
139) {
140 let ptr_ty = isa.pointer_type();
141
142 if tls {
143 func.dfg.replace(inst).tls_value(ptr_ty, gv);
144 } else {
145 func.dfg.replace(inst).symbol_value(ptr_ty, gv);
146 }
147}