diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp --- a/clang/unittests/Tooling/RefactoringTest.cpp +++ b/clang/unittests/Tooling/RefactoringTest.cpp @@ -1288,7 +1288,7 @@ TEST_F(AtomicChangeTest, Metadata) { AtomicChange Change(Context.Sources, DefaultLoc, 17); const llvm::Any &Metadata = Change.getMetadata(); - ASSERT_TRUE(llvm::any_isa(Metadata)); + ASSERT_TRUE(llvm::any_cast(&Metadata)); EXPECT_EQ(llvm::any_cast(Metadata), 17); } diff --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp --- a/clang/unittests/Tooling/TransformerTest.cpp +++ b/clang/unittests/Tooling/TransformerTest.cpp @@ -806,7 +806,7 @@ "clang-tool", std::make_shared(), {})); ASSERT_EQ(Changes.size(), 1u); const llvm::Any &Metadata = Changes[0].getMetadata(); - ASSERT_TRUE(llvm::any_isa(Metadata)); + ASSERT_TRUE(llvm::any_cast(&Metadata)); EXPECT_THAT(llvm::any_cast(Metadata), 5); } diff --git a/lldb/include/lldb/Core/RichManglingContext.h b/lldb/include/lldb/Core/RichManglingContext.h --- a/lldb/include/lldb/Core/RichManglingContext.h +++ b/lldb/include/lldb/Core/RichManglingContext.h @@ -87,8 +87,8 @@ /// can't access CPlusPlusLanguage::MethodName from within the header. template static ParserT *get(llvm::Any parser) { assert(parser.has_value()); - assert(llvm::any_isa(parser)); - return llvm::any_cast(parser); + assert(llvm::any_cast(&parser)); + return *llvm::any_cast(&parser); } }; diff --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h --- a/llvm/include/llvm/ADT/Any.h +++ b/llvm/include/llvm/ADT/Any.h @@ -107,6 +107,13 @@ void reset() { Storage.reset(); } private: + // Only used for the internal llvm::Any implementation + template bool isa() const { + if (!Storage) + return false; + return Storage->id() == &Any::TypeId>::Id; + } + template friend T any_cast(const Any &Value); template friend T any_cast(Any &Value); template friend T any_cast(Any &&Value); @@ -119,36 +126,37 @@ template char Any::TypeId::Id = 0; -template bool any_isa(const Any &Value) { - if (!Value.Storage) - return false; - return Value.Storage->id() == &Any::TypeId>::Id; +template +LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast") +bool any_isa(const Any &Value) { + return Value.isa(); } template T any_cast(const Any &Value) { + assert(Value.isa() && "Bad any cast!"); return static_cast(*any_cast>(&Value)); } template T any_cast(Any &Value) { + assert(Value.isa() && "Bad any cast!"); return static_cast(*any_cast>(&Value)); } template T any_cast(Any &&Value) { + assert(Value.isa() && "Bad any cast!"); return static_cast(std::move(*any_cast>(&Value))); } template const T *any_cast(const Any *Value) { using U = remove_cvref_t; - assert(Value && any_isa(*Value) && "Bad any cast!"); - if (!Value || !any_isa(*Value)) + if (!Value || !Value->isa()) return nullptr; return &static_cast &>(*Value->Storage).Value; } template T *any_cast(Any *Value) { using U = std::decay_t; - assert(Value && any_isa(*Value) && "Bad any cast!"); - if (!Value || !any_isa(*Value)) + if (!Value || !Value->isa()) return nullptr; return &static_cast &>(*Value->Storage).Value; } diff --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp --- a/llvm/lib/CodeGen/MachinePassManager.cpp +++ b/llvm/lib/CodeGen/MachinePassManager.cpp @@ -41,7 +41,7 @@ // current pipeline is the top-level pipeline. Callbacks are not used after // current pipeline. PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) { - assert(any_isa(IR)); + assert(any_cast(&IR)); const MachineFunction *MF = any_cast(IR); assert(MF && "Machine function should be valid for printing"); std::string Banner = std::string("After ") + std::string(PassID); diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -17,7 +17,6 @@ #include "ConstantsContext.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" -#include "llvm/ADT/Any.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp --- a/llvm/lib/Passes/StandardInstrumentations.cpp +++ b/llvm/lib/Passes/StandardInstrumentations.cpp @@ -123,20 +123,18 @@ /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match /// certain global filters. Will never return nullptr if \p Force is true. const Module *unwrapModule(Any IR, bool Force = false) { - if (any_isa(IR)) - return any_cast(IR); + if (const auto **M = any_cast(&IR)) + return *M; - if (any_isa(IR)) { - const Function *F = any_cast(IR); - if (!Force && !isFunctionInPrintList(F->getName())) + if (const auto **F = any_cast(&IR)) { + if (!Force && !isFunctionInPrintList((*F)->getName())) return nullptr; - return F->getParent(); + return (*F)->getParent(); } - if (any_isa(IR)) { - const LazyCallGraph::SCC *C = any_cast(IR); - for (const LazyCallGraph::Node &N : *C) { + if (const auto **C = any_cast(&IR)) { + for (const LazyCallGraph::Node &N : **C) { const Function &F = N.getFunction(); if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) { return F.getParent(); @@ -146,9 +144,8 @@ return nullptr; } - if (any_isa(IR)) { - const Loop *L = any_cast(IR); - const Function *F = L->getHeader()->getParent(); + if (const auto **L = any_cast(&IR)) { + const Function *F = (*L)->getHeader()->getParent(); if (!Force && !isFunctionInPrintList(F->getName())) return nullptr; return F->getParent(); @@ -190,23 +187,17 @@ } std::string getIRName(Any IR) { - if (any_isa(IR)) + if (any_cast(&IR)) return "[module]"; - if (any_isa(IR)) { - const Function *F = any_cast(IR); - return F->getName().str(); - } + if (const auto **F = any_cast(&IR)) + return (*F)->getName().str(); - if (any_isa(IR)) { - const LazyCallGraph::SCC *C = any_cast(IR); - return C->getName(); - } + if (const auto **C = any_cast(&IR)) + return (*C)->getName(); - if (any_isa(IR)) { - const Loop *L = any_cast(IR); - return L->getName().str(); - } + if (const auto **L = any_cast(&IR)) + return (*L)->getName().str(); llvm_unreachable("Unknown wrapped IR type"); } @@ -228,30 +219,22 @@ } bool shouldPrintIR(Any IR) { - if (any_isa(IR)) { - const Module *M = any_cast(IR); - return moduleContainsFilterPrintFunc(*M); - } + if (const auto **M = any_cast(&IR)) + return moduleContainsFilterPrintFunc(**M); - if (any_isa(IR)) { - const Function *F = any_cast(IR); - return isFunctionInPrintList(F->getName()); - } + if (const auto **F = any_cast(&IR)) + return isFunctionInPrintList((*F)->getName()); - if (any_isa(IR)) { - const LazyCallGraph::SCC *C = any_cast(IR); - return sccContainsFilterPrintFunc(*C); - } + if (const auto **C = any_cast(&IR)) + return sccContainsFilterPrintFunc(**C); - if (any_isa(IR)) { - const Loop *L = any_cast(IR); - return isFunctionInPrintList(L->getHeader()->getParent()->getName()); - } + if (const auto **L = any_cast(&IR)) + return isFunctionInPrintList((*L)->getHeader()->getParent()->getName()); llvm_unreachable("Unknown wrapped IR type"); } /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into -/// llvm::Any and does actual print job. +/// Any and does actual print job. void unwrapAndPrint(raw_ostream &OS, Any IR) { if (!shouldPrintIR(IR)) return; @@ -263,27 +246,23 @@ return; } - if (any_isa(IR)) { - const Module *M = any_cast(IR); - printIR(OS, M); + if (const auto **M = any_cast(&IR)) { + printIR(OS, *M); return; } - if (any_isa(IR)) { - const Function *F = any_cast(IR); - printIR(OS, F); + if (const auto **F = any_cast(&IR)) { + printIR(OS, *F); return; } - if (any_isa(IR)) { - const LazyCallGraph::SCC *C = any_cast(IR); - printIR(OS, C); + if (const auto **C = any_cast(&IR)) { + printIR(OS, *C); return; } - if (any_isa(IR)) { - const Loop *L = any_cast(IR); - printIR(OS, L); + if (const auto **L = any_cast(&IR)) { + printIR(OS, *L); return; } llvm_unreachable("Unknown wrapped IR type"); @@ -313,10 +292,10 @@ // Return the module when that is the appropriate level of comparison for \p IR. const Module *getModuleForComparison(Any IR) { - if (any_isa(IR)) - return any_cast(IR); - if (any_isa(IR)) - return any_cast(IR) + if (const auto **M = any_cast(&IR)) + return *M; + if (const auto **C = any_cast(&IR)) + return (*C) ->begin() ->getFunction() .getParent(); @@ -332,8 +311,8 @@ bool isInteresting(Any IR, StringRef PassID, StringRef PassName) { if (isIgnored(PassID) || !isPassInPrintList(PassName)) return false; - if (any_isa(IR)) - return isInterestingFunction(*any_cast(IR)); + if (const auto **F = any_cast(&IR)) + return isInterestingFunction(**F); return true; } @@ -655,13 +634,12 @@ return; } - const Function *F = nullptr; - if (any_isa(IR)) - F = any_cast(IR); - else { - assert(any_isa(IR) && "Unknown IR unit."); - const Loop *L = any_cast(IR); - F = L->getHeader()->getParent(); + const Function **FPtr = any_cast(&IR); + const Function *F = FPtr ? *FPtr : nullptr; + if (!F) { + const Loop **L = any_cast(&IR); + assert(L && "Unknown IR unit."); + F = (*L)->getHeader()->getParent(); } assert(F && "Unknown IR unit."); generateFunctionData(Data, *F); @@ -816,11 +794,11 @@ } bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) { - const Function *F = nullptr; - if (any_isa(IR)) { - F = any_cast(IR); - } else if (any_isa(IR)) { - F = any_cast(IR)->getHeader()->getParent(); + const Function **FPtr = any_cast(&IR); + const Function *F = FPtr ? *FPtr : nullptr; + if (!F) { + if (const auto **L = any_cast(&IR)) + F = (*L)->getHeader()->getParent(); } bool ShouldRun = !(F && F->hasOptNone()); if (!ShouldRun && DebugLogging) { @@ -895,14 +873,14 @@ auto &OS = print(); OS << "Running pass: " << PassID << " on " << getIRName(IR); - if (any_isa(IR)) { - unsigned Count = any_cast(IR)->getInstructionCount(); + if (const auto **F = any_cast(&IR)) { + unsigned Count = (*F)->getInstructionCount(); OS << " (" << Count << " instruction"; if (Count != 1) OS << 's'; OS << ')'; - } else if (any_isa(IR)) { - int Count = any_cast(IR)->size(); + } else if (const auto **C = any_cast(&IR)) { + int Count = (*C)->size(); OS << " (" << Count << " node"; if (Count != 1) OS << 's'; @@ -1100,18 +1078,19 @@ report_fatal_error(Twine("CFG unexpectedly changed by ", Pass)); }; - PIC.registerBeforeNonSkippedPassCallback([this, &FAM](StringRef P, Any IR) { + PIC.registerBeforeNonSkippedPassCallback( + [this, &FAM](StringRef P, Any IR) { #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS - assert(&PassStack.emplace_back(P)); + assert(&PassStack.emplace_back(P)); #endif - (void)this; - if (!any_isa(IR)) - return; + (void)this; + const auto **F = any_cast(&IR); + if (!F) + return; - const auto *F = any_cast(IR); - // Make sure a fresh CFG snapshot is available before the pass. - FAM.getResult(*const_cast(F)); - }); + // Make sure a fresh CFG snapshot is available before the pass. + FAM.getResult(*const_cast(*F)); + }); PIC.registerAfterPassInvalidatedCallback( [this](StringRef P, const PreservedAnalyses &PassPA) { @@ -1131,18 +1110,18 @@ #endif (void)this; - if (!any_isa(IR)) + const auto **F = any_cast(&IR); + if (!F) return; if (!PassPA.allAnalysesInSetPreserved() && !PassPA.allAnalysesInSetPreserved>()) return; - const auto *F = any_cast(IR); if (auto *GraphBefore = FAM.getCachedResult( - *const_cast(F))) - checkCFG(P, F->getName(), *GraphBefore, - CFG(F, /* TrackBBLifetime */ false)); + *const_cast(*F))) + checkCFG(P, (*F)->getName(), *GraphBefore, + CFG(*F, /* TrackBBLifetime */ false)); }); } @@ -1152,46 +1131,50 @@ [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) { if (isIgnored(P) || P == "VerifierPass") return; - if (any_isa(IR) || any_isa(IR)) { - const Function *F; - if (any_isa(IR)) - F = any_cast(IR)->getHeader()->getParent(); - else - F = any_cast(IR); + const Function **FPtr = any_cast(&IR); + const Function *F = FPtr ? *FPtr : nullptr; + if (!F) { + if (const auto **L = any_cast(&IR)) + F = (*L)->getHeader()->getParent(); + } + + if (F) { if (DebugLogging) dbgs() << "Verifying function " << F->getName() << "\n"; if (verifyFunction(*F, &errs())) report_fatal_error("Broken function found, compilation aborted!"); - } else if (any_isa(IR) || - any_isa(IR)) { - const Module *M; - if (any_isa(IR)) - M = any_cast(IR) - ->begin() - ->getFunction() - .getParent(); - else - M = any_cast(IR); - if (DebugLogging) - dbgs() << "Verifying module " << M->getName() << "\n"; - - if (verifyModule(*M, &errs())) - report_fatal_error("Broken module found, compilation aborted!"); + } else { + const Module **MPtr = any_cast(&IR); + const Module *M = MPtr ? *MPtr : nullptr; + if (!M) { + if (const auto **C = any_cast(&IR)) + M = (*C)->begin()->getFunction().getParent(); + } + + if (M) { + if (DebugLogging) + dbgs() << "Verifying module " << M->getName() << "\n"; + + if (verifyModule(*M, &errs())) + report_fatal_error("Broken module found, compilation aborted!"); + } } }); } InLineChangePrinter::~InLineChangePrinter() = default; -void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID, +void InLineChangePrinter::generateIRRepresentation(Any IR, + StringRef PassID, IRDataT &D) { IRComparer::analyzeIR(IR, D); } void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name, const IRDataT &Before, - const IRDataT &After, Any IR) { + const IRDataT &After, + Any IR) { SmallString<20> Banner = formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name); Out << Banner; diff --git a/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp b/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp --- a/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp @@ -98,14 +98,14 @@ std::string Banner = "\n*** Pseudo Probe Verification After " + PassID.str() + " ***\n"; dbgs() << Banner; - if (any_isa(IR)) - runAfterPass(any_cast(IR)); - else if (any_isa(IR)) - runAfterPass(any_cast(IR)); - else if (any_isa(IR)) - runAfterPass(any_cast(IR)); - else if (any_isa(IR)) - runAfterPass(any_cast(IR)); + if (const auto **M = any_cast(&IR)) + runAfterPass(*M); + else if (const auto **F = any_cast(&IR)) + runAfterPass(*F); + else if (const auto **C = any_cast(&IR)) + runAfterPass(*C); + else if (const auto **L = any_cast(&IR)) + runAfterPass(*L); else llvm_unreachable("Unknown IR unit"); } diff --git a/llvm/lib/Transforms/Scalar/LoopPassManager.cpp b/llvm/lib/Transforms/Scalar/LoopPassManager.cpp --- a/llvm/lib/Transforms/Scalar/LoopPassManager.cpp +++ b/llvm/lib/Transforms/Scalar/LoopPassManager.cpp @@ -269,10 +269,11 @@ PI.pushBeforeNonSkippedPassCallback([&LAR, &LI](StringRef PassID, Any IR) { if (isSpecialPass(PassID, {"PassManager"})) return; - assert(any_isa(IR) || any_isa(IR)); - const Loop *L = any_isa(IR) - ? any_cast(IR) - : &any_cast(IR)->getOutermostLoop(); + assert(any_cast(&IR) || any_cast(&IR)); + const Loop **LPtr = any_cast(&IR); + const Loop *L = LPtr ? *LPtr : nullptr; + if (!L) + L = &any_cast(IR)->getOutermostLoop(); assert(L && "Loop should be valid for printing"); // Verify the loop structure and LCSSA form before visiting the loop. diff --git a/llvm/lib/Transforms/Utils/Debugify.cpp b/llvm/lib/Transforms/Utils/Debugify.cpp --- a/llvm/lib/Transforms/Utils/Debugify.cpp +++ b/llvm/lib/Transforms/Utils/Debugify.cpp @@ -1031,19 +1031,19 @@ PIC.registerBeforeNonSkippedPassCallback([this](StringRef P, Any IR) { if (isIgnoredPass(P)) return; - if (any_isa(IR)) - applyDebugify(*const_cast(any_cast(IR)), + if (const auto **F = any_cast(&IR)) + applyDebugify(*const_cast(*F), Mode, DebugInfoBeforePass, P); - else if (any_isa(IR)) - applyDebugify(*const_cast(any_cast(IR)), + else if (const auto **M = any_cast(&IR)) + applyDebugify(*const_cast(*M), Mode, DebugInfoBeforePass, P); }); PIC.registerAfterPassCallback([this](StringRef P, Any IR, const PreservedAnalyses &PassPA) { if (isIgnoredPass(P)) return; - if (any_isa(IR)) { - auto &F = *const_cast(any_cast(IR)); + if (const auto **CF = any_cast(&IR)) { + auto &F = *const_cast(*CF); Module &M = *F.getParent(); auto It = F.getIterator(); if (Mode == DebugifyMode::SyntheticDebugInfo) @@ -1054,8 +1054,8 @@ M, make_range(It, std::next(It)), *DebugInfoBeforePass, "CheckModuleDebugify (original debuginfo)", P, OrigDIVerifyBugsReportFilePath); - } else if (any_isa(IR)) { - auto &M = *const_cast(any_cast(IR)); + } else if (const auto **CM = any_cast(&IR)) { + auto &M = *const_cast(*CM); if (Mode == DebugifyMode::SyntheticDebugInfo) checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify", /*Strip=*/true, DIStatsMap); diff --git a/llvm/unittests/ADT/AnyTest.cpp b/llvm/unittests/ADT/AnyTest.cpp --- a/llvm/unittests/ADT/AnyTest.cpp +++ b/llvm/unittests/ADT/AnyTest.cpp @@ -24,55 +24,55 @@ // An empty Any is not anything. EXPECT_FALSE(A.has_value()); - EXPECT_FALSE(any_isa(A)); + EXPECT_FALSE(any_cast(&A)); // An int is an int but not something else. EXPECT_TRUE(B.has_value()); - EXPECT_TRUE(any_isa(B)); - EXPECT_FALSE(any_isa(B)); + EXPECT_TRUE(any_cast(&B)); + EXPECT_FALSE(any_cast(&B)); EXPECT_TRUE(C.has_value()); - EXPECT_TRUE(any_isa(C)); + EXPECT_TRUE(any_cast(&C)); // A const char * is a const char * but not an int. EXPECT_TRUE(D.has_value()); - EXPECT_TRUE(any_isa(D)); - EXPECT_FALSE(any_isa(D)); + EXPECT_TRUE(any_cast(&D)); + EXPECT_FALSE(any_cast(&D)); // A double is a double but not a float. EXPECT_TRUE(E.has_value()); - EXPECT_TRUE(any_isa(E)); - EXPECT_FALSE(any_isa(E)); + EXPECT_TRUE(any_cast(&E)); + EXPECT_FALSE(any_cast(&E)); // After copy constructing from an int, the new item and old item are both // ints. llvm::Any F(B); EXPECT_TRUE(B.has_value()); EXPECT_TRUE(F.has_value()); - EXPECT_TRUE(any_isa(F)); - EXPECT_TRUE(any_isa(B)); + EXPECT_TRUE(any_cast(&F)); + EXPECT_TRUE(any_cast(&B)); // After move constructing from an int, the new item is an int and the old one // isn't. llvm::Any G(std::move(C)); EXPECT_FALSE(C.has_value()); EXPECT_TRUE(G.has_value()); - EXPECT_TRUE(any_isa(G)); - EXPECT_FALSE(any_isa(C)); + EXPECT_TRUE(any_cast(&G)); + EXPECT_FALSE(any_cast(&C)); // After copy-assigning from an int, the new item and old item are both ints. A = F; EXPECT_TRUE(A.has_value()); EXPECT_TRUE(F.has_value()); - EXPECT_TRUE(any_isa(A)); - EXPECT_TRUE(any_isa(F)); + EXPECT_TRUE(any_cast(&A)); + EXPECT_TRUE(any_cast(&F)); // After move-assigning from an int, the new item and old item are both ints. B = std::move(G); EXPECT_TRUE(B.has_value()); EXPECT_FALSE(G.has_value()); - EXPECT_TRUE(any_isa(B)); - EXPECT_FALSE(any_isa(G)); + EXPECT_TRUE(any_cast(&B)); + EXPECT_FALSE(any_cast(&G)); } TEST(AnyTest, GoodAnyCast) { diff --git a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp --- a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp +++ b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp @@ -290,17 +290,17 @@ return std::string(name); } -template <> std::string getName(const llvm::Any &WrappedIR) { - if (any_isa(WrappedIR)) - return any_cast(WrappedIR)->getName().str(); - if (any_isa(WrappedIR)) - return any_cast(WrappedIR)->getName().str(); - if (any_isa(WrappedIR)) - return any_cast(WrappedIR)->getName().str(); - if (any_isa(WrappedIR)) - return any_cast(WrappedIR)->getName().str(); - if (any_isa(WrappedIR)) - return any_cast(WrappedIR)->getName(); +template <> std::string getName(const Any &WrappedIR) { + if (const auto *const *M = any_cast(&WrappedIR)) + return (*M)->getName().str(); + if (const auto *const *F = any_cast(&WrappedIR)) + return (*F)->getName().str(); + if (const auto *const *L = any_cast(&WrappedIR)) + return (*L)->getName().str(); + if (const auto *const *L = any_cast(&WrappedIR)) + return (*L)->getName().str(); + if (const auto *const *C = any_cast(&WrappedIR)) + return (*C)->getName(); return ""; } /// Define a custom matcher for objects which support a 'getName' method.