diff --git a/include/llvm/Transforms/Utils/PredicateInfo.h b/include/llvm/Transforms/Utils/PredicateInfo.h index bc1e405a56c8..8150f1528397 100644 --- a/include/llvm/Transforms/Utils/PredicateInfo.h +++ b/include/llvm/Transforms/Utils/PredicateInfo.h @@ -1,4 +1,4 @@ -//===- PredicateInfo.h - Build PredicateInfo --------------------*- C++ -*-===// +//===- PredicateInfo.h - Build PredicateInfo ----------------------*-C++-*-===// // // The LLVM Compiler Infrastructure // @@ -6,7 +6,7 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// -// +/// /// \file /// \brief This file implements the PredicateInfo analysis, which creates an Extended /// SSA form for operations used in branch comparisons and llvm.assume @@ -44,7 +44,8 @@ /// inserted where it would actually be live. This means if there are no uses of /// an operation dominated by the branch edges, or by an assume, the associated /// predicate info is never inserted. -// +/// +/// //===----------------------------------------------------------------------===// #ifndef LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H @@ -56,27 +57,39 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/ilist.h" #include "llvm/ADT/ilist_node.h" +#include "llvm/ADT/iterator.h" +#include "llvm/Analysis/AssumptionCache.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Dominators.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/OperandTraits.h" +#include "llvm/IR/Type.h" +#include "llvm/IR/Use.h" +#include "llvm/IR/User.h" +#include "llvm/IR/Value.h" #include "llvm/Pass.h" +#include "llvm/PassAnalysisSupport.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Transforms/Utils/OrderedInstructions.h" +#include +#include +#include +#include +#include #include namespace llvm { -class AssumptionCache; -class BasicBlock; class DominatorTree; class Function; -class IntrinsicInst; +class Instruction; +class MemoryAccess; +class LLVMContext; class raw_ostream; -class Value; - -// This name is used in a few places, so kick it into their own namespace -namespace PredicateInfoClasses { - -struct ValueDFS; - -} enum PredicateType { PT_Branch, PT_Assume, PT_Switch }; @@ -85,12 +98,10 @@ enum PredicateType { PT_Branch, PT_Assume, PT_Switch }; class PredicateBase : public ilist_node { public: PredicateType Type; - // The original operand before we renamed it. // This can be use by passes, when destroying predicateinfo, to know // whether they can just drop the intrinsic, or have to merge metadata. Value *OriginalOp; - PredicateBase(const PredicateBase &) = delete; PredicateBase &operator=(const PredicateBase &) = delete; PredicateBase() = delete; @@ -103,7 +114,6 @@ class PredicateBase : public ilist_node { class PredicateWithCondition : public PredicateBase { public: Value *Condition; - static bool classof(const PredicateBase *PB) { return PB->Type == PT_Assume || PB->Type == PT_Branch || PB->Type == PT_Switch; @@ -120,12 +130,10 @@ class PredicateWithCondition : public PredicateBase { class PredicateAssume : public PredicateWithCondition { public: IntrinsicInst *AssumeInst; - PredicateAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition) : PredicateWithCondition(PT_Assume, Op, Condition), AssumeInst(AssumeInst) {} PredicateAssume() = delete; - static bool classof(const PredicateBase *PB) { return PB->Type == PT_Assume; } @@ -138,9 +146,7 @@ class PredicateWithEdge : public PredicateWithCondition { public: BasicBlock *From; BasicBlock *To; - PredicateWithEdge() = delete; - static bool classof(const PredicateBase *PB) { return PB->Type == PT_Branch || PB->Type == PT_Switch; } @@ -156,13 +162,11 @@ class PredicateBranch : public PredicateWithEdge { public: // If true, SplitBB is the true successor, otherwise it's the false successor. bool TrueEdge; - PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB, Value *Condition, bool TakenEdge) : PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition), TrueEdge(TakenEdge) {} PredicateBranch() = delete; - static bool classof(const PredicateBase *PB) { return PB->Type == PT_Branch; } @@ -171,22 +175,24 @@ class PredicateBranch : public PredicateWithEdge { class PredicateSwitch : public PredicateWithEdge { public: Value *CaseValue; - // This is the switch instruction. SwitchInst *Switch; - PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB, Value *CaseValue, SwitchInst *SI) : PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB, SI->getCondition()), CaseValue(CaseValue), Switch(SI) {} PredicateSwitch() = delete; - static bool classof(const PredicateBase *PB) { return PB->Type == PT_Switch; } }; +// This name is used in a few places, so kick it into their own namespace +namespace PredicateInfoClasses { +struct ValueDFS; +} + /// \brief Encapsulates PredicateInfo, including all data associated with memory /// accesses. class PredicateInfo { @@ -199,12 +205,11 @@ class PredicateInfo { SmallVector Infos; SmallVector UninsertedInfos; }; - // This owns the all the predicate infos in the function, placed or not. iplist AllInfos; public: - PredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC); + PredicateInfo(Function &, DominatorTree &, AssumptionCache &); ~PredicateInfo(); void verifyPredicateInfo() const; @@ -222,14 +227,13 @@ class PredicateInfo { friend class PredicateInfoPrinterLegacyPass; private: - using ValueDFS = PredicateInfoClasses::ValueDFS; - using ValueDFSStack = SmallVectorImpl; - void buildPredicateInfo(); void processAssume(IntrinsicInst *, BasicBlock *, SmallPtrSetImpl &); void processBranch(BranchInst *, BasicBlock *, SmallPtrSetImpl &); void processSwitch(SwitchInst *, BasicBlock *, SmallPtrSetImpl &); void renameUses(SmallPtrSetImpl &); + using ValueDFS = PredicateInfoClasses::ValueDFS; + typedef SmallVectorImpl ValueDFSStack; void convertUsesToDFSOrdered(Value *, SmallVectorImpl &); Value *materializeStack(unsigned int &, ValueDFSStack &, Value *); bool stackIsInScope(const ValueDFSStack &, const ValueDFS &) const; @@ -238,27 +242,22 @@ class PredicateInfo { void addInfoFor(SmallPtrSetImpl &OpsToRename, Value *Op, PredicateBase *PB); const ValueInfo &getValueInfo(Value *) const; - Function &F; DominatorTree &DT; AssumptionCache &AC; OrderedInstructions OI; - // This maps from copy operands to Predicate Info. Note that it does not own // the Predicate Info, they belong to the ValueInfo structs in the ValueInfos // vector. DenseMap PredicateMap; - // This stores info about each operand or comparison result we make copies // of. The real ValueInfos start at index 1, index 0 is unused so that we can // more easily detect invalid indexing. SmallVector ValueInfos; - // This gives the index into the ValueInfos array for a given Value. Because // 0 is not a valid Value Info index, you can use DenseMap::lookup and tell // whether it returned a valid result. DenseMap ValueInfoNums; - // The set of edges along which we can only handle phi uses, due to critical // edges. DenseSet> EdgeUsesOnly; @@ -269,10 +268,9 @@ class PredicateInfo { // the tests to be able to build, dump, and verify PredicateInfo. class PredicateInfoPrinterLegacyPass : public FunctionPass { public: - static char ID; - PredicateInfoPrinterLegacyPass(); + static char ID; bool runOnFunction(Function &) override; void getAnalysisUsage(AnalysisUsage &AU) const override; }; @@ -284,7 +282,6 @@ class PredicateInfoPrinterPass public: explicit PredicateInfoPrinterPass(raw_ostream &OS) : OS(OS) {} - PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); }; diff --git a/lib/Transforms/Utils/PredicateInfo.cpp b/lib/Transforms/Utils/PredicateInfo.cpp index 4002c71fbf39..16b80923bf55 100644 --- a/lib/Transforms/Utils/PredicateInfo.cpp +++ b/lib/Transforms/Utils/PredicateInfo.cpp @@ -1,78 +1,61 @@ -//===- PredicateInfo.cpp - PredicateInfo Builder --------------------------===// +//===-- PredicateInfo.cpp - PredicateInfo Builder--------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // -//===----------------------------------------------------------------------===// +//===----------------------------------------------------------------===// // // This file implements the PredicateInfo class. // -//===----------------------------------------------------------------------===// +//===----------------------------------------------------------------===// #include "llvm/Transforms/Utils/PredicateInfo.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/Twine.h" +#include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AssumptionCache.h" -#include "llvm/IR/Argument.h" +#include "llvm/Analysis/CFG.h" #include "llvm/IR/AssemblyAnnotationWriter.h" -#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/DataLayout.h" #include "llvm/IR/Dominators.h" -#include "llvm/IR/Function.h" +#include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" -#include "llvm/IR/InstrTypes.h" -#include "llvm/IR/Instruction.h" -#include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" -#include "llvm/IR/Intrinsics.h" -#include "llvm/IR/PassManager.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Metadata.h" +#include "llvm/IR/Module.h" #include "llvm/IR/PatternMatch.h" -#include "llvm/IR/Use.h" -#include "llvm/IR/User.h" -#include "llvm/IR/Value.h" -#include "llvm/Pass.h" -#include "llvm/Support/Casting.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/DebugCounter.h" -#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FormattedStream.h" -#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/OrderedInstructions.h" #include -#include -#include -#include -#include - +#define DEBUG_TYPE "predicateinfo" using namespace llvm; using namespace PatternMatch; using namespace llvm::PredicateInfoClasses; -#define DEBUG_TYPE "predicateinfo" - INITIALIZE_PASS_BEGIN(PredicateInfoPrinterLegacyPass, "print-predicateinfo", "PredicateInfo Printer", false, false) INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) INITIALIZE_PASS_END(PredicateInfoPrinterLegacyPass, "print-predicateinfo", "PredicateInfo Printer", false, false) - static cl::opt VerifyPredicateInfo( "verify-predicateinfo", cl::init(false), cl::Hidden, cl::desc("Verify PredicateInfo in legacy printer pass.")); - DEBUG_COUNTER(RenameCounter, "predicateinfo-rename", "Controls which variables are renamed with predicateinfo"); +namespace { // Given a predicate info that is a type of branching terminator, get the // branching block. -static const BasicBlock *getBranchBlock(const PredicateBase *PB) { +const BasicBlock *getBranchBlock(const PredicateBase *PB) { assert(isa(PB) && "Only branches and switches should have PHIOnly defs that " "require branch blocks."); @@ -89,31 +72,17 @@ static Instruction *getBranchTerminator(const PredicateBase *PB) { // Given a predicate info that is a type of branching terminator, get the // edge this predicate info represents -static const std::pair +const std::pair getBlockEdge(const PredicateBase *PB) { assert(isa(PB) && "Not a predicate info type we know how to get an edge from."); const auto *PEdge = cast(PB); return std::make_pair(PEdge->From, PEdge->To); } - -// Perform a strict weak ordering on instructions and arguments. -static bool valueComesBefore(OrderedInstructions &OI, const Value *A, - const Value *B) { - auto *ArgA = dyn_cast_or_null(A); - auto *ArgB = dyn_cast_or_null(B); - if (ArgA && !ArgB) - return true; - if (ArgB && !ArgA) - return false; - if (ArgA && ArgB) - return ArgA->getArgNo() < ArgB->getArgNo(); - return OI.dominates(cast(A), cast(B)); } namespace llvm { namespace PredicateInfoClasses { - enum LocalNum { // Operations that must appear first in the block. LN_First, @@ -138,12 +107,25 @@ struct ValueDFS { bool EdgeOnly = false; }; +// Perform a strict weak ordering on instructions and arguments. +static bool valueComesBefore(OrderedInstructions &OI, const Value *A, + const Value *B) { + auto *ArgA = dyn_cast_or_null(A); + auto *ArgB = dyn_cast_or_null(B); + if (ArgA && !ArgB) + return true; + if (ArgB && !ArgA) + return false; + if (ArgA && ArgB) + return ArgA->getArgNo() < ArgB->getArgNo(); + return OI.dominates(cast(A), cast(B)); +} + // This compares ValueDFS structures, creating OrderedBasicBlocks where // necessary to compare uses/defs in the same block. Doing so allows us to walk // the minimum number of instructions necessary to compute our def/use ordering. struct ValueDFS_Compare { OrderedInstructions &OI; - ValueDFS_Compare(OrderedInstructions &OI) : OI(OI) {} bool operator()(const ValueDFS &A, const ValueDFS &B) const { @@ -237,8 +219,7 @@ struct ValueDFS_Compare { } }; -} // end namespace PredicateInfoClasses -} // end namespace llvm +} // namespace PredicateInfoClasses bool PredicateInfo::stackIsInScope(const ValueDFSStack &Stack, const ValueDFS &VDUse) const { @@ -308,8 +289,7 @@ void PredicateInfo::convertUsesToDFSOrdered( // Collect relevant operations from Comparison that we may want to insert copies // for. -static void collectCmpOps(CmpInst *Comparison, - SmallVectorImpl &CmpOperands) { +void collectCmpOps(CmpInst *Comparison, SmallVectorImpl &CmpOperands) { auto *Op0 = Comparison->getOperand(0); auto *Op1 = Comparison->getOperand(1); if (Op0 == Op1) @@ -444,7 +424,6 @@ void PredicateInfo::processBranch(BranchInst *BI, BasicBlock *BranchBB, CmpOperands.clear(); } } - // Process a block terminating switch, and place relevant operations to be // renamed into OpsToRename. void PredicateInfo::processSwitch(SwitchInst *SI, BasicBlock *BranchBB, @@ -718,7 +697,7 @@ PredicateInfo::PredicateInfo(Function &F, DominatorTree &DT, buildPredicateInfo(); } -PredicateInfo::~PredicateInfo() = default; +PredicateInfo::~PredicateInfo() {} void PredicateInfo::verifyPredicateInfo() const {} @@ -760,17 +739,16 @@ PreservedAnalyses PredicateInfoPrinterPass::run(Function &F, /// comments. class PredicateInfoAnnotatedWriter : public AssemblyAnnotationWriter { friend class PredicateInfo; - const PredicateInfo *PredInfo; public: PredicateInfoAnnotatedWriter(const PredicateInfo *M) : PredInfo(M) {} - void emitBasicBlockStartAnnot(const BasicBlock *BB, - formatted_raw_ostream &OS) override {} + virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, + formatted_raw_ostream &OS) {} - void emitInstructionAnnot(const Instruction *I, - formatted_raw_ostream &OS) override { + virtual void emitInstructionAnnot(const Instruction *I, + formatted_raw_ostream &OS) { if (const auto *PI = PredInfo->getPredicateInfoFor(I)) { OS << "; Has predicate info\n"; if (const auto *PB = dyn_cast(PI)) { @@ -813,3 +791,4 @@ PreservedAnalyses PredicateInfoVerifierPass::run(Function &F, return PreservedAnalyses::all(); } +}