[clang] [CIR] -Wreturn-type (1/2): Add an analysis classifying how control reaches a function's end (PR #239774)

Bruno Cardoso Lopes via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 17:49:37 PST 2026


================
@@ -0,0 +1,713 @@
+//===- CIRFallOffAnalysis.cpp - Can control flow off the end of a function ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// A reachability walk over structured CIR. Each region reports how control
+// leaves it (falling through, break, continue, explicit return, or reaching an
+// `implicit` terminator), and each structured operation combines the exits of
+// its regions according to its semantics. Reachable noreturn calls, user
+// traps and throws are recorded as the abnormal and fake edges Sema's CFG
+// check uses to tell "maybe" from "always".
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CIR/Dialect/Analysis/CIRFallOffAnalysis.h"
+
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/Matchers.h"
+#include "mlir/IR/Threading.h"
+#include "mlir/Interfaces/SideEffectInterfaces.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/Support/TimeProfiler.h"
+
+using namespace cir;
+
+namespace {
+
+/// How control can leave a region or a structured operation.
+struct Exits {
+  bool normal = false;
+  bool breaks = false;
+  bool continues = false;
+  bool explicitReturn = false;
+  bool implicitReturn = false;
+  // Outcomes of a reachable `cir.condition` terminating the region.
+  bool condTrue = false;
+  bool condFalse = false;
+
+  /// Merge the exits that escape an enclosed operation.
+  void mergeEscaping(const Exits &other) {
+    breaks |= other.breaks;
+    continues |= other.continues;
+    explicitReturn |= other.explicitReturn;
+    implicitReturn |= other.implicitReturn;
+  }
+
+  bool any() const {
+    return normal || breaks || continues || explicitReturn || implicitReturn;
+  }
+};
+
+class FallOffAnalyzer {
+public:
+  explicit FallOffAnalyzer(FuncOp func, bool tailOnly = false)
+      : func(func), tailOnly(tailOnly) {}
+
+  FallOffKind run();
+
+  /// Conservatively, whether `op` alone completes when entered at the top. A
+  /// label inside it, or dead code that may hold one, means yes.
+  bool mayCompleteFromTop(mlir::Operation *op) {
+    assert(tailOnly && "needs a tail-only analyzer");
+    return visitStructured(op, /*entered=*/true).normal || unsure;
+  }
+
+private:
+  Exits visitRegion(mlir::Region &region, bool entered);
+  Exits visitStructured(mlir::Operation *op, bool entered);
+  Exits visitLoop(LoopOpInterface loop, bool entered);
+  Exits visitSwitch(SwitchOp op, bool entered);
+  Exits visitCleanupScope(CleanupScopeOp op, bool entered);
+  void visitTerminator(mlir::Operation *op, Exits &exits,
+                       llvm::SmallVectorImpl<mlir::Block *> &liveSuccessors);
+  void addAddressTakenLabels();
+  void updateLabelAncestors();
+
+  /// Whether a dead operation still needs a visit because a label inside it
+  /// can be reached through a goto.
+  bool containsTargetedLabel(mlir::Operation *op) const {
+    return labelAncestors.contains(op);
+  }
+  bool isTargetedLabel(mlir::Operation *op) const {
+    auto label = mlir::dyn_cast<LabelOp>(op);
+    return label && targetedLabels.contains(label.getLabelAttr());
+  }
+
+  FuncOp func;
+  bool tailOnly;
+  bool unsure = false;
+  bool abnormalEdge = false;
+  bool fakeEdge = false;
+  /// Labels control can reach through a goto or an indirect branch, and the
+  /// operations enclosing them.
+  llvm::DenseSet<mlir::StringAttr> targetedLabels;
+  llvm::DenseSet<mlir::Operation *> labelAncestors;
+  /// Labels targeted by the gotos reached so far.
+  llvm::DenseSet<mlir::StringAttr> reachedGotoTargets;
+  /// Whether a cir.indirect_goto is reachable; it can reach every label whose
+  /// address is taken.
+  bool reachedIndirectGoto = false;
+  bool addedAddressTakenLabels = false;
+  /// The function's labels, collected once a goto makes them matter.
+  std::optional<llvm::SmallVector<LabelOp>> labels;
----------------
bcardosolopes wrote:

Nah, left over from prototyping, we might have needed a 3-state here, but we don't, fixed in f767825

https://github.com/llvm/llvm-project/pull/239774


More information about the cfe-commits mailing list