From: Roman Lebedev Date: Fri, 15 Jan 2021 17:57:28 +0000 (+0300) Subject: [SimplifyCFG] switchToSelect(): don't forget to insert DomTree edge iff needed X-Git-Tag: llvmorg-13-init~1054 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a14c36fe27f5c36de44049237011d140a7277774;p=platform%2Fupstream%2Fllvm.git [SimplifyCFG] switchToSelect(): don't forget to insert DomTree edge iff needed DestBB might or might not already be a successor of SelectBB, and it wasn't we need to ensure that we record the fact in DomTree. The testcase used to crash in lazy domtree updater mode + non-per-function domtree validity checks disabled. --- diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 4c0427e..559830e 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -5394,20 +5394,25 @@ static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI, Value *SelectValue, IRBuilder<> &Builder, DomTreeUpdater *DTU) { + std::vector Updates; + BasicBlock *SelectBB = SI->getParent(); + BasicBlock *DestBB = PHI->getParent(); + + if (!is_contained(predecessors(DestBB), SelectBB)) + Updates.push_back({DominatorTree::Insert, SelectBB, DestBB}); + Builder.CreateBr(DestBB); + + // Remove the switch. + while (PHI->getBasicBlockIndex(SelectBB) >= 0) PHI->removeIncomingValue(SelectBB); PHI->addIncoming(SelectValue, SelectBB); - Builder.CreateBr(PHI->getParent()); - - std::vector Updates; - - // Remove the switch. for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) { BasicBlock *Succ = SI->getSuccessor(i); - if (Succ == PHI->getParent()) + if (Succ == DestBB) continue; Succ->removePredecessor(SelectBB); Updates.push_back({DominatorTree::Delete, SelectBB, Succ}); diff --git a/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll b/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll index 7f3428b..72e8875 100644 --- a/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll +++ b/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll @@ -1,5 +1,5 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py -; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 -sink-common-insts=1 < %s | FileCheck %s +; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -sink-common-insts=1 < %s | FileCheck %s declare void @widget(i8)