10c09e5bdSPhilip Reames; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
20c09e5bdSPhilip Reames; RUN: opt -loop-versioning -S < %s | FileCheck %s -check-prefix=LV
30c09e5bdSPhilip Reames
40c09e5bdSPhilip Reamestarget datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
50c09e5bdSPhilip Reames
60c09e5bdSPhilip Reames; For this loop:
70c09e5bdSPhilip Reames;   unsigned index = 0;
80c09e5bdSPhilip Reames;   for (int i = 0; i < n; i++) {
90c09e5bdSPhilip Reames;    A[2 * index] = A[2 * index] + B[i];
100c09e5bdSPhilip Reames;    index++;
110c09e5bdSPhilip Reames;   }
120c09e5bdSPhilip Reames;
130c09e5bdSPhilip Reames; SCEV is unable to prove that A[2 * i] does not overflow.
140c09e5bdSPhilip Reames;
150c09e5bdSPhilip Reames; Analyzing the IR does not help us because the GEPs are not
160c09e5bdSPhilip Reames; affine AddRecExprs. However, we can turn them into AddRecExprs
170c09e5bdSPhilip Reames; using SCEV Predicates.
180c09e5bdSPhilip Reames;
190c09e5bdSPhilip Reames; Once we have an affine expression we need to add an additional NUSW
200c09e5bdSPhilip Reames; to check that the pointers don't wrap since the GEPs are not
210c09e5bdSPhilip Reames; inbound.
220c09e5bdSPhilip Reames
230c09e5bdSPhilip Reames; The expression for %mul_ext as analyzed by SCEV is
240c09e5bdSPhilip Reames;    (zext i32 {0,+,2}<%for.body> to i64)
250c09e5bdSPhilip Reames; We have added the nusw flag to turn this expression into the SCEV expression:
260c09e5bdSPhilip Reames;    i64 {0,+,2}<%for.body>
270c09e5bdSPhilip Reames
280c09e5bdSPhilip Reamesdefine void @f1(i16* noalias %a,
290c09e5bdSPhilip Reames; LV-LABEL: @f1(
300c09e5bdSPhilip Reames; LV-NEXT:  for.body.lver.check:
31e735f2bfSPhilip Reames; LV-NEXT:    [[A5:%.*]] = bitcast i16* [[A:%.*]] to i8*
320c09e5bdSPhilip Reames; LV-NEXT:    [[TMP0:%.*]] = add i64 [[N:%.*]], -1
33b2915971SRoman Lebedev; LV-NEXT:    [[TMP7:%.*]] = icmp ugt i64 [[TMP0]], 4294967295
34e735f2bfSPhilip Reames; LV-NEXT:    [[MUL2:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 4, i64 [[TMP0]])
35e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_RESULT3:%.*]] = extractvalue { i64, i1 } [[MUL2]], 0
36e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW4:%.*]] = extractvalue { i64, i1 } [[MUL2]], 1
37b2915971SRoman Lebedev; LV-NEXT:    [[TMP11:%.*]] = sub i64 0, [[MUL_RESULT3]]
38b2915971SRoman Lebedev; LV-NEXT:    [[TMP12:%.*]] = getelementptr i8, i8* [[A5]], i64 [[MUL_RESULT3]]
39b2915971SRoman Lebedev; LV-NEXT:    [[TMP15:%.*]] = icmp ult i8* [[TMP12]], [[A5]]
40f395a4f8SFlorian Hahn; LV-NEXT:    [[TMP17:%.*]] = or i1 [[TMP15]], [[MUL_OVERFLOW4]]
41f0ef1ea6SFlorian Hahn; LV-NEXT:    [[TMP18:%.*]] = or i1 [[TMP7]], [[TMP17]]
42b2915971SRoman Lebedev; LV-NEXT:    br i1 [[TMP18]], label [[FOR_BODY_PH_LVER_ORIG:%.*]], label [[FOR_BODY_PH:%.*]]
430c09e5bdSPhilip Reames; LV:       for.body.ph.lver.orig:
440c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY_LVER_ORIG:%.*]]
450c09e5bdSPhilip Reames; LV:       for.body.lver.orig:
460c09e5bdSPhilip Reames; LV-NEXT:    [[IND_LVER_ORIG:%.*]] = phi i64 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
470c09e5bdSPhilip Reames; LV-NEXT:    [[IND1_LVER_ORIG:%.*]] = phi i32 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC1_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
480c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_LVER_ORIG:%.*]] = mul i32 [[IND1_LVER_ORIG]], 2
490c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT_LVER_ORIG:%.*]] = zext i32 [[MUL_LVER_ORIG]] to i64
500c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA_LVER_ORIG:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT_LVER_ORIG]]
510c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXA_LVER_ORIG]], align 2
520c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB_LVER_ORIG:%.*]] = getelementptr i16, i16* [[B:%.*]], i64 [[IND_LVER_ORIG]]
530c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXB_LVER_ORIG]], align 2
540c09e5bdSPhilip Reames; LV-NEXT:    [[ADD_LVER_ORIG:%.*]] = mul i16 [[LOADA_LVER_ORIG]], [[LOADB_LVER_ORIG]]
550c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD_LVER_ORIG]], i16* [[ARRAYIDXA_LVER_ORIG]], align 2
560c09e5bdSPhilip Reames; LV-NEXT:    [[INC_LVER_ORIG]] = add nuw nsw i64 [[IND_LVER_ORIG]], 1
570c09e5bdSPhilip Reames; LV-NEXT:    [[INC1_LVER_ORIG]] = add i32 [[IND1_LVER_ORIG]], 1
580c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND_LVER_ORIG:%.*]] = icmp eq i64 [[INC_LVER_ORIG]], [[N]]
590c09e5bdSPhilip Reames; LV-NEXT:    br i1 [[EXITCOND_LVER_ORIG]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY_LVER_ORIG]]
600c09e5bdSPhilip Reames; LV:       for.body.ph:
610c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY:%.*]]
620c09e5bdSPhilip Reames; LV:       for.body:
630c09e5bdSPhilip Reames; LV-NEXT:    [[IND:%.*]] = phi i64 [ 0, [[FOR_BODY_PH]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
640c09e5bdSPhilip Reames; LV-NEXT:    [[IND1:%.*]] = phi i32 [ 0, [[FOR_BODY_PH]] ], [ [[INC1:%.*]], [[FOR_BODY]] ]
650c09e5bdSPhilip Reames; LV-NEXT:    [[MUL:%.*]] = mul i32 [[IND1]], 2
660c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT:%.*]] = zext i32 [[MUL]] to i64
670c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT]]
680c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA:%.*]] = load i16, i16* [[ARRAYIDXA]], align 2
690c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB:%.*]] = getelementptr i16, i16* [[B]], i64 [[IND]]
700c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB:%.*]] = load i16, i16* [[ARRAYIDXB]], align 2
710c09e5bdSPhilip Reames; LV-NEXT:    [[ADD:%.*]] = mul i16 [[LOADA]], [[LOADB]]
720c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD]], i16* [[ARRAYIDXA]], align 2
730c09e5bdSPhilip Reames; LV-NEXT:    [[INC]] = add nuw nsw i64 [[IND]], 1
740c09e5bdSPhilip Reames; LV-NEXT:    [[INC1]] = add i32 [[IND1]], 1
750c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INC]], [[N]]
76ad1b8772SFlorian Hahn; LV-NEXT:    br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT3:%.*]], label [[FOR_BODY]]
770c09e5bdSPhilip Reames; LV:       for.end.loopexit:
780c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END:%.*]]
79ad1b8772SFlorian Hahn; LV:       for.end.loopexit3:
800c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END]]
810c09e5bdSPhilip Reames; LV:       for.end:
820c09e5bdSPhilip Reames; LV-NEXT:    ret void
830c09e5bdSPhilip Reames;
840c09e5bdSPhilip Reames  i16* noalias %b, i64 %N) {
850c09e5bdSPhilip Reamesentry:
860c09e5bdSPhilip Reames  br label %for.body
870c09e5bdSPhilip Reames
880c09e5bdSPhilip Reamesfor.body:                                         ; preds = %for.body, %entry
890c09e5bdSPhilip Reames  %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
900c09e5bdSPhilip Reames  %ind1 = phi i32 [ 0, %entry ], [ %inc1, %for.body ]
910c09e5bdSPhilip Reames
920c09e5bdSPhilip Reames  %mul = mul i32 %ind1, 2
930c09e5bdSPhilip Reames  %mul_ext = zext i32 %mul to i64
940c09e5bdSPhilip Reames
950c09e5bdSPhilip Reames  %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
960c09e5bdSPhilip Reames  %loadA = load i16, i16* %arrayidxA, align 2
970c09e5bdSPhilip Reames
980c09e5bdSPhilip Reames  %arrayidxB = getelementptr i16, i16* %b, i64 %ind
990c09e5bdSPhilip Reames  %loadB = load i16, i16* %arrayidxB, align 2
1000c09e5bdSPhilip Reames
1010c09e5bdSPhilip Reames  %add = mul i16 %loadA, %loadB
1020c09e5bdSPhilip Reames
1030c09e5bdSPhilip Reames  store i16 %add, i16* %arrayidxA, align 2
1040c09e5bdSPhilip Reames
1050c09e5bdSPhilip Reames  %inc = add nuw nsw i64 %ind, 1
1060c09e5bdSPhilip Reames  %inc1 = add i32 %ind1, 1
1070c09e5bdSPhilip Reames
1080c09e5bdSPhilip Reames  %exitcond = icmp eq i64 %inc, %N
1090c09e5bdSPhilip Reames  br i1 %exitcond, label %for.end, label %for.body
1100c09e5bdSPhilip Reames
1110c09e5bdSPhilip Reamesfor.end:                                          ; preds = %for.body
1120c09e5bdSPhilip Reames  ret void
1130c09e5bdSPhilip Reames}
1140c09e5bdSPhilip Reames
1150c09e5bdSPhilip Reames; For this loop:
1160c09e5bdSPhilip Reames;   unsigned index = n;
1170c09e5bdSPhilip Reames;   for (int i = 0; i < n; i++) {
1180c09e5bdSPhilip Reames;    A[2 * index] = A[2 * index] + B[i];
1190c09e5bdSPhilip Reames;    index--;
1200c09e5bdSPhilip Reames;   }
1210c09e5bdSPhilip Reames;
1220c09e5bdSPhilip Reames; the SCEV expression for 2 * index is not an AddRecExpr
1230c09e5bdSPhilip Reames; (and implictly not affine). However, we are able to make assumptions
1240c09e5bdSPhilip Reames; that will turn the expression into an affine one and continue the
1250c09e5bdSPhilip Reames; analysis.
1260c09e5bdSPhilip Reames;
1270c09e5bdSPhilip Reames; Once we have an affine expression we need to add an additional NUSW
1280c09e5bdSPhilip Reames; to check that the pointers don't wrap since the GEPs are not
1290c09e5bdSPhilip Reames; inbounds.
1300c09e5bdSPhilip Reames;
1310c09e5bdSPhilip Reames; This loop has a negative stride for A, and the nusw flag is required in
1320c09e5bdSPhilip Reames; order to properly extend the increment from i32 -4 to i64 -4.
1330c09e5bdSPhilip Reames
1340c09e5bdSPhilip Reames; The expression for %mul_ext as analyzed by SCEV is
1350c09e5bdSPhilip Reames;     (zext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)
1360c09e5bdSPhilip Reames; We have added the nusw flag to turn this expression into the following SCEV:
1370c09e5bdSPhilip Reames;     i64 {zext i32 (2 * (trunc i64 %N to i32)) to i64,+,-2}<%for.body>
1380c09e5bdSPhilip Reames
1390c09e5bdSPhilip Reamesdefine void @f2(i16* noalias %a,
1400c09e5bdSPhilip Reames; LV-LABEL: @f2(
1410c09e5bdSPhilip Reames; LV-NEXT:  for.body.lver.check:
1420c09e5bdSPhilip Reames; LV-NEXT:    [[TRUNCN:%.*]] = trunc i64 [[N:%.*]] to i32
1430c09e5bdSPhilip Reames; LV-NEXT:    [[TMP0:%.*]] = add i64 [[N]], -1
1440c09e5bdSPhilip Reames; LV-NEXT:    [[TMP1:%.*]] = shl i32 [[TRUNCN]], 1
1450c09e5bdSPhilip Reames; LV-NEXT:    [[TMP2:%.*]] = trunc i64 [[TMP0]] to i32
1460c09e5bdSPhilip Reames; LV-NEXT:    [[MUL1:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 2, i32 [[TMP2]])
1470c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_RESULT:%.*]] = extractvalue { i32, i1 } [[MUL1]], 0
1480c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW:%.*]] = extractvalue { i32, i1 } [[MUL1]], 1
1490c09e5bdSPhilip Reames; LV-NEXT:    [[TMP4:%.*]] = sub i32 [[TMP1]], [[MUL_RESULT]]
1500c09e5bdSPhilip Reames; LV-NEXT:    [[TMP5:%.*]] = icmp ugt i32 [[TMP4]], [[TMP1]]
1517f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP9:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
152b2915971SRoman Lebedev; LV-NEXT:    [[TMP8:%.*]] = icmp ugt i64 [[TMP0]], 4294967295
1537f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP10:%.*]] = or i1 [[TMP9]], [[TMP8]]
154b2915971SRoman Lebedev; LV-NEXT:    [[TMP12:%.*]] = trunc i64 [[N]] to i31
155b2915971SRoman Lebedev; LV-NEXT:    [[TMP13:%.*]] = zext i31 [[TMP12]] to i64
156b2915971SRoman Lebedev; LV-NEXT:    [[TMP14:%.*]] = shl nuw nsw i64 [[TMP13]], 1
157b2915971SRoman Lebedev; LV-NEXT:    [[SCEVGEP:%.*]] = getelementptr i16, i16* [[A:%.*]], i64 [[TMP14]]
158e735f2bfSPhilip Reames; LV-NEXT:    [[MUL2:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 4, i64 [[TMP0]])
159e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_RESULT3:%.*]] = extractvalue { i64, i1 } [[MUL2]], 0
160e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW4:%.*]] = extractvalue { i64, i1 } [[MUL2]], 1
161e735f2bfSPhilip Reames; LV-NEXT:    [[SCEVGEP5:%.*]] = bitcast i16* [[SCEVGEP]] to i8*
162b2915971SRoman Lebedev; LV-NEXT:    [[TMP15:%.*]] = sub i64 0, [[MUL_RESULT3]]
163b2915971SRoman Lebedev; LV-NEXT:    [[TMP17:%.*]] = getelementptr i8, i8* [[SCEVGEP5]], i64 [[TMP15]]
164b2915971SRoman Lebedev; LV-NEXT:    [[TMP18:%.*]] = icmp ugt i8* [[TMP17]], [[SCEVGEP5]]
165f395a4f8SFlorian Hahn; LV-NEXT:    [[TMP21:%.*]] = or i1 [[TMP18]], [[MUL_OVERFLOW4]]
16686d113a8SFlorian Hahn; LV-NEXT:    [[TMP22:%.*]] = or i1 [[TMP10]], [[TMP21]]
167b2915971SRoman Lebedev; LV-NEXT:    br i1 [[TMP22]], label [[FOR_BODY_PH_LVER_ORIG:%.*]], label [[FOR_BODY_PH:%.*]]
1680c09e5bdSPhilip Reames; LV:       for.body.ph.lver.orig:
1690c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY_LVER_ORIG:%.*]]
1700c09e5bdSPhilip Reames; LV:       for.body.lver.orig:
1710c09e5bdSPhilip Reames; LV-NEXT:    [[IND_LVER_ORIG:%.*]] = phi i64 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
1720c09e5bdSPhilip Reames; LV-NEXT:    [[IND1_LVER_ORIG:%.*]] = phi i32 [ [[TRUNCN]], [[FOR_BODY_PH_LVER_ORIG]] ], [ [[DEC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
1730c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_LVER_ORIG:%.*]] = mul i32 [[IND1_LVER_ORIG]], 2
1740c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT_LVER_ORIG:%.*]] = zext i32 [[MUL_LVER_ORIG]] to i64
1750c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA_LVER_ORIG:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT_LVER_ORIG]]
1760c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXA_LVER_ORIG]], align 2
1770c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB_LVER_ORIG:%.*]] = getelementptr i16, i16* [[B:%.*]], i64 [[IND_LVER_ORIG]]
1780c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXB_LVER_ORIG]], align 2
1790c09e5bdSPhilip Reames; LV-NEXT:    [[ADD_LVER_ORIG:%.*]] = mul i16 [[LOADA_LVER_ORIG]], [[LOADB_LVER_ORIG]]
1800c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD_LVER_ORIG]], i16* [[ARRAYIDXA_LVER_ORIG]], align 2
1810c09e5bdSPhilip Reames; LV-NEXT:    [[INC_LVER_ORIG]] = add nuw nsw i64 [[IND_LVER_ORIG]], 1
1820c09e5bdSPhilip Reames; LV-NEXT:    [[DEC_LVER_ORIG]] = sub i32 [[IND1_LVER_ORIG]], 1
1830c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND_LVER_ORIG:%.*]] = icmp eq i64 [[INC_LVER_ORIG]], [[N]]
1840c09e5bdSPhilip Reames; LV-NEXT:    br i1 [[EXITCOND_LVER_ORIG]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY_LVER_ORIG]]
1850c09e5bdSPhilip Reames; LV:       for.body.ph:
1860c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY:%.*]]
1870c09e5bdSPhilip Reames; LV:       for.body:
1880c09e5bdSPhilip Reames; LV-NEXT:    [[IND:%.*]] = phi i64 [ 0, [[FOR_BODY_PH]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
1890c09e5bdSPhilip Reames; LV-NEXT:    [[IND1:%.*]] = phi i32 [ [[TRUNCN]], [[FOR_BODY_PH]] ], [ [[DEC:%.*]], [[FOR_BODY]] ]
1900c09e5bdSPhilip Reames; LV-NEXT:    [[MUL:%.*]] = mul i32 [[IND1]], 2
1910c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT:%.*]] = zext i32 [[MUL]] to i64
1920c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT]]
1930c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA:%.*]] = load i16, i16* [[ARRAYIDXA]], align 2
1940c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB:%.*]] = getelementptr i16, i16* [[B]], i64 [[IND]]
1950c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB:%.*]] = load i16, i16* [[ARRAYIDXB]], align 2
1960c09e5bdSPhilip Reames; LV-NEXT:    [[ADD:%.*]] = mul i16 [[LOADA]], [[LOADB]]
1970c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD]], i16* [[ARRAYIDXA]], align 2
1980c09e5bdSPhilip Reames; LV-NEXT:    [[INC]] = add nuw nsw i64 [[IND]], 1
1990c09e5bdSPhilip Reames; LV-NEXT:    [[DEC]] = sub i32 [[IND1]], 1
2000c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INC]], [[N]]
201c86e1ce7SNikita Popov; LV-NEXT:    br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT6:%.*]], label [[FOR_BODY]]
2020c09e5bdSPhilip Reames; LV:       for.end.loopexit:
2030c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END:%.*]]
204c86e1ce7SNikita Popov; LV:       for.end.loopexit6:
2050c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END]]
2060c09e5bdSPhilip Reames; LV:       for.end:
2070c09e5bdSPhilip Reames; LV-NEXT:    ret void
2080c09e5bdSPhilip Reames;
2090c09e5bdSPhilip Reames  i16* noalias %b, i64 %N) {
2100c09e5bdSPhilip Reamesentry:
2110c09e5bdSPhilip Reames  %TruncN = trunc i64 %N to i32
2120c09e5bdSPhilip Reames  br label %for.body
2130c09e5bdSPhilip Reames
2140c09e5bdSPhilip Reamesfor.body:                                         ; preds = %for.body, %entry
2150c09e5bdSPhilip Reames  %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
2160c09e5bdSPhilip Reames  %ind1 = phi i32 [ %TruncN, %entry ], [ %dec, %for.body ]
2170c09e5bdSPhilip Reames
2180c09e5bdSPhilip Reames  %mul = mul i32 %ind1, 2
2190c09e5bdSPhilip Reames  %mul_ext = zext i32 %mul to i64
2200c09e5bdSPhilip Reames
2210c09e5bdSPhilip Reames  %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
2220c09e5bdSPhilip Reames  %loadA = load i16, i16* %arrayidxA, align 2
2230c09e5bdSPhilip Reames
2240c09e5bdSPhilip Reames  %arrayidxB = getelementptr i16, i16* %b, i64 %ind
2250c09e5bdSPhilip Reames  %loadB = load i16, i16* %arrayidxB, align 2
2260c09e5bdSPhilip Reames
2270c09e5bdSPhilip Reames  %add = mul i16 %loadA, %loadB
2280c09e5bdSPhilip Reames
2290c09e5bdSPhilip Reames  store i16 %add, i16* %arrayidxA, align 2
2300c09e5bdSPhilip Reames
2310c09e5bdSPhilip Reames  %inc = add nuw nsw i64 %ind, 1
2320c09e5bdSPhilip Reames  %dec = sub i32 %ind1, 1
2330c09e5bdSPhilip Reames
2340c09e5bdSPhilip Reames  %exitcond = icmp eq i64 %inc, %N
2350c09e5bdSPhilip Reames  br i1 %exitcond, label %for.end, label %for.body
2360c09e5bdSPhilip Reames
2370c09e5bdSPhilip Reamesfor.end:                                          ; preds = %for.body
2380c09e5bdSPhilip Reames  ret void
2390c09e5bdSPhilip Reames}
2400c09e5bdSPhilip Reames
2410c09e5bdSPhilip Reames; We replicate the tests above, but this time sign extend 2 * index instead
2420c09e5bdSPhilip Reames; of zero extending it.
2430c09e5bdSPhilip Reames
2440c09e5bdSPhilip Reames; The expression for %mul_ext as analyzed by SCEV is
2450c09e5bdSPhilip Reames;     i64 (sext i32 {0,+,2}<%for.body> to i64)
2460c09e5bdSPhilip Reames; We have added the nssw flag to turn this expression into the following SCEV:
2470c09e5bdSPhilip Reames;     i64 {0,+,2}<%for.body>
2480c09e5bdSPhilip Reames
2490c09e5bdSPhilip Reamesdefine void @f3(i16* noalias %a,
2500c09e5bdSPhilip Reames; LV-LABEL: @f3(
2510c09e5bdSPhilip Reames; LV-NEXT:  for.body.lver.check:
252e735f2bfSPhilip Reames; LV-NEXT:    [[A5:%.*]] = bitcast i16* [[A:%.*]] to i8*
2530c09e5bdSPhilip Reames; LV-NEXT:    [[TMP0:%.*]] = add i64 [[N:%.*]], -1
2540c09e5bdSPhilip Reames; LV-NEXT:    [[TMP1:%.*]] = trunc i64 [[TMP0]] to i32
2550c09e5bdSPhilip Reames; LV-NEXT:    [[MUL1:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 2, i32 [[TMP1]])
2560c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_RESULT:%.*]] = extractvalue { i32, i1 } [[MUL1]], 0
2570c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW:%.*]] = extractvalue { i32, i1 } [[MUL1]], 1
258*7e680613SFlorian Hahn; LV-NEXT:    [[TMP5:%.*]] = icmp slt i32 [[MUL_RESULT]], 0
2597f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP8:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
260b2915971SRoman Lebedev; LV-NEXT:    [[TMP7:%.*]] = icmp ugt i64 [[TMP0]], 4294967295
2617f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP9:%.*]] = or i1 [[TMP8]], [[TMP7]]
262e735f2bfSPhilip Reames; LV-NEXT:    [[MUL2:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 4, i64 [[TMP0]])
263e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_RESULT3:%.*]] = extractvalue { i64, i1 } [[MUL2]], 0
264e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW4:%.*]] = extractvalue { i64, i1 } [[MUL2]], 1
265b2915971SRoman Lebedev; LV-NEXT:    [[TMP11:%.*]] = sub i64 0, [[MUL_RESULT3]]
266b2915971SRoman Lebedev; LV-NEXT:    [[TMP12:%.*]] = getelementptr i8, i8* [[A5]], i64 [[MUL_RESULT3]]
267b2915971SRoman Lebedev; LV-NEXT:    [[TMP15:%.*]] = icmp ult i8* [[TMP12]], [[A5]]
268f395a4f8SFlorian Hahn; LV-NEXT:    [[TMP17:%.*]] = or i1 [[TMP15]], [[MUL_OVERFLOW4]]
26986d113a8SFlorian Hahn; LV-NEXT:    [[TMP18:%.*]] = or i1 [[TMP9]], [[TMP17]]
270b2915971SRoman Lebedev; LV-NEXT:    br i1 [[TMP18]], label [[FOR_BODY_PH_LVER_ORIG:%.*]], label [[FOR_BODY_PH:%.*]]
2710c09e5bdSPhilip Reames; LV:       for.body.ph.lver.orig:
2720c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY_LVER_ORIG:%.*]]
2730c09e5bdSPhilip Reames; LV:       for.body.lver.orig:
2740c09e5bdSPhilip Reames; LV-NEXT:    [[IND_LVER_ORIG:%.*]] = phi i64 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
2750c09e5bdSPhilip Reames; LV-NEXT:    [[IND1_LVER_ORIG:%.*]] = phi i32 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC1_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
2760c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_LVER_ORIG:%.*]] = mul i32 [[IND1_LVER_ORIG]], 2
2770c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT_LVER_ORIG:%.*]] = sext i32 [[MUL_LVER_ORIG]] to i64
2780c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA_LVER_ORIG:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT_LVER_ORIG]]
2790c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXA_LVER_ORIG]], align 2
2800c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB_LVER_ORIG:%.*]] = getelementptr i16, i16* [[B:%.*]], i64 [[IND_LVER_ORIG]]
2810c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXB_LVER_ORIG]], align 2
2820c09e5bdSPhilip Reames; LV-NEXT:    [[ADD_LVER_ORIG:%.*]] = mul i16 [[LOADA_LVER_ORIG]], [[LOADB_LVER_ORIG]]
2830c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD_LVER_ORIG]], i16* [[ARRAYIDXA_LVER_ORIG]], align 2
2840c09e5bdSPhilip Reames; LV-NEXT:    [[INC_LVER_ORIG]] = add nuw nsw i64 [[IND_LVER_ORIG]], 1
2850c09e5bdSPhilip Reames; LV-NEXT:    [[INC1_LVER_ORIG]] = add i32 [[IND1_LVER_ORIG]], 1
2860c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND_LVER_ORIG:%.*]] = icmp eq i64 [[INC_LVER_ORIG]], [[N]]
2870c09e5bdSPhilip Reames; LV-NEXT:    br i1 [[EXITCOND_LVER_ORIG]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY_LVER_ORIG]]
2880c09e5bdSPhilip Reames; LV:       for.body.ph:
2890c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY:%.*]]
2900c09e5bdSPhilip Reames; LV:       for.body:
2910c09e5bdSPhilip Reames; LV-NEXT:    [[IND:%.*]] = phi i64 [ 0, [[FOR_BODY_PH]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
2920c09e5bdSPhilip Reames; LV-NEXT:    [[IND1:%.*]] = phi i32 [ 0, [[FOR_BODY_PH]] ], [ [[INC1:%.*]], [[FOR_BODY]] ]
2930c09e5bdSPhilip Reames; LV-NEXT:    [[MUL:%.*]] = mul i32 [[IND1]], 2
2940c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT:%.*]] = sext i32 [[MUL]] to i64
2950c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT]]
2960c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA:%.*]] = load i16, i16* [[ARRAYIDXA]], align 2
2970c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB:%.*]] = getelementptr i16, i16* [[B]], i64 [[IND]]
2980c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB:%.*]] = load i16, i16* [[ARRAYIDXB]], align 2
2990c09e5bdSPhilip Reames; LV-NEXT:    [[ADD:%.*]] = mul i16 [[LOADA]], [[LOADB]]
3000c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD]], i16* [[ARRAYIDXA]], align 2
3010c09e5bdSPhilip Reames; LV-NEXT:    [[INC]] = add nuw nsw i64 [[IND]], 1
3020c09e5bdSPhilip Reames; LV-NEXT:    [[INC1]] = add i32 [[IND1]], 1
3030c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INC]], [[N]]
304c86e1ce7SNikita Popov; LV-NEXT:    br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT6:%.*]], label [[FOR_BODY]]
3050c09e5bdSPhilip Reames; LV:       for.end.loopexit:
3060c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END:%.*]]
307c86e1ce7SNikita Popov; LV:       for.end.loopexit6:
3080c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END]]
3090c09e5bdSPhilip Reames; LV:       for.end:
3100c09e5bdSPhilip Reames; LV-NEXT:    ret void
3110c09e5bdSPhilip Reames;
3120c09e5bdSPhilip Reames  i16* noalias %b, i64 %N) {
3130c09e5bdSPhilip Reamesentry:
3140c09e5bdSPhilip Reames  br label %for.body
3150c09e5bdSPhilip Reames
3160c09e5bdSPhilip Reamesfor.body:                                         ; preds = %for.body, %entry
3170c09e5bdSPhilip Reames  %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
3180c09e5bdSPhilip Reames  %ind1 = phi i32 [ 0, %entry ], [ %inc1, %for.body ]
3190c09e5bdSPhilip Reames
3200c09e5bdSPhilip Reames  %mul = mul i32 %ind1, 2
3210c09e5bdSPhilip Reames  %mul_ext = sext i32 %mul to i64
3220c09e5bdSPhilip Reames
3230c09e5bdSPhilip Reames  %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
3240c09e5bdSPhilip Reames  %loadA = load i16, i16* %arrayidxA, align 2
3250c09e5bdSPhilip Reames
3260c09e5bdSPhilip Reames  %arrayidxB = getelementptr i16, i16* %b, i64 %ind
3270c09e5bdSPhilip Reames  %loadB = load i16, i16* %arrayidxB, align 2
3280c09e5bdSPhilip Reames
3290c09e5bdSPhilip Reames  %add = mul i16 %loadA, %loadB
3300c09e5bdSPhilip Reames
3310c09e5bdSPhilip Reames  store i16 %add, i16* %arrayidxA, align 2
3320c09e5bdSPhilip Reames
3330c09e5bdSPhilip Reames  %inc = add nuw nsw i64 %ind, 1
3340c09e5bdSPhilip Reames  %inc1 = add i32 %ind1, 1
3350c09e5bdSPhilip Reames
3360c09e5bdSPhilip Reames  %exitcond = icmp eq i64 %inc, %N
3370c09e5bdSPhilip Reames  br i1 %exitcond, label %for.end, label %for.body
3380c09e5bdSPhilip Reames
3390c09e5bdSPhilip Reamesfor.end:                                          ; preds = %for.body
3400c09e5bdSPhilip Reames  ret void
3410c09e5bdSPhilip Reames}
3420c09e5bdSPhilip Reames
3430c09e5bdSPhilip Reamesdefine void @f4(i16* noalias %a,
3440c09e5bdSPhilip Reames; LV-LABEL: @f4(
3450c09e5bdSPhilip Reames; LV-NEXT:  for.body.lver.check:
3460c09e5bdSPhilip Reames; LV-NEXT:    [[TRUNCN:%.*]] = trunc i64 [[N:%.*]] to i32
3470c09e5bdSPhilip Reames; LV-NEXT:    [[TMP0:%.*]] = add i64 [[N]], -1
3480c09e5bdSPhilip Reames; LV-NEXT:    [[TMP1:%.*]] = shl i32 [[TRUNCN]], 1
3490c09e5bdSPhilip Reames; LV-NEXT:    [[TMP2:%.*]] = trunc i64 [[TMP0]] to i32
3500c09e5bdSPhilip Reames; LV-NEXT:    [[MUL1:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 2, i32 [[TMP2]])
3510c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_RESULT:%.*]] = extractvalue { i32, i1 } [[MUL1]], 0
3520c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW:%.*]] = extractvalue { i32, i1 } [[MUL1]], 1
3530c09e5bdSPhilip Reames; LV-NEXT:    [[TMP4:%.*]] = sub i32 [[TMP1]], [[MUL_RESULT]]
3540c09e5bdSPhilip Reames; LV-NEXT:    [[TMP5:%.*]] = icmp sgt i32 [[TMP4]], [[TMP1]]
3557f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP9:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
356b2915971SRoman Lebedev; LV-NEXT:    [[TMP8:%.*]] = icmp ugt i64 [[TMP0]], 4294967295
3577f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP10:%.*]] = or i1 [[TMP9]], [[TMP8]]
358b2915971SRoman Lebedev; LV-NEXT:    [[TMP12:%.*]] = sext i32 [[TMP1]] to i64
359b2915971SRoman Lebedev; LV-NEXT:    [[SCEVGEP:%.*]] = getelementptr i16, i16* [[A:%.*]], i64 [[TMP12]]
360e735f2bfSPhilip Reames; LV-NEXT:    [[MUL2:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 4, i64 [[TMP0]])
361e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_RESULT3:%.*]] = extractvalue { i64, i1 } [[MUL2]], 0
362e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW4:%.*]] = extractvalue { i64, i1 } [[MUL2]], 1
363e735f2bfSPhilip Reames; LV-NEXT:    [[SCEVGEP5:%.*]] = bitcast i16* [[SCEVGEP]] to i8*
364b2915971SRoman Lebedev; LV-NEXT:    [[TMP13:%.*]] = sub i64 0, [[MUL_RESULT3]]
365b2915971SRoman Lebedev; LV-NEXT:    [[TMP15:%.*]] = getelementptr i8, i8* [[SCEVGEP5]], i64 [[TMP13]]
366b2915971SRoman Lebedev; LV-NEXT:    [[TMP16:%.*]] = icmp ugt i8* [[TMP15]], [[SCEVGEP5]]
367f395a4f8SFlorian Hahn; LV-NEXT:    [[TMP19:%.*]] = or i1 [[TMP16]], [[MUL_OVERFLOW4]]
36886d113a8SFlorian Hahn; LV-NEXT:    [[TMP20:%.*]] = or i1 [[TMP10]], [[TMP19]]
369b2915971SRoman Lebedev; LV-NEXT:    br i1 [[TMP20]], label [[FOR_BODY_PH_LVER_ORIG:%.*]], label [[FOR_BODY_PH:%.*]]
3700c09e5bdSPhilip Reames; LV:       for.body.ph.lver.orig:
3710c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY_LVER_ORIG:%.*]]
3720c09e5bdSPhilip Reames; LV:       for.body.lver.orig:
3730c09e5bdSPhilip Reames; LV-NEXT:    [[IND_LVER_ORIG:%.*]] = phi i64 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
3740c09e5bdSPhilip Reames; LV-NEXT:    [[IND1_LVER_ORIG:%.*]] = phi i32 [ [[TRUNCN]], [[FOR_BODY_PH_LVER_ORIG]] ], [ [[DEC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
3750c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_LVER_ORIG:%.*]] = mul i32 [[IND1_LVER_ORIG]], 2
3760c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT_LVER_ORIG:%.*]] = sext i32 [[MUL_LVER_ORIG]] to i64
3770c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA_LVER_ORIG:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT_LVER_ORIG]]
3780c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXA_LVER_ORIG]], align 2
3790c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB_LVER_ORIG:%.*]] = getelementptr i16, i16* [[B:%.*]], i64 [[IND_LVER_ORIG]]
3800c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXB_LVER_ORIG]], align 2
3810c09e5bdSPhilip Reames; LV-NEXT:    [[ADD_LVER_ORIG:%.*]] = mul i16 [[LOADA_LVER_ORIG]], [[LOADB_LVER_ORIG]]
3820c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD_LVER_ORIG]], i16* [[ARRAYIDXA_LVER_ORIG]], align 2
3830c09e5bdSPhilip Reames; LV-NEXT:    [[INC_LVER_ORIG]] = add nuw nsw i64 [[IND_LVER_ORIG]], 1
3840c09e5bdSPhilip Reames; LV-NEXT:    [[DEC_LVER_ORIG]] = sub i32 [[IND1_LVER_ORIG]], 1
3850c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND_LVER_ORIG:%.*]] = icmp eq i64 [[INC_LVER_ORIG]], [[N]]
3860c09e5bdSPhilip Reames; LV-NEXT:    br i1 [[EXITCOND_LVER_ORIG]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY_LVER_ORIG]]
3870c09e5bdSPhilip Reames; LV:       for.body.ph:
3880c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY:%.*]]
3890c09e5bdSPhilip Reames; LV:       for.body:
3900c09e5bdSPhilip Reames; LV-NEXT:    [[IND:%.*]] = phi i64 [ 0, [[FOR_BODY_PH]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
3910c09e5bdSPhilip Reames; LV-NEXT:    [[IND1:%.*]] = phi i32 [ [[TRUNCN]], [[FOR_BODY_PH]] ], [ [[DEC:%.*]], [[FOR_BODY]] ]
3920c09e5bdSPhilip Reames; LV-NEXT:    [[MUL:%.*]] = mul i32 [[IND1]], 2
3930c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_EXT:%.*]] = sext i32 [[MUL]] to i64
3940c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA:%.*]] = getelementptr i16, i16* [[A]], i64 [[MUL_EXT]]
3950c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA:%.*]] = load i16, i16* [[ARRAYIDXA]], align 2
3960c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB:%.*]] = getelementptr i16, i16* [[B]], i64 [[IND]]
3970c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB:%.*]] = load i16, i16* [[ARRAYIDXB]], align 2
3980c09e5bdSPhilip Reames; LV-NEXT:    [[ADD:%.*]] = mul i16 [[LOADA]], [[LOADB]]
3990c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD]], i16* [[ARRAYIDXA]], align 2
4000c09e5bdSPhilip Reames; LV-NEXT:    [[INC]] = add nuw nsw i64 [[IND]], 1
4010c09e5bdSPhilip Reames; LV-NEXT:    [[DEC]] = sub i32 [[IND1]], 1
4020c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INC]], [[N]]
403c86e1ce7SNikita Popov; LV-NEXT:    br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT6:%.*]], label [[FOR_BODY]]
4040c09e5bdSPhilip Reames; LV:       for.end.loopexit:
4050c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END:%.*]]
406c86e1ce7SNikita Popov; LV:       for.end.loopexit6:
4070c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END]]
4080c09e5bdSPhilip Reames; LV:       for.end:
4090c09e5bdSPhilip Reames; LV-NEXT:    ret void
4100c09e5bdSPhilip Reames;
4110c09e5bdSPhilip Reames  i16* noalias %b, i64 %N) {
4120c09e5bdSPhilip Reamesentry:
4130c09e5bdSPhilip Reames  %TruncN = trunc i64 %N to i32
4140c09e5bdSPhilip Reames  br label %for.body
4150c09e5bdSPhilip Reames
4160c09e5bdSPhilip Reamesfor.body:                                         ; preds = %for.body, %entry
4170c09e5bdSPhilip Reames  %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
4180c09e5bdSPhilip Reames  %ind1 = phi i32 [ %TruncN, %entry ], [ %dec, %for.body ]
4190c09e5bdSPhilip Reames
4200c09e5bdSPhilip Reames  %mul = mul i32 %ind1, 2
4210c09e5bdSPhilip Reames  %mul_ext = sext i32 %mul to i64
4220c09e5bdSPhilip Reames
4230c09e5bdSPhilip Reames  %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
4240c09e5bdSPhilip Reames  %loadA = load i16, i16* %arrayidxA, align 2
4250c09e5bdSPhilip Reames
4260c09e5bdSPhilip Reames  %arrayidxB = getelementptr i16, i16* %b, i64 %ind
4270c09e5bdSPhilip Reames  %loadB = load i16, i16* %arrayidxB, align 2
4280c09e5bdSPhilip Reames
4290c09e5bdSPhilip Reames  %add = mul i16 %loadA, %loadB
4300c09e5bdSPhilip Reames
4310c09e5bdSPhilip Reames  store i16 %add, i16* %arrayidxA, align 2
4320c09e5bdSPhilip Reames
4330c09e5bdSPhilip Reames  %inc = add nuw nsw i64 %ind, 1
4340c09e5bdSPhilip Reames  %dec = sub i32 %ind1, 1
4350c09e5bdSPhilip Reames
4360c09e5bdSPhilip Reames  %exitcond = icmp eq i64 %inc, %N
4370c09e5bdSPhilip Reames  br i1 %exitcond, label %for.end, label %for.body
4380c09e5bdSPhilip Reames
4390c09e5bdSPhilip Reamesfor.end:                                          ; preds = %for.body
4400c09e5bdSPhilip Reames  ret void
4410c09e5bdSPhilip Reames}
4420c09e5bdSPhilip Reames
4430c09e5bdSPhilip Reames; The following function is similar to the one above, but has the GEP
4440c09e5bdSPhilip Reames; to pointer %A inbounds. The index %mul doesn't have the nsw flag.
4450c09e5bdSPhilip Reames; This means that the SCEV expression for %mul can wrap and we need
4460c09e5bdSPhilip Reames; a SCEV predicate to continue analysis.
4470c09e5bdSPhilip Reames;
4480c09e5bdSPhilip Reames; We can still analyze this by adding the required no wrap SCEV predicates.
4490c09e5bdSPhilip Reames
4500c09e5bdSPhilip Reamesdefine void @f5(i16* noalias %a,
4510c09e5bdSPhilip Reames; LV-LABEL: @f5(
4520c09e5bdSPhilip Reames; LV-NEXT:  for.body.lver.check:
4530c09e5bdSPhilip Reames; LV-NEXT:    [[TRUNCN:%.*]] = trunc i64 [[N:%.*]] to i32
4540c09e5bdSPhilip Reames; LV-NEXT:    [[TMP0:%.*]] = add i64 [[N]], -1
4550c09e5bdSPhilip Reames; LV-NEXT:    [[TMP1:%.*]] = shl i32 [[TRUNCN]], 1
4560c09e5bdSPhilip Reames; LV-NEXT:    [[TMP2:%.*]] = trunc i64 [[TMP0]] to i32
4570c09e5bdSPhilip Reames; LV-NEXT:    [[MUL1:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 2, i32 [[TMP2]])
4580c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_RESULT:%.*]] = extractvalue { i32, i1 } [[MUL1]], 0
4590c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW:%.*]] = extractvalue { i32, i1 } [[MUL1]], 1
4600c09e5bdSPhilip Reames; LV-NEXT:    [[TMP4:%.*]] = sub i32 [[TMP1]], [[MUL_RESULT]]
4610c09e5bdSPhilip Reames; LV-NEXT:    [[TMP5:%.*]] = icmp sgt i32 [[TMP4]], [[TMP1]]
4627f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP9:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
463b2915971SRoman Lebedev; LV-NEXT:    [[TMP8:%.*]] = icmp ugt i64 [[TMP0]], 4294967295
4647f1bf68dSFlorian Hahn; LV-NEXT:    [[TMP10:%.*]] = or i1 [[TMP9]], [[TMP8]]
465b2915971SRoman Lebedev; LV-NEXT:    [[TMP12:%.*]] = sext i32 [[TMP1]] to i64
466b2915971SRoman Lebedev; LV-NEXT:    [[SCEVGEP:%.*]] = getelementptr i16, i16* [[A:%.*]], i64 [[TMP12]]
467e735f2bfSPhilip Reames; LV-NEXT:    [[MUL2:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 4, i64 [[TMP0]])
468e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_RESULT3:%.*]] = extractvalue { i64, i1 } [[MUL2]], 0
469e735f2bfSPhilip Reames; LV-NEXT:    [[MUL_OVERFLOW4:%.*]] = extractvalue { i64, i1 } [[MUL2]], 1
470e735f2bfSPhilip Reames; LV-NEXT:    [[SCEVGEP5:%.*]] = bitcast i16* [[SCEVGEP]] to i8*
471b2915971SRoman Lebedev; LV-NEXT:    [[TMP13:%.*]] = sub i64 0, [[MUL_RESULT3]]
472b2915971SRoman Lebedev; LV-NEXT:    [[TMP15:%.*]] = getelementptr i8, i8* [[SCEVGEP5]], i64 [[TMP13]]
473b2915971SRoman Lebedev; LV-NEXT:    [[TMP16:%.*]] = icmp ugt i8* [[TMP15]], [[SCEVGEP5]]
474f395a4f8SFlorian Hahn; LV-NEXT:    [[TMP19:%.*]] = or i1 [[TMP16]], [[MUL_OVERFLOW4]]
47586d113a8SFlorian Hahn; LV-NEXT:    [[TMP20:%.*]] = or i1 [[TMP10]], [[TMP19]]
476b2915971SRoman Lebedev; LV-NEXT:    br i1 [[TMP20]], label [[FOR_BODY_PH_LVER_ORIG:%.*]], label [[FOR_BODY_PH:%.*]]
4770c09e5bdSPhilip Reames; LV:       for.body.ph.lver.orig:
4780c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY_LVER_ORIG:%.*]]
4790c09e5bdSPhilip Reames; LV:       for.body.lver.orig:
4800c09e5bdSPhilip Reames; LV-NEXT:    [[IND_LVER_ORIG:%.*]] = phi i64 [ 0, [[FOR_BODY_PH_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
4810c09e5bdSPhilip Reames; LV-NEXT:    [[IND1_LVER_ORIG:%.*]] = phi i32 [ [[TRUNCN]], [[FOR_BODY_PH_LVER_ORIG]] ], [ [[DEC_LVER_ORIG:%.*]], [[FOR_BODY_LVER_ORIG]] ]
4820c09e5bdSPhilip Reames; LV-NEXT:    [[MUL_LVER_ORIG:%.*]] = mul i32 [[IND1_LVER_ORIG]], 2
4830c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA_LVER_ORIG:%.*]] = getelementptr inbounds i16, i16* [[A]], i32 [[MUL_LVER_ORIG]]
4840c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXA_LVER_ORIG]], align 2
4850c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB_LVER_ORIG:%.*]] = getelementptr inbounds i16, i16* [[B:%.*]], i64 [[IND_LVER_ORIG]]
4860c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB_LVER_ORIG:%.*]] = load i16, i16* [[ARRAYIDXB_LVER_ORIG]], align 2
4870c09e5bdSPhilip Reames; LV-NEXT:    [[ADD_LVER_ORIG:%.*]] = mul i16 [[LOADA_LVER_ORIG]], [[LOADB_LVER_ORIG]]
4880c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD_LVER_ORIG]], i16* [[ARRAYIDXA_LVER_ORIG]], align 2
4890c09e5bdSPhilip Reames; LV-NEXT:    [[INC_LVER_ORIG]] = add nuw nsw i64 [[IND_LVER_ORIG]], 1
4900c09e5bdSPhilip Reames; LV-NEXT:    [[DEC_LVER_ORIG]] = sub i32 [[IND1_LVER_ORIG]], 1
4910c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND_LVER_ORIG:%.*]] = icmp eq i64 [[INC_LVER_ORIG]], [[N]]
4920c09e5bdSPhilip Reames; LV-NEXT:    br i1 [[EXITCOND_LVER_ORIG]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY_LVER_ORIG]]
4930c09e5bdSPhilip Reames; LV:       for.body.ph:
4940c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_BODY:%.*]]
4950c09e5bdSPhilip Reames; LV:       for.body:
4960c09e5bdSPhilip Reames; LV-NEXT:    [[IND:%.*]] = phi i64 [ 0, [[FOR_BODY_PH]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
4970c09e5bdSPhilip Reames; LV-NEXT:    [[IND1:%.*]] = phi i32 [ [[TRUNCN]], [[FOR_BODY_PH]] ], [ [[DEC:%.*]], [[FOR_BODY]] ]
4980c09e5bdSPhilip Reames; LV-NEXT:    [[MUL:%.*]] = mul i32 [[IND1]], 2
4990c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXA:%.*]] = getelementptr inbounds i16, i16* [[A]], i32 [[MUL]]
5000c09e5bdSPhilip Reames; LV-NEXT:    [[LOADA:%.*]] = load i16, i16* [[ARRAYIDXA]], align 2
5010c09e5bdSPhilip Reames; LV-NEXT:    [[ARRAYIDXB:%.*]] = getelementptr inbounds i16, i16* [[B]], i64 [[IND]]
5020c09e5bdSPhilip Reames; LV-NEXT:    [[LOADB:%.*]] = load i16, i16* [[ARRAYIDXB]], align 2
5030c09e5bdSPhilip Reames; LV-NEXT:    [[ADD:%.*]] = mul i16 [[LOADA]], [[LOADB]]
5040c09e5bdSPhilip Reames; LV-NEXT:    store i16 [[ADD]], i16* [[ARRAYIDXA]], align 2
5050c09e5bdSPhilip Reames; LV-NEXT:    [[INC]] = add nuw nsw i64 [[IND]], 1
5060c09e5bdSPhilip Reames; LV-NEXT:    [[DEC]] = sub i32 [[IND1]], 1
5070c09e5bdSPhilip Reames; LV-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INC]], [[N]]
508c86e1ce7SNikita Popov; LV-NEXT:    br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT6:%.*]], label [[FOR_BODY]]
5090c09e5bdSPhilip Reames; LV:       for.end.loopexit:
5100c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END:%.*]]
511c86e1ce7SNikita Popov; LV:       for.end.loopexit6:
5120c09e5bdSPhilip Reames; LV-NEXT:    br label [[FOR_END]]
5130c09e5bdSPhilip Reames; LV:       for.end:
5140c09e5bdSPhilip Reames; LV-NEXT:    ret void
5150c09e5bdSPhilip Reames;
5160c09e5bdSPhilip Reames  i16* noalias %b, i64 %N) {
5170c09e5bdSPhilip Reamesentry:
5180c09e5bdSPhilip Reames  %TruncN = trunc i64 %N to i32
5190c09e5bdSPhilip Reames  br label %for.body
5200c09e5bdSPhilip Reames
5210c09e5bdSPhilip Reamesfor.body:                                         ; preds = %for.body, %entry
5220c09e5bdSPhilip Reames  %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
5230c09e5bdSPhilip Reames  %ind1 = phi i32 [ %TruncN, %entry ], [ %dec, %for.body ]
5240c09e5bdSPhilip Reames
5250c09e5bdSPhilip Reames  %mul = mul i32 %ind1, 2
5260c09e5bdSPhilip Reames
5270c09e5bdSPhilip Reames  %arrayidxA = getelementptr inbounds i16, i16* %a, i32 %mul
5280c09e5bdSPhilip Reames  %loadA = load i16, i16* %arrayidxA, align 2
5290c09e5bdSPhilip Reames
5300c09e5bdSPhilip Reames  %arrayidxB = getelementptr inbounds i16, i16* %b, i64 %ind
5310c09e5bdSPhilip Reames  %loadB = load i16, i16* %arrayidxB, align 2
5320c09e5bdSPhilip Reames
5330c09e5bdSPhilip Reames  %add = mul i16 %loadA, %loadB
5340c09e5bdSPhilip Reames
5350c09e5bdSPhilip Reames  store i16 %add, i16* %arrayidxA, align 2
5360c09e5bdSPhilip Reames
5370c09e5bdSPhilip Reames  %inc = add nuw nsw i64 %ind, 1
5380c09e5bdSPhilip Reames  %dec = sub i32 %ind1, 1
5390c09e5bdSPhilip Reames
5400c09e5bdSPhilip Reames  %exitcond = icmp eq i64 %inc, %N
5410c09e5bdSPhilip Reames  br i1 %exitcond, label %for.end, label %for.body
5420c09e5bdSPhilip Reames
5430c09e5bdSPhilip Reamesfor.end:                                          ; preds = %for.body
5440c09e5bdSPhilip Reames  ret void
5450c09e5bdSPhilip Reames}
546