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
//! Byte-neutral responsibility split of the former monolith impl; see the parent module.
use super::*;
impl LlModule {
pub(in crate::native) fn infer_local_alloca_pointees(&mut self) {
for f in &self.functions {
let allocas = self.local_allocas(f);
if allocas.is_empty() {
continue;
}
let mut roots: HashMap<String, String> = allocas
.keys()
.map(|name| (name.clone(), name.clone()))
.collect();
let mut sources: HashMap<String, HashSet<LlType>> = HashMap::new();
let mut changed = true;
while changed {
changed = false;
for inst in f.carrier_insts() {
if let Some((res, base)) = &inst.identity_ptr_bitcast {
if let Some(root) = roots.get(base).cloned() {
if roots.insert(res.clone(), root).is_none() {
changed = true;
}
}
continue;
}
let Some(res) = &inst.result else {
continue;
};
if let Some(gep) = &inst.gep {
let LlValue::Local(base) = &gep.base.value else {
continue;
};
let Some(root) = roots.get(base).cloned() else {
continue;
};
sources
.entry(root.clone())
.or_default()
.insert(gep.source_ty.clone());
if roots.insert(res.clone(), root).is_none() {
changed = true;
}
continue;
}
if let Some(incoming) = &inst.phi_incoming_values {
let mut root: Option<String> = None;
for value in incoming {
let LlValue::Local(name) = value else {
continue;
};
let Some(candidate) = roots.get(name).cloned() else {
continue;
};
match &root {
Some(existing) if existing != &candidate => {
root = None;
break;
}
None => root = Some(candidate),
_ => {}
}
}
if let Some(root) = root {
if roots.insert(res.clone(), root).is_none() {
changed = true;
}
}
continue;
}
}
}
for (name, seen) in sources {
let Some(original) = allocas.get(&name) else {
continue;
};
let candidates = seen
.into_iter()
.filter(|ty| ty != original)
.filter(|ty| self.is_local_alloca_reinterpret_candidate(original, ty))
.collect::<Vec<_>>();
if let [candidate] = candidates.as_slice() {
self.local_alloca_pointees
.insert((f.name.clone(), name), candidate.clone());
} else if candidates.len() > 1 {
// Conflicting same-size views of one alloca (e.g. a union staged through both an
// `{ i64 }` and a `{ {i32}, {i32} }` reinterpret). A single struct view can't
// receive the others, but a byte array is the universal receiver: every
// incompatible typed view lowers through the byte-reinterpret raw GEP + byte-
// assembled load/store path. Only force it when the views themselves include an
// i8-array (the byte-fill idiom), so exotic multi-struct sets that validate
// structurally today keep their current model.
let byte_view = candidates.iter().find(|ty| {
matches!(self.resolve_known_type(ty),
LlType::Array(elem, _) if *elem == LlType::Int(8))
});
if let Some(byte_view) = byte_view {
self.local_alloca_pointees
.insert((f.name.clone(), name), byte_view.clone());
}
}
}
}
}
pub(in crate::native) fn local_allocas(&self, f: &LlFunction) -> HashMap<String, LlType> {
let mut allocas = HashMap::new();
for inst in f.carrier_insts() {
// `alloca_ty` is `resolve_alloca_ty` — the exact `parse_type(parts[0])` on the post-`alloca`
// comma-split the reader ran, computed only for `opcode == "alloca"`, so a `Some` here is
// precisely the reader's parsed alloca type. The result name is the reader's LHS.
let (Some(name), Some(ty)) = (&inst.result, &inst.alloca_ty) else {
continue;
};
allocas.insert(name.clone(), ty.clone());
}
allocas
}
pub(in crate::native) fn is_local_alloca_reinterpret_candidate(
&self,
original: &LlType,
candidate: &LlType,
) -> bool {
let original = self.resolve_known_type(original);
let candidate = self.resolve_known_type(candidate);
if self.type_contains_pointer(&original) || self.type_contains_pointer(&candidate) {
return false;
}
let Some((original_size, _)) = self.native_memcpy_type_size_align(&original) else {
return false;
};
let Some((candidate_size, _)) = self.native_memcpy_type_size_align(&candidate) else {
return false;
};
original_size == candidate_size
}
pub(crate) fn resolve_known_type(&self, ty: &LlType) -> LlType {
match ty {
LlType::Int(1) => LlType::Bool,
LlType::Named(name) => self
.types
.get(name)
.map(|ty| self.resolve_known_type(ty))
.unwrap_or_else(|| ty.clone()),
LlType::Vector(elem, 1) => self.resolve_known_type(elem),
LlType::Vector(elem, lanes) => {
LlType::Vector(Box::new(self.resolve_known_type(elem)), *lanes)
}
LlType::Array(elem, len) => {
LlType::Array(Box::new(self.resolve_known_type(elem)), *len)
}
LlType::Struct(fields) => LlType::Struct(
fields
.iter()
.map(|field| self.resolve_known_type(field))
.collect(),
),
_ => ty.clone(),
}
}
}