xref: /llvm-project/clang/test/CodeGen/builtin-align-array.c (revision 590884a860ccc5fce50bf0a7eba91ec1bfe71fb6)
1 // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2 /// Check that the alignment builtins handle array-to-pointer decay
3 // RUN: %clang_cc1 -triple=x86_64-unknown-unknown -o - -emit-llvm %s | FileCheck %s
4 
5 extern int func(char *c);
6 
7 // CHECK-LABEL: @test_array(
8 // CHECK-NEXT:  entry:
9 // CHECK-NEXT:    [[BUF:%.*]] = alloca [1024 x i8], align 16
10 // CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [1024 x i8], ptr [[BUF]], i64 0, i64 44
11 // CHECK-NEXT:    [[ALIGNED_RESULT:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[ARRAYIDX]], i64 -16)
12 // CHECK-NEXT:    [[CALL:%.*]] = call i32 @func(ptr noundef [[ALIGNED_RESULT]])
13 // CHECK-NEXT:    [[ARRAYIDX1:%.*]] = getelementptr inbounds [1024 x i8], ptr [[BUF]], i64 0, i64 22
14 // CHECK-NEXT:    [[OVER_BOUNDARY:%.*]] = getelementptr inbounds i8, ptr [[ARRAYIDX1]], i64 31
15 // CHECK-NEXT:    [[ALIGNED_RESULT2:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[OVER_BOUNDARY]], i64 -32)
16 // CHECK-NEXT:    [[CALL3:%.*]] = call i32 @func(ptr noundef [[ALIGNED_RESULT2]])
17 // CHECK-NEXT:    [[ARRAYIDX4:%.*]] = getelementptr inbounds [1024 x i8], ptr [[BUF]], i64 0, i64 16
18 // CHECK-NEXT:    [[SRC_ADDR:%.*]] = ptrtoint ptr [[ARRAYIDX4]] to i64
19 // CHECK-NEXT:    [[SET_BITS:%.*]] = and i64 [[SRC_ADDR]], 63
20 // CHECK-NEXT:    [[IS_ALIGNED:%.*]] = icmp eq i64 [[SET_BITS]], 0
21 // CHECK-NEXT:    [[CONV:%.*]] = zext i1 [[IS_ALIGNED]] to i32
22 // CHECK-NEXT:    ret i32 [[CONV]]
23 //
test_array(void)24 int test_array(void) {
25   char buf[1024];
26   func(__builtin_align_down(&buf[44], 16));
27   func(__builtin_align_up(&buf[22], 32));
28   return __builtin_is_aligned(&buf[16], 64);
29 }
30 
31 // CHECK-LABEL: @test_array_should_not_mask(
32 // CHECK-NEXT:  entry:
33 // CHECK-NEXT:    [[BUF:%.*]] = alloca [1024 x i8], align 32
34 // CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [1024 x i8], ptr [[BUF]], i64 0, i64 64
35 // CHECK-NEXT:    [[ALIGNED_RESULT:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[ARRAYIDX]], i64 -16)
36 // CHECK-NEXT:    [[CALL:%.*]] = call i32 @func(ptr noundef [[ALIGNED_RESULT]])
37 // CHECK-NEXT:    [[ARRAYIDX1:%.*]] = getelementptr inbounds [1024 x i8], ptr [[BUF]], i64 0, i64 32
38 // CHECK-NEXT:    [[OVER_BOUNDARY:%.*]] = getelementptr inbounds i8, ptr [[ARRAYIDX1]], i64 31
39 // CHECK-NEXT:    [[ALIGNED_RESULT2:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[OVER_BOUNDARY]], i64 -32)
40 // CHECK-NEXT:    [[CALL3:%.*]] = call i32 @func(ptr noundef [[ALIGNED_RESULT2]])
41 // CHECK-NEXT:    ret i32 1
42 //
test_array_should_not_mask(void)43 int test_array_should_not_mask(void) {
44   _Alignas(32) char buf[1024];
45   // TODO: The align_up and align_down calls should be folded to no-ops
46   func(__builtin_align_down(&buf[64], 16));
47   func(__builtin_align_up(&buf[32], 32));
48   // This expression can be constant-evaluated:
49   return __builtin_is_aligned(&buf[64], 32);
50 }
51