[llvm-branch-commits] [llvm] 022da61 - [SimplifyCFG] Change 'LoopHeaders' to be ArrayRef, not a naked set, thus avoiding dangling pointers

2021-01-23 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-23T16:48:35+03:00
New Revision: 022da61f6b30626708e5b4c1c009afb453d12ebe

URL: 
https://github.com/llvm/llvm-project/commit/022da61f6b30626708e5b4c1c009afb453d12ebe
DIFF: 
https://github.com/llvm/llvm-project/commit/022da61f6b30626708e5b4c1c009afb453d12ebe.diff

LOG: [SimplifyCFG] Change 'LoopHeaders' to be ArrayRef, not a naked 
set, thus avoiding dangling pointers

If i change it to AssertingVH instead, a number of existing tests fail,
which means we don't consistently remove from the set when deleting blocks,
which means newly-created blocks may happen to appear in that set
if they happen to occupy the same memory chunk as did some block
that was in the set originally.

There are many places where we delete blocks,
and while we could probably consistently delete from LoopHeaders
when deleting a block in transforms located in SimplifyCFG.cpp itself,
transforms located elsewhere (Local.cpp/BasicBlockUtils.cpp) also may
delete blocks, and it doesn't seem good to teach them to deal with it.

Since we at most only ever delete from LoopHeaders,
let's just delegate to WeakVH to do that automatically.

But to be honest, personally, i'm not sure that the idea
behind LoopHeaders is sound.

Added: 


Modified: 
llvm/include/llvm/Transforms/Utils/Local.h
llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/include/llvm/Transforms/Utils/Local.h 
b/llvm/include/llvm/Transforms/Utils/Local.h
index ebfb62a48bbf..c712dda483e4 100644
--- a/llvm/include/llvm/Transforms/Utils/Local.h
+++ b/llvm/include/llvm/Transforms/Utils/Local.h
@@ -16,7 +16,6 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/TinyPtrVector.h"
 #include "llvm/Analysis/Utils/Local.h"
@@ -177,7 +176,7 @@ extern cl::opt RequireAndPreserveDomTree;
 bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
  DomTreeUpdater *DTU = nullptr,
  const SimplifyCFGOptions &Options = {},
- SmallPtrSetImpl *LoopHeaders = nullptr);
+ ArrayRef LoopHeaders = {});
 
 /// This function is used to flatten a CFG. For example, it uses parallel-and
 /// and parallel-or mode to collapse if-conditions and merge if-regions with

diff  --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp 
b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
index 0e1ec7194527..38e7109ead57 100644
--- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
+++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
@@ -36,6 +36,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/CommandLine.h"
@@ -200,9 +201,12 @@ static bool iterativelySimplifyCFG(Function &F, const 
TargetTransformInfo &TTI,
 
   SmallVector, 32> Edges;
   FindFunctionBackedges(F, Edges);
-  SmallPtrSet LoopHeaders;
+  SmallPtrSet UniqueLoopHeaders;
   for (unsigned i = 0, e = Edges.size(); i != e; ++i)
-LoopHeaders.insert(const_cast(Edges[i].second));
+UniqueLoopHeaders.insert(const_cast(Edges[i].second));
+
+  SmallVector LoopHeaders(UniqueLoopHeaders.begin(),
+  UniqueLoopHeaders.end());
 
   while (LocalChange) {
 LocalChange = false;
@@ -219,7 +223,7 @@ static bool iterativelySimplifyCFG(Function &F, const 
TargetTransformInfo &TTI,
 while (BBIt != F.end() && DTU->isBBPendingDeletion(&*BBIt))
   ++BBIt;
   }
-  if (simplifyCFG(&BB, TTI, DTU, Options, &LoopHeaders)) {
+  if (simplifyCFG(&BB, TTI, DTU, Options, LoopHeaders)) {
 LocalChange = true;
 ++NumSimpl;
   }

diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index bad8d90cda65..deaa0bbcf772 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -61,6 +61,7 @@
 #include "llvm/IR/Use.h"
 #include "llvm/IR/User.h"
 #include "llvm/IR/Value.h"
+#include "llvm/IR/ValueHandle.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
@@ -218,7 +219,7 @@ class SimplifyCFGOpt {
   const TargetTransformInfo &TTI;
   DomTreeUpdater *DTU;
   const DataLayout &DL;
-  SmallPtrSetImpl *LoopHeaders;
+  ArrayRef LoopHeaders;
   const SimplifyCFGOptions &Options;
   bool Resimplify;
 
@@ -261,8 +262,7 @@ class SimplifyCFGOpt {
 
 public:
   SimplifyCFGOpt(const TargetTransformInfo &TTI, DomTreeUpdater *DTU,
- const DataLayout &DL,
- SmallPtrSetImpl *LoopHeaders,
+ const DataLayout &DL, ArrayRef LoopHeaders,
  const SimplifyCFGOptions &Opts)
   

[llvm-branch-commits] [llvm] 67f9c87 - [NFC][SimplifyCFG] Perform early-continue in FoldValueComparisonIntoPredecessors() per-pred loop

2021-01-23 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-24T00:54:54+03:00
New Revision: 67f9c87a651a9c3a57a2b1bf32e6e0be2479ebc7

URL: 
https://github.com/llvm/llvm-project/commit/67f9c87a651a9c3a57a2b1bf32e6e0be2479ebc7
DIFF: 
https://github.com/llvm/llvm-project/commit/67f9c87a651a9c3a57a2b1bf32e6e0be2479ebc7.diff

LOG: [NFC][SimplifyCFG] Perform early-continue in 
FoldValueComparisonIntoPredecessors() per-pred loop

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 318e6d5cf810..6dab65d3f063 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1273,24 +1273,27 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   SmallSetVector Preds(pred_begin(BB), pred_end(BB));
   while (!Preds.empty()) {
 BasicBlock *Pred = Preds.pop_back_val();
+Instruction *PTI = Pred->getTerminator();
+
+// Don't try to fold into itself.
+if (Pred == BB)
+  continue;
 
 // See if the predecessor is a comparison with the same value.
-Instruction *PTI = Pred->getTerminator();
 Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
+if (PCV != CV)
+  continue;
 
-if (PCV == CV && TI != PTI) {
-  SmallSetVector FailBlocks;
-  if (!SafeToMergeTerminators(TI, PTI, &FailBlocks)) {
-for (auto *Succ : FailBlocks) {
-  if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split",
-  DTU))
-return false;
-}
+SmallSetVector FailBlocks;
+if (!SafeToMergeTerminators(TI, PTI, &FailBlocks)) {
+  for (auto *Succ : FailBlocks) {
+if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split", DTU))
+  return false;
   }
-
-  PerformValueComparisonIntoPredecessorFolding(TI, CV, PTI, Builder);
-  Changed = true;
 }
+
+PerformValueComparisonIntoPredecessorFolding(TI, CV, PTI, Builder);
+Changed = true;
   }
   return Changed;
 }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] a4e6c2e - [NFC][SimplifyCFG] Extract PerformValueComparisonIntoPredecessorFolding() out of FoldValueComparisonIntoPredecessors()

2021-01-23 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-24T00:54:54+03:00
New Revision: a4e6c2e647b09dd8c2c5cf55bb05e3c7fd89646c

URL: 
https://github.com/llvm/llvm-project/commit/a4e6c2e647b09dd8c2c5cf55bb05e3c7fd89646c
DIFF: 
https://github.com/llvm/llvm-project/commit/a4e6c2e647b09dd8c2c5cf55bb05e3c7fd89646c.diff

LOG: [NFC][SimplifyCFG] Extract PerformValueComparisonIntoPredecessorFolding() 
out of FoldValueComparisonIntoPredecessors()

Less nested code is much easier to follow and modify.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index deaa0bbcf772..318e6d5cf810 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -229,6 +229,9 @@ class SimplifyCFGOpt {
   bool SimplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
  BasicBlock *Pred,
  IRBuilder<> &Builder);
+  bool PerformValueComparisonIntoPredecessorFolding(Instruction *TI, Value 
*&CV,
+Instruction *PTI,
+IRBuilder<> &Builder);
   bool FoldValueComparisonIntoPredecessors(Instruction *TI,
IRBuilder<> &Builder);
 
@@ -1046,6 +1049,215 @@ static void FitWeights(MutableArrayRef 
Weights) {
   }
 }
 
+bool SimplifyCFGOpt::PerformValueComparisonIntoPredecessorFolding(
+Instruction *TI, Value *&CV, Instruction *PTI, IRBuilder<> &Builder) {
+  BasicBlock *BB = TI->getParent();
+  BasicBlock *Pred = PTI->getParent();
+
+  std::vector Updates;
+
+  // Figure out which 'cases' to copy from SI to PSI.
+  std::vector BBCases;
+  BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
+
+  std::vector PredCases;
+  BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
+
+  // Based on whether the default edge from PTI goes to BB or not, fill in
+  // PredCases and PredDefault with the new switch cases we would like to
+  // build.
+  SmallMapVector NewSuccessors;
+
+  // Update the branch weight metadata along the way
+  SmallVector Weights;
+  bool PredHasWeights = HasBranchWeights(PTI);
+  bool SuccHasWeights = HasBranchWeights(TI);
+
+  if (PredHasWeights) {
+GetBranchWeights(PTI, Weights);
+// branch-weight metadata is inconsistent here.
+if (Weights.size() != 1 + PredCases.size())
+  PredHasWeights = SuccHasWeights = false;
+  } else if (SuccHasWeights)
+// If there are no predecessor weights but there are successor weights,
+// populate Weights with 1, which will later be scaled to the sum of
+// successor's weights
+Weights.assign(1 + PredCases.size(), 1);
+
+  SmallVector SuccWeights;
+  if (SuccHasWeights) {
+GetBranchWeights(TI, SuccWeights);
+// branch-weight metadata is inconsistent here.
+if (SuccWeights.size() != 1 + BBCases.size())
+  PredHasWeights = SuccHasWeights = false;
+  } else if (PredHasWeights)
+SuccWeights.assign(1 + BBCases.size(), 1);
+
+  if (PredDefault == BB) {
+// If this is the default destination from PTI, only the edges in TI
+// that don't occur in PTI, or that branch to BB will be activated.
+std::set PTIHandled;
+for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
+  if (PredCases[i].Dest != BB)
+PTIHandled.insert(PredCases[i].Value);
+  else {
+// The default destination is BB, we don't need explicit targets.
+std::swap(PredCases[i], PredCases.back());
+
+if (PredHasWeights || SuccHasWeights) {
+  // Increase weight for the default case.
+  Weights[0] += Weights[i + 1];
+  std::swap(Weights[i + 1], Weights.back());
+  Weights.pop_back();
+}
+
+PredCases.pop_back();
+--i;
+--e;
+  }
+
+// Reconstruct the new switch statement we will be building.
+if (PredDefault != BBDefault) {
+  PredDefault->removePredecessor(Pred);
+  if (PredDefault != BB)
+Updates.push_back({DominatorTree::Delete, Pred, PredDefault});
+  PredDefault = BBDefault;
+  ++NewSuccessors[BBDefault];
+}
+
+unsigned CasesFromPred = Weights.size();
+uint64_t ValidTotalSuccWeight = 0;
+for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
+  if (!PTIHandled.count(BBCases[i].Value) && BBCases[i].Dest != BBDefault) 
{
+PredCases.push_back(BBCases[i]);
+++NewSuccessors[BBCases[i].Dest];
+if (SuccHasWeights || PredHasWeights) {
+  // The default weight is at index 0, so weight for the ith case
+  // should be at index i+1. Scale the cases from successor by
+  // PredDefaultWeight (Weights[0]).
+  Weights.push_back(Weights[0] * Succ

[llvm-branch-commits] [llvm] 6f27532 - [NFC][SimplifyCFG] Extract CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses() out of PerformBranchToCommonDestFolding()

2021-01-23 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-24T00:54:55+03:00
New Revision: 6f2753273ee6d891cabd11626e4efbce0d901661

URL: 
https://github.com/llvm/llvm-project/commit/6f2753273ee6d891cabd11626e4efbce0d901661
DIFF: 
https://github.com/llvm/llvm-project/commit/6f2753273ee6d891cabd11626e4efbce0d901661.diff

LOG: [NFC][SimplifyCFG] Extract 
CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses() out of 
PerformBranchToCommonDestFolding()

To be used in PerformValueComparisonIntoPredecessorFolding()

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6dab65d3f063..7cfe17618cde 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1049,6 +1049,55 @@ static void FitWeights(MutableArrayRef 
Weights) {
   }
 }
 
+static void CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
+BasicBlock *BB, BasicBlock *PredBlock, ValueToValueMapTy &VMap) {
+  Instruction *PTI = PredBlock->getTerminator();
+
+  // If we have bonus instructions, clone them into the predecessor block.
+  // Note that there may be multiple predecessor blocks, so we cannot move
+  // bonus instructions to a predecessor block.
+  for (Instruction &BonusInst : *BB) {
+if (isa(BonusInst) || BonusInst.isTerminator())
+  continue;
+
+Instruction *NewBonusInst = BonusInst.clone();
+
+if (PTI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
+  // Unless the instruction has the same !dbg location as the original
+  // branch, drop it. When we fold the bonus instructions we want to make
+  // sure we reset their debug locations in order to avoid stepping on
+  // dead code caused by folding dead branches.
+  NewBonusInst->setDebugLoc(DebugLoc());
+}
+
+RemapInstruction(NewBonusInst, VMap,
+ RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
+VMap[&BonusInst] = NewBonusInst;
+
+// If we moved a load, we cannot any longer claim any knowledge about
+// its potential value. The previous information might have been valid
+// only given the branch precondition.
+// For an analogous reason, we must also drop all the metadata whose
+// semantics we don't understand. We *can* preserve !annotation, because
+// it is tied to the instruction itself, not the value or position.
+NewBonusInst->dropUnknownNonDebugMetadata(LLVMContext::MD_annotation);
+
+PredBlock->getInstList().insert(PTI->getIterator(), NewBonusInst);
+NewBonusInst->takeName(&BonusInst);
+BonusInst.setName(NewBonusInst->getName() + ".old");
+
+// Update (liveout) uses of bonus instructions,
+// now that the bonus instruction has been cloned into predecessor.
+SSAUpdater SSAUpdate;
+SSAUpdate.Initialize(BonusInst.getType(),
+ (NewBonusInst->getName() + ".merge").str());
+SSAUpdate.AddAvailableValue(BB, &BonusInst);
+SSAUpdate.AddAvailableValue(PredBlock, NewBonusInst);
+for (Use &U : make_early_inc_range(BonusInst.uses()))
+  SSAUpdate.RewriteUseAfterInsertions(U);
+  }
+}
+
 bool SimplifyCFGOpt::PerformValueComparisonIntoPredecessorFolding(
 Instruction *TI, Value *&CV, Instruction *PTI, IRBuilder<> &Builder) {
   BasicBlock *BB = TI->getParent();
@@ -2890,50 +2939,8 @@ static bool PerformBranchToCommonDestFolding(BranchInst 
*BI, BranchInst *PBI,
   if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
 PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
 
-  // If we have bonus instructions, clone them into the predecessor block.
-  // Note that there may be multiple predecessor blocks, so we cannot move
-  // bonus instructions to a predecessor block.
   ValueToValueMapTy VMap; // maps original values to cloned values
-  for (Instruction &BonusInst : *BB) {
-if (isa(BonusInst) || isa(BonusInst))
-  continue;
-
-Instruction *NewBonusInst = BonusInst.clone();
-
-if (PBI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
-  // Unless the instruction has the same !dbg location as the original
-  // branch, drop it. When we fold the bonus instructions we want to make
-  // sure we reset their debug locations in order to avoid stepping on
-  // dead code caused by folding dead branches.
-  NewBonusInst->setDebugLoc(DebugLoc());
-}
-
-RemapInstruction(NewBonusInst, VMap,
- RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
-VMap[&BonusInst] = NewBonusInst;
-
-// If we moved a load, we cannot any longer claim any knowledge about
-// its potential value. The previous information might have been valid
-// only given the branch precondition.
-// For an analogous reason, we must also drop all the metadata whose
-// semantics we don't understand. We *can* preserve !annotation, because
-  

[llvm-branch-commits] [llvm] b06c55a - [X86][CostModel] Fix cost model for non-power-of-two vector load/stores

2021-04-16 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-04-16T15:30:57+03:00
New Revision: b06c55a6986e0e1d571663eec507664013b22f00

URL: 
https://github.com/llvm/llvm-project/commit/b06c55a6986e0e1d571663eec507664013b22f00
DIFF: 
https://github.com/llvm/llvm-project/commit/b06c55a6986e0e1d571663eec507664013b22f00.diff

LOG: [X86][CostModel] Fix cost model for non-power-of-two vector load/stores

Sometimes LV has to produce really wide vectors,
and sometimes they end up being not powers of two.
As it can be seen from the diff, the cost computation
is currently completely non-sensical in those cases.

Instead of just scalarizing everything, split/factorize the wide vector
into a number of subvectors, each one having a power-of-two elements,
recurse to get the cost of op on this subvector. Also, check how we'd
legalize this subvector, and if the legalized type is scalar,
also account for the scalarization cost.

Note that for sub-vector loads, we might be able to do better,
when the vectors are properly aligned.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D100099

Added: 


Modified: 
llvm/lib/Target/X86/X86TargetTransformInfo.cpp
llvm/test/Analysis/CostModel/X86/load_store.ll

Removed: 




diff  --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp 
b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index fa3f97ebcaf1e..9a17077bda896 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -3209,42 +3209,48 @@ InstructionCost X86TTIImpl::getMemoryOpCost(unsigned 
Opcode, Type *Src,
 return TTI::TCC_Basic;
   }
 
-  // Handle non-power-of-two vectors such as <3 x float>
-  if (auto *VTy = dyn_cast(Src)) {
-unsigned NumElem = VTy->getNumElements();
+  assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
+ "Invalid Opcode");
+  // Type legalization can't handle structs
+  if (TLI->getValueType(DL, Src, true) == MVT::Other)
+return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
+  CostKind);
 
-// Handle a few common cases:
-// <3 x float>
-if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
-  // Cost = 64 bit store + extract + 32 bit store.
-  return 3;
+  // Handle non-power-of-two vectors such as <3 x float> and <48 x i16>
+  if (auto *VTy = dyn_cast(Src)) {
+const unsigned NumElem = VTy->getNumElements();
+if (!isPowerOf2_32(NumElem)) {
+  // Factorize NumElem into sum of power-of-two.
+  InstructionCost Cost = 0;
+  unsigned NumElemDone = 0;
+  for (unsigned NumElemLeft = NumElem, Factor;
+   Factor = PowerOf2Floor(NumElemLeft), NumElemLeft > 0;
+   NumElemLeft -= Factor) {
+Type *SubTy = FixedVectorType::get(VTy->getScalarType(), Factor);
+unsigned SubTyBytes = SubTy->getPrimitiveSizeInBits() / 8;
 
-// <3 x double>
-if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
-  // Cost = 128 bit store + unpack + 64 bit store.
-  return 3;
+Cost +=
+getMemoryOpCost(Opcode, SubTy, Alignment, AddressSpace, CostKind);
+
+std::pair LST = TLI->getTypeLegalizationCost(DL, SubTy);
+if (!LST.second.isVector()) {
+  APInt DemandedElts =
+  APInt::getBitsSet(NumElem, NumElemDone, NumElemDone + Factor);
+  Cost += getScalarizationOverhead(VTy, DemandedElts,
+   Opcode == Instruction::Load,
+   Opcode == Instruction::Store);
+}
 
-// Assume that all other non-power-of-two numbers are scalarized.
-if (!isPowerOf2_32(NumElem)) {
-  APInt DemandedElts = APInt::getAllOnesValue(NumElem);
-  InstructionCost Cost = BaseT::getMemoryOpCost(
-  Opcode, VTy->getScalarType(), Alignment, AddressSpace, CostKind);
-  int SplitCost = getScalarizationOverhead(VTy, DemandedElts,
-   Opcode == Instruction::Load,
-   Opcode == Instruction::Store);
-  return NumElem * Cost + SplitCost;
+NumElemDone += Factor;
+Alignment = commonAlignment(Alignment.valueOrOne(), SubTyBytes);
+  }
+  assert(NumElemDone == NumElem && "Processed wrong element count?");
+  return Cost;
 }
   }
 
-  // Type legalization can't handle structs
-  if (TLI->getValueType(DL, Src,  true) == MVT::Other)
-return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
-  CostKind);
-
   // Legalize the type.
   std::pair LT = TLI->getTypeLegalizationCost(DL, Src);
-  assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
- "Invalid Opcode");
 
   // Each load/store unit costs 1.
   int Cost = LT.first * 1;

diff  --git a/llvm/test/Analysis/CostModel/X86/load_store.ll 
b/llvm/test

[llvm-branch-commits] [llvm] 61ec228 - [NFC][SimplifyCFG] Add testcase showing that we fail to preserve DomTree in switchToSelect()

2021-01-15 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-15T23:35:55+03:00
New Revision: 61ec2280308bd5e2161efe2959d7d26798c85cb4

URL: 
https://github.com/llvm/llvm-project/commit/61ec2280308bd5e2161efe2959d7d26798c85cb4
DIFF: 
https://github.com/llvm/llvm-project/commit/61ec2280308bd5e2161efe2959d7d26798c85cb4.diff

LOG: [NFC][SimplifyCFG] Add testcase showing that we fail to preserve DomTree 
in switchToSelect()

Added: 

llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll
new file mode 100644
index ..7f3428ba8c7b
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll
@@ -0,0 +1,57 @@
+; 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
+
+declare void @widget(i8)
+
+define void @baz(i8 %arg, i8 %arg10, i1 %arg11) {
+; CHECK-LABEL: @baz(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:br label [[BB12:%.*]]
+; CHECK:   bb12:
+; CHECK-NEXT:[[TMP:%.*]] = icmp eq i8 [[ARG:%.*]], 0
+; CHECK-NEXT:br i1 [[TMP]], label [[BB17:%.*]], label [[BB13:%.*]]
+; CHECK:   bb13:
+; CHECK-NEXT:tail call void @widget(i8 11)
+; CHECK-NEXT:[[SWITCH_SELECTCMP:%.*]] = icmp eq i8 [[ARG10:%.*]], 73
+; CHECK-NEXT:[[SWITCH_SELECT:%.*]] = select i1 [[SWITCH_SELECTCMP]], i8 
44, i8 22
+; CHECK-NEXT:[[SWITCH_SELECTCMP1:%.*]] = icmp eq i8 [[ARG10]], 68
+; CHECK-NEXT:[[SWITCH_SELECT2:%.*]] = select i1 [[SWITCH_SELECTCMP1]], i8 
33, i8 [[SWITCH_SELECT]]
+; CHECK-NEXT:tail call void @widget(i8 [[SWITCH_SELECT2]])
+; CHECK-NEXT:br label [[BB17]]
+; CHECK:   bb17:
+; CHECK-NEXT:br i1 [[ARG11:%.*]], label [[BB12]], label [[BB18:%.*]]
+; CHECK:   bb18:
+; CHECK-NEXT:ret void
+;
+bb:
+  br label %bb12
+
+bb12: ; preds = %bb17, %bb
+  %tmp = icmp eq i8 %arg, 0
+  br i1 %tmp, label %bb17, label %bb13
+
+bb13: ; preds = %bb12
+  tail call void @widget(i8 11)
+  switch i8 %arg10, label %bb14 [
+  i8 68, label %bb15
+  i8 73, label %bb16
+  ]
+
+bb14: ; preds = %bb13
+  tail call void @widget(i8 22)
+  br label %bb17
+
+bb15: ; preds = %bb13
+  tail call void @widget(i8 33)
+  br label %bb17
+
+bb16: ; preds = %bb13
+  tail call void @widget(i8 44)
+  br label %bb17
+
+bb17: ; preds = %bb16, %bb15, 
%bb14, %bb12
+  br i1 %arg11, label %bb12, label %bb18
+
+bb18: ; preds = %bb17
+  ret void
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] b81f75f - [Utils] splitBlockBefore() always operates on DomTreeUpdater, so take it, not DomTree

2021-01-15 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-15T23:35:56+03:00
New Revision: b81f75fa79162e9e2ba84d6b4cdd72f564b050c6

URL: 
https://github.com/llvm/llvm-project/commit/b81f75fa79162e9e2ba84d6b4cdd72f564b050c6
DIFF: 
https://github.com/llvm/llvm-project/commit/b81f75fa79162e9e2ba84d6b4cdd72f564b050c6.diff

LOG: [Utils] splitBlockBefore() always operates on DomTreeUpdater, so take it, 
not DomTree

Even though not all it's users operate on DomTreeUpdater,
it itself internally operates on DomTreeUpdater,
so it must mean everything is fine with that,
so just do that globally.

Added: 


Modified: 
llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Removed: 




diff  --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h 
b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
index a1d5ee8fb91b..a8380981ccfa 100644
--- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
@@ -271,7 +271,7 @@ BasicBlock *SplitBlock(BasicBlock *Old, Instruction 
*SplitPt,
 /// old block are joined by inserting an unconditional branch to the end of the
 /// new block. The new block with name \p BBName is returned.
 BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
- DominatorTree *DT, LoopInfo *LI,
+ DomTreeUpdater *DTU, LoopInfo *LI,
  MemorySSAUpdater *MSSAU, const Twine &BBName = 
"");
 
 /// This method introduces at least one new basic block into the function and

diff  --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp 
b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index bfad88f64c7d..f89a1fad5f1d 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -546,8 +546,10 @@ BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction 
*SplitPt,
  DominatorTree *DT, LoopInfo *LI,
  MemorySSAUpdater *MSSAU, const Twine &BBName,
  bool Before) {
-  if (Before)
-return splitBlockBefore(Old, SplitPt, DT, LI, MSSAU, BBName);
+  if (Before) {
+DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
+return splitBlockBefore(Old, SplitPt, &DTU, LI, MSSAU, BBName);
+  }
   BasicBlock::iterator SplitIt = SplitPt->getIterator();
   while (isa(SplitIt) || SplitIt->isEHPad())
 ++SplitIt;
@@ -580,7 +582,7 @@ BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction 
*SplitPt,
 }
 
 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
-   DominatorTree *DT, LoopInfo *LI,
+   DomTreeUpdater *DTU, LoopInfo *LI,
MemorySSAUpdater *MSSAU,
const Twine &BBName) {
 
@@ -598,25 +600,25 @@ BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, 
Instruction *SplitPt,
 if (Loop *L = LI->getLoopFor(Old))
   L->addBasicBlockToLoop(New, *LI);
 
-  if (DT) {
-DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
+  if (DTU) {
 SmallVector DTUpdates;
 // New dominates Old. The predecessor nodes of the Old node dominate
 // New node.
+SmallSetVector UniquePredecessorsOfOld(pred_begin(New),
+pred_end(New));
 DTUpdates.push_back({DominatorTree::Insert, New, Old});
-for (BasicBlock *Pred : predecessors(New))
-  if (DT->getNode(Pred)) {
-DTUpdates.push_back({DominatorTree::Insert, Pred, New});
-DTUpdates.push_back({DominatorTree::Delete, Pred, Old});
-  }
+DTUpdates.reserve(DTUpdates.size() + 2 * UniquePredecessorsOfOld.size());
+for (BasicBlock *UniquePredecessorOfOld : UniquePredecessorsOfOld) {
+  DTUpdates.push_back({DominatorTree::Insert, UniquePredecessorOfOld, 
New});
+  DTUpdates.push_back({DominatorTree::Delete, UniquePredecessorOfOld, 
Old});
+}
 
-DTU.applyUpdates(DTUpdates);
-DTU.flush();
+DTU->applyUpdates(DTUpdates);
 
 // Move MemoryAccesses still tracked in Old, but part of New now.
 // Update accesses in successor blocks accordingly.
 if (MSSAU) {
-  MSSAU->applyUpdates(DTUpdates, *DT);
+  MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());
   if (VerifyMemorySSA)
 MSSAU->getMemorySSA()->verifyMemorySSA();
 }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] c845c72 - [Utils][SimplifyCFG] Port SplitBlock() to DomTreeUpdater

2021-01-15 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-15T23:35:56+03:00
New Revision: c845c724c2323660e81a0b284aaa461842f1b402

URL: 
https://github.com/llvm/llvm-project/commit/c845c724c2323660e81a0b284aaa461842f1b402
DIFF: 
https://github.com/llvm/llvm-project/commit/c845c724c2323660e81a0b284aaa461842f1b402.diff

LOG: [Utils][SimplifyCFG] Port SplitBlock() to DomTreeUpdater

This is not nice, but it's the best transient solution possible,
and is better than just duplicating the whole function.

The problem is, this function is widely used,
and it is not at all obvious that all the users
could be painlessly switched to operate on DomTreeUpdater,
and somehow i don't feel like porting all those users first.

This function is one of last three that not operate on DomTreeUpdater.

Added: 


Modified: 
llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h 
b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
index a8380981ccfa..a75e49d75c53 100644
--- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
@@ -252,6 +252,21 @@ BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
   MemorySSAUpdater *MSSAU = nullptr,
   const Twine &BBName = "");
 
+/// Split the specified block at the specified instruction.
+///
+/// If \p Before is true, splitBlockBefore handles the block
+/// splitting. Otherwise, execution proceeds as described below.
+///
+/// Everything before \p SplitPt stays in \p Old and everything starting with 
\p
+/// SplitPt moves to a new block. The two blocks are joined by an unconditional
+/// branch. The new block with name \p BBName is returned.
+///
+/// FIXME: deprecated, switch to the DomTreeUpdater-based one.
+BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree 
*DT,
+   LoopInfo *LI = nullptr,
+   MemorySSAUpdater *MSSAU = nullptr,
+   const Twine &BBName = "", bool Before = false);
+
 /// Split the specified block at the specified instruction.
 ///
 /// If \p Before is true, splitBlockBefore handles the block
@@ -261,7 +276,7 @@ BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
 /// SplitPt moves to a new block. The two blocks are joined by an unconditional
 /// branch. The new block with name \p BBName is returned.
 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
-   DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
+   DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
MemorySSAUpdater *MSSAU = nullptr,
const Twine &BBName = "", bool Before = false);
 

diff  --git a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp 
b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
index a2276193b460..aa9894ca32b3 100644
--- a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
@@ -1106,12 +1106,15 @@ class LowerMatrixIntrinsics {
 for (BasicBlock *Succ : successors(Check0))
   DTUpdates.push_back({DT->Delete, Check0, Succ});
 
-BasicBlock *Check1 = SplitBlock(MatMul->getParent(), MatMul, nullptr, LI,
-nullptr, "alias_cont");
+BasicBlock *Check1 =
+SplitBlock(MatMul->getParent(), MatMul, (DomTreeUpdater *)nullptr, LI,
+   nullptr, "alias_cont");
 BasicBlock *Copy =
-SplitBlock(MatMul->getParent(), MatMul, nullptr, LI, nullptr, "copy");
-BasicBlock *Fusion = SplitBlock(MatMul->getParent(), MatMul, nullptr, LI,
-nullptr, "no_alias");
+SplitBlock(MatMul->getParent(), MatMul, (DomTreeUpdater *)nullptr, LI,
+   nullptr, "copy");
+BasicBlock *Fusion =
+SplitBlock(MatMul->getParent(), MatMul, (DomTreeUpdater *)nullptr, LI,
+   nullptr, "no_alias");
 
 // Check if the loaded memory location begins before the end of the store
 // location. If the condition holds, they might overlap, otherwise they are

diff  --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp 
b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index f89a1fad5f1d..91ce48509132 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -542,13 +542,15 @@ llvm::SplitAllCriticalEdges(Function &F,
   return NumBroken;
 }
 
-BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
- DominatorTree *DT, LoopInfo *LI,
- MemorySSAUpdater *MSSAU, const Twine &BBName,
- 

[llvm-branch-commits] [llvm] 286cf6c - [SimplifyCFG] Port SplitBlockAndInsertIfThen() to DomTreeUpdater

2021-01-15 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-15T23:35:56+03:00
New Revision: 286cf6cb029a9942df6ff1d99570e93c25fe29f0

URL: 
https://github.com/llvm/llvm-project/commit/286cf6cb029a9942df6ff1d99570e93c25fe29f0
DIFF: 
https://github.com/llvm/llvm-project/commit/286cf6cb029a9942df6ff1d99570e93c25fe29f0.diff

LOG: [SimplifyCFG] Port SplitBlockAndInsertIfThen() to DomTreeUpdater

This is not nice, but it's the best transient solution possible,
and is better than just duplicating the whole function.

The problem is, this function is widely used,
and it is not at all obvious that all the users
could be painlessly switched to operate on DomTreeUpdater,
and somehow i don't feel like porting all those users first.

This function is one of last three that not operate on DomTreeUpdater.

Added: 


Modified: 
llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h 
b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
index a75e49d75c53..b75fe1f0f9f7 100644
--- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
@@ -335,6 +335,35 @@ ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, 
BasicBlock *BB,
BasicBlock *Pred,
DomTreeUpdater *DTU = nullptr);
 
+/// Split the containing block at the specified instruction - everything before
+/// SplitBefore stays in the old basic block, and the rest of the instructions
+/// in the BB are moved to a new block. The two blocks are connected by a
+/// conditional branch (with value of Cmp being the condition).
+/// Before:
+///   Head
+///   SplitBefore
+///   Tail
+/// After:
+///   Head
+///   if (Cond)
+/// ThenBlock
+///   SplitBefore
+///   Tail
+///
+/// If \p ThenBlock is not specified, a new block will be created for it.
+/// If \p Unreachable is true, the newly created block will end with
+/// UnreachableInst, otherwise it branches to Tail.
+/// Returns the NewBasicBlock's terminator.
+///
+/// Updates DT and LI if given.
+///
+/// FIXME: deprecated, switch to the DomTreeUpdater-based one.
+Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
+   bool Unreachable, MDNode *BranchWeights,
+   DominatorTree *DT,
+   LoopInfo *LI = nullptr,
+   BasicBlock *ThenBlock = nullptr);
+
 /// Split the containing block at the specified instruction - everything before
 /// SplitBefore stays in the old basic block, and the rest of the instructions
 /// in the BB are moved to a new block. The two blocks are connected by a
@@ -359,7 +388,7 @@ ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, 
BasicBlock *BB,
 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
bool Unreachable,
MDNode *BranchWeights = nullptr,
-   DominatorTree *DT = nullptr,
+   DomTreeUpdater *DTU = nullptr,
LoopInfo *LI = nullptr,
BasicBlock *ThenBlock = nullptr);
 

diff  --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp 
b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 79a2aa354339..fedd9bfc977e 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -773,7 +773,8 @@ void HWAddressSanitizer::instrumentMemAccessInline(Value 
*Ptr, bool IsWrite,
   Value *PtrLowBitsOOB = IRB.CreateICmpUGE(PtrLowBits, MemTag);
   SplitBlockAndInsertIfThen(PtrLowBitsOOB, CheckTerm, false,
 MDBuilder(*C).createBranchWeights(1, 10),
-nullptr, nullptr, CheckFailTerm->getParent());
+(DomTreeUpdater *)nullptr, nullptr,
+CheckFailTerm->getParent());
 
   IRB.SetInsertPoint(CheckTerm);
   Value *InlineTagAddr = IRB.CreateOr(AddrLong, 15);
@@ -782,7 +783,8 @@ void HWAddressSanitizer::instrumentMemAccessInline(Value 
*Ptr, bool IsWrite,
   Value *InlineTagMismatch = IRB.CreateICmpNE(PtrTag, InlineTag);
   SplitBlockAndInsertIfThen(InlineTagMismatch, CheckTerm, false,
 MDBuilder(*C).createBranchWeights(1, 10),
-nullptr, nullptr, CheckFailTerm->getParent());
+(DomTreeUpdater *)nullptr, nullptr,
+CheckFailTerm->getPare

[llvm-branch-commits] [llvm] c6654a4 - [SimplifyCFG][BasicBlockUtils] Port SplitBlockPredecessors()/SplitLandingPadPredecessors() to DomTreeUpdater

2021-01-15 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-15T23:35:56+03:00
New Revision: c6654a4cdab4156bae51970fa64993e790fc4adb

URL: 
https://github.com/llvm/llvm-project/commit/c6654a4cdab4156bae51970fa64993e790fc4adb
DIFF: 
https://github.com/llvm/llvm-project/commit/c6654a4cdab4156bae51970fa64993e790fc4adb.diff

LOG: [SimplifyCFG][BasicBlockUtils] Port 
SplitBlockPredecessors()/SplitLandingPadPredecessors() to DomTreeUpdater

This is not nice, but it's the best transient solution possible,
and is better than just duplicating the whole function.

The problem is, this function is widely used,
and it is not at all obvious that all the users
could be painlessly switched to operate on DomTreeUpdater,
and somehow i don't feel like porting all those users first.

This function is one of last three that not operate on DomTreeUpdater.

Added: 


Modified: 
llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h 
b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
index b75fe1f0f9f7..1dda73913826 100644
--- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
@@ -289,6 +289,28 @@ BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction 
*SplitPt,
  DomTreeUpdater *DTU, LoopInfo *LI,
  MemorySSAUpdater *MSSAU, const Twine &BBName = 
"");
 
+/// This method introduces at least one new basic block into the function and
+/// moves some of the predecessors of BB to be predecessors of the new block.
+/// The new predecessors are indicated by the Preds array. The new block is
+/// given a suffix of 'Suffix'. Returns new basic block to which predecessors
+/// from Preds are now pointing.
+///
+/// If BB is a landingpad block then additional basicblock might be introduced.
+/// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
+/// details on this case.
+///
+/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
+/// no other analyses. In particular, it does not preserve LoopSimplify
+/// (because it's complicated to handle the case where one of the edges being
+/// split is an exit of a loop with other exits).
+///
+/// FIXME: deprecated, switch to the DomTreeUpdater-based one.
+BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef 
Preds,
+   const char *Suffix, DominatorTree *DT,
+   LoopInfo *LI = nullptr,
+   MemorySSAUpdater *MSSAU = nullptr,
+   bool PreserveLCSSA = false);
+
 /// This method introduces at least one new basic block into the function and
 /// moves some of the predecessors of BB to be predecessors of the new block.
 /// The new predecessors are indicated by the Preds array. The new block is
@@ -305,11 +327,32 @@ BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction 
*SplitPt,
 /// split is an exit of a loop with other exits).
 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef 
Preds,
const char *Suffix,
-   DominatorTree *DT = nullptr,
+   DomTreeUpdater *DTU = nullptr,
LoopInfo *LI = nullptr,
MemorySSAUpdater *MSSAU = nullptr,
bool PreserveLCSSA = false);
 
+/// This method transforms the landing pad, OrigBB, by introducing two new 
basic
+/// blocks into the function. One of those new basic blocks gets the
+/// predecessors listed in Preds. The other basic block gets the remaining
+/// predecessors of OrigBB. The landingpad instruction OrigBB is clone into 
both
+/// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' 
and
+/// 'Suffix2', and are returned in the NewBBs vector.
+///
+/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
+/// no other analyses. In particular, it does not preserve LoopSimplify
+/// (because it's complicated to handle the case where one of the edges being
+/// split is an exit of a loop with other exits).
+///
+/// FIXME: deprecated, switch to the DomTreeUpdater-based one.
+void SplitLandingPadPredecessors(BasicBlock *OrigBB,
+ ArrayRef Preds,
+ const char *Suffix, const char *Suffix2,
+ SmallVectorImpl &NewBBs,
+ DominatorTree *DT, LoopInfo *LI = nullptr,
+ MemorySSAUpdater *MSSAU = nullptr,
+ bool PreserveLCSSA = false);
+
 /// This method transforms the landing pad, Orig

[llvm-branch-commits] [llvm] a14c36f - [SimplifyCFG] switchToSelect(): don't forget to insert DomTree edge iff needed

2021-01-15 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-15T23:35:57+03:00
New Revision: a14c36fe27f5c36de44049237011d140a724

URL: 
https://github.com/llvm/llvm-project/commit/a14c36fe27f5c36de44049237011d140a724
DIFF: 
https://github.com/llvm/llvm-project/commit/a14c36fe27f5c36de44049237011d140a724.diff

LOG: [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.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

llvm/test/Transforms/SimplifyCFG/switchToSelect-domtree-preservation-edgecase.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 4c0427e103f7..559830ed9a17 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 7f3428ba8c7b..72e887502694 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)
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 32fc323 - [SimplifyCFG] markAliveBlocks(): catchswitch: preserve PostDomTree

2021-01-16 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-17T01:21:05+03:00
New Revision: 32fc32317a31fc00e7e4086d6c93dd1eab75960c

URL: 
https://github.com/llvm/llvm-project/commit/32fc32317a31fc00e7e4086d6c93dd1eab75960c
DIFF: 
https://github.com/llvm/llvm-project/commit/32fc32317a31fc00e7e4086d6c93dd1eab75960c.diff

LOG: [SimplifyCFG] markAliveBlocks(): catchswitch: preserve PostDomTree

When removing catchpad's from catchswitch, if that removes a successor,
we need to record that in DomTreeUpdater.

This fixes PostDomTree preservation failure in an existing test.
This appears to be the single issue that i see in my current test coverage.

Added: 


Modified: 
llvm/lib/Transforms/Utils/Local.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/Local.cpp 
b/llvm/lib/Transforms/Utils/Local.cpp
index 6e526cc4f105..1f94c6191554 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2286,6 +2286,7 @@ static bool markAliveBlocks(Function &F,
 }
   };
 
+  SmallMapVector NumPerSuccessorCases;
   // Set of unique CatchPads.
   SmallDenseMap>
@@ -2295,14 +2296,22 @@ static bool markAliveBlocks(Function &F,
  E = CatchSwitch->handler_end();
I != E; ++I) {
 BasicBlock *HandlerBB = *I;
+++NumPerSuccessorCases[HandlerBB];
 auto *CatchPad = cast(HandlerBB->getFirstNonPHI());
 if (!HandlerSet.insert({CatchPad, Empty}).second) {
+  --NumPerSuccessorCases[HandlerBB];
   CatchSwitch->removeHandler(I);
   --I;
   --E;
   Changed = true;
 }
   }
+  std::vector Updates;
+  for (const std::pair &I : NumPerSuccessorCases)
+if (I.second == 0)
+  Updates.push_back({DominatorTree::Delete, BB, I.first});
+  if (DTU)
+DTU->applyUpdates(Updates);
 }
 
 Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU);



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] aabed37 - [NFCI-ish][SimplifyCFG] FoldBranchToCommonDest(): really don't deal with uncond branches

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:10+03:00
New Revision: aabed3718ae25476c0f6b7e70c83ba4658f00e5c

URL: 
https://github.com/llvm/llvm-project/commit/aabed3718ae25476c0f6b7e70c83ba4658f00e5c
DIFF: 
https://github.com/llvm/llvm-project/commit/aabed3718ae25476c0f6b7e70c83ba4658f00e5c.diff

LOG: [NFCI-ish][SimplifyCFG] FoldBranchToCommonDest(): really don't deal with 
uncond branches

While we already ignore uncond branches, we could still potentially
end up with a conditional branches with identical destinations
due to the visitation order, or because we were called as an utility.
But if we have such a disguised uncond branch,
we still probably shouldn't deal with it here.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d0028c013fa3..5ca8a0b33176 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2775,7 +2775,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
   unsigned BonusInstThreshold) {
   // If this block ends with an unconditional branch,
   // let SpeculativelyExecuteBB() deal with it.
-  if (!BI->isConditional())
+  if (!BI->isConditional() || is_splat(successors(BI)))
 return false;
 
   BasicBlock *BB = BI->getParent();
@@ -2863,7 +2863,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 // Check that we have two conditional branches.  If there is a PHI node in
 // the common successor, verify that the same value flows in from both
 // blocks.
-if (!PBI || PBI->isUnconditional() || !SafeToMergeTerminators(BI, PBI))
+if (!PBI || PBI->isUnconditional() || is_splat(successors(PBI)) ||
+!SafeToMergeTerminators(BI, PBI))
   continue;
 
 // Determine if the two branches share a common destination.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 0895b83 - [SimplifyCFG] FoldBranchToCommonDest(): don't deal with unconditional branches

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:22:49+03:00
New Revision: 0895b836d74ed333468ddece2102140494eb33b6

URL: 
https://github.com/llvm/llvm-project/commit/0895b836d74ed333468ddece2102140494eb33b6
DIFF: 
https://github.com/llvm/llvm-project/commit/0895b836d74ed333468ddece2102140494eb33b6.diff

LOG: [SimplifyCFG] FoldBranchToCommonDest(): don't deal with unconditional 
branches

The case where BB ends with an unconditional branch,
and has a single predecessor w/ conditional branch
to BB and a single successor of BB is exactly the pattern
SpeculativelyExecuteBB() transform deals with.
(and in this case they both allow speculating only a single instruction)

Well, or FoldTwoEntryPHINode(), if the final block
has only those two predecessors.

Here, in FoldBranchToCommonDest(), only a weird subset of that
transform is supported, and it's glued on the side in a weird way.
  In particular, it took me a bit to understand that the Cond
isn't actually a branch condition in that case, but just the value
we allow to speculate (otherwise it reads as a miscompile to me).
  Additionally, this only supports for the speculated instruction
to be an ICmp.

So let's just unclutter FoldBranchToCommonDest(), and leave
this transform up to SpeculativelyExecuteBB(). As far as i can tell,
this shouldn't really impact optimization potential, but if it does,
improving SpeculativelyExecuteBB() will be more beneficial anyways.

Notably, this only affects a single test,
but EarlyCSE should have run beforehand in the pipeline,
and then FoldTwoEntryPHINode() would have caught it.

This reverts commit rL158392 / commit d33f4efbfdef6ffccf212ab3e40a7673589085fd.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/branch-fold.ll

Removed: 
llvm/test/Transforms/SimplifyCFG/X86/fold-branch-debuginvariant.ll



diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a243a2e37950..d0028c013fa3 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -314,46 +314,6 @@ SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
   return !Fail;
 }
 
-/// Return true if it is safe and profitable to merge these two terminator
-/// instructions together, where SI1 is an unconditional branch. PhiNodes will
-/// store all PHI nodes in common successors.
-static bool
-isProfitableToFoldUnconditional(BranchInst *SI1, BranchInst *SI2,
-Instruction *Cond,
-SmallVectorImpl &PhiNodes) {
-  if (SI1 == SI2)
-return false; // Can't merge with self!
-  assert(SI1->isUnconditional() && SI2->isConditional());
-
-  // We fold the unconditional branch if we can easily update all PHI nodes in
-  // common successors:
-  // 1> We have a constant incoming value for the conditional branch;
-  // 2> We have "Cond" as the incoming value for the unconditional branch;
-  // 3> SI2->getCondition() and Cond have same operands.
-  CmpInst *Ci2 = dyn_cast(SI2->getCondition());
-  if (!Ci2)
-return false;
-  if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
-Cond->getOperand(1) == Ci2->getOperand(1)) &&
-  !(Cond->getOperand(0) == Ci2->getOperand(1) &&
-Cond->getOperand(1) == Ci2->getOperand(0)))
-return false;
-
-  BasicBlock *SI1BB = SI1->getParent();
-  BasicBlock *SI2BB = SI2->getParent();
-  SmallPtrSet SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
-  for (BasicBlock *Succ : successors(SI2BB))
-if (SI1Succs.count(Succ))
-  for (BasicBlock::iterator BBI = Succ->begin(); isa(BBI); ++BBI) 
{
-PHINode *PN = cast(BBI);
-if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
-!isa(PN->getIncomingValueForBlock(SI2BB)))
-  return false;
-PhiNodes.push_back(PN);
-  }
-  return true;
-}
-
 /// Update PHI nodes in Succ to indicate that there will now be entries in it
 /// from the 'NewPred' block. The values that will be flowing into the PHI 
nodes
 /// will be the same as those coming in from ExistPred, an existing predecessor
@@ -2783,23 +2743,6 @@ bool 
SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
   return true;
 }
 
-/// Return true if the given instruction is available
-/// in its predecessor block. If yes, the instruction will be removed.
-static bool tryCSEWithPredecessor(Instruction *Inst, BasicBlock *PB) {
-  if (!isa(Inst) && !isa(Inst))
-return false;
-  for (Instruction &I : *PB) {
-Instruction *PBI = &I;
-// Check whether Inst and PBI generate the same value.
-if (Inst->isIdenticalTo(PBI)) {
-  Inst->replaceAllUsesWith(PBI);
-  Inst->eraseFromParent();
-  return true;
-}
-  }
-  return false;
-}
-
 /// Return true if either PBI or BI has branch weight available, and store
 /// the weights in {Pred|Succ}{True|False}Weight. If one of PBI

[llvm-branch-commits] [llvm] 256a035 - [NFC][SimplifyCFG] FoldBranchToCommonDest(): unclutter Cond/CondInPred handling

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:11+03:00
New Revision: 256a0357524b6cea3c705a77ec3d3c0122ede861

URL: 
https://github.com/llvm/llvm-project/commit/256a0357524b6cea3c705a77ec3d3c0122ede861
DIFF: 
https://github.com/llvm/llvm-project/commit/256a0357524b6cea3c705a77ec3d3c0122ede861.diff

LOG: [NFC][SimplifyCFG] FoldBranchToCommonDest(): unclutter Cond/CondInPred 
handling

We don't need those variables, we can just get the final value directly.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 5ca8a0b33176..d1d687c721fb 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2934,16 +2934,12 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 // Note that there may be multiple predecessor blocks, so we cannot move
 // bonus instructions to a predecessor block.
 ValueToValueMapTy VMap; // maps original values to cloned values
-Instruction *CondInPred;
 for (Instruction &BonusInst : *BB) {
   if (isa(BonusInst) || isa(BonusInst))
 continue;
 
   Instruction *NewBonusInst = BonusInst.clone();
 
-  if (&BonusInst == Cond)
-CondInPred = NewBonusInst;
-
   if (PBI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
 // Unless the instruction has the same !dbg location as the original
 // branch, drop it. When we fold the bonus instructions we want to make
@@ -3004,8 +3000,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 
 // Now that the Cond was cloned into the predecessor basic block,
 // or/and the two conditions together.
-Instruction *NewCond = cast(
-Builder.CreateBinOp(Opc, PBI->getCondition(), CondInPred, "or.cond"));
+Instruction *NewCond = cast(Builder.CreateBinOp(
+Opc, PBI->getCondition(), VMap[BI->getCondition()], "or.cond"));
 PBI->setCondition(NewCond);
 
 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 7b89efb - [NFC][SimplifyCFG] FoldBranchToCommonDest(): somewhat better structure weight updating code

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:41+03:00
New Revision: 7b89efb55e4e5d6078aa9571f40859cc9ea01bcc

URL: 
https://github.com/llvm/llvm-project/commit/7b89efb55e4e5d6078aa9571f40859cc9ea01bcc
DIFF: 
https://github.com/llvm/llvm-project/commit/7b89efb55e4e5d6078aa9571f40859cc9ea01bcc.diff

LOG: [NFC][SimplifyCFG] FoldBranchToCommonDest(): somewhat better structure 
weight updating code

Hoist the successor updating out of the code that deals with branch
weight updating, and hoist the 'has weights' check from the latter,
making code more consistent and easier to follow.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d1d687c721fb..bdf4280609f1 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3005,13 +3005,11 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 PBI->setCondition(NewCond);
 
 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
-bool HasWeights =
-extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
-   SuccTrueWeight, SuccFalseWeight);
-SmallVector NewWeights;
+if (extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
+   SuccTrueWeight, SuccFalseWeight)) {
+  SmallVector NewWeights;
 
-if (PBI->getSuccessor(0) == BB) {
-  if (HasWeights) {
+  if (PBI->getSuccessor(0) == BB) {
 // PBI: br i1 %x, BB, FalseDest
 // BI:  br i1 %y, UniqueSucc, FalseDest
 // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
@@ -3023,11 +3021,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 NewWeights.push_back(PredFalseWeight *
  (SuccFalseWeight + SuccTrueWeight) +
  PredTrueWeight * SuccFalseWeight);
-  }
-  PBI->setSuccessor(0, UniqueSucc);
-}
-if (PBI->getSuccessor(1) == BB) {
-  if (HasWeights) {
+  } else {
 // PBI: br i1 %x, TrueDest, BB
 // BI:  br i1 %y, TrueDest, UniqueSucc
 // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
@@ -3038,17 +3032,20 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
 NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
   }
-  PBI->setSuccessor(1, UniqueSucc);
-}
-if (NewWeights.size() == 2) {
+
   // Halve the weights if any of them cannot fit in an uint32_t
   FitWeights(NewWeights);
 
   SmallVector MDWeights(NewWeights.begin(), NewWeights.end());
   setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
+
+  // TODO: If BB is reachable from all paths through PredBlock, then we
+  // could replace PBI's branch probabilities with BI's.
 } else
   PBI->setMetadata(LLVMContext::MD_prof, nullptr);
 
+PBI->setSuccessor(PBI->getSuccessor(0) != BB, UniqueSucc);
+
 if (DTU)
   DTU->applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
  {DominatorTree::Delete, PredBlock, BB}});
@@ -3058,9 +3055,6 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
   PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
 
-// TODO: If BB is reachable from all paths through PredBlock, then we
-// could replace PBI's branch probabilities with BI's.
-
 // Copy any debug value intrinsics into the end of PredBlock.
 for (Instruction &I : *BB) {
   if (isa(I)) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] b482560 - [NFC][SimplifyCFG] FoldBranchToCommonDest(): extract check for destination sharing into a helper function

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:53+03:00
New Revision: b482560a597697789d81e4b9b22fb14e1f2f3c9a

URL: 
https://github.com/llvm/llvm-project/commit/b482560a597697789d81e4b9b22fb14e1f2f3c9a
DIFF: 
https://github.com/llvm/llvm-project/commit/b482560a597697789d81e4b9b22fb14e1f2f3c9a.diff

LOG: [NFC][SimplifyCFG] FoldBranchToCommonDest(): extract check for destination 
sharing into a helper function

As a follow-up, i'll extract the actual transform into a function,
and this helper will be called from both places,
so this avoids code duplication.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index bdf4280609f1..4c830be4e6ab 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2766,6 +2766,27 @@ static bool extractPredSuccWeights(BranchInst *PBI, 
BranchInst *BI,
   }
 }
 
+// Determine if the two branches share a common destination,
+// and deduce a glue that we need to use to join branch's conditions
+// to arrive at the common destination.
+static Optional>
+CheckIfCondBranchesShareCommonDestination(BranchInst *BI, BranchInst *PBI) {
+  assert(BI && PBI && BI->isConditional() && PBI->isConditional() &&
+ "Both blocks must end with a conditional branches.");
+  assert(is_contained(predecessors(BI->getParent()), PBI->getParent()) &&
+ "PredBB must be a predecessor of BB.");
+
+  if (PBI->getSuccessor(0) == BI->getSuccessor(0))
+return {{Instruction::Or, false}};
+  else if (PBI->getSuccessor(1) == BI->getSuccessor(1))
+return {{Instruction::And, false}};
+  else if (PBI->getSuccessor(0) == BI->getSuccessor(1))
+return {{Instruction::And, true}};
+  else if (PBI->getSuccessor(1) == BI->getSuccessor(0))
+return {{Instruction::Or, true}};
+  return None;
+}
+
 /// If this basic block is simple enough, and if a predecessor branches to us
 /// and one of our successors, fold the block into the predecessor and use
 /// logical operations to pick the right destination.
@@ -2868,22 +2889,12 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
   continue;
 
 // Determine if the two branches share a common destination.
-Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
-bool InvertPredCond = false;
-
-if (PBI->getSuccessor(0) == TrueDest) {
-  Opc = Instruction::Or;
-} else if (PBI->getSuccessor(1) == FalseDest) {
-  Opc = Instruction::And;
-} else if (PBI->getSuccessor(0) == FalseDest) {
-  Opc = Instruction::And;
-  InvertPredCond = true;
-} else if (PBI->getSuccessor(1) == TrueDest) {
-  Opc = Instruction::Or;
-  InvertPredCond = true;
-} else {
+Instruction::BinaryOps Opc;
+bool InvertPredCond;
+if (auto Recepie = CheckIfCondBranchesShareCommonDestination(BI, PBI))
+  std::tie(Opc, InvertPredCond) = *Recepie;
+else
   continue;
-}
 
 // Check the cost of inserting the necessary logic before performing the
 // transformation.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] efeb8ca - [NFC][SimplifyCFG] FoldBranchToCommonDest(): extract the actual transform into helper function

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:53+03:00
New Revision: efeb8caf8bd10f2ad794c6f434fbc4ba133cd7e3

URL: 
https://github.com/llvm/llvm-project/commit/efeb8caf8bd10f2ad794c6f434fbc4ba133cd7e3
DIFF: 
https://github.com/llvm/llvm-project/commit/efeb8caf8bd10f2ad794c6f434fbc4ba133cd7e3.diff

LOG: [NFC][SimplifyCFG] FoldBranchToCommonDest(): extract the actual transform 
into helper function

I'm intentionally structuring it this way, so that the actual fold only
does the fold, and no legality/correctness checks, all of which must be
done by the caller. This allows for the fold code to be more compact
and more easily grokable.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 4c830be4e6ab..4191f2104f3d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2787,6 +2787,188 @@ CheckIfCondBranchesShareCommonDestination(BranchInst 
*BI, BranchInst *PBI) {
   return None;
 }
 
+static bool PerformBranchToCommonDestFolding(BranchInst *BI, BranchInst *PBI,
+ DomTreeUpdater *DTU,
+ MemorySSAUpdater *MSSAU) {
+  BasicBlock *BB = BI->getParent();
+  BasicBlock *PredBlock = PBI->getParent();
+
+  // Determine if the two branches share a common destination.
+  Instruction::BinaryOps Opc;
+  bool InvertPredCond;
+  std::tie(Opc, InvertPredCond) =
+  *CheckIfCondBranchesShareCommonDestination(BI, PBI);
+
+  LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
+
+  IRBuilder<> Builder(PBI);
+  // The builder is used to create instructions to eliminate the branch in BB.
+  // If BB's terminator has !annotation metadata, add it to the new
+  // instructions.
+  Builder.CollectMetadataToCopy(BB->getTerminator(),
+{LLVMContext::MD_annotation});
+
+  // If we need to invert the condition in the pred block to match, do so now.
+  if (InvertPredCond) {
+Value *NewCond = PBI->getCondition();
+if (NewCond->hasOneUse() && isa(NewCond)) {
+  CmpInst *CI = cast(NewCond);
+  CI->setPredicate(CI->getInversePredicate());
+} else {
+  NewCond =
+  Builder.CreateNot(NewCond, PBI->getCondition()->getName() + ".not");
+}
+
+PBI->setCondition(NewCond);
+PBI->swapSuccessors();
+  }
+
+  BasicBlock *UniqueSucc =
+  PBI->getSuccessor(0) == BB ? BI->getSuccessor(0) : BI->getSuccessor(1);
+
+  // Before cloning instructions, notify the successor basic block that it
+  // is about to have a new predecessor. This will update PHI nodes,
+  // which will allow us to update live-out uses of bonus instructions.
+  AddPredecessorToBlock(UniqueSucc, PredBlock, BB, MSSAU);
+
+  // If we have bonus instructions, clone them into the predecessor block.
+  // Note that there may be multiple predecessor blocks, so we cannot move
+  // bonus instructions to a predecessor block.
+  ValueToValueMapTy VMap; // maps original values to cloned values
+  for (Instruction &BonusInst : *BB) {
+if (isa(BonusInst) || isa(BonusInst))
+  continue;
+
+Instruction *NewBonusInst = BonusInst.clone();
+
+if (PBI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
+  // Unless the instruction has the same !dbg location as the original
+  // branch, drop it. When we fold the bonus instructions we want to make
+  // sure we reset their debug locations in order to avoid stepping on
+  // dead code caused by folding dead branches.
+  NewBonusInst->setDebugLoc(DebugLoc());
+}
+
+RemapInstruction(NewBonusInst, VMap,
+ RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
+VMap[&BonusInst] = NewBonusInst;
+
+// If we moved a load, we cannot any longer claim any knowledge about
+// its potential value. The previous information might have been valid
+// only given the branch precondition.
+// For an analogous reason, we must also drop all the metadata whose
+// semantics we don't understand. We *can* preserve !annotation, because
+// it is tied to the instruction itself, not the value or position.
+NewBonusInst->dropUnknownNonDebugMetadata(LLVMContext::MD_annotation);
+
+PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
+NewBonusInst->takeName(&BonusInst);
+BonusInst.setName(BonusInst.getName() + ".old");
+BonusInst.replaceUsesWithIf(
+NewBonusInst, [BB, BI, UniqueSucc, PredBlock](Use &U) {
+  auto *User = cast(U.getUser());
+  // Ignore non-external uses of bonus instructions.
+  if (User->getParent() == BB) {
+assert(!isa(User) &&
+   "Non-external users are never PHI instructions.");
+return false;
+  }
+  i

[llvm-branch-commits] [llvm] 4ed0d8f - [NFC][InstCombine] Extract freelyInvertAllUsersOf() out of canonicalizeICmpPredicate()

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:53+03:00
New Revision: 4ed0d8f2f07d0e17942366d48a29c165384ace52

URL: 
https://github.com/llvm/llvm-project/commit/4ed0d8f2f07d0e17942366d48a29c165384ace52
DIFF: 
https://github.com/llvm/llvm-project/commit/4ed0d8f2f07d0e17942366d48a29c165384ace52.diff

LOG: [NFC][InstCombine] Extract freelyInvertAllUsersOf() out of 
canonicalizeICmpPredicate()

I'd like to use it in an upcoming fold.

Added: 


Modified: 
llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Removed: 




diff  --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h 
b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
index a5aed720cda6..aae0694e4cab 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
@@ -263,8 +263,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
   }
 
   /// Given i1 V, can every user of V be freely adapted if V is changed to !V ?
-  /// InstCombine's canonicalizeICmpPredicate() must be kept in sync with this
-  /// fn.
+  /// InstCombine's freelyInvertAllUsersOf() must be kept in sync with this fn.
   ///
   /// See also: isFreeToInvert()
   static bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) {

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9b3cfb3bd754..cd9a036179b6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5328,26 +5328,8 @@ CmpInst 
*InstCombinerImpl::canonicalizeICmpPredicate(CmpInst &I) {
   I.setPredicate(CmpInst::getInversePredicate(Pred));
   I.setName(I.getName() + ".not");
 
-  // And now let's adjust every user.
-  for (User *U : I.users()) {
-switch (cast(U)->getOpcode()) {
-case Instruction::Select: {
-  auto *SI = cast(U);
-  SI->swapValues();
-  SI->swapProfMetadata();
-  break;
-}
-case Instruction::Br:
-  cast(U)->swapSuccessors(); // swaps prof metadata too
-  break;
-case Instruction::Xor:
-  replaceInstUsesWith(cast(*U), &I);
-  break;
-default:
-  llvm_unreachable("Got unexpected user - out of sync with "
-   "canFreelyInvertAllUsersOf() ?");
-}
-  }
+  // And, adapt users.
+  freelyInvertAllUsersOf(&I);
 
   return &I;
 }

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h 
b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 6468e406c527..16bc26520c18 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -323,6 +323,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
   Instruction *matchSAddSubSat(SelectInst &MinMax1);
 
+  void freelyInvertAllUsersOf(Value *V);
+
   /// Determine if a pair of casts can be replaced by a single cast.
   ///
   /// \param CI1 The first of a pair of casts.

diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp 
b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 570a07ec6a5a..2f8a80a89992 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -870,6 +870,30 @@ Value 
*InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
   return SI;
 }
 
+/// Freely adapt every user of V as-if V was changed to !V.
+/// WARNING: only if canFreelyInvertAllUsersOf() said this can be done.
+void InstCombinerImpl::freelyInvertAllUsersOf(Value *I) {
+  for (User *U : I->users()) {
+switch (cast(U)->getOpcode()) {
+case Instruction::Select: {
+  auto *SI = cast(U);
+  SI->swapValues();
+  SI->swapProfMetadata();
+  break;
+}
+case Instruction::Br:
+  cast(U)->swapSuccessors(); // swaps prof metadata too
+  break;
+case Instruction::Xor:
+  replaceInstUsesWith(cast(*U), I);
+  break;
+default:
+  llvm_unreachable("Got unexpected user - out of sync with "
+   "canFreelyInvertAllUsersOf() ?");
+}
+  }
+}
+
 /// Given a 'sub' instruction, return the RHS of the instruction if the LHS is 
a
 /// constant zero (which is the 'negate' form).
 Value *InstCombinerImpl::dyn_castNegVal(Value *V) const {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 6260490 - [NFC][InstCombine] Add tests for `(~x) &/| y` --> `~(x |/& (~y))` fold

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:54+03:00
New Revision: 62604906b5b29c4a55f83226a60f0de9ff9f8df2

URL: 
https://github.com/llvm/llvm-project/commit/62604906b5b29c4a55f83226a60f0de9ff9f8df2
DIFF: 
https://github.com/llvm/llvm-project/commit/62604906b5b29c4a55f83226a60f0de9ff9f8df2.diff

LOG: [NFC][InstCombine] Add tests for `(~x) &/| y` --> `~(x |/& (~y))` fold

Iff y is free to invert, and the users of the expression can be updated,
we can undo De-Morgan fold, and immediately get rid of the `not` op.

Added: 
llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll
llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll 
b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll
new file mode 100644
index ..14c889b23e39
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll
@@ -0,0 +1,96 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+; Transform
+;   z = (~x) & y
+; into:
+;   z = ~(x | (~y))
+; iff y is free to invert and all uses of z can be freely updated.
+
+declare void @use1(i1)
+
+; Most basic positive test
+define i32 @t0(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
+; CHECK-LABEL: @t0(
+; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0:%.*]], true
+; CHECK-NEXT:[[I3:%.*]] = and i1 [[I1]], [[I2]]
+; CHECK-NEXT:[[I4:%.*]] = select i1 [[I3]], i32 [[V2:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT:ret i32 [[I4]]
+;
+  %i1 = icmp eq i32 %v0, %v1
+  %i2 = xor i1 %i0, -1
+  %i3 = and i1 %i2, %i1
+  %i4 = select i1 %i3, i32 %v2, i32 %v3
+  ret i32 %i4
+}
+define i32 @t1(i32 %v0, i32 %v1, i32 %v2, i32 %v3, i32 %v4, i32 %v5) {
+; CHECK-LABEL: @t1(
+; CHECK-NEXT:[[I0:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V2:%.*]], [[V3:%.*]]
+; CHECK-NEXT:call void @use1(i1 [[I0]])
+; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0]], true
+; CHECK-NEXT:[[I3:%.*]] = and i1 [[I1]], [[I2]]
+; CHECK-NEXT:[[I4:%.*]] = select i1 [[I3]], i32 [[V4:%.*]], i32 [[V5:%.*]]
+; CHECK-NEXT:ret i32 [[I4]]
+;
+  %i0 = icmp eq i32 %v0, %v1
+  %i1 = icmp eq i32 %v2, %v3
+  call void @use1(i1 %i0)
+  %i2 = xor i1 %i0, -1
+  %i3 = and i1 %i2, %i1
+  %i4 = select i1 %i3, i32 %v4, i32 %v5
+  ret i32 %i4
+}
+
+; All users of %i3 must be invertible
+define i1 @n2(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
+; CHECK-LABEL: @n2(
+; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0:%.*]], true
+; CHECK-NEXT:[[I3:%.*]] = and i1 [[I1]], [[I2]]
+; CHECK-NEXT:ret i1 [[I3]]
+;
+  %i1 = icmp eq i32 %v0, %v1
+  %i2 = xor i1 %i0, -1
+  %i3 = and i1 %i2, %i1
+  ret i1 %i3 ; can not be inverted
+}
+
+; %i1 must be invertible
+define i32 @n3(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
+; CHECK-LABEL: @n3(
+; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:call void @use1(i1 [[I1]])
+; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0:%.*]], true
+; CHECK-NEXT:[[I3:%.*]] = and i1 [[I1]], [[I2]]
+; CHECK-NEXT:[[I4:%.*]] = select i1 [[I3]], i32 [[V2:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT:ret i32 [[I4]]
+;
+  %i1 = icmp eq i32 %v0, %v1 ; has extra uninvertible use
+  call void @use1(i1 %i1) ; bad extra use
+  %i2 = xor i1 %i0, -1
+  %i3 = and i1 %i2, %i1
+  %i4 = select i1 %i3, i32 %v2, i32 %v3
+  ret i32 %i4
+}
+
+; FIXME: we could invert all uses of %i1 here
+define i32 @n4(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3, i32 %v4, i32 %v5, 
i32* %dst) {
+; CHECK-LABEL: @n4(
+; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:[[I2:%.*]] = select i1 [[I1]], i32 [[V2:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT:store i32 [[I2]], i32* [[DST:%.*]], align 4
+; CHECK-NEXT:[[I3:%.*]] = xor i1 [[I0:%.*]], true
+; CHECK-NEXT:[[I4:%.*]] = and i1 [[I1]], [[I3]]
+; CHECK-NEXT:[[I5:%.*]] = select i1 [[I4]], i32 [[V4:%.*]], i32 [[V5:%.*]]
+; CHECK-NEXT:ret i32 [[I5]]
+;
+  %i1 = icmp eq i32 %v0, %v1 ; has extra invertible use
+  %i2 = select i1 %i1, i32 %v2, i32 %v3 ; invertible use
+  store i32 %i2, i32* %dst
+  %i3 = xor i1 %i0, -1
+  %i4 = and i1 %i3, %i1
+  %i5 = select i1 %i4, i32 %v4, i32 %v5
+  ret i32 %i5
+}

diff  --git 
a/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll 
b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll
new file mode 100644
index ..57ef48725ead
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll
@@ -0,0 +1,96 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instcombine -S | FileC

[llvm-branch-commits] [llvm] 79b0d21 - [InstCombine] Fold `(~x) & y` --> `~(x | (~y))` iff it is free to do so

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:54+03:00
New Revision: 79b0d21ce92f1a5ff4c822d1a5c664196b338535

URL: 
https://github.com/llvm/llvm-project/commit/79b0d21ce92f1a5ff4c822d1a5c664196b338535
DIFF: 
https://github.com/llvm/llvm-project/commit/79b0d21ce92f1a5ff4c822d1a5c664196b338535.diff

LOG: [InstCombine] Fold `(~x) & y` --> `~(x | (~y))` iff it is free to do so

Iff we know we can get rid of the inversions in the new pattern,
we can thus get rid of the inversion in the old pattern,
this decreasing instruction count.

Added: 


Modified: 
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll

Removed: 




diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index c42f113feca3..944e7c4b1325 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1983,6 +1983,10 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator 
&I) {
 return SelectInst::Create(NewICmpInst, X, ConstantInt::getNullValue(Ty));
   }
 
+  // (~x) & y  -->  ~(x | (~y))  iff that gets rid of inversions
+  if (sinkNotIntoOtherHandOfAnd(I))
+return &I;
+
   return nullptr;
 }
 
@@ -3089,6 +3093,45 @@ static Instruction *sinkNotIntoXor(BinaryOperator &I,
   return BinaryOperator::CreateXor(NotX, Y, I.getName() + ".demorgan");
 }
 
+// Transform
+//   z = (~x) & y
+// into:
+//   z = ~(x | (~y))
+// iff y is free to invert and all uses of z can be freely updated.
+bool InstCombinerImpl::sinkNotIntoOtherHandOfAnd(BinaryOperator &I) {
+  Instruction::BinaryOps NewOpc;
+  switch (I.getOpcode()) {
+  case Instruction::And:
+NewOpc = Instruction::Or;
+break;
+  default:
+return false;
+  };
+
+  Value *X, *Y;
+  if (!match(&I, m_c_BinOp(m_Not(m_Value(X)), m_Value(Y
+return false;
+
+  // Will we be able to fold the `not` into Y eventually?
+  if (!InstCombiner::isFreeToInvert(Y, Y->hasOneUse()))
+return false;
+
+  // And can our users be adapted?
+  if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
+return false;
+
+  Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
+  Value *NewBinOp =
+  BinaryOperator::Create(NewOpc, X, NotY, I.getName() + ".not");
+  Builder.Insert(NewBinOp);
+  replaceInstUsesWith(I, NewBinOp);
+  // We can not just create an outer `not`, it will most likely be immediately
+  // folded back, reconstructing our initial pattern, and causing an
+  // infinite combine loop, so immediately manually fold it away.
+  freelyInvertAllUsersOf(NewBinOp);
+  return true;
+}
+
 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
 // here. We should standardize that construct where it is needed or choose some
 // other way to ensure that commutated variants of patterns are not missed.

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h 
b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 16bc26520c18..c56b31bd227b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -105,6 +105,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
   Instruction *visitAnd(BinaryOperator &I);
   Instruction *visitOr(BinaryOperator &I);
+  bool sinkNotIntoOtherHandOfAnd(BinaryOperator &I);
   Instruction *visitXor(BinaryOperator &I);
   Instruction *visitShl(BinaryOperator &I);
   Value *reassociateShiftAmtsOfTwoSameDirectionShifts(

diff  --git 
a/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll 
b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll
index 14c889b23e39..17c37c836bb3 100644
--- a/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll
+++ b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-and.ll
@@ -12,10 +12,9 @@ declare void @use1(i1)
 ; Most basic positive test
 define i32 @t0(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
 ; CHECK-LABEL: @t0(
-; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
-; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0:%.*]], true
-; CHECK-NEXT:[[I3:%.*]] = and i1 [[I1]], [[I2]]
-; CHECK-NEXT:[[I4:%.*]] = select i1 [[I3]], i32 [[V2:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT:[[I1:%.*]] = icmp ne i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:[[TMP1:%.*]] = or i1 [[I1]], [[I0:%.*]]
+; CHECK-NEXT:[[I4:%.*]] = select i1 [[TMP1]], i32 [[V3:%.*]], i32 
[[V2:%.*]]
 ; CHECK-NEXT:ret i32 [[I4]]
 ;
   %i1 = icmp eq i32 %v0, %v1
@@ -27,11 +26,10 @@ define i32 @t0(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
 define i32 @t1(i32 %v0, i32 %v1, i32 %v2, i32 %v3, i32 %v4, i32 %v5) 

[llvm-branch-commits] [llvm] d1a6f92 - [InstCombine] Fold `(~x) | y` --> `~(x & (~y))` iff it is free to do so

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:23:54+03:00
New Revision: d1a6f92fd545726aab0784e2dcfb193ce185c418

URL: 
https://github.com/llvm/llvm-project/commit/d1a6f92fd545726aab0784e2dcfb193ce185c418
DIFF: 
https://github.com/llvm/llvm-project/commit/d1a6f92fd545726aab0784e2dcfb193ce185c418.diff

LOG: [InstCombine] Fold `(~x) | y` --> `~(x & (~y))` iff it is free to do so

Iff we know we can get rid of the inversions in the new pattern,
we can thus get rid of the inversion in the old pattern,
this decreasing instruction count.

Note that we could position this transformation as just hoisting
of the `not` (still, iff y is freely negatible), but the test changes
show a number of regressions, so let's not do that.

Added: 


Modified: 
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll
llvm/test/Transforms/SimplifyCFG/merge-cond-stores.ll

Removed: 




diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 944e7c4b1325..68c4156af2c4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1984,7 +1984,7 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator 
&I) {
   }
 
   // (~x) & y  -->  ~(x | (~y))  iff that gets rid of inversions
-  if (sinkNotIntoOtherHandOfAnd(I))
+  if (sinkNotIntoOtherHandOfAndOrOr(I))
 return &I;
 
   return nullptr;
@@ -2867,6 +2867,10 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator 
&I) {
 }
   }
 
+  // (~x) | y  -->  ~(x & (~y))  iff that gets rid of inversions
+  if (sinkNotIntoOtherHandOfAndOrOr(I))
+return &I;
+
   return nullptr;
 }
 
@@ -3094,16 +3098,19 @@ static Instruction *sinkNotIntoXor(BinaryOperator &I,
 }
 
 // Transform
-//   z = (~x) & y
+//   z = (~x) &/| y
 // into:
-//   z = ~(x | (~y))
+//   z = ~(x |/& (~y))
 // iff y is free to invert and all uses of z can be freely updated.
-bool InstCombinerImpl::sinkNotIntoOtherHandOfAnd(BinaryOperator &I) {
+bool InstCombinerImpl::sinkNotIntoOtherHandOfAndOrOr(BinaryOperator &I) {
   Instruction::BinaryOps NewOpc;
   switch (I.getOpcode()) {
   case Instruction::And:
 NewOpc = Instruction::Or;
 break;
+  case Instruction::Or:
+NewOpc = Instruction::And;
+break;
   default:
 return false;
   };

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h 
b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index c56b31bd227b..5e466c5e13e7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -105,7 +105,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
   Instruction *visitAnd(BinaryOperator &I);
   Instruction *visitOr(BinaryOperator &I);
-  bool sinkNotIntoOtherHandOfAnd(BinaryOperator &I);
+  bool sinkNotIntoOtherHandOfAndOrOr(BinaryOperator &I);
   Instruction *visitXor(BinaryOperator &I);
   Instruction *visitShl(BinaryOperator &I);
   Value *reassociateShiftAmtsOfTwoSameDirectionShifts(

diff  --git 
a/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll 
b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll
index 57ef48725ead..d40acc37ca73 100644
--- a/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll
+++ b/llvm/test/Transforms/InstCombine/sink-not-into-another-hand-of-or.ll
@@ -12,10 +12,9 @@ declare void @use1(i1)
 ; Most basic positive test
 define i32 @t0(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
 ; CHECK-LABEL: @t0(
-; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
-; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0:%.*]], true
-; CHECK-NEXT:[[I3:%.*]] = or i1 [[I1]], [[I2]]
-; CHECK-NEXT:[[I4:%.*]] = select i1 [[I3]], i32 [[V2:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT:[[I1:%.*]] = icmp ne i32 [[V0:%.*]], [[V1:%.*]]
+; CHECK-NEXT:[[TMP1:%.*]] = and i1 [[I1]], [[I0:%.*]]
+; CHECK-NEXT:[[I4:%.*]] = select i1 [[TMP1]], i32 [[V3:%.*]], i32 
[[V2:%.*]]
 ; CHECK-NEXT:ret i32 [[I4]]
 ;
   %i1 = icmp eq i32 %v0, %v1
@@ -27,11 +26,10 @@ define i32 @t0(i1 %i0, i32 %v0, i32 %v1, i32 %v2, i32 %v3) {
 define i32 @t1(i32 %v0, i32 %v1, i32 %v2, i32 %v3, i32 %v4, i32 %v5) {
 ; CHECK-LABEL: @t1(
 ; CHECK-NEXT:[[I0:%.*]] = icmp eq i32 [[V0:%.*]], [[V1:%.*]]
-; CHECK-NEXT:[[I1:%.*]] = icmp eq i32 [[V2:%.*]], [[V3:%.*]]
+; CHECK-NEXT:[[I1:%.*]] = icmp ne i32 [[V2:%.*]], [[V3:%.*]]
 ; CHECK-NEXT:call void @use1(i1 [[I0]])
-; CHECK-NEXT:[[I2:%.*]] = xor i1 [[I0]], true
-; CHECK-NEXT:[[I3:%.*]] = or i1 [[I1]], [[I2]]
-; CHECK-NEXT:[[I4:%.*]] = select i1 [[I3]], i32 [[V4:%.*]], i32 [[V5:%.*]]
+; CHECK-NEXT:[[TMP1:%.*]] = an

[llvm-branch-commits] [llvm] 85e7578 - Revert "[NFCI-ish][SimplifyCFG] FoldBranchToCommonDest(): really don't deal with uncond branches"

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-22T17:37:11+03:00
New Revision: 85e7578c6db81abb3283cb87fce8592f83ae0ea8

URL: 
https://github.com/llvm/llvm-project/commit/85e7578c6db81abb3283cb87fce8592f83ae0ea8
DIFF: 
https://github.com/llvm/llvm-project/commit/85e7578c6db81abb3283cb87fce8592f83ae0ea8.diff

LOG: Revert "[NFCI-ish][SimplifyCFG] FoldBranchToCommonDest(): really don't 
deal with uncond branches"

Does not build in XCode:
http://green.lab.llvm.org/green/job/clang-stage1-RA/17963/consoleFull#-1704658317a1ca8a51-895e-46c6-af87-ce24fa4cd561

This reverts commit aabed3718ae25476c0f6b7e70c83ba4658f00e5c.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 4191f2104f3d..5fbcdd6abf6d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2978,7 +2978,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
   unsigned BonusInstThreshold) {
   // If this block ends with an unconditional branch,
   // let SpeculativelyExecuteBB() deal with it.
-  if (!BI->isConditional() || is_splat(successors(BI)))
+  if (!BI->isConditional())
 return false;
 
   BasicBlock *BB = BI->getParent();
@@ -3059,8 +3059,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 // Check that we have two conditional branches.  If there is a PHI node in
 // the common successor, verify that the same value flows in from both
 // blocks.
-if (!PBI || PBI->isUnconditional() || is_splat(successors(PBI)) ||
-!SafeToMergeTerminators(BI, PBI))
+if (!PBI || PBI->isUnconditional() || !SafeToMergeTerminators(BI, PBI))
   continue;
 
 // Determine if the two branches share a common destination.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 9bd8bcf - [NFC][SimplifyCFG] PerformBranchToCommonDestFolding(): fix instruction name preservation

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-23T01:29:03+03:00
New Revision: 9bd8bcf993f6e829b2417deefbab78c610436a11

URL: 
https://github.com/llvm/llvm-project/commit/9bd8bcf993f6e829b2417deefbab78c610436a11
DIFF: 
https://github.com/llvm/llvm-project/commit/9bd8bcf993f6e829b2417deefbab78c610436a11.diff

LOG: [NFC][SimplifyCFG] PerformBranchToCommonDestFolding(): fix instruction 
name preservation

NewBonusInst just took name from BonusInst, so BonusInst has no name,
so BonusInst.getName() makes no sense.
So we need to ask NewBonusInst for the name.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 5fbcdd6abf6d..c160bd2b3cd8 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2863,7 +2863,7 @@ static bool PerformBranchToCommonDestFolding(BranchInst 
*BI, BranchInst *PBI,
 
 PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
 NewBonusInst->takeName(&BonusInst);
-BonusInst.setName(BonusInst.getName() + ".old");
+BonusInst.setName(NewBonusInst->getName() + ".old");
 BonusInst.replaceUsesWithIf(
 NewBonusInst, [BB, BI, UniqueSucc, PredBlock](Use &U) {
   auto *User = cast(U.getUser());

diff  --git a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll 
b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
index 7455cc691cf9..927f81ae69a1 100644
--- a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
+++ b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
@@ -44,9 +44,9 @@ define void @two_preds(i8 %v0, i8 %v1, i8 %v2, i8 %v3) {
 ; CHECK-NEXT:br i1 [[C0]], label [[PRED0:%.*]], label [[PRED1:%.*]]
 ; CHECK:   pred0:
 ; CHECK-NEXT:[[C1:%.*]] = icmp eq i8 [[V1:%.*]], 0
-; CHECK-NEXT:[[DOTOLD:%.*]] = icmp eq i8 [[V3:%.*]], 0
-; CHECK-NEXT:[[OR_COND2:%.*]] = or i1 [[C1]], [[DOTOLD]]
-; CHECK-NEXT:br i1 [[OR_COND2]], label [[FINAL_LEFT:%.*]], label 
[[FINAL_RIGHT:%.*]]
+; CHECK-NEXT:[[C3_OLD:%.*]] = icmp eq i8 [[V3:%.*]], 0
+; CHECK-NEXT:[[OR_COND1:%.*]] = or i1 [[C1]], [[C3_OLD]]
+; CHECK-NEXT:br i1 [[OR_COND1]], label [[FINAL_LEFT:%.*]], label 
[[FINAL_RIGHT:%.*]]
 ; CHECK:   pred1:
 ; CHECK-NEXT:[[C2:%.*]] = icmp eq i8 [[V2:%.*]], 0
 ; CHECK-NEXT:[[C3:%.*]] = icmp eq i8 [[V3]], 0
@@ -118,10 +118,10 @@ define void @two_preds_with_extra_op(i8 %v0, i8 %v1, i8 
%v2, i8 %v3) {
 ; CHECK-NEXT:br i1 [[C0]], label [[PRED0:%.*]], label [[PRED1:%.*]]
 ; CHECK:   pred0:
 ; CHECK-NEXT:[[C1:%.*]] = icmp eq i8 [[V1:%.*]], 0
-; CHECK-NEXT:[[DOTOLD:%.*]] = add i8 [[V1]], [[V2:%.*]]
-; CHECK-NEXT:[[DOTOLD1:%.*]] = icmp eq i8 [[DOTOLD]], 0
-; CHECK-NEXT:[[OR_COND4:%.*]] = or i1 [[C1]], [[DOTOLD1]]
-; CHECK-NEXT:br i1 [[OR_COND4]], label [[FINAL_LEFT:%.*]], label 
[[FINAL_RIGHT:%.*]]
+; CHECK-NEXT:[[V3_ADJ_OLD:%.*]] = add i8 [[V1]], [[V2:%.*]]
+; CHECK-NEXT:[[C3_OLD:%.*]] = icmp eq i8 [[V3_ADJ_OLD]], 0
+; CHECK-NEXT:[[OR_COND1:%.*]] = or i1 [[C1]], [[C3_OLD]]
+; CHECK-NEXT:br i1 [[OR_COND1]], label [[FINAL_LEFT:%.*]], label 
[[FINAL_RIGHT:%.*]]
 ; CHECK:   pred1:
 ; CHECK-NEXT:[[C2:%.*]] = icmp eq i8 [[V2]], 0
 ; CHECK-NEXT:[[V3_ADJ:%.*]] = add i8 [[V1]], [[V2]]
@@ -197,11 +197,11 @@ define void @two_preds_with_extra_op_multiuse(i8 %v0, i8 
%v1, i8 %v2, i8 %v3) {
 ; CHECK-NEXT:br i1 [[C0]], label [[PRED0:%.*]], label [[PRED1:%.*]]
 ; CHECK:   pred0:
 ; CHECK-NEXT:[[C1:%.*]] = icmp eq i8 [[V1:%.*]], 0
-; CHECK-NEXT:[[DOTOLD:%.*]] = add i8 [[V1]], [[V2:%.*]]
-; CHECK-NEXT:[[DOTOLD1:%.*]] = add i8 [[DOTOLD]], [[DOTOLD]]
-; CHECK-NEXT:[[DOTOLD2:%.*]] = icmp eq i8 [[DOTOLD1]], 0
-; CHECK-NEXT:[[OR_COND6:%.*]] = or i1 [[C1]], [[DOTOLD2]]
-; CHECK-NEXT:br i1 [[OR_COND6]], label [[FINAL_LEFT:%.*]], label 
[[FINAL_RIGHT:%.*]]
+; CHECK-NEXT:[[V3_ADJ_OLD:%.*]] = add i8 [[V1]], [[V2:%.*]]
+; CHECK-NEXT:[[V3_ADJ_ADJ_OLD:%.*]] = add i8 [[V3_ADJ_OLD]], [[V3_ADJ_OLD]]
+; CHECK-NEXT:[[C3_OLD:%.*]] = icmp eq i8 [[V3_ADJ_ADJ_OLD]], 0
+; CHECK-NEXT:[[OR_COND1:%.*]] = or i1 [[C1]], [[C3_OLD]]
+; CHECK-NEXT:br i1 [[OR_COND1]], label [[FINAL_LEFT:%.*]], label 
[[FINAL_RIGHT:%.*]]
 ; CHECK:   pred1:
 ; CHECK-NEXT:[[C2:%.*]] = icmp eq i8 [[V2]], 0
 ; CHECK-NEXT:[[V3_ADJ:%.*]] = add i8 [[V1]], [[V2]]
@@ -372,11 +372,11 @@ define void @two_preds_with_extra_op_liveout(i8 %v0, i8 
%v1, i8 %v2, i8 %v3) {
 ; CHECK-NEXT:[[OR_COND:%.*]] = and i1 [[C2]], [[C3]]
 ; CHECK-NEXT:br i1 [[OR_COND]], label [[FINAL_LEFT]], label 
[[FINAL_RIGHT:%.*]]
 ; CHECK:   dispatch:
-; CHECK-NEXT:[[DOTOLD:%.*]] = add i8 [[V1]], [[V2]]
-; CHECK-NEXT:[[DOTOLD1:%.*]] = 

[llvm-branch-commits] [llvm] eae1cc0 - [NFC][SimplifyCFG] PerformBranchToCommonDestFolding(): move instruction cloning to after CFG update

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-23T01:29:04+03:00
New Revision: eae1cc0de5b9c3b97ce1b6f4275b474ab10b83d0

URL: 
https://github.com/llvm/llvm-project/commit/eae1cc0de5b9c3b97ce1b6f4275b474ab10b83d0
DIFF: 
https://github.com/llvm/llvm-project/commit/eae1cc0de5b9c3b97ce1b6f4275b474ab10b83d0.diff

LOG: [NFC][SimplifyCFG] PerformBranchToCommonDestFolding(): move instruction 
cloning to after CFG update

This simplifies follow-up patch, and is NFC otherwise.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index c160bd2b3cd8..510387bbf9ef 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2831,6 +2831,58 @@ static bool PerformBranchToCommonDestFolding(BranchInst 
*BI, BranchInst *PBI,
   // which will allow us to update live-out uses of bonus instructions.
   AddPredecessorToBlock(UniqueSucc, PredBlock, BB, MSSAU);
 
+  // Try to update branch weights.
+  uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
+  if (extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
+ SuccTrueWeight, SuccFalseWeight)) {
+SmallVector NewWeights;
+
+if (PBI->getSuccessor(0) == BB) {
+  // PBI: br i1 %x, BB, FalseDest
+  // BI:  br i1 %y, UniqueSucc, FalseDest
+  // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
+  NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
+  // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
+  //   TrueWeight for PBI * FalseWeight for BI.
+  // We assume that total weights of a BranchInst can fit into 32 bits.
+  // Therefore, we will not have overflow using 64-bit arithmetic.
+  NewWeights.push_back(PredFalseWeight *
+   (SuccFalseWeight + SuccTrueWeight) +
+   PredTrueWeight * SuccFalseWeight);
+} else {
+  // PBI: br i1 %x, TrueDest, BB
+  // BI:  br i1 %y, TrueDest, UniqueSucc
+  // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
+  //  FalseWeight for PBI * TrueWeight for BI.
+  NewWeights.push_back(PredTrueWeight * (SuccFalseWeight + SuccTrueWeight) 
+
+   PredFalseWeight * SuccTrueWeight);
+  // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
+  NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
+}
+
+// Halve the weights if any of them cannot fit in an uint32_t
+FitWeights(NewWeights);
+
+SmallVector MDWeights(NewWeights.begin(), NewWeights.end());
+setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
+
+// TODO: If BB is reachable from all paths through PredBlock, then we
+// could replace PBI's branch probabilities with BI's.
+  } else
+PBI->setMetadata(LLVMContext::MD_prof, nullptr);
+
+  // Now, update the CFG.
+  PBI->setSuccessor(PBI->getSuccessor(0) != BB, UniqueSucc);
+
+  if (DTU)
+DTU->applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
+   {DominatorTree::Delete, PredBlock, BB}});
+
+  // If BI was a loop latch, it may have had associated loop metadata.
+  // We need to copy it to the new latch, that is, PBI.
+  if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
+PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
+
   // If we have bonus instructions, clone them into the predecessor block.
   // Note that there may be multiple predecessor blocks, so we cannot move
   // bonus instructions to a predecessor block.
@@ -2905,56 +2957,6 @@ static bool PerformBranchToCommonDestFolding(BranchInst 
*BI, BranchInst *PBI,
   Opc, PBI->getCondition(), VMap[BI->getCondition()], "or.cond"));
   PBI->setCondition(NewCond);
 
-  uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
-  if (extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
- SuccTrueWeight, SuccFalseWeight)) {
-SmallVector NewWeights;
-
-if (PBI->getSuccessor(0) == BB) {
-  // PBI: br i1 %x, BB, FalseDest
-  // BI:  br i1 %y, UniqueSucc, FalseDest
-  // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
-  NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
-  // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
-  //   TrueWeight for PBI * FalseWeight for BI.
-  // We assume that total weights of a BranchInst can fit into 32 bits.
-  // Therefore, we will not have overflow using 64-bit arithmetic.
-  NewWeights.push_back(PredFalseWeight *
-   (SuccFalseWeight + SuccTrueWeight) +
-   PredTrueWeight * SuccFalseWeight);
-} else {
-  // PBI: br i1 %x, TrueDest, BB
-  // BI:  br i1 %y, TrueDest, UniqueSucc
-  

[llvm-branch-commits] [llvm] e838750 - [NFC][SimplifyCFG] fold-branch-to-common-dest.ll: reduce complexity of @pr48450* test

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-23T01:29:04+03:00
New Revision: e8387500059e7f3c261b2127a241b2c4c81ab36b

URL: 
https://github.com/llvm/llvm-project/commit/e8387500059e7f3c261b2127a241b2c4c81ab36b
DIFF: 
https://github.com/llvm/llvm-project/commit/e8387500059e7f3c261b2127a241b2c4c81ab36b.diff

LOG: [NFC][SimplifyCFG] fold-branch-to-common-dest.ll: reduce complexity of 
@pr48450* test

We don't need that many iterations there,
having less iterations helps alive2 verify it.

Added: 


Modified: 
llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll

Removed: 




diff  --git a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll 
b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
index 927f81ae69a1..ba7a5840d600 100644
--- a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
+++ b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
@@ -739,12 +739,12 @@ define void @pr48450() {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:br label [[FOR_BODY:%.*]]
 ; CHECK:   for.body:
-; CHECK-NEXT:[[COUNTDOWN:%.*]] = phi i16 [ 128, [[ENTRY:%.*]] ], [ 
[[DEC:%.*]], [[FOR_BODYTHREAD_PRE_SPLIT:%.*]] ]
+; CHECK-NEXT:[[COUNTDOWN:%.*]] = phi i8 [ 8, [[ENTRY:%.*]] ], [ 
[[DEC:%.*]], [[FOR_BODYTHREAD_PRE_SPLIT:%.*]] ]
 ; CHECK-NEXT:[[C:%.*]] = call i1 @gen1()
 ; CHECK-NEXT:br i1 [[C]], label [[FOR_INC:%.*]], label [[IF_THEN:%.*]]
 ; CHECK:   for.inc:
-; CHECK-NEXT:[[DEC]] = add i16 [[COUNTDOWN]], -1
-; CHECK-NEXT:[[CMP_NOT:%.*]] = icmp eq i16 [[COUNTDOWN]], 0
+; CHECK-NEXT:[[DEC]] = add i8 [[COUNTDOWN]], -1
+; CHECK-NEXT:[[CMP_NOT:%.*]] = icmp eq i8 [[COUNTDOWN]], 0
 ; CHECK-NEXT:br i1 [[CMP_NOT]], label [[IF_END_LOOPEXIT:%.*]], label 
[[FOR_BODYTHREAD_PRE_SPLIT]]
 ; CHECK:   if.then:
 ; CHECK-NEXT:[[C2:%.*]] = call i1 @gen1()
@@ -759,13 +759,13 @@ entry:
   br label %for.body
 
 for.body:
-  %countdown = phi i16 [ 128, %entry ], [ %dec, %for.bodythread-pre-split ]
+  %countdown = phi i8 [ 8, %entry ], [ %dec, %for.bodythread-pre-split ]
   %c = call i1 @gen1()
   br i1 %c, label %for.inc, label %if.then
 
 for.inc:
-  %dec = add i16 %countdown, -1
-  %cmp.not = icmp eq i16 %countdown, 0
+  %dec = add i8 %countdown, -1
+  %cmp.not = icmp eq i8 %countdown, 0
   br i1 %cmp.not, label %if.end.loopexit, label %for.bodythread-pre-split
 
 if.then:
@@ -785,12 +785,12 @@ define void @pr48450_2(i1 %enable_loopback) {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:br label [[FOR_BODY:%.*]]
 ; CHECK:   for.body:
-; CHECK-NEXT:[[COUNTDOWN:%.*]] = phi i16 [ 128, [[ENTRY:%.*]] ], [ 
[[DEC:%.*]], [[FOR_BODYTHREAD_PRE_SPLIT:%.*]] ]
+; CHECK-NEXT:[[COUNTDOWN:%.*]] = phi i8 [ 8, [[ENTRY:%.*]] ], [ 
[[DEC:%.*]], [[FOR_BODYTHREAD_PRE_SPLIT:%.*]] ]
 ; CHECK-NEXT:[[C:%.*]] = call i1 @gen1()
 ; CHECK-NEXT:br i1 [[C]], label [[FOR_INC:%.*]], label [[IF_THEN:%.*]]
 ; CHECK:   for.inc:
-; CHECK-NEXT:[[DEC]] = add i16 [[COUNTDOWN]], -1
-; CHECK-NEXT:[[CMP_NOT:%.*]] = icmp eq i16 [[COUNTDOWN]], 0
+; CHECK-NEXT:[[DEC]] = add i8 [[COUNTDOWN]], -1
+; CHECK-NEXT:[[CMP_NOT:%.*]] = icmp eq i8 [[COUNTDOWN]], 0
 ; CHECK-NEXT:br i1 [[CMP_NOT]], label [[IF_END_LOOPEXIT:%.*]], label 
[[FOR_BODYTHREAD_PRE_SPLIT]]
 ; CHECK:   if.then:
 ; CHECK-NEXT:[[C2:%.*]] = call i1 @gen1()
@@ -810,13 +810,13 @@ entry:
   br label %for.body
 
 for.body:
-  %countdown = phi i16 [ 128, %entry ], [ %dec, %for.bodythread-pre-split ]
+  %countdown = phi i8 [ 8, %entry ], [ %dec, %for.bodythread-pre-split ]
   %c = call i1 @gen1()
   br i1 %c, label %for.inc, label %if.then
 
 for.inc:
-  %dec = add i16 %countdown, -1
-  %cmp.not = icmp eq i16 %countdown, 0
+  %dec = add i8 %countdown, -1
+  %cmp.not = icmp eq i8 %countdown, 0
   br i1 %cmp.not, label %if.end.loopexit, label %for.bodythread-pre-split
 
 if.then:
@@ -837,14 +837,14 @@ if.end.loopexit:
   ret void
 }
 
-@f.b = external global i16, align 1
+@f.b = external global i8, align 1
 define void @pr48450_3() {
 ; CHECK-LABEL: @pr48450_3(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:br label [[FOR_COND1:%.*]]
 ; CHECK:   for.cond1:
-; CHECK-NEXT:[[V:%.*]] = load i16, i16* @f.b, align 1
-; CHECK-NEXT:[[CMP:%.*]] = icmp slt i16 [[V]], 1
+; CHECK-NEXT:[[V:%.*]] = load i8, i8* @f.b, align 1
+; CHECK-NEXT:[[CMP:%.*]] = icmp slt i8 [[V]], 1
 ; CHECK-NEXT:call void @llvm.assume(i1 [[CMP]])
 ; CHECK-NEXT:br label [[FOR_COND1]]
 ;
@@ -852,15 +852,15 @@ entry:
   br label %for.cond1
 
 for.cond1:
-  %v = load i16, i16* @f.b, align 1
-  %cmp = icmp slt i16 %v, 1
+  %v = load i8, i8* @f.b, align 1
+  %cmp = icmp slt i8 %v, 1
   br i1 %cmp, label %for.body, label %for.end
 
 for.body:
   br label %for.cond1
 
 for.end:
-  %tobool = icmp ne i16 %v, 0
+  %tobool = icmp ne i8 %v, 0
   br i1 %tobool, label %if.then, label %if.end
 
 if.then:
@@ -870,8 +870,8 @@ if.end:
   br label %for

[llvm-branch-commits] [llvm] 1742203 - [SimplifyCFG] FoldBranchToCommonDest(): re-lift restrictions on liveout uses of bonus instructions

2021-01-22 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-23T01:29:05+03:00
New Revision: 17422038442c9e2b572c7324b5a22d32e7fd9b83

URL: 
https://github.com/llvm/llvm-project/commit/17422038442c9e2b572c7324b5a22d32e7fd9b83
DIFF: 
https://github.com/llvm/llvm-project/commit/17422038442c9e2b572c7324b5a22d32e7fd9b83.diff

LOG: [SimplifyCFG] FoldBranchToCommonDest(): re-lift restrictions on liveout 
uses of bonus instructions

I have previously tried doing that in
b33fbbaa34f0fe9fb16789afc72ae424c1825b69 / 
d38205144febf4dc42c9270c6aa3d978f1ef65e1,
but eventually it was pointed out that the approach taken there
was just broken wrt how the uses of bonus instructions are updated
to account for the fact that they should now use either bonus instruction
or the cloned bonus instruction. In particluar, all that manual handling
of PHI nodes in successors was just wrong.

But, the fix is actually much much simpler than my initial approach:
just tell SSAUpdate about both instances of bonus instruction,
and let it deal with all the PHI handling.

Alive2 confirms that the reproducers from the original bugs (@pr48450*)
are now handled correctly.

This effectively reverts commit 59560e85897afc50090b6c3d920bacfd28b49d06,
effectively relanding b33fbbaa34f0fe9fb16789afc72ae424c1825b69.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/CodeGen/Thumb2/mve-float16regloops.ll
llvm/test/CodeGen/Thumb2/mve-float32regloops.ll
llvm/test/CodeGen/Thumb2/mve-postinc-lsr.ll
llvm/test/Transforms/LoopUnroll/peel-loop-inner.ll
llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 510387bbf9ef..bad8d90cda65 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2916,39 +2916,16 @@ static bool PerformBranchToCommonDestFolding(BranchInst 
*BI, BranchInst *PBI,
 PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
 NewBonusInst->takeName(&BonusInst);
 BonusInst.setName(NewBonusInst->getName() + ".old");
-BonusInst.replaceUsesWithIf(
-NewBonusInst, [BB, BI, UniqueSucc, PredBlock](Use &U) {
-  auto *User = cast(U.getUser());
-  // Ignore non-external uses of bonus instructions.
-  if (User->getParent() == BB) {
-assert(!isa(User) &&
-   "Non-external users are never PHI instructions.");
-return false;
-  }
-  if (User->getParent() == PredBlock) {
-// The "exteral" use is in the block into which we just cloned the
-// bonus instruction. This means two things: 1. we are in an
-// unreachable block 2. the instruction is self-referencing.
-// So let's just rewrite it...
-return true;
-  }
-  (void)BI;
-  assert(isa(User) && "All external users must be PHI's.");
-  auto *PN = cast(User);
-  assert(is_contained(successors(BB), User->getParent()) &&
- "All external users must be in successors of BB.");
-  assert((PN->getIncomingBlock(U) == BB ||
-  PN->getIncomingBlock(U) == PredBlock) &&
- "The incoming block for that incoming value external use "
- "must be either the original block with bonus instructions, "
- "or the new predecessor block.");
-  // UniqueSucc is the block for which we change it's predecessors,
-  // so it is the only block in which we'll need to update PHI nodes.
-  if (User->getParent() != UniqueSucc)
-return false;
-  // Update the incoming value for the new predecessor.
-  return PN->getIncomingBlock(U) == PredBlock;
-});
+
+// Update (liveout) uses of bonus instructions,
+// now that the bonus instruction has been cloned into predecessor.
+SSAUpdater SSAUpdate;
+SSAUpdate.Initialize(BonusInst.getType(),
+ (NewBonusInst->getName() + ".merge").str());
+SSAUpdate.AddAvailableValue(BB, &BonusInst);
+SSAUpdate.AddAvailableValue(PredBlock, NewBonusInst);
+for (Use &U : make_early_inc_range(BonusInst.uses()))
+  SSAUpdate.RewriteUseAfterInsertions(U);
   }
 
   // Now that the Cond was cloned into the predecessor basic block,
@@ -3025,22 +3002,6 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
   return Changed;
   }
 
-  // Also, for now, all liveout uses of bonus instructions must be in PHI nodes
-  // in successor blocks as incoming values from the bonus instructions's 
block,
-  // otherwise we'll fail to update them.
-  // FIXME: We could lift this restriction, but we need to form PHI nodes and
-  // rewrite offending uses, but we can't do that without having a domtree.
-  if (any_of(*BB, [BB](I

[llvm-branch-commits] [llvm] ef93f7a - [SimplifyCFG] FoldBranchToCommonDest: gracefully handle unreachable code ()

2020-12-28 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-28T23:31:19+03:00
New Revision: ef93f7a11c347534ac768ec8bbbed64cd20c41d2

URL: 
https://github.com/llvm/llvm-project/commit/ef93f7a11c347534ac768ec8bbbed64cd20c41d2
DIFF: 
https://github.com/llvm/llvm-project/commit/ef93f7a11c347534ac768ec8bbbed64cd20c41d2.diff

LOG: [SimplifyCFG] FoldBranchToCommonDest: gracefully handle unreachable code ()

We might be dealing with an unreachable code,
so the bonus instruction we clone might be self-referencing.

There is a sanity check that all uses of bonus instructions
that are not in the original block with said bonus instructions
are PHI nodes, and that is obviously not the case
for self-referencing instructions..

So if we find such an use, just rewrite it.

Thanks to Mikael Holmén for the reproducer!

Fixes https://bugs.llvm.org/show_bug.cgi?id=48450#c8

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 67e0d2ac9cd7..7d5f6daba7b2 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2986,6 +2986,13 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
  "Non-external users are never PHI instructions.");
   return false;
 }
+if (User->getParent() == PredBlock) {
+  // The "exteral" use is in the block into which we just cloned 
the
+  // bonus instruction. This means two things: 1. we are in an
+  // unreachable block 2. the instruction is self-referencing.
+  // So let's just rewrite it...
+  return true;
+}
 (void)BI;
 assert(isa(User) && "All external users must be PHI's.");
 auto *PN = cast(User);

diff  --git a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll 
b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
index 3a70241fed16..7455cc691cf9 100644
--- a/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
+++ b/llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest.ll
@@ -836,3 +836,57 @@ for.bodythread-pre-split.loopback:
 if.end.loopexit:
   ret void
 }
+
+@f.b = external global i16, align 1
+define void @pr48450_3() {
+; CHECK-LABEL: @pr48450_3(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:br label [[FOR_COND1:%.*]]
+; CHECK:   for.cond1:
+; CHECK-NEXT:[[V:%.*]] = load i16, i16* @f.b, align 1
+; CHECK-NEXT:[[CMP:%.*]] = icmp slt i16 [[V]], 1
+; CHECK-NEXT:call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT:br label [[FOR_COND1]]
+;
+entry:
+  br label %for.cond1
+
+for.cond1:
+  %v = load i16, i16* @f.b, align 1
+  %cmp = icmp slt i16 %v, 1
+  br i1 %cmp, label %for.body, label %for.end
+
+for.body:
+  br label %for.cond1
+
+for.end:
+  %tobool = icmp ne i16 %v, 0
+  br i1 %tobool, label %if.then, label %if.end
+
+if.then:
+  unreachable
+
+if.end:
+  br label %for.cond2
+
+for.cond2:
+  %c.0 = phi i16 [ undef, %if.end ], [ %inc, %if.end7 ]
+  %cmp3 = icmp slt i16 %c.0, 1
+  br i1 %cmp3, label %for.body4, label %for.cond.cleanup
+
+for.cond.cleanup:
+  br label %cleanup
+
+for.body4:
+  br i1 undef, label %if.then6, label %if.end7
+
+if.then6:
+  br label %cleanup
+
+if.end7:
+  %inc = add nsw i16 %c.0, 1
+  br label %for.cond2
+
+cleanup:
+  unreachable
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 374ef57 - [InstCombine] 'hoist xor-by-constant from xor-by-value': completely give up on constant exprs

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-29T16:28:18+03:00
New Revision: 374ef57f1379d3d3bbe5bfb19f1d2ea7e79b6db9

URL: 
https://github.com/llvm/llvm-project/commit/374ef57f1379d3d3bbe5bfb19f1d2ea7e79b6db9
DIFF: 
https://github.com/llvm/llvm-project/commit/374ef57f1379d3d3bbe5bfb19f1d2ea7e79b6db9.diff

LOG: [InstCombine] 'hoist xor-by-constant from xor-by-value': completely give 
up on constant exprs

As Mikael Holmén is noting in the post-commit review for the first fix
https://reviews.llvm.org/rGd4ccef38d0bb#967466
not hoisting constantexprs is not enough,
because if the xor originally was a constantexpr (i.e. X is a constantexpr).
`SimplifyAssociativeOrCommutative()` in `visitXor()` will immediately
undo this transform, thus again causing an infinite combine loop.

This transform has resulted in a surprising number of constantexpr failures.

Added: 


Modified: 
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/test/Transforms/InstCombine/hoist-xor-by-constant-from-xor-by-value.ll

Removed: 




diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index c0823c950368..15dcf2d19c15 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3459,9 +3459,11 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator 
&I) {
 
   // Otherwise, if all else failed, try to hoist the xor-by-constant:
   //   (X ^ C) ^ Y --> (X ^ Y) ^ C
-  // Just like we do in other places, we avoid the fold for constantexprs,
-  // at least to avoid endless combine loop.
-  if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(X), m_ImmConstant(C1))),
+  // Just like we do in other places, we completely avoid the fold
+  // for constantexprs, at least to avoid endless combine loop.
+  if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_CombineAnd(m_Value(X),
+
m_Unless(m_ConstantExpr())),
+   m_ImmConstant(C1))),
 m_Value(Y
 return BinaryOperator::CreateXor(Builder.CreateXor(X, Y), C1);
 

diff  --git 
a/llvm/test/Transforms/InstCombine/hoist-xor-by-constant-from-xor-by-value.ll 
b/llvm/test/Transforms/InstCombine/hoist-xor-by-constant-from-xor-by-value.ll
index 14227d304df7..46bdb1a6092e 100644
--- 
a/llvm/test/Transforms/InstCombine/hoist-xor-by-constant-from-xor-by-value.ll
+++ 
b/llvm/test/Transforms/InstCombine/hoist-xor-by-constant-from-xor-by-value.ll
@@ -87,3 +87,23 @@ entry:
   %r = xor i8 %or, xor (i8 xor (i8 ptrtoint (i32* @global_constant to i8), i8 
-1), i8 xor (i8 ptrtoint (i32* @global_constant2 to i8), i8 -1))
   ret i8 %r
 }
+
+@global_constant3 = external global [6 x [1 x i64]], align 1
+@global_constant4 = external global i64, align 1
+@global_constant5 = external global i16*, align 1
+
+define i16 @constantexpr2() {
+; CHECK-LABEL: @constantexpr2(
+; CHECK-NEXT:[[I2:%.*]] = load i16*, i16** @global_constant5, align 1
+; CHECK-NEXT:[[I3:%.*]] = load i16, i16* [[I2]], align 1
+; CHECK-NEXT:[[I5:%.*]] = xor i16 [[I3]], xor (i16 zext (i1 icmp ne (i64* 
getelementptr inbounds ([6 x [1 x i64]], [6 x [1 x i64]]* @global_constant3, 
i64 0, i64 5, i64 0), i64* @global_constant4) to i16), i16 -1)
+; CHECK-NEXT:ret i16 [[I5]]
+;
+  %i0 = icmp ne i64* getelementptr inbounds ([6 x [1 x i64]], [6 x [1 x i64]]* 
@global_constant3, i16 0, i16 5, i16 0), @global_constant4
+  %i1 = zext i1 %i0 to i16
+  %i2 = load i16*, i16** @global_constant5, align 1
+  %i3 = load i16, i16* %i2, align 1
+  %i4 = xor i16 %i3, %i1
+  %i5 = xor i16 %i4, -1
+  ret i16 %i5
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 6027e05 - [SimplifyCFG] Teach SimplifyEqualityComparisonWithOnlyPredecessor() to preserve DomTree, part 1

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:10+03:00
New Revision: 6027e05dbfc59a780bf6fc42d5a4f80a59878be1

URL: 
https://github.com/llvm/llvm-project/commit/6027e05dbfc59a780bf6fc42d5a4f80a59878be1
DIFF: 
https://github.com/llvm/llvm-project/commit/6027e05dbfc59a780bf6fc42d5a4f80a59878be1.diff

LOG: [SimplifyCFG] Teach SimplifyEqualityComparisonWithOnlyPredecessor() to 
preserve DomTree, part 1

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/2005-06-16-PHICrash.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 7d5f6daba7b2..7b08b95356fb 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -910,13 +910,18 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
   (void)NI;
 
   // Remove PHI node entries for the dead edge.
-  ThisCases[0].Dest->removePredecessor(TI->getParent());
+  ThisCases[0].Dest->removePredecessor(PredDef);
 
   LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
 << "Through successor TI: " << *TI << "Leaving: " << 
*NI
 << "\n");
 
   EraseTerminatorAndDCECond(TI);
+
+  if (DTU)
+DTU->applyUpdatesPermissive(
+{{DominatorTree::Delete, PredDef, ThisCases[0].Dest}});
+
   return true;
 }
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/2005-06-16-PHICrash.ll 
b/llvm/test/Transforms/SimplifyCFG/2005-06-16-PHICrash.ll
index 8fd1fae34503..848aec7f6ad0 100644
--- a/llvm/test/Transforms/SimplifyCFG/2005-06-16-PHICrash.ll
+++ b/llvm/test/Transforms/SimplifyCFG/2005-06-16-PHICrash.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -disable-output
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-disable-output
 ; PR584
 @g_38098584 = external global i32  ;  [#uses=1]
 @g_60187400 = external global i32  ;  [#uses=1]



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] fe9bdd9 - [SimplifyCFG] Teach SimplifyEqualityComparisonWithOnlyPredecessor() to preserve DomTree, part 2

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:10+03:00
New Revision: fe9bdd962152d6b3d1203f2b6129df122c462599

URL: 
https://github.com/llvm/llvm-project/commit/fe9bdd962152d6b3d1203f2b6129df122c462599
DIFF: 
https://github.com/llvm/llvm-project/commit/fe9bdd962152d6b3d1203f2b6129df122c462599.diff

LOG: [SimplifyCFG] Teach SimplifyEqualityComparisonWithOnlyPredecessor() to 
preserve DomTree, part 2

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/InfLoop.ll
llvm/test/Transforms/SimplifyCFG/implied-cond-matching-imm.ll
llvm/test/Transforms/SimplifyCFG/iterative-simplify.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 7b08b95356fb..41af29b82791 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -970,12 +970,15 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
   if (!TheRealDest)
 TheRealDest = ThisDef;
 
+  SmallVector Updates;
+
   // Remove PHI node entries for dead edges.
   BasicBlock *CheckEdge = TheRealDest;
   for (BasicBlock *Succ : successors(TIBB))
-if (Succ != CheckEdge)
+if (Succ != CheckEdge) {
   Succ->removePredecessor(TIBB);
-else
+  Updates.push_back({DominatorTree::Delete, TIBB, Succ});
+} else
   CheckEdge = nullptr;
 
   // Insert the new branch.
@@ -987,6 +990,8 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
 << "\n");
 
   EraseTerminatorAndDCECond(TI);
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
   return true;
 }
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/InfLoop.ll 
b/llvm/test/Transforms/SimplifyCFG/InfLoop.ll
index a56076e42ce1..32c6b0353286 100644
--- a/llvm/test/Transforms/SimplifyCFG/InfLoop.ll
+++ b/llvm/test/Transforms/SimplifyCFG/InfLoop.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -disable-output
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-disable-output
 ; END.
 
 target datalayout = 
"e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"

diff  --git a/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-imm.ll 
b/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-imm.ll
index 60ef81365982..d7f5694ad737 100644
--- a/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-imm.ll
+++ b/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-imm.ll
@@ -1,4 +1,4 @@
-; RUN: opt %s -S -simplifycfg | FileCheck %s
+; RUN: opt %s -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 | 
FileCheck %s
 
 ; cmp1 implies cmp2 is false
 ; CHECK-LABEL: @test1

diff  --git a/llvm/test/Transforms/SimplifyCFG/iterative-simplify.ll 
b/llvm/test/Transforms/SimplifyCFG/iterative-simplify.ll
index 60728b9a9578..ef2482134e7d 100644
--- a/llvm/test/Transforms/SimplifyCFG/iterative-simplify.ll
+++ b/llvm/test/Transforms/SimplifyCFG/iterative-simplify.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -S | not grep bb17
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
not grep bb17
 ; PR1786
 
 define i32 @main() {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 18c407b - [SimplifyCFG] Teach HoistThenElseCodeToIf() to preserve DomTree

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:10+03:00
New Revision: 18c407bf4c16800a836abe8096a7e4c41ce46b24

URL: 
https://github.com/llvm/llvm-project/commit/18c407bf4c16800a836abe8096a7e4c41ce46b24
DIFF: 
https://github.com/llvm/llvm-project/commit/18c407bf4c16800a836abe8096a7e4c41ce46b24.diff

LOG: [SimplifyCFG] Teach HoistThenElseCodeToIf() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/GVNSink/indirect-call.ll
llvm/test/Transforms/GVNSink/sink-common-code.ll
llvm/test/Transforms/SimplifyCFG/BrUnwind.ll
llvm/test/Transforms/SimplifyCFG/HoistCode.ll
llvm/test/Transforms/SimplifyCFG/UncondBranchToReturn.ll
llvm/test/Transforms/SimplifyCFG/X86/empty-cleanuppad.ll
llvm/test/Transforms/SimplifyCFG/X86/pr39187-g.ll
llvm/test/Transforms/SimplifyCFG/hoist-common-code.ll
llvm/test/Transforms/SimplifyCFG/hoist-with-range.ll
llvm/test/Transforms/SimplifyCFG/pr39807.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 41af29b82791..6af60467cf45 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1508,11 +1508,19 @@ bool SimplifyCFGOpt::HoistThenElseCodeToIf(BranchInst 
*BI,
 }
   }
 
+  SmallVector Updates;
+
   // Update any PHI nodes in our new successors.
-  for (BasicBlock *Succ : successors(BB1))
+  for (BasicBlock *Succ : successors(BB1)) {
 AddPredecessorToBlock(Succ, BIParent, BB1);
+Updates.push_back({DominatorTree::Insert, BIParent, Succ});
+  }
+  for (BasicBlock *Succ : successors(BI))
+Updates.push_back({DominatorTree::Delete, BIParent, Succ});
 
   EraseTerminatorAndDCECond(BI);
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
   return Changed;
 }
 

diff  --git a/llvm/test/Transforms/GVNSink/indirect-call.ll 
b/llvm/test/Transforms/GVNSink/indirect-call.ll
index 66c1a4f647a9..db84fe7410fd 100644
--- a/llvm/test/Transforms/GVNSink/indirect-call.ll
+++ b/llvm/test/Transforms/GVNSink/indirect-call.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -gvn-sink -simplifycfg -hoist-common-insts=true 
-simplifycfg-sink-common=false -S | FileCheck %s
+; RUN: opt < %s -gvn-sink -simplifycfg 
-simplifycfg-require-and-preserve-domtree=1 -hoist-common-insts=true 
-simplifycfg-sink-common=false -S | FileCheck %s
 
 declare i8 @ext(i1)
 

diff  --git a/llvm/test/Transforms/GVNSink/sink-common-code.ll 
b/llvm/test/Transforms/GVNSink/sink-common-code.ll
index 4131fb37485f..d0b533b4ac57 100644
--- a/llvm/test/Transforms/GVNSink/sink-common-code.ll
+++ b/llvm/test/Transforms/GVNSink/sink-common-code.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -gvn-sink -simplifycfg -hoist-common-insts=true 
-simplifycfg-sink-common=false -S | FileCheck %s
+; RUN: opt < %s -gvn-sink -simplifycfg 
-simplifycfg-require-and-preserve-domtree=1 -hoist-common-insts=true 
-simplifycfg-sink-common=false -S | FileCheck %s
 
 define zeroext i1 @test1(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) 
{
 entry:

diff  --git a/llvm/test/Transforms/SimplifyCFG/BrUnwind.ll 
b/llvm/test/Transforms/SimplifyCFG/BrUnwind.ll
index 6cd3ad6e4b02..c40f5513cebf 100644
--- a/llvm/test/Transforms/SimplifyCFG/BrUnwind.ll
+++ b/llvm/test/Transforms/SimplifyCFG/BrUnwind.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -hoist-common-insts=true -S | \
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-hoist-common-insts=true -S | \
 ; RUN: not grep "br label"
 
 define void @test(i1 %C) {

diff  --git a/llvm/test/Transforms/SimplifyCFG/HoistCode.ll 
b/llvm/test/Transforms/SimplifyCFG/HoistCode.ll
index 975107da4928..51baa5b7f9b3 100644
--- a/llvm/test/Transforms/SimplifyCFG/HoistCode.ll
+++ b/llvm/test/Transforms/SimplifyCFG/HoistCode.ll
@@ -1,6 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -passes=simplifycfg -hoist-common-insts=true -S | FileCheck %s
-; RUN: opt < %s -simplifycfg -hoist-common-insts=true -S | FileCheck %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-hoist-common-insts=true -S | FileCheck %s
 
 define void @foo(i1 %C, i32* %P) {
 ; CHECK-LABEL: @foo(

diff  --git a/llvm/test/Transforms/SimplifyCFG/UncondBranchToReturn.ll 
b/llvm/test/Transforms/SimplifyCFG/UncondBranchToReturn.ll
index 78bcd345daf9..122368406118 100644
--- a/llvm/test/Transforms/SimplifyCFG/UncondBranchToReturn.ll
+++ b/llvm/test/Transforms/SimplifyCFG/UncondBranchToReturn.ll
@@ -2,7 +2,7 @@
 ; a PHI node and a return.  Make sure the simplify cfg can straighten out this
 ; important case.  This is basically the most trivial form of tail-duplication.
 
-; RUN: opt < %s -simplifycfg -hoist-common-insts=true -S | \
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-hoist-common-insts=true -S | \
 ; RUN:not grep "br label"
 
 define

[llvm-branch-commits] [llvm] b8121b2 - [SimplifyCFG] Teach SinkCommonCodeFromPredecessors() to preserve DomTree

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:11+03:00
New Revision: b8121b2e62d58ca42aa78b0dd644af9d096957a3

URL: 
https://github.com/llvm/llvm-project/commit/b8121b2e62d58ca42aa78b0dd644af9d096957a3
DIFF: 
https://github.com/llvm/llvm-project/commit/b8121b2e62d58ca42aa78b0dd644af9d096957a3.diff

LOG: [SimplifyCFG] Teach SinkCommonCodeFromPredecessors() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/sink-common-code.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6af60467cf45..2fcfc557393a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1812,7 +1812,8 @@ namespace {
 /// true, sink any common code from the predecessors to BB.
 /// We also allow one predecessor to end with conditional branch (but no more
 /// than one).
-static bool SinkCommonCodeFromPredecessors(BasicBlock *BB) {
+static bool SinkCommonCodeFromPredecessors(BasicBlock *BB,
+   DomTreeUpdater *DTU) {
   // We support two situations:
   //   (1) all incoming arcs are unconditional
   //   (2) one incoming arc is conditional
@@ -1930,7 +1931,8 @@ static bool SinkCommonCodeFromPredecessors(BasicBlock 
*BB) {
 LLVM_DEBUG(dbgs() << "SINK: Splitting edge\n");
 // We have a conditional edge and we're going to sink some instructions.
 // Insert a new block postdominating all blocks we're going to sink from.
-if (!SplitBlockPredecessors(BB, UnconditionalPreds, ".sink.split"))
+if (!SplitBlockPredecessors(BB, UnconditionalPreds, ".sink.split",
+DTU ? &DTU->getDomTree() : nullptr))
   // Edges couldn't be split.
   return false;
 Changed = true;
@@ -6461,7 +6463,7 @@ bool SimplifyCFGOpt::simplifyOnceImpl(BasicBlock *BB) {
 return true;
 
   if (SinkCommon && Options.SinkCommonInsts)
-Changed |= SinkCommonCodeFromPredecessors(BB);
+Changed |= SinkCommonCodeFromPredecessors(BB, DTU);
 
   IRBuilder<> Builder(BB);
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/sink-common-code.ll 
b/llvm/test/Transforms/SimplifyCFG/sink-common-code.ll
index 00329ae5d640..48dd2d92f4c3 100644
--- a/llvm/test/Transforms/SimplifyCFG/sink-common-code.ll
+++ b/llvm/test/Transforms/SimplifyCFG/sink-common-code.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -sink-common-insts -S | FileCheck 
-enable-var-scope %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-sink-common-insts -S | FileCheck -enable-var-scope %s
 ; RUN: opt < %s -passes='simplify-cfg' -S | FileCheck 
-enable-var-scope %s
 
 define zeroext i1 @test1(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) 
{



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] d4c0abb - [SimplifyCFG] Teach FoldCondBranchOnPHI() to preserve DomTree

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:11+03:00
New Revision: d4c0abb4a31a9e6a8fccb656d897f7140781022c

URL: 
https://github.com/llvm/llvm-project/commit/d4c0abb4a31a9e6a8fccb656d897f7140781022c
DIFF: 
https://github.com/llvm/llvm-project/commit/d4c0abb4a31a9e6a8fccb656d897f7140781022c.diff

LOG: [SimplifyCFG] Teach FoldCondBranchOnPHI() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/LoopVectorize/if-pred-stores.ll
llvm/test/Transforms/SimplifyCFG/2004-12-10-SimplifyCFGCrash.ll
llvm/test/Transforms/SimplifyCFG/X86/critedge-assume.ll
llvm/test/Transforms/SimplifyCFG/branch-phi-thread.ll
llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll
llvm/test/Transforms/SimplifyCFG/pr46638.ll
llvm/test/Transforms/SimplifyCFG/unprofitable-pr.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 2fcfc557393a..95a5de09c18f 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2344,8 +2344,8 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock 
*BB) {
 /// If we have a conditional branch on a PHI node value that is defined in the
 /// same block as the branch and if any PHI entries are constants, thread edges
 /// corresponding to that entry to be branches to their ultimate destination.
-static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
-AssumptionCache *AC) {
+static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
+const DataLayout &DL, AssumptionCache *AC) {
   BasicBlock *BB = BI->getParent();
   PHINode *PN = dyn_cast(BI->getCondition());
   // NOTE: we currently cannot transform this case if the PHI node is used
@@ -2381,6 +2381,8 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const 
DataLayout &DL,
 if (isa(PredBB->getTerminator()))
   continue;
 
+SmallVector Updates;
+
 // The dest block might have PHI nodes, other predecessors and other
 // 
diff icult cases.  Instead of being smart about this, just insert a new
 // block that jumps to the destination block, effectively splitting
@@ -2389,6 +2391,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const 
DataLayout &DL,
 BasicBlock::Create(BB->getContext(), RealDest->getName() + ".critedge",
RealDest->getParent(), RealDest);
 BranchInst *CritEdgeBranch = BranchInst::Create(RealDest, EdgeBB);
+Updates.push_back({DominatorTree::Insert, EdgeBB, RealDest});
 CritEdgeBranch->setDebugLoc(BI->getDebugLoc());
 
 // Update PHI nodes.
@@ -2447,8 +2450,14 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const 
DataLayout &DL,
 PredBBTI->setSuccessor(i, EdgeBB);
   }
 
+Updates.push_back({DominatorTree::Delete, PredBB, BB});
+Updates.push_back({DominatorTree::Insert, PredBB, EdgeBB});
+
+if (DTU)
+  DTU->applyUpdatesPermissive(Updates);
+
 // Recurse, simplifying any other constants.
-return FoldCondBranchOnPHI(BI, DL, AC) || true;
+return FoldCondBranchOnPHI(BI, DTU, DL, AC) || true;
   }
 
   return false;
@@ -6331,7 +6340,7 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, 
IRBuilder<> &Builder) {
   // through this block if any PHI node entries are constants.
   if (PHINode *PN = dyn_cast(BI->getCondition()))
 if (PN->getParent() == BI->getParent())
-  if (FoldCondBranchOnPHI(BI, DL, Options.AC))
+  if (FoldCondBranchOnPHI(BI, DTU, DL, Options.AC))
 return requestResimplify();
 
   // Scan predecessor blocks for conditional branches.

diff  --git a/llvm/test/Transforms/LoopVectorize/if-pred-stores.ll 
b/llvm/test/Transforms/LoopVectorize/if-pred-stores.ll
index 795df0caedd6..3c8d07356632 100644
--- a/llvm/test/Transforms/LoopVectorize/if-pred-stores.ll
+++ b/llvm/test/Transforms/LoopVectorize/if-pred-stores.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=1 
-force-vector-interleave=2 -loop-vectorize -verify-loop-info -simplifycfg < %s 
| FileCheck %s --check-prefix=UNROLL
+; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=1 
-force-vector-interleave=2 -loop-vectorize -verify-loop-info -simplifycfg 
-simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s 
--check-prefix=UNROLL
 ; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=1 
-force-vector-interleave=2 -loop-vectorize -verify-loop-info < %s | FileCheck 
%s --check-prefix=UNROLL-NOSIMPLIFY
-; RUN: opt -S -vectorize-num-stores-pred=1 -force-vector-width=2 
-force-vector-interleave=1 -loop-vectorize -verify-loop-info -simplifycfg < %s 
| FileCheck %s --check-prefix=VEC
+; RUN: opt -S -vectorize-num-stores-pred=1 -force

[llvm-branch-commits] [llvm] 3071562 - [SimplifyCFG] Teach mergeConditionalStoreToAddress() to preserve DomTree

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:11+03:00
New Revision: 307156246f7d8a1ff5293b97414f5ba7f19298b7

URL: 
https://github.com/llvm/llvm-project/commit/307156246f7d8a1ff5293b97414f5ba7f19298b7
DIFF: 
https://github.com/llvm/llvm-project/commit/307156246f7d8a1ff5293b97414f5ba7f19298b7.diff

LOG: [SimplifyCFG] Teach mergeConditionalStoreToAddress() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/merge-cond-stores-2.ll
llvm/test/Transforms/SimplifyCFG/merge-cond-stores.ll
llvm/test/Transforms/SimplifyCFG/preserve-store-alignment.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 95a5de09c18f..03626abc3faf 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3238,12 +3238,10 @@ static Value *ensureValueAvailableInSuccessor(Value *V, 
BasicBlock *BB,
   return PHI;
 }
 
-static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB,
-   BasicBlock *QTB, BasicBlock *QFB,
-   BasicBlock *PostBB, Value *Address,
-   bool InvertPCond, bool InvertQCond,
-   const DataLayout &DL,
-   const TargetTransformInfo &TTI) {
+static bool mergeConditionalStoreToAddress(
+BasicBlock *PTB, BasicBlock *PFB, BasicBlock *QTB, BasicBlock *QFB,
+BasicBlock *PostBB, Value *Address, bool InvertPCond, bool InvertQCond,
+DomTreeUpdater *DTU, const DataLayout &DL, const TargetTransformInfo &TTI) 
{
   // For every pointer, there must be exactly two stores, one coming from
   // PTB or PFB, and the other from QTB or QFB. We don't support more than one
   // store (to any address) in PTB,PFB or QTB,QFB.
@@ -3332,8 +3330,9 @@ static bool mergeConditionalStoreToAddress(BasicBlock 
*PTB, BasicBlock *PFB,
 // If QTB does not exist, then QFB's only predecessor has a conditional
 // branch to QFB and PostBB.
 BasicBlock *TruePred = QTB ? QTB : QFB->getSinglePredecessor();
-BasicBlock *NewBB = SplitBlockPredecessors(PostBB, { QFB, TruePred},
-   "condstore.split");
+BasicBlock *NewBB =
+SplitBlockPredecessors(PostBB, {QFB, TruePred}, "condstore.split",
+   DTU ? &DTU->getDomTree() : nullptr);
 if (!NewBB)
   return false;
 PostBB = NewBB;
@@ -3362,8 +3361,9 @@ static bool mergeConditionalStoreToAddress(BasicBlock 
*PTB, BasicBlock *PFB,
 QPred = QB.CreateNot(QPred);
   Value *CombinedPred = QB.CreateOr(PPred, QPred);
 
-  auto *T =
-  SplitBlockAndInsertIfThen(CombinedPred, &*QB.GetInsertPoint(), false);
+  auto *T = SplitBlockAndInsertIfThen(
+  CombinedPred, &*QB.GetInsertPoint(), /*Unreachable=*/false,
+  /*BranchWeights=*/nullptr, DTU ? &DTU->getDomTree() : nullptr);
   QB.SetInsertPoint(T);
   StoreInst *SI = cast(QB.CreateStore(QPHI, Address));
   AAMDNodes AAMD;
@@ -3383,7 +3383,7 @@ static bool mergeConditionalStoreToAddress(BasicBlock 
*PTB, BasicBlock *PFB,
 }
 
 static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI,
-   const DataLayout &DL,
+   DomTreeUpdater *DTU, const DataLayout &DL,
const TargetTransformInfo &TTI) {
   // The intention here is to find diamonds or triangles (see below) where each
   // conditional block contains a store to the same address. Both of these
@@ -3485,12 +3485,12 @@ static bool mergeConditionalStores(BranchInst *PBI, 
BranchInst *QBI,
 
   bool Changed = false;
   for (auto *Address : CommonAddresses)
-Changed |= mergeConditionalStoreToAddress(
-PTB, PFB, QTB, QFB, PostBB, Address, InvertPCond, InvertQCond, DL, 
TTI);
+Changed |=
+mergeConditionalStoreToAddress(PTB, PFB, QTB, QFB, PostBB, Address,
+   InvertPCond, InvertQCond, DTU, DL, TTI);
   return Changed;
 }
 
-
 /// If the previous block ended with a widenable branch, determine if reusing
 /// the target block is profitable and legal.  This will have the effect of
 /// "widening" PBI, but doesn't require us to reason about hosting safety.
@@ -3536,6 +3536,7 @@ static bool tryWidenCondBranchToCondBranch(BranchInst 
*PBI, BranchInst *BI) {
 /// that PBI and BI are both conditional branches, and BI is in one of the
 /// successor blocks of PBI - PBI branches to BI.
 static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
+   DomTreeUpdater *DTU,
const DataLayout &DL,
const TargetTran

[llvm-branch-commits] [llvm] ec0b671 - [SimplifyCFG] Teach SimplifyCondBranchToCondBranch() to preserve DomTree

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:12+03:00
New Revision: ec0b671a6147f669a0ab155000b1b5f19b214b7d

URL: 
https://github.com/llvm/llvm-project/commit/ec0b671a6147f669a0ab155000b1b5f19b214b7d
DIFF: 
https://github.com/llvm/llvm-project/commit/ec0b671a6147f669a0ab155000b1b5f19b214b7d.diff

LOG: [SimplifyCFG] Teach SimplifyCondBranchToCondBranch() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
llvm/test/Transforms/SimplifyCFG/extract-cost.ll
llvm/test/Transforms/SimplifyCFG/pr34131.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 03626abc3faf..d1851da69138 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3668,6 +3668,8 @@ static bool SimplifyCondBranchToCondBranch(BranchInst 
*PBI, BranchInst *BI,
   LLVM_DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
 << "AND: " << *BI->getParent());
 
+  SmallVector Updates;
+
   // If OtherDest *is* BB, then BB is a basic block with a single conditional
   // branch in it, where one edge (OtherDest) goes back to itself but the other
   // exits.  We don't *know* that the program avoids the infinite loop
@@ -3681,6 +3683,7 @@ static bool SimplifyCondBranchToCondBranch(BranchInst 
*PBI, BranchInst *BI,
 BasicBlock *InfLoopBlock =
 BasicBlock::Create(BB->getContext(), "infloop", BB->getParent());
 BranchInst::Create(InfLoopBlock, InfLoopBlock);
+Updates.push_back({DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
 OtherDest = InfLoopBlock;
   }
 
@@ -3702,11 +3705,20 @@ static bool SimplifyCondBranchToCondBranch(BranchInst 
*PBI, BranchInst *BI,
   // Merge the conditions.
   Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
 
+  for (auto *Successor : successors(PBI->getParent()))
+Updates.push_back({DominatorTree::Delete, PBI->getParent(), Successor});
+
   // Modify PBI to branch on the new condition to the new dests.
   PBI->setCondition(Cond);
   PBI->setSuccessor(0, CommonDest);
   PBI->setSuccessor(1, OtherDest);
 
+  for (auto *Successor : successors(PBI->getParent()))
+Updates.push_back({DominatorTree::Insert, PBI->getParent(), Successor});
+
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
+
   // Update branch weight for PBI.
   uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
   uint64_t PredCommon, PredOther, SuccCommon, SuccOther;

diff  --git a/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll 
b/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
index dee2e9b3294f..037428b7791e 100644
--- a/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
+++ b/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -simplifycfg -S | FileCheck %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 ; PR2540
 ; Outval should end up with a select from 0/2, not all constants.
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/extract-cost.ll 
b/llvm/test/Transforms/SimplifyCFG/extract-cost.ll
index 0a544f585cd1..190ceba0ea95 100644
--- a/llvm/test/Transforms/SimplifyCFG/extract-cost.ll
+++ b/llvm/test/Transforms/SimplifyCFG/extract-cost.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -simplifycfg -S  < %s | FileCheck %s
+; RUN: opt -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S  < %s | 
FileCheck %s
 
 declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32) #1
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/pr34131.ll 
b/llvm/test/Transforms/SimplifyCFG/pr34131.ll
index b64b6876e04e..ddd1f538e8bd 100644
--- a/llvm/test/Transforms/SimplifyCFG/pr34131.ll
+++ b/llvm/test/Transforms/SimplifyCFG/pr34131.ll
@@ -1,4 +1,4 @@
-; RUN: opt -simplifycfg -S < %s | FileCheck %s
+; RUN: opt -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | 
FileCheck %s
 
 ; Just checking for lack of crash here, but we should be able to check the IR?
 ; Earlier version using auto-generated checks from utils/update_test_checks.py



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 39a56f7 - [SimplifyCFG] Teach SimplifyTerminatorOnSelect() to preserve DomTree

2020-12-29 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T00:48:12+03:00
New Revision: 39a56f7f1722b1e917a3bd5c829ec1d7effd5a11

URL: 
https://github.com/llvm/llvm-project/commit/39a56f7f1722b1e917a3bd5c829ec1d7effd5a11
DIFF: 
https://github.com/llvm/llvm-project/commit/39a56f7f1722b1e917a3bd5c829ec1d7effd5a11.diff

LOG: [SimplifyCFG] Teach SimplifyTerminatorOnSelect() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch-on-const-select.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d1851da69138..24572810be70 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3805,6 +3805,8 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
   BasicBlock *KeepEdge1 = TrueBB;
   BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
 
+  SmallVector Updates;
+
   // Then remove the rest.
   for (BasicBlock *Succ : successors(OldTerm)) {
 // Make sure only to keep exactly one copy of each edge.
@@ -3812,9 +3814,11 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
   KeepEdge1 = nullptr;
 else if (Succ == KeepEdge2)
   KeepEdge2 = nullptr;
-else
+else {
   Succ->removePredecessor(OldTerm->getParent(),
   /*KeepOneInputPHIs=*/true);
+  Updates.push_back({DominatorTree::Delete, OldTerm->getParent(), Succ});
+}
   }
 
   IRBuilder<> Builder(OldTerm);
@@ -3822,14 +3826,17 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 
   // Insert an appropriate new terminator.
   if (!KeepEdge1 && !KeepEdge2) {
-if (TrueBB == FalseBB)
+if (TrueBB == FalseBB) {
   // We were only looking for one successor, and it was present.
   // Create an unconditional branch to it.
   Builder.CreateBr(TrueBB);
-else {
+  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), TrueBB});
+} else {
   // We found both of the successors we were looking for.
   // Create a conditional branch sharing the condition of the select.
   BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
+  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), TrueBB});
+  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), 
FalseBB});
   if (TrueWeight != FalseWeight)
 setBranchWeights(NewBI, TrueWeight, FalseWeight);
 }
@@ -3841,15 +3848,20 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 // One of the selected values was a successor, but the other wasn't.
 // Insert an unconditional branch to the one that was found;
 // the edge to the one that wasn't must be unreachable.
-if (!KeepEdge1)
+if (!KeepEdge1) {
   // Only TrueBB was found.
   Builder.CreateBr(TrueBB);
-else
+  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), TrueBB});
+} else {
   // Only FalseBB was found.
   Builder.CreateBr(FalseBB);
+  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), 
FalseBB});
+}
   }
 
   EraseTerminatorAndDCECond(OldTerm);
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
   return true;
 }
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-on-const-select.ll 
b/llvm/test/Transforms/SimplifyCFG/switch-on-const-select.ll
index 98c434a5a0ec..013096625b1c 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-on-const-select.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-on-const-select.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -simplifycfg -S | FileCheck -enable-var-scope %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck -enable-var-scope %s
 
 ; Test basic folding to a conditional branch.
 define i32 @foo(i64 %x, i64 %y) nounwind {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] c45f765 - [SimplifyCFG] Teach SimplifyBranchOnICmpChain() to preserve DomTree

2020-12-30 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T23:58:40+03:00
New Revision: c45f765c0d41ad94b857ee4b7e007d58b41ed650

URL: 
https://github.com/llvm/llvm-project/commit/c45f765c0d41ad94b857ee4b7e007d58b41ed650
DIFF: 
https://github.com/llvm/llvm-project/commit/c45f765c0d41ad94b857ee4b7e007d58b41ed650.diff

LOG: [SimplifyCFG] Teach SimplifyBranchOnICmpChain() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch_msan.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 5b60faee751a..c730c1427ec8 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4101,12 +4101,16 @@ bool 
SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst *BI,
 << " cases into SWITCH.  BB is:\n"
 << *BB);
 
+  SmallVector Updates;
+
   // If there are any extra values that couldn't be folded into the switch
   // then we evaluate them with an explicit branch first. Split the block
   // right before the condbr to handle it.
   if (ExtraCase) {
 BasicBlock *NewBB =
-BB->splitBasicBlock(BI->getIterator(), "switch.early.test");
+SplitBlock(BB, BI, DTU ? &DTU->getDomTree() : nullptr, /*LI=*/nullptr,
+   /*MSSAU=*/nullptr, "switch.early.test");
+
 // Remove the uncond branch added to the old block.
 Instruction *OldTI = BB->getTerminator();
 Builder.SetInsertPoint(OldTI);
@@ -4118,6 +4122,8 @@ bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst 
*BI,
 
 OldTI->eraseFromParent();
 
+Updates.push_back({DominatorTree::Insert, BB, EdgeBB});
+
 // If there are PHI nodes in EdgeBB, then we need to add a new entry to 
them
 // for the edge we just added.
 AddPredecessorToBlock(EdgeBB, BB, NewBB);
@@ -4144,6 +4150,7 @@ bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst 
*BI,
   // We added edges from PI to the EdgeBB.  As such, if there were any
   // PHI nodes in EdgeBB, they need entries to be added corresponding to
   // the number of edges added.
+  Updates.push_back({DominatorTree::Insert, BB, EdgeBB});
   for (BasicBlock::iterator BBI = EdgeBB->begin(); isa(BBI); ++BBI) {
 PHINode *PN = cast(BBI);
 Value *InVal = PN->getIncomingValueForBlock(BB);
@@ -4153,6 +4160,8 @@ bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst 
*BI,
 
   // Erase the old branch instruction.
   EraseTerminatorAndDCECond(BI);
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
 
   LLVM_DEBUG(dbgs() << "  ** 'icmp' chain result is:\n" << *BB << '\n');
   return true;

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch_msan.ll 
b/llvm/test/Transforms/SimplifyCFG/switch_msan.ll
index 96e798289772..a59515ed463a 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_msan.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_msan.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -simplifycfg < %s | FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 declare i8 @next_char();
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 7f221c9 - [SimplifyCFG] Teach SwitchToLookupTable() to preserve DomTree

2020-12-30 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T23:58:41+03:00
New Revision: 7f221c9196de2f042030e2a31f81089889d705bd

URL: 
https://github.com/llvm/llvm-project/commit/7f221c9196de2f042030e2a31f81089889d705bd
DIFF: 
https://github.com/llvm/llvm-project/commit/7f221c9196de2f042030e2a31f81089889d705bd.diff

LOG: [SimplifyCFG] Teach SwitchToLookupTable() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/X86/CoveredLookupTable.ll
llvm/test/Transforms/SimplifyCFG/X86/disable-lookup-table.ll
llvm/test/Transforms/SimplifyCFG/X86/switch-covered-bug.ll
llvm/test/Transforms/SimplifyCFG/X86/switch-table-bug.ll
llvm/test/Transforms/SimplifyCFG/rangereduce.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 2b631175f55e..7e49d3a1524c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5716,7 +5716,7 @@ static void reuseTableCompare(
 /// successor block with 
diff erent constant values, replace the switch with
 /// lookup tables.
 static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
-const DataLayout &DL,
+DomTreeUpdater *DTU, const DataLayout &DL,
 const TargetTransformInfo &TTI) {
   assert(SI->getNumCases() > 1 && "Degenerate switch?");
 
@@ -5814,6 +5814,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
   if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
 return false;
 
+  std::vector Updates;
+
   // Create the BB that does the lookups.
   Module &Mod = *CommonDest->getParent()->getParent();
   BasicBlock *LookupBB = BasicBlock::Create(
@@ -5846,6 +5848,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 
   if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
 Builder.CreateBr(LookupBB);
+Updates.push_back({DominatorTree::Insert, SI->getParent(), LookupBB});
 // Note: We call removeProdecessor later since we need to be able to get 
the
 // PHI value for the default case in case we're using a bit mask.
   } else {
@@ -5853,6 +5856,9 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
 RangeCheckBranch =
 Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
+Updates.push_back({DominatorTree::Insert, SI->getParent(), LookupBB});
+Updates.push_back(
+{DominatorTree::Insert, SI->getParent(), SI->getDefaultDest()});
   }
 
   // Populate the BB that does the lookups.
@@ -5890,7 +5896,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 Value *LoBit = Builder.CreateTrunc(
 Shifted, Type::getInt1Ty(Mod.getContext()), "switch.lobit");
 Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
-
+Updates.push_back({DominatorTree::Insert, MaskBB, LookupBB});
+Updates.push_back({DominatorTree::Insert, MaskBB, SI->getDefaultDest()});
 Builder.SetInsertPoint(LookupBB);
 AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
   }
@@ -5900,6 +5907,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 // do not delete PHINodes here.
 SI->getDefaultDest()->removePredecessor(SI->getParent(),
 /*KeepOneInputPHIs=*/true);
+Updates.push_back(
+{DominatorTree::Delete, SI->getParent(), SI->getDefaultDest()});
   }
 
   bool ReturnedEarly = false;
@@ -5936,8 +5945,10 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 PHI->addIncoming(Result, LookupBB);
   }
 
-  if (!ReturnedEarly)
+  if (!ReturnedEarly) {
 Builder.CreateBr(CommonDest);
+Updates.push_back({DominatorTree::Insert, LookupBB, CommonDest});
+  }
 
   // Remove the switch.
   for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
@@ -5946,8 +5957,11 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 if (Succ == SI->getDefaultDest())
   continue;
 Succ->removePredecessor(SI->getParent());
+Updates.push_back({DominatorTree::Delete, SI->getParent(), Succ});
   }
   SI->eraseFromParent();
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
 
   ++NumLookupTables;
   if (NeedMask)
@@ -6099,7 +6113,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, 
IRBuilder<> &Builder) {
   // CVP. Therefore, only apply this transformation during late stages of the
   // optimisation pipeline.
   if (Options.ConvertSwitchToLookupTable &&
-  SwitchToLookupTable(SI, Builder, DL, TTI))
+  SwitchToLookupTable(SI, Builder, DTU, DL, TTI))
 return requestResimplify();
 
   if (ReduceSwitchRange(SI, Builder, DL, TTI))

[llvm-branch-commits] [llvm] a17025a - [SimplifyCFG] Teach switchToSelect() to preserve DomTree

2020-12-30 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T23:58:40+03:00
New Revision: a17025aa61b16021ee85ff5deec47a9ed40ae1d4

URL: 
https://github.com/llvm/llvm-project/commit/a17025aa61b16021ee85ff5deec47a9ed40ae1d4
DIFF: 
https://github.com/llvm/llvm-project/commit/a17025aa61b16021ee85ff5deec47a9ed40ae1d4.diff

LOG: [SimplifyCFG] Teach switchToSelect() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index c730c1427ec8..2b631175f55e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5282,7 +5282,8 @@ static Value *ConvertTwoCaseSwitch(const 
SwitchCaseResultVectorTy &ResultVector,
 // a select, fixing up PHI nodes and basic blocks.
 static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
   Value *SelectValue,
-  IRBuilder<> &Builder) {
+  IRBuilder<> &Builder,
+  DomTreeUpdater *DTU) {
   BasicBlock *SelectBB = SI->getParent();
   while (PHI->getBasicBlockIndex(SelectBB) >= 0)
 PHI->removeIncomingValue(SelectBB);
@@ -5290,6 +5291,8 @@ static void RemoveSwitchAfterSelectConversion(SwitchInst 
*SI, PHINode *PHI,
 
   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);
@@ -5297,15 +5300,18 @@ static void 
RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
 if (Succ == PHI->getParent())
   continue;
 Succ->removePredecessor(SelectBB);
+Updates.push_back({DominatorTree::Delete, SelectBB, Succ});
   }
   SI->eraseFromParent();
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
 }
 
 /// If the switch is only used to initialize one or more
 /// phi nodes in a common successor block with only two 
diff erent
 /// constant values, replace the switch with select.
 static bool switchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
-   const DataLayout &DL,
+   DomTreeUpdater *DTU, const DataLayout &DL,
const TargetTransformInfo &TTI) {
   Value *const Cond = SI->getCondition();
   PHINode *PHI = nullptr;
@@ -5325,7 +5331,7 @@ static bool switchToSelect(SwitchInst *SI, IRBuilder<> 
&Builder,
   Value *SelectValue =
   ConvertTwoCaseSwitch(UniqueResults, DefaultResult, Cond, Builder);
   if (SelectValue) {
-RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder);
+RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder, DTU);
 return true;
   }
   // The switch couldn't be converted into a select.
@@ -6081,7 +6087,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, 
IRBuilder<> &Builder) {
   if (eliminateDeadSwitchCases(SI, Options.AC, DL))
 return requestResimplify();
 
-  if (switchToSelect(SI, Builder, DL, TTI))
+  if (switchToSelect(SI, Builder, DTU, DL, TTI))
 return requestResimplify();
 
   if (Options.ForwardSwitchCondToPhi && ForwardSwitchConditionToPHI(SI))

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll 
b/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll
index 31f5410bae40..bd3d5dff94d3 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-to-select-two-case.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -S | FileCheck %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 
 ; int foo1_with_default(int a) {
 ;   switch(a) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 51879a5 - [LoopIdiom] 'left-shift until bittest': don't forget to check that PHI node is in loop header

2020-12-30 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2020-12-30T23:58:41+03:00
New Revision: 51879a525649c8151f7e841b66a5cea0e1c8e74e

URL: 
https://github.com/llvm/llvm-project/commit/51879a525649c8151f7e841b66a5cea0e1c8e74e
DIFF: 
https://github.com/llvm/llvm-project/commit/51879a525649c8151f7e841b66a5cea0e1c8e74e.diff

LOG: [LoopIdiom] 'left-shift until bittest': don't forget to check that PHI 
node is in loop header

Fixes an issue reported by Peter Collingbourne in
https://reviews.llvm.org/D91726#2475301

Added: 


Modified: 
llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp 
b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index 3612f8cc1a71..8064c02e2b39 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -2024,8 +2024,8 @@ static bool detectShiftUntilBitTestIdiom(Loop *CurLoop, 
Value *&BaseX,
 
   // Step 3: Check if the recurrence is in desirable form.
   auto *CurrXPN = dyn_cast(CurrX);
-  if (!CurrXPN) {
-LLVM_DEBUG(dbgs() << DEBUG_TYPE " Not a PHI node.\n");
+  if (!CurrXPN || CurrXPN->getParent() != LoopHeaderBB) {
+LLVM_DEBUG(dbgs() << DEBUG_TYPE " Not an expected PHI node.\n");
 return false;
   }
 

diff  --git a/llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll 
b/llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll
index 17ff7fc7663b..82dc8451382f 100644
--- a/llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll
+++ b/llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll
@@ -1812,3 +1812,84 @@ end:
   call void @use1(i1 %x.curr.isbitunset)
   ret i32 %x.curr
 }
+
+; %x.curr is not a PHI node
+define i32 @n33(i32 %x, i32 %bit, i32 %x.curr) {
+; ALL-LABEL: @n33(
+; ALL-NEXT:  entry:
+; ALL-NEXT:[[BITMASK:%.*]] = shl i32 1, [[BIT:%.*]], [[DBG507:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[BITMASK]], 
[[META503:metadata !.*]], metadata !DIExpression()), [[DBG507]]
+; ALL-NEXT:br label [[LOOP:%.*]], [[DBG508:!dbg !.*]]
+; ALL:   loop:
+; ALL-NEXT:[[X_CURR_BITMASKED:%.*]] = and i32 [[X_CURR:%.*]], [[BITMASK]], 
[[DBG509:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[X_CURR_BITMASKED]], 
[[META504:metadata !.*]], metadata !DIExpression()), [[DBG509]]
+; ALL-NEXT:[[X_CURR_ISBITUNSET:%.*]] = icmp eq i32 [[X_CURR_BITMASKED]], 
0, [[DBG510:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i1 [[X_CURR_ISBITUNSET]], 
[[META505:metadata !.*]], metadata !DIExpression()), [[DBG510]]
+; ALL-NEXT:[[X_NEXT:%.*]] = shl i32 [[X_CURR]], 1, [[DBG511:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[X_NEXT]], 
[[META506:metadata !.*]], metadata !DIExpression()), [[DBG511]]
+; ALL-NEXT:br i1 [[X_CURR_ISBITUNSET]], label [[LOOP]], label [[END:%.*]], 
[[DBG512:!dbg !.*]]
+; ALL:   end:
+; ALL-NEXT:ret i32 [[X_CURR]], [[DBG513:!dbg !.*]]
+;
+entry:
+  %bitmask = shl i32 1, %bit
+  br label %loop
+
+loop:
+  %x.curr.bitmasked = and i32 %x.curr, %bitmask
+  %x.curr.isbitunset = icmp eq i32 %x.curr.bitmasked, 0
+  %x.next = shl i32 %x.curr, 1
+  br i1 %x.curr.isbitunset, label %loop, label %end
+
+end:
+  ret i32 %x.curr
+}
+
+; %x.curr is a PHI node in a wrong block
+define i32 @n34(i32 %bit, i1 %c, i32 %x0, i32 %x1) {
+; ALL-LABEL: @n34(
+; ALL-NEXT:  entry:
+; ALL-NEXT:[[BITMASK:%.*]] = shl i32 1, [[BIT:%.*]], [[DBG521:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[BITMASK]], 
[[META516:metadata !.*]], metadata !DIExpression()), [[DBG521]]
+; ALL-NEXT:br i1 [[C:%.*]], label [[BB0:%.*]], label [[BB1:%.*]], 
[[DBG522:!dbg !.*]]
+; ALL:   bb0:
+; ALL-NEXT:br label [[MERGE:%.*]], [[DBG523:!dbg !.*]]
+; ALL:   bb1:
+; ALL-NEXT:br label [[MERGE]], [[DBG524:!dbg !.*]]
+; ALL:   merge:
+; ALL-NEXT:[[X_CURR:%.*]] = phi i32 [ [[X0:%.*]], [[BB0]] ], [ [[X1:%.*]], 
[[BB1]] ], [[DBG525:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[X_CURR]], 
[[META517:metadata !.*]], metadata !DIExpression()), [[DBG525]]
+; ALL-NEXT:br label [[LOOP:%.*]], [[DBG526:!dbg !.*]]
+; ALL:   loop:
+; ALL-NEXT:[[X_CURR_BITMASKED:%.*]] = and i32 [[X_CURR]], [[BITMASK]], 
[[DBG527:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[X_CURR_BITMASKED]], 
[[META518:metadata !.*]], metadata !DIExpression()), [[DBG527]]
+; ALL-NEXT:[[X_CURR_ISBITUNSET:%.*]] = icmp eq i32 [[X_CURR_BITMASKED]], 
0, [[DBG528:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i1 [[X_CURR_ISBITUNSET]], 
[[META519:metadata !.*]], metadata !DIExpression()), [[DBG528]]
+; ALL-NEXT:[[X_NEXT:%.*]] = shl i32 [[X_CURR]], 1, [[DBG529:!dbg !.*]]
+; ALL-NEXT:call void @llvm.dbg.value(metadata i32 [[X_NEXT]], 
[[META520:meta

[llvm-branch-commits] [llvm] c1b825d - [SimplifyCFG] Teach FoldValueComparisonIntoPredecessors() to preserve DomTree, part 1

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:22+03:00
New Revision: c1b825d4b8a68178613972a50088b2b73105e91e

URL: 
https://github.com/llvm/llvm-project/commit/c1b825d4b8a68178613972a50088b2b73105e91e
DIFF: 
https://github.com/llvm/llvm-project/commit/c1b825d4b8a68178613972a50088b2b73105e91e.diff

LOG: [SimplifyCFG] Teach FoldValueComparisonIntoPredecessors() to preserve 
DomTree, part 1

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/ADCE/domtree-DoubleDeletion.ll
llvm/test/Transforms/JumpThreading/lvi-tristate.ll
llvm/test/Transforms/SimplifyCFG/2005-08-01-PHIUpdateFail.ll
llvm/test/Transforms/SimplifyCFG/2006-08-03-Crash.ll
llvm/test/Transforms/SimplifyCFG/ARM/switch-to-lookup-table.ll
llvm/test/Transforms/SimplifyCFG/DeadSetCC.ll
llvm/test/Transforms/SimplifyCFG/X86/MagicPointer.ll
llvm/test/Transforms/SimplifyCFG/preserve-branchweights-partial.ll
llvm/test/Transforms/SimplifyCFG/preserve-branchweights-switch-create.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 7e49d3a1524c..f1e6c50130c8 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1088,11 +1088,14 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   SmallSetVector FailBlocks;
   if (!SafeToMergeTerminators(TI, PTI, &FailBlocks)) {
 for (auto *Succ : FailBlocks) {
-  if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split"))
+  if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split",
+  DTU ? &DTU->getDomTree() : nullptr))
 return false;
 }
   }
 
+  std::vector Updates;
+
   // Figure out which 'cases' to copy from SI to PSI.
   std::vector BBCases;
   BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
@@ -1156,6 +1159,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
 // Reconstruct the new switch statement we will be building.
 if (PredDefault != BBDefault) {
   PredDefault->removePredecessor(Pred);
+  Updates.push_back({DominatorTree::Delete, Pred, PredDefault});
   PredDefault = BBDefault;
   NewSuccessors.push_back(BBDefault);
 }
@@ -1232,8 +1236,10 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   // Okay, at this point, we know which new successor Pred will get.  Make
   // sure we update the number of entries in the PHI nodes for these
   // successors.
-  for (BasicBlock *NewSuccessor : NewSuccessors)
+  for (BasicBlock *NewSuccessor : NewSuccessors) {
 AddPredecessorToBlock(NewSuccessor, Pred, BB);
+Updates.push_back({DominatorTree::Insert, Pred, NewSuccessor});
+  }
 
   Builder.SetInsertPoint(PTI);
   // Convert pointer to int before we switch.
@@ -1272,10 +1278,20 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
 InfLoopBlock = BasicBlock::Create(BB->getContext(), "infloop",
   BB->getParent());
 BranchInst::Create(InfLoopBlock, InfLoopBlock);
+Updates.push_back(
+{DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
   }
   NewSI->setSuccessor(i, InfLoopBlock);
 }
 
+  if (InfLoopBlock) {
+Updates.push_back({DominatorTree::Delete, Pred, BB});
+Updates.push_back({DominatorTree::Insert, Pred, InfLoopBlock});
+  }
+
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
+
   Changed = true;
 }
   }

diff  --git a/llvm/test/Transforms/ADCE/domtree-DoubleDeletion.ll 
b/llvm/test/Transforms/ADCE/domtree-DoubleDeletion.ll
index 018eb79b26fd..1175288cd09e 100644
--- a/llvm/test/Transforms/ADCE/domtree-DoubleDeletion.ll
+++ b/llvm/test/Transforms/ADCE/domtree-DoubleDeletion.ll
@@ -1,5 +1,5 @@
-; RUN: opt < %s -gvn -simplifycfg -adce | llvm-dis
-; RUN: opt < %s -gvn -simplifycfg -adce -verify-dom-info | llvm-dis
+; RUN: opt < %s -gvn -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-adce | llvm-dis
+; RUN: opt < %s -gvn -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-adce -verify-dom-info | llvm-dis
 
 ; This test makes sure that the DominatorTree properly handles
 ; deletion of edges that go to forward-unreachable regions.

diff  --git a/llvm/test/Transforms/JumpThreading/lvi-tristate.ll 
b/llvm/test/Transforms/JumpThreading/lvi-tristate.ll
index 94fd0e5049c4..ee0140035c3b 100644
--- a/llvm/test/Transforms/JumpThreading/lvi-tristate.ll
+++ b/llvm/test/Transforms/JumpThreading/lvi-tristate.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -jump-threading -

[llvm-branch-commits] [llvm] b7c463d - [SimplifyCFG] Teach FoldBranchToCommonDest() to preserve DomTree, part 2

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:23+03:00
New Revision: b7c463d7b8dda2c2b3e9a4db51f94dc94542ee68

URL: 
https://github.com/llvm/llvm-project/commit/b7c463d7b8dda2c2b3e9a4db51f94dc94542ee68
DIFF: 
https://github.com/llvm/llvm-project/commit/b7c463d7b8dda2c2b3e9a4db51f94dc94542ee68.diff

LOG: [SimplifyCFG] Teach FoldBranchToCommonDest() to preserve DomTree, part 2

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/branch-fold.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index f1e6c50130c8..d081908c49e9 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3121,6 +3121,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 assert(PBI_C->getType()->isIntegerTy(1));
 Instruction *MergedCond = nullptr;
 if (PBI->getSuccessor(0) == UniqueSucc) {
+  Updates.push_back(
+  {DominatorTree::Delete, PredBlock, PBI->getSuccessor(1)});
   // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
   // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
   //   is false: !PBI_Cond and BI_Value
@@ -3133,6 +3135,9 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
 MergedCond = cast(Builder.CreateBinOp(
 Instruction::Or, PBI->getCondition(), MergedCond, "or.cond"));
 } else {
+  assert(PBI->getSuccessor(1) == UniqueSucc && "Unexpected branch");
+  Updates.push_back(
+  {DominatorTree::Delete, PredBlock, PBI->getSuccessor(0)});
   // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
   // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
   //   is false: PBI_Cond and BI_Value

diff  --git a/llvm/test/Transforms/SimplifyCFG/branch-fold.ll 
b/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
index 7097dea424e1..a4ac23bada70 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -S | FileCheck %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 
 define void @test(i32* %P, i32* %Q, i1 %A, i1 %B) {
 ; CHECK: test



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 9f17dab - [SimplifyCFG] Teach simplifyIndirectBr() to preserve DomTree

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:23+03:00
New Revision: 9f17dab1f48eed788d29e4c6f045e64b0679b3a9

URL: 
https://github.com/llvm/llvm-project/commit/9f17dab1f48eed788d29e4c6f045e64b0679b3a9
DIFF: 
https://github.com/llvm/llvm-project/commit/9f17dab1f48eed788d29e4c6f045e64b0679b3a9.diff

LOG: [SimplifyCFG] Teach simplifyIndirectBr() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/indirectbr.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d081908c49e9..1fd2956fac51 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6148,10 +6148,13 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst 
*IBI) {
   bool Changed = false;
 
   // Eliminate redundant destinations.
+  std::vector Updates;
   SmallPtrSet Succs;
   for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
 BasicBlock *Dest = IBI->getDestination(i);
 if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
+  if (!Dest->hasAddressTaken())
+Updates.push_back({DominatorTree::Delete, BB, Dest});
   Dest->removePredecessor(BB);
   IBI->removeDestination(i);
   --i;
@@ -6160,6 +6163,10 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst 
*IBI) {
 }
   }
 
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
+  Updates.clear();
+
   if (IBI->getNumDestinations() == 0) {
 // If the indirectbr has no successors, change it to unreachable.
 new UnreachableInst(IBI->getContext(), IBI);

diff  --git a/llvm/test/Transforms/SimplifyCFG/indirectbr.ll 
b/llvm/test/Transforms/SimplifyCFG/indirectbr.ll
index 67e23d2fe935..52ef6b77166c 100644
--- a/llvm/test/Transforms/SimplifyCFG/indirectbr.ll
+++ b/llvm/test/Transforms/SimplifyCFG/indirectbr.ll
@@ -1,4 +1,4 @@
-; RUN: opt -S -simplifycfg < %s | FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 ; SimplifyCFG should eliminate redundant indirectbr edges.
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 0d2f219 - [SimplifyCFG] Teach SimplifyEqualityComparisonWithOnlyPredecessor() to preserve DomTree, part 3

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:23+03:00
New Revision: 0d2f219d4d0b4c61491508e6980055ecc241418c

URL: 
https://github.com/llvm/llvm-project/commit/0d2f219d4d0b4c61491508e6980055ecc241418c
DIFF: 
https://github.com/llvm/llvm-project/commit/0d2f219d4d0b4c61491508e6980055ecc241418c.diff

LOG: [SimplifyCFG] Teach SimplifyEqualityComparisonWithOnlyPredecessor() to 
preserve DomTree, part 3

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch_thread.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1fd2956fac51..402b98efadad 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -13,6 +13,7 @@
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/ScopeExit.h"
@@ -934,13 +935,25 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
   << "Through successor TI: " << *TI);
 
+SmallMapVector NumPerSuccessorCases;
 for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) 
{
   --i;
+  auto *Successor = i->getCaseSuccessor();
+  ++NumPerSuccessorCases[Successor];
   if (DeadCases.count(i->getCaseValue())) {
-i->getCaseSuccessor()->removePredecessor(TI->getParent());
+Successor->removePredecessor(PredDef);
 SI.removeCase(i);
+--NumPerSuccessorCases[Successor];
   }
 }
+
+std::vector Updates;
+for (const std::pair &I : NumPerSuccessorCases)
+  if (I.second == 0)
+Updates.push_back({DominatorTree::Delete, PredDef, I.first});
+if (DTU)
+  DTU->applyUpdatesPermissive(Updates);
+
 LLVM_DEBUG(dbgs() << "Leaving: " << *TI << "\n");
 return true;
   }

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch_thread.ll 
b/llvm/test/Transforms/SimplifyCFG/switch_thread.ll
index e38865699d2e..3688757253ad 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_thread.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_thread.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -simplifycfg -S | FileCheck %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 
 ; Test that we can thread a simple known condition through switch statements.
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] a815b6b - [SimplifyCFG] Teach eliminateDeadSwitchCases() to preserve DomTree, part 1

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:24+03:00
New Revision: a815b6b2b2cc483cfe9d2b520790322a39f8d37b

URL: 
https://github.com/llvm/llvm-project/commit/a815b6b2b2cc483cfe9d2b520790322a39f8d37b
DIFF: 
https://github.com/llvm/llvm-project/commit/a815b6b2b2cc483cfe9d2b520790322a39f8d37b.diff

LOG: [SimplifyCFG] Teach eliminateDeadSwitchCases() to preserve DomTree, part 1

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 402b98efadad..2ff576b17751 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4908,7 +4908,8 @@ bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst 
*SI,
 
 /// Compute masked bits for the condition of a switch
 /// and use it to remove dead cases.
-static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
+static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
+ AssumptionCache *AC,
  const DataLayout &DL) {
   Value *Cond = SI->getCondition();
   unsigned Bits = Cond->getType()->getIntegerBitWidth();
@@ -4922,11 +4923,15 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, 
AssumptionCache *AC,
 
   // Gather dead cases.
   SmallVector DeadCases;
+  SmallMapVector NumPerSuccessorCases;
   for (auto &Case : SI->cases()) {
+auto *Successor = Case.getCaseSuccessor();
+++NumPerSuccessorCases[Successor];
 const APInt &CaseVal = Case.getCaseValue()->getValue();
 if (Known.Zero.intersects(CaseVal) || !Known.One.isSubsetOf(CaseVal) ||
 (CaseVal.getMinSignedBits() > MaxSignificantBitsInCond)) {
   DeadCases.push_back(Case.getCaseValue());
+  --NumPerSuccessorCases[Successor];
   LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
 << " is dead.\n");
 }
@@ -4961,6 +4966,13 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, 
AssumptionCache *AC,
 SIW.removeCase(CaseI);
   }
 
+  std::vector Updates;
+  for (const std::pair &I : NumPerSuccessorCases)
+if (I.second == 0)
+  Updates.push_back({DominatorTree::Delete, SI->getParent(), I.first});
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
+
   return true;
 }
 
@@ -6132,7 +6144,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, 
IRBuilder<> &Builder) {
 return requestResimplify();
 
   // Remove unreachable cases.
-  if (eliminateDeadSwitchCases(SI, Options.AC, DL))
+  if (eliminateDeadSwitchCases(SI, DTU, Options.AC, DL))
 return requestResimplify();
 
   if (switchToSelect(SI, Builder, DTU, DL, TTI))

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll 
b/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
index 21cecc5c942b..1b8d6e70b7dd 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-masked-bits.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -simplifycfg < %s | FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 define i32 @test1(i32 %x) nounwind {
 ; CHECK-LABEL: @test1(

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll 
b/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll
index bbc0bd78da7c..755ad0a16338 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-simplify-crash.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -disable-output
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-disable-output
 
 define void @NewExtractNames() {
 entry:



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 8866583 - [SimplifyCFG] Teach FoldValueComparisonIntoPredecessors() to preserve DomTree, part 2

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:24+03:00
New Revision: 8866583953f1e9038b5ca4ed4c199e0476790e0c

URL: 
https://github.com/llvm/llvm-project/commit/8866583953f1e9038b5ca4ed4c199e0476790e0c
DIFF: 
https://github.com/llvm/llvm-project/commit/8866583953f1e9038b5ca4ed4c199e0476790e0c.diff

LOG: [SimplifyCFG] Teach FoldValueComparisonIntoPredecessors() to preserve 
DomTree, part 2

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/PGOProfile/chr.ll
llvm/test/Transforms/SimplifyCFG/2008-12-16-DCECond.ll
llvm/test/Transforms/SimplifyCFG/switch_switch_fold.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 2ff576b17751..bf61d34f7322 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1109,6 +1109,8 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
 
   std::vector Updates;
 
+  Updates.push_back({DominatorTree::Delete, Pred, BB});
+
   // Figure out which 'cases' to copy from SI to PSI.
   std::vector BBCases;
   BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);

diff  --git a/llvm/test/Transforms/PGOProfile/chr.ll 
b/llvm/test/Transforms/PGOProfile/chr.ll
index 0390b87d..8a05a624209e 100644
--- a/llvm/test/Transforms/PGOProfile/chr.ll
+++ b/llvm/test/Transforms/PGOProfile/chr.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -chr -instcombine -simplifycfg -S -enable-new-pm=0 | FileCheck 
%s
+; RUN: opt < %s -chr -instcombine -simplifycfg 
-simplifycfg-require-and-preserve-domtree=1 -S -enable-new-pm=0 | FileCheck %s
 ; RUN: opt < %s 
-passes='require,function(chr,instcombine,simplify-cfg)' -S | 
FileCheck %s
 
 declare void @foo()

diff  --git a/llvm/test/Transforms/SimplifyCFG/2008-12-16-DCECond.ll 
b/llvm/test/Transforms/SimplifyCFG/2008-12-16-DCECond.ll
index 2e356df75ca2..108fa3094add 100644
--- a/llvm/test/Transforms/SimplifyCFG/2008-12-16-DCECond.ll
+++ b/llvm/test/Transforms/SimplifyCFG/2008-12-16-DCECond.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -simplifycfg -S -hoist-common-insts=true | not grep icmp
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S 
-hoist-common-insts=true | not grep icmp
 ; ModuleID = '/tmp/x.bc'
 target datalayout = 
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
 target triple = "i686-pc-linux-gnu"

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch_switch_fold.ll 
b/llvm/test/Transforms/SimplifyCFG/switch_switch_fold.ll
index 0df40349c1b3..e1095431f31f 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_switch_fold.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_switch_fold.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -simplifycfg -S | FileCheck %s
+; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 
 ; Test that a switch going to a switch on the same value can be merged.
 ; All three switches in this example can be merged into one big one.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 831636b - [SimplifyCFG] SUCCESS! Teach createUnreachableSwitchDefault() to preserve DomTree

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:25+03:00
New Revision: 831636b0e6321500fd7bf3f32e8663369152a956

URL: 
https://github.com/llvm/llvm-project/commit/831636b0e6321500fd7bf3f32e8663369152a956
DIFF: 
https://github.com/llvm/llvm-project/commit/831636b0e6321500fd7bf3f32e8663369152a956.diff

LOG: [SimplifyCFG] SUCCESS! Teach createUnreachableSwitchDefault() to preserve 
DomTree

This pretty much concludes patch series for updating SimplifyCFG
to preserve DomTree. All 318 dedicated `-simplifycfg` tests now pass
with `-simplifycfg-require-and-preserve-domtree=1`.

There are a few leftovers that apparently don't have good test coverage.
I do not yet know what gaps in test coverage will the wider-scale testing
reveal, but the default flip might be close.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index f513db3783fb..53353cd5e923 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4783,15 +4783,29 @@ static bool 
CasesAreContiguous(SmallVectorImpl &Cases) {
   return true;
 }
 
-static void createUnreachableSwitchDefault(SwitchInst *Switch) {
+static void createUnreachableSwitchDefault(SwitchInst *Switch,
+   DomTreeUpdater *DTU) {
   LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
+  auto *BB = Switch->getParent();
   BasicBlock *NewDefaultBlock =
- SplitBlockPredecessors(Switch->getDefaultDest(), Switch->getParent(), "");
+  SplitBlockPredecessors(Switch->getDefaultDest(), Switch->getParent(), "",
+ DTU ? &DTU->getDomTree() : nullptr);
+  auto *OrigDefaultBlock = Switch->getDefaultDest();
   Switch->setDefaultDest(&*NewDefaultBlock);
-  SplitBlock(&*NewDefaultBlock, &NewDefaultBlock->front());
+  if (DTU)
+DTU->applyUpdatesPermissive(
+{{DominatorTree::Delete, BB, OrigDefaultBlock},
+ {DominatorTree::Insert, BB, &*NewDefaultBlock}});
+  SplitBlock(&*NewDefaultBlock, &NewDefaultBlock->front(),
+ DTU ? &DTU->getDomTree() : nullptr);
+  SmallVector Updates;
+  for (auto *Successor : successors(NewDefaultBlock))
+Updates.push_back({DominatorTree::Delete, NewDefaultBlock, Successor});
   auto *NewTerminator = NewDefaultBlock->getTerminator();
   new UnreachableInst(Switch->getContext(), NewTerminator);
   EraseTerminatorAndDCECond(NewTerminator);
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
 }
 
 /// Turn a switch with two reachable destinations into an integer range
@@ -4803,6 +4817,8 @@ bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst 
*SI,
   bool HasDefault =
   !isa(SI->getDefaultDest()->getFirstNonPHIOrDbg());
 
+  auto *BB = SI->getParent();
+
   // Partition the cases into two sets with 
diff erent destinations.
   BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr;
   BasicBlock *DestB = nullptr;
@@ -4906,11 +4922,17 @@ bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst 
*SI,
   // Clean up the default block - it may have phis or other instructions before
   // the unreachable terminator.
   if (!HasDefault)
-createUnreachableSwitchDefault(SI);
+createUnreachableSwitchDefault(SI, DTU);
+
+  auto *UnreachableDefault = SI->getDefaultDest();
 
   // Drop the switch.
   SI->eraseFromParent();
 
+  if (!HasDefault && DTU)
+DTU->applyUpdatesPermissive(
+{{DominatorTree::Delete, BB, UnreachableDefault}});
+
   return true;
 }
 
@@ -4957,7 +4979,7 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, 
DomTreeUpdater *DTU,
   if (HasDefault && DeadCases.empty() &&
   NumUnknownBits < 64 /* avoid overflow */ &&
   SI->getNumCases() == (1ULL << NumUnknownBits)) {
-createUnreachableSwitchDefault(SI);
+createUnreachableSwitchDefault(SI, /*DTU=*/nullptr);
 return true;
   }
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll 
b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
index d99e84b6b940..7125575b2f33 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt %s -simplifycfg -S | FileCheck %s
+; RUN: opt %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 
 declare i32 @f(i32)
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] e1440d4 - [SimplifyCFG] Teach tryToSimplifyUncondBranchWithICmpInIt() to preserve DomTree

2020-12-31 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-01T03:25:25+03:00
New Revision: e1440d43bca5438544a12bebe82a3cd1a37157f1

URL: 
https://github.com/llvm/llvm-project/commit/e1440d43bca5438544a12bebe82a3cd1a37157f1
DIFF: 
https://github.com/llvm/llvm-project/commit/e1440d43bca5438544a12bebe82a3cd1a37157f1.diff

LOG: [SimplifyCFG] Teach tryToSimplifyUncondBranchWithICmpInIt() to preserve 
DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll
llvm/test/Transforms/SimplifyCFG/switch_create-custom-dl.ll
llvm/test/Transforms/SimplifyCFG/switch_create.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index bf61d34f7322..f513db3783fb 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4052,6 +4052,8 @@ bool 
SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
   ICI->replaceAllUsesWith(DefaultCst);
   ICI->eraseFromParent();
 
+  SmallVector Updates;
+
   // Okay, the switch goes to this block on a default value.  Add an edge from
   // the switch to the merge point on the compared value.
   BasicBlock *NewBB =
@@ -4065,13 +4067,17 @@ bool 
SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
   SIW.setSuccessorWeight(0, *NewW);
 }
 SIW.addCase(Cst, NewBB, NewW);
+Updates.push_back({DominatorTree::Insert, Pred, NewBB});
   }
 
   // NewBB branches to the phi block, add the uncond branch and the phi entry.
   Builder.SetInsertPoint(NewBB);
   Builder.SetCurrentDebugLocation(SI->getDebugLoc());
   Builder.CreateBr(SuccBlock);
+  Updates.push_back({DominatorTree::Insert, NewBB, SuccBlock});
   PHIUse->addIncoming(NewCst, NewBB);
+  if (DTU)
+DTU->applyUpdatesPermissive(Updates);
   return true;
 }
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll 
b/llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll
index 66a34bab3e5b..078d0aa2f1de 100644
--- a/llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll
+++ b/llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -simplifycfg -S -o - < %s | FileCheck %s
+; RUN: opt -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S -o - < 
%s | FileCheck %s
 
 declare void @helper(i32)
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch_create-custom-dl.ll 
b/llvm/test/Transforms/SimplifyCFG/switch_create-custom-dl.ll
index dc07eb20568d..cdc334e474b6 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_create-custom-dl.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_create-custom-dl.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -simplifycfg < %s | FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 target datalayout="p:40:64:64:32"
 
 declare void @foo1()

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch_create.ll 
b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
index 10689a03c398..a4c84a66cafb 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_create.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
@@ -1,6 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -simplifycfg < %s | FileCheck %s
-; RUN: opt -S -data-layout="p:32:32-p1:16:16" -simplifycfg < %s | FileCheck 
-check-prefix=CHECK -check-prefix=DL %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
+; RUN: opt -S -data-layout="p:32:32-p1:16:16" -simplifycfg 
-simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck 
-check-prefix=CHECK -check-prefix=DL %s
 
 declare void @foo1()
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] e08fea3 - [SimplifyCFGPass] Ensure that DominatorTreeWrapperPass is init'd before SimplifyCFG

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:17+03:00
New Revision: e08fea3b240994572a4dd6b34bd846aef023a123

URL: 
https://github.com/llvm/llvm-project/commit/e08fea3b240994572a4dd6b34bd846aef023a123
DIFF: 
https://github.com/llvm/llvm-project/commit/e08fea3b240994572a4dd6b34bd846aef023a123.diff

LOG: [SimplifyCFGPass] Ensure that DominatorTreeWrapperPass is init'd before 
SimplifyCFG

It's probably better than hoping that it will happen to be
already initialized.

Added: 


Modified: 
llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp 
b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
index 35ab1e52ee2e..3efdc0e9ea86 100644
--- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
+++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
@@ -357,6 +357,7 @@ INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", 
"Simplify the CFG", false,
   false)
 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
+INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
 INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
 false)
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] f1ce696 - [SimplifyCFG] Teach tryWidenCondBranchToCondBranch() to preserve DomTree

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:17+03:00
New Revision: f1ce6960561bc28129fa3306f57a5c6df21258ab

URL: 
https://github.com/llvm/llvm-project/commit/f1ce6960561bc28129fa3306f57a5c6df21258ab
DIFF: 
https://github.com/llvm/llvm-project/commit/f1ce6960561bc28129fa3306f57a5c6df21258ab.diff

LOG: [SimplifyCFG] Teach tryWidenCondBranchToCondBranch() to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/wc-widen-block.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 53353cd5e923..30e3f0bc174c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3530,7 +3530,8 @@ static bool mergeConditionalStores(BranchInst *PBI, 
BranchInst *QBI,
 /// If the previous block ended with a widenable branch, determine if reusing
 /// the target block is profitable and legal.  This will have the effect of
 /// "widening" PBI, but doesn't require us to reason about hosting safety.
-static bool tryWidenCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
+static bool tryWidenCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
+   DomTreeUpdater *DTU) {
   // TODO: This can be generalized in two important ways:
   // 1) We can allow phi nodes in IfFalseBB and simply reuse all the input
   //values from the PBI edge.
@@ -3553,15 +3554,25 @@ static bool tryWidenCondBranchToCondBranch(BranchInst 
*PBI, BranchInst *BI) {
   if (BI->getSuccessor(1) != IfFalseBB && // no inf looping
   BI->getSuccessor(1)->getTerminatingDeoptimizeCall() && // profitability
   NoSideEffects(*BI->getParent())) {
-BI->getSuccessor(1)->removePredecessor(BI->getParent());
+auto *OldSuccessor = BI->getSuccessor(1);
+OldSuccessor->removePredecessor(BI->getParent());
 BI->setSuccessor(1, IfFalseBB);
+if (DTU)
+  DTU->applyUpdatesPermissive(
+  {{DominatorTree::Delete, BI->getParent(), OldSuccessor},
+   {DominatorTree::Insert, BI->getParent(), IfFalseBB}});
 return true;
   }
   if (BI->getSuccessor(0) != IfFalseBB && // no inf looping
   BI->getSuccessor(0)->getTerminatingDeoptimizeCall() && // profitability
   NoSideEffects(*BI->getParent())) {
-BI->getSuccessor(0)->removePredecessor(BI->getParent());
+auto *OldSuccessor = BI->getSuccessor(0);
+OldSuccessor->removePredecessor(BI->getParent());
 BI->setSuccessor(0, IfFalseBB);
+if (DTU)
+  DTU->applyUpdatesPermissive(
+  {{DominatorTree::Delete, BI->getParent(), OldSuccessor},
+   {DominatorTree::Insert, BI->getParent(), IfFalseBB}});
 return true;
   }
   return false;
@@ -3626,7 +3637,7 @@ static bool SimplifyCondBranchToCondBranch(BranchInst 
*PBI, BranchInst *BI,
   // If the previous block ended with a widenable branch, determine if reusing
   // the target block is profitable and legal.  This will have the effect of
   // "widening" PBI, but doesn't require us to reason about hosting safety.
-  if (tryWidenCondBranchToCondBranch(PBI, BI))
+  if (tryWidenCondBranchToCondBranch(PBI, BI, DTU))
 return true;
 
   if (auto *CE = dyn_cast(BI->getCondition()))

diff  --git a/llvm/test/Transforms/SimplifyCFG/wc-widen-block.ll 
b/llvm/test/Transforms/SimplifyCFG/wc-widen-block.ll
index 6e959bc37ed1..aefadabf3bb0 100644
--- a/llvm/test/Transforms/SimplifyCFG/wc-widen-block.ll
+++ b/llvm/test/Transforms/SimplifyCFG/wc-widen-block.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -passes=simplify-cfg -S < %s | FileCheck %s
+; RUN: opt -passes=simplify-cfg -simplifycfg-require-and-preserve-domtree=1 -S 
< %s | FileCheck %s
 
 define i32 @basic(i1 %cond_0, i32* %p) {
 ; CHECK-LABEL: @basic(



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] db75326 - [IR] PassManagerTest: Register DominatorTreeAnalysis before running SimplifyCFGPass

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:18+03:00
New Revision: db753269d912936d9ab27018faf867afce4b16de

URL: 
https://github.com/llvm/llvm-project/commit/db753269d912936d9ab27018faf867afce4b16de
DIFF: 
https://github.com/llvm/llvm-project/commit/db753269d912936d9ab27018faf867afce4b16de.diff

LOG: [IR] PassManagerTest: Register DominatorTreeAnalysis before running 
SimplifyCFGPass

Otherwise these particular tests fail when SimplifyCFG requires DomTree

Added: 


Modified: 
llvm/unittests/IR/PassManagerTest.cpp

Removed: 




diff  --git a/llvm/unittests/IR/PassManagerTest.cpp 
b/llvm/unittests/IR/PassManagerTest.cpp
index ea33a0b64ea4..5e24ae3e5410 100644
--- a/llvm/unittests/IR/PassManagerTest.cpp
+++ b/llvm/unittests/IR/PassManagerTest.cpp
@@ -10,6 +10,7 @@
 #include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Dominators.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
@@ -828,6 +829,7 @@ TEST_F(PassManagerTest, FunctionPassCFGChecker) {
   StandardInstrumentations SI(/*DebugLogging*/ true);
   SI.registerCallbacks(PIC);
   FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
+  FAM.registerPass([&] { return DominatorTreeAnalysis(); });
   FAM.registerPass([&] { return AssumptionAnalysis(); });
   FAM.registerPass([&] { return TargetIRAnalysis(); });
 
@@ -873,6 +875,7 @@ TEST_F(PassManagerTest, 
FunctionPassCFGCheckerInvalidateAnalysis) {
   StandardInstrumentations SI(/*DebugLogging*/ true);
   SI.registerCallbacks(PIC);
   FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
+  FAM.registerPass([&] { return DominatorTreeAnalysis(); });
   FAM.registerPass([&] { return AssumptionAnalysis(); });
   FAM.registerPass([&] { return TargetIRAnalysis(); });
 
@@ -937,6 +940,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerWrapped) {
   StandardInstrumentations SI(/*DebugLogging*/ true);
   SI.registerCallbacks(PIC);
   FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
+  FAM.registerPass([&] { return DominatorTreeAnalysis(); });
   FAM.registerPass([&] { return AssumptionAnalysis(); });
   FAM.registerPass([&] { return TargetIRAnalysis(); });
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 657c1e0 - [SimplifyCFG] Teach eliminateDeadSwitchCases() to preserve DomTree, part 2

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:18+03:00
New Revision: 657c1e09da9fb5e63b963fe8eeace01e42e7d3ba

URL: 
https://github.com/llvm/llvm-project/commit/657c1e09da9fb5e63b963fe8eeace01e42e7d3ba
DIFF: 
https://github.com/llvm/llvm-project/commit/657c1e09da9fb5e63b963fe8eeace01e42e7d3ba.diff

LOG: [SimplifyCFG] Teach eliminateDeadSwitchCases() to preserve DomTree, part 2

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 30e3f0bc174c..37e594dbd1a4 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4990,7 +4990,7 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, 
DomTreeUpdater *DTU,
   if (HasDefault && DeadCases.empty() &&
   NumUnknownBits < 64 /* avoid overflow */ &&
   SI->getNumCases() == (1ULL << NumUnknownBits)) {
-createUnreachableSwitchDefault(SI, /*DTU=*/nullptr);
+createUnreachableSwitchDefault(SI, DTU);
 return true;
   }
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll 
b/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll
index 2ce04ec85060..975696541799 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt %s -S -passes='simplify-cfg' | FileCheck %s
+; RUN: opt %s -S -passes='simplify-cfg' 
-simplifycfg-require-and-preserve-domtree=1 | FileCheck %s
+
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 declare void @foo(i32)
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 5fe0798 - [Utils] LocalTest: fix SimplifyCFGWithNullAC test to work with `-simplifycfg-require-and-preserve-domtree=1`

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:18+03:00
New Revision: 5fe0798dee6e2093aad7be11956c2bcaaf61dc8c

URL: 
https://github.com/llvm/llvm-project/commit/5fe0798dee6e2093aad7be11956c2bcaaf61dc8c
DIFF: 
https://github.com/llvm/llvm-project/commit/5fe0798dee6e2093aad7be11956c2bcaaf61dc8c.diff

LOG: [Utils] LocalTest: fix SimplifyCFGWithNullAC test to work with 
`-simplifycfg-require-and-preserve-domtree=1`

Added: 


Modified: 
llvm/unittests/Transforms/Utils/LocalTest.cpp

Removed: 




diff  --git a/llvm/unittests/Transforms/Utils/LocalTest.cpp 
b/llvm/unittests/Transforms/Utils/LocalTest.cpp
index ece83950955a..a57a18ce0be1 100644
--- a/llvm/unittests/Transforms/Utils/LocalTest.cpp
+++ b/llvm/unittests/Transforms/Utils/LocalTest.cpp
@@ -998,8 +998,12 @@ TEST(Local, SimplifyCFGWithNullAC) {
   }
   ASSERT_TRUE(TestBB);
 
+  DominatorTree DT(F);
+  DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
+
   // %test.bb is expected to be simplified by FoldCondBranchOnPHI.
-  EXPECT_TRUE(simplifyCFG(TestBB, TTI, /*DTU=*/nullptr, Options));
+  EXPECT_TRUE(simplifyCFG(TestBB, TTI,
+  RequireAndPreserveDomTree ? &DTU : nullptr, 
Options));
 }
 
 TEST(Local, CanReplaceOperandWithVariable) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] c38739a - [NFC] clang-format the entire DwarfEHPrepare.cpp

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:19+03:00
New Revision: c38739ad8f10beb3144abcfebdcf50547328d346

URL: 
https://github.com/llvm/llvm-project/commit/c38739ad8f10beb3144abcfebdcf50547328d346
DIFF: 
https://github.com/llvm/llvm-project/commit/c38739ad8f10beb3144abcfebdcf50547328d346.diff

LOG: [NFC] clang-format the entire DwarfEHPrepare.cpp

Added: 


Modified: 
llvm/lib/CodeGen/DwarfEHPrepare.cpp

Removed: 




diff  --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp 
b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index c7048337cdf2..c05364354155 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -44,52 +44,51 @@ STATISTIC(NumResumesLowered, "Number of resume calls 
lowered");
 
 namespace {
 
-  class DwarfEHPrepare : public FunctionPass {
-// RewindFunction - _Unwind_Resume or the target equivalent.
-FunctionCallee RewindFunction = nullptr;
+class DwarfEHPrepare : public FunctionPass {
+  // RewindFunction - _Unwind_Resume or the target equivalent.
+  FunctionCallee RewindFunction = nullptr;
 
-CodeGenOpt::Level OptLevel;
-DominatorTree *DT = nullptr;
-const TargetLowering *TLI = nullptr;
+  CodeGenOpt::Level OptLevel;
+  DominatorTree *DT = nullptr;
+  const TargetLowering *TLI = nullptr;
 
-bool InsertUnwindResumeCalls(Function &Fn);
-Value *GetExceptionObject(ResumeInst *RI);
-size_t
-pruneUnreachableResumes(Function &Fn,
-SmallVectorImpl &Resumes,
-SmallVectorImpl &CleanupLPads);
+  bool InsertUnwindResumeCalls(Function &Fn);
+  Value *GetExceptionObject(ResumeInst *RI);
+  size_t
+  pruneUnreachableResumes(Function &Fn, SmallVectorImpl &Resumes,
+  SmallVectorImpl &CleanupLPads);
 
-  public:
-static char ID; // Pass identification, replacement for typeid.
+public:
+  static char ID; // Pass identification, replacement for typeid.
 
-DwarfEHPrepare(CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
+  DwarfEHPrepare(CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
   : FunctionPass(ID), OptLevel(OptLevel) {}
 
-bool runOnFunction(Function &Fn) override;
+  bool runOnFunction(Function &Fn) override;
 
-bool doFinalization(Module &M) override {
-  RewindFunction = nullptr;
-  return false;
-}
+  bool doFinalization(Module &M) override {
+RewindFunction = nullptr;
+return false;
+  }
 
-void getAnalysisUsage(AnalysisUsage &AU) const override;
+  void getAnalysisUsage(AnalysisUsage &AU) const override;
 
-StringRef getPassName() const override {
-  return "Exception handling preparation";
-}
-  };
+  StringRef getPassName() const override {
+return "Exception handling preparation";
+  }
+};
 
 } // end anonymous namespace
 
 char DwarfEHPrepare::ID = 0;
 
-INITIALIZE_PASS_BEGIN(DwarfEHPrepare, DEBUG_TYPE,
-  "Prepare DWARF exceptions", false, false)
+INITIALIZE_PASS_BEGIN(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions",
+  false, false)
 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
-INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE,
-"Prepare DWARF exceptions", false, false)
+INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions",
+false, false)
 
 FunctionPass *llvm::createDwarfEHPass(CodeGenOpt::Level OptLevel) {
   return new DwarfEHPrepare(OptLevel);
@@ -187,8 +186,8 @@ size_t DwarfEHPrepare::pruneUnreachableResumes(
 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
 /// into calls to the appropriate _Unwind_Resume function.
 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
-  SmallVector Resumes;
-  SmallVector CleanupLPads;
+  SmallVector Resumes;
+  SmallVector CleanupLPads;
   for (BasicBlock &BB : Fn) {
 if (auto *RI = dyn_cast(BB.getTerminator()))
   Resumes.push_back(RI);
@@ -216,8 +215,8 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
 
   // Find the rewind function if we didn't already.
   if (!RewindFunction) {
-FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
-  Type::getInt8PtrTy(Ctx), false);
+FunctionType *FTy =
+FunctionType::get(Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), 
false);
 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
   }
@@ -241,8 +240,8 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
   }
 
   BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
-  PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft,
-"exn.obj", UnwindBB);
+  PHINode *PN = PHINod

[llvm-branch-commits] [llvm] e6b1a27 - [NFC][CodeGen] Split DwarfEHPrepare pass into an actual transform and an legacy-PM wrapper

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:19+03:00
New Revision: e6b1a27fb9c71a9a81439917368a25ddc7d371a9

URL: 
https://github.com/llvm/llvm-project/commit/e6b1a27fb9c71a9a81439917368a25ddc7d371a9
DIFF: 
https://github.com/llvm/llvm-project/commit/e6b1a27fb9c71a9a81439917368a25ddc7d371a9.diff

LOG: [NFC][CodeGen] Split DwarfEHPrepare pass into an actual transform and an 
legacy-PM wrapper

This is consistent with the layout of other passes,
and simplifies further refinements regarding DomTree handling.

This is indended to be a NFC commit.

Added: 


Modified: 
llvm/include/llvm/InitializePasses.h
llvm/lib/CodeGen/CodeGen.cpp
llvm/lib/CodeGen/DwarfEHPrepare.cpp
llvm/tools/opt/opt.cpp

Removed: 




diff  --git a/llvm/include/llvm/InitializePasses.h 
b/llvm/include/llvm/InitializePasses.h
index f77de64e2c64..4f89179a03de 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -140,7 +140,7 @@ void initializeDomPrinterPass(PassRegistry&);
 void initializeDomViewerPass(PassRegistry&);
 void initializeDominanceFrontierWrapperPassPass(PassRegistry&);
 void initializeDominatorTreeWrapperPassPass(PassRegistry&);
-void initializeDwarfEHPreparePass(PassRegistry&);
+void initializeDwarfEHPrepareLegacyPassPass(PassRegistry &);
 void initializeEarlyCSELegacyPassPass(PassRegistry&);
 void initializeEarlyCSEMemSSALegacyPassPass(PassRegistry&);
 void initializeEarlyIfConverterPass(PassRegistry&);

diff  --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 94925498b01c..d2400d0371e3 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -30,7 +30,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeDeadMachineInstructionElimPass(Registry);
   initializeDebugifyMachineModulePass(Registry);
   initializeDetectDeadLanesPass(Registry);
-  initializeDwarfEHPreparePass(Registry);
+  initializeDwarfEHPrepareLegacyPassPass(Registry);
   initializeEarlyIfConverterPass(Registry);
   initializeEarlyIfPredicatorPass(Registry);
   initializeEarlyMachineLICMPass(Registry);

diff  --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp 
b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index c05364354155..5696b2e46d3b 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -44,66 +44,44 @@ STATISTIC(NumResumesLowered, "Number of resume calls 
lowered");
 
 namespace {
 
-class DwarfEHPrepare : public FunctionPass {
+class DwarfEHPrepare {
+  CodeGenOpt::Level OptLevel;
+
   // RewindFunction - _Unwind_Resume or the target equivalent.
-  FunctionCallee RewindFunction = nullptr;
+  FunctionCallee &RewindFunction;
 
-  CodeGenOpt::Level OptLevel;
-  DominatorTree *DT = nullptr;
-  const TargetLowering *TLI = nullptr;
+  Function &F;
+  const TargetLowering &TLI;
+  DominatorTree *DT;
+  const TargetTransformInfo *TTI;
 
-  bool InsertUnwindResumeCalls(Function &Fn);
+  /// Return the exception object from the value passed into
+  /// the 'resume' instruction (typically an aggregate). Clean up any dead
+  /// instructions, including the 'resume' instruction.
   Value *GetExceptionObject(ResumeInst *RI);
+
+  /// Replace resumes that are not reachable from a cleanup landing pad with
+  /// unreachable and then simplify those blocks.
   size_t
-  pruneUnreachableResumes(Function &Fn, SmallVectorImpl &Resumes,
+  pruneUnreachableResumes(SmallVectorImpl &Resumes,
   SmallVectorImpl &CleanupLPads);
 
-public:
-  static char ID; // Pass identification, replacement for typeid.
-
-  DwarfEHPrepare(CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
-  : FunctionPass(ID), OptLevel(OptLevel) {}
-
-  bool runOnFunction(Function &Fn) override;
-
-  bool doFinalization(Module &M) override {
-RewindFunction = nullptr;
-return false;
-  }
+  /// Convert the ResumeInsts that are still present
+  /// into calls to the appropriate _Unwind_Resume function.
+  bool InsertUnwindResumeCalls();
 
-  void getAnalysisUsage(AnalysisUsage &AU) const override;
+public:
+  DwarfEHPrepare(CodeGenOpt::Level OptLevel_, FunctionCallee &RewindFunction_,
+ Function &F_, const TargetLowering &TLI_, DominatorTree *DT_,
+ const TargetTransformInfo *TTI_)
+  : OptLevel(OptLevel_), RewindFunction(RewindFunction_), F(F_), TLI(TLI_),
+DT(DT_), TTI(TTI_) {}
 
-  StringRef getPassName() const override {
-return "Exception handling preparation";
-  }
+  bool run();
 };
 
-} // end anonymous namespace
-
-char DwarfEHPrepare::ID = 0;
-
-INITIALIZE_PASS_BEGIN(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions",
-  false, false)
-INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
-INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
-INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
-INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE, "Prepare DWARF exceptions",

[llvm-branch-commits] [llvm] b23b1bc - [NFC][CodeGen][Tests] Mark all tests that fail to preserve DomTree for SimplifyCFG as such

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:19+03:00
New Revision: b23b1bcc26e775a4998a78b6fe256e8cb0e3e0d3

URL: 
https://github.com/llvm/llvm-project/commit/b23b1bcc26e775a4998a78b6fe256e8cb0e3e0d3
DIFF: 
https://github.com/llvm/llvm-project/commit/b23b1bcc26e775a4998a78b6fe256e8cb0e3e0d3.diff

LOG: [NFC][CodeGen][Tests] Mark all tests that fail to preserve DomTree for 
SimplifyCFG as such

These tests start to fail when the SimplifyCFG's default regarding DomTree
updating is switched on, so mark them as needing changes.

Added: 


Modified: 
llvm/test/CodeGen/AArch64/pic-eh-stubs.ll
llvm/test/CodeGen/AMDGPU/atomic_optimizations_pixelshader.ll
llvm/test/CodeGen/AMDGPU/branch-condition-and.ll
llvm/test/CodeGen/AMDGPU/branch-relaxation.ll
llvm/test/CodeGen/AMDGPU/infinite-loop.ll
llvm/test/CodeGen/AMDGPU/kill-infinite-loop.ll
llvm/test/CodeGen/AMDGPU/mixed-wave32-wave64.ll
llvm/test/CodeGen/AMDGPU/multi-divergent-exit-region.ll
llvm/test/CodeGen/AMDGPU/ret_jump.ll
llvm/test/CodeGen/AMDGPU/si-annotate-cf-noloop.ll
llvm/test/CodeGen/AMDGPU/si-lower-control-flow-unreachable-block.ll
llvm/test/CodeGen/AMDGPU/skip-if-dead.ll
llvm/test/CodeGen/AMDGPU/unigine-liveness-crash.ll
llvm/test/CodeGen/AMDGPU/unstructured-cfg-def-use-issue.ll
llvm/test/CodeGen/AMDGPU/update-phi.ll
llvm/test/CodeGen/AMDGPU/valu-i1.ll
llvm/test/CodeGen/AMDGPU/wave32.ll
llvm/test/CodeGen/ARM/2011-05-04-MultipleLandingPadSuccs.ll
llvm/test/CodeGen/ARM/2014-05-14-DwarfEHCrash.ll
llvm/test/CodeGen/ARM/arm-ttype-target2.ll
llvm/test/CodeGen/ARM/dwarf-eh.ll
llvm/test/CodeGen/ARM/ehabi-filters.ll
llvm/test/CodeGen/ARM/global-merge.ll
llvm/test/CodeGen/ARM/setjmp_longjmp.ll
llvm/test/CodeGen/Hexagon/cfi_offset.ll
llvm/test/CodeGen/Hexagon/ehabi.ll
llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll
llvm/test/CodeGen/Hexagon/packetize-allocframe.ll
llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll
llvm/test/CodeGen/Hexagon/swp-order-deps3.ll
llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll
llvm/test/CodeGen/PowerPC/2007-11-16-landingpad-split.ll
llvm/test/CodeGen/PowerPC/aix-exception.ll
llvm/test/CodeGen/SPARC/exception.ll
llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll
llvm/test/CodeGen/X86/2007-05-05-Personality.ll
llvm/test/CodeGen/X86/2010-08-04-MingWCrash.ll
llvm/test/CodeGen/X86/2012-01-10-UndefExceptionEdge.ll
llvm/test/CodeGen/X86/basic-block-sections-eh.ll
llvm/test/CodeGen/X86/code-model-kernel.ll
llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
llvm/test/CodeGen/X86/gcc_except_table-multi.ll
llvm/test/CodeGen/X86/indirect-branch-tracking-eh2.ll

Removed: 




diff  --git a/llvm/test/CodeGen/AArch64/pic-eh-stubs.ll 
b/llvm/test/CodeGen/AArch64/pic-eh-stubs.ll
index c59a5b6743d6..af95b7e100a6 100644
--- a/llvm/test/CodeGen/AArch64/pic-eh-stubs.ll
+++ b/llvm/test/CodeGen/AArch64/pic-eh-stubs.ll
@@ -1,5 +1,5 @@
-; RUN: llc -mtriple=aarch64-none-linux-gnu -relocation-model=pic -o - %s | 
FileCheck %s
-; RUN: llc -mtriple=aarch64_be-none-linux-gnu -relocation-model=pic -o - %s | 
FileCheck %s
+; RUN: llc -mtriple=aarch64-none-linux-gnu -relocation-model=pic 
-simplifycfg-require-and-preserve-domtree=0 -o - %s | FileCheck %s
+; RUN: llc -mtriple=aarch64_be-none-linux-gnu -relocation-model=pic 
-simplifycfg-require-and-preserve-domtree=0 -o - %s | FileCheck %s
 
 ; Make sure exception-handling PIC code can be linked correctly. An alternative
 ; to the sequence described below would have .gcc_except_table itself writable

diff  --git a/llvm/test/CodeGen/AMDGPU/atomic_optimizations_pixelshader.ll 
b/llvm/test/CodeGen/AMDGPU/atomic_optimizations_pixelshader.ll
index 95d3e0d2cd0a..f4876e4c10b0 100644
--- a/llvm/test/CodeGen/AMDGPU/atomic_optimizations_pixelshader.ll
+++ b/llvm/test/CodeGen/AMDGPU/atomic_optimizations_pixelshader.ll
@@ -1,9 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc -mtriple=amdgcn-- -amdgpu-atomic-optimizations=true 
-verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefixes=GFX7 %s
-; RUN: llc  -mtriple=amdgcn-- -mcpu=tonga -mattr=-flat-for-global 
-amdgpu-atomic-optimizations=true -verify-machineinstrs < %s | FileCheck 
-enable-var-scope -check-prefixes=GFX8 %s
-; RUN: llc -mtriple=amdgcn-- -mcpu=gfx900 -mattr=-flat-for-global 
-amdgpu-atomic-optimizations=true -verify-machineinstrs < %s | FileCheck 
-enable-var-scope -check-prefixes=GFX9 %s
-; RUN: llc -mtriple=amdgcn-- -mcpu=gfx1010 
-mattr=-wavefrontsize32,+wavefrontsize64 -mattr=-flat-for-global 
-amdgpu-atomic-optimizations=true -verify-machineinstrs < %s | FileCheck 
-enable-var-scope -check-prefixes=GFX1064 %s
-; RUN: llc -mtriple=amdgcn-- -mcpu=gfx1010 
-mattr=+wavefrontsize32,-wavefrontsize64 -mattr=-flat-for-global 
-amdgpu-atomic-optimiz

[llvm-branch-commits] [llvm] 2461cdb - [CodeGen][SimplifyCFG] Teach DwarfEHPrepare to preserve DomTree

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:19+03:00
New Revision: 2461cdb41724298591133c811df82b0064adfa6b

URL: 
https://github.com/llvm/llvm-project/commit/2461cdb41724298591133c811df82b0064adfa6b
DIFF: 
https://github.com/llvm/llvm-project/commit/2461cdb41724298591133c811df82b0064adfa6b.diff

LOG: [CodeGen][SimplifyCFG] Teach DwarfEHPrepare to preserve DomTree

Once the default for SimplifyCFG flips, we can no longer pass nullptr
instead of DomTree to SimplifyCFG, so we need to propagate it here.

We don't strictly need to actually preserve DomTree in DwarfEHPrepare,
but we might as well do it, since it's trivial.

Added: 


Modified: 
llvm/lib/CodeGen/DwarfEHPrepare.cpp
llvm/test/CodeGen/AArch64/pic-eh-stubs.ll
llvm/test/CodeGen/ARM/2011-05-04-MultipleLandingPadSuccs.ll
llvm/test/CodeGen/ARM/2014-05-14-DwarfEHCrash.ll
llvm/test/CodeGen/ARM/arm-ttype-target2.ll
llvm/test/CodeGen/ARM/dwarf-eh.ll
llvm/test/CodeGen/ARM/ehabi-filters.ll
llvm/test/CodeGen/ARM/global-merge.ll
llvm/test/CodeGen/ARM/setjmp_longjmp.ll
llvm/test/CodeGen/Hexagon/cfi_offset.ll
llvm/test/CodeGen/Hexagon/ehabi.ll
llvm/test/CodeGen/Hexagon/packetize-allocframe.ll
llvm/test/CodeGen/PowerPC/2007-11-16-landingpad-split.ll
llvm/test/CodeGen/PowerPC/aix-exception.ll
llvm/test/CodeGen/SPARC/exception.ll
llvm/test/CodeGen/X86/2007-05-05-Personality.ll
llvm/test/CodeGen/X86/2010-08-04-MingWCrash.ll
llvm/test/CodeGen/X86/2012-01-10-UndefExceptionEdge.ll
llvm/test/CodeGen/X86/basic-block-sections-eh.ll
llvm/test/CodeGen/X86/code-model-kernel.ll
llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
llvm/test/CodeGen/X86/gcc_except_table-multi.ll
llvm/test/CodeGen/X86/indirect-branch-tracking-eh2.ll

Removed: 




diff  --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp 
b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index 5696b2e46d3b..a4824ef21640 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/CFG.h"
+#include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/EHPersonalities.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/RuntimeLibcalls.h"
@@ -52,7 +53,7 @@ class DwarfEHPrepare {
 
   Function &F;
   const TargetLowering &TLI;
-  DominatorTree *DT;
+  DomTreeUpdater *DTU;
   const TargetTransformInfo *TTI;
 
   /// Return the exception object from the value passed into
@@ -72,10 +73,10 @@ class DwarfEHPrepare {
 
 public:
   DwarfEHPrepare(CodeGenOpt::Level OptLevel_, FunctionCallee &RewindFunction_,
- Function &F_, const TargetLowering &TLI_, DominatorTree *DT_,
+ Function &F_, const TargetLowering &TLI_, DomTreeUpdater 
*DTU_,
  const TargetTransformInfo *TTI_)
   : OptLevel(OptLevel_), RewindFunction(RewindFunction_), F(F_), TLI(TLI_),
-DT(DT_), TTI(TTI_) {}
+DTU(DTU_), TTI(TTI_) {}
 
   bool run();
 };
@@ -122,11 +123,13 @@ Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) 
{
 size_t DwarfEHPrepare::pruneUnreachableResumes(
 SmallVectorImpl &Resumes,
 SmallVectorImpl &CleanupLPads) {
+  assert(DTU && "Should have DomTreeUpdater here.");
+
   BitVector ResumeReachable(Resumes.size());
   size_t ResumeIndex = 0;
   for (auto *RI : Resumes) {
 for (auto *LP : CleanupLPads) {
-  if (isPotentiallyReachable(LP, RI, nullptr, DT)) {
+  if (isPotentiallyReachable(LP, RI, nullptr, &DTU->getDomTree())) {
 ResumeReachable.set(ResumeIndex);
 break;
   }
@@ -150,7 +153,7 @@ size_t DwarfEHPrepare::pruneUnreachableResumes(
   BasicBlock *BB = RI->getParent();
   new UnreachableInst(Ctx, RI);
   RI->eraseFromParent();
-  simplifyCFG(BB, *TTI);
+  simplifyCFG(BB, *TTI, DTU);
 }
   }
   Resumes.resize(ResumesLeft);
@@ -211,6 +214,9 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls() {
 return true;
   }
 
+  std::vector Updates;
+  Updates.reserve(Resumes.size());
+
   BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &F);
   PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft, 
"exn.obj",
 UnwindBB);
@@ -220,6 +226,7 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls() {
   for (ResumeInst *RI : Resumes) {
 BasicBlock *Parent = RI->getParent();
 BranchInst::Create(UnwindBB, Parent);
+Updates.push_back({DominatorTree::Insert, Parent, UnwindBB});
 
 Value *ExnObj = GetExceptionObject(RI);
 PN->addIncoming(ExnObj, Parent);
@@ -234,10 +241,39 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls() {
   // We never expect _Unwind_Resume to return.
   CI->setDoesNotReturn();
   new UnreachableInst(Ctx, UnwindBB);
+
+  if (DTU && RequireAndPreserveDomTree)
+DTU->applyUpdatesPermissive(Updates);
+
   return true;

[llvm-branch-commits] [llvm] b4429f3 - [SimplifyCFG] Teach removeUndefIntroducingPredecessor to preserve DomTree

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:20+03:00
New Revision: b4429f3cdd1a8511bd014877b22d9679c4a74dbd

URL: 
https://github.com/llvm/llvm-project/commit/b4429f3cdd1a8511bd014877b22d9679c4a74dbd
DIFF: 
https://github.com/llvm/llvm-project/commit/b4429f3cdd1a8511bd014877b22d9679c4a74dbd.diff

LOG: [SimplifyCFG] Teach removeUndefIntroducingPredecessor to preserve DomTree

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll
llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll
llvm/test/CodeGen/Hexagon/swp-order-deps3.ll
llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll
llvm/test/CodeGen/Thumb2/2009-08-04-CoalescerBug.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 37e594dbd1a4..fa12d8b99644 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6563,14 +6563,16 @@ static bool passingValueIsAlwaysUndefined(Value *V, 
Instruction *I) {
 
 /// If BB has an incoming value that will always trigger undefined behavior
 /// (eg. null pointer dereference), remove the branch leading here.
-static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
+static bool removeUndefIntroducingPredecessor(BasicBlock *BB,
+  DomTreeUpdater *DTU) {
   for (PHINode &PHI : BB->phis())
 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i)
   if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) {
-Instruction *T = PHI.getIncomingBlock(i)->getTerminator();
+BasicBlock *Predecessor = PHI.getIncomingBlock(i);
+Instruction *T = Predecessor->getTerminator();
 IRBuilder<> Builder(T);
 if (BranchInst *BI = dyn_cast(T)) {
-  BB->removePredecessor(PHI.getIncomingBlock(i));
+  BB->removePredecessor(Predecessor);
   // Turn uncoditional branches into unreachables and remove the dead
   // destination from conditional branches.
   if (BI->isUnconditional())
@@ -6579,6 +6581,9 @@ static bool removeUndefIntroducingPredecessor(BasicBlock 
*BB) {
 Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1)
: BI->getSuccessor(0));
   BI->eraseFromParent();
+  if (DTU)
+DTU->applyUpdatesPermissive(
+{{DominatorTree::Delete, Predecessor, BB}});
   return true;
 }
 // TODO: SwitchInst.
@@ -6611,7 +6616,7 @@ bool SimplifyCFGOpt::simplifyOnceImpl(BasicBlock *BB) {
   Changed |= EliminateDuplicatePHINodes(BB);
 
   // Check for and remove branches that will always cause undefined behavior.
-  Changed |= removeUndefIntroducingPredecessor(BB);
+  Changed |= removeUndefIntroducingPredecessor(BB, DTU);
 
   // Merge basic blocks into their predecessor if there is only one distinct
   // pred, and if there is only one distinct successor of the predecessor, and

diff  --git 
a/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll 
b/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll
index ba867e68a95e..0945e17531be 100644
--- a/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll
+++ b/llvm/test/CodeGen/Hexagon/misaligned_double_vector_store_not_fast.ll
@@ -1,4 +1,4 @@
-; RUN: llc -march=hexagon -O3 -debug-only=isel 2>&1 
-simplifycfg-require-and-preserve-domtree=0 < %s | FileCheck %s
+; RUN: llc -march=hexagon -O3 -debug-only=isel 2>&1 
-simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s
 ; REQUIRES: asserts
 
 ; DAGCombiner converts the two vector stores to a double vector store,

diff  --git a/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll 
b/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll
index 192f1356fe42..5b5b45d13210 100644
--- a/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll
+++ b/llvm/test/CodeGen/Hexagon/swp-epilog-phi10.ll
@@ -1,4 +1,4 @@
-; RUN: llc -march=hexagon -mcpu=hexagonv5 
-simplifycfg-require-and-preserve-domtree=0 < %s
+; RUN: llc -march=hexagon -mcpu=hexagonv5 
-simplifycfg-require-and-preserve-domtree=1 < %s
 ; REQUIRES: asserts
 
 define void @test(i8* noalias nocapture readonly %src, i32 %srcStride) 
local_unnamed_addr #0 {

diff  --git a/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll 
b/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll
index 6f5245348187..e85d35549385 100644
--- a/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll
+++ b/llvm/test/CodeGen/Hexagon/swp-order-deps3.ll
@@ -1,4 +1,4 @@
-; RUN: llc -march=hexagon -O2 -simplifycfg-require-and-preserve-domtree=0 < %s
+; RUN: llc -march=hexagon -O2 -simplifycfg-require-and-preserve-domtree=1 < %s
 ; REQUIRES: asserts
 
 ; Function Attrs: noinline nounwind ssp

diff  --git a/llvm/test/CodeGen/Hexagon/swp-reuse-phi-4.ll 
b/llvm/tes

[llvm-branch-commits] [llvm] 4b80647 - [AMDGPU][SimplifyCFG] Teach AMDGPUUnifyDivergentExitNodes to preserve {, Post}DomTree

2021-01-01 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T01:01:20+03:00
New Revision: 4b80647367950ba3da6a08260487fd0dbc50a9c5

URL: 
https://github.com/llvm/llvm-project/commit/4b80647367950ba3da6a08260487fd0dbc50a9c5
DIFF: 
https://github.com/llvm/llvm-project/commit/4b80647367950ba3da6a08260487fd0dbc50a9c5.diff

LOG: [AMDGPU][SimplifyCFG] Teach AMDGPUUnifyDivergentExitNodes to preserve 
{,Post}DomTree

This is a (last big?) part of the patch series to make SimplifyCFG
preserve DomTree. Currently, it still does not actually preserve it,
even thought it is pretty much fully updated to preserve it.

Once the default is flipped, a valid DomTree must be passed into
simplifyCFG, which means that whatever pass calls simplifyCFG,
should also be smart about DomTree's.

As far as i can see from `check-llvm` with default flipped,
this is the last LLVM test batch (other than bugpoint tests)
that needed fixes to not break with default flipped.

The changes here are boringly identical to the ones i did
over 42+ times/commits recently already,
so while AMDGPU is outside of my normal ecosystem,
i'm going to go for post-commit review here,
like in all the other 42+ changes.

Note that while the pass is taught to preserve {,Post}DomTree,
it still doesn't do that by default, because simplifycfg
still doesn't do that by default, and flipping default
in this pass will implicitly flip the default for simplifycfg.
That will happen, but not right now.

Added: 


Modified: 
llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
llvm/test/CodeGen/AMDGPU/atomic_optimizations_pixelshader.ll
llvm/test/CodeGen/AMDGPU/branch-condition-and.ll
llvm/test/CodeGen/AMDGPU/branch-relaxation.ll
llvm/test/CodeGen/AMDGPU/infinite-loop.ll
llvm/test/CodeGen/AMDGPU/kill-infinite-loop.ll
llvm/test/CodeGen/AMDGPU/mixed-wave32-wave64.ll
llvm/test/CodeGen/AMDGPU/multi-divergent-exit-region.ll
llvm/test/CodeGen/AMDGPU/ret_jump.ll
llvm/test/CodeGen/AMDGPU/si-annotate-cf-noloop.ll
llvm/test/CodeGen/AMDGPU/si-lower-control-flow-unreachable-block.ll
llvm/test/CodeGen/AMDGPU/skip-if-dead.ll
llvm/test/CodeGen/AMDGPU/unigine-liveness-crash.ll
llvm/test/CodeGen/AMDGPU/unstructured-cfg-def-use-issue.ll
llvm/test/CodeGen/AMDGPU/update-phi.ll
llvm/test/CodeGen/AMDGPU/valu-i1.ll
llvm/test/CodeGen/AMDGPU/wave32.ll

Removed: 




diff  --git a/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp 
b/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
index 2232af66e0da..96de1bd8f8d1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
@@ -25,17 +25,19 @@
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
 #include "llvm/Analysis/PostDominators.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/Dominators.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Intrinsics.h"
-#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Type.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
@@ -71,17 +73,25 @@ char &llvm::AMDGPUUnifyDivergentExitNodesID = 
AMDGPUUnifyDivergentExitNodes::ID;
 
 INITIALIZE_PASS_BEGIN(AMDGPUUnifyDivergentExitNodes, DEBUG_TYPE,
  "Unify divergent function exit nodes", false, false)
+INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
 INITIALIZE_PASS_END(AMDGPUUnifyDivergentExitNodes, DEBUG_TYPE,
 "Unify divergent function exit nodes", false, false)
 
 void AMDGPUUnifyDivergentExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
-  // TODO: Preserve dominator tree.
+  if (RequireAndPreserveDomTree)
+AU.addRequired();
+
   AU.addRequired();
 
   AU.addRequired();
 
+  if (RequireAndPreserveDomTree) {
+AU.addPreserved();
+AU.addPreserved();
+  }
+
   // No divergent values are changed, only blocks and branch edges.
   AU.addPreserved();
 
@@ -134,7 +144,7 @@ static void removeDoneExport(Function &F) {
   }
 }
 
-static BasicBlock *unifyReturnBlockSet(Function &F,
+static BasicBlock *unifyReturnBlockSet(Function &F, DomTreeUpdater &DTU,
ArrayRef ReturningBlocks,
bool InsertExport,
const TargetTransformInfo &TTI,
@@ -175,6 +185,8 @@ static BasicBlock *unifyReturnBlockSet(Function &F,
 
   // Loop over all of the blocks, replacing the return instruction with an
   // unconditional branch.

[llvm-branch-commits] [llvm] f4ea219 - [NFCI][CodeGen] DwarfEHPrepare: don't actually pass DTU into simplifyCFG by default

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T14:38:52+03:00
New Revision: f4ea21947d907c8730fef2be8fbad457f1d96a0e

URL: 
https://github.com/llvm/llvm-project/commit/f4ea21947d907c8730fef2be8fbad457f1d96a0e
DIFF: 
https://github.com/llvm/llvm-project/commit/f4ea21947d907c8730fef2be8fbad457f1d96a0e.diff

LOG: [NFCI][CodeGen] DwarfEHPrepare: don't actually pass DTU into simplifyCFG 
by default

also, don't verify DomTree unless we intend to maintain it.
This is a very dumb think-o, i guess i was even warned about it
by subconsciousness in 4b80647367950ba3da6a08260487fd0dbc50a9c5's
commit message..

Fixes a compile-time regression reported by Martin Storsjö
in post-commit review of 2461cdb41724298591133c811df82b0064adfa6b.

Added: 


Modified: 
llvm/lib/CodeGen/DwarfEHPrepare.cpp

Removed: 




diff  --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp 
b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index 34a04feec040..c1b764214546 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -153,7 +153,7 @@ size_t DwarfEHPrepare::pruneUnreachableResumes(
   BasicBlock *BB = RI->getParent();
   new UnreachableInst(Ctx, RI);
   RI->eraseFromParent();
-  simplifyCFG(BB, *TTI, DTU);
+  simplifyCFG(BB, *TTI, RequireAndPreserveDomTree ? DTU : nullptr);
 }
   }
   Resumes.resize(ResumesLeft);
@@ -249,7 +249,7 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls() {
 }
 
 bool DwarfEHPrepare::run() {
-  assert(((OptLevel == CodeGenOpt::None) ||
+  assert(((OptLevel == CodeGenOpt::None || !RequireAndPreserveDomTree) ||
   (DTU &&
DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full))) 
&&
  "Original domtree is invalid?");



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] b9da488 - [SimplifyCFG] Don't actually take DomTreeUpdater unless we intend to maintain DomTree validity

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-02T14:40:55+03:00
New Revision: b9da488ad729a6604439d1b5e456330bab9321bd

URL: 
https://github.com/llvm/llvm-project/commit/b9da488ad729a6604439d1b5e456330bab9321bd
DIFF: 
https://github.com/llvm/llvm-project/commit/b9da488ad729a6604439d1b5e456330bab9321bd.diff

LOG: [SimplifyCFG] Don't actually take DomTreeUpdater unless we intend to 
maintain DomTree validity

This guards against unintentional mistakes
like the one i just fixed in previous commit.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index fa12d8b99644..e1f7ef636a89 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6700,7 +6700,7 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
 bool llvm::simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
DomTreeUpdater *DTU, const SimplifyCFGOptions &Options,
SmallPtrSetImpl *LoopHeaders) {
-  return SimplifyCFGOpt(TTI, DTU, BB->getModule()->getDataLayout(), 
LoopHeaders,
-Options)
+  return SimplifyCFGOpt(TTI, RequireAndPreserveDomTree ? DTU : nullptr,
+BB->getModule()->getDataLayout(), LoopHeaders, Options)
   .run(BB);
 }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 19ab181 - [llvm-reduce] Fix removal of unused llvm intrinsics declarations

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:47+03:00
New Revision: 19ab1817b61d3b716f69f78f727de8bd8887f53f

URL: 
https://github.com/llvm/llvm-project/commit/19ab1817b61d3b716f69f78f727de8bd8887f53f
DIFF: 
https://github.com/llvm/llvm-project/commit/19ab1817b61d3b716f69f78f727de8bd8887f53f.diff

LOG: [llvm-reduce] Fix removal of unused llvm intrinsics declarations

ee6e25e4391a6d3ac0a3c89615474e512f44cda6 changed
the delta pass to skip intrinsics, which means we may end up being
left with declarations of intrinsics, that aren't otherwise referenced
in the module. This is obviously unwanted, do drop them.

Added: 
llvm/test/Reduce/remove-unused-declarations.ll

Modified: 
llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp

Removed: 




diff  --git a/llvm/test/Reduce/remove-unused-declarations.ll 
b/llvm/test/Reduce/remove-unused-declarations.ll
new file mode 100644
index ..5ae3a3edbad0
--- /dev/null
+++ b/llvm/test/Reduce/remove-unused-declarations.ll
@@ -0,0 +1,21 @@
+; RUN: llvm-reduce --test FileCheck --test-arg 
--check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL 
--implicit-check-not=uninteresting %s
+
+declare void @llvm.uninteresting()
+declare void @uninteresting()
+
+; CHECK-ALL: declare void @llvm.interesting()
+; CHECK-ALL: declare void @interesting()
+declare void @llvm.interesting()
+declare void @interesting()
+
+; CHECK-ALL: define void @main() {
+; CHECK-ALL-NEXT:  call void @llvm.interesting()
+; CHECK-ALL-NEXT:   call void @interesting()
+; CHECK-ALL-NEXT:   ret void
+; CHECK-ALL-NEXT: }
+define void @main() {
+  call void @llvm.interesting()
+  call void @interesting()
+  ret void
+}

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
index 6bb8f81c3dbd..d100935ee422 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp
@@ -33,8 +33,8 @@ static void extractFunctionsFromModule(const 
std::vector &ChunksToKeep,
   [&O](Function &F) {
 // Intrinsics don't have function bodies that are useful to
 // reduce. Additionally, intrinsics may have additional operand
-// constraints.
-return !F.isIntrinsic() && !O.shouldKeep();
+// constraints. But, do drop intrinsics that are not referenced.
+return (!F.isIntrinsic() || F.use_empty()) && !O.shouldKeep();
   });
 
   // Then, drop body of each of them. We want to batch this and do nothing else
@@ -51,7 +51,7 @@ static void extractFunctionsFromModule(const 
std::vector &ChunksToKeep,
   }
 }
 
-/// Counts the amount of non-declaration functions and prints their
+/// Counts the amount of functions and prints their
 /// respective name & index
 static int countFunctions(Module *Program) {
   // TODO: Silence index with --quiet flag
@@ -59,7 +59,7 @@ static int countFunctions(Module *Program) {
   errs() << "Function Index Reference:\n";
   int FunctionCount = 0;
   for (auto &F : *Program) {
-if (F.isIntrinsic())
+if (F.isIntrinsic() && !F.use_empty())
   continue;
 
 errs() << '\t' << ++FunctionCount << ": " << F.getName() << '\n';



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 7c8b806 - [SimplifyCFG][AMDGPU] AMDGPUUnifyDivergentExitNodes: SimplifyCFG isn't ready to preserve PostDomTree

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:46+03:00
New Revision: 7c8b8063b66c7b936d41a0c4069c506669e13115

URL: 
https://github.com/llvm/llvm-project/commit/7c8b8063b66c7b936d41a0c4069c506669e13115
DIFF: 
https://github.com/llvm/llvm-project/commit/7c8b8063b66c7b936d41a0c4069c506669e13115.diff

LOG: [SimplifyCFG][AMDGPU] AMDGPUUnifyDivergentExitNodes: SimplifyCFG isn't 
ready to preserve PostDomTree

There is a number of transforms in SimplifyCFG that take DomTree out of
DomTreeUpdater, and do updates manually. Until they are fixed,
user passes are unable to claim that PDT is preserved.

Note that the default for SimplifyCFG is still not to preserve DomTree,
so this is still effectively NFC.

Added: 


Modified: 
llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp 
b/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
index 96de1bd8f8d1..9ea8b3265b0d 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
@@ -89,7 +89,7 @@ void 
AMDGPUUnifyDivergentExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
 
   if (RequireAndPreserveDomTree) {
 AU.addPreserved();
-AU.addPreserved();
+// FIXME: preserve PostDominatorTreeWrapperPass
   }
 
   // No divergent values are changed, only blocks and branch edges.
@@ -369,7 +369,8 @@ bool AMDGPUUnifyDivergentExitNodes::runOnFunction(Function 
&F) {
 }
   }
 
-  DomTreeUpdater DTU(DT, &PDT, DomTreeUpdater::UpdateStrategy::Eager);
+  // FIXME: add PDT here once simplifycfg is ready.
+  DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
   if (RequireAndPreserveDomTree)
 DTU.applyUpdates(Updates);
   Updates.clear();

diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index e1f7ef636a89..825de4214c64 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -263,7 +263,11 @@ class SimplifyCFGOpt {
  const DataLayout &DL,
  SmallPtrSetImpl *LoopHeaders,
  const SimplifyCFGOptions &Opts)
-  : TTI(TTI), DTU(DTU), DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {}
+  : TTI(TTI), DTU(DTU), DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {
+assert((!DTU || !DTU->hasPostDomTree()) &&
+   "SimplifyCFG is not yet capable of maintaining validity of a "
+   "PostDomTree, so don't ask for it.");
+  }
 
   bool simplifyOnce(BasicBlock *BB);
   bool simplifyOnceImpl(BasicBlock *BB);



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 5799fc7 - [llvm-reduce] Refactor global variable delta pass

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:47+03:00
New Revision: 5799fc79c3fdbc81dd421afae38197009ad605c9

URL: 
https://github.com/llvm/llvm-project/commit/5799fc79c3fdbc81dd421afae38197009ad605c9
DIFF: 
https://github.com/llvm/llvm-project/commit/5799fc79c3fdbc81dd421afae38197009ad605c9.diff

LOG: [llvm-reduce] Refactor global variable delta pass

The limitation of the current pass that it skips initializer-less GV's
seems arbitrary, in all the reduced cases i (personally) looked at,
the globals weren't needed, yet they were kept.

So let's do two things:
1. allow reducing initializer-less globals
2. before reducing globals, reduce their initializers, much like we do function 
bodies

Added: 
llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.h

Modified: 
llvm/test/Reduce/remove-global-vars.ll
llvm/tools/llvm-reduce/CMakeLists.txt
llvm/tools/llvm-reduce/DeltaManager.h
llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp
llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.h

Removed: 
llvm/test/Reduce/Inputs/remove-global-vars.py



diff  --git a/llvm/test/Reduce/Inputs/remove-global-vars.py 
b/llvm/test/Reduce/Inputs/remove-global-vars.py
deleted file mode 100755
index 1ae8b0e6e76c..
--- a/llvm/test/Reduce/Inputs/remove-global-vars.py
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-
-InterestingVar = 0
-
-input = open(sys.argv[1], "r")
-for line in input:
-  i = line.find(';')
-  if i >= 0:
-line = line[:i]
-  if line.startswith("@interesting = global") or "@interesting" in line:
-InterestingVar += 1
-
-if InterestingVar == 4:
-  sys.exit(0) # interesting!
-
-sys.exit(1)

diff  --git a/llvm/test/Reduce/remove-global-vars.ll 
b/llvm/test/Reduce/remove-global-vars.ll
index 4fca4a1e6973..d078cad6b995 100644
--- a/llvm/test/Reduce/remove-global-vars.ll
+++ b/llvm/test/Reduce/remove-global-vars.ll
@@ -1,24 +1,47 @@
 ; Test that llvm-reduce can remove uninteresting Global Variables as well as
 ; their direct uses (which in turn are replaced with 'undef').
-;
-; RUN: llvm-reduce --test %python --test-arg %p/Inputs/remove-global-vars.py 
%s -o %t
-; RUN: cat %t | FileCheck -implicit-check-not=uninteresting %s
 
-; CHECK: @interesting = global
+; RUN: llvm-reduce --test FileCheck --test-arg 
--check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL 
--implicit-check-not=uninteresting %s
+
+; CHECK-INTERESTINGNESS: @interesting = {{.*}}global i32{{.*}}, align 4
+; CHECK-INTERESTINGNESS: @interesting2 = global i32 0, align 4
+; CHECK-INTERESTINGNESS: @interesting3 = {{.*}}global i32{{.*}}, align 4
+
+; CHECK-FINAL: @interesting = external global i32, align 4
+; CHECK-FINAL: @interesting2 = global i32 0, align 4
+; CHECK-FINAL: @interesting3 = external global i32, align 4
 @interesting = global i32 0, align 4
+@interesting2 = global i32 0, align 4
+@interesting3 = external global i32, align 4
 @uninteresting = global i32 1, align 4
+@uninteresting2 = external global i32, align 4
 
 define i32 @main() {
 entry:
   %0 = load i32, i32* @uninteresting, align 4
-  ; CHECK: store i32 undef, i32* @interesting, align 4
+
+  ; CHECK-INTERESTINGNESS: store i32 {{.*}}, i32* @interesting, align 4
+  ; CHECK-FINAL: store i32 undef, i32* @interesting, align 4
   store i32 %0, i32* @interesting, align 4
 
-  ; CHECK: load i32, i32* @interesting, align 4
+  ; CHECK-INTERESTINGNESS: store i32 {{.*}}, i32* @interesting3, align 4
+  ; CHECK-FINAL: store i32 undef, i32* @interesting3, align 4
+  store i32 %0, i32* @interesting3, align 4
+
+  ; CHECK-ALL: load i32, i32* @interesting, align 4
   %1 = load i32, i32* @interesting, align 4
   store i32 %1, i32* @uninteresting, align 4
 
-  ; CHECK: store i32 5, i32* @interesting, align 4
+  ; CHECK-ALL: load i32, i32* @interesting3, align 4
+  %2 = load i32, i32* @interesting3, align 4
+  store i32 %2, i32* @uninteresting2, align 4
+
+  ; CHECK-ALL: store i32 5, i32* @interesting, align 4
   store i32 5, i32* @interesting, align 4
+
+  ; CHECK-ALL: store i32 5, i32* @interesting3, align 4
+  store i32 5, i32* @interesting3, align 4
+
   ret i32 0
 }

diff  --git a/llvm/tools/llvm-reduce/CMakeLists.txt 
b/llvm/tools/llvm-reduce/CMakeLists.txt
index 3d635d178a71..b6db920e01bf 100644
--- a/llvm/tools/llvm-reduce/CMakeLists.txt
+++ b/llvm/tools/llvm-reduce/CMakeLists.txt
@@ -19,6 +19,7 @@ add_llvm_tool(llvm-reduce
   deltas/ReduceBasicBlocks.cpp
   deltas/ReduceFunctionBodies.cpp
   deltas/ReduceFunctions.cpp
+  deltas/ReduceGlobalVarInitializers.cpp
   deltas/ReduceGlobalVars.cpp
   deltas/ReduceInstructions.cpp
   deltas/ReduceMetadata.cpp

diff  --git a/llvm/tools/llvm-reduce/DeltaManager.h 
b/llvm/tools/llvm-reduce/DeltaManager.h
index 9c18da9793e7..18a6b0d363c4 100644
--

[llvm-branch-commits] [llvm] 6a3a8d1 - [SimplifyCFG] FoldValueComparisonIntoPredecessors(): fine-tune/fix DomTree preservation

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:48+03:00
New Revision: 6a3a8d17ebae0669e797ac22a2b2963b89ee12fa

URL: 
https://github.com/llvm/llvm-project/commit/6a3a8d17ebae0669e797ac22a2b2963b89ee12fa
DIFF: 
https://github.com/llvm/llvm-project/commit/6a3a8d17ebae0669e797ac22a2b2963b89ee12fa.diff

LOG: [SimplifyCFG] FoldValueComparisonIntoPredecessors(): fine-tune/fix DomTree 
preservation

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 825de4214c64..738e524fbfd9 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1113,8 +1113,6 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
 
   std::vector Updates;
 
-  Updates.push_back({DominatorTree::Delete, Pred, BB});
-
   // Figure out which 'cases' to copy from SI to PSI.
   std::vector BBCases;
   BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
@@ -1303,10 +1301,10 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   NewSI->setSuccessor(i, InfLoopBlock);
 }
 
-  if (InfLoopBlock) {
-Updates.push_back({DominatorTree::Delete, Pred, BB});
+  if (InfLoopBlock)
 Updates.push_back({DominatorTree::Insert, Pred, InfLoopBlock});
-  }
+
+  Updates.push_back({DominatorTree::Delete, Pred, BB});
 
   if (DTU)
 DTU->applyUpdatesPermissive(Updates);

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll
index 0d7718b865b2..10c02515e9d1 100644
--- 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll
+++ 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-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 < %s | 
FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 define dso_local i32 @readCBPandCoeffsFromNAL(i1 %c, i32 %x, i32 %y) 
local_unnamed_addr {
 ; CHECK-LABEL: @readCBPandCoeffsFromNAL(



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] eda5030 - [NFC][SimplifyCFG] Add test for switch creation where we fail to maintain DomTree

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:47+03:00
New Revision: eda50309f5930d249fef4747efb9a047d239ba05

URL: 
https://github.com/llvm/llvm-project/commit/eda50309f5930d249fef4747efb9a047d239ba05
DIFF: 
https://github.com/llvm/llvm-project/commit/eda50309f5930d249fef4747efb9a047d239ba05.diff

LOG: [NFC][SimplifyCFG] Add test for switch creation where we fail to maintain 
DomTree

Reduced from vanilla test-suite

Added: 

llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll
new file mode 100644
index ..0d7718b865b2
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase.ll
@@ -0,0 +1,49 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 < %s | 
FileCheck %s
+
+define dso_local i32 @readCBPandCoeffsFromNAL(i1 %c, i32 %x, i32 %y) 
local_unnamed_addr {
+; CHECK-LABEL: @readCBPandCoeffsFromNAL(
+; CHECK-NEXT:  if.end:
+; CHECK-NEXT:br i1 [[C:%.*]], label [[IF_END80:%.*]], label 
[[IF_THEN64:%.*]]
+; CHECK:   if.then64:
+; CHECK-NEXT:[[MERGE:%.*]] = phi i32 [ [[Y:%.*]], [[IF_END:%.*]] ], [ 1, 
[[IF_END172237:%.*]] ], [ 0, [[IF_END80]] ], [ 0, [[IF_END80]] ]
+; CHECK-NEXT:ret i32 [[MERGE]]
+; CHECK:   if.end80:
+; CHECK-NEXT:switch i32 [[X:%.*]], label [[INFLOOP:%.*]] [
+; CHECK-NEXT:i32 10, label [[IF_END172237]]
+; CHECK-NEXT:i32 14, label [[IF_END172237]]
+; CHECK-NEXT:i32 9, label [[IF_THEN64]]
+; CHECK-NEXT:i32 12, label [[IF_THEN64]]
+; CHECK-NEXT:]
+; CHECK:   if.end172237:
+; CHECK-NEXT:br label [[IF_THEN64]]
+; CHECK:   infloop:
+; CHECK-NEXT:br label [[INFLOOP]]
+;
+if.end:
+  br i1 %c, label %if.end80, label %if.then64
+
+if.then64:; preds = %if.end
+  ret i32 %y
+
+if.end80: ; preds = %if.end
+  switch i32 %x, label %lor.lhs.false89 [
+  i32 10, label %if.end172237
+  i32 14, label %if.end172237
+  i32 9, label %if.end172
+  ]
+
+lor.lhs.false89:  ; preds = %lor.lhs.false89, 
%if.end80
+  %cmp91 = icmp eq i32 %x, 12
+  br i1 %cmp91, label %if.end172, label %lor.lhs.false89
+
+if.end172:; preds = %lor.lhs.false89, 
%if.end80
+  br label %if.end239
+
+if.end172237: ; preds = %if.end80, 
%if.end80
+  br label %if.end239
+
+if.end239:; preds = %if.end172237, 
%if.end172
+  %cbp.0 = phi i32 [ 1, %if.end172237 ], [ 0, %if.end172 ]
+  ret i32 %cbp.0
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] a001393 - [NFC][SimplifyCFG] Add another test for switch creation where we fail to maintain DomTree

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:48+03:00
New Revision: a0013934b6a194f0ecc4d98118920326b12d07a0

URL: 
https://github.com/llvm/llvm-project/commit/a0013934b6a194f0ecc4d98118920326b12d07a0
DIFF: 
https://github.com/llvm/llvm-project/commit/a0013934b6a194f0ecc4d98118920326b12d07a0.diff

LOG: [NFC][SimplifyCFG] Add another test for switch creation where we fail to 
maintain DomTree

Added: 

llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll
 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll
new file mode 100644
index ..79857cccfbe4
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll
@@ -0,0 +1,80 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 < %s | 
FileCheck %s
+
+declare void @widget()
+declare i16 @baz()
+declare void @snork()
+declare void @spam()
+
+define void @zot() local_unnamed_addr align 2 personality i8* undef {
+; CHECK-LABEL: @zot(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:invoke void @widget()
+; CHECK-NEXT:to label [[BB14:%.*]] unwind label [[BB21:%.*]]
+; CHECK:   bb14:
+; CHECK-NEXT:[[I0:%.*]] = invoke i16 @baz()
+; CHECK-NEXT:to label [[BB15:%.*]] unwind label [[BB25:%.*]]
+; CHECK:   bb15:
+; CHECK-NEXT:switch i16 [[I0]], label [[BB19:%.*]] [
+; CHECK-NEXT:i16 42, label [[BB23:%.*]]
+; CHECK-NEXT:i16 21330, label [[BB23]]
+; CHECK-NEXT:]
+; CHECK:   bb19:
+; CHECK-NEXT:invoke void @snork()
+; CHECK-NEXT:to label [[BB20:%.*]] unwind label [[BB25]]
+; CHECK:   bb20:
+; CHECK-NEXT:unreachable
+; CHECK:   bb21:
+; CHECK-NEXT:[[I22:%.*]] = landingpad { i8*, i32 }
+; CHECK-NEXT:cleanup
+; CHECK-NEXT:ret void
+; CHECK:   bb23:
+; CHECK-NEXT:invoke void @spam()
+; CHECK-NEXT:to label [[BB24:%.*]] unwind label [[BB25]]
+; CHECK:   bb24:
+; CHECK-NEXT:ret void
+; CHECK:   bb25:
+; CHECK-NEXT:[[I26:%.*]] = landingpad { i8*, i32 }
+; CHECK-NEXT:cleanup
+; CHECK-NEXT:br label [[BB24]]
+;
+bb:
+  invoke void @widget()
+  to label %bb14 unwind label %bb21
+
+bb14: ; preds = %bb
+  %i0 = invoke i16 @baz()
+  to label %bb15 unwind label %bb25
+
+bb15: ; preds = %bb14
+  %i16 = icmp eq i16 %i0, 42
+  br i1 %i16, label %bb23, label %bb17
+
+bb17: ; preds = %bb15
+  %i18 = icmp eq i16 %i0, 21330
+  br i1 %i18, label %bb23, label %bb19
+
+bb19: ; preds = %bb17
+  invoke void @snork()
+  to label %bb20 unwind label %bb25
+
+bb20: ; preds = %bb19
+  unreachable
+
+bb21: ; preds = %bb
+  %i22 = landingpad { i8*, i32 }
+  cleanup
+  ret void
+
+bb23: ; preds = %bb17, %bb15
+  invoke void @spam()
+  to label %bb24 unwind label %bb25
+
+bb24: ; preds = %bb25, %bb23
+  ret void
+
+bb25: ; preds = %bb23, %bb19, %bb14
+  %i26 = landingpad { i8*, i32 }
+  cleanup
+  br label %bb24
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 5fa241a - [SimplifyCFG] FoldValueComparisonIntoPredecessors(): fine-tune/fix DomTree preservation, take 2

2021-01-02 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-03T01:45:48+03:00
New Revision: 5fa241a6571c79c1cd0c0d9e7f87e5e361e2dab4

URL: 
https://github.com/llvm/llvm-project/commit/5fa241a6571c79c1cd0c0d9e7f87e5e361e2dab4
DIFF: 
https://github.com/llvm/llvm-project/commit/5fa241a6571c79c1cd0c0d9e7f87e5e361e2dab4.diff

LOG: [SimplifyCFG] FoldValueComparisonIntoPredecessors(): fine-tune/fix DomTree 
preservation, take 2

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 738e524fbfd9..d581c97f9fea 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1176,7 +1176,8 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
 // Reconstruct the new switch statement we will be building.
 if (PredDefault != BBDefault) {
   PredDefault->removePredecessor(Pred);
-  Updates.push_back({DominatorTree::Delete, Pred, PredDefault});
+  if (PredDefault != BB)
+Updates.push_back({DominatorTree::Delete, Pred, PredDefault});
   PredDefault = BBDefault;
   NewSuccessors.push_back(BBDefault);
 }

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll
 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll
index 79857cccfbe4..a3f7ca5e1741 100644
--- 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.ll
+++ 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-domtree-preservation-edgecase-2.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 < %s | 
FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 declare void @widget()
 declare i16 @baz()



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 4fc9080 - [NFC][SimplifyCFG] Add a test where we fail to preserve DomTree validity

2021-01-03 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-04T01:02:01+03:00
New Revision: 4fc908025fd003a7517e392f35389364fb60500d

URL: 
https://github.com/llvm/llvm-project/commit/4fc908025fd003a7517e392f35389364fb60500d
DIFF: 
https://github.com/llvm/llvm-project/commit/4fc908025fd003a7517e392f35389364fb60500d.diff

LOG: [NFC][SimplifyCFG] Add a test where we fail to preserve DomTree validity

Added: 

llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll
new file mode 100644
index ..752de63c2933
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 < %s | 
FileCheck %s
+
+declare void @widget()
+declare void @baz(i8)
+declare void @snork()
+declare void @spam(i8)
+declare void @zot()
+
+define void @wombat(i64 %arg, i1 %arg1) {
+; CHECK-LABEL: @wombat(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:[[COND:%.*]] = icmp eq i64 [[ARG:%.*]], 0
+; CHECK-NEXT:br i1 [[COND]], label [[BB4:%.*]], label [[BB2:%.*]]
+; CHECK:   bb2:
+; CHECK-NEXT:call void @widget()
+; CHECK-NEXT:br label [[BB4]]
+; CHECK:   bb4:
+; CHECK-NEXT:[[TMP:%.*]] = phi i8 [ 0, [[BB:%.*]] ], [ 1, [[BB2]] ]
+; CHECK-NEXT:call void @baz(i8 [[TMP]])
+; CHECK-NEXT:call void @snork()
+; CHECK-NEXT:call void @spam(i8 4)
+; CHECK-NEXT:ret void
+;
+bb:
+  switch i64 %arg, label %bb2 [
+  i64 0, label %bb3
+  ]
+
+bb2:  ; preds = %bb
+  call void @widget()
+  br label %bb3
+
+bb3:  ; preds = %bb2, %bb
+  %tmp = phi i8 [ 0, %bb ], [ 1, %bb2 ]
+  br label %bb4
+
+bb4:  ; preds = %bb3
+  call void @baz(i8 %tmp)
+  %tmp5 = select i1 %arg1, i64 6, i64 3
+  switch i64 %tmp5, label %bb7 [
+  i64 1, label %bb6
+  i64 0, label %bb8
+  ]
+
+bb6:  ; preds = %bb4
+  call void @zot()
+  br label %bb8
+
+bb7:  ; preds = %bb4
+  call void @snork()
+  br label %bb8
+
+bb8:  ; preds = %bb7, %bb6, %bb4
+  %tmp9 = phi i8 [ 2, %bb4 ], [ 3, %bb6 ], [ 4, %bb7 ]
+  call void @spam(i8 %tmp9)
+  ret void
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 70935b9 - [NFC][SimplifyCFG] SimplifyTerminatorOnSelect(): pull out OldTerm->getParent() into a variable

2021-01-03 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-04T01:02:02+03:00
New Revision: 70935b9595a410794882d043726a1aad38d44ebd

URL: 
https://github.com/llvm/llvm-project/commit/70935b9595a410794882d043726a1aad38d44ebd
DIFF: 
https://github.com/llvm/llvm-project/commit/70935b9595a410794882d043726a1aad38d44ebd.diff

LOG: [NFC][SimplifyCFG] SimplifyTerminatorOnSelect(): pull out 
OldTerm->getParent() into a variable

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d581c97f9fea..d4f77757a290 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3848,6 +3848,8 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 BasicBlock *FalseBB,
 uint32_t TrueWeight,
 uint32_t FalseWeight) {
+  auto *BB = OldTerm->getParent();
+
   // Remove any superfluous successor edges from the CFG.
   // First, figure out which successors to preserve.
   // If TrueBB and FalseBB are equal, only try to preserve one copy of that
@@ -3865,9 +3867,9 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 else if (Succ == KeepEdge2)
   KeepEdge2 = nullptr;
 else {
-  Succ->removePredecessor(OldTerm->getParent(),
+  Succ->removePredecessor(BB,
   /*KeepOneInputPHIs=*/true);
-  Updates.push_back({DominatorTree::Delete, OldTerm->getParent(), Succ});
+  Updates.push_back({DominatorTree::Delete, BB, Succ});
 }
   }
 
@@ -3880,13 +3882,13 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
   // We were only looking for one successor, and it was present.
   // Create an unconditional branch to it.
   Builder.CreateBr(TrueBB);
-  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), TrueBB});
+  Updates.push_back({DominatorTree::Insert, BB, TrueBB});
 } else {
   // We found both of the successors we were looking for.
   // Create a conditional branch sharing the condition of the select.
   BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
-  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), TrueBB});
-  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), 
FalseBB});
+  Updates.push_back({DominatorTree::Insert, BB, TrueBB});
+  Updates.push_back({DominatorTree::Insert, BB, FalseBB});
   if (TrueWeight != FalseWeight)
 setBranchWeights(NewBI, TrueWeight, FalseWeight);
 }
@@ -3901,11 +3903,11 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 if (!KeepEdge1) {
   // Only TrueBB was found.
   Builder.CreateBr(TrueBB);
-  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), TrueBB});
+  Updates.push_back({DominatorTree::Insert, BB, TrueBB});
 } else {
   // Only FalseBB was found.
   Builder.CreateBr(FalseBB);
-  Updates.push_back({DominatorTree::Insert, OldTerm->getParent(), 
FalseBB});
+  Updates.push_back({DominatorTree::Insert, BB, FalseBB});
 }
   }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] a768494 - [SimplifyCFG] SimplifyTerminatorOnSelect(): fix/tune DomTree updates

2021-01-03 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-04T01:02:02+03:00
New Revision: a7684940f0e4eafb1bafc75cfb0e620ee358abd4

URL: 
https://github.com/llvm/llvm-project/commit/a7684940f0e4eafb1bafc75cfb0e620ee358abd4
DIFF: 
https://github.com/llvm/llvm-project/commit/a7684940f0e4eafb1bafc75cfb0e620ee358abd4.diff

LOG: [SimplifyCFG] SimplifyTerminatorOnSelect(): fix/tune DomTree updates

We only need to remove non-TrueBB/non-FalseBB successors,
and we only need to do that once. We don't need to insert
any new edges, because no new successors will be added.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d4f77757a290..f02ff2252f7d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3849,7 +3849,6 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 uint32_t TrueWeight,
 uint32_t FalseWeight) {
   auto *BB = OldTerm->getParent();
-
   // Remove any superfluous successor edges from the CFG.
   // First, figure out which successors to preserve.
   // If TrueBB and FalseBB are equal, only try to preserve one copy of that
@@ -3857,7 +3856,7 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
   BasicBlock *KeepEdge1 = TrueBB;
   BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
 
-  SmallVector Updates;
+  SmallSetVector RemovedSuccessors;
 
   // Then remove the rest.
   for (BasicBlock *Succ : successors(OldTerm)) {
@@ -3869,7 +3868,9 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 else {
   Succ->removePredecessor(BB,
   /*KeepOneInputPHIs=*/true);
-  Updates.push_back({DominatorTree::Delete, BB, Succ});
+
+  if (Succ != TrueBB && Succ != FalseBB)
+RemovedSuccessors.insert(Succ);
 }
   }
 
@@ -3882,13 +3883,10 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
   // We were only looking for one successor, and it was present.
   // Create an unconditional branch to it.
   Builder.CreateBr(TrueBB);
-  Updates.push_back({DominatorTree::Insert, BB, TrueBB});
 } else {
   // We found both of the successors we were looking for.
   // Create a conditional branch sharing the condition of the select.
   BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
-  Updates.push_back({DominatorTree::Insert, BB, TrueBB});
-  Updates.push_back({DominatorTree::Insert, BB, FalseBB});
   if (TrueWeight != FalseWeight)
 setBranchWeights(NewBI, TrueWeight, FalseWeight);
 }
@@ -3903,17 +3901,22 @@ bool 
SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
 if (!KeepEdge1) {
   // Only TrueBB was found.
   Builder.CreateBr(TrueBB);
-  Updates.push_back({DominatorTree::Insert, BB, TrueBB});
 } else {
   // Only FalseBB was found.
   Builder.CreateBr(FalseBB);
-  Updates.push_back({DominatorTree::Insert, BB, FalseBB});
 }
   }
 
   EraseTerminatorAndDCECond(OldTerm);
-  if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+
+  if (DTU) {
+SmallVector Updates;
+Updates.reserve(RemovedSuccessors.size());
+for (auto *RemovedSuccessor : RemovedSuccessors)
+  Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
+DTU->applyUpdates(Updates);
+  }
+
   return true;
 }
 

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll
index 752de63c2933..792a1a534a93 100644
--- 
a/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-domtree-preservation-edgecase.ll
+++ 
b/llvm/test/Transforms/SimplifyCFG/SimplifyTerminatorOnSelect-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 < %s | 
FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 declare void @widget()
 declare void @baz(i8)



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 98cd1c3 - [NFC][SimplifyCFG] Hoist 'original' DomTree verification from simplifyOnce() into run()

2021-01-03 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-04T01:02:02+03:00
New Revision: 98cd1c33e3c2c3cfee36fb0fea3285fda06224d3

URL: 
https://github.com/llvm/llvm-project/commit/98cd1c33e3c2c3cfee36fb0fea3285fda06224d3
DIFF: 
https://github.com/llvm/llvm-project/commit/98cd1c33e3c2c3cfee36fb0fea3285fda06224d3.diff

LOG: [NFC][SimplifyCFG] Hoist 'original' DomTree verification from 
simplifyOnce() into run()

This is NFC since SimplifyCFG still currently defaults to not preserving 
DomTree.

SimplifyCFGOpt::simplifyOnce() is only be called from SimplifyCFGOpt::run(),
and can not be called externally, since SimplifyCFGOpt is defined in .cpp
This avoids some needless verifications, and is thus a bit faster
without sacrificing precision.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index f02ff2252f7d..a4faf22b8294 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6675,11 +6675,6 @@ bool SimplifyCFGOpt::simplifyOnceImpl(BasicBlock *BB) {
 }
 
 bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
-  assert((!RequireAndPreserveDomTree ||
-  (DTU &&
-   DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full))) 
&&
- "Original domtree is invalid?");
-
   bool Changed = simplifyOnceImpl(BB);
 
   assert((!RequireAndPreserveDomTree ||
@@ -6691,6 +6686,11 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
 }
 
 bool SimplifyCFGOpt::run(BasicBlock *BB) {
+  assert((!RequireAndPreserveDomTree ||
+  (DTU &&
+   DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full))) 
&&
+ "Original domtree is invalid?");
+
   bool Changed = false;
 
   // Repeated simplify BB as long as resimplification is requested.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 09b3f3f - [benchmark] Fixed a build error when using CMake 3.15.1 + NDK-R20

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: AnZhong Huang
Date: 2021-01-04T11:00:57+03:00
New Revision: 09b3f3f22cbe159a737c44b2e78de08bbbfa5be3

URL: 
https://github.com/llvm/llvm-project/commit/09b3f3f22cbe159a737c44b2e78de08bbbfa5be3
DIFF: 
https://github.com/llvm/llvm-project/commit/09b3f3f22cbe159a737c44b2e78de08bbbfa5be3.diff

LOG: [benchmark] Fixed a build error when using CMake 3.15.1 + NDK-R20

std::decay_t used by llvm/utils/benchmark/include/benchmark/benchmark.h is a 
c++14 feature, but the CMakelist uses c++11, it's the root-cause of build error.

There are two options to fix the error.
1) change the CMakelist to support c++14.
2) change std::decay_t to std::decay, it's what the patch done.

This bug can only be reproduced by CMake 3.15, we didn't observer the bug 
with CMake 3.16. But based on the code's logic, it's an obvious bug of LLVM.

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D93794

Added: 


Modified: 
llvm/utils/benchmark/include/benchmark/benchmark.h

Removed: 




diff  --git a/llvm/utils/benchmark/include/benchmark/benchmark.h 
b/llvm/utils/benchmark/include/benchmark/benchmark.h
index ab61c46e9386..3b535f1b7d52 100644
--- a/llvm/utils/benchmark/include/benchmark/benchmark.h
+++ b/llvm/utils/benchmark/include/benchmark/benchmark.h
@@ -990,7 +990,7 @@ inline internal::Benchmark* RegisterBenchmark(const char* 
name,
 #ifdef BENCHMARK_HAS_CXX11
 template 
 internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn) {
-  using BenchType = internal::LambdaBenchmark>;
+  using BenchType = internal::LambdaBenchmark::type>;
   return internal::RegisterBenchmarkInternal(
   ::new BenchType(name, std::forward(fn)));
 }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] b4f519b - [NFCI] DwarfEHPrepare: update DomTree in non-permissive mode, when present

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-05T01:26:36+03:00
New Revision: b4f519bddda853443405d21728154c481837e18b

URL: 
https://github.com/llvm/llvm-project/commit/b4f519bddda853443405d21728154c481837e18b
DIFF: 
https://github.com/llvm/llvm-project/commit/b4f519bddda853443405d21728154c481837e18b.diff

LOG: [NFCI] DwarfEHPrepare: update DomTree in non-permissive mode, when present

Being stricter will catch issues that would be just papered over
in permissive mode, and is likely faster.

Added: 


Modified: 
llvm/lib/CodeGen/DwarfEHPrepare.cpp

Removed: 




diff  --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp 
b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index c1b764214546..97e0162f35a1 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -243,7 +243,7 @@ bool DwarfEHPrepare::InsertUnwindResumeCalls() {
   new UnreachableInst(Ctx, UnwindBB);
 
   if (DTU && RequireAndPreserveDomTree)
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 
   return true;
 }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 3fb5722 - [NFCI] SimplifyCFG: switch to non-permissive DomTree updates, where possible

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-05T01:26:36+03:00
New Revision: 3fb57222c4c0db02f13f32579fb83d0d488becad

URL: 
https://github.com/llvm/llvm-project/commit/3fb57222c4c0db02f13f32579fb83d0d488becad
DIFF: 
https://github.com/llvm/llvm-project/commit/3fb57222c4c0db02f13f32579fb83d0d488becad.diff

LOG: [NFCI] SimplifyCFG: switch to non-permissive DomTree updates, where 
possible

Notably, this doesn't switch *every* case, remaining cases
don't actually pass sanity checks in non-permissve mode,
and therefore require further analysis.

Note that SimplifyCFG still defaults to not preserving DomTree by default,
so this is effectively a NFC change.

Added: 


Modified: 
llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp 
b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
index 3efdc0e9ea86..2c3454c46b30 100644
--- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
+++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
@@ -77,6 +77,7 @@ STATISTIC(NumSimpl, "Number of blocks simplified");
 
 /// If we have more than one empty (other than phi node) return blocks,
 /// merge them together to promote recursive block merging.
+// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 static bool mergeEmptyReturnBlocks(Function &F, DomTreeUpdater *DTU) {
   bool Changed = false;
 

diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a4faf22b8294..567b2e02b71c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -872,6 +872,7 @@ static void setBranchWeights(Instruction *I, uint32_t 
TrueWeight,
 /// also a value comparison with the same value, and if that comparison
 /// determines the outcome of this comparison. If so, simplify TI. This does a
 /// very limited form of jump threading.
+// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
 Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
   Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
@@ -924,7 +925,7 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
   EraseTerminatorAndDCECond(TI);
 
   if (DTU)
-DTU->applyUpdatesPermissive(
+DTU->applyUpdates(
 {{DominatorTree::Delete, PredDef, ThisCases[0].Dest}});
 
   return true;
@@ -956,7 +957,7 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
   if (I.second == 0)
 Updates.push_back({DominatorTree::Delete, PredDef, I.first});
 if (DTU)
-  DTU->applyUpdatesPermissive(Updates);
+  DTU->applyUpdates(Updates);
 
 LLVM_DEBUG(dbgs() << "Leaving: " << *TI << "\n");
 return true;
@@ -1080,6 +1081,7 @@ static void FitWeights(MutableArrayRef Weights) 
{
 /// (either a switch or a branch on "X == c").
 /// See if any of the predecessors of the terminator block are value 
comparisons
 /// on the same value.  If so, and if safe to do so, fold them together.
+// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
  IRBuilder<> &Builder) 
{
   BasicBlock *BB = TI->getParent();
@@ -1554,7 +1556,7 @@ bool SimplifyCFGOpt::HoistThenElseCodeToIf(BranchInst *BI,
 
   EraseTerminatorAndDCECond(BI);
   if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
   return Changed;
 }
 
@@ -2488,7 +2490,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, 
DomTreeUpdater *DTU,
 Updates.push_back({DominatorTree::Insert, PredBB, EdgeBB});
 
 if (DTU)
-  DTU->applyUpdatesPermissive(Updates);
+  DTU->applyUpdates(Updates);
 
 // Recurse, simplifying any other constants.
 return FoldCondBranchOnPHI(BI, DTU, DL, AC) || true;
@@ -2660,7 +2662,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const 
TargetTransformInfo &TTI,
 
   OldTI->eraseFromParent();
   if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 
   return true;
 }
@@ -2668,6 +2670,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const 
TargetTransformInfo &TTI,
 /// If we found a conditional branch that goes to two returning blocks,
 /// try to merge them together into one return,
 /// introducing a select if the return values disagree.
+// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
 IRBuilder<> &Builder) {
   auto *BB = BI->getParent();
@@ -3169,7 +3172,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, 
DomTreeUpdater *DTU,
   }
 }
 // Update PHI Node.
- 

[llvm-branch-commits] [llvm] ed9de61 - [SimplifyCFGPass] mergeEmptyReturnBlocks(): switch to non-permissive DomTree updates

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-05T01:26:36+03:00
New Revision: ed9de61cc3e280f84e3f0f98a49af21c7e59c4c9

URL: 
https://github.com/llvm/llvm-project/commit/ed9de61cc3e280f84e3f0f98a49af21c7e59c4c9
DIFF: 
https://github.com/llvm/llvm-project/commit/ed9de61cc3e280f84e3f0f98a49af21c7e59c4c9.diff

LOG: [SimplifyCFGPass] mergeEmptyReturnBlocks(): switch to non-permissive 
DomTree updates

... which requires not inserting an edge that already exists.

Added: 


Modified: 
llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp 
b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
index 2c3454c46b30..c0edde8648f5 100644
--- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
+++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
@@ -77,7 +77,6 @@ STATISTIC(NumSimpl, "Number of blocks simplified");
 
 /// If we have more than one empty (other than phi node) return blocks,
 /// merge them together to promote recursive block merging.
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 static bool mergeEmptyReturnBlocks(Function &F, DomTreeUpdater *DTU) {
   bool Changed = false;
 
@@ -143,7 +142,10 @@ static bool mergeEmptyReturnBlocks(Function &F, 
DomTreeUpdater *DTU) {
   if (DTU) {
 for (auto *Predecessor : predecessors(&BB)) {
   Updates.push_back({DominatorTree::Delete, Predecessor, &BB});
-  Updates.push_back({DominatorTree::Insert, Predecessor, RetBlock});
+  // But, iff Predecessor already branches to RetBlock,
+  // don't (re-)add DomTree edge, because it already exists.
+  if (!is_contained(successors(Predecessor), RetBlock))
+Updates.push_back({DominatorTree::Insert, Predecessor, RetBlock});
 }
   }
   BB.replaceAllUsesWith(RetBlock);
@@ -176,7 +178,7 @@ static bool mergeEmptyReturnBlocks(Function &F, 
DomTreeUpdater *DTU) {
   }
 
   if (DTU) {
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 for (auto *BB : DeadBlocks)
   DTU->deleteBB(BB);
   } else {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 110b3d7 - [SimplifyCFG] SimplifyEqualityComparisonWithOnlyPredecessor(): switch to non-permissive DomTree updates

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-05T01:26:37+03:00
New Revision: 110b3d7855ef71a7d43a0779b2e1c32e1a31daae

URL: 
https://github.com/llvm/llvm-project/commit/110b3d7855ef71a7d43a0779b2e1c32e1a31daae
DIFF: 
https://github.com/llvm/llvm-project/commit/110b3d7855ef71a7d43a0779b2e1c32e1a31daae.diff

LOG: [SimplifyCFG] SimplifyEqualityComparisonWithOnlyPredecessor(): switch to 
non-permissive DomTree updates

... which requires not deleting an edge that just got deleted.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 60fa8a876b53..a61e48d84f01 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -872,7 +872,6 @@ static void setBranchWeights(Instruction *I, uint32_t 
TrueWeight,
 /// also a value comparison with the same value, and if that comparison
 /// determines the outcome of this comparison. If so, simplify TI. This does a
 /// very limited form of jump threading.
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
 Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
   Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
@@ -988,14 +987,14 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
   if (!TheRealDest)
 TheRealDest = ThisDef;
 
-  SmallVector Updates;
+  SmallSetVector RemovedSuccs;
 
   // Remove PHI node entries for dead edges.
   BasicBlock *CheckEdge = TheRealDest;
   for (BasicBlock *Succ : successors(TIBB))
 if (Succ != CheckEdge) {
+  RemovedSuccs.insert(Succ);
   Succ->removePredecessor(TIBB);
-  Updates.push_back({DominatorTree::Delete, TIBB, Succ});
 } else
   CheckEdge = nullptr;
 
@@ -1008,8 +1007,13 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
 << "\n");
 
   EraseTerminatorAndDCECond(TI);
-  if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+  if (DTU) {
+SmallVector Updates;
+Updates.reserve(RemovedSuccs.size());
+for (auto *RemovedSucc : RemovedSuccs)
+  Updates.push_back({DominatorTree::Delete, TIBB, RemovedSucc});
+DTU->applyUpdates(Updates);
+  }
   return true;
 }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] a8604e3 - [SimplifyCFG] simplifyIndirectBr(): switch to non-permissive DomTree updates

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-05T01:26:36+03:00
New Revision: a8604e3d5b7112da11508f100805d65a4eddeb33

URL: 
https://github.com/llvm/llvm-project/commit/a8604e3d5b7112da11508f100805d65a4eddeb33
DIFF: 
https://github.com/llvm/llvm-project/commit/a8604e3d5b7112da11508f100805d65a4eddeb33.diff

LOG: [SimplifyCFG] simplifyIndirectBr(): switch to non-permissive DomTree 
updates

... which requires not deleting an edge that just got deleted.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 567b2e02b71c..60fa8a876b53 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6218,19 +6218,18 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, 
IRBuilder<> &Builder) {
   return false;
 }
 
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
   BasicBlock *BB = IBI->getParent();
   bool Changed = false;
 
   // Eliminate redundant destinations.
-  std::vector Updates;
   SmallPtrSet Succs;
+  SmallSetVector RemovedSuccs;
   for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
 BasicBlock *Dest = IBI->getDestination(i);
 if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
   if (!Dest->hasAddressTaken())
-Updates.push_back({DominatorTree::Delete, BB, Dest});
+RemovedSuccs.insert(Dest);
   Dest->removePredecessor(BB);
   IBI->removeDestination(i);
   --i;
@@ -6239,9 +6238,13 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst 
*IBI) {
 }
   }
 
-  if (DTU)
-DTU->applyUpdatesPermissive(Updates);
-  Updates.clear();
+  if (DTU) {
+std::vector Updates;
+Updates.reserve(RemovedSuccs.size());
+for (auto *RemovedSucc : RemovedSuccs)
+  Updates.push_back({DominatorTree::Delete, BB, RemovedSucc});
+DTU->applyUpdates(Updates);
+  }
 
   if (IBI->getNumDestinations() == 0) {
 // If the indirectbr has no successors, change it to unreachable.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 32c47eb - [SimplifyCFG] SimplifyCondBranchToTwoReturns(): switch to non-permissive DomTree updates

2021-01-04 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-05T01:26:37+03:00
New Revision: 32c47ebef18d904445ce909e4a6922ffbfe4053f

URL: 
https://github.com/llvm/llvm-project/commit/32c47ebef18d904445ce909e4a6922ffbfe4053f
DIFF: 
https://github.com/llvm/llvm-project/commit/32c47ebef18d904445ce909e4a6922ffbfe4053f.diff

LOG: [SimplifyCFG] SimplifyCondBranchToTwoReturns(): switch to non-permissive 
DomTree updates

... which requires not deleting an edge that just got deleted,
because we could be dealing with a block that didn't go through
ConstantFoldTerminator() yet, and thus has a degenerate cond br
with matching true/false destinations.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a61e48d84f01..9f808278d899 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2674,13 +2674,13 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const 
TargetTransformInfo &TTI,
 /// If we found a conditional branch that goes to two returning blocks,
 /// try to merge them together into one return,
 /// introducing a select if the return values disagree.
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
 IRBuilder<> &Builder) {
   auto *BB = BI->getParent();
   assert(BI->isConditional() && "Must be a conditional branch");
   BasicBlock *TrueSucc = BI->getSuccessor(0);
   BasicBlock *FalseSucc = BI->getSuccessor(1);
+  // NOTE: destinations may match, this could be degenerate uncond branch.
   ReturnInst *TrueRet = cast(TrueSucc->getTerminator());
   ReturnInst *FalseRet = cast(FalseSucc->getTerminator());
 
@@ -2702,8 +2702,11 @@ bool 
SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
 Builder.CreateRetVoid();
 EraseTerminatorAndDCECond(BI);
 if (DTU) {
-  DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, TrueSucc},
-   {DominatorTree::Delete, BB, FalseSucc}});
+  SmallVector Updates;
+  Updates.push_back({DominatorTree::Delete, BB, TrueSucc});
+  if (TrueSucc != FalseSucc)
+Updates.push_back({DominatorTree::Delete, BB, FalseSucc});
+  DTU->applyUpdates(Updates);
 }
 return true;
   }
@@ -2761,10 +2764,12 @@ bool 
SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
 << *TrueSucc << "\nFALSEBLOCK: " << *FalseSucc);
 
   EraseTerminatorAndDCECond(BI);
-
   if (DTU) {
-DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, TrueSucc},
- {DominatorTree::Delete, BB, FalseSucc}});
+SmallVector Updates;
+Updates.push_back({DominatorTree::Delete, BB, TrueSucc});
+if (TrueSucc != FalseSucc)
+  Updates.push_back({DominatorTree::Delete, BB, FalseSucc});
+DTU->applyUpdates(Updates);
   }
 
   return true;



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] f985356 - [SimplifyCFG] simplifyUnreachable(): switch to non-permissive DomTree updates

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:36+03:00
New Revision: f98535686e3c1fa76986337639df1636282692c9

URL: 
https://github.com/llvm/llvm-project/commit/f98535686e3c1fa76986337639df1636282692c9
DIFF: 
https://github.com/llvm/llvm-project/commit/f98535686e3c1fa76986337639df1636282692c9.diff

LOG: [SimplifyCFG] simplifyUnreachable(): switch to non-permissive DomTree 
updates

... which requires not removing a DomTree edge if the switch's default
still points at that destination, because it can't be removed;
... and not processing the same predecessor more than once.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 9f808278d899..d01d9512212c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4616,7 +4616,6 @@ bool SimplifyCFGOpt::simplifyReturn(ReturnInst *RI, 
IRBuilder<> &Builder) {
   return false;
 }
 
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
   BasicBlock *BB = UI->getParent();
 
@@ -4678,7 +4677,7 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst 
*UI) {
 
   std::vector Updates;
 
-  SmallVector Preds(predecessors(BB));
+  SmallSetVector Preds(pred_begin(BB), pred_end(BB));
   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
 auto *Predecessor = Preds[i];
 Instruction *TI = Predecessor->getTerminator();
@@ -4718,7 +4717,9 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst 
*UI) {
 e = SU->case_end();
 Changed = true;
   }
-  Updates.push_back({DominatorTree::Delete, Predecessor, BB});
+  // Note that the default destination can't be removed!
+  if (SI->getDefaultDest() != BB)
+Updates.push_back({DominatorTree::Delete, Predecessor, BB});
 } else if (auto *II = dyn_cast(TI)) {
   if (II->getUnwindDest() == BB) {
 if (DTU)
@@ -4785,7 +4786,7 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst 
*UI) {
   }
 
   if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 
   // If this block is now dead, remove it.
   if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 3460719 - [NFC][SimplifyCFG] Add a test with same-destination condidional branch

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:36+03:00
New Revision: 3460719f583583e6990ec5c1b2a718cc01797bf7

URL: 
https://github.com/llvm/llvm-project/commit/3460719f583583e6990ec5c1b2a718cc01797bf7
DIFF: 
https://github.com/llvm/llvm-project/commit/3460719f583583e6990ec5c1b2a718cc01797bf7.diff

LOG: [NFC][SimplifyCFG] Add a test with same-destination condidional branch

Reported by Mikael Holmén as post-commit feedback on
https://reviews.llvm.org/rG2d07414ee5f74a09fb89723b4a9bb0818bdc2e18#968162

Added: 

llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll

Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d01d9512212c..cf4854d27f4a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4690,9 +4690,9 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst 
*UI) {
 Changed = true;
   } else {
 Value* Cond = BI->getCondition();
-assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
-   "Same-destination conditional branch instruction was "
-   "already canonicalized into an unconditional branch.");
+//assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
+//   "Same-destination conditional branch instruction was "
+//   "already canonicalized into an unconditional 
branch.");
 if (BI->getSuccessor(0) == BB) {
   Builder.CreateAssumption(Builder.CreateNot(Cond));
   Builder.CreateBr(BI->getSuccessor(1));

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll
 
b/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll
new file mode 100644
index ..b56d9d435925
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll
@@ -0,0 +1,48 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 < %s | 
FileCheck %s
+
+@global = external global i16, align 1
+@global.1 = external global i16, align 1
+@global.2 = external global i16, align 1
+
+define void @widget() {
+; CHECK-LABEL: @widget(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:[[I:%.*]] = load i16, i16* @global, align 1
+; CHECK-NEXT:[[I13:%.*]] = icmp ne i16 [[I]], 0
+; CHECK-NEXT:call void @llvm.assume(i1 [[I13]])
+; CHECK-NEXT:[[I17:%.*]] = load i16, i16* @global, align 1
+; CHECK-NEXT:[[I18:%.*]] = sdiv i16 2, [[I17]]
+; CHECK-NEXT:[[I19:%.*]] = icmp ne i16 [[I18]], 0
+; CHECK-NEXT:[[I20:%.*]] = zext i1 [[I19]] to i16
+; CHECK-NEXT:[[I21:%.*]] = load i16, i16* @global.1, align 1
+; CHECK-NEXT:[[SPEC_SELECT:%.*]] = select i1 [[I19]], i16 [[I20]], i16 
[[I21]]
+; CHECK-NEXT:[[TMP0:%.*]] = xor i1 [[I19]], true
+; CHECK-NEXT:call void @llvm.assume(i1 [[TMP0]])
+; CHECK-NEXT:unreachable
+;
+bb:
+  %i = load i16, i16* @global, align 1
+  %i13 = icmp ne i16 %i, 0
+  br i1 %i13, label %bb16, label %bb14
+
+bb14: ; preds = %bb
+  %i15 = load i16, i16* @global.1, align 1
+  br label %bb23
+
+bb16: ; preds = %bb
+  %i17 = load i16, i16* @global, align 1
+  %i18 = sdiv i16 2, %i17
+  %i19 = icmp ne i16 %i18, 0
+  %i20 = zext i1 %i19 to i16
+  %i21 = load i16, i16* @global.1, align 1
+  br i1 %i19, label %bb22, label %bb23
+
+bb22: ; preds = %bb16
+  br label %bb23
+
+bb23: ; preds = %bb22, %bb16, %bb14
+  %i24 = phi i16 [ %i20, %bb22 ], [ %i21, %bb16 ], [ %i15, %bb14 ]
+  store i16 %i24, i16* @global.2, align 1
+  unreachable
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 29ca7d5 - [SimplifyCFG] simplifyUnreachable(): fix handling of degenerate same-destination conditional branch

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:36+03:00
New Revision: 29ca7d5a1ad968c371124b8d82edd8d91eee7b4f

URL: 
https://github.com/llvm/llvm-project/commit/29ca7d5a1ad968c371124b8d82edd8d91eee7b4f
DIFF: 
https://github.com/llvm/llvm-project/commit/29ca7d5a1ad968c371124b8d82edd8d91eee7b4f.diff

LOG: [SimplifyCFG] simplifyUnreachable(): fix handling of degenerate 
same-destination conditional branch

One would hope that it would have been already canonicalized into an
unconditional branch, but that isn't really guaranteed to happen
with SimplifyCFG's visitation order.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index cf4854d27f4a..7921a7462e2d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4683,16 +4683,18 @@ bool 
SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
 Instruction *TI = Predecessor->getTerminator();
 IRBuilder<> Builder(TI);
 if (auto *BI = dyn_cast(TI)) {
-  if (BI->isUnconditional()) {
-assert(BI->getSuccessor(0) == BB && "Incorrect CFG");
+  // We could either have a proper unconditional branch,
+  // or a degenerate conditional branch with matching destinations.
+  if (all_of(BI->successors(),
+ [BB](auto *Successor) { return Successor == BB; })) {
 new UnreachableInst(TI->getContext(), TI);
 TI->eraseFromParent();
 Changed = true;
   } else {
+assert(BI->isConditional() && "Can't get here with an uncond branch.");
 Value* Cond = BI->getCondition();
-//assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
-//   "Same-destination conditional branch instruction was "
-//   "already canonicalized into an unconditional 
branch.");
+assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
+   "The destinations are guaranteed to be 
diff erent here.");
 if (BI->getSuccessor(0) == BB) {
   Builder.CreateAssumption(Builder.CreateNot(Cond));
   Builder.CreateBr(BI->getSuccessor(1));

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll
 
b/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll
index b56d9d435925..66218a10521b 100644
--- 
a/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.ll
+++ 
b/llvm/test/Transforms/SimplifyCFG/simplifyUnreachable-degenerate-conditional-branch-with-matching-destinations.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 < %s | 
FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 @global = external global i16, align 1
 @global.1 = external global i16, align 1
@@ -11,14 +11,6 @@ define void @widget() {
 ; CHECK-NEXT:[[I:%.*]] = load i16, i16* @global, align 1
 ; CHECK-NEXT:[[I13:%.*]] = icmp ne i16 [[I]], 0
 ; CHECK-NEXT:call void @llvm.assume(i1 [[I13]])
-; CHECK-NEXT:[[I17:%.*]] = load i16, i16* @global, align 1
-; CHECK-NEXT:[[I18:%.*]] = sdiv i16 2, [[I17]]
-; CHECK-NEXT:[[I19:%.*]] = icmp ne i16 [[I18]], 0
-; CHECK-NEXT:[[I20:%.*]] = zext i1 [[I19]] to i16
-; CHECK-NEXT:[[I21:%.*]] = load i16, i16* @global.1, align 1
-; CHECK-NEXT:[[SPEC_SELECT:%.*]] = select i1 [[I19]], i16 [[I20]], i16 
[[I21]]
-; CHECK-NEXT:[[TMP0:%.*]] = xor i1 [[I19]], true
-; CHECK-NEXT:call void @llvm.assume(i1 [[TMP0]])
 ; CHECK-NEXT:unreachable
 ;
 bb:



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] fc96cb2 - [SimplifyCFG] FoldValueComparisonIntoPredecessors(): switch to non-permissive DomTree updates

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:37+03:00
New Revision: fc96cb2dad6b8293124f12d00fb55ff75c2ebe71

URL: 
https://github.com/llvm/llvm-project/commit/fc96cb2dad6b8293124f12d00fb55ff75c2ebe71
DIFF: 
https://github.com/llvm/llvm-project/commit/fc96cb2dad6b8293124f12d00fb55ff75c2ebe71.diff

LOG: [SimplifyCFG] FoldValueComparisonIntoPredecessors(): switch to 
non-permissive DomTree updates

... which requires not adding a DomTree edge that we just added.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 7921a7462e2d..08005c198298 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -17,6 +17,7 @@
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/ScopeExit.h"
+#include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SetOperations.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
@@ -1085,7 +1086,6 @@ static void FitWeights(MutableArrayRef Weights) 
{
 /// (either a switch or a branch on "X == c").
 /// See if any of the predecessors of the terminator block are value 
comparisons
 /// on the same value.  If so, and if safe to do so, fold them together.
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
  IRBuilder<> &Builder) 
{
   BasicBlock *BB = TI->getParent();
@@ -1129,7 +1129,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   // Based on whether the default edge from PTI goes to BB or not, fill in
   // PredCases and PredDefault with the new switch cases we would like to
   // build.
-  SmallVector NewSuccessors;
+  SmallMapVector NewSuccessors;
 
   // Update the branch weight metadata along the way
   SmallVector Weights;
@@ -1185,7 +1185,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   if (PredDefault != BB)
 Updates.push_back({DominatorTree::Delete, Pred, PredDefault});
   PredDefault = BBDefault;
-  NewSuccessors.push_back(BBDefault);
+  ++NewSuccessors[BBDefault];
 }
 
 unsigned CasesFromPred = Weights.size();
@@ -1194,7 +1194,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   if (!PTIHandled.count(BBCases[i].Value) &&
   BBCases[i].Dest != BBDefault) {
 PredCases.push_back(BBCases[i]);
-NewSuccessors.push_back(BBCases[i].Dest);
+++NewSuccessors[BBCases[i].Dest];
 if (SuccHasWeights || PredHasWeights) {
   // The default weight is at index 0, so weight for the ith case
   // should be at index i+1. Scale the cases from successor by
@@ -1242,7 +1242,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
 if (PredHasWeights || SuccHasWeights)
   Weights.push_back(WeightsForHandled[BBCases[i].Value]);
 PredCases.push_back(BBCases[i]);
-NewSuccessors.push_back(BBCases[i].Dest);
+++NewSuccessors[BBCases[i].Dest];
 PTIHandled.erase(
 BBCases[i].Value); // This constant is taken care of
   }
@@ -1253,16 +1253,20 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   if (PredHasWeights || SuccHasWeights)
 Weights.push_back(WeightsForHandled[I]);
   PredCases.push_back(ValueEqualityComparisonCase(I, BBDefault));
-  NewSuccessors.push_back(BBDefault);
+  ++NewSuccessors[BBDefault];
 }
   }
 
   // Okay, at this point, we know which new successor Pred will get.  Make
   // sure we update the number of entries in the PHI nodes for these
   // successors.
-  for (BasicBlock *NewSuccessor : NewSuccessors) {
-AddPredecessorToBlock(NewSuccessor, Pred, BB);
-Updates.push_back({DominatorTree::Insert, Pred, NewSuccessor});
+  for (const std::pair &NewSuccessor :
+   NewSuccessors) {
+for (auto I : seq(0, NewSuccessor.second)) {
+  (void)I;
+  AddPredecessorToBlock(NewSuccessor.first, Pred, BB);
+}
+Updates.push_back({DominatorTree::Insert, Pred, NewSuccessor.first});
   }
 
   Builder.SetInsertPoint(PTI);
@@ -1314,7 +1318,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   Updates.push_back({DominatorTree::Delete, Pred, BB});
 
   if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 
   Changed = true;
 }



___
llvm-branch-commits mailing

[llvm-branch-commits] [llvm] d15d81c - [SimplifyCFG] FoldValueComparisonIntoPredecessors(): deal with each predecessor only once

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:37+03:00
New Revision: d15d81ce15e086208f30d99ce2257a75401dc12c

URL: 
https://github.com/llvm/llvm-project/commit/d15d81ce15e086208f30d99ce2257a75401dc12c
DIFF: 
https://github.com/llvm/llvm-project/commit/d15d81ce15e086208f30d99ce2257a75401dc12c.diff

LOG: [SimplifyCFG] FoldValueComparisonIntoPredecessors(): deal with each 
predecessor only once

If the predecessor is a switch, and BB is not the default destination,
multiple cases could have the same destination. and it doesn't
make sense to re-process the predecessor, because we won't make any changes,
once is enough.

I'm not sure this can be really tested, other than via the assertion
being added here, which fires without the fix.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 08005c198298..6d5a68214e7c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1099,7 +1099,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   ++NumFoldValueComparisonIntoPredecessors;
   });
 
-  SmallVector Preds(predecessors(BB));
+  SmallSetVector Preds(pred_begin(BB), pred_end(BB));
   while (!Preds.empty()) {
 BasicBlock *Pred = Preds.pop_back_val();
 
@@ -1260,6 +1260,7 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   // Okay, at this point, we know which new successor Pred will get.  Make
   // sure we update the number of entries in the PHI nodes for these
   // successors.
+  assert(!NewSuccessors.empty() && "Should be adding some new 
successors.");
   for (const std::pair &NewSuccessor :
NewSuccessors) {
 for (auto I : seq(0, NewSuccessor.second)) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] fa5447a - [NFC][SimplifyCFG] SwitchToLookupTable(): pull out SI->getParent() into a variable

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:38+03:00
New Revision: fa5447aa3fec313bfd8ec31b7c66d390a5589b94

URL: 
https://github.com/llvm/llvm-project/commit/fa5447aa3fec313bfd8ec31b7c66d390a5589b94
DIFF: 
https://github.com/llvm/llvm-project/commit/fa5447aa3fec313bfd8ec31b7c66d390a5589b94.diff

LOG: [NFC][SimplifyCFG] SwitchToLookupTable(): pull out SI->getParent() into a 
variable

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6d5a68214e7c..dc931557ebb6 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5833,7 +5833,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 const TargetTransformInfo &TTI) {
   assert(SI->getNumCases() > 1 && "Degenerate switch?");
 
-  Function *Fn = SI->getParent()->getParent();
+  BasicBlock *BB = SI->getParent();
+  Function *Fn = BB->getParent();
   // Only build lookup table when we have a target that supports it or the
   // attribute is not set.
   if (!TTI.shouldBuildLookupTables() ||
@@ -5961,7 +5962,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 
   if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
 Builder.CreateBr(LookupBB);
-Updates.push_back({DominatorTree::Insert, SI->getParent(), LookupBB});
+Updates.push_back({DominatorTree::Insert, BB, LookupBB});
 // Note: We call removeProdecessor later since we need to be able to get 
the
 // PHI value for the default case in case we're using a bit mask.
   } else {
@@ -5969,9 +5970,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
 RangeCheckBranch =
 Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
-Updates.push_back({DominatorTree::Insert, SI->getParent(), LookupBB});
-Updates.push_back(
-{DominatorTree::Insert, SI->getParent(), SI->getDefaultDest()});
+Updates.push_back({DominatorTree::Insert, BB, LookupBB});
+Updates.push_back({DominatorTree::Insert, BB, SI->getDefaultDest()});
   }
 
   // Populate the BB that does the lookups.
@@ -6012,16 +6012,15 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 Updates.push_back({DominatorTree::Insert, MaskBB, LookupBB});
 Updates.push_back({DominatorTree::Insert, MaskBB, SI->getDefaultDest()});
 Builder.SetInsertPoint(LookupBB);
-AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
+AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, BB);
   }
 
   if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
 // We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
 // do not delete PHINodes here.
-SI->getDefaultDest()->removePredecessor(SI->getParent(),
+SI->getDefaultDest()->removePredecessor(BB,
 /*KeepOneInputPHIs=*/true);
-Updates.push_back(
-{DominatorTree::Delete, SI->getParent(), SI->getDefaultDest()});
+Updates.push_back({DominatorTree::Delete, BB, SI->getDefaultDest()});
   }
 
   bool ReturnedEarly = false;
@@ -6069,8 +6068,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
 
 if (Succ == SI->getDefaultDest())
   continue;
-Succ->removePredecessor(SI->getParent());
-Updates.push_back({DominatorTree::Delete, SI->getParent(), Succ});
+Succ->removePredecessor(BB);
+Updates.push_back({DominatorTree::Delete, BB, Succ});
   }
   SI->eraseFromParent();
   if (DTU)



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 2b437fc - [SimplifyCFG] SwitchToLookupTable(): switch to non-permissive DomTree updates

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:38+03:00
New Revision: 2b437fcd479befb96bd61e71c4de8143bd861a48

URL: 
https://github.com/llvm/llvm-project/commit/2b437fcd479befb96bd61e71c4de8143bd861a48
DIFF: 
https://github.com/llvm/llvm-project/commit/2b437fcd479befb96bd61e71c4de8143bd861a48.diff

LOG: [SimplifyCFG] SwitchToLookupTable(): switch to non-permissive DomTree 
updates

... which requires not deleting a DomTree edge that we just deleted.

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index dc931557ebb6..a433d04f2422 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5827,7 +5827,6 @@ static void reuseTableCompare(
 /// If the switch is only used to initialize one or more phi nodes in a common
 /// successor block with 
diff erent constant values, replace the switch with
 /// lookup tables.
-// FIXME: switch to non-permissive DomTreeUpdater::applyUpdates().
 static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
 DomTreeUpdater *DTU, const DataLayout &DL,
 const TargetTransformInfo &TTI) {
@@ -6063,17 +6062,22 @@ static bool SwitchToLookupTable(SwitchInst *SI, 
IRBuilder<> &Builder,
   }
 
   // Remove the switch.
+  SmallSetVector RemovedSuccessors;
   for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
 BasicBlock *Succ = SI->getSuccessor(i);
 
 if (Succ == SI->getDefaultDest())
   continue;
 Succ->removePredecessor(BB);
-Updates.push_back({DominatorTree::Delete, BB, Succ});
+RemovedSuccessors.insert(Succ);
   }
   SI->eraseFromParent();
-  if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+
+  if (DTU) {
+for (BasicBlock *RemovedSuccessor : RemovedSuccessors)
+  Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
+DTU->applyUpdates(Updates);
+  }
 
   ++NumLookupTables;
   if (NeedMask)



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 0a87e53 - [NFC][SimplifyCFG] Add a test where SimplifyEqualityComparisonWithOnlyPredecessor() deletes existing edge

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:39+03:00
New Revision: 0a87e53fc40ffd644139cdd210e7c382dbe329c8

URL: 
https://github.com/llvm/llvm-project/commit/0a87e53fc40ffd644139cdd210e7c382dbe329c8
DIFF: 
https://github.com/llvm/llvm-project/commit/0a87e53fc40ffd644139cdd210e7c382dbe329c8.diff

LOG: [NFC][SimplifyCFG] Add a test where 
SimplifyEqualityComparisonWithOnlyPredecessor() deletes existing edge

Added: 

llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll
new file mode 100644
index ..d6b9ea7f52c7
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=0 < %s | 
FileCheck %s
+
+declare void @zzz()
+
+define i32 @lex(i1 %c0, i1 %c1, i32 %r0, i32 %r1, i32 %v) {
+; CHECK-LABEL: @lex(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:[[C0_NOT:%.*]] = xor i1 [[C0:%.*]], true
+; CHECK-NEXT:[[C1_NOT:%.*]] = xor i1 [[C1:%.*]], true
+; CHECK-NEXT:[[BRMERGE:%.*]] = or i1 [[C0_NOT]], [[C1_NOT]]
+; CHECK-NEXT:[[R0_MUX:%.*]] = select i1 [[C0_NOT]], i32 [[R0:%.*]], i32 
[[R1:%.*]]
+; CHECK-NEXT:br i1 [[BRMERGE]], label [[IF_THEN:%.*]], label 
[[DO_BODY:%.*]]
+; CHECK:   if.then:
+; CHECK-NEXT:[[MERGE:%.*]] = phi i32 [ [[R0_MUX]], [[ENTRY:%.*]] ], [ 
[[R1]], [[DO_BODY]] ]
+; CHECK-NEXT:ret i32 [[MERGE]]
+; CHECK:   do.body:
+; CHECK-NEXT:call void @zzz()
+; CHECK-NEXT:switch i32 [[V:%.*]], label [[IF_THEN]] [
+; CHECK-NEXT:i32 10, label [[DO_BODY]]
+; CHECK-NEXT:i32 32, label [[DO_BODY]]
+; CHECK-NEXT:i32 9, label [[DO_BODY]]
+; CHECK-NEXT:]
+;
+entry:
+  br i1 %c0, label %if.end, label %if.then
+
+if.then:  ; preds = %entry
+  ret i32 %r0
+
+if.end:   ; preds = %entry
+  br i1 %c1, label %do.body, label %do.end
+
+do.body:  ; preds = %if.then193, 
%if.then193, %if.then193, %do.body, %do.body, %if.end
+  call void @zzz()
+  switch i32 %v, label %do.end [
+  i32 10, label %if.then193
+  i32 32, label %do.body
+  i32 9, label %do.body
+  ]
+
+if.then193:   ; preds = %do.body
+  switch i32 %v, label %do.end [
+  i32 32, label %do.body
+  i32 10, label %do.body
+  i32 9, label %do.body
+  ]
+
+do.end:   ; preds = %if.then193, 
%do.body, %if.end
+  ret i32 %r1
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] a14945c - [SimplifyCFG] SimplifyEqualityComparisonWithOnlyPredecessor(): really don't delete DomTree edges multiple times

2021-01-05 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-06T01:52:39+03:00
New Revision: a14945c1db614261a6f8d5d199e246d78f51e977

URL: 
https://github.com/llvm/llvm-project/commit/a14945c1db614261a6f8d5d199e246d78f51e977
DIFF: 
https://github.com/llvm/llvm-project/commit/a14945c1db614261a6f8d5d199e246d78f51e977.diff

LOG: [SimplifyCFG] SimplifyEqualityComparisonWithOnlyPredecessor(): really 
don't delete DomTree edges multiple times

Added: 


Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a433d04f2422..3fbc22a85be4 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -994,7 +994,8 @@ bool 
SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
   BasicBlock *CheckEdge = TheRealDest;
   for (BasicBlock *Succ : successors(TIBB))
 if (Succ != CheckEdge) {
-  RemovedSuccs.insert(Succ);
+  if (Succ != TheRealDest)
+RemovedSuccs.insert(Succ);
   Succ->removePredecessor(TIBB);
 } else
   CheckEdge = nullptr;

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll
 
b/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll
index d6b9ea7f52c7..617cd062652e 100644
--- 
a/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-domtree-preservation-edgecase.ll
+++ 
b/llvm/test/Transforms/SimplifyCFG/SimplifyEqualityComparisonWithOnlyPredecessor-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 < %s | 
FileCheck %s
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 declare void @zzz()
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 8dee0b4 - [llvm-reduce] ReduceGlobalVarInitializers delta pass: fix handling of globals w/ comdat/non-external linkage

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-07T18:05:03+03:00
New Revision: 8dee0b4bd6376f5518accf45e9ecc4a44a4c8481

URL: 
https://github.com/llvm/llvm-project/commit/8dee0b4bd6376f5518accf45e9ecc4a44a4c8481
DIFF: 
https://github.com/llvm/llvm-project/commit/8dee0b4bd6376f5518accf45e9ecc4a44a4c8481.diff

LOG: [llvm-reduce] ReduceGlobalVarInitializers delta pass: fix handling of 
globals w/ comdat/non-external linkage

Much like with ReduceFunctionBodies delta pass,
we need to remove comdat and set linkage to external,
else verifier will complain, and our deltas are invalid.

Added: 


Modified: 
llvm/test/Reduce/remove-global-vars.ll
llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp

Removed: 




diff  --git a/llvm/test/Reduce/remove-global-vars.ll 
b/llvm/test/Reduce/remove-global-vars.ll
index d078cad6b995..e791fd50bf7c 100644
--- a/llvm/test/Reduce/remove-global-vars.ll
+++ b/llvm/test/Reduce/remove-global-vars.ll
@@ -4,16 +4,25 @@
 ; RUN: llvm-reduce --test FileCheck --test-arg 
--check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg 
--input-file %s -o %t
 ; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL 
--implicit-check-not=uninteresting %s
 
+$interesting5 = comdat any
+
 ; CHECK-INTERESTINGNESS: @interesting = {{.*}}global i32{{.*}}, align 4
 ; CHECK-INTERESTINGNESS: @interesting2 = global i32 0, align 4
 ; CHECK-INTERESTINGNESS: @interesting3 = {{.*}}global i32{{.*}}, align 4
+; CHECK-INTERESTINGNESS: @interesting4 = {{.*}}constant i32{{.*}}, align 4
+; CHECK-INTERESTINGNESS: @interesting5 = {{.*}}global i32{{.*}}, align 4
 
 ; CHECK-FINAL: @interesting = external global i32, align 4
 ; CHECK-FINAL: @interesting2 = global i32 0, align 4
 ; CHECK-FINAL: @interesting3 = external global i32, align 4
+; CHECK-FINAL: @interesting4 = external dso_local constant i32, align 4
+; CHECK-FINAL: @interesting5 = external global i32, align 4
 @interesting = global i32 0, align 4
 @interesting2 = global i32 0, align 4
 @interesting3 = external global i32, align 4
+@interesting4 = private constant i32 2, align 4
+@interesting5 = global i32 2, align 4, comdat
+
 @uninteresting = global i32 1, align 4
 @uninteresting2 = external global i32, align 4
 

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
index 1128710f64c4..fd5a5d1f02c6 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp
@@ -13,6 +13,7 @@
 
 #include "ReduceGlobalVarInitializers.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalValue.h"
 
 using namespace llvm;
 
@@ -23,8 +24,11 @@ static void extractGVsFromModule(std::vector 
ChunksToKeep,
 
   // Drop initializers of out-of-chunk GVs
   for (auto &GV : Program->globals())
-if (GV.hasInitializer() && !O.shouldKeep())
+if (GV.hasInitializer() && !O.shouldKeep()) {
   GV.setInitializer(nullptr);
+  GV.setLinkage(GlobalValue::LinkageTypes::ExternalLinkage);
+  GV.setComdat(nullptr);
+}
 }
 
 /// Counts the amount of initialized GVs and displays their



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 6be1fd6 - [SimplifyCFG] FoldValueComparisonIntoPredecessors(): drop reachable errneous assert

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-07T18:05:04+03:00
New Revision: 6be1fd6b20f3418543a50ce9b04ab4a49585a7eb

URL: 
https://github.com/llvm/llvm-project/commit/6be1fd6b20f3418543a50ce9b04ab4a49585a7eb
DIFF: 
https://github.com/llvm/llvm-project/commit/6be1fd6b20f3418543a50ce9b04ab4a49585a7eb.diff

LOG: [SimplifyCFG] FoldValueComparisonIntoPredecessors(): drop reachable 
errneous assert

I have added it in d15d81c because it *seemed* correct, was holding
for all the tests so far, and was validating the fix added in the same
commit, but as David Major is pointing out (with a reproducer),
the assertion isn't really correct after all. So remove it.

Note that the d15d81c still fine.

Added: 

llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-no-new-successors.ll

Modified: 
llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 3fbc22a85be4..9aedb918ac32 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1261,7 +1261,6 @@ bool 
SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
   // Okay, at this point, we know which new successor Pred will get.  Make
   // sure we update the number of entries in the PHI nodes for these
   // successors.
-  assert(!NewSuccessors.empty() && "Should be adding some new 
successors.");
   for (const std::pair &NewSuccessor :
NewSuccessors) {
 for (auto I : seq(0, NewSuccessor.second)) {

diff  --git 
a/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-no-new-successors.ll
 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-no-new-successors.ll
new file mode 100644
index ..03490c9ac34b
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/FoldValueComparisonIntoPredecessors-no-new-successors.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
+
+define void @widget(i32 %arg) {
+; CHECK-LABEL: @widget(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:[[SWITCH:%.*]] = icmp ult i32 [[ARG:%.*]], 2
+; CHECK-NEXT:br i1 [[SWITCH]], label [[BB2:%.*]], label [[INFLOOP:%.*]]
+; CHECK:   bb2:
+; CHECK-NEXT:ret void
+; CHECK:   infloop:
+; CHECK-NEXT:br label [[INFLOOP]]
+;
+bb:
+  %tmp = icmp eq i32 %arg, 0
+  br i1 %tmp, label %bb2, label %bb1
+
+bb1:  ; preds = %bb1
+  %tmp4 = icmp eq i32 %arg, 1
+  br i1 %tmp4, label %bb6, label %bb5
+
+bb5:  ; preds = %bb5, %bb5
+  switch i32 %arg, label %bb5 [
+  i32 0, label %bb9
+  ]
+
+bb2:
+  ret void
+
+bb6:  ; preds = %bb1
+  ret void
+
+bb9:  ; preds = %bb5
+  ret void
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 087be53 - [NFC][SimplifyCFG] Add a test with cond br on constant w/ identical destinations

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:24+03:00
New Revision: 087be536feab0aacc043aed52bee2e48e90e538c

URL: 
https://github.com/llvm/llvm-project/commit/087be536feab0aacc043aed52bee2e48e90e538c
DIFF: 
https://github.com/llvm/llvm-project/commit/087be536feab0aacc043aed52bee2e48e90e538c.diff

LOG: [NFC][SimplifyCFG] Add a test with cond br on constant w/ identical 
destinations

Added: 


Modified: 
llvm/test/Transforms/SimplifyCFG/branch-fold.ll

Removed: 




diff  --git a/llvm/test/Transforms/SimplifyCFG/branch-fold.ll 
b/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
index a4ac23bada70..5b9f20b97d4e 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-fold.ll
@@ -1,35 +1,59 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | 
FileCheck %s
 
 define void @test(i32* %P, i32* %Q, i1 %A, i1 %B) {
-; CHECK: test
-; CHECK: br i1
-; CHECK-NOT: br i1
-; CHECK: ret
-; CHECK: ret
+; CHECK-LABEL: @test(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:[[A_NOT:%.*]] = xor i1 [[A:%.*]], true
+; CHECK-NEXT:[[BRMERGE:%.*]] = or i1 [[A_NOT]], [[B:%.*]]
+; CHECK-NEXT:br i1 [[BRMERGE]], label [[B:%.*]], label [[C:%.*]]
+; CHECK:   b:
+; CHECK-NEXT:store i32 123, i32* [[P:%.*]], align 4
+; CHECK-NEXT:ret void
+; CHECK:   c:
+; CHECK-NEXT:ret void
+;
 
 entry:
-br i1 %A, label %a, label %b
+  br i1 %A, label %a, label %b
 a:
-br i1 %B, label %b, label %c
+  br i1 %B, label %b, label %c
 b:
-store i32 123, i32* %P
-ret void
+  store i32 123, i32* %P
+  ret void
 c:
-ret void
+  ret void
 }
 
 ; rdar://10554090
 define zeroext i1 @test2(i64 %i0, i64 %i1) nounwind uwtable readonly ssp {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:[[AND_I_I:%.*]] = and i64 [[I0:%.*]], 281474976710655
+; CHECK-NEXT:[[AND_I11_I:%.*]] = and i64 [[I1:%.*]], 281474976710655
+; CHECK-NEXT:[[OR_COND:%.*]] = icmp eq i64 [[AND_I_I]], [[AND_I11_I]]
+; CHECK-NEXT:br i1 [[OR_COND]], label [[C:%.*]], label [[A:%.*]]
+; CHECK:   a:
+; CHECK-NEXT:[[SHR_I4_I:%.*]] = lshr i64 [[I0]], 48
+; CHECK-NEXT:[[AND_I5_I:%.*]] = and i64 [[SHR_I4_I]], 32767
+; CHECK-NEXT:[[SHR_I_I:%.*]] = lshr i64 [[I1]], 48
+; CHECK-NEXT:[[AND_I2_I:%.*]] = and i64 [[SHR_I_I]], 32767
+; CHECK-NEXT:[[CMP9_I:%.*]] = icmp ult i64 [[AND_I5_I]], [[AND_I2_I]]
+; CHECK-NEXT:[[PHITMP:%.*]] = icmp uge i64 [[AND_I2_I]], [[AND_I5_I]]
+; CHECK-NEXT:[[NOT_COND:%.*]] = xor i1 [[CMP9_I]], true
+; CHECK-NEXT:[[AND_COND:%.*]] = and i1 [[NOT_COND]], [[PHITMP]]
+; CHECK-NEXT:br label [[C]]
+; CHECK:   c:
+; CHECK-NEXT:[[O2:%.*]] = phi i1 [ [[AND_COND]], [[A]] ], [ false, 
[[ENTRY:%.*]] ]
+; CHECK-NEXT:ret i1 [[O2]]
+;
 entry:
-; CHECK: test2
-; CHECK: br i1
   %and.i.i = and i64 %i0, 281474976710655
   %and.i11.i = and i64 %i1, 281474976710655
   %or.cond = icmp eq i64 %and.i.i, %and.i11.i
   br i1 %or.cond, label %c, label %a
 
 a:
-; CHECK: br
   %shr.i4.i = lshr i64 %i0, 48
   %and.i5.i = and i64 %shr.i4.i, 32767
   %shr.i.i = lshr i64 %i1, 48
@@ -38,7 +62,6 @@ a:
   br i1 %cmp9.i, label %c, label %b
 
 b:
-; CHECK-NOT: br
   %shr.i13.i9 = lshr i64 %i1, 48
   %and.i14.i10 = and i64 %shr.i13.i9, 32767
   %shr.i.i11 = lshr i64 %i0, 48
@@ -53,6 +76,10 @@ c:
 
 ; PR13180
 define void @pr13180(i8 %p) {
+; CHECK-LABEL: @pr13180(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:unreachable
+;
 entry:
   %tobool = icmp eq i8 %p, 0
   br i1 %tobool, label %cond.false, label %cond.true
@@ -68,3 +95,18 @@ cond.end: ; preds = 
%cond.false, %cond.t
   %cond = phi i1 [ undef, %cond.true ], [ %phitmp, %cond.false ]
   unreachable
 }
+
+declare void @foo()
+define void @test3() {
+; CHECK-LABEL: @test3(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:call void @foo()
+; CHECK-NEXT:ret void
+;
+entry:
+  br i1 0, label %bb0, label %bb0
+
+bb0:
+  call void @foo()
+  ret void
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 16ab8e5 - [SimplifyCFG] ConstantFoldTerminator(): handle matching destinations of condbr earlier

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:24+03:00
New Revision: 16ab8e5f6dbbeb5b8e900677f4a64c9924ecd7ba

URL: 
https://github.com/llvm/llvm-project/commit/16ab8e5f6dbbeb5b8e900677f4a64c9924ecd7ba
DIFF: 
https://github.com/llvm/llvm-project/commit/16ab8e5f6dbbeb5b8e900677f4a64c9924ecd7ba.diff

LOG: [SimplifyCFG] ConstantFoldTerminator(): handle matching destinations of 
condbr earlier

We need to handle this case before dealing with the case of constant
branch condition, because if the destinations match, latter fold
would try to remove the DomTree edge that would still be present.

This allows to make that particular DomTree update non-permissive

Added: 


Modified: 
llvm/lib/Transforms/Utils/Local.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/Local.cpp 
b/llvm/lib/Transforms/Utils/Local.cpp
index e3bdfbae9287..107929e801d9 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -134,27 +134,10 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
   // Branch - See if we are conditional jumping on constant
   if (auto *BI = dyn_cast(T)) {
 if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
+
 BasicBlock *Dest1 = BI->getSuccessor(0);
 BasicBlock *Dest2 = BI->getSuccessor(1);
 
-if (auto *Cond = dyn_cast(BI->getCondition())) {
-  // Are we branching on constant?
-  // YES.  Change to unconditional branch...
-  BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
-  BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
-
-  // Let the basic block know that we are letting go of it.  Based on this,
-  // it will adjust it's PHI nodes.
-  OldDest->removePredecessor(BB);
-
-  // Replace the conditional branch with an unconditional one.
-  Builder.CreateBr(Destination);
-  BI->eraseFromParent();
-  if (DTU)
-DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, OldDest}});
-  return true;
-}
-
 if (Dest2 == Dest1) {   // Conditional branch to same location?
   // This branch matches something like this:
   // br bool %cond, label %Dest, label %Dest
@@ -172,6 +155,25 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
   return true;
 }
+
+if (auto *Cond = dyn_cast(BI->getCondition())) {
+  // Are we branching on constant?
+  // YES.  Change to unconditional branch...
+  BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
+  BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
+
+  // Let the basic block know that we are letting go of it.  Based on this,
+  // it will adjust it's PHI nodes.
+  OldDest->removePredecessor(BB);
+
+  // Replace the conditional branch with an unconditional one.
+  Builder.CreateBr(Destination);
+  BI->eraseFromParent();
+  if (DTU)
+DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}});
+  return true;
+}
+
 return false;
   }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 36593a3 - [SimplifyCFG] ConstantFoldTerminator(): switch to non-permissive DomTree updates in `SwitchInst` handling

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:24+03:00
New Revision: 36593a30a40b52e8040d821bbd294ef6758cf9cf

URL: 
https://github.com/llvm/llvm-project/commit/36593a30a40b52e8040d821bbd294ef6758cf9cf
DIFF: 
https://github.com/llvm/llvm-project/commit/36593a30a40b52e8040d821bbd294ef6758cf9cf.diff

LOG: [SimplifyCFG] ConstantFoldTerminator(): switch to non-permissive DomTree 
updates in `SwitchInst` handling

... which requires not deleting edges that will still be present.

Added: 


Modified: 
llvm/lib/Transforms/Utils/Local.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/Local.cpp 
b/llvm/lib/Transforms/Utils/Local.cpp
index 107929e801d9..5ce0e29cadab 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -231,9 +231,6 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
 i = SI->removeCase(i);
 e = SI->case_end();
 Changed = true;
-if (DTU)
-  DTU->applyUpdatesPermissive(
-  {{DominatorTree::Delete, ParentBB, DefaultDest}});
 continue;
   }
 
@@ -259,19 +256,19 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
   // Insert the new branch.
   Builder.CreateBr(TheOnlyDest);
   BasicBlock *BB = SI->getParent();
-  std::vector  Updates;
-  if (DTU)
-Updates.reserve(SI->getNumSuccessors() - 1);
+
+  SmallSetVector RemovedSuccessors;
 
   // Remove entries from PHI nodes which we no longer branch to...
+  BasicBlock *SuccToKeep = TheOnlyDest;
   for (BasicBlock *Succ : successors(SI)) {
+if (DTU && Succ != TheOnlyDest)
+  RemovedSuccessors.insert(Succ);
 // Found case matching a constant operand?
-if (Succ == TheOnlyDest) {
-  TheOnlyDest = nullptr; // Don't modify the first branch to 
TheOnlyDest
+if (Succ == SuccToKeep) {
+  SuccToKeep = nullptr; // Don't modify the first branch to TheOnlyDest
 } else {
   Succ->removePredecessor(BB);
-  if (DTU)
-Updates.push_back({DominatorTree::Delete, BB, Succ});
 }
   }
 
@@ -280,8 +277,13 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
   SI->eraseFromParent();
   if (DeleteDeadConditions)
 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
-  if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+  if (DTU) {
+std::vector Updates;
+Updates.reserve(RemovedSuccessors.size());
+for (auto *RemovedSuccessor : RemovedSuccessors)
+  Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
+DTU->applyUpdates(Updates);
+  }
   return true;
 }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 8b9a0e6 - [NFC][SimlifyCFG] Add some indirectbr-of-blockaddress tests

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:25+03:00
New Revision: 8b9a0e6f7ed2fa3293ba5cd2c2fb1acd21db6e2d

URL: 
https://github.com/llvm/llvm-project/commit/8b9a0e6f7ed2fa3293ba5cd2c2fb1acd21db6e2d
DIFF: 
https://github.com/llvm/llvm-project/commit/8b9a0e6f7ed2fa3293ba5cd2c2fb1acd21db6e2d.diff

LOG: [NFC][SimlifyCFG] Add some indirectbr-of-blockaddress tests

Added: 


Modified: 
llvm/test/Transforms/SimplifyCFG/indirectbr.ll

Removed: 




diff  --git a/llvm/test/Transforms/SimplifyCFG/indirectbr.ll 
b/llvm/test/Transforms/SimplifyCFG/indirectbr.ll
index 52ef6b77166c..3c82f5a64ee3 100644
--- a/llvm/test/Transforms/SimplifyCFG/indirectbr.ll
+++ b/llvm/test/Transforms/SimplifyCFG/indirectbr.ll
@@ -1,17 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | 
FileCheck %s
 
 ; SimplifyCFG should eliminate redundant indirectbr edges.
 
-; CHECK: indbrtest0
-; CHECK: indirectbr i8* %t, [label %BB0, label %BB1, label %BB2]
-; CHECK: %x = phi i32 [ 0, %BB0 ], [ 1, %entry ]
-
 declare void @foo()
 declare void @A()
 declare void @B(i32)
 declare void @C()
 
 define void @indbrtest0(i8** %P, i8** %Q) {
+; CHECK-LABEL: @indbrtest0(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:store i8* blockaddress(@indbrtest0, [[BB0:%.*]]), i8** 
[[P:%.*]], align 8
+; CHECK-NEXT:store i8* blockaddress(@indbrtest0, [[BB1:%.*]]), i8** [[P]], 
align 8
+; CHECK-NEXT:store i8* blockaddress(@indbrtest0, [[BB2:%.*]]), i8** [[P]], 
align 8
+; CHECK-NEXT:call void @foo()
+; CHECK-NEXT:[[T:%.*]] = load i8*, i8** [[Q:%.*]], align 8
+; CHECK-NEXT:indirectbr i8* [[T]], [label [[BB0]], label [[BB1]], label 
%BB2]
+; CHECK:   BB0:
+; CHECK-NEXT:call void @A()
+; CHECK-NEXT:br label [[BB1]]
+; CHECK:   BB1:
+; CHECK-NEXT:[[X:%.*]] = phi i32 [ 0, [[BB0]] ], [ 1, [[ENTRY:%.*]] ]
+; CHECK-NEXT:call void @B(i32 [[X]])
+; CHECK-NEXT:ret void
+; CHECK:   BB2:
+; CHECK-NEXT:call void @C()
+; CHECK-NEXT:ret void
+;
 entry:
   store i8* blockaddress(@indbrtest0, %BB0), i8** %P
   store i8* blockaddress(@indbrtest0, %BB1), i8** %P
@@ -35,10 +51,17 @@ BB2:
 ; better if it removed the branch altogether, but simplifycfdg currently misses
 ; that because the predecessor is the entry block.
 
-; CHECK: indbrtest1
-; CHECK: br label %BB0
 
 define void @indbrtest1(i8** %P, i8** %Q) {
+; CHECK-LABEL: @indbrtest1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:store i8* blockaddress(@indbrtest1, [[BB0:%.*]]), i8** 
[[P:%.*]], align 8
+; CHECK-NEXT:call void @foo()
+; CHECK-NEXT:br label [[BB0]]
+; CHECK:   BB0:
+; CHECK-NEXT:call void @A()
+; CHECK-NEXT:ret void
+;
 entry:
   store i8* blockaddress(@indbrtest1, %BB0), i8** %P
   call void @foo()
@@ -52,11 +75,12 @@ BB0:
 ; SimplifyCFG should notice that BB0 does not have its address taken and
 ; remove it from entry's successor list.
 
-; CHECK: indbrtest2
-; CHECK: entry:
-; CHECK-NEXT: unreachable
 
 define void @indbrtest2(i8* %t) {
+; CHECK-LABEL: @indbrtest2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:unreachable
+;
 entry:
   indirectbr i8* %t, [label %BB0, label %BB0]
 BB0:
@@ -77,13 +101,17 @@ BB0:
 ; SimplifyCFG should turn the indirectbr into a conditional branch on the
 ; condition of the select.
 
-; CHECK-LABEL: @indbrtest3(
-; CHECK-NEXT: entry:
-; CHECK-NEXT: br i1 %cond, label %L1, label %L2
-; CHECK-NOT: indirectbr
-; CHECK-NOT: br
-; CHECK-NOT: L3:
 define void @indbrtest3(i1 %cond, i8* %address) nounwind {
+; CHECK-LABEL: @indbrtest3(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:br i1 [[COND:%.*]], label [[L1:%.*]], label [[L2:%.*]]
+; CHECK:   L1:
+; CHECK-NEXT:call void @A()
+; CHECK-NEXT:ret void
+; CHECK:   L2:
+; CHECK-NEXT:call void @C()
+; CHECK-NEXT:ret void
+;
 entry:
   %indirect.goto.dest = select i1 %cond, i8* blockaddress(@indbrtest3, %L1), 
i8* blockaddress(@indbrtest3, %L2)
   indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
@@ -104,10 +132,14 @@ L3:
 ; As in @indbrtest1, it should really remove the branch entirely, but it 
doesn't
 ; because it's in the entry block.
 
-; CHECK-LABEL: @indbrtest4(
-; CHECK-NEXT: entry:
-; CHECK-NEXT: br label %L1
 define void @indbrtest4(i1 %cond) nounwind {
+; CHECK-LABEL: @indbrtest4(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:br label [[L1:%.*]]
+; CHECK:   L1:
+; CHECK-NEXT:call void @A()
+; CHECK-NEXT:ret void
+;
 entry:
   %indirect.goto.dest = select i1 %cond, i8* blockaddress(@indbrtest4, %L1), 
i8* blockaddress(@indbrtest4, %L1)
   indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
@@ -126,11 +158,11 @@ L3:
 ; SimplifyCFG should turn the indirectbr into an unreachable because neither
 ; destination is listed as a successor.
 
-; CHECK-LABEL: @indbrtest5(
-; CHECK-NEXT: entry:
-; CHECK-NEXT: unreachable
-

[llvm-branch-commits] [llvm] b382272 - [SimplifyCFG] ConstantFoldTerminator(): switch to non-permissive DomTree updates in `indirectbr` handling

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:25+03:00
New Revision: b3822728fae2e3755d6daff7fc31fbac16e61fe4

URL: 
https://github.com/llvm/llvm-project/commit/b3822728fae2e3755d6daff7fc31fbac16e61fe4
DIFF: 
https://github.com/llvm/llvm-project/commit/b3822728fae2e3755d6daff7fc31fbac16e61fe4.diff

LOG: [SimplifyCFG] ConstantFoldTerminator(): switch to non-permissive DomTree 
updates in `indirectbr` handling

... which requires not deleting edges that were just deleted already.

Added: 


Modified: 
llvm/lib/Transforms/Utils/Local.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/Local.cpp 
b/llvm/lib/Transforms/Utils/Local.cpp
index 5ce0e29cadab..38cfee31c35d 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -329,22 +329,20 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
 if (auto *BA =
   dyn_cast(IBI->getAddress()->stripPointerCasts())) {
   BasicBlock *TheOnlyDest = BA->getBasicBlock();
-  std::vector  Updates;
-  if (DTU)
-Updates.reserve(IBI->getNumDestinations() - 1);
+  SmallSetVector RemovedSuccessors;
 
   // Insert the new branch.
   Builder.CreateBr(TheOnlyDest);
 
+  BasicBlock *SuccToKeep = TheOnlyDest;
   for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
-if (IBI->getDestination(i) == TheOnlyDest) {
-  TheOnlyDest = nullptr;
+BasicBlock *DestBB = IBI->getDestination(i);
+if (DTU && DestBB != TheOnlyDest)
+  RemovedSuccessors.insert(DestBB);
+if (IBI->getDestination(i) == SuccToKeep) {
+  SuccToKeep = nullptr;
 } else {
-  BasicBlock *ParentBB = IBI->getParent();
-  BasicBlock *DestBB = IBI->getDestination(i);
-  DestBB->removePredecessor(ParentBB);
-  if (DTU)
-Updates.push_back({DominatorTree::Delete, ParentBB, DestBB});
+  DestBB->removePredecessor(BB);
 }
   }
   Value *Address = IBI->getAddress();
@@ -361,13 +359,18 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool 
DeleteDeadConditions,
   // If we didn't find our destination in the IBI successor list, then we
   // have undefined behavior.  Replace the unconditional branch with an
   // 'unreachable' instruction.
-  if (TheOnlyDest) {
+  if (SuccToKeep) {
 BB->getTerminator()->eraseFromParent();
 new UnreachableInst(BB->getContext(), BB);
   }
 
-  if (DTU)
-DTU->applyUpdatesPermissive(Updates);
+  if (DTU) {
+std::vector Updates;
+Updates.reserve(RemovedSuccessors.size());
+for (auto *RemovedSuccessor : RemovedSuccessors)
+  Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
+DTU->applyUpdates(Updates);
+  }
   return true;
 }
   }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 1f9b591 - [SimplifyCFG] TryToSimplifyUncondBranchFromEmptyBlock(): switch to non-permissive DomTree updates

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:25+03:00
New Revision: 1f9b591ee66fe5abd6f63990b085e1f1f559d8d9

URL: 
https://github.com/llvm/llvm-project/commit/1f9b591ee66fe5abd6f63990b085e1f1f559d8d9
DIFF: 
https://github.com/llvm/llvm-project/commit/1f9b591ee66fe5abd6f63990b085e1f1f559d8d9.diff

LOG: [SimplifyCFG] TryToSimplifyUncondBranchFromEmptyBlock(): switch to 
non-permissive DomTree updates

... which requires not deleting edges that were just deleted already,
by not processing the same predecessor more than once.

Added: 


Modified: 
llvm/lib/Transforms/Utils/Local.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/Local.cpp 
b/llvm/lib/Transforms/Utils/Local.cpp
index 38cfee31c35d..7c962edba3ca 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1048,11 +1048,13 @@ bool 
llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
   if (DTU) {
 Updates.push_back({DominatorTree::Delete, BB, Succ});
 // All predecessors of BB will be moved to Succ.
-for (auto I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
-  Updates.push_back({DominatorTree::Delete, *I, BB});
+SmallSetVector Predecessors(pred_begin(BB), pred_end(BB));
+Updates.reserve(Updates.size() + 2 * Predecessors.size());
+for (auto *Predecessor : Predecessors) {
+  Updates.push_back({DominatorTree::Delete, Predecessor, BB});
   // This predecessor of BB may already have Succ as a successor.
-  if (!llvm::is_contained(successors(*I), Succ))
-Updates.push_back({DominatorTree::Insert, *I, Succ});
+  if (!llvm::is_contained(successors(Predecessor), Succ))
+Updates.push_back({DominatorTree::Insert, Predecessor, Succ});
 }
   }
 
@@ -1109,7 +,7 @@ bool 
llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
"applying corresponding DTU updates.");
 
   if (DTU) {
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 DTU->deleteBB(BB);
   } else {
 BB->eraseFromParent(); // Delete the old basic block.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] f8875c3 - [NFC][SimplifyCFG] Add test with an unreachable block with two identical successors

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:25+03:00
New Revision: f8875c313c381764a9734dbd6e94539e8837d9f7

URL: 
https://github.com/llvm/llvm-project/commit/f8875c313c381764a9734dbd6e94539e8837d9f7
DIFF: 
https://github.com/llvm/llvm-project/commit/f8875c313c381764a9734dbd6e94539e8837d9f7.diff

LOG: [NFC][SimplifyCFG] Add test with an unreachable block with two identical 
successors

Added: 
llvm/test/Transforms/SimplifyCFG/unreachable-matching-successor.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/unreachable-matching-successor.ll 
b/llvm/test/Transforms/SimplifyCFG/unreachable-matching-successor.ll
new file mode 100644
index ..25a4ab7203f5
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/unreachable-matching-successor.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | 
FileCheck %s
+
+define void @fn(i1 %c) {
+; CHECK-LABEL: @fn(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:ret void
+;
+entry:
+  ret void
+
+unreachable_bb0:
+  br i1 %c, label %unreachable_bb1, label %unreachable_bb1
+unreachable_bb1:
+  br label %unreachable_bb0
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 7600d7c - [SimplifyCFG] removeUnreachableBlocks(): switch to non-permissive DomTree updates

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:26+03:00
New Revision: 7600d7c7be07ee78543522d0fbd1e92e672a0327

URL: 
https://github.com/llvm/llvm-project/commit/7600d7c7be07ee78543522d0fbd1e92e672a0327
DIFF: 
https://github.com/llvm/llvm-project/commit/7600d7c7be07ee78543522d0fbd1e92e672a0327.diff

LOG: [SimplifyCFG] removeUnreachableBlocks(): switch to non-permissive DomTree 
updates

... which requires not deleting edges that were just deleted already,
by not processing the same predecessor more than once.

Added: 


Modified: 
llvm/lib/Transforms/Utils/Local.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Utils/Local.cpp 
b/llvm/lib/Transforms/Utils/Local.cpp
index 7c962edba3ca..220a4d43b491 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2376,12 +2376,16 @@ bool llvm::removeUnreachableBlocks(Function &F, 
DomTreeUpdater *DTU,
   // their internal references. Update DTU if available.
   std::vector Updates;
   for (auto *BB : DeadBlockSet) {
+SmallSetVector UniqueSuccessors;
 for (BasicBlock *Successor : successors(BB)) {
   if (!DeadBlockSet.count(Successor))
 Successor->removePredecessor(BB);
   if (DTU)
-Updates.push_back({DominatorTree::Delete, BB, Successor});
+UniqueSuccessors.insert(Successor);
 }
+if (DTU)
+  for (auto *UniqueSuccessor : UniqueSuccessors)
+Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor});
 BB->dropAllReferences();
 if (DTU) {
   Instruction *TI = BB->getTerminator();
@@ -2398,7 +2402,7 @@ bool llvm::removeUnreachableBlocks(Function &F, 
DomTreeUpdater *DTU,
   }
 
   if (DTU) {
-DTU->applyUpdatesPermissive(Updates);
+DTU->applyUpdates(Updates);
 bool Deleted = false;
 for (auto *BB : DeadBlockSet) {
   if (DTU->isBBPendingDeletion(BB))



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 6984781 - [NFC][SimplifyCFG] Add a test with an undef cond branch to identical destinations

2021-01-07 Thread Roman Lebedev via llvm-branch-commits

Author: Roman Lebedev
Date: 2021-01-08T02:15:26+03:00
New Revision: 6984781df9b584febce51b7740c8738a076f5692

URL: 
https://github.com/llvm/llvm-project/commit/6984781df9b584febce51b7740c8738a076f5692
DIFF: 
https://github.com/llvm/llvm-project/commit/6984781df9b584febce51b7740c8738a076f5692.diff

LOG: [NFC][SimplifyCFG] Add a test with an undef cond branch to identical 
destinations

Added: 
llvm/test/Transforms/SimplifyCFG/change-to-unreachable-matching-successor.ll

Modified: 


Removed: 




diff  --git 
a/llvm/test/Transforms/SimplifyCFG/change-to-unreachable-matching-successor.ll 
b/llvm/test/Transforms/SimplifyCFG/change-to-unreachable-matching-successor.ll
new file mode 100644
index ..4fd017d6780f
--- /dev/null
+++ 
b/llvm/test/Transforms/SimplifyCFG/change-to-unreachable-matching-successor.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | 
FileCheck %s
+
+declare void @llvm.assume(i1)
+
+define void @fn(i1 %c) {
+; CHECK-LABEL: @fn(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:unreachable
+;
+entry:
+  call void @llvm.assume(i1 undef)
+  br i1 %c, label %bb1, label %bb1
+bb1:
+  ret void
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


  1   2   >