1 ; RUN: opt < %s -loop-vectorize -force-vector-unroll=2 -force-vector-width=4 -S | FileCheck %s 2 3 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-S128" 4 5 ; Make sure consecutive vector generates correct negative indices. 6 ; PR15882 7 8 ; CHECK-LABEL: @reverse_induction_i64( 9 ; CHECK: add <4 x i64> %[[SPLAT:.*]], <i64 0, i64 -1, i64 -2, i64 -3> 10 ; CHECK: add <4 x i64> %[[SPLAT]], <i64 -4, i64 -5, i64 -6, i64 -7> 11 12 define i32 @reverse_induction_i64(i64 %startval, i32 * %ptr) { 13 entry: 14 br label %for.body 15 16 for.body: 17 %add.i7 = phi i64 [ %startval, %entry ], [ %add.i, %for.body ] 18 %i.06 = phi i32 [ 0, %entry ], [ %inc4, %for.body ] 19 %redux5 = phi i32 [ 0, %entry ], [ %inc.redux, %for.body ] 20 %add.i = add i64 %add.i7, -1 21 %kind_.i = getelementptr inbounds i32* %ptr, i64 %add.i 22 %tmp.i1 = load i32* %kind_.i, align 4 23 %inc.redux = add i32 %tmp.i1, %redux5 24 %inc4 = add i32 %i.06, 1 25 %exitcond = icmp ne i32 %inc4, 1024 26 br i1 %exitcond, label %for.body, label %loopend 27 28 loopend: 29 ret i32 %inc.redux 30 } 31 32 ; CHECK-LABEL: @reverse_induction_i128( 33 ; CHECK: add <4 x i128> %[[SPLAT:.*]], <i128 0, i128 -1, i128 -2, i128 -3> 34 ; CHECK: add <4 x i128> %[[SPLAT]], <i128 -4, i128 -5, i128 -6, i128 -7> 35 define i32 @reverse_induction_i128(i128 %startval, i32 * %ptr) { 36 entry: 37 br label %for.body 38 39 for.body: 40 %add.i7 = phi i128 [ %startval, %entry ], [ %add.i, %for.body ] 41 %i.06 = phi i32 [ 0, %entry ], [ %inc4, %for.body ] 42 %redux5 = phi i32 [ 0, %entry ], [ %inc.redux, %for.body ] 43 %add.i = add i128 %add.i7, -1 44 %kind_.i = getelementptr inbounds i32* %ptr, i128 %add.i 45 %tmp.i1 = load i32* %kind_.i, align 4 46 %inc.redux = add i32 %tmp.i1, %redux5 47 %inc4 = add i32 %i.06, 1 48 %exitcond = icmp ne i32 %inc4, 1024 49 br i1 %exitcond, label %for.body, label %loopend 50 51 loopend: 52 ret i32 %inc.redux 53 } 54 55 ; CHECK-LABEL: @reverse_induction_i16( 56 ; CHECK: add <4 x i16> %[[SPLAT:.*]], <i16 0, i16 -1, i16 -2, i16 -3> 57 ; CHECK: add <4 x i16> %[[SPLAT]], <i16 -4, i16 -5, i16 -6, i16 -7> 58 59 define i32 @reverse_induction_i16(i16 %startval, i32 * %ptr) { 60 entry: 61 br label %for.body 62 63 for.body: 64 %add.i7 = phi i16 [ %startval, %entry ], [ %add.i, %for.body ] 65 %i.06 = phi i32 [ 0, %entry ], [ %inc4, %for.body ] 66 %redux5 = phi i32 [ 0, %entry ], [ %inc.redux, %for.body ] 67 %add.i = add i16 %add.i7, -1 68 %kind_.i = getelementptr inbounds i32* %ptr, i16 %add.i 69 %tmp.i1 = load i32* %kind_.i, align 4 70 %inc.redux = add i32 %tmp.i1, %redux5 71 %inc4 = add i32 %i.06, 1 72 %exitcond = icmp ne i32 %inc4, 1024 73 br i1 %exitcond, label %for.body, label %loopend 74 75 loopend: 76 ret i32 %inc.redux 77 } 78 79 80 @a = common global [1024 x i32] zeroinitializer, align 16 81 82 ; We incorrectly transformed this loop into an empty one because we left the 83 ; induction variable in i8 type and truncated the exit value 1024 to 0. 84 ; int a[1024]; 85 ; 86 ; void fail() { 87 ; int reverse_induction = 1023; 88 ; unsigned char forward_induction = 0; 89 ; while ((reverse_induction) >= 0) { 90 ; forward_induction++; 91 ; a[reverse_induction] = forward_induction; 92 ; --reverse_induction; 93 ; } 94 ; } 95 96 ; CHECK-LABEL: @reverse_forward_induction_i64_i8( 97 ; CHECK: vector.body 98 ; CHECK: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ] 99 ; CHECK: %normalized.idx = sub i64 %index, 0 100 ; CHECK: %reverse.idx = sub i64 1023, %normalized.idx 101 ; CHECK: trunc i64 %index to i8 102 103 define void @reverse_forward_induction_i64_i8() { 104 entry: 105 br label %while.body 106 107 while.body: 108 %indvars.iv = phi i64 [ 1023, %entry ], [ %indvars.iv.next, %while.body ] 109 %forward_induction.05 = phi i8 [ 0, %entry ], [ %inc, %while.body ] 110 %inc = add i8 %forward_induction.05, 1 111 %conv = zext i8 %inc to i32 112 %arrayidx = getelementptr inbounds [1024 x i32]* @a, i64 0, i64 %indvars.iv 113 store i32 %conv, i32* %arrayidx, align 4 114 %indvars.iv.next = add i64 %indvars.iv, -1 115 %0 = trunc i64 %indvars.iv to i32 116 %cmp = icmp sgt i32 %0, 0 117 br i1 %cmp, label %while.body, label %while.end 118 119 while.end: 120 ret void 121 } 122 123 ; CHECK-LABEL: @reverse_forward_induction_i64_i8_signed( 124 ; CHECK: vector.body: 125 ; CHECK: %index = phi i64 [ 129, %vector.ph ], [ %index.next, %vector.body ] 126 ; CHECK: %normalized.idx = sub i64 %index, 129 127 ; CHECK: %reverse.idx = sub i64 1023, %normalized.idx 128 ; CHECK: trunc i64 %index to i8 129 130 define void @reverse_forward_induction_i64_i8_signed() { 131 entry: 132 br label %while.body 133 134 while.body: 135 %indvars.iv = phi i64 [ 1023, %entry ], [ %indvars.iv.next, %while.body ] 136 %forward_induction.05 = phi i8 [ -127, %entry ], [ %inc, %while.body ] 137 %inc = add i8 %forward_induction.05, 1 138 %conv = sext i8 %inc to i32 139 %arrayidx = getelementptr inbounds [1024 x i32]* @a, i64 0, i64 %indvars.iv 140 store i32 %conv, i32* %arrayidx, align 4 141 %indvars.iv.next = add i64 %indvars.iv, -1 142 %0 = trunc i64 %indvars.iv to i32 143 %cmp = icmp sgt i32 %0, 0 144 br i1 %cmp, label %while.body, label %while.end 145 146 while.end: 147 ret void 148 } 149