1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::{
AnalysisResults, BlockArgument, Context, Function, Instruction, IrError, Pass, PassMutability,
ScopedPass, Type, Value,
};
pub const RETDEMOTION_NAME: &str = "retdemotion";
pub fn create_ret_demotion_pass() -> Pass {
Pass {
name: RETDEMOTION_NAME,
descr: "By-value function return value demotion to by-reference.",
deps: Vec::new(),
runner: ScopedPass::FunctionPass(PassMutability::Transform(ret_val_demotion)),
}
}
pub fn ret_val_demotion(
context: &mut Context,
_: &AnalysisResults,
function: Function,
) -> Result<bool, IrError> {
let ret_type = function.get_return_type(context);
if !super::target_fuel::is_demotable_type(context, &ret_type) {
return Ok(false);
}
let ptr_ret_type = Type::new_ptr(context, ret_type);
function.set_return_type(context, ptr_ret_type);
let entry_block = function.get_entry_block(context);
let ptr_arg_val = if function.is_entry(context) {
let ret_var =
function.new_unique_local_var(context, "__ret_value".to_owned(), ret_type, None);
let get_ret_var = Value::new_instruction(context, Instruction::GetLocal(ret_var));
context.blocks[entry_block.0]
.instructions
.insert(0, get_ret_var);
get_ret_var
} else {
let ptr_arg_val = Value::new_argument(
context,
BlockArgument {
block: entry_block,
idx: function.num_args(context),
ty: ptr_ret_type,
},
);
function.add_arg(context, "__ret_value", ptr_arg_val);
entry_block.add_arg(context, ptr_arg_val);
ptr_arg_val
};
let ret_blocks = function
.block_iter(context)
.filter_map(|block| {
block.get_terminator(context).and_then(|term| {
if let Instruction::Ret(ret_val, _ty) = term {
Some((block, *ret_val))
} else {
None
}
})
})
.collect::<Vec<_>>();
for (ret_block, ret_val) in ret_blocks {
let block_instrs = &mut context.blocks[ret_block.0].instructions;
let orig_ret_val = block_instrs.last().copied();
block_instrs.pop();
let md_idx = orig_ret_val.and_then(|val| val.get_metadata(context));
ret_block
.ins(context)
.store(ptr_arg_val, ret_val)
.add_metadatum(context, md_idx);
ret_block
.ins(context)
.ret(ptr_arg_val, ptr_ret_type)
.add_metadatum(context, md_idx);
}
if !function.is_entry(context) {
update_callers(context, function, ret_type);
}
Ok(true)
}
fn update_callers(context: &mut Context, function: Function, ret_type: Type) {
let call_sites = context
.module_iter()
.flat_map(|module| module.function_iter(context))
.flat_map(|ref call_from_func| {
call_from_func
.block_iter(context)
.flat_map(|ref block| {
block
.instruction_iter(context)
.filter_map(|instr_val| {
if let Instruction::Call(call_to_func, _) = instr_val
.get_instruction(context)
.expect("`instruction_iter()` must return instruction values.")
{
(*call_to_func == function).then_some((
*call_from_func,
*block,
instr_val,
))
} else {
None
}
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
for (calling_func, calling_block, call_val) in call_sites {
let loc_var =
calling_func.new_unique_local_var(context, "__ret_val".to_owned(), ret_type, None);
let get_loc_val = Value::new_instruction(context, Instruction::GetLocal(loc_var));
let Some(Instruction::Call(_, args)) = call_val.get_instruction(context) else {
unreachable!("`call_val` is definitely a call instruction.");
};
let mut new_args = args.clone();
new_args.push(get_loc_val);
let new_call_val = Value::new_instruction(context, Instruction::Call(function, new_args));
let load_val = Value::new_instruction(context, Instruction::Load(new_call_val));
let block_instrs = &mut context.blocks[calling_block.0].instructions;
let call_inst_idx = block_instrs
.iter()
.position(|&instr_val| instr_val == call_val)
.unwrap();
block_instrs[call_inst_idx] = get_loc_val;
block_instrs.insert(call_inst_idx + 1, new_call_val);
block_instrs.insert(call_inst_idx + 2, load_val);
calling_func.replace_value(context, call_val, load_val, None);
}
}