cranelift_codegen/legalizer/
globalvalue.rs

1//! Legalization of global values.
2//!
3//! This module exports the `expand_global_value` function which transforms a `global_value`
4//! instruction into code that depends on the kind of global value referenced.
5
6use crate::cursor::{Cursor, FuncCursor};
7use crate::ir::{self, InstBuilder};
8use crate::isa::TargetIsa;
9
10/// Expand a `global_value` instruction according to the definition of the global value.
11pub 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    // Use a minimum of 128-bits for the base type.
47    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
54/// Expand a `global_value` instruction for a vmctx global.
55fn vmctx_addr(inst: ir::Inst, func: &mut ir::Function) {
56    // Get the value representing the `vmctx` argument.
57    let vmctx = func
58        .special_param(ir::ArgumentPurpose::VMContext)
59        .expect("Missing vmctx parameter");
60
61    // Replace the `global_value` instruction's value with an alias to the vmctx arg.
62    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
68/// Expand a `global_value` instruction for an iadd_imm global.
69fn 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    // Get the value for the lhs. For tidiness, expand VMContext here so that we avoid
79    // `vmctx_addr` which creates an otherwise unneeded value alias.
80    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    // Simply replace the `global_value` instruction with an `iadd_imm`, reusing the result value.
89    pos.func.dfg.replace(inst).iadd_imm(lhs, offset);
90}
91
92/// Expand a `global_value` instruction for a load global.
93fn 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    // We need to load a pointer from the `base` global value, so insert a new `global_value`
103    // instruction. This depends on the iterative legalization loop. Note that the IR verifier
104    // detects any cycles in the `load` globals.
105    let ptr_ty = isa.pointer_type();
106    let mut pos = FuncCursor::new(func).at_inst(inst);
107    pos.use_srcloc(inst);
108
109    // Get the value for the base. For tidiness, expand VMContext here so that we avoid
110    // `vmctx_addr` which creates an otherwise unneeded value alias.
111    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    // Global-value loads are always notrap and aligned. They may be readonly.
120    let mut mflags = ir::MemFlags::trusted();
121    if readonly {
122        mflags.set_readonly();
123    }
124
125    // Perform the load.
126    pos.func
127        .dfg
128        .replace(inst)
129        .load(global_type, mflags, base_addr, offset);
130}
131
132/// Expand a `global_value` instruction for a symbolic name global.
133fn 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}