<div dir="ltr">r314531 should fix this.<br><div><br></div><div>Thanks</div><div>hongbin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 29, 2017 at 5:04 AM, NAKAMURA Takumi <span dir="ltr"><<a href="mailto:geek4civic@gmail.com" target="_blank">geek4civic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">It crashed during LTO.<div><a href="http://bb.pgr.jp/builders/clang-3stage-x86_64-linux/builds/38" target="_blank">http://bb.pgr.jp/builders/<wbr>clang-3stage-x86_64-linux/<wbr>builds/38</a><br></div><div><br></div><div>Reduced testscase attached. "opt -indvars" should reproduce.</div><div><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Wed, Sep 27, 2017 at 12:13 PM Hongbin Zheng via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: ether<br>
Date: Tue Sep 26 20:11:46 2017<br>
New Revision: 314266<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=314266&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=314266&view=rev</a><br>
Log:<br>
[SimplifyIndVar] Constant fold IV users<br>
<br>
This patch tries to transform cases like:<br>
<br>
for (unsigned i = 0; i < N; i += 2) {<br>
  bool c0 = (i & 0x1) == 0;<br>
  bool c1 = ((i + 1) & 0x1) == 1;<br>
}<br>
To<br>
<br>
for (unsigned i = 0; i < N; i += 2) {<br>
  bool c0 = true;<br>
  bool c1 = true;<br>
}<br>
<br>
This commit also update test/Transforms/<wbr>IndVarSimplify/replace-srem-<wbr>by-urem.ll to prevent constant folding.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D38272" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D38272</a><br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/<wbr>IndVarSimplify/constant-fold.<wbr>ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/<wbr>Utils/SimplifyIndVar.cpp<br>
    llvm/trunk/test/Transforms/<wbr>IndVarSimplify/replace-srem-<wbr>by-urem.ll<br>
<br>
Modified: llvm/trunk/lib/Transforms/<wbr>Utils/SimplifyIndVar.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp?rev=314266&r1=314265&r2=314266&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/<wbr>Transforms/Utils/<wbr>SimplifyIndVar.cpp?rev=314266&<wbr>r1=314265&r2=314266&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Transforms/<wbr>Utils/SimplifyIndVar.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/<wbr>Utils/SimplifyIndVar.cpp Tue Sep 26 20:11:46 2017<br>
@@ -35,6 +35,7 @@ using namespace llvm;<br>
<br>
 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");<br>
 STATISTIC(NumElimOperand,  "Number of IV operands folded into a use");<br>
+STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");<br>
 STATISTIC(NumElimRem     , "Number of IV remainder operations eliminated");<br>
 STATISTIC(<br>
     NumSimplifiedSDiv,<br>
@@ -76,6 +77,7 @@ namespace {<br>
     Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);<br>
<br>
     bool eliminateIdentitySCEV(<wbr>Instruction *UseInst, Instruction *IVOperand);<br>
+    bool foldConstantSCEV(Instruction *UseInst);<br>
<br>
     bool eliminateOverflowIntrinsic(<wbr>CallInst *CI);<br>
     bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);<br>
@@ -534,6 +536,30 @@ bool SimplifyIndvar::<wbr>eliminateIVUser(Ins<br>
   return false;<br>
 }<br>
