[clang] [clang][dataflow] Refactor processing of terminator element (PR #84499)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 12 04:13:39 PDT 2024


================
@@ -337,26 +274,33 @@ computeBlockInputState(const CFGBlock &Block, AnalysisContext &AC) {
         AC.BlockStates[Pred->getBlockID()];
     if (!MaybePredState)
       continue;
-
-    if (AC.Analysis.builtinOptions()) {
-      if (const Stmt *PredTerminatorStmt = Pred->getTerminatorStmt()) {
-        // We have a terminator: we need to mutate an environment to describe
-        // when the terminator is taken. Copy now.
+    const TypeErasedDataflowAnalysisState &PredState = *MaybePredState;
+
+    if (const Stmt *PredTerminatorStmt = Pred->getTerminatorStmt()) {
----------------
martinboehme wrote:

To make this clearer, here's how the whole code block could be changed to make it (IMO) simpler (I can't do this as a suggested edit because I would have to include deleted lines in the block of code, and github doesn't allow me to do this):

```cxx
    const Expr *TerminatorCond = getTerminatorCondition(Pred->getTerminatorStmt());
    if (TerminatorCond == nullptr) {
      Builder.addUnowned(PredState);
      continue;
    }

    bool BranchVal = blockIndexInPredecessor(*Pred, Block) == 0;

    // `transferBranch` may need to mutate the environment to describe the
    // dynamic effect of the terminator for a given branch.  Copy now.
    TypeErasedDataflowAnalysisState Copy = PredState.fork();
    if (AC.Analysis.builtinOptions()) {
      auto *CondVal = Copy.Env.get<BoolValue>(*TerminatorCond);
      // In transferCFGBlock(), we ensure that we always have a `Value`
      // for the terminator condition, so assert this. We consciously
      // assert ourselves instead of asserting via `cast()` so that we get
      // a more meaningful line number if the assertion fails.
      assert(CondVal != nullptr);
      BoolValue *AssertedVal =
          BranchVal ? CondVal : &Copy.Env.makeNot(*CondVal);
      Copy.Env.assume(AssertedVal->formula());
    }
    AC.Analysis.transferBranchTypeErased(BranchVal, TerminatorCond, Copy.Lattice,
                                         Copy.Env);
    Builder.addOwned(std::move(Copy));
```

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


More information about the cfe-commits mailing list