================ @@ -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 ®ion, 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; ---------------- bcardosolopes wrote:
Done! https://github.com/llvm/llvm-project/pull/239774 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
