[llvm] [LowerSwitch] Implement verifyAnalysis (PR #68294)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 02:57:47 PDT 2023


https://github.com/jayfoad created https://github.com/llvm/llvm-project/pull/68294

This pass is used like an analysis so it's useful to be able to verify
passes that claim to have preserved it.


>From 20810ec7c17bb847dec33163015a3809fcaa85a4 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 5 Oct 2023 10:10:41 +0100
Subject: [PATCH] [LowerSwitch] Implement verifyAnalysis

This pass is used like an analysis so it's useful to be able to verify
passes that claim to have preserved it.
---
 llvm/lib/Transforms/Utils/LowerSwitch.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/llvm/lib/Transforms/Utils/LowerSwitch.cpp b/llvm/lib/Transforms/Utils/LowerSwitch.cpp
index 227de425ff85549..2b046017d06abda 100644
--- a/llvm/lib/Transforms/Utils/LowerSwitch.cpp
+++ b/llvm/lib/Transforms/Utils/LowerSwitch.cpp
@@ -569,8 +569,17 @@ class LowerSwitchLegacyPass : public FunctionPass {
     initializeLowerSwitchLegacyPassPass(*PassRegistry::getPassRegistry());
   }
 
+  // Remember the current function. Only used for verification.
+  Function *F;
+
   bool runOnFunction(Function &F) override;
 
+  void verifyAnalysis() const override {
+    for (auto &BB : *F)
+      assert(!isa<SwitchInst>(BB.getTerminator()) &&
+             "Found an unlowered switch!");
+  }
+
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.addRequired<LazyValueInfoWrapperPass>();
   }
@@ -596,6 +605,7 @@ FunctionPass *llvm::createLowerSwitchPass() {
 }
 
 bool LowerSwitchLegacyPass::runOnFunction(Function &F) {
+  this->F = &F;
   LazyValueInfo *LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
   auto *ACT = getAnalysisIfAvailable<AssumptionCacheTracker>();
   AssumptionCache *AC = ACT ? &ACT->getAssumptionCache(F) : nullptr;



More information about the llvm-commits mailing list