[llvm] r347289 - Recommit "[LoopSimplifyCFG] Teach LoopSimplifyCFG to constant-fold branches and switches"

Mikael Holmén via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 23 01:05:26 PST 2018



On 11/23/18 10:03 AM, Maxim Kazantsev wrote:
> It's not jump threading, here's what I see with opt -jump-threading -loop-simplifycfg -S -print-before-all -print-after-all -verify-loop-info -verify-dom-info -verify-loop-lcssa
> 
> 
> *** IR Dump Before Simplify loop CFG ***
> ; Preheader:
> entry:
>    br label %for.cond
> 
> ; Loop:
> for.cond:                                         ; preds = %for.inc, %entry
>    br i1 icmp ne (void () addrspace(40)* @f1, void () addrspace(40)* null), label %if.then, label %for.inc
> 
> if.then:                                          ; preds = %for.cond
>    %0 = load i16, i16* @a, align 1
>    %tobool = icmp ne i16 %0, 0
>    br i1 %tobool, label %for.inc, label %for.inc
> 
> for.inc:                                          ; preds = %if.then, %if.then, %for.cond
>    %c.1 = phi i16 [ undef, %for.cond ], [ undef, %if.then ], [ undef, %if.then ]
>    br label %for.cond
> *** IR Dump After Simplify loop CFG ***
> ; Preheader:
> entry:
>    br label %for.cond
> 
> ; Loop:
> for.cond:                                         ; preds = %for.inc, %entry
>    br i1 icmp ne (void () addrspace(40)* @f1, void () addrspace(40)* null), label %if.then, label %for.inc
> 
> if.then:                                          ; preds = %for.cond
>    %0 = load i16, i16* @a, align 1
>    %tobool = icmp ne i16 %0, 0
>    br label %for.inc
> 
> for.inc:                                          ; preds = %if.then, %for.cond
>    %c.1 = phi i16 [ undef, %for.cond ], [ undef, %if.then ], [ undef, %if.then ]
>    br label %for.cond
> 
> So it used to be a predecessor twice, and now it is a predecessor only once and the Phi wasn't properly fixed by removePredecessor... It's crap. :( I will temporarily disable my opt and see how this can be fixed.

:(

Ok, thanks anyway :)
/Mikael

> 
> --Max
> 
> -----Original Message-----
> From: Mikael Holmén <mikael.holmen at ericsson.com>
> Sent: Friday, November 23, 2018 3:36 PM
> To: Maxim Kazantsev <max.kazantsev at azul.com>
> Cc: llvm-commits at lists.llvm.org
> Subject: Re: [llvm] r347289 - Recommit "[LoopSimplifyCFG] Teach LoopSimplifyCFG to constant-fold branches and switches"
> 
> Hi again,
> 
> This starts failing with r347289 and still fails on trunk now with r347484:
> 
>    opt -S -o - bbi-21358.ll -jump-threading -loop-simplifycfg
> 
> It gives me
> 
> PHINode should have one entry for each predecessor of its parent basic block!
>     %c.1 = phi i16 [ undef, %for.cond ], [ undef, %if.then ], [ undef, %if.then ] in function f1 LLVM ERROR: Broken function found, compilation aborted!
> 
> But I wonder if this is a bug in jump-threading that now just got exposed with your change for some reason?
> 
> After jump-threading we have this:
> 
> if.then:                                          ; preds = %for.cond
>     %0 = load i16, i16* @a, align 1
>     %tobool = icmp ne i16 %0, 0
>     br i1 %tobool, label %for.inc, label %for.inc
> 
> for.inc:                                          ; preds = %if.then,
> %if.then, %for.cond
>     %c.1 = phi i16 [ undef, %for.cond ], [ undef, %if.then ], [ undef, %if.then ]
>     br label %for.cond
> 
> Is that phi ok with %if.then appearing two times?
> 
> If I run just
>    -jump-threading -verify
> or
>    -jump-threading -verify-loop-info
> it passes but with
>    -jump-threading -loop-simplifycfg
> it fails.
> 
> /Mikael
> 
> On 11/23/18 9:13 AM, Mikael Holmén wrote:
>> Hi,
>>
>> Yep I just verified that my bbi-21357.ll case doesn't hit the
>> assertion anymore.
>>
>> I'll see what happens with the other suspected cases as well.
>>
>> Thanks!
>> /Mikael
>>
>> On 11/23/18 9:10 AM, Maxim Kazantsev wrote:
>>> On my side it passed with the patch. Seems the same problem found by
>>> Dave earlier. Fixed now.
>>>
>>> --Max
>>>
>>> -----Original Message-----
>>> From: Maxim Kazantsev
>>> Sent: Friday, November 23, 2018 3:00 PM
>>> To: 'Mikael Holmén' <mikael.holmen at ericsson.com>
>>> Cc: llvm-commits at lists.llvm.org
>>> Subject: RE: [llvm] r347289 - Recommit "[LoopSimplifyCFG] Teach
>>> LoopSimplifyCFG to constant-fold branches and switches"
>>>
>>> Hi Mikael,
>>>
>>> I've just merged https://reviews.llvm.org/rL347484, could you please
>>> check if it helps?
>>>
>>> --Max
>>>
>>> -----Original Message-----
>>> From: Mikael Holmén <mikael.holmen at ericsson.com>
>>> Sent: Friday, November 23, 2018 2:42 PM
>>> To: Maxim Kazantsev <max.kazantsev at azul.com>
>>> Cc: llvm-commits at lists.llvm.org
>>> Subject: Re: [llvm] r347289 - Recommit "[LoopSimplifyCFG] Teach
>>> LoopSimplifyCFG to constant-fold branches and switches"
>>>
>>> Hi Max,
>>>
>>> With this commit the following starts crashing:
>>>
>>>     opt -o /dev/null bbi-21357.ll -loop-simplifycfg -loop-deletion
>>>
>>> I get:
>>>
>>> opt: ../lib/Transforms/Scalar/LoopDeletion.cpp:138:
>>> LoopDeletionResult deleteLoopIfDead(llvm::Loop *, llvm::DominatorTree
>>> &, llvm::ScalarEvolution &, llvm::LoopInfo &): Assertion
>>> `L->isLCSSAForm(DT) && "Expected LCSSA!"' failed.
>>> Stack dump:
>>> 0.      Program arguments: build-all/bin/opt -o /dev/null
>>> bbi-21357.ll -loop-simplifycfg -loop-deletion 1.      Running pass
>>> 'Function Pass Manager' on module 'bbi-21357.ll'.
>>> 2.      Running pass 'Loop Pass Manager' on function '@f1'
>>> 3.      Running pass 'Delete dead loops' on basic block '%bb1'
>>> #0 0x00000000021e8eb4 PrintStackTraceSignalHandler(void*)
>>> (build-all/bin/opt+0x21e8eb4)
>>> #1 0x00000000021e6fe0 llvm::sys::RunSignalHandlers()
>>> (build-all/bin/opt+0x21e6fe0)
>>> #2 0x00000000021e9218 SignalHandler(int)
>>> (build-all/bin/opt+0x21e9218)
>>> #3 0x00007f6880ec6330 __restore_rt
>>> (/lib/x86_64-linux-gnu/libpthread.so.0+0x10330)
>>> #4 0x00007f687fab5c37 gsignal
>>> /build/eglibc-ripdx6/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/lin
>>> ux/raise.c:56:0
>>>
>>> #5 0x00007f687fab9028 abort
>>> /build/eglibc-ripdx6/eglibc-2.19/stdlib/abort.c:91:0
>>> #6 0x00007f687faaebf6 __assert_fail_base
>>> /build/eglibc-ripdx6/eglibc-2.19/assert/assert.c:92:0
>>> #7 0x00007f687faaeca2 (/lib/x86_64-linux-gnu/libc.so.6+0x2fca2)
>>> #8 0x0000000001ffa3a4 deleteLoopIfDead(llvm::Loop*,
>>> llvm::DominatorTree&, llvm::ScalarEvolution&, llvm::LoopInfo&)
>>> (build-all/bin/opt+0x1ffa3a4)
>>> #9 0x0000000001ffa8b6 (anonymous
>>> namespace)::LoopDeletionLegacyPass::runOnLoop(llvm::Loop*,
>>> llvm::LPPassManager&) (build-all/bin/opt+0x1ffa8b6)
>>> #10 0x000000000167a58c
>>> llvm::LPPassManager::runOnFunction(llvm::Function&)
>>> (build-all/bin/opt+0x167a58c)
>>> #11 0x0000000001c0797d
>>> llvm::FPPassManager::runOnFunction(llvm::Function&)
>>> (build-all/bin/opt+0x1c0797d)
>>> #12 0x0000000001c07c38
>>> llvm::FPPassManager::runOnModule(llvm::Module&)
>>> (build-all/bin/opt+0x1c07c38)
>>> #13 0x0000000001c0809a
>>> llvm::legacy::PassManagerImpl::run(llvm::Module&)
>>> (build-all/bin/opt+0x1c0809a)
>>> #14 0x000000000077b08b main (build-all/bin/opt+0x77b08b)
>>> #15 0x00007f687faa0f45 __libc_start_main
>>> /build/eglibc-ripdx6/eglibc-2.19/csu/libc-start.c:321:0
>>> #16 0x00000000007605fd _start (build-all/bin/opt+0x7605fd)
>>>
>>> I've seen a couple of other failures too recently, e.g.
>>>
>>>     PHINode should have one entry for each predecessor of its parent
>>> basic block!
>>>
>>> and also
>>>
>>> opt: ../lib/Transforms/Scalar/IndVarSimplify.cpp:2585: bool
>>> (anonymous namespace)::IndVarSimplify::run(llvm::Loop *): Assertion
>>> `L->isRecursivelyLCSSAForm(*DT, *LI) && "LCSSA required to run
>>> indvars!"' failed."
>>>
>>> but I haven't reduced and really verified they are due to this commit
>>> yet.
>>>
>>> /Mikael
>>>
>>> On 11/20/18 6:43 AM, Max Kazantsev via llvm-commits wrote:
>>>> Author: mkazantsev
>>>> Date: Mon Nov 19 21:43:32 2018
>>>> New Revision: 347289
>>>>
>>>> URL: http://llvm.org/viewvc/llvm-project?rev=347289&view=rev
>>>> Log:
>>>> Recommit "[LoopSimplifyCFG] Teach LoopSimplifyCFG to constant-fold
>>>> branches and switches"
>>>>
>>>> The initial version of patch lacked Phi nodes updates in
>>>> destinations of removed edges. This version contains this update and
>>>> tests on this situation.
>>>>
>>>> Differential Revision: https://reviews.llvm.org/D54021
>>>>
>>>> Modified:
>>>>        llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
>>>> llvm/trunk/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll
>>>>
>>>> Modified: llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
>>>> URL:
>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar
>>>> /L oopSimplifyCFG.cpp?rev=347289&r1=347288&r2=347289&view=diff
>>>> ====================================================================
>>>> ==
>>>> ========
>>>> --- llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp (original)
>>>> +++ llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp Mon Nov 19
>>>> +++ 21:43:32 2018
>>>> @@ -41,6 +41,318 @@ using namespace llvm;
>>>>     #define DEBUG_TYPE "loop-simplifycfg"
>>>> +STATISTIC(NumTerminatorsFolded,
>>>> +          "Number of terminators folded to unconditional
>>>> +branches");
>>>> +
>>>> +/// If \p BB is a switch or a conditional branch, but only one of
>>>> +its successors /// can be reached from this block in runtime,
>>>> +return this successor. Otherwise, /// return nullptr.
>>>> +static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) {
>>>> +  Instruction *TI = BB->getTerminator();
>>>> +  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
>>>> +    if (BI->isUnconditional())
>>>> +      return nullptr;
>>>> +    if (BI->getSuccessor(0) == BI->getSuccessor(1))
>>>> +      return BI->getSuccessor(0);
>>>> +    ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
>>>> +    if (!Cond)
>>>> +      return nullptr;
>>>> +    return Cond->isZero() ? BI->getSuccessor(1) :
>>>> +BI->getSuccessor(0);
>>>> +  }
>>>> +
>>>> +  if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
>>>> +    auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
>>>> +    if (!CI)
>>>> +      return nullptr;
>>>> +    for (auto Case : SI->cases())
>>>> +      if (Case.getCaseValue() == CI)
>>>> +        return Case.getCaseSuccessor();
>>>> +    return SI->getDefaultDest();
>>>> +  }
>>>> +
>>>> +  return nullptr;
>>>> +}
>>>> +
>>>> +/// Helper class that can turn branches and switches with constant
>>>> +conditions /// into unconditional branches.
>>>> +class ConstantTerminatorFoldingImpl {
>>>> +private:
>>>> +  Loop &L;
>>>> +  LoopInfo &LI;
>>>> +  DominatorTree &DT;
>>>> +
>>>> +  // Whether or not the current loop will still exist after
>>>> +terminator constant  // folding will be done. In theory, there are
>>>> two ways how it can happen:
>>>> +  // 1. Loop's latch(es) become unreachable from loop header;  // 2.
>>>> + Loop's header becomes unreachable from method entry.
>>>> +  // In practice, the second situation is impossible because we
>>>> +only  modify the  // current loop and its preheader and do not
>>>> +affect  preheader's reachibility  // from any other block. So this
>>>> +variable  set to true means that loop's latch  // has become
>>>> +unreachable from
>>>> loop header.
>>>> +  bool DeleteCurrentLoop = false;
>>>> +
>>>> +  // The blocks of the original loop that will still be reachable
>>>> +from entry  // after the constant folding.
>>>> +  SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;  // The blocks of
>>>> +the  original loop that will become unreachable from entry  //
>>>> +after the  constant folding.
>>>> +  SmallPtrSet<BasicBlock *, 8> DeadLoopBlocks;  // The exits of the
>>>> +original loop that will still be reachable from entry  // after the
>>>> +constant folding.
>>>> +  SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;  // The exits of the
>>>> +original loop that will become unreachable from entry  // after the
>>>> +constant folding.
>>>> +  SmallPtrSet<BasicBlock *, 8> DeadExitBlocks;  // The blocks that
>>>> +will still be a part of the current loop after folding.
>>>> +  SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;  // The
>>>> +blocks that have terminators with constant condition that can be
>>>> +//  folded. Note: fold candidates should be in L but not in any of
>>>> +its  // subloops to avoid complex LI updates.
>>>> +  SmallVector<BasicBlock *, 8> FoldCandidates;
>>>> +
>>>> +  void dump() const {
>>>> +    dbgs() << "Constant terminator folding for loop " << L << "\n";
>>>> +    dbgs() << "After terminator constant-folding, the loop will";
>>>> +    if (!DeleteCurrentLoop)
>>>> +      dbgs() << " not";
>>>> +    dbgs() << " be destroyed\n";
>>>> +    dbgs() << "Blocks in which we can constant-fold terminator:\n";
>>>> +    for (const BasicBlock *BB : FoldCandidates)
>>>> +      dbgs() << "\t" << BB->getName() << "\n";
>>>> +    auto PrintOutSet = [&](const char *Message,
>>>> +                           const SmallPtrSetImpl<BasicBlock *> &S)
>>>> +{
>>>> +      dbgs() << Message << "\n";
>>>> +      for (const BasicBlock *BB : S)
>>>> +        dbgs() << "\t" << BB->getName() << "\n";
>>>> +    };
>>>> +    PrintOutSet("Live blocks from the original loop:",
>>>> +LiveLoopBlocks);
>>>> +    PrintOutSet("Dead blocks from the original loop:",
>>>> +DeadLoopBlocks);
>>>> +    PrintOutSet("Live exit blocks:", LiveExitBlocks);
>>>> +    PrintOutSet("Dead exit blocks:", DeadExitBlocks);
>>>> +    if (!DeleteCurrentLoop)
>>>> +      PrintOutSet("The following blocks will still be part of the
>>>> loop:",
>>>> +                  BlocksInLoopAfterFolding);  }
>>>> +
>>>> +  /// Fill all information about status of blocks and exits of the
>>>> +current loop  /// if constant folding of all branches will be done.
>>>> +  void analyze() {
>>>> +    LoopBlocksDFS DFS(&L);
>>>> +    DFS.perform(&LI);
>>>> +    assert(DFS.isComplete() && "DFS is expected to be finished");
>>>> +
>>>> +    // Collect live and dead loop blocks and exits.
>>>> +    SmallPtrSet<BasicBlock *, 8> ExitBlocks;
>>>> +    LiveLoopBlocks.insert(L.getHeader());
>>>> +    for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
>>>> +      BasicBlock *BB = *I;
>>>> +
>>>> +      // If a loop block wasn't marked as live so far, then it's dead.
>>>> +      if (!LiveLoopBlocks.count(BB)) {
>>>> +        DeadLoopBlocks.insert(BB);
>>>> +        continue;
>>>> +      }
>>>> +
>>>> +      BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
>>>> +
>>>> +      // If a block has only one live successor, it's a candidate
>>>> +on
>>>> constant
>>>> +      // folding. Only handle blocks from current loop: branches in
>>>> child loops
>>>> +      // are skipped because if they can be folded, they should be
>>>> folded during
>>>> +      // the processing of child loops.
>>>> +      if (TheOnlySucc && LI.getLoopFor(BB) == &L)
>>>> +        FoldCandidates.push_back(BB);
>>>> +
>>>> +      // Handle successors.
>>>> +      auto ProcessSuccessor = [&](BasicBlock *Succ, bool IsLive) {
>>>> +        if (!L.contains(Succ)) {
>>>> +          if (IsLive)
>>>> +            LiveExitBlocks.insert(Succ);
>>>> +          ExitBlocks.insert(Succ);
>>>> +        } else if (IsLive)
>>>> +          LiveLoopBlocks.insert(Succ);
>>>> +      };
>>>> +      for (BasicBlock *Succ : successors(BB))
>>>> +        ProcessSuccessor(Succ, !TheOnlySucc || TheOnlySucc ==
>>>> +Succ);
>>>> +    }
>>>> +
>>>> +    // Sanity check: amount of dead and live loop blocks should
>>>> match the total
>>>> +    // number of blocks in loop.
>>>> +    assert(L.getNumBlocks() == LiveLoopBlocks.size() +
>>>> DeadLoopBlocks.size() &&
>>>> +           "Malformed block sets?");
>>>> +
>>>> +    // Now, all exit blocks that are not marked as live are dead.
>>>> +    for (auto *ExitBlock : ExitBlocks)
>>>> +      if (!LiveExitBlocks.count(ExitBlock))
>>>> +        DeadExitBlocks.insert(ExitBlock);
>>>> +
>>>> +    // Whether or not the edge From->To will still be present in
>>>> graph after the
>>>> +    // folding.
>>>> +    auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) {
>>>> +      if (!LiveLoopBlocks.count(From))
>>>> +        return false;
>>>> +      BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From);
>>>> +      return !TheOnlySucc || TheOnlySucc == To;
>>>> +    };
>>>> +
>>>> +    // The loop will not be destroyed if its latch is live.
>>>> +    DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(),
>>>> +L.getHeader());
>>>> +
>>>> +    // If we are going to delete the current loop completely, no
>>>> extra analysis
>>>> +    // is needed.
>>>> +    if (DeleteCurrentLoop)
>>>> +      return;
>>>> +
>>>> +    // Otherwise, we should check which blocks will still be a part
>>>> of the
>>>> +    // current loop after the transform.
>>>> +    BlocksInLoopAfterFolding.insert(L.getLoopLatch());
>>>> +    // If the loop is live, then we should compute what blocks are
>>>> still in
>>>> +    // loop after all branch folding has been done. A block is in
>>>> loop if
>>>> +    // it has a live edge to another block that is in the loop; by
>>>> definition,
>>>> +    // latch is in the loop.
>>>> +    auto BlockIsInLoop = [&](BasicBlock *BB) {
>>>> +      return any_of(successors(BB), [&](BasicBlock *Succ) {
>>>> +        return BlocksInLoopAfterFolding.count(Succ) &&
>>>> IsEdgeLive(BB, Succ);
>>>> +      });
>>>> +    };
>>>> +    for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I
>>>> +!=
>>>> E; ++I) {
>>>> +      BasicBlock *BB = *I;
>>>> +      if (BlockIsInLoop(BB))
>>>> +        BlocksInLoopAfterFolding.insert(BB);
>>>> +    }
>>>> +
>>>> +    // Sanity check: header must be in loop.
>>>> +    assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
>>>> +           "Header not in loop?");
>>>> +  }
>>>> +
>>>> +  /// Constant-fold terminators of blocks acculumated in
>>>> +FoldCandidates into the  /// unconditional branches.
>>>> +  void foldTerminators() {
>>>> +    DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
>>>> +
>>>> +    for (BasicBlock *BB : FoldCandidates) {
>>>> +      assert(LI.getLoopFor(BB) == &L && "Should be a loop block!");
>>>> +      BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
>>>> +      assert(TheOnlySucc && "Should have one live successor!");
>>>> +
>>>> +      LLVM_DEBUG(dbgs() << "Replacing terminator of " <<
>>>> +BB->getName()
>>>> +                        << " with an unconditional branch to the
>>>> block "
>>>> +                        << TheOnlySucc->getName() << "\n");
>>>> +
>>>> +      SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
>>>> +      // Remove all BB's successors except for the live one.
>>>> +      for (auto *Succ : successors(BB))
>>>> +        if (Succ != TheOnlySucc) {
>>>> +          DeadSuccessors.insert(Succ);
>>>> +          Succ->removePredecessor(BB);
>>>> +        }
>>>> +
>>>> +      IRBuilder<> Builder(BB->getContext());
>>>> +      Instruction *Term = BB->getTerminator();
>>>> +      Builder.SetInsertPoint(Term);
>>>> +      Builder.CreateBr(TheOnlySucc);
>>>> +      Term->eraseFromParent();
>>>> +
>>>> +      for (auto *DeadSucc : DeadSuccessors)
>>>> +        DTU.deleteEdge(BB, DeadSucc);
>>>> +
>>>> +      ++NumTerminatorsFolded;
>>>> +    }
>>>> +  }
>>>> +
>>>> +public:
>>>> +  ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI,
>>>> +DominatorTree
>>>> &DT)
>>>> +      : L(L), LI(LI), DT(DT) {}
>>>> +  bool run() {
>>>> +    assert(L.getLoopLatch() && "Should be single latch!");
>>>> +
>>>> +    // Collect all available information about status of blocks
>>>> after constant
>>>> +    // folding.
>>>> +    analyze();
>>>> +
>>>> +    LLVM_DEBUG(dbgs() << "In function " <<
>>>> L.getHeader()->getParent()->getName()
>>>> +                      << ": ");
>>>> +
>>>> +    // Nothing to constant-fold.
>>>> +    if (FoldCandidates.empty()) {
>>>> +      LLVM_DEBUG(
>>>> +          dbgs() << "No constant terminator folding candidates
>>>> +found
>>>> in loop "
>>>> +                 << L.getHeader()->getName() << "\n");
>>>> +      return false;
>>>> +    }
>>>> +
>>>> +    // TODO: Support deletion of the current loop.
>>>> +    if (DeleteCurrentLoop) {
>>>> +      LLVM_DEBUG(
>>>> +          dbgs()
>>>> +          << "Give up constant terminator folding in loop "
>>>> +          << L.getHeader()->getName()
>>>> +          << ": we don't currently support deletion of the current
>>>> loop.\n");
>>>> +      return false;
>>>> +    }
>>>> +
>>>> +    // TODO: Support deletion of dead loop blocks.
>>>> +    if (!DeadLoopBlocks.empty()) {
>>>> +      LLVM_DEBUG(dbgs() << "Give up constant terminator folding in
>>>> loop "
>>>> +                        << L.getHeader()->getName()
>>>> +                        << ": we don't currently"
>>>> +                           " support deletion of dead in-loop
>>>> blocks.\n");
>>>> +      return false;
>>>> +    }
>>>> +
>>>> +    // TODO: Support dead loop exits.
>>>> +    if (!DeadExitBlocks.empty()) {
>>>> +      LLVM_DEBUG(dbgs() << "Give up constant terminator folding in
>>>> loop "
>>>> +                        << L.getHeader()->getName()
>>>> +                        << ": we don't currently support dead loop
>>>> exits.\n");
>>>> +      return false;
>>>> +    }
>>>> +
>>>> +    // TODO: Support blocks that are not dead, but also not in loop
>>>> after the
>>>> +    // folding.
>>>> +    if (BlocksInLoopAfterFolding.size() != L.getNumBlocks()) {
>>>> +      LLVM_DEBUG(
>>>> +          dbgs() << "Give up constant terminator folding in loop "
>>>> +                 << L.getHeader()->getName()
>>>> +                 << ": we don't currently"
>>>> +                    " support blocks that are not dead, but will stop "
>>>> +                    "being a part of the loop after
>>>> constant-folding.\n");
>>>> +      return false;
>>>> +    }
>>>> +
>>>> +    // Dump analysis results.
>>>> +    LLVM_DEBUG(dump());
>>>> +
>>>> +    LLVM_DEBUG(dbgs() << "Constant-folding " <<
>>>> +FoldCandidates.size()
>>>> +                      << " terminators in loop " <<
>>>> L.getHeader()->getName()
>>>> +                      << "\n");
>>>> +
>>>> +    // Make the actual transforms.
>>>> +    foldTerminators();
>>>> +
>>>> +#ifndef NDEBUG
>>>> +    // Make sure that we have preserved all data structures after
>>>> the transform.
>>>> +    DT.verify();
>>>> +    assert(DT.isReachableFromEntry(L.getHeader()));
>>>> +    LI.verify(DT);
>>>> +#endif
>>>> +
>>>> +    return true;
>>>> +  }
>>>> +};
>>>> +
>>>> +/// Turn branches and switches with known constant conditions into
>>>> +unconditional /// branches.
>>>> +static bool constantFoldTerminators(Loop &L, DominatorTree &DT,
>>>> +LoopInfo &LI) {
>>>> +  // To keep things simple, only process loops with single latch.
>>>> +We
>>>> +  // canonicalize most loops to this form. We can support
>>>> multi-latch if needed.
>>>> +  if (!L.getLoopLatch())
>>>> +    return false;
>>>> +
>>>> +  ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT);
>>>> +  return BranchFolder.run();
>>>> +}
>>>> +
>>>>     static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree
>>>> &DT,
>>>>                                             LoopInfo &LI,
>>>> MemorySSAUpdater *MSSAU) {
>>>>       bool Changed = false;
>>>> @@ -73,6 +385,9 @@ static bool simplifyLoopCFG(Loop &L, Dom
>>>>                                 ScalarEvolution &SE, MemorySSAUpdater
>>>> *MSSAU) {
>>>>       bool Changed = false;
>>>> +  // Constant-fold terminators with known constant conditions.
>>>> +  Changed |= constantFoldTerminators(L, DT, LI);
>>>> +
>>>>       // Eliminate unconditional branches by merging blocks into
>>>> their predecessors.
>>>>       Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU);
>>>>
>>>> Modified:
>>>> llvm/trunk/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll
>>>> URL:
>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopS
>>>> im
>>>> plifyCFG/constant-fold-branch.ll?rev=347289&r1=347288&r2=347289&view
>>>> =d
>>>> iff
>>>> ====================================================================
>>>> ==
>>>> ========
>>>> ---
>>>> llvm/trunk/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll
>>>> (original)
>>>> +++ llvm/trunk/test/Transforms/LoopSimplifyCFG/constant-fold-branch.
>>>> +++ ll
>>>> +++ Mon Nov 19 21:43:32 2018
>>>> @@ -1,10 +1,58 @@
>>>>     ; NOTE: Assertions have been autogenerated by
>>>> utils/update_test_checks.py -; RUN: opt -S -loop-simplifycfg < %s |
>>>> FileCheck %s -; RUN: opt -S
>>>> -passes='require<domtree>,loop(simplify-cfg)' < %s | FileCheck %s -;
>>>> RUN: opt -S -loop-simplifycfg -enable-mssa-loop-dependency=true
>>>> -verify-memoryssa < %s | FileCheck %s
>>>> +; REQUIRES: asserts
>>>> +; RUN: opt -S -loop-simplifycfg -debug-only=loop-simplifycfg 2>&1 <
>>>> +%s | FileCheck %s ; RUN: opt -S
>>>> +-passes='require<domtree>,loop(simplify-cfg)'
>>>> +-debug-only=loop-simplifycfg 2>&1 < %s | FileCheck %s ; RUN: opt -S
>>>> +-loop-simplifycfg -enable-mssa-loop-dependency=true
>>>> +-verify-memoryssa -debug-only=loop-simplifycfg 2>&1 < %s |
>>>> +FileCheck %s
>>>>     target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:1"
>>>> +; CHECK-LABEL: In function dead_backedge_test_branch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support blocks that are not dead, but will stop being a part of the
>>>> loop after constant-folding.
>>>> +; CHECK-LABEL: In function dead_backedge_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support blocks that are not dead, but will stop being a part of the
>>>> loop after constant-folding.
>>>> +; CHECK-LABEL: In function dead_block_test_branch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function dead_block_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function dead_block_propogate_test_branch_loop:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function dead_block_propogate_test_switch_loop:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function dead_exit_test_branch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support dead loop exits.
>>>> +; CHECK-LABEL: In function dead_exit_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support dead loop exits.
>>>> +; CHECK-LABEL: In function dead_loop_test_branch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of the current loop.
>>>> +; CHECK-LABEL: In function dead_loop_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of the current loop.
>>>> +; CHECK-LABEL: In function dead_sub_loop_test_branch_loop: No
>>>> +constant terminator folding candidates found in loop dead_loop ;
>>>> +CHECK-LABEL: In function dead_sub_loop_test_branch_loop: No
>>>> +constant
>>>> terminator folding candidates found in loop live_loop ; CHECK-LABEL:
>>>> In function dead_sub_loop_test_branch_loop: Give up constant
>>>> terminator folding in loop header: we don't currently support
>>>> deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function dead_sub_loop_test_switch_loop: No
>>>> +constant terminator folding candidates found in loop live_loop ;
>>>> +CHECK-LABEL: In function dead_sub_loop_test_switch_loop: No
>>>> +constant
>>>> terminator folding candidates found in loop dead_loop ; CHECK-LABEL:
>>>> In function dead_sub_loop_test_switch_loop: Give up constant
>>>> terminator folding in loop header: we don't currently support
>>>> deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function inf_loop_test_branch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function inf_loop_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function live_block_test_branch_loop: Constant
>>>> terminator folding for loop Loop at depth 1 containing:
>>>> %header<header>,%check,%live,%backedge<latch><exiting>
>>>> +; CHECK:         Replacing terminator of check with an
>>>> +unconditional
>>>> branch to the block backedge
>>>> +; CHECK-LABEL: In function live_block_test_branch_loop_phis:
>>>> Constant terminator folding for loop Loop at depth 1 containing:
>>>> %header<header>,%check,%live,%backedge<latch><exiting>
>>>> +; CHECK:         Replacing terminator of check with an
>>>> +unconditional
>>>> branch to the block backedge
>>>> +; CHECK-LABEL: In function live_block_test_switch_loop: Constant
>>>> terminator folding for loop Loop at depth 1 containing:
>>>> %header<header>,%check,%live,%backedge<latch><exiting>
>>>> +; CHECK:         Replacing terminator of check with an
>>>> +unconditional
>>>> branch to the block backedge
>>>> +; CHECK-LABEL: In function live_block_test_switch_loop_phis:
>>>> Constant terminator folding for loop Loop at depth 1 containing:
>>>> %header<header>,%check,%live,%backedge<latch><exiting>
>>>> +; CHECK:         Replacing terminator of check with an
>>>> +unconditional
>>>> branch to the block backedge
>>>> +; CHECK-LABEL: In function partial_sub_loop_test_branch_loop: Give
>>>> up constant terminator folding in loop header: we don't currently
>>>> support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function partial_sub_loop_test_branch_loop: No
>>>> +constant terminator folding candidates found in loop outer_header ;
>>>> CHECK-LABEL: In function partial_sub_loop_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function partial_sub_loop_test_switch_loop: No
>>>> +constant terminator folding candidates found in loop outer_header ;
>>>> CHECK-LABEL: In function full_sub_loop_test_branch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of the current loop.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_branch_loop: No
>>>> +constant terminator folding candidates found in loop outer_header ;
>>>> CHECK-LABEL: In function full_sub_loop_test_switch_loop: Give up
>>>> constant terminator folding in loop header: we don't currently
>>>> support deletion of the current loop.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_switch_loop: No
>>>> +constant terminator folding candidates found in loop outer_header ;
>>>> CHECK-LABEL: In function full_sub_loop_test_branch_loop_inverse_1:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support deletion of the current loop.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_branch_loop_inverse_1:
>>>> +No constant terminator folding candidates found in loop
>>>> +outer_header
>>>> ; CHECK-LABEL: In function full_sub_loop_test_switch_loop_inverse_1:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support deletion of the current loop.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_switch_loop_inverse_1:
>>>> +No constant terminator folding candidates found in loop
>>>> +outer_header
>>>> ; CHECK-LABEL: In function full_sub_loop_test_branch_loop_inverse_2:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support dead loop exits.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_branch_loop_inverse_2:
>>>> +No constant terminator folding candidates found in loop
>>>> +outer_header
>>>> ; CHECK-LABEL: In function full_sub_loop_test_switch_loop_inverse_2:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support dead loop exits.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_switch_loop_inverse_2:
>>>> +No constant terminator folding candidates found in loop
>>>> +outer_header
>>>> ; CHECK-LABEL: In function full_sub_loop_test_branch_loop_inverse_3:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_branch_loop_inverse_3:
>>>> +No constant terminator folding candidates found in loop
>>>> +outer_header
>>>> ; CHECK-LABEL: In function full_sub_loop_test_switch_loop_inverse_3:
>>>> Give up constant terminator folding in loop header: we don't
>>>> currently support deletion of dead in-loop blocks.
>>>> +; CHECK-LABEL: In function full_sub_loop_test_switch_loop_inverse_3:
>>>> +No constant terminator folding candidates found in loop
>>>> +outer_header
>>>> +
>>>>     ; Make sure that we can eliminate a provably dead backedge.
>>>>     define i32 @dead_backedge_test_branch_loop(i32 %end) {
>>>>     ; CHECK-LABEL: @dead_backedge_test_branch_loop( @@ -707,7 +755,7
>>>> @@ define i32 @live_block_test_branch_loop(
>>>>     ; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, [[PREHEADER:%.*]] ], [
>>>> [[I_INC:%.*]], [[BACKEDGE:%.*]] ]
>>>>     ; CHECK-NEXT:    br i1 [[C:%.*]], label [[CHECK:%.*]], label
>>>> [[LIVE:%.*]]
>>>>     ; CHECK:       check:
>>>> -; CHECK-NEXT:    br i1 true, label [[BACKEDGE]], label [[LIVE]]
>>>> +; CHECK-NEXT:    br label [[BACKEDGE]]
>>>>     ; CHECK:       live:
>>>>     ; CHECK-NEXT:    [[I_2:%.*]] = add i32 [[I]], 1
>>>>     ; CHECK-NEXT:    br label [[BACKEDGE]] @@ -744,6 +792,54 @@ exit:
>>>>       ret i32 %i.inc
>>>>     }
>>>> +; Check that when the block is not actually dead, we don't remove it.
>>>> +Version ; with Phi node.
>>>> +define i32 @live_block_test_branch_loop_phis(i1 %c, i32 %end) { ;
>>>> +CHECK-LABEL: @live_block_test_branch_loop_phis(
>>>> +; CHECK-NEXT:  preheader:
>>>> +; CHECK-NEXT:    br label [[HEADER:%.*]] ; CHECK:       header:
>>>> +; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, [[PREHEADER:%.*]] ], [
>>>> [[I_INC:%.*]], [[BACKEDGE:%.*]] ]
>>>> +; CHECK-NEXT:    br i1 [[C:%.*]], label [[CHECK:%.*]], label
>>>> [[LIVE:%.*]]
>>>> +; CHECK:       check:
>>>> +; CHECK-NEXT:    br label [[BACKEDGE]] ; CHECK:       live:
>>>> +; CHECK-NEXT:    [[I_2:%.*]] = add i32 [[I]], 1 ; CHECK-NEXT:    br
>>>> +label [[BACKEDGE]] ; CHECK:       backedge:
>>>> +; CHECK-NEXT:    [[I_1:%.*]] = phi i32 [ [[I]], [[CHECK]] ], [
>>>> [[I_2]], [[LIVE]] ]
>>>> +; CHECK-NEXT:    [[I_INC]] = add i32 [[I_1]], 1 ; CHECK-NEXT:
>>>> +[[CMP:%.*]] = icmp slt i32 [[I_INC]], [[END:%.*]] ; CHECK-NEXT:
>>>> +br i1 [[CMP]], label [[HEADER]], label [[EXIT:%.*]] ; CHECK:
>>>> +exit:
>>>> +; CHECK-NEXT:    [[I_INC_LCSSA:%.*]] = phi i32 [ [[I_INC]],
>>>> [[BACKEDGE]] ]
>>>> +; CHECK-NEXT:    ret i32 [[I_INC_LCSSA]] ;
>>>> +preheader:
>>>> +  br label %header
>>>> +
>>>> +header:
>>>> +  %i = phi i32 [0, %preheader], [%i.inc, %backedge]
>>>> +  br i1 %c, label %check, label %live
>>>> +
>>>> +check:
>>>> +  br i1 true, label %backedge, label %live
>>>> +
>>>> +live:
>>>> +  %phi = phi i32 [ 1, %header ], [ -1, %check ]
>>>> +  %i.2 = add i32 %i, %phi
>>>> +  br label %backedge
>>>> +
>>>> +backedge:
>>>> +  %i.1 = phi i32 [%i, %check], [%i.2, %live]
>>>> +  %i.inc = add i32 %i.1, 1
>>>> +  %cmp = icmp slt i32 %i.inc, %end
>>>> +  br i1 %cmp, label %header, label %exit
>>>> +
>>>> +exit:
>>>> +  ret i32 %i.inc
>>>> +}
>>>> +
>>>>     define i32 @live_block_test_switch_loop(i1 %c, i32 %end) {
>>>>     ; CHECK-LABEL: @live_block_test_switch_loop(
>>>>     ; CHECK-NEXT:  preheader:
>>>> @@ -752,11 +848,7 @@ define i32 @live_block_test_switch_loop(
>>>>     ; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, [[PREHEADER:%.*]] ], [
>>>> [[I_INC:%.*]], [[BACKEDGE:%.*]] ]
>>>>     ; CHECK-NEXT:    br i1 [[C:%.*]], label [[CHECK:%.*]], label
>>>> [[LIVE:%.*]]
>>>>     ; CHECK:       check:
>>>> -; CHECK-NEXT:    switch i32 1, label [[LIVE]] [ -; CHECK-NEXT:
>>>> i32 0, label [[LIVE]] -; CHECK-NEXT:    i32 1, label [[BACKEDGE]] -;
>>>> CHECK-NEXT:    i32 2, label [[LIVE]] -; CHECK-NEXT:    ]
>>>> +; CHECK-NEXT:    br label [[BACKEDGE]]
>>>>     ; CHECK:       live:
>>>>     ; CHECK-NEXT:    [[I_2:%.*]] = add i32 [[I]], 1
>>>>     ; CHECK-NEXT:    br label [[BACKEDGE]] @@ -786,6 +878,54 @@ live:
>>>>       br label %backedge
>>>>     backedge:
>>>> +  %i.1 = phi i32 [%i, %check], [%i.2, %live]  %i.inc = add i32
>>>> +%i.1,
>>>> + 1  %cmp = icmp slt i32 %i.inc, %end  br i1 %cmp, label %header,
>>>> +label %exit
>>>> +
>>>> +exit:
>>>> +  ret i32 %i.inc
>>>> +}
>>>> +
>>>> +define i32 @live_block_test_switch_loop_phis(i1 %c, i32 %end) { ;
>>>> +CHECK-LABEL: @live_block_test_switch_loop_phis(
>>>> +; CHECK-NEXT:  preheader:
>>>> +; CHECK-NEXT:    br label [[HEADER:%.*]] ; CHECK:       header:
>>>> +; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, [[PREHEADER:%.*]] ], [
>>>> [[I_INC:%.*]], [[BACKEDGE:%.*]] ]
>>>> +; CHECK-NEXT:    br i1 [[C:%.*]], label [[CHECK:%.*]], label
>>>> [[LIVE:%.*]]
>>>> +; CHECK:       check:
>>>> +; CHECK-NEXT:    br label [[BACKEDGE]] ; CHECK:       live:
>>>> +; CHECK-NEXT:    [[I_2:%.*]] = add i32 [[I]], 1 ; CHECK-NEXT:    br
>>>> +label [[BACKEDGE]] ; CHECK:       backedge:
>>>> +; CHECK-NEXT:    [[I_1:%.*]] = phi i32 [ [[I]], [[CHECK]] ], [
>>>> [[I_2]], [[LIVE]] ]
>>>> +; CHECK-NEXT:    [[I_INC]] = add i32 [[I_1]], 1 ; CHECK-NEXT:
>>>> +[[CMP:%.*]] = icmp slt i32 [[I_INC]], [[END:%.*]] ; CHECK-NEXT:
>>>> +br i1 [[CMP]], label [[HEADER]], label [[EXIT:%.*]] ; CHECK:
>>>> +exit:
>>>> +; CHECK-NEXT:    [[I_INC_LCSSA:%.*]] = phi i32 [ [[I_INC]],
>>>> [[BACKEDGE]] ]
>>>> +; CHECK-NEXT:    ret i32 [[I_INC_LCSSA]] ;
>>>> +preheader:
>>>> +  br label %header
>>>> +
>>>> +header:
>>>> +  %i = phi i32 [0, %preheader], [%i.inc, %backedge]
>>>> +  br i1 %c, label %check, label %live
>>>> +
>>>> +check:
>>>> +  switch i32 1, label %live [i32 0, label %live
>>>> +  i32 1, label %backedge
>>>> +  i32 2, label %live]
>>>> +
>>>> +live:
>>>> +  %phi = phi i32 [ 1, %header ], [ -1, %check ], [ -1, %check ], [
>>>> +-1, %check ]
>>>> +  %i.2 = add i32 %i, %phi
>>>> +  br label %backedge
>>>> +
>>>> +backedge:
>>>>       %i.1 = phi i32 [%i, %check], [%i.2, %live]
>>>>       %i.inc = add i32 %i.1, 1
>>>>       %cmp = icmp slt i32 %i.inc, %end
>>>>
>>>>
>>>> _______________________________________________
>>>> llvm-commits mailing list
>>>> llvm-commits at lists.llvm.org
>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>>
>>



More information about the llvm-commits mailing list