Index: include/llvm/Analysis/ScalarEvolutionExpander.h =================================================================== --- include/llvm/Analysis/ScalarEvolutionExpander.h +++ include/llvm/Analysis/ScalarEvolutionExpander.h @@ -315,8 +315,10 @@ SmallPtrSetImpl &Processed); /// Insert the specified binary operator, doing a small amount of work to - /// avoid inserting an obviously redundant operation. - Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS); + /// avoid inserting an obviously redundant operation, and hoisting to an + /// outer loop when the opportunity is there and it is safe. + Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, + bool IsSafeToHoist = true); /// Arrange for there to be a cast of V to Ty at IP, reusing an existing /// cast if a suitable one exists, moving an existing cast if a suitable one Index: lib/Analysis/ScalarEvolutionExpander.cpp =================================================================== --- lib/Analysis/ScalarEvolutionExpander.cpp +++ lib/Analysis/ScalarEvolutionExpander.cpp @@ -166,9 +166,10 @@ } /// InsertBinop - Insert the specified binary operator, doing a small amount -/// of work to avoid inserting an obviously redundant operation. +/// of work to avoid inserting an obviously redundant operation, and hoisting +/// to an outer loop when the opportunity is there and it is safe. Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, - Value *LHS, Value *RHS) { + Value *LHS, Value *RHS, bool IsSafeToHoist) { // Fold a binop with constant operands. if (Constant *CLHS = dyn_cast(LHS)) if (Constant *CRHS = dyn_cast(RHS)) @@ -210,14 +211,16 @@ DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc(); SCEVInsertPointGuard Guard(Builder, this); - // Move the insertion point out of as many loops as we can. - while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) { - if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break; - BasicBlock *Preheader = L->getLoopPreheader(); - if (!Preheader) break; + if (IsSafeToHoist) { + // Move the insertion point out of as many loops as we can. + while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) { + if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break; + BasicBlock *Preheader = L->getLoopPreheader(); + if (!Preheader) break; - // Ok, move up a level. - Builder.SetInsertPoint(Preheader->getTerminator()); + // Ok, move up a level. + Builder.SetInsertPoint(Preheader->getTerminator()); + } } // If we haven't found this binop, insert it. @@ -846,7 +849,8 @@ } Value *RHS = expandCodeFor(S->getRHS(), Ty); - return InsertBinop(Instruction::UDiv, LHS, RHS); + return InsertBinop(Instruction::UDiv, LHS, RHS, + SE.isKnownNonZero(S->getRHS())); } /// Move parts of Base into Rest to leave Base with the minimal Index: test/Transforms/LoopVectorize/pr38697.ll =================================================================== --- test/Transforms/LoopVectorize/pr38697.ll +++ test/Transforms/LoopVectorize/pr38697.ll @@ -0,0 +1,339 @@ +; RUN: opt -loop-vectorize -force-vector-width=2 -S < %s 2>&1 | FileCheck %s +; RUN: opt -indvars -S < %s 2>&1 | FileCheck %s -check-prefix=INDVARCHECK + +; Produced from test-case: +; +; void testCountIncrLoop(unsigned char *ptr, int lim, int count, int val) +; { +; int inx = 0; +; for (int outer_i = 0; outer_i < lim; ++outer_i) { +; if (count > 0) { // At runtime, 'count' is 0, so the following code is dead. +; int result = val; +; int tmp = count; +; +; while (tmp < 8) { +; result += val >> tmp; +; tmp += count; +; } +; +; ptr[inx++] = (unsigned char) result; +; } +; } +; } +; +; No explicit division appears in the input, but a division is generated during +; vectorization, and that division is a division-by-0 when the input 'count' +; is 0, so it cannot be hoisted above the guard of 'count > 0'. + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @testCountIncrLoop(i8* %ptr, i32 %lim, i32 %count, i32 %val) { +entry: + %cmp1 = icmp sgt i32 %lim, 0 + br i1 %cmp1, label %loop1.preheader, label %exit + +; Verify that a 'udiv' does not appear between the 'loop1.preheader' label, and +; the 'while.cond.preheader' label. And verify that a 'udiv' does appear in the +; 'while.body.preheader'. +; CHECK: loop1.preheader: +; CHECK-NOT: udiv +; CHECK: while.body.preheader: +; CHECK: udiv +; CHECK: br i1 + +loop1.preheader: + %cmp2 = icmp sgt i32 %count, 0 + %cmp4 = icmp slt i32 %count, 8 + br label %loop1.body + +loop1.body: + %outer_i = phi i32 [ 0, %loop1.preheader ], [ %outer_i.1, %loop1.inc ] + %inx.1 = phi i32 [ 0, %loop1.preheader ], [ %inx.2, %loop1.inc ] + br i1 %cmp2, label %while.cond.preheader, label %loop1.inc + +while.cond.preheader: + br i1 %cmp4, label %while.body, label %while.end + +while.body: + %tmp = phi i32 [ %add3, %while.body ], [ %count, %while.cond.preheader ] + %result.1 = phi i32 [ %add, %while.body ], [ %val, %while.cond.preheader ] + %shr = ashr i32 %val, %tmp + %add = add nsw i32 %shr, %result.1 + %add3 = add nsw i32 %tmp, %count + %cmp3 = icmp slt i32 %add3, 8 + br i1 %cmp3, label %while.body, label %while.end + +while.end: + %result.0.lcssa = phi i32 [ %val, %while.cond.preheader ], [ %add, %while.body ] + %conv = trunc i32 %result.0.lcssa to i8 + %inc = add nsw i32 %inx.1, 1 + %idxprom = sext i32 %inx.1 to i64 + %arrayidx = getelementptr inbounds i8, i8* %ptr, i64 %idxprom + store i8 %conv, i8* %arrayidx, align 1 + br label %loop1.inc + +loop1.inc: + %inx.2 = phi i32 [ %inc, %while.end ], [ %inx.1, %loop1.body ] + %outer_i.1 = add nuw nsw i32 %outer_i, 1 + %exitcond = icmp eq i32 %outer_i.1, %lim + br i1 %exitcond, label %exit, label %loop1.body + +exit: + ret void +} + +; These next tests are all based on the following source code, with slight +; variations on the calculation of 'incr' (all of which are loop-invariant +; divisions, but only some of which can be safely hoisted): +; +; uint32_t foo(uint32_t *ptr, uint32_t start1, uint32_t start2) { +; uint32_t counter1, counter2; +; uint32_t val = start1; +; for (counter1 = 1; counter1 < 100; ++counter1) { +; uint32_t index = 0; +; val += ptr[index]; +; for (counter2 = start2; counter2 < 10; ++counter2) { +; // Division is loop invariant, and denominator is guaranteed non-zero: +; // Safe to hoist it out of the inner loop. +; uint32_t incr = 16 / counter1; +; index += incr; +; val += ptr[index]; +; } +; } +; return val; +; } + +; This version is as written above, where 'incr' is '16/counter1', and it is +; guaranted that 'counter1' is always non-zero. So it is safe to hoist the +; division from the inner loop to the preheader. +define i32 @NonZeroDivHoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) { +; Verify that the 'udiv' is hoisted to the preheader, and is not in the loop body. +; INDVARCHECK-LABEL: NonZeroDivHoist +; INDVARCHECK: for.body3.lr.ph: +; INDVARCHECK: udiv +; INDVARCHECK: for.body3: +; INDVARCHECK-NOT: udiv +; INDVARCHECK: br i1 +entry: + br label %for.cond + +for.cond: + %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ] + %counter1.0 = phi i32 [ 1, %entry ], [ %inc9, %for.end ] + %cmp = icmp ult i32 %counter1.0, 100 + br i1 %cmp, label %for.body, label %for.end10 + +for.body: + %0 = load i32, i32* %ptr, align 4 + %add = add i32 %0, %val.0 + %cmp224 = icmp ult i32 %start2, 10 + br i1 %cmp224, label %for.body3.lr.ph, label %for.end + +for.body3.lr.ph: + br label %for.body3 + +for.body3: + %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ] + %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ] + %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ] + %div = udiv i32 16, %counter1.0 + %add4 = add i32 %div, %index.027 + %idxprom5 = zext i32 %add4 to i64 + %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5 + %1 = load i32, i32* %arrayidx6, align 4 + %add7 = add i32 %1, %val.126 + %inc = add i32 %counter2.025, 1 + %cmp2 = icmp ult i32 %inc, 10 + br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge + +for.cond1.for.end_crit_edge: + %split = phi i32 [ %add7, %for.body3 ] + br label %for.end + +for.end: + %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ] + %inc9 = add i32 %counter1.0, 1 + br label %for.cond + +for.end10: + %val.0.lcssa = phi i32 [ %val.0, %for.cond ] + ret i32 %val.0.lcssa +} + +; This version is identical to the above 'NonZeroDivHoist' case, except the +; outer ('counter1') loop starts at the unknown value of 'start1' rather than 1, +; and so it is illegal to hoist the division because if 'start1' is 0, hoisting +; it would incorrectly cause a divide-by-zero trap. +define i32 @ZeroDivNoHoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) { +; Verify that the 'udiv' is not hoisted to the preheader, and is remains in the +; loop body. +; INDVARCHECK-LABEL: ZeroDivNoHoist +; INDVARCHECK-NOT: udiv +; INDVARCHECK: for.body3: +; INDVARCHECK: udiv +; INDVARCHECK: br i1 +entry: + br label %for.cond + +for.cond: + %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ] + %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ] + %cmp = icmp ult i32 %counter1.0, 100 + br i1 %cmp, label %for.body, label %for.end10 + +for.body: + %0 = load i32, i32* %ptr, align 4 + %add = add i32 %0, %val.0 + %cmp224 = icmp ult i32 %start2, 10 + br i1 %cmp224, label %for.body3.lr.ph, label %for.end + +for.body3.lr.ph: + br label %for.body3 + +for.body3: + %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ] + %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ] + %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ] + %div = udiv i32 16, %counter1.0 + %add4 = add i32 %div, %index.027 + %idxprom5 = zext i32 %add4 to i64 + %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5 + %1 = load i32, i32* %arrayidx6, align 4 + %add7 = add i32 %1, %val.126 + %inc = add i32 %counter2.025, 1 + %cmp2 = icmp ult i32 %inc, 10 + br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge + +for.cond1.for.end_crit_edge: + %split = phi i32 [ %add7, %for.body3 ] + br label %for.end + +for.end: + %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ] + %inc9 = add i32 %counter1.0, 1 + br label %for.cond + +for.end10: + %val.0.lcssa = phi i32 [ %val.0, %for.cond ] + ret i32 %val.0.lcssa +} + +; This version is has a clearly safe division by a non-zero constant (16). The +; division is transformed to a logical-shift-right of 4, and it is safely +; hoisted. +define i32 @DivBy16Hoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) { +; Verify that the division-operation is hoisted, and that it appears as a +; right-shift ('lshr') rather than an explicit division. +; loop body. +; INDVARCHECK-LABEL: DivBy16Hoist +; INDVARCHECK: for.cond: +; INDVARCHECK: lshr +; INDVARCHECK: for.body3: +; INDVARCHECK-NOT: lshr +; INDVARCHECK-NOT: udiv +; INDVARCHECK: br i1 +entry: + br label %for.cond + +for.cond: + %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ] + %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ] + %cmp = icmp ult i32 %counter1.0, 100 + br i1 %cmp, label %for.body, label %for.end10 + +for.body: + %0 = load i32, i32* %ptr, align 4 + %add = add i32 %0, %val.0 + %cmp224 = icmp ult i32 %start2, 10 + br i1 %cmp224, label %for.body3.lr.ph, label %for.end + +for.body3.lr.ph: + br label %for.body3 + +for.body3: + %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ] + %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ] + %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ] + %div = udiv i32 %counter1.0, 16 + %add4 = add i32 %div, %index.027 + %idxprom5 = zext i32 %add4 to i64 + %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5 + %1 = load i32, i32* %arrayidx6, align 4 + %add7 = add i32 %1, %val.126 + %inc = add i32 %counter2.025, 1 + %cmp2 = icmp ult i32 %inc, 10 + br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge + +for.cond1.for.end_crit_edge: + %split = phi i32 [ %add7, %for.body3 ] + br label %for.end + +for.end: + %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ] + %inc9 = add i32 %counter1.0, 1 + br label %for.cond + +for.end10: + %val.0.lcssa = phi i32 [ %val.0, %for.cond ] + ret i32 %val.0.lcssa +} + +; This version is has a clearly safe division by a non-zero constant (17). The +; division is safely hoisted, as it was in the 'DivBy16Hoist' verison, but here +; it remains a division, rather than being transformed to a right-shift. +define i32 @DivBy17Hoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) { +; Verify that the division-operation is hoisted. +; loop body. +; INDVARCHECK-LABEL: DivBy17Hoist +; INDVARCHECK: for.cond: +; INDVARCHECK: udiv +; INDVARCHECK: br i1 +; INDVARCHECK: for.body3: +; INDVARCHECK-NOT: udiv +; INDVARCHECK: br i1 +entry: + br label %for.cond + +for.cond: + %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ] + %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ] + %cmp = icmp ult i32 %counter1.0, 100 + br i1 %cmp, label %for.body, label %for.end10 + +for.body: + %0 = load i32, i32* %ptr, align 4 + %add = add i32 %0, %val.0 + %cmp224 = icmp ult i32 %start2, 10 + br i1 %cmp224, label %for.body3.lr.ph, label %for.end + +for.body3.lr.ph: + br label %for.body3 + +for.body3: + %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ] + %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ] + %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ] + %div = udiv i32 %counter1.0, 17 + %add4 = add i32 %div, %index.027 + %idxprom5 = zext i32 %add4 to i64 + %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5 + %1 = load i32, i32* %arrayidx6, align 4 + %add7 = add i32 %1, %val.126 + %inc = add i32 %counter2.025, 1 + %cmp2 = icmp ult i32 %inc, 10 + br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge + +for.cond1.for.end_crit_edge: + %split = phi i32 [ %add7, %for.body3 ] + br label %for.end + +for.end: + %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ] + %inc9 = add i32 %counter1.0, 1 + br label %for.cond + +for.end10: + %val.0.lcssa = phi i32 [ %val.0, %for.cond ] + ret i32 %val.0.lcssa +}