1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -loop-reduce -S < %s | FileCheck %s
5 ; LSR should properly handle the post-inc offset when folding the
6 ; non-IV operand of an icmp into the IV.
8 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"
10 %struct.Vector2 = type { ptr, [64 x i16], i32 }
12 @.str = private unnamed_addr constant [37 x i8] c"0123456789abcdefghijklmnopqrstuvwxyz\00"
14 define void @_Z15IntegerToStringjjR7Vector2(i32 %i, i32 %radix, ptr nocapture %result) nounwind noinline {
15 ; CHECK-LABEL: @_Z15IntegerToStringjjR7Vector2(
17 ; CHECK-NEXT: [[BUFFER:%.*]] = alloca [33 x i16], align 16
18 ; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr [33 x i16], ptr [[BUFFER]], i64 0, i64 33
19 ; CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[ADD_PTR]] to i64
20 ; CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[ADD_PTR]] to i64
21 ; CHECK-NEXT: [[SCEVGEP3:%.*]] = getelementptr i8, ptr [[BUFFER]], i64 64
22 ; CHECK-NEXT: br label [[DO_BODY:%.*]]
24 ; CHECK-NEXT: [[LSR_IV10:%.*]] = phi i64 [ [[LSR_IV_NEXT11:%.*]], [[DO_BODY]] ], [ -1, [[ENTRY:%.*]] ]
25 ; CHECK-NEXT: [[LSR_IV8:%.*]] = phi ptr [ [[SCEVGEP9:%.*]], [[DO_BODY]] ], [ [[ADD_PTR]], [[ENTRY]] ]
26 ; CHECK-NEXT: [[LSR_IV4:%.*]] = phi ptr [ [[SCEVGEP5:%.*]], [[DO_BODY]] ], [ [[SCEVGEP3]], [[ENTRY]] ]
27 ; CHECK-NEXT: [[I_ADDR_0:%.*]] = phi i32 [ [[DIV:%.*]], [[DO_BODY]] ], [ [[I:%.*]], [[ENTRY]] ]
28 ; CHECK-NEXT: [[REM:%.*]] = urem i32 [[I_ADDR_0]], 10
29 ; CHECK-NEXT: [[DIV]] = udiv i32 [[I_ADDR_0]], 10
30 ; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[REM]] to i64
31 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [37 x i8], ptr @.str, i64 0, i64 [[IDXPROM]]
32 ; CHECK-NEXT: [[INST5:%.*]] = load i8, ptr [[ARRAYIDX]], align 1
33 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[INST5]] to i16
34 ; CHECK-NEXT: store i16 [[CONV]], ptr [[LSR_IV4]], align 2
35 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt i32 [[I_ADDR_0]], 9
36 ; CHECK-NEXT: [[SCEVGEP5]] = getelementptr i8, ptr [[LSR_IV4]], i64 -2
37 ; CHECK-NEXT: [[SCEVGEP9]] = getelementptr i8, ptr [[LSR_IV8]], i64 -2
38 ; CHECK-NEXT: [[LSR_IV_NEXT11]] = add i64 [[LSR_IV10]], 1
39 ; CHECK-NEXT: br i1 [[TMP0]], label [[DO_BODY]], label [[DO_END:%.*]]
41 ; CHECK-NEXT: [[SCEVGEP9_LCSSA:%.*]] = phi ptr [ [[SCEVGEP9]], [[DO_BODY]] ]
42 ; CHECK-NEXT: [[XAP_0:%.*]] = inttoptr i64 [[LSR_IV_NEXT11]] to ptr
43 ; CHECK-NEXT: [[CAP_0:%.*]] = ptrtoint ptr [[XAP_0]] to i64
44 ; CHECK-NEXT: [[SUB_PTR_SUB:%.*]] = sub i64 [[SUB_PTR_LHS_CAST]], [[SUB_PTR_RHS_CAST]]
45 ; CHECK-NEXT: [[SUB_PTR_DIV39:%.*]] = lshr exact i64 [[SUB_PTR_SUB]], 1
46 ; CHECK-NEXT: [[CONV11:%.*]] = trunc i64 [[SUB_PTR_DIV39]] to i32
47 ; CHECK-NEXT: [[MLENGTH:%.*]] = getelementptr inbounds [[STRUCT_VECTOR2:%.*]], ptr [[RESULT:%.*]], i64 0, i32 2
48 ; CHECK-NEXT: [[IDX_EXT21:%.*]] = bitcast i64 [[SUB_PTR_DIV39]] to i64
49 ; CHECK-NEXT: [[CMP2740:%.*]] = icmp eq i64 [[IDX_EXT21]], 0
50 ; CHECK-NEXT: br i1 [[CMP2740]], label [[FOR_END:%.*]], label [[FOR_BODY_LR_PH:%.*]]
51 ; CHECK: for.body.lr.ph:
52 ; CHECK-NEXT: [[INST16:%.*]] = load i32, ptr [[MLENGTH]], align 4
53 ; CHECK-NEXT: [[INST14:%.*]] = load ptr, ptr [[RESULT]], align 8
54 ; CHECK-NEXT: [[INST48:%.*]] = zext i32 [[INST16]] to i64
55 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[INST48]], 1
56 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[INST14]], i64 [[TMP1]]
57 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
59 ; CHECK-NEXT: [[LSR_IV6:%.*]] = phi ptr [ [[SCEVGEP7:%.*]], [[FOR_BODY]] ], [ [[SCEVGEP9_LCSSA]], [[FOR_BODY_LR_PH]] ]
60 ; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[FOR_BODY_LR_PH]] ]
61 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[SCEVGEP]], i64 [[LSR_IV]]
62 ; CHECK-NEXT: [[INST29:%.*]] = load i16, ptr [[LSR_IV6]], align 2
63 ; CHECK-NEXT: store i16 [[INST29]], ptr [[SCEVGEP1]], align 2
64 ; CHECK-NEXT: [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], 2
65 ; CHECK-NEXT: [[LSR_IV_NEXT2:%.*]] = inttoptr i64 [[LSR_IV_NEXT]] to ptr
66 ; CHECK-NEXT: [[SCEVGEP7]] = getelementptr i8, ptr [[LSR_IV6]], i64 2
67 ; CHECK-NEXT: [[CMP27:%.*]] = icmp eq ptr [[LSR_IV_NEXT2]], null
68 ; CHECK-NEXT: br i1 [[CMP27]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY]]
69 ; CHECK: for.end.loopexit:
70 ; CHECK-NEXT: br label [[FOR_END]]
72 ; CHECK-NEXT: [[INST38:%.*]] = load i32, ptr [[MLENGTH]], align 4
73 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[INST38]], [[CONV11]]
74 ; CHECK-NEXT: store i32 [[ADD]], ptr [[MLENGTH]], align 4
75 ; CHECK-NEXT: ret void
78 %buffer = alloca [33 x i16], align 16
79 %add.ptr = getelementptr inbounds [33 x i16], ptr %buffer, i64 0, i64 33
80 %sub.ptr.lhs.cast = ptrtoint ptr %add.ptr to i64
81 %sub.ptr.rhs.cast = ptrtoint ptr %add.ptr to i64
84 do.body: ; preds = %do.body, %entry
85 %0 = phi i64 [ %indvar.next44, %do.body ], [ 0, %entry ]
86 %i.addr.0 = phi i32 [ %div, %do.body ], [ %i, %entry ]
87 %inst51 = sub i64 32, %0
88 %incdec.ptr = getelementptr [33 x i16], ptr %buffer, i64 0, i64 %inst51
89 %rem = urem i32 %i.addr.0, 10
90 %div = udiv i32 %i.addr.0, 10
91 %idxprom = zext i32 %rem to i64
92 %arrayidx = getelementptr inbounds [37 x i8], ptr @.str, i64 0, i64 %idxprom
93 %inst5 = load i8, ptr %arrayidx, align 1
94 %conv = sext i8 %inst5 to i16
95 store i16 %conv, ptr %incdec.ptr, align 2
96 %1 = icmp ugt i32 %i.addr.0, 9
97 %indvar.next44 = add i64 %0, 1
98 br i1 %1, label %do.body, label %do.end
100 do.end: ; preds = %do.body
101 %xap.0 = inttoptr i64 %0 to ptr
102 %cap.0 = ptrtoint ptr %xap.0 to i64
103 %sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
104 %sub.ptr.div39 = lshr exact i64 %sub.ptr.sub, 1
105 %conv11 = trunc i64 %sub.ptr.div39 to i32
106 %mLength = getelementptr inbounds %struct.Vector2, ptr %result, i64 0, i32 2
107 %idx.ext21 = bitcast i64 %sub.ptr.div39 to i64
108 %incdec.ptr.sum = add i64 %idx.ext21, -1
109 %cp.0.sum = sub i64 %incdec.ptr.sum, %0
110 %add.ptr22 = getelementptr [33 x i16], ptr %buffer, i64 1, i64 %cp.0.sum
111 %cmp2740 = icmp eq i64 %idx.ext21, 0
112 br i1 %cmp2740, label %for.end, label %for.body.lr.ph
114 for.body.lr.ph: ; preds = %do.end
115 %inst16 = load i32, ptr %mLength, align 4
116 %inst14 = load ptr, ptr %result, align 8
117 %inst48 = zext i32 %inst16 to i64
120 for.body: ; preds = %for.body, %for.body.lr.ph
121 %indvar = phi i64 [ 0, %for.body.lr.ph ], [ %indvar.next, %for.body ]
122 %inst46 = add i64 %inst51, %indvar
123 %p.042 = getelementptr [33 x i16], ptr %buffer, i64 0, i64 %inst46
124 %inst47 = sub i64 %indvar, %0
125 %incdec.ptr32 = getelementptr [33 x i16], ptr %buffer, i64 1, i64 %inst47
126 %inst49 = add i64 %inst48, %indvar
127 %dst.041 = getelementptr i16, ptr %inst14, i64 %inst49
128 %inst29 = load i16, ptr %p.042, align 2
129 store i16 %inst29, ptr %dst.041, align 2
130 %cmp27 = icmp eq ptr %incdec.ptr32, %add.ptr22
131 %indvar.next = add i64 %indvar, 1
132 br i1 %cmp27, label %for.end.loopexit, label %for.body
134 for.end.loopexit: ; preds = %for.body
137 for.end: ; preds = %for.end.loopexit, %do.end
138 %inst38 = load i32, ptr %mLength, align 4
139 %add = add i32 %inst38, %conv11
140 store i32 %add, ptr %mLength, align 4