summaryrefslogtreecommitdiffstats
path: root/tests/codegen/align-offset.rs
blob: 7c7660c5a55ab759153e420aca30d1eefc706600 (plain)
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
// compile-flags: -O
// min-llvm-version: 15.0 (because we're using opaque pointers)
// ignore-debug (debug assertions in `slice::from_raw_parts` block optimizations)

#![crate_type = "lib"]

// CHECK-LABEL: @align8
#[no_mangle]
pub fn align8(p: *const u8) -> bool {
    // CHECK: ret i1 true
    p.align_offset(8) < 8
}

#[repr(align(4))]
pub struct Align4([u8; 4]);

// CHECK-LABEL: @align_to4
#[no_mangle]
pub fn align_to4(x: &[u8]) -> bool {
    // CHECK: ret i1 true
    let (prefix, _middle, suffix) = unsafe { x.align_to::<Align4>() };
    prefix.len() < 4 && suffix.len() < 4
}

// CHECK-LABEL: @align_offset_byte_ptr(ptr{{.+}}%ptr)
#[no_mangle]
pub fn align_offset_byte_ptr(ptr: *const u8) -> usize {
    // CHECK: %[[ADDR:.+]] = ptrtoint ptr %ptr to [[USIZE:i[0-9]+]]
    // CHECK: %[[UP:.+]] = add [[USIZE]] %[[ADDR]], 31
    // CHECK: %[[ALIGNED:.+]] = and [[USIZE]] %[[UP]], -32
    // CHECK: %[[OFFSET:.+]] = sub [[USIZE]] %[[ALIGNED]], %[[ADDR]]

    // Since we're offsetting a byte pointer, there's no further fixups
    // CHECK-NOT: shr
    // CHECK-NOT: div
    // CHECK-NOT: select

    // CHECK: ret [[USIZE]] %[[OFFSET]]
    ptr.align_offset(32)
}

// CHECK-LABEL: @align_offset_word_slice(ptr{{.+}}align 4{{.+}}%slice.0
#[no_mangle]
pub fn align_offset_word_slice(slice: &[Align4]) -> usize {
    // CHECK: %[[ADDR:.+]] = ptrtoint ptr %slice.0 to [[USIZE]]
    // CHECK: %[[UP:.+]] = add [[USIZE]] %[[ADDR]], 31
    // CHECK: %[[ALIGNED:.+]] = and [[USIZE]] %[[UP]], -32
    // CHECK: %[[BOFFSET:.+]] = sub [[USIZE]] %[[ALIGNED]], %[[ADDR]]
    // CHECK: %[[OFFSET:.+]] = lshr exact [[USIZE]] %[[BOFFSET]], 2

    // Slices are known to be aligned, so we don't need the "maybe -1" path
    // CHECK-NOT: select

    // CHECK: ret [[USIZE]] %[[OFFSET]]
    slice.as_ptr().align_offset(32)
}


// CHECK-LABEL: @align_offset_word_ptr(ptr{{.+}}%ptr
#[no_mangle]
pub fn align_offset_word_ptr(ptr: *const Align4) -> usize {
    // CHECK: %[[ADDR:.+]] = ptrtoint ptr %ptr to [[USIZE]]
    // CHECK: %[[UP:.+]] = add [[USIZE]] %[[ADDR]], 31
    // CHECK: %[[ALIGNED:.+]] = and [[USIZE]] %[[UP]], -32
    // CHECK: %[[BOFFSET:.+]] = sub [[USIZE]] %[[ALIGNED]], %[[ADDR]]

    // While we can always get a *byte* offset that will work, if the original
    // pointer is unaligned it might be impossible to return an *element* offset
    // that will make it aligned. We want it to be a `select`, not a `br`, so
    // that the assembly will be branchless.
    // CHECK: %[[LOW:.+]] = and [[USIZE]] %[[ADDR]], 3
    // CHECK: %[[ORIGINAL_ALIGNED:.+]] = icmp eq [[USIZE]] %[[LOW]], 0
    // CHECK: %[[OFFSET:.+]] = lshr exact [[USIZE]] %[[BOFFSET]], 2
    // CHECK: %[[R:.+]] = select i1 %[[ORIGINAL_ALIGNED]], [[USIZE]] %[[OFFSET]], [[USIZE]] -1

    // CHECK: ret [[USIZE]] %[[R]]
    ptr.align_offset(32)
}