Repository: llvm/llvm-project
File: mlir/lib/Dialect/ControlFlow/IR/ControlFlowOps.cpp (C++)

Code before:
///   use(%c)                   // %arg0 has no uses and can be removed
///
/// If all incoming values for a block argument from all predecessors are the
/// same SSA value, replace uses of the block argument with that value. This
/// allows the block argument to be removed by dead code elimination.
static bool simplifyUniformBlockArgs(Block *dest, PatternRewriter &rewriter) {
  if (dest->hasNoPredecessors() ||
      llvm::hasSingleElement(dest->getPredecessors()))
    return false;

  bool changed = false;
  for (BlockArgument arg : dest->getArguments()) {
    if (arg.use_empty())
      continue;

    Value commonValue;
    bool allSame = true;
    for (Block *pred : dest->getPredecessors()) {
      auto branch = dyn_cast<BranchOpInterface>(pred->getTerminator());
      if (!branch) {
        allSame = false;
        break;
      }

      Value incoming;
      for (unsigned i = 0; i < branch->getNumSuccessors(); ++i) {
        if (branch->getSuccessor(i) != dest)
          continue;
        SuccessorOperands succOps = branch.getSuccessorOperands(i);
        if (arg.getArgNumber() >= succOps.size()) {
          allSame = false;
          break;
        }
        incoming = succOps[arg.getArgNumber()];
        break;
      }
      if (!incoming || (commonValue && commonValue != incoming)) {
        allSame = false;
        break;
      }
      commonValue = incoming;
    }

    if (allSame && commonValue && commonValue != arg) {
      rewriter.replaceAllUsesWith(arg, commonValue);
      changed = true;
    }
  }
  return changed;
}


Review comment (suggestion):
```suggestion
      for (int i : llvm::seq<int>(0, branch->getNumSuccessors())) {
```

Nit: this makes it more clear that `i` isn't modified in the loop.

Code after:
/// ^bb1(%arg0: i32):
///   use(%arg0)
/// ->
/// ^bb1(%arg0: i32):
///   use(%c)                   // %arg0 has no uses and can be removed
///
static LogicalResult simplifyUniformBlockArgs(Block *dest,
                                              PatternRewriter &rewriter) {
  if (dest->hasNoPredecessors() ||
      llvm::hasSingleElement(dest->getPredecessors()))
    return failure();

  bool changed = false;
  for (BlockArgument arg : dest->getArguments()) {
    if (arg.use_empty())
      continue;

    Value commonValue;
    for (Block *pred : dest->getPredecessors()) {
      auto branch = dyn_cast<BranchOpInterface>(pred->getTerminator());
      if (!branch) {
        commonValue = Value();
        break;
      }

      for (auto [i, succ] : llvm::enumerate(branch->getSuccessors())) {
        if (succ != dest)
          continue;

        // Produced operands are modeled by BranchOpInterface as null Values.
        Value val = branch.getSuccessorOperands(i)[arg.getArgNumber()];
        if (commonValue && commonValue != val) {
          commonValue = Value();
          break;
        }
        commonValue = val;
      }

      if (!commonValue)
        break;
    }

    if (commonValue && commonValue != arg) {
      rewriter.replaceAllUsesWith(arg, commonValue);
      changed = true;
    }
  }
  return success(changed);
}

namespace {