From: Matthew Simpson Date: Mon, 27 Mar 2017 20:07:38 +0000 (+0000) Subject: [LV] Transform truncations of non-primary induction variables X-Git-Tag: llvmorg-5.0.0-rc1~9070 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b8ff4a4a700d3d24e20e98a1e849fd57c8b0bbd0;p=platform%2Fupstream%2Fllvm.git [LV] Transform truncations of non-primary induction variables The vectorizer tries to replace truncations of induction variables with new induction variables having the smaller type. After r295063, this optimization was applied to all integer induction variables, including non-primary ones. When optimizing the truncation of a non-primary induction variable, we still need to transform the new induction so that it has the correct start value. This should fix PR32419. Reference: https://bugs.llvm.org/show_bug.cgi?id=32419 llvm-svn: 298882 --- diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index fa2c71d..af2d3f5 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -2521,22 +2521,21 @@ void InnerLoopVectorizer::widenIntOrFpInduction(PHINode *IV, TruncInst *Trunc) { // induction variable and step. Otherwise, derive these values from the // induction descriptor. if (!VectorizedIV || NeedsScalarIV) { + ScalarIV = Induction; + if (IV != OldInduction) { + ScalarIV = IV->getType()->isIntegerTy() + ? Builder.CreateSExtOrTrunc(Induction, IV->getType()) + : Builder.CreateCast(Instruction::SIToFP, Induction, + IV->getType()); + ScalarIV = ID.transform(Builder, ScalarIV, PSE.getSE(), DL); + ScalarIV->setName("offset.idx"); + } if (Trunc) { auto *TruncType = cast(Trunc->getType()); assert(Step->getType()->isIntegerTy() && "Truncation requires an integer step"); - ScalarIV = Builder.CreateCast(Instruction::Trunc, Induction, TruncType); + ScalarIV = Builder.CreateTrunc(ScalarIV, TruncType); Step = Builder.CreateTrunc(Step, TruncType); - } else { - ScalarIV = Induction; - if (IV != OldInduction) { - ScalarIV = IV->getType()->isIntegerTy() - ? Builder.CreateSExtOrTrunc(ScalarIV, IV->getType()) - : Builder.CreateCast(Instruction::SIToFP, Induction, - IV->getType()); - ScalarIV = ID.transform(Builder, ScalarIV, PSE.getSE(), DL); - ScalarIV->setName("offset.idx"); - } } } diff --git a/llvm/test/Transforms/LoopVectorize/induction.ll b/llvm/test/Transforms/LoopVectorize/induction.ll index da20a2a..0d7d9fe 100644 --- a/llvm/test/Transforms/LoopVectorize/induction.ll +++ b/llvm/test/Transforms/LoopVectorize/induction.ll @@ -804,3 +804,48 @@ for.body: for.end: ret void } + +; PR32419. Ensure we transform truncated non-primary induction variables. In +; the test case below we replace %tmp1 with a new induction variable. Because +; the truncated value is non-primary, we must compute an offset from the +; primary induction variable. +; +; CHECK-LABEL: @PR32419( +; CHECK: vector.body: +; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %vector.ph ], [ [[INDEX_NEXT:%.*]], %[[PRED_UREM_CONTINUE4:.*]] ] +; CHECK: [[OFFSET_IDX:%.*]] = add i32 -20, [[INDEX]] +; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[OFFSET_IDX]] to i16 +; CHECK: [[TMP8:%.*]] = add i16 [[TMP1]], 0 +; CHECK-NEXT: [[TMP9:%.*]] = urem i16 %b, [[TMP8]] +; CHECK: [[TMP15:%.*]] = add i16 [[TMP1]], 1 +; CHECK-NEXT: [[TMP16:%.*]] = urem i16 %b, [[TMP15]] +; CHECK: [[PRED_UREM_CONTINUE4]]: +; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body +; +define i32 @PR32419(i32 %a, i16 %b) { +entry: + br label %for.body + +for.body: + %i = phi i32 [ -20, %entry ], [ %i.next, %for.inc ] + %tmp0 = phi i32 [ %a, %entry ], [ %tmp6, %for.inc ] + %tmp1 = trunc i32 %i to i16 + %tmp2 = icmp eq i16 %tmp1, 0 + br i1 %tmp2, label %for.inc, label %for.cond + +for.cond: + %tmp3 = urem i16 %b, %tmp1 + br label %for.inc + +for.inc: + %tmp4 = phi i16 [ %tmp3, %for.cond ], [ 0, %for.body ] + %tmp5 = sext i16 %tmp4 to i32 + %tmp6 = or i32 %tmp0, %tmp5 + %i.next = add nsw i32 %i, 1 + %cond = icmp eq i32 %i.next, 0 + br i1 %cond, label %for.end, label %for.body + +for.end: + %tmp7 = phi i32 [ %tmp6, %for.inc ] + ret i32 %tmp7 +}