1; RUN: opt -S -passes=instcombine,simplify-cfg < %s 2>&1 | FileCheck %s
2
3declare void @llvm.assume(i1 noundef)
4
5define void @f1(i8* %a) {
6; CHECK-LABEL: @f1(
7; CHECK-NEXT:  entry:
8; CHECK-NEXT:    [[PTR:%.*]] = getelementptr inbounds i8, i8* [[A:%.*]], i64 4
9; CHECK-NEXT:    [[TMP0:%.*]] = ptrtoint i8* [[PTR]] to i64
10; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[TMP0]], 3
11; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i64 [[TMP1]], 0
12; CHECK-NEXT:    br i1 [[TMP2]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
13; CHECK:       if.then:
14; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "align"(i8* [[PTR]], i64 4) ]
15; CHECK-NEXT:    [[TMP3:%.*]] = bitcast i8* [[PTR]] to i32*
16; CHECK-NEXT:    store i32 4, i32* [[TMP3]], align 4
17; CHECK-NEXT:    br label [[IF_END]]
18; CHECK:       if.end:
19; CHECK-NEXT:    ret void
20;
21entry:
22  %ptr = getelementptr inbounds i8, i8* %a, i64 4
23  %0 = ptrtoint i8* %ptr to i64
24  %1 = and i64 %0, 3
25  %2 = icmp eq i64 %1, 0
26  br i1 %2, label %if.then, label %if.end
27
28if.then:                                          ; preds = %entry
29  call void @llvm.assume(i1 true) [ "align"(i8* %ptr, i64 4) ]
30  %3 = ptrtoint i8* %ptr to i64
31  %4 = and i64 %3, 3
32  %5 = icmp eq i64 %4, 0
33  br i1 %5, label %if.then1, label %if.else1
34
35if.then1:                                         ; preds = %if.then
36  %6 = bitcast i8* %ptr to i32*
37  store i32 4, i32* %6, align 4
38  br label %if.end
39
40if.else1:                                         ; preds = %if.then
41  store i8 1, i8* %ptr, align 1
42  br label %if.end
43
44if.end:                                           ; preds = %if.then1, %if.else1, %entry
45  ret void
46}
47
48; TODO: We could fold away the branch "br i1 %3, ..." by either using a GEP or make getKnowledgeValidInContext aware the alignment bundle offset, and the improvement of value tracking of GEP.
49
50define void @f2(i8* %a) {
51; CHECK-LABEL: @f2(
52; CHECK-NEXT:  entry:
53; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "align"(i8* [[A:%.*]], i64 32, i32 24) ]
54; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 8
55; CHECK-NEXT:    [[TMP1:%.*]] = ptrtoint i8* [[TMP0]] to i64
56; CHECK-NEXT:    [[TMP2:%.*]] = and i64 [[TMP1]], 8
57; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
58; CHECK-NEXT:    br i1 [[TMP3]], label [[IF_THEN1:%.*]], label [[IF_ELSE1:%.*]]
59;
60entry:
61  call void @llvm.assume(i1 true) [ "align"(i8* %a, i64 32, i32 24) ]
62  %0 = getelementptr inbounds i8, i8* %a, i64 8
63  %1 = ptrtoint i8* %0 to i64
64  %2 = and i64 %1, 15
65  %3 = icmp eq i64 %2, 0
66  br i1 %3, label %if.then, label %if.else
67
68if.then:                                          ; preds = %entry
69  %4 = bitcast i8* %0 to i64*
70  store i64 16, i64* %4, align 4
71  br label %if.end
72
73if.else:                                          ; preds = %entry
74  store i8 1, i8* %0, align 1
75  br label %if.end
76
77if.end:                                           ; preds = %if.else, %if.then
78  ret void
79}
80
81