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
use crate::{
constant::{Constant, ConstantValue},
context::Context,
error::IrError,
function::Function,
instruction::Instruction,
value::{Value, ValueContent, ValueDatum},
};
pub fn combine_constants(context: &mut Context, function: &Function) -> Result<bool, IrError> {
let mut modified = false;
loop {
if combine_const_insert_values(context, function) {
modified = true;
continue;
}
break;
}
Ok(modified)
}
fn combine_const_insert_values(context: &mut Context, function: &Function) -> bool {
let candidate = function
.instruction_iter(context)
.find_map(|(block, ins_val)| {
match &context.values[ins_val.0].value {
ValueDatum::Instruction(Instruction::InsertValue {
aggregate,
ty: _,
value,
indices,
}) if value.is_constant(context)
&& matches!(
&context.values[aggregate.0].value,
ValueDatum::Constant(Constant {
value: ConstantValue::Struct(_),
..
}),
) =>
{
Some((block, ins_val, *aggregate, *value, indices.clone()))
}
_otherwise => None,
}
});
if let Some((block, ins_val, aggregate, const_val, indices)) = candidate {
let new_aggregate =
combine_const_aggregate_field(context, function, aggregate, const_val, &indices);
function.replace_value(context, ins_val, new_aggregate, None);
block.remove_instruction(context, ins_val);
return true;
}
false
}
fn combine_const_aggregate_field(
context: &mut Context,
function: &Function,
aggregate: Value,
const_value: Value,
indices: &[u64],
) -> Value {
let (mut new_aggregate, span_md_idx) = match &context.values[aggregate.0] {
ValueContent {
value: ValueDatum::Constant(c),
span_md_idx,
} => (c.clone(), *span_md_idx),
_otherwise => {
unreachable!("BUG! Invalid aggregate parameter to combine_const_insert_value()")
}
};
let const_value = match &context.values[const_value.0].value {
ValueDatum::Constant(c) => c.clone(),
_otherwise => {
unreachable!("BUG! Invalid const_value parameter to combine_const_insert_value()")
}
};
inject_constant_into_aggregate(&mut new_aggregate, const_value, indices);
let new_aggregate_value = Value::new_constant(context, new_aggregate, span_md_idx);
function.replace_value(context, aggregate, new_aggregate_value, None);
new_aggregate_value
}
fn inject_constant_into_aggregate(aggregate: &mut Constant, value: Constant, indices: &[u64]) {
if indices.is_empty() {
*aggregate = value;
} else {
match &mut aggregate.value {
ConstantValue::Struct(fields) => inject_constant_into_aggregate(
&mut fields[indices[0] as usize],
value,
&indices[1..],
),
_otherwise => {
unreachable!("Bug! Invalid aggregate parameter to inject_constant_into_aggregate()")
}
}
}
}