1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-tidy-linelength
12 // compile-flags: -C no-prepopulate-passes
13 // min-llvm-version 7.0
15 #![crate_type = "lib"]
16 #![feature(repr_packed)]
30 // CHECK-LABEL: @write_pkd1
32 pub fn write_pkd1(pkd: &mut Packed1) -> u32 {
33 // CHECK: %{{.*}} = load i32, i32* %{{.*}}, align 1
34 // CHECK: store i32 42, i32* %{{.*}}, align 1
35 let result = pkd.data;
40 // CHECK-LABEL: @write_pkd2
42 pub fn write_pkd2(pkd: &mut Packed2) -> u32 {
43 // CHECK: %{{.*}} = load i32, i32* %{{.*}}, align 2
44 // CHECK: store i32 42, i32* %{{.*}}, align 2
45 let result = pkd.data;
50 pub struct Array([i32; 8]);
52 pub struct BigPacked1 {
58 pub struct BigPacked2 {
63 // CHECK-LABEL: @call_pkd1
65 pub fn call_pkd1(f: fn() -> Array) -> BigPacked1 {
66 // CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
67 // CHECK: call void %{{.*}}(%Array* noalias nocapture sret dereferenceable(32) [[ALLOCA]])
68 // CHECK: call void @llvm.memcpy.{{.*}}(i8* align 1 %{{.*}}, i8* align 1 %{{.*}}, i{{[0-9]+}} 32, i1 false)
69 // check that calls whose destination is a field of a packed struct
70 // go through an alloca rather than calling the function with an
71 // unaligned destination.
72 BigPacked1 { dealign: 0, data: f() }
75 // CHECK-LABEL: @call_pkd2
77 pub fn call_pkd2(f: fn() -> Array) -> BigPacked2 {
78 // CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
79 // CHECK: call void %{{.*}}(%Array* noalias nocapture sret dereferenceable(32) [[ALLOCA]])
80 // CHECK: call void @llvm.memcpy.{{.*}}(i8* align 2 %{{.*}}, i8* align 2 %{{.*}}, i{{[0-9]+}} 32, i1 false)
81 // check that calls whose destination is a field of a packed struct
82 // go through an alloca rather than calling the function with an
83 // unaligned destination.
84 BigPacked2 { dealign: 0, data: f() }
88 #[derive(Copy, Clone)]
89 pub struct Packed1Pair(u8, u32);
92 #[derive(Copy, Clone)]
93 pub struct Packed2Pair(u8, u32);
95 // CHECK-LABEL: @pkd1_pair
97 pub fn pkd1_pair(pair1: &mut Packed1Pair, pair2: &mut Packed1Pair) {
98 // CHECK: call void @llvm.memcpy.{{.*}}(i8* align 1 %{{.*}}, i8* align 1 %{{.*}}, i{{[0-9]+}} 5, i1 false)
102 // CHECK-LABEL: @pkd2_pair
104 pub fn pkd2_pair(pair1: &mut Packed2Pair, pair2: &mut Packed2Pair) {
105 // CHECK: call void @llvm.memcpy.{{.*}}(i8* align 2 %{{.*}}, i8* align 2 %{{.*}}, i{{[0-9]+}} 6, i1 false)
110 #[derive(Copy, Clone)]
111 pub struct Packed1NestedPair((u32, u32));
114 #[derive(Copy, Clone)]
115 pub struct Packed2NestedPair((u32, u32));
117 // CHECK-LABEL: @pkd1_nested_pair
119 pub fn pkd1_nested_pair(pair1: &mut Packed1NestedPair, pair2: &mut Packed1NestedPair) {
120 // CHECK: call void @llvm.memcpy.{{.*}}(i8* align 1 %{{.*}}, i8* align 1 %{{.*}}, i{{[0-9]+}} 8, i1 false)
124 // CHECK-LABEL: @pkd2_nested_pair
126 pub fn pkd2_nested_pair(pair1: &mut Packed2NestedPair, pair2: &mut Packed2NestedPair) {
127 // CHECK: call void @llvm.memcpy.{{.*}}(i8* align 2 %{{.*}}, i8* align 2 %{{.*}}, i{{[0-9]+}} 8, i1 false)