From 03e7d3ca93a6de90f8b1ce760a36fcb5667097b7 Mon Sep 17 00:00:00 2001 From: Serguei Katkov Date: Thu, 22 Aug 2019 10:06:46 +0000 Subject: [PATCH] [Loop Peeling] Fix silly bug in metadata update. We must update loop metedata before we moved to parent loop if it is present. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369637 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LoopUnrollPeel.cpp | 12 ++--- test/Transforms/LoopUnroll/peel-loop-inner.ll | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 test/Transforms/LoopUnroll/peel-loop-inner.ll diff --git a/lib/Transforms/Utils/LoopUnrollPeel.cpp b/lib/Transforms/Utils/LoopUnrollPeel.cpp index 3fad421f58f..58e42074f96 100644 --- a/lib/Transforms/Utils/LoopUnrollPeel.cpp +++ b/lib/Transforms/Utils/LoopUnrollPeel.cpp @@ -741,6 +741,12 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, fixupBranchWeights(Header, LatchBR, ExitWeight, FallThroughWeight); + // Update Metadata for count of peeled off iterations. + unsigned AlreadyPeeled = 0; + if (auto Peeled = getOptionalIntLoopAttribute(L, PeeledCountMetaData)) + AlreadyPeeled = *Peeled; + addStringMetadataToLoop(L, PeeledCountMetaData, AlreadyPeeled + PeelCount); + if (Loop *ParentLoop = L->getParentLoop()) L = ParentLoop; @@ -755,11 +761,5 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, NumPeeled++; - // Update Metadata for count of peeled off iterations. - unsigned AlreadyPeeled = 0; - if (auto Peeled = getOptionalIntLoopAttribute(L, PeeledCountMetaData)) - AlreadyPeeled = *Peeled; - addStringMetadataToLoop(L, PeeledCountMetaData, AlreadyPeeled + PeelCount); - return true; } diff --git a/test/Transforms/LoopUnroll/peel-loop-inner.ll b/test/Transforms/LoopUnroll/peel-loop-inner.ll new file mode 100644 index 00000000000..a83f3bfb935 --- /dev/null +++ b/test/Transforms/LoopUnroll/peel-loop-inner.ll @@ -0,0 +1,50 @@ +; RUN: opt < %s -S -passes='require,unroll,simplify-cfg,instcombine' -unroll-force-peel-count=3 -verify-dom-info | FileCheck %s + +define void @basic(i32 %K, i32 %N) { +; CHECK-LABEL: @basic( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[OUTER:%.*]] +; CHECK: outer: +; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_INC:%.*]], [[OUTER_BACKEDGE:%.*]] ] +; CHECK-NEXT: [[CMP_INNER_PEEL:%.*]] = icmp sgt i32 [[K:%.*]], 1 +; CHECK-NEXT: br i1 [[CMP_INNER_PEEL]], label [[INNER_PEEL2:%.*]], label [[OUTER_BACKEDGE]] +; CHECK: inner.peel2: +; CHECK-NEXT: [[CMP_INNER_PEEL4:%.*]] = icmp eq i32 [[K]], 2 +; CHECK-NEXT: br i1 [[CMP_INNER_PEEL4]], label [[OUTER_BACKEDGE]], label [[INNER_PEEL6:%.*]] +; CHECK: inner.peel6: +; CHECK-NEXT: [[CMP_INNER_PEEL8:%.*]] = icmp sgt i32 [[K]], 3 +; CHECK-NEXT: br i1 [[CMP_INNER_PEEL8]], label [[INNER:%.*]], label [[OUTER_BACKEDGE]] +; CHECK: inner: +; CHECK-NEXT: [[J:%.*]] = phi i32 [ [[J_INC:%.*]], [[INNER]] ], [ 3, [[INNER_PEEL6]] ] +; CHECK-NEXT: [[J_INC]] = add nuw nsw i32 [[J]], 1 +; CHECK-NEXT: [[CMP_INNER:%.*]] = icmp slt i32 [[J_INC]], [[K]] +; CHECK-NEXT: br i1 [[CMP_INNER]], label [[INNER]], label [[OUTER_BACKEDGE]], !llvm.loop !0 +; CHECK: outer.backedge: +; CHECK-NEXT: [[I_INC]] = add i32 [[I]], 1 +; CHECK-NEXT: [[CMP_OUTER:%.*]] = icmp slt i32 [[I_INC]], [[N:%.*]] +; CHECK-NOT: !llvm.loop +; CHECK: end: +; +entry: + br label %outer + +outer: + %i = phi i32 [ 0, %entry ], [ %i.inc, %outer.backedge ] + br label %inner + +inner: + %j = phi i32 [ 0, %outer ], [ %j.inc, %inner ] + %j.inc = add i32 %j, 1 + %cmp.inner = icmp slt i32 %j.inc, %K + br i1 %cmp.inner, label %inner, label %outer.backedge, !llvm.loop !1 + +outer.backedge: + %i.inc = add i32 %i, 1 + %cmp.outer = icmp slt i32 %i.inc, %N + br i1 %cmp.outer, label %outer, label %end + +end: + ret void +} + +!1 = distinct !{!1} -- 2.40.0