[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 16:58:12 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) {
----------------
bcardosolopes wrote:

It meant entered at its start, without a goto jumping in. mayCompleteFromTop is gone in 5d059fd, what replaced it is documented as `mayCompleteWhenEntered`.

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


More information about the cfe-commits mailing list