summaryrefslogtreecommitdiffstats
path: root/dynamic-layers/clang-layer/recipes-devtools/clang/files/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch
diff options
context:
space:
mode:
Diffstat (limited to 'dynamic-layers/clang-layer/recipes-devtools/clang/files/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch')
-rw-r--r--dynamic-layers/clang-layer/recipes-devtools/clang/files/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch146
1 files changed, 146 insertions, 0 deletions
diff --git a/dynamic-layers/clang-layer/recipes-devtools/clang/files/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch b/dynamic-layers/clang-layer/recipes-devtools/clang/files/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch
new file mode 100644
index 00000000..48307deb
--- /dev/null
+++ b/dynamic-layers/clang-layer/recipes-devtools/clang/files/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch
@@ -0,0 +1,146 @@
1From 35e218a886f4c066eabd18685240d55270bd5a6d Mon Sep 17 00:00:00 2001
2From: Aleksander Us <aleksander.us@intel.com>
3Date: Mon, 26 Aug 2019 15:45:47 +0300
4Subject: [PATCH] [IndVarSimplify] Do not use SCEV expander for IVCount in
5 LFTR when possible.
6
7SCEV analysis cannot properly cache instruction with poison flags
8(for example, add nsw outside of loop will not be reused by expander).
9This can lead to generating of additional instructions by SCEV expander.
10
11Example IR:
12
13 ...
14 %maxval = add nuw nsw i32 %a1, %a2
15 ...
16for.body:
17 ...
18 %cmp22 = icmp ult i32 %ivadd, %maxval
19 br i1 %cmp22, label %for.body, label %for.end
20 ...
21
22SCEV expander will generate copy of %maxval in preheader but without
23nuw/nsw flags. This can be avoided by explicit check that iv count
24value gives the same SCEV expressions as calculated by LFTR.
25
26Upstream-Status: Submitted [https://reviews.llvm.org/D66890]
27
28https://github.com/intel/llvm-patches/commit/fd6a6c97341a56fd21bc32bc940afea751312e8f
29
30Signed-off-by: Naveen Saini <naveen.kumar.saini@intel.com>
31---
32 lib/Transforms/Scalar/IndVarSimplify.cpp | 12 +++++++++-
33 test/Transforms/IndVarSimplify/add_nsw.ll | 23 ++++++++++++++++++++
34 test/Transforms/IndVarSimplify/lftr-reuse.ll | 9 +++-----
35 test/Transforms/IndVarSimplify/udiv.ll | 1 +
36 4 files changed, 38 insertions(+), 7 deletions(-)
37 create mode 100644 test/Transforms/IndVarSimplify/add_nsw.ll
38
39diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp
40index f9fc698a4a9..5e04dac8aa6 100644
41--- a/lib/Transforms/Scalar/IndVarSimplify.cpp
42+++ b/lib/Transforms/Scalar/IndVarSimplify.cpp
43@@ -2375,6 +2375,17 @@ static Value *genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB,
44 if (UsePostInc)
45 IVLimit = SE->getAddExpr(IVLimit, SE->getOne(IVLimit->getType()));
46
47+ // If computed limit is equal to old limit then do not use SCEV expander
48+ // because it can lost NUW/NSW flags and create extra instructions.
49+ BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
50+ if (ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getOperand(0))) {
51+ Value *Limit = Cmp->getOperand(0);
52+ if (!L->isLoopInvariant(Limit))
53+ Limit = Cmp->getOperand(1);
54+ if (SE->getSCEV(Limit) == IVLimit)
55+ return Limit;
56+ }
57+
58 // Expand the code for the iteration count.
59 assert(SE->isLoopInvariant(IVLimit, L) &&
60 "Computed iteration count is not loop invariant!");
61@@ -2383,7 +2394,6 @@ static Value *genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB,
62 // SCEV expression (IVInit) for a pointer type IV value (IndVar).
63 Type *LimitTy = ExitCount->getType()->isPointerTy() ?
64 IndVar->getType() : ExitCount->getType();
65- BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
66 return Rewriter.expandCodeFor(IVLimit, LimitTy, BI);
67 }
68 }
69diff --git a/test/Transforms/IndVarSimplify/add_nsw.ll b/test/Transforms/IndVarSimplify/add_nsw.ll
70new file mode 100644
71index 00000000000..abd1cbb6c51
72--- /dev/null
73+++ b/test/Transforms/IndVarSimplify/add_nsw.ll
74@@ -0,0 +1,23 @@
75+; RUN: opt -indvars -S %s | FileCheck %s
76+
77+target datalayout = "e-p:32:32-i64:64-n8:16:32"
78+
79+; CHECK: for.body.preheader:
80+; CHECK-NOT: add
81+; CHECK: for.body:
82+
83+define void @foo(i32 %a1, i32 %a2) {
84+entry:
85+ %maxval = add nuw nsw i32 %a1, %a2
86+ %cmp = icmp slt i32 %maxval, 1
87+ br i1 %cmp, label %for.end, label %for.body
88+
89+for.body: ; preds = %entry, %for.body
90+ %j.02 = phi i32 [ 0, %entry ], [ %add31, %for.body ]
91+ %add31 = add nuw nsw i32 %j.02, 1
92+ %cmp22 = icmp slt i32 %add31, %maxval
93+ br i1 %cmp22, label %for.body, label %for.end
94+
95+for.end: ; preds = %for.body
96+ ret void
97+}
98diff --git a/test/Transforms/IndVarSimplify/lftr-reuse.ll b/test/Transforms/IndVarSimplify/lftr-reuse.ll
99index 14ae9738696..509d662b767 100644
100--- a/test/Transforms/IndVarSimplify/lftr-reuse.ll
101+++ b/test/Transforms/IndVarSimplify/lftr-reuse.ll
102@@ -67,11 +67,9 @@ define void @expandOuterRecurrence(i32 %arg) nounwind {
103 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 0, [[SUB1]]
104 ; CHECK-NEXT: br i1 [[CMP1]], label [[OUTER_PREHEADER:%.*]], label [[EXIT:%.*]]
105 ; CHECK: outer.preheader:
106-; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[ARG]], -1
107 ; CHECK-NEXT: br label [[OUTER:%.*]]
108 ; CHECK: outer:
109-; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i32 [ [[TMP0]], [[OUTER_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[OUTER_INC:%.*]] ]
110-; CHECK-NEXT: [[I:%.*]] = phi i32 [ [[I_INC:%.*]], [[OUTER_INC]] ], [ 0, [[OUTER_PREHEADER]] ]
111+; CHECK-NEXT: [[I:%.*]] = phi i32 [ [[I_INC:%.*]], [[OUTER_INC:%.*]] ], [ 0, [[OUTER_PREHEADER]] ]
112 ; CHECK-NEXT: [[SUB2:%.*]] = sub nsw i32 [[ARG]], [[I]]
113 ; CHECK-NEXT: [[SUB3:%.*]] = sub nsw i32 [[SUB2]], 1
114 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 0, [[SUB3]]
115@@ -81,14 +79,13 @@ define void @expandOuterRecurrence(i32 %arg) nounwind {
116 ; CHECK: inner:
117 ; CHECK-NEXT: [[J:%.*]] = phi i32 [ 0, [[INNER_PH]] ], [ [[J_INC:%.*]], [[INNER]] ]
118 ; CHECK-NEXT: [[J_INC]] = add nuw nsw i32 [[J]], 1
119-; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[J_INC]], [[INDVARS_IV]]
120+; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[J_INC]], [[SUB3]]
121 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[INNER]], label [[OUTER_INC_LOOPEXIT:%.*]]
122 ; CHECK: outer.inc.loopexit:
123 ; CHECK-NEXT: br label [[OUTER_INC]]
124 ; CHECK: outer.inc:
125 ; CHECK-NEXT: [[I_INC]] = add nuw nsw i32 [[I]], 1
126-; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add i32 [[INDVARS_IV]], -1
127-; CHECK-NEXT: [[EXITCOND1:%.*]] = icmp ne i32 [[I_INC]], [[TMP0]]
128+; CHECK-NEXT: [[EXITCOND1:%.*]] = icmp ne i32 [[I_INC]], [[SUB1]]
129 ; CHECK-NEXT: br i1 [[EXITCOND1]], label [[OUTER]], label [[EXIT_LOOPEXIT:%.*]]
130 ; CHECK: exit.loopexit:
131 ; CHECK-NEXT: br label [[EXIT]]
132diff --git a/test/Transforms/IndVarSimplify/udiv.ll b/test/Transforms/IndVarSimplify/udiv.ll
133index b3f2c2a6a66..3530343ef4a 100644
134--- a/test/Transforms/IndVarSimplify/udiv.ll
135+++ b/test/Transforms/IndVarSimplify/udiv.ll
136@@ -133,6 +133,7 @@ declare i32 @printf(i8* nocapture, ...) nounwind
137 ; CHECK-LABEL: @foo(
138 ; CHECK: for.body.preheader:
139 ; CHECK-NOT: udiv
140+; CHECK: for.body:
141
142 define void @foo(double* %p, i64 %n) nounwind {
143 entry:
144--
1452.18.0
146