[llvm-dev] [IndVars] Rewriting exit value of SCEV add expressions

Bakhvalov, Denis via llvm-dev llvm-dev at lists.llvm.org
Mon Mar 25 11:57:50 PDT 2019


Hi,

I found issues with INDVARS <-> LSR passes interactions after https://reviews.llvm.org/rL346397.

There were two main changes in this commit:
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.
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.

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.

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.

Here is the reproducer:

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@c = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global i32*, align 8
@b = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global i32, align 4

define i32 @foo(){
entry:
  %0 = load i32*, i32** @a, align 8
  %.pre = load i32, i32* @c, align 4
  %.pre2 = load i32, i32* @d, align 4
  br label %do.body

do.body:                                          ; preds = %do.body, %entry
  %1 = phi i32 [ %dec, %do.body ], [ %.pre2, %entry ]
  %2 = phi i32 [ %inc, %do.body ], [ %.pre, %entry ]
  %inc = add nsw i32 %2, 1
  store i32 %inc, i32* @c, align 4
  %3 = load i32, i32* %0, align 4
  store i32 %3, i32* @b, align 4
  %dec = add nsw i32 %1, -1
  store i32 %dec, i32* @d, align 4
  %tobool = icmp eq i32 %dec, 0
  br i1 %tobool, label %do.end, label %do.body

do.end:                                           ; preds = %do.body
  %.lcssa = phi i32 [ %2, %do.body ]
  %inc1 = add nsw i32 %.lcssa, 2
  store i32 %inc1, i32* @c, align 4
  ret i32 undef
}

# Run this before and after rL346397:
$ opt a.ll -indvars -loop-reduce -S -o b.ll

After rL346397 we have additional ADD instruction inside the loop which is a performance regression.

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.

Simple fix would be just returning back original check for SCEVType in lib/Transforms/Scalar/IndVarSimplify.cpp:
-        if (!isa<SCEVConstant>(ExitValue) && hasHardUserWithinLoop(L, Inst))
+        if ((ExitValue->getSCEVType() >= scMulExpr) && hasHardUserWithinLoop(L, Inst))

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.

I'm not opening a new bug yet, but rather want to hear your comments.

Best regards,
Denis Bakhvalov.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190325/268e5abb/attachment.html>


More information about the llvm-dev mailing list