/// 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<const Module *>(IR))
- return any_cast<const Module *>(IR);
+ if (const auto **M = any_cast<const Module *>(&IR))
+ return *M;
- if (any_isa<const Function *>(IR)) {
- const Function *F = any_cast<const Function *>(IR);
- if (!Force && !isFunctionInPrintList(F->getName()))
+ if (const auto **F = any_cast<const Function *>(&IR)) {
+ if (!Force && !isFunctionInPrintList((*F)->getName()))
return nullptr;
- return F->getParent();
+ return (*F)->getParent();
}
- if (any_isa<const LazyCallGraph::SCC *>(IR)) {
- const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
- for (const LazyCallGraph::Node &N : *C) {
+ if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
+ for (const LazyCallGraph::Node &N : **C) {
const Function &F = N.getFunction();
if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
return F.getParent();
return nullptr;
}
- if (any_isa<const Loop *>(IR)) {
- const Loop *L = any_cast<const Loop *>(IR);
- const Function *F = L->getHeader()->getParent();
+ if (const auto **L = any_cast<const Loop *>(&IR)) {
+ const Function *F = (*L)->getHeader()->getParent();
if (!Force && !isFunctionInPrintList(F->getName()))
return nullptr;
return F->getParent();
}
std::string getIRName(Any IR) {
- if (any_isa<const Module *>(IR))
+ if (any_cast<const Module *>(&IR))
return "[module]";
- if (any_isa<const Function *>(IR)) {
- const Function *F = any_cast<const Function *>(IR);
- return F->getName().str();
- }
+ if (const auto **F = any_cast<const Function *>(&IR))
+ return (*F)->getName().str();
- if (any_isa<const LazyCallGraph::SCC *>(IR)) {
- const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
- return C->getName();
- }
+ if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+ return (*C)->getName();
- if (any_isa<const Loop *>(IR)) {
- const Loop *L = any_cast<const Loop *>(IR);
- return L->getName().str();
- }
+ if (const auto **L = any_cast<const Loop *>(&IR))
+ return (*L)->getName().str();
llvm_unreachable("Unknown wrapped IR type");
}
}
bool shouldPrintIR(Any IR) {
- if (any_isa<const Module *>(IR)) {
- const Module *M = any_cast<const Module *>(IR);
- return moduleContainsFilterPrintFunc(*M);
- }
+ if (const auto **M = any_cast<const Module *>(&IR))
+ return moduleContainsFilterPrintFunc(**M);
- if (any_isa<const Function *>(IR)) {
- const Function *F = any_cast<const Function *>(IR);
- return isFunctionInPrintList(F->getName());
- }
+ if (const auto **F = any_cast<const Function *>(&IR))
+ return isFunctionInPrintList((*F)->getName());
- if (any_isa<const LazyCallGraph::SCC *>(IR)) {
- const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
- return sccContainsFilterPrintFunc(*C);
- }
+ if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+ return sccContainsFilterPrintFunc(**C);
- if (any_isa<const Loop *>(IR)) {
- const Loop *L = any_cast<const Loop *>(IR);
- return isFunctionInPrintList(L->getHeader()->getParent()->getName());
- }
+ if (const auto **L = any_cast<const Loop *>(&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;
return;
}
- if (any_isa<const Module *>(IR)) {
- const Module *M = any_cast<const Module *>(IR);
- printIR(OS, M);
+ if (const auto **M = any_cast<const Module *>(&IR)) {
+ printIR(OS, *M);
return;
}
- if (any_isa<const Function *>(IR)) {
- const Function *F = any_cast<const Function *>(IR);
- printIR(OS, F);
+ if (const auto **F = any_cast<const Function *>(&IR)) {
+ printIR(OS, *F);
return;
}
- if (any_isa<const LazyCallGraph::SCC *>(IR)) {
- const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
- printIR(OS, C);
+ if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
+ printIR(OS, *C);
return;
}
- if (any_isa<const Loop *>(IR)) {
- const Loop *L = any_cast<const Loop *>(IR);
- printIR(OS, L);
+ if (const auto **L = any_cast<const Loop *>(&IR)) {
+ printIR(OS, *L);
return;
}
llvm_unreachable("Unknown wrapped IR type");
// Return the module when that is the appropriate level of comparison for \p IR.
const Module *getModuleForComparison(Any IR) {
- if (any_isa<const Module *>(IR))
- return any_cast<const Module *>(IR);
- if (any_isa<const LazyCallGraph::SCC *>(IR))
- return any_cast<const LazyCallGraph::SCC *>(IR)
+ if (const auto **M = any_cast<const Module *>(&IR))
+ return *M;
+ if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+ return (*C)
->begin()
->getFunction()
.getParent();
bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
if (isIgnored(PassID) || !isPassInPrintList(PassName))
return false;
- if (any_isa<const Function *>(IR))
- return isInterestingFunction(*any_cast<const Function *>(IR));
+ if (const auto **F = any_cast<const Function *>(&IR))
+ return isInterestingFunction(**F);
return true;
}
return;
}
- const Function *F = nullptr;
- if (any_isa<const Function *>(IR))
- F = any_cast<const Function *>(IR);
- else {
- assert(any_isa<const Loop *>(IR) && "Unknown IR unit.");
- const Loop *L = any_cast<const Loop *>(IR);
- F = L->getHeader()->getParent();
+ const Function **FPtr = any_cast<const Function *>(&IR);
+ const Function *F = FPtr ? *FPtr : nullptr;
+ if (!F) {
+ const Loop **L = any_cast<const Loop *>(&IR);
+ assert(L && "Unknown IR unit.");
+ F = (*L)->getHeader()->getParent();
}
assert(F && "Unknown IR unit.");
generateFunctionData(Data, *F);
}
bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
- const Function *F = nullptr;
- if (any_isa<const Function *>(IR)) {
- F = any_cast<const Function *>(IR);
- } else if (any_isa<const Loop *>(IR)) {
- F = any_cast<const Loop *>(IR)->getHeader()->getParent();
+ const Function **FPtr = any_cast<const Function *>(&IR);
+ const Function *F = FPtr ? *FPtr : nullptr;
+ if (!F) {
+ if (const auto **L = any_cast<const Loop *>(&IR))
+ F = (*L)->getHeader()->getParent();
}
bool ShouldRun = !(F && F->hasOptNone());
if (!ShouldRun && DebugLogging) {
auto &OS = print();
OS << "Running pass: " << PassID << " on " << getIRName(IR);
- if (any_isa<const Function *>(IR)) {
- unsigned Count = any_cast<const Function *>(IR)->getInstructionCount();
+ if (const auto **F = any_cast<const Function *>(&IR)) {
+ unsigned Count = (*F)->getInstructionCount();
OS << " (" << Count << " instruction";
if (Count != 1)
OS << 's';
OS << ')';
- } else if (any_isa<const LazyCallGraph::SCC *>(IR)) {
- int Count = any_cast<const LazyCallGraph::SCC *>(IR)->size();
+ } else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
+ int Count = (*C)->size();
OS << " (" << Count << " node";
if (Count != 1)
OS << 's';
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<const Function *>(IR))
- return;
+ (void)this;
+ const auto **F = any_cast<const Function *>(&IR);
+ if (!F)
+ return;
- const auto *F = any_cast<const Function *>(IR);
- // Make sure a fresh CFG snapshot is available before the pass.
- FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(F));
- });
+ // Make sure a fresh CFG snapshot is available before the pass.
+ FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(*F));
+ });
PIC.registerAfterPassInvalidatedCallback(
[this](StringRef P, const PreservedAnalyses &PassPA) {
#endif
(void)this;
- if (!any_isa<const Function *>(IR))
+ const auto **F = any_cast<const Function *>(&IR);
+ if (!F)
return;
if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
!PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
return;
- const auto *F = any_cast<const Function *>(IR);
if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>(
- *const_cast<Function *>(F)))
- checkCFG(P, F->getName(), *GraphBefore,
- CFG(F, /* TrackBBLifetime */ false));
+ *const_cast<Function *>(*F)))
+ checkCFG(P, (*F)->getName(), *GraphBefore,
+ CFG(*F, /* TrackBBLifetime */ false));
});
}
[this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
if (isIgnored(P) || P == "VerifierPass")
return;
- if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) {
- const Function *F;
- if (any_isa<const Loop *>(IR))
- F = any_cast<const Loop *>(IR)->getHeader()->getParent();
- else
- F = any_cast<const Function *>(IR);
+ const Function **FPtr = any_cast<const Function *>(&IR);
+ const Function *F = FPtr ? *FPtr : nullptr;
+ if (!F) {
+ if (const auto **L = any_cast<const Loop *>(&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<const Module *>(IR) ||
- any_isa<const LazyCallGraph::SCC *>(IR)) {
- const Module *M;
- if (any_isa<const LazyCallGraph::SCC *>(IR))
- M = any_cast<const LazyCallGraph::SCC *>(IR)
- ->begin()
- ->getFunction()
- .getParent();
- else
- M = any_cast<const Module *>(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<const Module *>(&IR);
+ const Module *M = MPtr ? *MPtr : nullptr;
+ if (!M) {
+ if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&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<EmptyData> &D) {
IRComparer<EmptyData>::analyzeIR(IR, D);
}
void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name,
const IRDataT<EmptyData> &Before,
- const IRDataT<EmptyData> &After, Any IR) {
+ const IRDataT<EmptyData> &After,
+ Any IR) {
SmallString<20> Banner =
formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
Out << Banner;
// An empty Any is not anything.
EXPECT_FALSE(A.has_value());
- EXPECT_FALSE(any_isa<int>(A));
+ EXPECT_FALSE(any_cast<int>(&A));
// An int is an int but not something else.
EXPECT_TRUE(B.has_value());
- EXPECT_TRUE(any_isa<int>(B));
- EXPECT_FALSE(any_isa<float>(B));
+ EXPECT_TRUE(any_cast<int>(&B));
+ EXPECT_FALSE(any_cast<float>(&B));
EXPECT_TRUE(C.has_value());
- EXPECT_TRUE(any_isa<int>(C));
+ EXPECT_TRUE(any_cast<int>(&C));
// A const char * is a const char * but not an int.
EXPECT_TRUE(D.has_value());
- EXPECT_TRUE(any_isa<const char *>(D));
- EXPECT_FALSE(any_isa<int>(D));
+ EXPECT_TRUE(any_cast<const char *>(&D));
+ EXPECT_FALSE(any_cast<int>(&D));
// A double is a double but not a float.
EXPECT_TRUE(E.has_value());
- EXPECT_TRUE(any_isa<double>(E));
- EXPECT_FALSE(any_isa<float>(E));
+ EXPECT_TRUE(any_cast<double>(&E));
+ EXPECT_FALSE(any_cast<float>(&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<int>(F));
- EXPECT_TRUE(any_isa<int>(B));
+ EXPECT_TRUE(any_cast<int>(&F));
+ EXPECT_TRUE(any_cast<int>(&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<int>(G));
- EXPECT_FALSE(any_isa<int>(C));
+ EXPECT_TRUE(any_cast<int>(&G));
+ EXPECT_FALSE(any_cast<int>(&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<int>(A));
- EXPECT_TRUE(any_isa<int>(F));
+ EXPECT_TRUE(any_cast<int>(&A));
+ EXPECT_TRUE(any_cast<int>(&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<int>(B));
- EXPECT_FALSE(any_isa<int>(G));
+ EXPECT_TRUE(any_cast<int>(&B));
+ EXPECT_FALSE(any_cast<int>(&G));
}
TEST(AnyTest, GoodAnyCast) {