From 1dafaa87d9c3b25f53bd18cab18725dd1d76023e Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Wed, 29 Aug 2018 11:37:34 +0000 Subject: [PATCH] [NFC] Unify guards detection We have multiple places in code where we try to identify whether or not some instruction is a guard. This patch factors out this logic into a separate utility function which works uniformly in all places. Differential Revision: https://reviews.llvm.org/D51152 Reviewed By: fedor.sergeev llvm-svn: 340921 --- llvm/include/llvm/Transforms/Utils/GuardUtils.h | 4 ++++ llvm/lib/Analysis/AliasSetTracker.cpp | 4 ++-- llvm/lib/Analysis/ValueTracking.cpp | 4 ++-- llvm/lib/Transforms/Scalar/EarlyCSE.cpp | 3 ++- llvm/lib/Transforms/Scalar/GuardWidening.cpp | 7 +------ llvm/lib/Transforms/Scalar/JumpThreading.cpp | 6 +++--- llvm/lib/Transforms/Scalar/LICM.cpp | 3 ++- llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp | 6 ++---- llvm/lib/Transforms/Utils/GuardUtils.cpp | 6 ++++++ 9 files changed, 24 insertions(+), 19 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/GuardUtils.h b/llvm/include/llvm/Transforms/Utils/GuardUtils.h index 537045e..4850fc6 100644 --- a/llvm/include/llvm/Transforms/Utils/GuardUtils.h +++ b/llvm/include/llvm/Transforms/Utils/GuardUtils.h @@ -17,6 +17,10 @@ namespace llvm { class CallInst; class Function; +class User; + +/// Returns true iff \p U has semantics of a guard. +bool isGuard(const User *U); /// Splits control flow at point of \p Guard, replacing it with explicit branch /// by the condition of guard's first argument. The taken branch then goes to diff --git a/llvm/lib/Analysis/AliasSetTracker.cpp b/llvm/lib/Analysis/AliasSetTracker.cpp index 377db8a..fd9263f 100644 --- a/llvm/lib/Analysis/AliasSetTracker.cpp +++ b/llvm/lib/Analysis/AliasSetTracker.cpp @@ -34,6 +34,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/GuardUtils.h" #include #include #include @@ -172,8 +173,7 @@ void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) { // Guards are marked as modifying memory for control flow modelling purposes, // but don't actually modify any specific memory location. using namespace PatternMatch; - bool MayWriteMemory = I->mayWriteToMemory() && - !match(I, m_Intrinsic()) && + bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) && !(I->use_empty() && match(I, m_Intrinsic())); if (!MayWriteMemory) { Alias = SetMayAlias; diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index a207a50..527673f 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -66,6 +66,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/KnownBits.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Transforms/Utils/GuardUtils.h" #include #include #include @@ -1902,8 +1903,7 @@ static bool isKnownNonNullFromDominatingCondition(const Value *V, BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) return true; - } else if (Pred == ICmpInst::ICMP_NE && - match(Curr, m_Intrinsic()) && + } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) && DT->dominates(cast(Curr), CtxI)) { return true; } diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp index 533d16e..d24ad03 100644 --- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp +++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp @@ -54,6 +54,7 @@ #include "llvm/Support/RecyclingAllocator.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Utils/GuardUtils.h" #include #include #include @@ -863,7 +864,7 @@ bool EarlyCSE::processNode(DomTreeNode *Node) { continue; } - if (match(Inst, m_Intrinsic())) { + if (isGuard(Inst)) { if (auto *CondI = dyn_cast(cast(Inst)->getArgOperand(0))) { if (SimpleValue::canHandle(CondI)) { diff --git a/llvm/lib/Transforms/Scalar/GuardWidening.cpp b/llvm/lib/Transforms/Scalar/GuardWidening.cpp index 68be46e..428c8bf 100644 --- a/llvm/lib/Transforms/Scalar/GuardWidening.cpp +++ b/llvm/lib/Transforms/Scalar/GuardWidening.cpp @@ -57,6 +57,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/KnownBits.h" #include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Utils/GuardUtils.h" #include "llvm/Transforms/Utils/LoopUtils.h" using namespace llvm; @@ -107,12 +108,6 @@ static void setCondition(Instruction *I, Value *NewCond) { cast(I)->setCondition(NewCond); } -// Whether or not the particular instruction \p I is a guard. -static bool isGuard(const Instruction *I) { - using namespace llvm::PatternMatch; - return match(I, m_Intrinsic()); -} - // Eliminates the guard instruction properly. static void eliminateGuard(Instruction *GuardInst) { GuardInst->eraseFromParent(); diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp index 39895dc..ddcc3da 100644 --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -65,6 +65,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Transforms/Utils/GuardUtils.h" #include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/SSAUpdater.h" #include "llvm/Transforms/Utils/ValueMapper.h" @@ -2607,9 +2608,8 @@ bool JumpThreadingPass::ProcessGuards(BasicBlock *BB) { if (auto *BI = dyn_cast(Parent->getTerminator())) for (auto &I : *BB) - if (match(&I, m_Intrinsic())) - if (ThreadGuard(BB, cast(&I), BI)) - return true; + if (isGuard(&I) && ThreadGuard(BB, cast(&I), BI)) + return true; return false; } diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp index 94fcdaf..10f59d2 100644 --- a/llvm/lib/Transforms/Scalar/LICM.cpp +++ b/llvm/lib/Transforms/Scalar/LICM.cpp @@ -66,6 +66,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar/LoopPassManager.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" +#include "llvm/Transforms/Utils/GuardUtils.h" #include "llvm/Transforms/Utils/LoopUtils.h" #include "llvm/Transforms/Utils/SSAUpdater.h" #include @@ -527,7 +528,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI, using namespace PatternMatch; if (((I.use_empty() && match(&I, m_Intrinsic())) || - match(&I, m_Intrinsic())) && + isGuard(&I)) && IsMustExecute && IsMemoryNotModified && CurLoop->hasLoopInvariantOperands(&I)) { hoist(I, DT, CurLoop, SafetyInfo, ORE); diff --git a/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp b/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp index fac57014..dd81c97 100644 --- a/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp +++ b/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp @@ -49,10 +49,8 @@ static bool lowerGuardIntrinsic(Function &F) { SmallVector ToLower; for (auto &I : instructions(F)) - if (auto *CI = dyn_cast(&I)) - if (auto *F = CI->getCalledFunction()) - if (F->getIntrinsicID() == Intrinsic::experimental_guard) - ToLower.push_back(CI); + if (isGuard(&I)) + ToLower.push_back(cast(&I)); if (ToLower.empty()) return false; diff --git a/llvm/lib/Transforms/Utils/GuardUtils.cpp b/llvm/lib/Transforms/Utils/GuardUtils.cpp index 08de0a4..5a9a9df 100644 --- a/llvm/lib/Transforms/Utils/GuardUtils.cpp +++ b/llvm/lib/Transforms/Utils/GuardUtils.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/MDBuilder.h" +#include "llvm/IR/PatternMatch.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" using namespace llvm; @@ -24,6 +25,11 @@ static cl::opt PredicatePassBranchWeight( cl::desc("The probability of a guard failing is assumed to be the " "reciprocal of this value (default = 1 << 20)")); +bool llvm::isGuard(const User *U) { + using namespace llvm::PatternMatch; + return match(U, m_Intrinsic()); +} + void llvm::makeGuardControlFlowExplicit(Function *DeoptIntrinsic, CallInst *Guard) { OperandBundleDef DeoptOB(*Guard->getOperandBundle(LLVMContext::OB_deopt)); -- 2.7.4