[Mlir-commits] [llvm] [mlir] [IR] Split Br into UncondBr and CondBr (PR #184027)

Nikita Popov llvmlistbot at llvm.org
Sun Mar 1 12:17:25 PST 2026


================
@@ -3186,6 +3151,196 @@ struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
 
+//===----------------------------------------------------------------------===//
+//                               UncondBrInst Class
+//===----------------------------------------------------------------------===//
+
+//===---------------------------------------------------------------------------
+/// Unconditional Branch instruction.
+///
+class UncondBrInst : public BranchInst {
+  UncondBrInst(const UncondBrInst &BI, AllocInfo AllocInfo);
+  LLVM_ABI explicit UncondBrInst(BasicBlock *IfTrue, AllocInfo AllocInfo,
+                                 InsertPosition InsertBefore);
+
+protected:
+  // Note: Instruction needs to be a friend here to call cloneImpl.
+  friend class Instruction;
+
+  LLVM_ABI UncondBrInst *cloneImpl() const;
+
+public:
+  static UncondBrInst *Create(BasicBlock *IfTrue,
+                              InsertPosition InsertBefore = nullptr) {
+    IntrusiveOperandsAllocMarker AllocMarker{1};
+    return new (AllocMarker) UncondBrInst(IfTrue, AllocMarker, InsertBefore);
+  }
+
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
+private:
+  // Hide methods.
+  using BranchInst::getCondition;
+  using BranchInst::isConditional;
+  using BranchInst::isUnconditional;
+  using BranchInst::setCondition;
+  using BranchInst::swapSuccessors;
+
+public:
+  unsigned getNumSuccessors() const { return 1; }
+
+  BasicBlock *getSuccessor(unsigned i = 0) const {
+    assert(i == 0 && "Successor # out of range for Branch!");
+    return cast_or_null<BasicBlock>(Op<-1>().get());
+  }
+
+  void setSuccessor(BasicBlock *NewSucc) { Op<-1>() = NewSucc; }
+  void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
+    assert(idx == 0 && "Successor # out of range for Branch!");
+    Op<-1>() = NewSucc;
+  }
+
+  iterator_range<succ_op_iterator> successors() {
+    return make_range(succ_op_iterator(value_op_begin()),
+                      succ_op_iterator(value_op_end()));
+  }
+
+  iterator_range<const_succ_op_iterator> successors() const {
+    return make_range(const_succ_op_iterator(value_op_begin()),
+                      const_succ_op_iterator(value_op_end()));
+  }
+
+  // Methods for support type inquiry through isa, cast, and dyn_cast:
+  static bool classof(const Instruction *I) {
+    return (I->getOpcode() == Instruction::UncondBr);
+  }
+  static bool classof(const Value *V) {
+    return isa<Instruction>(V) && classof(cast<Instruction>(V));
+  }
+};
+
+template <>
+struct OperandTraits<UncondBrInst>
+    : public FixedNumOperandTraits<UncondBrInst, 1> {};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UncondBrInst, Value)
+
+//===----------------------------------------------------------------------===//
+//                               CondBrInst Class
+//===----------------------------------------------------------------------===//
+
+//===---------------------------------------------------------------------------
+/// Conditional or Unconditional Branch instruction.
----------------
nikic wrote:

```suggestion
/// Conditional Branch instruction.
```

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


More information about the Mlir-commits mailing list