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