diff options
author | Eugene Zelenko <eugene.zelenko@gmail.com> | 2017-08-31 21:56:16 +0000 |
---|---|---|
committer | Eugene Zelenko <eugene.zelenko@gmail.com> | 2017-08-31 21:56:16 +0000 |
commit | fa6434bebb74cb8db67a3835e18ebd3f1f176862 (patch) | |
tree | 97aab4d02aafcfea733775bab8fcb69fce3faf42 /llvm/lib | |
parent | 9da05740d34ef6cb0cb695d9df0b0a36d7f7951e (diff) | |
download | bcm5719-llvm-fa6434bebb74cb8db67a3835e18ebd3f1f176862.tar.gz bcm5719-llvm-fa6434bebb74cb8db67a3835e18ebd3f1f176862.zip |
[Analysis] Fix some Clang-tidy modernize-use-using and Include What You Use warnings; other minor fixes. Also affected in files (NFC).
llvm-svn: 312289
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/CGSCCPassManager.cpp | 47 | ||||
-rw-r--r-- | llvm/lib/Analysis/CallGraphSCCPass.cpp | 43 | ||||
-rw-r--r-- | llvm/lib/Target/AMDGPU/AMDGPUAnnotateKernelFeatures.cpp | 23 | ||||
-rw-r--r-- | llvm/lib/Transforms/Coroutines/CoroSplit.cpp | 51 | ||||
-rw-r--r-- | llvm/lib/Transforms/Coroutines/Coroutines.cpp | 27 | ||||
-rw-r--r-- | llvm/lib/Transforms/Scalar/LoopUnswitch.cpp | 81 |
6 files changed, 187 insertions, 85 deletions
diff --git a/llvm/lib/Analysis/CGSCCPassManager.cpp b/llvm/lib/Analysis/CGSCCPassManager.cpp index 46d384922af..9e6384437fb 100644 --- a/llvm/lib/Analysis/CGSCCPassManager.cpp +++ b/llvm/lib/Analysis/CGSCCPassManager.cpp @@ -8,8 +8,25 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/CGSCCPassManager.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/iterator_range.h" +#include "llvm/Analysis/LazyCallGraph.h" #include "llvm/IR/CallSite.h" +#include "llvm/IR/Constant.h" #include "llvm/IR/InstIterator.h" +#include "llvm/IR/Instruction.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" +#include <algorithm> +#include <cassert> +#include <iterator> #define DEBUG_TYPE "cgscc" @@ -262,7 +279,7 @@ bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate( return false; } -} // End llvm namespace +} // end namespace llvm /// When a new SCC is created for the graph and there might be function /// analysis results cached for the functions now in that SCC two forms of @@ -309,7 +326,6 @@ static void updateNewSCCFunctionAnalyses(LazyCallGraph::SCC &C, } } -namespace { /// Helper function to update both the \c CGSCCAnalysisManager \p AM and the \c /// CGSCCPassManager's \c CGSCCUpdateResult \p UR based on a range of newly /// added SCCs. @@ -321,11 +337,11 @@ namespace { /// This function returns the SCC containing \p N. This will be either \p C if /// no new SCCs have been split out, or it will be the new SCC containing \p N. template <typename SCCRangeT> -LazyCallGraph::SCC * +static LazyCallGraph::SCC * incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, LazyCallGraph::Node &N, LazyCallGraph::SCC *C, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { - typedef LazyCallGraph::SCC SCC; + using SCC = LazyCallGraph::SCC; if (NewSCCRange.begin() == NewSCCRange.end()) return C; @@ -363,8 +379,8 @@ incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, if (NeedFAMProxy) updateNewSCCFunctionAnalyses(*C, G, AM); - for (SCC &NewC : - reverse(make_range(std::next(NewSCCRange.begin()), NewSCCRange.end()))) { + for (SCC &NewC : llvm::reverse(make_range(std::next(NewSCCRange.begin()), + NewSCCRange.end()))) { assert(C != &NewC && "No need to re-visit the current SCC!"); assert(OldC != &NewC && "Already handled the original SCC!"); UR.CWorklist.insert(&NewC); @@ -380,15 +396,14 @@ incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, } return C; } -} LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { - typedef LazyCallGraph::Node Node; - typedef LazyCallGraph::Edge Edge; - typedef LazyCallGraph::SCC SCC; - typedef LazyCallGraph::RefSCC RefSCC; + using Node = LazyCallGraph::Node; + using Edge = LazyCallGraph::Edge; + using SCC = LazyCallGraph::SCC; + using RefSCC = LazyCallGraph::RefSCC; RefSCC &InitialRC = InitialC.getOuterRefSCC(); SCC *C = &InitialC; @@ -430,7 +445,7 @@ LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( // Now walk all references. for (Instruction &I : instructions(F)) for (Value *Op : I.operand_values()) - if (Constant *C = dyn_cast<Constant>(Op)) + if (auto *C = dyn_cast<Constant>(Op)) if (Visited.insert(C).second) Worklist.push_back(C); @@ -521,8 +536,8 @@ LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( // "bottom" we will continue processing in the bottom-up walk. assert(NewRefSCCs.front() == RC && "New current RefSCC not first in the returned list!"); - for (RefSCC *NewRC : - reverse(make_range(std::next(NewRefSCCs.begin()), NewRefSCCs.end()))) { + for (RefSCC *NewRC : llvm::reverse(make_range(std::next(NewRefSCCs.begin()), + NewRefSCCs.end()))) { assert(NewRC != RC && "Should not encounter the current RefSCC further " "in the postorder list of new RefSCCs."); UR.RCWorklist.insert(NewRC); @@ -644,8 +659,8 @@ LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist: " << *C << "\n"); // Enqueue in reverse order as we pop off the back of the worklist. - for (SCC &MovedC : reverse(make_range(RC->begin() + InitialSCCIndex, - RC->begin() + NewSCCIndex))) { + for (SCC &MovedC : llvm::reverse(make_range(RC->begin() + InitialSCCIndex, + RC->begin() + NewSCCIndex))) { UR.CWorklist.insert(&MovedC); DEBUG(dbgs() << "Enqueuing a newly earlier in post-order SCC: " << MovedC << "\n"); diff --git a/llvm/lib/Analysis/CallGraphSCCPass.cpp b/llvm/lib/Analysis/CallGraphSCCPass.cpp index facda246936..a2dda58a6a2 100644 --- a/llvm/lib/Analysis/CallGraphSCCPass.cpp +++ b/llvm/lib/Analysis/CallGraphSCCPass.cpp @@ -16,18 +16,27 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/CallGraphSCCPass.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SCCIterator.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/CallGraph.h" +#include "llvm/IR/CallSite.h" #include "llvm/IR/Function.h" -#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/Intrinsics.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassManagers.h" +#include "llvm/IR/Module.h" #include "llvm/IR/OptBisect.h" +#include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Timer.h" #include "llvm/Support/raw_ostream.h" +#include <cassert> +#include <string> +#include <utility> +#include <vector> + using namespace llvm; #define DEBUG_TYPE "cgscc-passmgr" @@ -47,8 +56,8 @@ namespace { class CGPassManager : public ModulePass, public PMDataManager { public: static char ID; - explicit CGPassManager() - : ModulePass(ID), PMDataManager() { } + + explicit CGPassManager() : ModulePass(ID), PMDataManager() {} /// Execute all of the passes scheduled for execution. Keep track of /// whether any of the passes modifies the module, and if so, return true. @@ -106,7 +115,6 @@ private: char CGPassManager::ID = 0; - bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, CallGraph &CG, bool &CallGraphUpToDate, bool &DevirtualizedCall) { @@ -135,7 +143,6 @@ bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, return Changed; } - assert(PM->getPassManagerType() == PMT_FunctionPassManager && "Invalid CGPassManager member"); FPPassManager *FPP = (FPPassManager*)P; @@ -162,7 +169,6 @@ bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, return Changed; } - /// Scan the functions in the specified CFG and resync the /// callgraph with the call sites found in it. This is used after /// FunctionPasses have potentially munged the callgraph, and can be used after @@ -172,7 +178,6 @@ bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, /// meaning it turned an indirect call into a direct call. This happens when /// a function pass like GVN optimizes away stuff feeding the indirect call. /// This never happens in checking mode. -/// bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, bool CheckingMode) { DenseMap<Value*, CallGraphNode*> CallSites; @@ -484,7 +489,6 @@ bool CGPassManager::runOnModule(Module &M) { return Changed; } - /// Initialize CG bool CGPassManager::doInitialization(CallGraph &CG) { bool Changed = false; @@ -536,7 +540,6 @@ void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { CGI->ReplaceNode(Old, New); } - //===----------------------------------------------------------------------===// // CallGraphSCCPass Implementation //===----------------------------------------------------------------------===// @@ -586,22 +589,23 @@ void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { AU.addPreserved<CallGraphWrapperPass>(); } - //===----------------------------------------------------------------------===// // PrintCallGraphPass Implementation //===----------------------------------------------------------------------===// namespace { + /// PrintCallGraphPass - Print a Module corresponding to a call graph. /// class PrintCallGraphPass : public CallGraphSCCPass { std::string Banner; - raw_ostream &Out; // raw_ostream to print on. + raw_ostream &OS; // raw_ostream to print on. public: static char ID; - PrintCallGraphPass(const std::string &B, raw_ostream &o) - : CallGraphSCCPass(ID), Banner(B), Out(o) {} + + PrintCallGraphPass(const std::string &B, raw_ostream &OS) + : CallGraphSCCPass(ID), Banner(B), OS(OS) {} void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesAll(); @@ -612,18 +616,18 @@ namespace { auto PrintBannerOnce = [&] () { if (BannerPrinted) return; - Out << Banner; + OS << Banner; BannerPrinted = true; }; for (CallGraphNode *CGN : SCC) { if (Function *F = CGN->getFunction()) { if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) { PrintBannerOnce(); - F->print(Out); + F->print(OS); } - } else if (llvm::isFunctionInPrintList("*")) { + } else if (isFunctionInPrintList("*")) { PrintBannerOnce(); - Out << "\nPrinting <null> Function\n"; + OS << "\nPrinting <null> Function\n"; } } return false; @@ -636,9 +640,9 @@ namespace { char PrintCallGraphPass::ID = 0; -Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O, +Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS, const std::string &Banner) const { - return new PrintCallGraphPass(Banner, O); + return new PrintCallGraphPass(Banner, OS); } bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const { @@ -649,5 +653,6 @@ bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const { } char DummyCGSCCPass::ID = 0; + INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false, false) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAnnotateKernelFeatures.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAnnotateKernelFeatures.cpp index 551737c1d27..ce17202f341 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUAnnotateKernelFeatures.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUAnnotateKernelFeatures.cpp @@ -1,4 +1,4 @@ -//===-- AMDGPUAnnotateKernelFeaturesPass.cpp ------------------------------===// +//===- AMDGPUAnnotateKernelFeaturesPass.cpp -------------------------------===// // // The LLVM Compiler Infrastructure // @@ -14,13 +14,28 @@ #include "AMDGPU.h" #include "AMDGPUSubtarget.h" +#include "Utils/AMDGPUBaseInfo.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" +#include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CallGraphSCCPass.h" #include "llvm/CodeGen/TargetPassConfig.h" +#include "llvm/IR/CallSite.h" +#include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" -#include "llvm/IR/InstIterator.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/Intrinsics.h" #include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" +#include "llvm/IR/Use.h" +#include "llvm/Pass.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Target/TargetMachine.h" #define DEBUG_TYPE "amdgpu-annotate-kernel-features" @@ -42,6 +57,7 @@ public: bool doInitialization(CallGraph &CG) override; bool runOnSCC(CallGraphSCC &SCC) override; + StringRef getPassName() const override { return "AMDGPU Annotate Kernel Features"; } @@ -58,7 +74,7 @@ public: AMDGPUAS AS); }; -} +} // end anonymous namespace char AMDGPUAnnotateKernelFeatures::ID = 0; @@ -294,7 +310,6 @@ bool AMDGPUAnnotateKernelFeatures::runOnSCC(CallGraphSCC &SCC) { Changed |= addFeatureAttributes(*F); } - return Changed; } diff --git a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp index f36827e0ffa..8712ca4823c 100644 --- a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp @@ -19,18 +19,53 @@ // coroutine. //===----------------------------------------------------------------------===// +#include "CoroInstr.h" #include "CoroInternal.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CallGraphSCCPass.h" -#include "llvm/IR/DebugInfoMetadata.h" +#include "llvm/IR/Argument.h" +#include "llvm/IR/Attributes.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/CFG.h" +#include "llvm/IR/CallSite.h" +#include "llvm/IR/CallingConv.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/GlobalValue.h" +#include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstIterator.h" +#include "llvm/IR/InstrTypes.h" +#include "llvm/IR/Instruction.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" +#include "llvm/IR/Value.h" #include "llvm/IR/Verifier.h" +#include "llvm/Pass.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/ValueMapper.h" +#include <cassert> +#include <cstddef> +#include <cstdint> +#include <initializer_list> +#include <iterator> using namespace llvm; @@ -343,7 +378,6 @@ static void replaceFrameSize(coro::Shape &Shape) { // Assumes that all the functions have the same signature. static void setCoroInfo(Function &F, CoroBeginInst *CoroBegin, std::initializer_list<Function *> Fns) { - SmallVector<Constant *, 4> Args(Fns.begin(), Fns.end()); assert(!Args.empty()); Function *Part = *Fns.begin(); @@ -364,7 +398,6 @@ static void setCoroInfo(Function &F, CoroBeginInst *CoroBegin, // Store addresses of Resume/Destroy/Cleanup functions in the coroutine frame. static void updateCoroFrame(coro::Shape &Shape, Function *ResumeFn, Function *DestroyFn, Function *CleanupFn) { - IRBuilder<> Builder(Shape.FramePtr->getNextNode()); auto *ResumeAddr = Builder.CreateConstInBoundsGEP2_32( Shape.FrameTy, Shape.FramePtr, 0, coro::Shape::ResumeField, @@ -388,7 +421,7 @@ static void updateCoroFrame(coro::Shape &Shape, Function *ResumeFn, static void postSplitCleanup(Function &F) { removeUnreachableBlocks(F); - llvm::legacy::FunctionPassManager FPM(F.getParent()); + legacy::FunctionPassManager FPM(F.getParent()); FPM.add(createVerifierPass()); FPM.add(createSCCPPass()); @@ -478,7 +511,7 @@ static void addMustTailToCoroResumes(Function &F) { // Set musttail on those that are followed by a ret instruction. for (CallInst *Call : Resumes) if (simplifyTerminatorLeadingToRet(Call->getNextNode())) { - Call->setTailCallKind(llvm::CallInst::TCK_MustTail); + Call->setTailCallKind(CallInst::TCK_MustTail); changed = true; } @@ -574,7 +607,7 @@ static void simplifySuspendPoints(coro::Shape &Shape) { size_t I = 0, N = S.size(); if (N == 0) return; - for (;;) { + while (true) { if (simplifySuspendPoint(S[I], Shape.CoroBegin)) { if (--N == I) break; @@ -769,6 +802,7 @@ namespace { struct CoroSplit : public CallGraphSCCPass { static char ID; // Pass identification, replacement for typeid + CoroSplit() : CallGraphSCCPass(ID) { initializeCoroSplitPass(*PassRegistry::getPassRegistry()); } @@ -817,11 +851,14 @@ struct CoroSplit : public CallGraphSCCPass { void getAnalysisUsage(AnalysisUsage &AU) const override { CallGraphSCCPass::getAnalysisUsage(AU); } + StringRef getPassName() const override { return "Coroutine Splitting"; } }; -} + +} // end anonymous namespace char CoroSplit::ID = 0; + INITIALIZE_PASS( CoroSplit, "coro-split", "Split coroutine into a set of functions driving its state machine", false, diff --git a/llvm/lib/Transforms/Coroutines/Coroutines.cpp b/llvm/lib/Transforms/Coroutines/Coroutines.cpp index 44e1f9b404e..10411c1bd65 100644 --- a/llvm/lib/Transforms/Coroutines/Coroutines.cpp +++ b/llvm/lib/Transforms/Coroutines/Coroutines.cpp @@ -1,4 +1,4 @@ -//===-- Coroutines.cpp ----------------------------------------------------===// +//===- Coroutines.cpp -----------------------------------------------------===// // // The LLVM Compiler Infrastructure // @@ -6,18 +6,38 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// +// // This file implements the common infrastructure for Coroutine Passes. +// //===----------------------------------------------------------------------===// +#include "CoroInstr.h" #include "CoroInternal.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CallGraphSCCPass.h" +#include "llvm/IR/Attributes.h" +#include "llvm/IR/CallSite.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" #include "llvm/IR/InstIterator.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/Intrinsics.h" #include "llvm/IR/LegacyPassManager.h" -#include "llvm/IR/Verifier.h" -#include "llvm/InitializePasses.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Transforms/Coroutines.h" #include "llvm/Transforms/IPO.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" #include "llvm/Transforms/Utils/Local.h" +#include <cassert> +#include <cstddef> +#include <utility> using namespace llvm; @@ -117,7 +137,6 @@ static bool isCoroutineIntrinsicName(StringRef Name) { // that names are intrinsic names. bool coro::declaresIntrinsics(Module &M, std::initializer_list<StringRef> List) { - for (StringRef Name : List) { assert(isCoroutineIntrinsicName(Name) && "not a coroutine intrinsic"); if (M.getNamedValue(Name)) diff --git a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp index bf830dd6377..71d4d5d080e 100644 --- a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -1,4 +1,4 @@ -//===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===// +//===- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop -------===// // // The LLVM Compiler Infrastructure // @@ -26,30 +26,40 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AssumptionCache.h" -#include "llvm/Analysis/BlockFrequencyInfo.h" -#include "llvm/Analysis/BlockFrequencyInfoImpl.h" -#include "llvm/Analysis/BranchProbabilityInfo.h" #include "llvm/Analysis/CodeMetrics.h" #include "llvm/Analysis/DivergenceAnalysis.h" -#include "llvm/Analysis/GlobalsModRef.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/TargetTransformInfo.h" +#include "llvm/IR/Attributes.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/CallSite.h" +#include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstrTypes.h" +#include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" -#include "llvm/IR/MDBuilder.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/Intrinsics.h" #include "llvm/IR/Module.h" -#include "llvm/Support/BranchProbability.h" +#include "llvm/IR/Type.h" +#include "llvm/IR/User.h" +#include "llvm/IR/Value.h" +#include "llvm/IR/ValueHandle.h" +#include "llvm/Pass.h" +#include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" @@ -58,9 +68,15 @@ #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/LoopUtils.h" +#include "llvm/Transforms/Utils/ValueMapper.h" #include <algorithm> +#include <cassert> #include <map> #include <set> +#include <tuple> +#include <utility> +#include <vector> + using namespace llvm; #define DEBUG_TYPE "loop-unswitch" @@ -82,11 +98,9 @@ Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), namespace { class LUAnalysisCache { - - typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> > - UnswitchedValsMap; - - typedef UnswitchedValsMap::iterator UnswitchedValsIt; + using UnswitchedValsMap = + DenseMap<const SwitchInst *, SmallPtrSet<const Value *, 8>>; + using UnswitchedValsIt = UnswitchedValsMap::iterator; struct LoopProperties { unsigned CanBeUnswitchedCount; @@ -97,12 +111,12 @@ namespace { // Here we use std::map instead of DenseMap, since we need to keep valid // LoopProperties pointer for current loop for better performance. - typedef std::map<const Loop*, LoopProperties> LoopPropsMap; - typedef LoopPropsMap::iterator LoopPropsMapIt; + using LoopPropsMap = std::map<const Loop *, LoopProperties>; + using LoopPropsMapIt = LoopPropsMap::iterator; LoopPropsMap LoopsProperties; - UnswitchedValsMap *CurLoopInstructions; - LoopProperties *CurrentLoopProperties; + UnswitchedValsMap *CurLoopInstructions = nullptr; + LoopProperties *CurrentLoopProperties = nullptr; // A loop unswitching with an estimated cost above this threshold // is not performed. MaxSize is turned into unswitching quota for @@ -121,9 +135,7 @@ namespace { unsigned MaxSize; public: - LUAnalysisCache() - : CurLoopInstructions(nullptr), CurrentLoopProperties(nullptr), - MaxSize(Threshold) {} + LUAnalysisCache() : MaxSize(Threshold) {} // Analyze loop. Check its size, calculate is it possible to unswitch // it. Returns true if we can unswitch this loop. @@ -164,12 +176,12 @@ namespace { LUAnalysisCache BranchesInfo; bool OptimizeForSize; - bool redoLoop; + bool redoLoop = false; - Loop *currentLoop; - DominatorTree *DT; - BasicBlock *loopHeader; - BasicBlock *loopPreheader; + Loop *currentLoop = nullptr; + DominatorTree *DT = nullptr; + BasicBlock *loopHeader = nullptr; + BasicBlock *loopPreheader = nullptr; bool SanitizeMemory; LoopSafetyInfo SafetyInfo; @@ -185,16 +197,17 @@ namespace { public: static char ID; // Pass ID, replacement for typeid - explicit LoopUnswitch(bool Os = false, bool hasBranchDivergence = false) : - LoopPass(ID), OptimizeForSize(Os), redoLoop(false), - currentLoop(nullptr), DT(nullptr), loopHeader(nullptr), - loopPreheader(nullptr), hasBranchDivergence(hasBranchDivergence) { + + explicit LoopUnswitch(bool Os = false, bool hasBranchDivergence = false) + : LoopPass(ID), OptimizeForSize(Os), + hasBranchDivergence(hasBranchDivergence) { initializeLoopUnswitchPass(*PassRegistry::getPassRegistry()); - } + } bool runOnLoop(Loop *L, LPPassManager &LPM) override; bool processCurrentLoop(); bool isUnreachableDueToPreviousUnswitching(BasicBlock *); + /// This transformation requires natural loop information & requires that /// loop preheaders be inserted into the CFG. /// @@ -207,7 +220,6 @@ namespace { } private: - void releaseMemory() override { BranchesInfo.forgetLoop(currentLoop); } @@ -247,13 +259,13 @@ namespace { Value *SimplifyInstructionWithNotEqual(Instruction *Inst, Value *Invariant, Constant *Val); }; -} + +} // end anonymous namespace // Analyze loop. Check its size, calculate is it possible to unswitch // it. Returns true if we can unswitch this loop. bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI, AssumptionCache *AC) { - LoopPropsMapIt PropsIt; bool Inserted; std::tie(PropsIt, Inserted) = @@ -302,7 +314,6 @@ bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI, // Clean all data related to given loop. void LUAnalysisCache::forgetLoop(const Loop *L) { - LoopPropsMapIt LIt = LoopsProperties.find(L); if (LIt != LoopsProperties.end()) { @@ -337,7 +348,6 @@ bool LUAnalysisCache::CostAllowsUnswitching() { // Note, that new loop data is stored inside the VMap. void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop, const ValueToValueMapTy &VMap) { - LoopProperties &NewLoopProps = LoopsProperties[NewLoop]; LoopProperties &OldLoopProps = *CurrentLoopProperties; UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals; @@ -367,6 +377,7 @@ void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop, } char LoopUnswitch::ID = 0; + INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops", false, false) INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |