summaryrefslogtreecommitdiffstats
path: root/dynamic-layers/clang-layer/recipes-devtools/clang/files/BasicBlockUtils-Add-metadata-fixing-in-SplitBlockPre.patch
blob: cd519971ccb43b619d64de34d8924ff0fb310a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
From eeb816d95f0910bd246e37bb2bb3923acf0edf6b Mon Sep 17 00:00:00 2001
From: Aleksander Us <aleksander.us@intel.com>
Date: Mon, 26 Aug 2019 15:47:41 +0300
Subject: [PATCH] [BasicBlockUtils] Add metadata fixing in
 SplitBlockPredecessors.

In case when BB is header of some loop and predecessor is latch of
this loop, metadata was not attached to newly created basic block.
This led to loss of loop metadata for other passes.

Upstream-Status: Submitted [https://reviews.llvm.org/D66892]

https://github.com/intel/llvm-patches/commit/8af4449e2d201707f7f2f832b473a0439e255f32

Signed-off-by: Naveen Saini <naveen.kumar.saini@intel.com>
---
 lib/Transforms/Utils/BasicBlockUtils.cpp      | 23 ++++++++----
 test/Transforms/LoopSimplify/loop_metadata.ll | 36 +++++++++++++++++++
 2 files changed, 52 insertions(+), 7 deletions(-)
 create mode 100644 test/Transforms/LoopSimplify/loop_metadata.ll

diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp
index 5fa371377c8..3a90ae061fb 100644
--- a/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -579,24 +579,33 @@ BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
 
   // The new block unconditionally branches to the old block.
   BranchInst *BI = BranchInst::Create(BB, NewBB);
+  bool IsBBHeader = LI && LI->isLoopHeader(BB);
+  Loop *BBLoop = LI ? LI->getLoopFor(BB) : nullptr;
   // Splitting the predecessors of a loop header creates a preheader block.
-  if (LI && LI->isLoopHeader(BB))
+  if (IsBBHeader)
     // Using the loop start line number prevents debuggers stepping into the
     // loop body for this instruction.
-    BI->setDebugLoc(LI->getLoopFor(BB)->getStartLoc());
+    BI->setDebugLoc(BBLoop->getStartLoc());
   else
     BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
 
   // Move the edges from Preds to point to NewBB instead of BB.
-  for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
+  for (BasicBlock *Pred : Preds) {
+    Instruction *PI = Pred->getTerminator();
     // This is slightly more strict than necessary; the minimum requirement
     // is that there be no more than one indirectbr branching to BB. And
     // all BlockAddress uses would need to be updated.
-    assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
+    assert(!isa<IndirectBrInst>(PI) &&
            "Cannot split an edge from an IndirectBrInst");
-    assert(!isa<CallBrInst>(Preds[i]->getTerminator()) &&
-           "Cannot split an edge from a CallBrInst");
-    Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
+    assert(!isa<CallBrInst>(PI) && "Cannot split an edge from a CallBrInst");
+    if (IsBBHeader && BBLoop->contains(Pred) && BBLoop->isLoopLatch(Pred)) {
+      // Update loop metadata if it exists.
+      if (MDNode *LoopMD = PI->getMetadata(LLVMContext::MD_loop)) {
+        BI->setMetadata(LLVMContext::MD_loop, LoopMD);
+        PI->setMetadata(LLVMContext::MD_loop, nullptr);
+      }
+    }
+    PI->replaceUsesOfWith(BB, NewBB);
   }
 
   // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
diff --git a/test/Transforms/LoopSimplify/loop_metadata.ll b/test/Transforms/LoopSimplify/loop_metadata.ll
new file mode 100644
index 00000000000..c15c92fe3ae
--- /dev/null
+++ b/test/Transforms/LoopSimplify/loop_metadata.ll
@@ -0,0 +1,36 @@
+; RUN: opt -S -loop-simplify < %s | FileCheck %s
+
+; CHECK: for.cond.loopexit:
+; CHECK: br label %for.cond, !llvm.loop !0
+; CHECK: br i1 %cmp1, label %for.body1, label %for.cond.loopexit
+
+define void @foo() {
+entry:
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.cond1, %entry
+  %j = phi i32 [ 0, %entry ], [ %add, %for.cond1 ]
+  %cmp = icmp ult i32 %j, 8
+  br i1 %cmp, label %for.body, label %for.end
+
+for.body:                                         ; preds = %for.cond
+  %dummy1 = add i32 1, 1
+  %add = add nuw nsw i32 %j, 1
+  br label %for.cond1
+
+for.cond1:                                      ; preds = %for.body1, %for.body
+  %i.0 = phi i32 [ 1, %for.body ], [ %inc, %for.body1 ]
+  %cmp1 = icmp ult i32 %i.0, 8
+  br i1 %cmp1, label %for.body1, label %for.cond, !llvm.loop !0
+
+for.body1:                                      ; preds = %for.cond1
+  %dummy2 = add i32 1, 1
+  %inc = add nuw nsw i32 %i.0, 1
+  br label %for.cond1
+
+for.end:                                       ; preds = %for.cond
+  ret void
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.unroll.full"}
-- 
2.18.0