Debian Codegraph @

external/llvm/releases/9.0.0/patches_external/IndVarSimplify-Do-not-use-SCEV-expander-for-IVCount-.patch

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