<br>
+/// Replace the UseInst with a constant if possible<br>
+bool SimplifyIndvar::<wbr>foldConstantSCEV(Instruction *I) {<br>
+  if (!SE->isSCEVable(I->getType())<wbr>)<br>
+    return false;<br>
+<br>
+  // Get the symbolic expression for this instruction.<br>
+  const SCEV *S = SE->getSCEV(I);<br>
+<br>
+  const Loop *L = LI->getLoopFor(I->getParent())<wbr>;<br>
+  S = SE->getSCEVAtScope(S, L);<br>
+<br>
+  if (auto *C = dyn_cast<SCEVConstant>(S)) {<br>
+    I->replaceAllUsesWith(C-><wbr>getValue());<br>
+    DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I<br>
+                 << " with constant: " << *C << '\n');<br>
+    ++NumFoldedUser;<br>
+    Changed = true;<br>
+    DeadInsts.emplace_back(I);<br>
+    return true;<br>
+  }<br>
+<br>
+  return false;<br>
+}<br>
+<br>
 /// Eliminate any operation that SCEV can prove is an identity function.<br>
 bool SimplifyIndvar::<wbr>eliminateIdentitySCEV(<wbr>Instruction *UseInst,<br>
                                            Instruction *IVOperand) {<br>
@@ -741,6 +767,10 @@ void SimplifyIndvar::simplifyUsers(<wbr>PHINo<br>
     // Bypass back edges to avoid extra work.<br>
     if (UseInst == CurrIV) continue;<br>
<br>
+    // Try to replace UseInst with a constant before any other simplifications<br>
+    if (foldConstantSCEV(UseInst))<br>
+      continue;<br>
+<br>
     Instruction *IVOperand = UseOper.second;<br>
     for (unsigned N = 0; IVOperand; ++N) {<br>
       assert(N <= Simplified.size() && "runaway iteration");<br>
<br>
Added: llvm/trunk/test/Transforms/<wbr>IndVarSimplify/constant-fold.<wbr>ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/constant-fold.ll?rev=314266&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/<wbr>Transforms/IndVarSimplify/<wbr>constant-fold.ll?rev=314266&<wbr>view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/Transforms/<wbr>IndVarSimplify/constant-fold.<wbr>ll (added)<br>
+++ llvm/trunk/test/Transforms/<wbr>IndVarSimplify/constant-fold.<wbr>ll Tue Sep 26 20:11:46 2017<br>
@@ -0,0 +1,47 @@<br>
+; RUN: opt -indvars -S < %s | FileCheck %s<br>
+<br>
+define void @test0(i32* %x) {<br>
+entry:<br>
+  br label %for.inc<br>
+<br>
+for.inc:                                          ; preds = %for.inc, %entry<br>
+  %i.01 = phi i32 [ 0, %entry ], [ %add, %for.inc ]<br>
+  %and = and i32 %i.01, 3<br>
+  %cmp1 = icmp eq i32 %and, 0<br>
+  %cond = select i1 %cmp1, i32 0, i32 1<br>
+  store i32 %cond, i32* %x, align 4<br>
+  %add = add i32 %i.01, 4<br>
+  %cmp = icmp ult i32 %add, 8<br>
+  br i1 %cmp, label %for.inc, label %for.end<br>
+<br>
+for.end:                                          ; preds = %for.inc<br>
+  ret void<br>
+}<br>
+<br>
+; Should fold the condition of the select into constant<br>
+; CHECK-LABEL: void @test<br>
+; CHECK:         icmp eq i32 0, 0<br>
+<br>
+define void @test1(i32* %a) {<br>
+entry:<br>
+  br label %for.body<br>
+<br>
+for.body:                                         ; preds = %entry, %for.body<br>
+  %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]<br>
+  %mul = mul nsw i32 %i.01, 64<br>
+  %rem = srem i32 %mul, 8<br>
+  %idxprom = sext i32 %rem to i64<br>
+  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom<br>
+  store i32 %i.01, i32* %arrayidx, align 4<br>
+  %inc = add nsw i32 %i.01, 1<br>
+  %cmp = icmp slt i32 %inc, 64<br>
+  br i1 %cmp, label %for.body, label %for.end<br>
+<br>
+for.end:                                          ; preds = %for.body<br>
+  ret void<br>
+}<br>
+<br>
+; Should fold the rem since %mul is multiple of 8<br>
+; CHECK-LABEL: @test1(<br>
+; CHECK-NOT:     rem<br>
+; CHECK:         sext i32 0 to i64<br>
<br>
Modified: llvm/trunk/test/Transforms/<wbr>IndVarSimplify/replace-srem-<wbr>by-urem.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/replace-srem-by-urem.ll?rev=314266&r1=314265&r2=314266&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/<wbr>Transforms/IndVarSimplify/<wbr>replace-srem-by-urem.ll?rev=<wbr>314266&r1=314265&r2=314266&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/Transforms/<wbr>IndVarSimplify/replace-srem-<wbr>by-urem.ll (original)<br>
+++ llvm/trunk/test/Transforms/<wbr>IndVarSimplify/replace-srem-<wbr>by-urem.ll Tue Sep 26 20:11:46 2017<br>
@@ -71,7 +71,7 @@ entry:<br>
 for.body:                                         ; preds = %entry, %for.body<br>
   %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]<br>
   %mul = mul nsw i32 %i.01, 64<br>
-  %rem = srem i32 %mul, 8<br>
+  %rem = srem i32 %mul, 7<br>
 ; CHECK:     urem<br>
 ; CHECK-NOT: srem<br>
   %idxprom = sext i32 %rem to i64<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div></div></div>
</blockquote></div><br></div>