Skip to content

Commit 12ff05f

Browse files
committed
Clean up matches that determine integers for specific alignment requirements
1 parent 4038189 commit 12ff05f

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

src/librustc/ty/layout.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ impl Integer {
339339
}
340340
}
341341

342+
pub fn align(&self, dl: &TargetDataLayout)-> Align {
343+
match *self {
344+
I1 => dl.i1_align,
345+
I8 => dl.i8_align,
346+
I16 => dl.i16_align,
347+
I32 => dl.i32_align,
348+
I64 => dl.i64_align,
349+
}
350+
}
351+
342352
pub fn to_ty<'a, 'tcx>(&self, tcx: &ty::TyCtxt<'a, 'tcx, 'tcx>,
343353
signed: bool) -> Ty<'tcx> {
344354
match (*self, signed) {
@@ -377,6 +387,18 @@ impl Integer {
377387
}
378388
}
379389

390+
//Find the smallest integer with the given alignment.
391+
pub fn for_abi_align(dl: &TargetDataLayout, align: Align) -> Option<Integer> {
392+
let wanted = align.abi();
393+
for &candidate in &[I8, I16, I32, I64] {
394+
let ty = Int(candidate);
395+
if wanted == ty.align(dl).abi() && wanted == ty.size(dl).bytes() {
396+
return Some(candidate);
397+
}
398+
}
399+
None
400+
}
401+
380402
/// Get the Integer type from an attr::IntType.
381403
pub fn from_attr(dl: &TargetDataLayout, ity: attr::IntType) -> Integer {
382404
match ity {
@@ -1149,20 +1171,7 @@ impl<'a, 'gcx, 'tcx> Layout {
11491171
// won't be so conservative.
11501172

11511173
// Use the initial field alignment
1152-
let wanted = start_align.abi();
1153-
let mut ity = min_ity;
1154-
for &candidate in &[I16, I32, I64] {
1155-
let ty = Int(candidate);
1156-
if wanted == ty.align(dl).abi() && wanted == ty.size(dl).bytes() {
1157-
ity = candidate;
1158-
break;
1159-
}
1160-
}
1161-
1162-
// FIXME(eddyb) conservative only to avoid diverging from trans::adt.
1163-
if align.abi() != start_align.abi() {
1164-
ity = min_ity;
1165-
}
1174+
let mut ity = Integer::for_abi_align(dl, start_align).unwrap_or(min_ity);
11661175

11671176
// If the alignment is not larger than the chosen discriminant size,
11681177
// don't use the alignment as the final size.

src/librustc_trans/adt.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,15 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
286286

287287
fn union_fill(cx: &CrateContext, size: u64, align: u64) -> Type {
288288
assert_eq!(size%align, 0);
289+
assert_eq!(align.count_ones(), 1, "Alignment must be a power fof 2. Got {}", align);
289290
let align_units = size/align;
290-
match align {
291-
1 => Type::array(&Type::i8(cx), align_units),
292-
2 => Type::array(&Type::i16(cx), align_units),
293-
4 => Type::array(&Type::i32(cx), align_units),
294-
8 if machine::llalign_of_min(cx, Type::i64(cx)) == 8 =>
295-
Type::array(&Type::i64(cx), align_units),
296-
a if a.count_ones() == 1 => Type::array(&Type::vector(&Type::i32(cx), a / 4),
297-
align_units),
298-
_ => bug!("unsupported union alignment: {}", align)
291+
let dl = &cx.tcx().data_layout;
292+
let layout_align = layout::Align::from_bytes(align, align).unwrap();
293+
if let Some(ity) = layout::Integer::for_abi_align(dl, layout_align) {
294+
Type::array(&Type::from_integer(cx, ity), align_units)
295+
} else {
296+
Type::array(&Type::vector(&Type::i32(cx), align/4),
297+
align_units)
299298
}
300299
}
301300

0 commit comments

Comments
 (0)