1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -loop-idiom -verify -verify-each -verify-dom-info -verify-loop-info < %s -S | FileCheck %s
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
6 ; We do not necessarily have a loop with comparison of two loaded values.
7 ; The loop may already be doing bcmp() itself. We just need to widen it.
9 ; FIXME: -memcpyopt does not promote memcmp() source into memcmp() / bcmp().
11 ; #include <algorithm>
14 ; bool widening_of_bcmp(char const* ptr0, char const* ptr1, size_t count) {
15 ; static constexpr auto Size = 32, Step = 32;
16 ; for(size_t i = 0; i < count; i++, ptr0 += Step, ptr1 += Step) {
17 ; if(bcmp(ptr0, ptr1, Size) != 0)
24 ; bool widening_of_overlapping_bcmp(char const* ptr0, char const* ptr1, size_t count) {
25 ; static constexpr auto Size = 32, Step = 16;
26 ; for(size_t i = 0; i < count; i++, ptr0 += Step, ptr1 += Step) {
27 ; if(bcmp(ptr0, ptr1, Size) != 0)
33 declare i32 @bcmp(i8*, i8*, i64)
36 define i1 @_Z18widening_of_bcmpPKcS0_m(i8* %ptr0, i8* %ptr1, i64 %count) {
37 ; CHECK-LABEL: @_Z18widening_of_bcmpPKcS0_m(
39 ; CHECK-NEXT: [[CMP7:%.*]] = icmp eq i64 [[COUNT:%.*]], 0
40 ; CHECK-NEXT: br i1 [[CMP7]], label [[CLEANUP:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
41 ; CHECK: for.body.preheader:
42 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
44 ; CHECK-NEXT: [[I_010:%.*]] = phi i64 [ [[INC:%.*]], [[FOR_INC:%.*]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
45 ; CHECK-NEXT: [[PTR1_ADDR_09:%.*]] = phi i8* [ [[ADD_PTR2:%.*]], [[FOR_INC]] ], [ [[PTR1:%.*]], [[FOR_BODY_PREHEADER]] ]
46 ; CHECK-NEXT: [[PTR0_ADDR_08:%.*]] = phi i8* [ [[ADD_PTR:%.*]], [[FOR_INC]] ], [ [[PTR0:%.*]], [[FOR_BODY_PREHEADER]] ]
47 ; CHECK-NEXT: [[BCMP:%.*]] = tail call i32 @bcmp(i8* [[PTR0_ADDR_08]], i8* [[PTR1_ADDR_09]], i64 32)
48 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[BCMP]], 0
49 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_INC]], label [[CLEANUP_LOOPEXIT:%.*]]
51 ; CHECK-NEXT: [[INC]] = add nuw i64 [[I_010]], 1
52 ; CHECK-NEXT: [[ADD_PTR]] = getelementptr inbounds i8, i8* [[PTR0_ADDR_08]], i64 32
53 ; CHECK-NEXT: [[ADD_PTR2]] = getelementptr inbounds i8, i8* [[PTR1_ADDR_09]], i64 32
54 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], [[COUNT]]
55 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP_LOOPEXIT]]
56 ; CHECK: cleanup.loopexit:
57 ; CHECK-NEXT: [[RES_PH:%.*]] = phi i1 [ false, [[FOR_BODY]] ], [ true, [[FOR_INC]] ]
58 ; CHECK-NEXT: br label [[CLEANUP]]
60 ; CHECK-NEXT: [[RES:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ [[RES_PH]], [[CLEANUP_LOOPEXIT]] ]
61 ; CHECK-NEXT: ret i1 [[RES]]
64 %cmp7 = icmp eq i64 %count, 0
65 br i1 %cmp7, label %cleanup, label %for.body
67 for.body: ; preds = %entry, %for.inc
68 %i.010 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
69 %ptr1.addr.09 = phi i8* [ %add.ptr2, %for.inc ], [ %ptr1, %entry ]
70 %ptr0.addr.08 = phi i8* [ %add.ptr, %for.inc ], [ %ptr0, %entry ]
71 %bcmp = tail call i32 @bcmp(i8* %ptr0.addr.08, i8* %ptr1.addr.09, i64 32)
72 %cmp1 = icmp eq i32 %bcmp, 0
73 br i1 %cmp1, label %for.inc, label %cleanup
75 for.inc: ; preds = %for.body
76 %inc = add nuw i64 %i.010, 1
77 %add.ptr = getelementptr inbounds i8, i8* %ptr0.addr.08, i64 32
78 %add.ptr2 = getelementptr inbounds i8, i8* %ptr1.addr.09, i64 32
79 %cmp = icmp ult i64 %inc, %count
80 br i1 %cmp, label %for.body, label %cleanup
82 cleanup: ; preds = %for.body, %for.inc, %entry
83 %res = phi i1 [ true, %entry ], [ true, %for.inc ], [ false, %for.body ]
88 define i1 @_Z30widening_of_overlapping_bcmpPKcS0_m(i8* %ptr0, i8* %ptr1, i64 %count) {
89 ; CHECK-LABEL: @_Z30widening_of_overlapping_bcmpPKcS0_m(
91 ; CHECK-NEXT: [[CMP7:%.*]] = icmp eq i64 [[COUNT:%.*]], 0
92 ; CHECK-NEXT: br i1 [[CMP7]], label [[CLEANUP:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
93 ; CHECK: for.body.preheader:
94 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
96 ; CHECK-NEXT: [[I_010:%.*]] = phi i64 [ [[INC:%.*]], [[FOR_INC:%.*]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
97 ; CHECK-NEXT: [[PTR1_ADDR_09:%.*]] = phi i8* [ [[ADD_PTR2:%.*]], [[FOR_INC]] ], [ [[PTR1:%.*]], [[FOR_BODY_PREHEADER]] ]
98 ; CHECK-NEXT: [[PTR0_ADDR_08:%.*]] = phi i8* [ [[ADD_PTR:%.*]], [[FOR_INC]] ], [ [[PTR0:%.*]], [[FOR_BODY_PREHEADER]] ]
99 ; CHECK-NEXT: [[BCMP:%.*]] = tail call i32 @bcmp(i8* [[PTR0_ADDR_08]], i8* [[PTR1_ADDR_09]], i64 32)
100 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[BCMP]], 0
101 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_INC]], label [[CLEANUP_LOOPEXIT:%.*]]
103 ; CHECK-NEXT: [[INC]] = add nuw i64 [[I_010]], 1
104 ; CHECK-NEXT: [[ADD_PTR]] = getelementptr inbounds i8, i8* [[PTR0_ADDR_08]], i64 16
105 ; CHECK-NEXT: [[ADD_PTR2]] = getelementptr inbounds i8, i8* [[PTR1_ADDR_09]], i64 16
106 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], [[COUNT]]
107 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP_LOOPEXIT]]
108 ; CHECK: cleanup.loopexit:
109 ; CHECK-NEXT: [[RES_PH:%.*]] = phi i1 [ false, [[FOR_BODY]] ], [ true, [[FOR_INC]] ]
110 ; CHECK-NEXT: br label [[CLEANUP]]
112 ; CHECK-NEXT: [[RES:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ [[RES_PH]], [[CLEANUP_LOOPEXIT]] ]
113 ; CHECK-NEXT: ret i1 [[RES]]
116 %cmp7 = icmp eq i64 %count, 0
117 br i1 %cmp7, label %cleanup, label %for.body
119 for.body: ; preds = %entry, %for.inc
120 %i.010 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
121 %ptr1.addr.09 = phi i8* [ %add.ptr2, %for.inc ], [ %ptr1, %entry ]
122 %ptr0.addr.08 = phi i8* [ %add.ptr, %for.inc ], [ %ptr0, %entry ]
123 %bcmp = tail call i32 @bcmp(i8* %ptr0.addr.08, i8* %ptr1.addr.09, i64 32)
124 %cmp1 = icmp eq i32 %bcmp, 0
125 br i1 %cmp1, label %for.inc, label %cleanup
127 for.inc: ; preds = %for.body
128 %inc = add nuw i64 %i.010, 1
129 %add.ptr = getelementptr inbounds i8, i8* %ptr0.addr.08, i64 16
130 %add.ptr2 = getelementptr inbounds i8, i8* %ptr1.addr.09, i64 16
131 %cmp = icmp ult i64 %inc, %count
132 br i1 %cmp, label %for.body, label %cleanup
134 cleanup: ; preds = %for.body, %for.inc, %entry
135 %res = phi i1 [ true, %entry ], [ true, %for.inc ], [ false, %for.body ]