<div dir="ltr"><div>Hello,</div><div>For the C code given below:<br></div><div>
<pre class="gmail-bz_comment_text" id="gmail-comment_text_0">#include<stdio.h>
int a=0;
int d() {
int e = 2;
for (a = 0; a <= 8; a++)
;
return e;
}
void main() {
int f = 0;
d();
printf("%d\n",a);
}</pre> <pre class="gmail-bz_comment_text" id="gmail-comment_text_0">$clang -O3 -c -emit-llvm -mllvm -disable-llvm-optzns small.c
$opt -gvn -licm -loop-rotate -loop-vectorize small.bc -o small-opt.bc<br><br></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_0">I see vectorized IR as follow:<br>; Function Attrs: nounwind uwtable<br>define dso_local i32 @d() #0 {<br>entry:<br> %e = alloca i32, align 4<br> %0 = bitcast i32* %e to i8*<br> call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #3<br> store i32 2, i32* %e, align 4, !tbaa !2<br> store i32 0, i32* @a, align 4, !tbaa !2<br> %a.promoted = load i32, i32* @a, align 4, !tbaa !2<br> br i1 false, label %<a href="http://scalar.ph">scalar.ph</a>, label %<a href="http://vector.ph">vector.ph</a><br><br><a href="http://vector.ph">vector.ph</a>: ; preds = %entry<br> %vector.recur.init = insertelement <16 x i32> undef, i32 %a.promoted, i32 15<br> br label %vector.body<br><br>vector.body: ; preds = %vector.body, %<a href="http://vector.ph">vector.ph</a><br> %index = phi i32 [ 0, %<a href="http://vector.ph">vector.ph</a> ], [ %index.next, %vector.body ]<br> %vector.recur = phi <16 x i32> [ %vector.recur.init, %<a href="http://vector.ph">vector.ph</a> ], [ %17, %vector.body ]<br> %vec.ind = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %<a href="http://vector.ph">vector.ph</a> ], [ %vec.ind.next, %vector.body ]<br><span style="color:rgb(255,0,0)"> %1 = add i32 %index, 0<br> %2 = add i32 %index, 1<br> %3 = add i32 %index, 2<br> %4 = add i32 %index, 3<br> %5 = add i32 %index, 4<br> %6 = add i32 %index, 5<br> %7 = add i32 %index, 6<br> %8 = add i32 %index, 7<br> %9 = add i32 %index, 8<br> %10 = add i32 %index, 9<br> %11 = add i32 %index, 10<br> %12 = add i32 %index, 11<br> %13 = add i32 %index, 12<br> %14 = add i32 %index, 13<br> %15 = add i32 %index, 14<br> %16 = add i32 %index, 15<br> %17 = add nsw <16 x i32> %vec.ind, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1><br> %18 = shufflevector <16 x i32> %vector.recur, <16 x i32> %17, <16 x i32> <i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30><br> %19 = icmp ule <16 x i32> %vec.ind, <i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9, i32 9></span><br> %index.next = add i32 %index, 16<br> %vec.ind.next = add <16 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16><br> %20 = icmp eq i32 %index.next, 16<br> br i1 %20, label %middle.block, label %vector.body, !llvm.loop !6<br><br>middle.block: ; preds = %vector.body<br> %vector.recur.extract = extractelement <16 x i32> %17, i32 15<br> %vector.recur.extract.for.phi = extractelement <16 x i32> %17, i32 14<br> br i1 true, label %for.end, label %<a href="http://scalar.ph">scalar.ph</a><br><br><a href="http://scalar.ph">scalar.ph</a>: ; preds = %middle.block, %entry<br> %scalar.recur.init = phi i32 [ %vector.recur.extract, %middle.block ], [ %a.promoted, %entry ]<br> %bc.resume.val = phi i32 [ 16, %middle.block ], [ 0, %entry ]<br> br label %for.cond<br><br>for.cond: ; preds = %for.cond, %<a href="http://scalar.ph">scalar.ph</a><br> %scalar.recur = phi i32 [ %inc, %for.cond ], [ %scalar.recur.init, %<a href="http://scalar.ph">scalar.ph</a> ]<br> %21 = phi i32 [ %inc, %for.cond ], [ %bc.resume.val, %<a href="http://scalar.ph">scalar.ph</a> ]<br> %cmp = icmp sle i32 %21, 8<br> %inc = add nsw i32 %21, 1<br> br i1 %cmp, label %for.cond, label %for.end, !llvm.loop !8<br><br>for.end: ; preds = %middle.block, %for.cond<br> %inc1.lcssa = phi i32 [ %scalar.recur, %for.cond ], [ %vector.recur.extract.for.phi, %middle.block ]<br> store i32 %inc1.lcssa, i32* @a, align 4, !tbaa !2<br> call void @llvm.lifetime.end.p0i8(i64 4, i8* %0) #3<br> ret i32 2<br>}<br><br></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_0">As highlighted above few instructions result are never used can someone explain why?<br></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_0">Also This gives output (variable a) 15 which is incorrect as output should 9. However I don't see any problem with vectorized code and hence a = 15 is not surprising.<br></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_0">So solution to this problem is that loop should have never vectorized?<br><br></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_0">-Vivek<br></pre>
</div></div>