<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="PL" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I found issues with INDVARS <-> LSR passes interactions after https://reviews.llvm.org/rL346397.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">There were two main changes in this commit:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">1. Previously we were propagating SCEV add expressions even if DefInst of exit value has hard uses inside the loop. After rL346397 we forbid propagation of SCEV add expressions if DefInst of exit value has hard use inside
the loop.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">2. Previously when checking for hard uses, we were only looking for immediate uses of DefInst. After rL346397 we are going down through the Def-Use chain.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">As I understand the motivation for this was that if Def instruction of the exit value will stay in the loop due to hard use (i.e. it can't be easily eliminated), there is no benefit in rewriting exit value.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">But I think it is profitable in some cases, because it helps reducing the loop strength. And in that sense we regressed here, because before rL346397 we would rewrite the exit value of SCEV add expression even if there
is hard use inside the loop.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Here is the reproducer:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">target triple = "x86_64-unknown-linux-gnu"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">@c = external dso_local local_unnamed_addr global i32, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">@a = external dso_local local_unnamed_addr global i32*, align 8<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">@b = external dso_local local_unnamed_addr global i32, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">@d = external dso_local local_unnamed_addr global i32, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">define i32 @foo(){<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">entry:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %0 = load i32*, i32** @a, align 8<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %.pre = load i32, i32* @c, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %.pre2 = load i32, i32* @d, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> br label %do.body<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">do.body: ; preds = %do.body, %entry<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %1 = phi i32 [ %dec, %do.body ], [ %.pre2, %entry ]<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %2 = phi i32 [ %inc, %do.body ], [ %.pre, %entry ]<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %inc = add nsw i32 %2, 1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> store i32 %inc, i32* @c, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %3 = load i32, i32* %0, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> store i32 %3, i32* @b, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %dec = add nsw i32 %1, -1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> store i32 %dec, i32* @d, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %tobool = icmp eq i32 %dec, 0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> br i1 %tobool, label %do.end, label %do.body<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">do.end: ; preds = %do.body<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %.lcssa = phi i32 [ %2, %do.body ]<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> %inc1 = add nsw i32 %.lcssa, 2<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> store i32 %inc1, i32* @c, align 4<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> ret i32 undef<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">}<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># Run this before and after rL346397:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">$ opt a.ll -indvars -loop-reduce -S -o b.ll
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">After rL346397 we have additional ADD instruction inside the loop which is a performance regression.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I think after rL346397 LSR is not able to rewrite all the uses of %2 because phi node (%.lcssa) was not rewritten by indvars pass.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Simple fix would be just returning back original check for SCEVType in lib/Transforms/Scalar/IndVarSimplify.cpp:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">- if (!isa<SCEVConstant>(ExitValue) && hasHardUserWithinLoop(L, Inst))<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">+ if ((ExitValue->getSCEVType() >= scMulExpr) && hasHardUserWithinLoop(L, Inst))<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Note, there is another bug opened for the mentioned patch: https://bugs.llvm.org/show_bug.cgi?id=39673, but it only fixes the problem when the SCEV expression type is constant.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I'm not opening a new bug yet, but rather want to hear your comments.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:PL">Best regards,<br>
Denis Bakhvalov.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</body>
</html>