[clang] [CIR] Fold cir.not into cir.brcond when flattening if and ternary (PR #233918)

Adam Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 15:00:52 PST 2026


https://github.com/adams381 updated https://github.com/llvm/llvm-project/pull/233918

>From 0e3406204922e6c54ac7ea54428441b384949209 Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Tue, 20 Oct 2026 10:24:14 -0700
Subject: [PATCH 1/2] [CIR] Fold cir.not into cir.brcond when flattening if and
 ternary

For branches like `if (!x)` and `!x ? a : b`, CIR branches on
`xor x, true`.  That xor hides the compare from CallSiteSplitting, which
runs before InstCombine can fold it away and only understands icmp
conditions.  FlattenCFG now branches on x and swaps the two targets
instead of branching on the cir.not.  Classic CodeGen does the same in
EmitBranchOnBoolExpr, and it keeps the xor out of the branch condition.

This was seen as a runtime performance issue on SPEC CPU 2017 502.gcc_r,
and kept a hot call to bitmap_elt_ior from being split and inlined.
The fix closes nearly all of gcc's 8% instruction-count gap against
classic.

Assisted-by: Cursor / Claude Opus 5.5
---
 .../lib/CIR/Dialect/Transforms/FlattenCFG.cpp |  47 ++++-
 clang/test/CIR/CodeGen/atomic.c               |  30 ++--
 clang/test/CIR/CodeGen/branch-on-not.c        | 120 +++++++++++++
 clang/test/CIR/CodeGen/nrvo.cpp               |   8 +-
 .../CIR/Transforms/flatten-brcond-not.cir     | 167 ++++++++++++++++++
 .../Transforms/flatten-cleanup-scope-eh.cir   |   6 +-
 6 files changed, 342 insertions(+), 36 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/branch-on-not.c
 create mode 100644 clang/test/CIR/Transforms/flatten-brcond-not.cir

diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index 2787b57adad6ed..6e8c2efe120c4f 100644
--- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
@@ -46,6 +46,35 @@ void lowerTerminator(mlir::Operation *op, mlir::Block *dest,
   rewriter.replaceOpWithNewOp<cir::BrOp>(op, dest);
 }
 
+/// Creates a cir.brcond to `destTrue` if `cond` is true and to `destFalse`
+/// otherwise.  If `cond` is a cir.not, the branch is on its operand with the
+/// destination blocks swapped, repeating for each nested cir.not.  The
+/// cir.not ops are left in place for eraseDeadNots.
+static void createBrCondPeelingNot(mlir::PatternRewriter &rewriter,
+                                   mlir::Location loc, mlir::Value cond,
+                                   mlir::Block *destTrue,
+                                   mlir::Block *destFalse) {
+  while (auto notOp = cond.getDefiningOp<cir::NotOp>()) {
+    cond = notOp.getInput();
+    std::swap(destTrue, destFalse);
+  }
+  cir::BrCondOp::create(rewriter, loc, cond, destTrue, destFalse);
+}
+
+/// Erases `cond` if it is an unused cir.not, then repeats on its operand,
+/// stopping at the first cir.not that still has uses.  Call after the
+/// structured op that used `cond` is replaced, so a cir.not that
+/// createBrCondPeelingNot looked through does not reach LLVM IR as a dead
+/// `xor`.
+static void eraseDeadNots(mlir::PatternRewriter &rewriter, mlir::Value cond) {
+  while (auto notOp = cond.getDefiningOp<cir::NotOp>()) {
+    if (!notOp->use_empty())
+      return;
+    cond = notOp.getInput();
+    rewriter.eraseOp(notOp);
+  }
+}
+
 /// Walks a region while skipping operations of type `Ops`. This ensures the
 /// callback is not applied to said operations and its children.
 template <typename... Ops>
@@ -173,8 +202,8 @@ struct CIRIfFlattening : public mlir::OpRewritePattern<cir::IfOp> {
     }
 
     rewriter.setInsertionPointToEnd(currentBlock);
-    cir::BrCondOp::create(rewriter, loc, ifOp.getCondition(), thenBeforeBody,
-                          elseBeforeBody);
+    mlir::Value cond = ifOp.getCondition();
+    createBrCondPeelingNot(rewriter, loc, cond, thenBeforeBody, elseBeforeBody);
 
     if (!emptyElse) {
       rewriter.setInsertionPointToEnd(elseAfterBody);
@@ -186,6 +215,7 @@ struct CIRIfFlattening : public mlir::OpRewritePattern<cir::IfOp> {
     }
 
     rewriter.replaceOp(ifOp, continueBlock->getArguments());
+    eraseDeadNots(rewriter, cond);
     return mlir::success();
   }
 };
@@ -800,9 +830,11 @@ class CIRTernaryOpFlattening : public mlir::OpRewritePattern<cir::TernaryOp> {
     rewriter.inlineRegionBefore(falseRegion, continueBlock);
 
     rewriter.setInsertionPointToEnd(condBlock);
-    cir::BrCondOp::create(rewriter, loc, op.getCond(), trueBlock, falseBlock);
+    mlir::Value cond = op.getCond();
+    createBrCondPeelingNot(rewriter, loc, cond, trueBlock, falseBlock);
 
     rewriter.replaceOp(op, continueBlock->getArguments());
+    eraseDeadNots(rewriter, cond);
 
     // Ok, we're done!
     return mlir::success();
@@ -2169,10 +2201,11 @@ void CIRFlattenCFGPass::runOnOperation() {
     changedListener.reset();
 
     // Collect flatten candidates post-order so an inner op is handled before
-    // its parent; op pointers stay valid across the block splits / region
-    // inlines the patterns perform (a pattern only erases the matched op and
-    // its descendants, which are visited first), so the list can be iterated
-    // directly.
+    // its parent.  Patterns erase only the matched op, its descendants, and the
+    // dead cir.not ops that eraseDeadNots removes.  Descendants are visited
+    // before their parent and cir.not ops are never collected, so op pointers
+    // in the list stay valid across the block splits and region inlines the
+    // patterns perform, and the list can be iterated directly.
     llvm::SmallVector<Operation *, 16> ops;
     getOperation()->walk<mlir::WalkOrder::PostOrder>([&](Operation *op) {
       if (isa<IfOp, ScopeOp, SwitchOp, LoopOpInterface, TernaryOp,
diff --git a/clang/test/CIR/CodeGen/atomic.c b/clang/test/CIR/CodeGen/atomic.c
index 97e7435d68f7af..e726fc3f7472c8 100644
--- a/clang/test/CIR/CodeGen/atomic.c
+++ b/clang/test/CIR/CodeGen/atomic.c
@@ -351,8 +351,7 @@ void c11_atomic_cmpxchg_strong(_Atomic(int) *ptr, int *expected, int desired, in
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -433,8 +432,7 @@ void c11_atomic_cmpxchg_weak(_Atomic(int) *ptr, int *expected, int desired, int
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -514,8 +512,7 @@ void atomic_cmpxchg(int *ptr, int *expected, int *desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -545,8 +542,7 @@ void atomic_cmpxchg(int *ptr, int *expected, int *desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -695,8 +691,7 @@ void atomic_cmpxchg_n(int *ptr, int *expected, int desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -726,8 +721,7 @@ void atomic_cmpxchg_n(int *ptr, int *expected, int desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3645,8 +3639,7 @@ void atomic_cmpxchg_maybe_weak(int *ptr, int *expected, int *desired, int failur
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3658,8 +3651,7 @@ void atomic_cmpxchg_maybe_weak(int *ptr, int *expected, int *desired, int failur
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3823,8 +3815,7 @@ void atomic_cmpxchg_n_maybe_weak(int *ptr, int *expected, int desired, int failu
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3836,8 +3827,7 @@ void atomic_cmpxchg_n_maybe_weak(int *ptr, int *expected, int desired, int failu
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    %[[FAILED:.+]] = xor i1 %[[SUCCESS]], true
-  // LLVM-NEXT:    br i1 %[[FAILED]], label %[[LABEL_FAILED:.+]], label %[[LABEL_CONT:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
diff --git a/clang/test/CIR/CodeGen/branch-on-not.c b/clang/test/CIR/CodeGen/branch-on-not.c
new file mode 100644
index 00000000000000..99afcb8750cf89
--- /dev/null
+++ b/clang/test/CIR/CodeGen/branch-on-not.c
@@ -0,0 +1,120 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVMCIR --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefixes=LLVM,OGCG --input-file=%t.ll %s
+
+void f(void);
+void h(void);
+int g(int *p, int y);
+int t(void);
+int u(void);
+
+void if_not(int *p) {
+  if (!p)
+    f();
+}
+
+// CIR-LABEL: cir.func{{.*}} @if_not(
+// CIR:         %[[B:.*]] = cir.cast ptr_to_bool %{{.*}} : !cir.ptr<!s32i> -> !cir.bool
+// CIR-NEXT:    %[[N:.*]] = cir.not %[[B]] : !cir.bool
+// CIR-NEXT:    cir.if %[[N]] {
+
+// LLVM-LABEL: define dso_local void @if_not(
+// LLVM:         %[[NONNULL:.*]] = icmp ne ptr %{{.*}}, null
+// LLVM-NEXT:    br i1 %[[NONNULL]], label %[[END:[a-z0-9.]+]], label %[[THEN:[a-z0-9.]+]]
+// LLVM:       {{^}}[[THEN]]:
+// LLVM-NEXT:    call void @f()
+// LLVM-NEXT:    br label %[[END]]
+
+void if_not_else(int *p) {
+  if (!p)
+    f();
+  else
+    h();
+}
+
+// CIR-LABEL: cir.func{{.*}} @if_not_else(
+// CIR:         %[[N:.*]] = cir.not %{{.*}} : !cir.bool
+// CIR-NEXT:    cir.if %[[N]] {
+
+// LLVM-LABEL: define dso_local void @if_not_else(
+// LLVM:         %[[NONNULL:.*]] = icmp ne ptr %{{.*}}, null
+// LLVM-NEXT:    br i1 %[[NONNULL]], label %[[ELSE:[a-z0-9.]+]], label %[[THEN:[a-z0-9.]+]]
+// LLVM:       {{^}}[[THEN]]:
+// LLVM-NEXT:    call void @f()
+// LLVM:       {{^}}[[ELSE]]:
+// LLVM-NEXT:    call void @h()
+
+int not_ternary(int a) { return !a ? t() : u(); }
+
+// CIR-LABEL: cir.func{{.*}} @not_ternary(
+// CIR:         %[[N:.*]] = cir.not %{{.*}} : !cir.bool
+// CIR-NEXT:    %{{.*}} = cir.ternary(%[[N]], true {
+
+// LLVM-LABEL: define dso_local i32 @not_ternary(
+// LLVM:         %[[NONZERO:.*]] = icmp ne i32 %{{.*}}, 0
+// LLVM-NEXT:    br i1 %[[NONZERO]], label %[[FALSE:[a-z0-9.]+]], label %[[TRUE:[a-z0-9.]+]]
+// LLVM:       {{^}}[[TRUE]]:
+// LLVM-NEXT:    %{{.*}} = call i32 @t()
+// LLVM:       {{^}}[[FALSE]]:
+// LLVM-NEXT:    %{{.*}} = call i32 @u()
+
+void while_not(volatile int *done) {
+  while (!*done)
+    f();
+}
+
+// CIR-LABEL: cir.func{{.*}} @while_not(
+// CIR:         %[[B:.*]] = cir.cast int_to_bool %{{.*}} : !s32i -> !cir.bool
+// CIR-NEXT:    %[[N:.*]] = cir.not %[[B]] : !cir.bool
+// CIR-NEXT:    cir.condition(%[[N]])
+
+// LLVM-LABEL: define dso_local void @while_not(
+// LLVM:         %[[NONZERO:.*]] = icmp ne i32 %{{.*}}, 0
+// LLVM-NEXT:    %[[LNOT:.*]] = xor i1 %[[NONZERO]], true
+// LLVM-NEXT:    br i1 %[[LNOT]], label %[[BODY:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
+// LLVM:       {{^}}[[BODY]]:
+// LLVM-NEXT:    call void @f()
+
+int not_or(int *p, int y) {
+  if (!p || *p == y)
+    return g(p, y);
+  return 0;
+}
+
+// CIR-LABEL: cir.func{{.*}} @not_or(
+// CIR:         %[[B:.*]] = cir.cast ptr_to_bool %{{.*}} : !cir.ptr<!s32i> -> !cir.bool
+// CIR-NEXT:    %[[N:.*]] = cir.not %[[B]] : !cir.bool
+// CIR-NEXT:    %{{.*}} = cir.ternary(%[[N]], true {
+
+// LLVM-LABEL: define dso_local i32 @not_or(
+// LLVM:         %[[NONNULL:.*]] = icmp ne ptr %{{.*}}, null
+// LLVM-NEXT:    br i1 %[[NONNULL]], label %[[RHS:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
+// LLVM:       {{^}}[[RHS]]:
+// LLVM:         %[[CMP:.*]] = icmp eq i32
+// LLVMCIR:      %[[OR:.*]] = phi i1 [ true, %{{.*}} ], [ %[[CMP]], %[[RHS]] ]
+// LLVMCIR:      br i1 %[[OR]], label %[[THEN:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
+// OGCG-NEXT:    br i1 %[[CMP]], label %[[THEN:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
+// LLVM:       {{^}}[[THEN]]:
+// LLVM:         call i32 @g(ptr noundef %{{.+}}, i32 noundef %{{.+}})
+
+void not_of_or(int a, int b) {
+  if (!(a || b))
+    f();
+}
+
+// CIR-LABEL: cir.func{{.*}} @not_of_or(
+// CIR:         %[[OR:.*]] = cir.ternary(
+// CIR:         %[[N:.*]] = cir.not %[[OR]] : !cir.bool
+// CIR-NEXT:    cir.if %[[N]] {
+
+// LLVM-LABEL: define dso_local void @not_of_or(
+// LLVMCIR:      %[[OR:.*]] = phi i1
+// LLVMCIR-NOT:  xor
+// LLVMCIR:      br i1 %[[OR]], label %{{[a-z0-9.]+}}, label %[[THEN:[a-z0-9.]+]]
+// OGCG:         br i1 %{{.*}}, label %[[END:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
+// OGCG:         br i1 %{{.*}}, label %[[END]], label %[[THEN:[a-z0-9.]+]]
+// LLVM:       {{^}}[[THEN]]:
+// LLVM-NEXT:    call void @f()
diff --git a/clang/test/CIR/CodeGen/nrvo.cpp b/clang/test/CIR/CodeGen/nrvo.cpp
index 56c7781b1262c7..4564504a2a1185 100644
--- a/clang/test/CIR/CodeGen/nrvo.cpp
+++ b/clang/test/CIR/CodeGen/nrvo.cpp
@@ -93,12 +93,10 @@ NonTrivial test_nrvo() {
 // LLVM:   call void @_Z10maybeThrowv()
 // LLVMCIR:   store i8 1, ptr %[[NRVO_FLAG]]
 // OGCG:   store i1 true, ptr %[[NRVO_FLAG]]
-// LLVMCIR:   %[[NRVO_VAL:.*]] = load i8, ptr %[[NRVO_FLAG]]
-// LLVMCIR:   %[[NRVO_VAL_TRUNC:.*]] = trunc i8 %[[NRVO_VAL]] to i1
-// LLVMCIR:   %[[NOT_NRVO_VAL:.*]] = xor i1 %[[NRVO_VAL_TRUNC]], true
-// LLVMCIR:   br i1 %[[NOT_NRVO_VAL]], label %[[NRVO_UNUSED:.*]], label %[[NRVO_DONE:.*]]
+// LLVMCIR:   %[[NRVO_BYTE:.*]] = load i8, ptr %[[NRVO_FLAG]]
+// LLVMCIR:   %[[NRVO_VAL:.*]] = trunc i8 %[[NRVO_BYTE]] to i1
 // OGCG:   %[[NRVO_VAL:.*]] = load i1, ptr %[[NRVO_FLAG]]
-// OGCG:   br i1 %[[NRVO_VAL]], label %[[NRVO_DONE:.*]], label %[[NRVO_UNUSED:.*]]
+// LLVM-NEXT:   br i1 %[[NRVO_VAL]], label %[[NRVO_DONE:.*]], label %[[NRVO_UNUSED:.*]]
 // LLVM: [[NRVO_UNUSED]]:
 // LLVM-NEXT:   call void @_ZN10NonTrivialD1Ev(ptr noundef nonnull align 1 dereferenceable(1) %[[RESULT]])
 // LLVM-NEXT:   br label %[[NRVO_DONE]]
diff --git a/clang/test/CIR/Transforms/flatten-brcond-not.cir b/clang/test/CIR/Transforms/flatten-brcond-not.cir
new file mode 100644
index 00000000000000..c681e6b9a47ed6
--- /dev/null
+++ b/clang/test/CIR/Transforms/flatten-brcond-not.cir
@@ -0,0 +1,167 @@
+// RUN: cir-opt %s -cir-flatten-cfg -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func private @onTrue()
+  cir.func private @onFalse()
+  cir.func private @body()
+  cir.func private @cond() -> !cir.bool
+
+  cir.func @if_not(%arg0: !cir.bool) {
+    %0 = cir.not %arg0 : !cir.bool
+    cir.if %0 {
+      cir.call @onTrue() : () -> ()
+    } else {
+      cir.call @onFalse() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_not(%arg0: !cir.bool)
+  // CHECK-NOT:     cir.not
+  // CHECK:         cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.call @onTrue()
+  // CHECK:       ^[[FALSE]]:
+  // CHECK-NEXT:    cir.call @onFalse()
+
+  cir.func @if_not_no_else(%arg0: !cir.bool) {
+    %0 = cir.not %arg0 : !cir.bool
+    cir.if %0 {
+      cir.call @onTrue() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_not_no_else(%arg0: !cir.bool)
+  // CHECK-NOT:     cir.not
+  // CHECK:         cir.brcond %arg0 ^[[CONT:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.call @onTrue()
+  // CHECK:       ^[[CONT]]:
+  // CHECK-NEXT:    cir.return
+
+  cir.func @ternary_not(%arg0: !cir.bool) {
+    %0 = cir.not %arg0 : !cir.bool
+    cir.ternary(%0, true {
+      cir.call @onTrue() : () -> ()
+      cir.yield
+    }, false {
+      cir.call @onFalse() : () -> ()
+      cir.yield
+    }) : (!cir.bool) -> ()
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @ternary_not(%arg0: !cir.bool)
+  // CHECK-NOT:     cir.not
+  // CHECK:         cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.call @onTrue()
+  // CHECK:       ^[[FALSE]]:
+  // CHECK-NEXT:    cir.call @onFalse()
+
+  cir.func @ternary_not_value(%arg0: !cir.bool, %arg1: !s32i, %arg2: !s32i) -> !s32i {
+    %0 = cir.not %arg0 : !cir.bool
+    %1 = cir.ternary(%0, true {
+      cir.yield %arg1 : !s32i
+    }, false {
+      cir.yield %arg2 : !s32i
+    }) : (!cir.bool) -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK-LABEL: cir.func @ternary_not_value(%arg0: !cir.bool, %arg1: !s32i, %arg2: !s32i) -> !s32i
+  // CHECK-NOT:     cir.not
+  // CHECK:         cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.br ^[[CONT:bb[0-9]+]](%arg1 : !s32i)
+  // CHECK:       ^[[FALSE]]:
+  // CHECK-NEXT:    cir.br ^[[CONT]](%arg2 : !s32i)
+  // CHECK:       ^[[CONT]](%[[RES:.*]]: !s32i):
+  // CHECK:         cir.return %[[RES]] : !s32i
+
+  cir.func @while_not() {
+    cir.while {
+      %0 = cir.call @cond() : () -> !cir.bool
+      %1 = cir.not %0 : !cir.bool
+      cir.condition(%1)
+    } do {
+      cir.call @body() : () -> ()
+      cir.yield
+    }
+    cir.return
+  }
+
+  // The cir.not feeding a loop condition is not folded into the branch.
+  // CHECK-LABEL: cir.func @while_not()
+  // CHECK:         %[[C:.*]] = cir.call @cond()
+  // CHECK-NEXT:    %[[N:.*]] = cir.not %[[C]] : !cir.bool
+  // CHECK-NEXT:    cir.brcond %[[N]] ^[[BODY:bb[0-9]+]], ^[[EXIT:bb[0-9]+]]
+  // CHECK:       ^[[BODY]]:
+  // CHECK-NEXT:    cir.call @body()
+  // CHECK:       ^[[EXIT]]:
+  // CHECK-NEXT:    cir.return
+
+  cir.func @if_double_not(%arg0: !cir.bool) {
+    %0 = cir.not %arg0 : !cir.bool
+    %1 = cir.not %0 : !cir.bool
+    cir.if %1 {
+      cir.call @onTrue() : () -> ()
+    } else {
+      cir.call @onFalse() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_double_not(%arg0: !cir.bool)
+  // CHECK-NOT:     cir.not
+  // CHECK:         cir.brcond %arg0 ^[[TRUE:bb[0-9]+]], ^[[FALSE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.call @onTrue()
+  // CHECK:       ^[[FALSE]]:
+  // CHECK-NEXT:    cir.call @onFalse()
+
+  cir.func @if_double_not_inner_used(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>) {
+    %0 = cir.not %arg0 : !cir.bool
+    cir.store %0, %arg1 : !cir.bool, !cir.ptr<!cir.bool>
+    %1 = cir.not %0 : !cir.bool
+    cir.if %1 {
+      cir.call @onTrue() : () -> ()
+    } else {
+      cir.call @onFalse() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_double_not_inner_used(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>)
+  // CHECK-NEXT:    %[[N:.*]] = cir.not %arg0 : !cir.bool
+  // CHECK-NEXT:    cir.store %[[N]], %arg1
+  // CHECK-NEXT:    cir.brcond %arg0 ^[[TRUE:bb[0-9]+]], ^[[FALSE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.call @onTrue()
+  // CHECK:       ^[[FALSE]]:
+  // CHECK-NEXT:    cir.call @onFalse()
+
+  cir.func @if_not_other_use(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>) {
+    %0 = cir.not %arg0 : !cir.bool
+    cir.store %0, %arg1 : !cir.bool, !cir.ptr<!cir.bool>
+    cir.if %0 {
+      cir.call @onTrue() : () -> ()
+    } else {
+      cir.call @onFalse() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_not_other_use(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>)
+  // CHECK-NEXT:    %[[N:.*]] = cir.not %arg0 : !cir.bool
+  // CHECK-NEXT:    cir.store %[[N]], %arg1
+  // CHECK-NEXT:    cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
+  // CHECK:       ^[[TRUE]]:
+  // CHECK-NEXT:    cir.call @onTrue()
+  // CHECK:       ^[[FALSE]]:
+  // CHECK-NEXT:    cir.call @onFalse()
+}
diff --git a/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir b/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir
index 93b3f991885620..58648f89f44e43 100644
--- a/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir
+++ b/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir
@@ -277,8 +277,7 @@ cir.func @test_nrvo() -> !rec_NonTrivial {
 // Normal cleanup: inlined cleanup region with flattened cir.if.
 // CHECK:       ^[[NORMAL_CLEANUP]]:
 // CHECK:         %[[N_FLAG:.*]] = cir.load align(1) %[[NRVO]] : !cir.ptr<!cir.bool>, !cir.bool
-// CHECK:         %[[N_NOT:.*]] = cir.not %[[N_FLAG]] : !cir.bool
-// CHECK:         cir.brcond %[[N_NOT]] ^[[N_IF_TRUE:bb[0-9]+]], ^[[N_MERGE:bb[0-9]+]]
+// CHECK-NEXT:    cir.brcond %[[N_FLAG]] ^[[N_MERGE:bb[0-9]+]], ^[[N_IF_TRUE:bb[0-9]+]]
 //
 // CHECK:       ^[[N_IF_TRUE]]:
 // CHECK:         cir.call @_ZN10NonTrivialD1Ev(%[[RETVAL]]) nothrow
@@ -299,8 +298,7 @@ cir.func @test_nrvo() -> !rec_NonTrivial {
 // CHECK:       ^[[EH_CLEANUP]](%[[ET:.*]]: !cir.eh_token):
 // CHECK:         %[[CT:.*]] = cir.begin_cleanup %[[ET]] : !cir.eh_token -> !cir.cleanup_token
 // CHECK:         %[[EH_FLAG:.*]] = cir.load align(1) %[[NRVO]] : !cir.ptr<!cir.bool>, !cir.bool
-// CHECK:         %[[EH_NOT:.*]] = cir.not %[[EH_FLAG]] : !cir.bool
-// CHECK:         cir.brcond %[[EH_NOT]] ^[[EH_IF_TRUE:bb[0-9]+]], ^[[EH_MERGE:bb[0-9]+]]
+// CHECK-NEXT:    cir.brcond %[[EH_FLAG]] ^[[EH_MERGE:bb[0-9]+]], ^[[EH_IF_TRUE:bb[0-9]+]]
 //
 // EH cleanup if-true: call destructor.
 // CHECK:       ^[[EH_IF_TRUE]]:

>From abe21cd190b2568a0ff2dea2c347c8dc30c5782f Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Tue, 3 Nov 2026 14:55:20 -0800
Subject: [PATCH 2/2] [CIR] Move the cir.not fold into LoweringPrepare

Assisted-by: Cursor / Claude Opus 5.5
---
 clang/include/clang/CIR/MissingFeatures.h     |   1 -
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp          |   6 -
 .../lib/CIR/Dialect/Transforms/FlattenCFG.cpp |  47 +----
 .../Dialect/Transforms/LoweringPrepare.cpp    |  62 +++++-
 clang/test/CIR/CodeGen/atomic.c               |  80 +++++---
 clang/test/CIR/CodeGen/branch-on-not.c        |  51 +++--
 clang/test/CIR/CodeGen/nrvo.cpp               |  18 +-
 .../Transforms/flatten-cleanup-scope-eh.cir   |   6 +-
 ...-not.cir => lowering-prepare-not-cond.cir} | 178 +++++++++++-------
 9 files changed, 276 insertions(+), 173 deletions(-)
 rename clang/test/CIR/Transforms/{flatten-brcond-not.cir => lowering-prepare-not-cond.cir} (50%)

diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h
index 323cf2423c354c..42bda6460261df 100644
--- a/clang/include/clang/CIR/MissingFeatures.h
+++ b/clang/include/clang/CIR/MissingFeatures.h
@@ -112,7 +112,6 @@ struct MissingFeatures {
 
   // Clang early optimizations or things defered to LLVM lowering.
   static bool mayHaveIntegerOverflow() { return false; }
-  static bool shouldReverseUnaryCondOnBoolExpr() { return false; }
 
   // RecordType
   static bool astRecordDeclAttr() { return false; }
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 68a06cf7e04ab5..103122235a2b0e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2828,12 +2828,6 @@ mlir::Value CIRGenFunction::emitOpOnBoolExpr(mlir::Location loc,
   assert(!cir::MissingFeatures::generateDebugInfo());
   cond = cond->IgnoreParens();
 
-  // In LLVM the condition is reversed here for efficient codegen.
-  // This should be done in CIR prior to LLVM lowering, if we do now
-  // we can make CIR based diagnostics misleading.
-  //  cir.ternary(!x, t, f) -> cir.ternary(x, f, t)
-  assert(!cir::MissingFeatures::shouldReverseUnaryCondOnBoolExpr());
-
   if (const ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(cond)) {
     Expr *trueExpr = condOp->getTrueExpr();
     Expr *falseExpr = condOp->getFalseExpr();
diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index 7019526c9e6aed..8a54faaee5129b 100644
--- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
@@ -49,35 +49,6 @@ void lowerTerminator(mlir::Operation *op, mlir::Block *dest,
                                          loopHints, dest);
 }
 
-/// Creates a cir.brcond to `destTrue` if `cond` is true and to `destFalse`
-/// otherwise.  If `cond` is a cir.not, the branch is on its operand with the
-/// destination blocks swapped, repeating for each nested cir.not.  The
-/// cir.not ops are left in place for eraseDeadNots.
-static void createBrCondPeelingNot(mlir::PatternRewriter &rewriter,
-                                   mlir::Location loc, mlir::Value cond,
-                                   mlir::Block *destTrue,
-                                   mlir::Block *destFalse) {
-  while (auto notOp = cond.getDefiningOp<cir::NotOp>()) {
-    cond = notOp.getInput();
-    std::swap(destTrue, destFalse);
-  }
-  cir::BrCondOp::create(rewriter, loc, cond, destTrue, destFalse);
-}
-
-/// Erases `cond` if it is an unused cir.not, then repeats on its operand,
-/// stopping at the first cir.not that still has uses.  Call after the
-/// structured op that used `cond` is replaced, so a cir.not that
-/// createBrCondPeelingNot looked through does not reach LLVM IR as a dead
-/// `xor`.
-static void eraseDeadNots(mlir::PatternRewriter &rewriter, mlir::Value cond) {
-  while (auto notOp = cond.getDefiningOp<cir::NotOp>()) {
-    if (!notOp->use_empty())
-      return;
-    cond = notOp.getInput();
-    rewriter.eraseOp(notOp);
-  }
-}
-
 /// Walks a region while skipping operations of type `Ops`. This ensures the
 /// callback is not applied to said operations and its children.
 template <typename... Ops>
@@ -207,8 +178,8 @@ struct CIRIfFlattening : public mlir::OpRewritePattern<cir::IfOp> {
     }
 
     rewriter.setInsertionPointToEnd(currentBlock);
-    mlir::Value cond = ifOp.getCondition();
-    createBrCondPeelingNot(rewriter, loc, cond, thenBeforeBody, elseBeforeBody);
+    cir::BrCondOp::create(rewriter, loc, ifOp.getCondition(), thenBeforeBody,
+                          elseBeforeBody);
 
     if (!emptyElse) {
       rewriter.setInsertionPointToEnd(elseAfterBody);
@@ -221,7 +192,6 @@ struct CIRIfFlattening : public mlir::OpRewritePattern<cir::IfOp> {
     }
 
     rewriter.replaceOp(ifOp, continueBlock->getArguments());
-    eraseDeadNots(rewriter, cond);
     return mlir::success();
   }
 };
@@ -858,11 +828,9 @@ class CIRTernaryOpFlattening : public mlir::OpRewritePattern<cir::TernaryOp> {
     rewriter.inlineRegionBefore(falseRegion, continueBlock);
 
     rewriter.setInsertionPointToEnd(condBlock);
-    mlir::Value cond = op.getCond();
-    createBrCondPeelingNot(rewriter, loc, cond, trueBlock, falseBlock);
+    cir::BrCondOp::create(rewriter, loc, op.getCond(), trueBlock, falseBlock);
 
     rewriter.replaceOp(op, continueBlock->getArguments());
-    eraseDeadNots(rewriter, cond);
 
     // Ok, we're done!
     return mlir::success();
@@ -2641,11 +2609,10 @@ void CIRFlattenCFGPass::runOnOperation() {
     changedListener.reset();
 
     // Collect flatten candidates post-order so an inner op is handled before
-    // its parent.  Patterns erase only the matched op, its descendants, and the
-    // dead cir.not ops that eraseDeadNots removes.  Descendants are visited
-    // before their parent and cir.not ops are never collected, so op pointers
-    // in the list stay valid across the block splits and region inlines the
-    // patterns perform, and the list can be iterated directly.
+    // its parent; op pointers stay valid across the block splits / region
+    // inlines the patterns perform (a pattern only erases the matched op and
+    // its descendants, which are visited first), so the list can be iterated
+    // directly.
     llvm::SmallVector<Operation *, 16> ops;
     getOperation()->walk<mlir::WalkOrder::PostOrder>([&](Operation *op) {
       if (isa<IfOp, ScopeOp, SwitchOp, LoopOpInterface, TernaryOp,
diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index 005d305085f322..b01439414a070e 100644
--- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -101,6 +101,8 @@ struct LoweringPreparePass
   void lowerLocalInitOp(cir::LocalInitOp op);
   void lowerRegisterExitDtorOp(cir::RegisterExitDtorOp op);
   void lowerStdOp(cir::StdOpInterface op);
+  void lowerIfOp(cir::IfOp op);
+  void lowerTernaryOp(cir::TernaryOp op);
 
   /// Return the FuncOp called by `callOp`.  Uses the cached `symbolTables`
   /// member to avoid the O(M) module-wide scan that the static
@@ -2784,6 +2786,59 @@ void LoweringPreparePass::lowerStdOp(cir::StdOpInterface typedOp) {
   op->erase();
 }
 
+/// If `cond` is the result of a chain of cir.not ops, sets it to the input of
+/// the innermost cir.not and erases the cir.not ops left without uses.  An
+/// odd number of cir.not ops inverts the condition, so `trueRegion` and
+/// `falseRegion` are swapped.  If one region is empty, the other is empty
+/// after a swap, and the caller restores any region its op requires.  Returns
+/// true if the regions were swapped.
+static bool stripNotsFromCondition(mlir::OpOperand &cond,
+                                   mlir::Region &trueRegion,
+                                   mlir::Region &falseRegion) {
+  llvm::SmallVector<cir::NotOp> notOps;
+  mlir::Value value = cond.get();
+  while (auto notOp = value.getDefiningOp<cir::NotOp>()) {
+    notOps.push_back(notOp);
+    value = notOp.getInput();
+  }
+  if (notOps.empty())
+    return false;
+
+  cond.set(value);
+  bool swapped = notOps.size() % 2 == 1;
+  if (swapped) {
+    mlir::Region trueBody;
+    trueBody.takeBody(trueRegion);
+    trueRegion.takeBody(falseRegion);
+    falseRegion.takeBody(trueBody);
+  }
+  for (cir::NotOp notOp : notOps) {
+    if (!notOp->use_empty())
+      break;
+    notOp.erase();
+  }
+  return swapped;
+}
+
+void LoweringPreparePass::lowerIfOp(cir::IfOp op) {
+  bool hadElse = !op.getElseRegion().empty();
+  bool swapped = stripNotsFromCondition(op.getConditionMutable(),
+                                        op.getThenRegion(), op.getElseRegion());
+  if (!swapped || hadElse)
+    return;
+
+  // The then body was swapped into the else region.  Only the else region of
+  // a cir.if may be empty, so give the then region a block that just yields.
+  cir::CIRBaseBuilderTy builder(getContext());
+  builder.createBlock(&op.getThenRegion());
+  cir::YieldOp::create(builder, op.getLoc());
+}
+
+void LoweringPreparePass::lowerTernaryOp(cir::TernaryOp op) {
+  stripNotsFromCondition(op.getCondMutable(), op.getTrueRegion(),
+                         op.getFalseRegion());
+}
+
 void LoweringPreparePass::runOnOp(mlir::Operation *op) {
   if (auto arrayCtor = dyn_cast<cir::ArrayCtor>(op)) {
     lowerArrayCtor(arrayCtor);
@@ -2833,6 +2888,10 @@ void LoweringPreparePass::runOnOp(mlir::Operation *op) {
     lowerThreeWayCmpOp(threeWayCmp);
   } else if (auto initOp = dyn_cast<cir::LocalInitOp>(op)) {
     lowerLocalInitOp(initOp);
+  } else if (auto ifOp = dyn_cast<cir::IfOp>(op)) {
+    lowerIfOp(ifOp);
+  } else if (auto ternaryOp = dyn_cast<cir::TernaryOp>(op)) {
+    lowerTernaryOp(ternaryOp);
   }
 }
 
@@ -3494,7 +3553,8 @@ void LoweringPreparePass::runOnOperation() {
                   cir::ComplexFMulOp, cir::ComplexFDivOp, cir::DynamicCastOp,
                   cir::FuncOp, cir::CallOp, cir::GetGlobalOp, cir::GlobalOp,
                   cir::StoreOp, cir::CmpThreeWayOp, cir::LocalInitOp,
-                  cir::RegisterExitDtorOp, cir::StdOpInterface>(op))
+                  cir::RegisterExitDtorOp, cir::StdOpInterface, cir::IfOp,
+                  cir::TernaryOp>(op))
       opsToTransform.push_back(op);
   });
 
diff --git a/clang/test/CIR/CodeGen/atomic.c b/clang/test/CIR/CodeGen/atomic.c
index e726fc3f7472c8..324f2a3b9333f5 100644
--- a/clang/test/CIR/CodeGen/atomic.c
+++ b/clang/test/CIR/CodeGen/atomic.c
@@ -342,8 +342,8 @@ void c11_atomic_cmpxchg_strong(_Atomic(int) *ptr, int *expected, int desired, in
   __c11_atomic_compare_exchange_strong(ptr, expected, desired,
                                        __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
   // CIR:         %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:    %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:    cir.if %[[FAILED]] {
+  // CIR-NEXT:    cir.if %[[SUCCESS]] {
+  // CIR-NEXT:    } else {
   // CIR-NEXT:      cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:    }
   // CIR-NEXT:    cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -351,7 +351,9 @@ void c11_atomic_cmpxchg_strong(_Atomic(int) *ptr, int *expected, int desired, in
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -423,8 +425,8 @@ void c11_atomic_cmpxchg_weak(_Atomic(int) *ptr, int *expected, int desired, int
   __c11_atomic_compare_exchange_weak(ptr, expected, desired,
                                      __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
   // CIR:         %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg weak success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:    %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:    cir.if %[[FAILED]] {
+  // CIR-NEXT:    cir.if %[[SUCCESS]] {
+  // CIR-NEXT:    } else {
   // CIR-NEXT:      cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:    }
   // CIR-NEXT:    cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -432,7 +434,9 @@ void c11_atomic_cmpxchg_weak(_Atomic(int) *ptr, int *expected, int desired, int
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -503,8 +507,8 @@ void atomic_cmpxchg(int *ptr, int *expected, int *desired, int failure) {
 
   __atomic_compare_exchange(ptr, expected, desired, /*weak=*/0, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
   // CIR:         %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:    %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:    cir.if %[[FAILED]] {
+  // CIR-NEXT:    cir.if %[[SUCCESS]] {
+  // CIR-NEXT:    } else {
   // CIR-NEXT:      cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:    }
   // CIR-NEXT:    cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -512,7 +516,9 @@ void atomic_cmpxchg(int *ptr, int *expected, int *desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -533,8 +539,8 @@ void atomic_cmpxchg(int *ptr, int *expected, int *desired, int failure) {
 
   __atomic_compare_exchange(ptr, expected, desired, /*weak=*/1, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
   // CIR:         %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg weak success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:    %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:    cir.if %[[FAILED]] {
+  // CIR-NEXT:    cir.if %[[SUCCESS]] {
+  // CIR-NEXT:    } else {
   // CIR-NEXT:      cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:    }
   // CIR-NEXT:    cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -542,7 +548,9 @@ void atomic_cmpxchg(int *ptr, int *expected, int *desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -682,8 +690,8 @@ void atomic_cmpxchg_n(int *ptr, int *expected, int desired, int failure) {
 
   __atomic_compare_exchange_n(ptr, expected, desired, /*weak=*/0, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
   // CIR:         %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:    %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:    cir.if %[[FAILED]] {
+  // CIR-NEXT:    cir.if %[[SUCCESS]] {
+  // CIR-NEXT:    } else {
   // CIR-NEXT:      cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:    }
   // CIR-NEXT:    cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -691,7 +699,9 @@ void atomic_cmpxchg_n(int *ptr, int *expected, int desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -712,8 +722,8 @@ void atomic_cmpxchg_n(int *ptr, int *expected, int desired, int failure) {
 
   __atomic_compare_exchange_n(ptr, expected, desired, /*weak=*/1, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
   // CIR:         %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg weak success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:    %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:    cir.if %[[FAILED]] {
+  // CIR-NEXT:    cir.if %[[SUCCESS]] {
+  // CIR-NEXT:    } else {
   // CIR-NEXT:      cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:    }
   // CIR-NEXT:    cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -721,7 +731,9 @@ void atomic_cmpxchg_n(int *ptr, int *expected, int desired, int failure) {
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3618,15 +3630,15 @@ void atomic_cmpxchg_maybe_weak(int *ptr, int *expected, int *desired, int failur
   // CIR-NEXT:    %[[WEAK_CAST:.*]] = cir.cast int_to_bool %[[WEAK_LOAD]]
   // CIR-NEXT:    cir.if %[[WEAK_CAST]] {
   // CIR:           %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg weak success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:      %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:      cir.if %[[FAILED]] {
+  // CIR-NEXT:      cir.if %[[SUCCESS]] {
+  // CIR-NEXT:      } else {
   // CIR-NEXT:        cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:      }
   // CIR-NEXT:      cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
   // CIR-NEXT:    } else {
   // CIR:           %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:      %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:      cir.if %[[FAILED]] {
+  // CIR-NEXT:      cir.if %[[SUCCESS]] {
+  // CIR-NEXT:      } else {
   // CIR-NEXT:        cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:      }
   // CIR-NEXT:      cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -3639,7 +3651,9 @@ void atomic_cmpxchg_maybe_weak(int *ptr, int *expected, int *desired, int failur
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3651,7 +3665,9 @@ void atomic_cmpxchg_maybe_weak(int *ptr, int *expected, int *desired, int failur
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3794,15 +3810,15 @@ void atomic_cmpxchg_n_maybe_weak(int *ptr, int *expected, int desired, int failu
   // CIR-NEXT:    %[[WEAK_CAST:.*]] = cir.cast int_to_bool %[[WEAK_LOAD]]
   // CIR-NEXT:    cir.if %[[WEAK_CAST]] {
   // CIR:           %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg weak success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:      %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:      cir.if %[[FAILED]] {
+  // CIR-NEXT:      cir.if %[[SUCCESS]] {
+  // CIR-NEXT:      } else {
   // CIR-NEXT:        cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:      }
   // CIR-NEXT:      cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
   // CIR-NEXT:    } else {
   // CIR:           %[[OLD:.+]], %[[SUCCESS:.+]] = cir.atomic.cmpxchg success(seq_cst) failure(acquire) syncscope(system) %{{.+}}, %{{.+}}, %{{.+}} align(4) : (!cir.ptr<!s32i>, !s32i, !s32i) -> (!s32i, !cir.bool)
-  // CIR-NEXT:      %[[FAILED:.+]] = cir.not %[[SUCCESS]] : !cir.bool
-  // CIR-NEXT:      cir.if %[[FAILED]] {
+  // CIR-NEXT:      cir.if %[[SUCCESS]] {
+  // CIR-NEXT:      } else {
   // CIR-NEXT:        cir.store align(4) %[[OLD]], %{{.+}} : !s32i, !cir.ptr<!s32i>
   // CIR-NEXT:      }
   // CIR-NEXT:      cir.store align(1) %[[SUCCESS]], %{{.+}} : !cir.bool, !cir.ptr<!cir.bool>
@@ -3815,7 +3831,9 @@ void atomic_cmpxchg_n_maybe_weak(int *ptr, int *expected, int desired, int failu
   // LLVM:         %[[RESULT:.+]] = cmpxchg weak ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
@@ -3827,7 +3845,9 @@ void atomic_cmpxchg_n_maybe_weak(int *ptr, int *expected, int desired, int failu
   // LLVM:         %[[RESULT:.+]] = cmpxchg ptr %{{.+}}, i32 %{{.+}}, i32 %{{.+}} seq_cst acquire, align 4
   // LLVM-NEXT:    %[[OLD:.+]] = extractvalue { i32, i1 } %[[RESULT]], 0
   // LLVM-NEXT:    %[[SUCCESS:.+]] = extractvalue { i32, i1 } %[[RESULT]], 1
-  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_CONT:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM-NEXT:    br i1 %[[SUCCESS]], label %[[LABEL_SKIP:.+]], label %[[LABEL_FAILED:.+]]
+  // LLVM:       [[LABEL_SKIP]]:
+  // LLVM-NEXT:    br label %[[LABEL_CONT:.+]]
   // LLVM:       [[LABEL_FAILED]]:
   // LLVM-NEXT:    store i32 %[[OLD]], ptr %{{.+}}, align 4
   // LLVM-NEXT:    br label %[[LABEL_CONT]]
diff --git a/clang/test/CIR/CodeGen/branch-on-not.c b/clang/test/CIR/CodeGen/branch-on-not.c
index 99afcb8750cf89..835e1af050bacf 100644
--- a/clang/test/CIR/CodeGen/branch-on-not.c
+++ b/clang/test/CIR/CodeGen/branch-on-not.c
@@ -18,15 +18,19 @@ void if_not(int *p) {
 
 // CIR-LABEL: cir.func{{.*}} @if_not(
 // CIR:         %[[B:.*]] = cir.cast ptr_to_bool %{{.*}} : !cir.ptr<!s32i> -> !cir.bool
-// CIR-NEXT:    %[[N:.*]] = cir.not %[[B]] : !cir.bool
-// CIR-NEXT:    cir.if %[[N]] {
+// CIR-NEXT:    cir.if %[[B]] {
+// CIR-NEXT:    } else {
+// CIR-NEXT:      cir.call @f()
 
 // LLVM-LABEL: define dso_local void @if_not(
 // LLVM:         %[[NONNULL:.*]] = icmp ne ptr %{{.*}}, null
-// LLVM-NEXT:    br i1 %[[NONNULL]], label %[[END:[a-z0-9.]+]], label %[[THEN:[a-z0-9.]+]]
+// LLVM-NEXT:    br i1 %[[NONNULL]], label %[[SKIP:[a-z0-9.]+]], label %[[THEN:[a-z0-9.]+]]
+// LLVMCIR:    {{^}}[[SKIP]]:
+// LLVMCIR-NEXT:  br label %[[END:[a-z0-9.]+]]
 // LLVM:       {{^}}[[THEN]]:
 // LLVM-NEXT:    call void @f()
-// LLVM-NEXT:    br label %[[END]]
+// LLVMCIR-NEXT: br label %[[END]]
+// OGCG-NEXT:    br label %[[SKIP]]
 
 void if_not_else(int *p) {
   if (!p)
@@ -36,30 +40,38 @@ void if_not_else(int *p) {
 }
 
 // CIR-LABEL: cir.func{{.*}} @if_not_else(
-// CIR:         %[[N:.*]] = cir.not %{{.*}} : !cir.bool
-// CIR-NEXT:    cir.if %[[N]] {
+// CIR:         %[[B:.*]] = cir.cast ptr_to_bool %{{.*}} : !cir.ptr<!s32i> -> !cir.bool
+// CIR-NEXT:    cir.if %[[B]] {
+// CIR-NEXT:      cir.call @h()
+// CIR-NEXT:    } else {
+// CIR-NEXT:      cir.call @f()
 
 // LLVM-LABEL: define dso_local void @if_not_else(
 // LLVM:         %[[NONNULL:.*]] = icmp ne ptr %{{.*}}, null
 // LLVM-NEXT:    br i1 %[[NONNULL]], label %[[ELSE:[a-z0-9.]+]], label %[[THEN:[a-z0-9.]+]]
+// LLVMCIR:    {{^}}[[ELSE]]:
+// LLVMCIR-NEXT:  call void @h()
 // LLVM:       {{^}}[[THEN]]:
 // LLVM-NEXT:    call void @f()
-// LLVM:       {{^}}[[ELSE]]:
-// LLVM-NEXT:    call void @h()
+// OGCG:       {{^}}[[ELSE]]:
+// OGCG-NEXT:    call void @h()
 
 int not_ternary(int a) { return !a ? t() : u(); }
 
 // CIR-LABEL: cir.func{{.*}} @not_ternary(
-// CIR:         %[[N:.*]] = cir.not %{{.*}} : !cir.bool
-// CIR-NEXT:    %{{.*}} = cir.ternary(%[[N]], true {
+// CIR:         %[[B:.*]] = cir.cast int_to_bool %{{.*}} : !s32i -> !cir.bool
+// CIR-NEXT:    %{{.*}} = cir.ternary(%[[B]], true {
+// CIR-NEXT:      %{{.*}} = cir.call @u()
 
 // LLVM-LABEL: define dso_local i32 @not_ternary(
 // LLVM:         %[[NONZERO:.*]] = icmp ne i32 %{{.*}}, 0
 // LLVM-NEXT:    br i1 %[[NONZERO]], label %[[FALSE:[a-z0-9.]+]], label %[[TRUE:[a-z0-9.]+]]
+// LLVMCIR:    {{^}}[[FALSE]]:
+// LLVMCIR-NEXT:  %{{.*}} = call i32 @u()
 // LLVM:       {{^}}[[TRUE]]:
 // LLVM-NEXT:    %{{.*}} = call i32 @t()
-// LLVM:       {{^}}[[FALSE]]:
-// LLVM-NEXT:    %{{.*}} = call i32 @u()
+// OGCG:       {{^}}[[FALSE]]:
+// OGCG-NEXT:    %{{.*}} = call i32 @u()
 
 void while_not(volatile int *done) {
   while (!*done)
@@ -86,8 +98,10 @@ int not_or(int *p, int y) {
 
 // CIR-LABEL: cir.func{{.*}} @not_or(
 // CIR:         %[[B:.*]] = cir.cast ptr_to_bool %{{.*}} : !cir.ptr<!s32i> -> !cir.bool
-// CIR-NEXT:    %[[N:.*]] = cir.not %[[B]] : !cir.bool
-// CIR-NEXT:    %{{.*}} = cir.ternary(%[[N]], true {
+// CIR-NEXT:    %{{.*}} = cir.ternary(%[[B]], true {
+// CIR:           cir.cmp eq
+// CIR:         }, false {
+// CIR-NEXT:      %{{.+}} = cir.const #true
 
 // LLVM-LABEL: define dso_local i32 @not_or(
 // LLVM:         %[[NONNULL:.*]] = icmp ne ptr %{{.*}}, null
@@ -107,14 +121,15 @@ void not_of_or(int a, int b) {
 
 // CIR-LABEL: cir.func{{.*}} @not_of_or(
 // CIR:         %[[OR:.*]] = cir.ternary(
-// CIR:         %[[N:.*]] = cir.not %[[OR]] : !cir.bool
-// CIR-NEXT:    cir.if %[[N]] {
+// CIR:         cir.if %[[OR]] {
+// CIR-NEXT:    } else {
+// CIR-NEXT:      cir.call @f()
 
 // LLVM-LABEL: define dso_local void @not_of_or(
 // LLVMCIR:      %[[OR:.*]] = phi i1
 // LLVMCIR-NOT:  xor
 // LLVMCIR:      br i1 %[[OR]], label %{{[a-z0-9.]+}}, label %[[THEN:[a-z0-9.]+]]
-// OGCG:         br i1 %{{.*}}, label %[[END:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
-// OGCG:         br i1 %{{.*}}, label %[[END]], label %[[THEN:[a-z0-9.]+]]
+// OGCG:         br i1 %{{[a-z0-9.]+}}, label %[[END:[a-z0-9.]+]], label %{{[a-z0-9.]+}}
+// OGCG:         br i1 %{{[a-z0-9.]+}}, label %[[END]], label %[[THEN:[a-z0-9.]+]]
 // LLVM:       {{^}}[[THEN]]:
 // LLVM-NEXT:    call void @f()
diff --git a/clang/test/CIR/CodeGen/nrvo.cpp b/clang/test/CIR/CodeGen/nrvo.cpp
index 4564504a2a1185..90744c89acedfd 100644
--- a/clang/test/CIR/CodeGen/nrvo.cpp
+++ b/clang/test/CIR/CodeGen/nrvo.cpp
@@ -72,9 +72,9 @@ NonTrivial test_nrvo() {
 // CIR:     cir.return
 // CIR:   } cleanup normal {
 // CIR:     %[[NRVO_FLAG_VAL:.*]] = cir.load{{.*}} %[[NRVO_FLAG]]
-// CIR:     %[[NOT_NRVO_VAL:.*]] = cir.not %[[NRVO_FLAG_VAL]]
-// CIR:     cir.if %[[NOT_NRVO_VAL]] {
-// CIR:       cir.call @_ZN10NonTrivialD1Ev(%[[RESULT]])
+// CIR-NEXT:     cir.if %[[NRVO_FLAG_VAL]] {
+// CIR-NEXT:     } else {
+// CIR-NEXT:       cir.call @_ZN10NonTrivialD1Ev(%[[RESULT]])
 // CIR:     }
 // CIR:     cir.yield
 // CIR:   }
@@ -96,9 +96,13 @@ NonTrivial test_nrvo() {
 // LLVMCIR:   %[[NRVO_BYTE:.*]] = load i8, ptr %[[NRVO_FLAG]]
 // LLVMCIR:   %[[NRVO_VAL:.*]] = trunc i8 %[[NRVO_BYTE]] to i1
 // OGCG:   %[[NRVO_VAL:.*]] = load i1, ptr %[[NRVO_FLAG]]
-// LLVM-NEXT:   br i1 %[[NRVO_VAL]], label %[[NRVO_DONE:.*]], label %[[NRVO_UNUSED:.*]]
-// LLVM: [[NRVO_UNUSED]]:
+// LLVM-NEXT:   br i1 %[[NRVO_VAL]], label %[[NRVO_SKIP:[a-z0-9.]+]], label %[[NRVO_UNUSED:[a-z0-9.]+]]
+// LLVMCIR: {{^}}[[NRVO_SKIP]]:
+// LLVMCIR-NEXT:   br label %[[NRVO_DONE:[a-z0-9.]+]]
+// LLVM: {{^}}[[NRVO_UNUSED]]:
 // LLVM-NEXT:   call void @_ZN10NonTrivialD1Ev(ptr noundef nonnull align 1 dereferenceable(1) %[[RESULT]])
-// LLVM-NEXT:   br label %[[NRVO_DONE]]
-// LLVM: [[NRVO_DONE]]:
+// LLVMCIR-NEXT:   br label %[[NRVO_DONE]]
+// LLVMCIR: {{^}}[[NRVO_DONE]]:
+// OGCG-NEXT:   br label %[[NRVO_SKIP]]
+// OGCG: {{^}}[[NRVO_SKIP]]:
 // LLVM:   ret void
diff --git a/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir b/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir
index 58648f89f44e43..93b3f991885620 100644
--- a/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir
+++ b/clang/test/CIR/Transforms/flatten-cleanup-scope-eh.cir
@@ -277,7 +277,8 @@ cir.func @test_nrvo() -> !rec_NonTrivial {
 // Normal cleanup: inlined cleanup region with flattened cir.if.
 // CHECK:       ^[[NORMAL_CLEANUP]]:
 // CHECK:         %[[N_FLAG:.*]] = cir.load align(1) %[[NRVO]] : !cir.ptr<!cir.bool>, !cir.bool
-// CHECK-NEXT:    cir.brcond %[[N_FLAG]] ^[[N_MERGE:bb[0-9]+]], ^[[N_IF_TRUE:bb[0-9]+]]
+// CHECK:         %[[N_NOT:.*]] = cir.not %[[N_FLAG]] : !cir.bool
+// CHECK:         cir.brcond %[[N_NOT]] ^[[N_IF_TRUE:bb[0-9]+]], ^[[N_MERGE:bb[0-9]+]]
 //
 // CHECK:       ^[[N_IF_TRUE]]:
 // CHECK:         cir.call @_ZN10NonTrivialD1Ev(%[[RETVAL]]) nothrow
@@ -298,7 +299,8 @@ cir.func @test_nrvo() -> !rec_NonTrivial {
 // CHECK:       ^[[EH_CLEANUP]](%[[ET:.*]]: !cir.eh_token):
 // CHECK:         %[[CT:.*]] = cir.begin_cleanup %[[ET]] : !cir.eh_token -> !cir.cleanup_token
 // CHECK:         %[[EH_FLAG:.*]] = cir.load align(1) %[[NRVO]] : !cir.ptr<!cir.bool>, !cir.bool
-// CHECK-NEXT:    cir.brcond %[[EH_FLAG]] ^[[EH_MERGE:bb[0-9]+]], ^[[EH_IF_TRUE:bb[0-9]+]]
+// CHECK:         %[[EH_NOT:.*]] = cir.not %[[EH_FLAG]] : !cir.bool
+// CHECK:         cir.brcond %[[EH_NOT]] ^[[EH_IF_TRUE:bb[0-9]+]], ^[[EH_MERGE:bb[0-9]+]]
 //
 // EH cleanup if-true: call destructor.
 // CHECK:       ^[[EH_IF_TRUE]]:
diff --git a/clang/test/CIR/Transforms/flatten-brcond-not.cir b/clang/test/CIR/Transforms/lowering-prepare-not-cond.cir
similarity index 50%
rename from clang/test/CIR/Transforms/flatten-brcond-not.cir
rename to clang/test/CIR/Transforms/lowering-prepare-not-cond.cir
index c681e6b9a47ed6..012a9bc96c6dd2 100644
--- a/clang/test/CIR/Transforms/flatten-brcond-not.cir
+++ b/clang/test/CIR/Transforms/lowering-prepare-not-cond.cir
@@ -1,9 +1,9 @@
-// RUN: cir-opt %s -cir-flatten-cfg -o %t.cir
+// RUN: cir-opt %s --cir-lowering-prepare -o %t.cir
 // RUN: FileCheck --input-file=%t.cir %s
 
 !s32i = !cir.int<s, 32>
 
-module {
+module attributes {cir.triple = "x86_64-unknown-linux-gnu"} {
   cir.func private @onTrue()
   cir.func private @onFalse()
   cir.func private @body()
@@ -20,12 +20,11 @@ module {
   }
 
   // CHECK-LABEL: cir.func @if_not(%arg0: !cir.bool)
-  // CHECK-NOT:     cir.not
-  // CHECK:         cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.call @onTrue()
-  // CHECK:       ^[[FALSE]]:
-  // CHECK-NEXT:    cir.call @onFalse()
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onFalse()
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onTrue()
+  // CHECK-NEXT:    }
 
   cir.func @if_not_no_else(%arg0: !cir.bool) {
     %0 = cir.not %arg0 : !cir.bool
@@ -36,12 +35,10 @@ module {
   }
 
   // CHECK-LABEL: cir.func @if_not_no_else(%arg0: !cir.bool)
-  // CHECK-NOT:     cir.not
-  // CHECK:         cir.brcond %arg0 ^[[CONT:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.call @onTrue()
-  // CHECK:       ^[[CONT]]:
-  // CHECK-NEXT:    cir.return
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onTrue()
+  // CHECK-NEXT:    }
 
   cir.func @ternary_not(%arg0: !cir.bool) {
     %0 = cir.not %arg0 : !cir.bool
@@ -56,12 +53,11 @@ module {
   }
 
   // CHECK-LABEL: cir.func @ternary_not(%arg0: !cir.bool)
-  // CHECK-NOT:     cir.not
-  // CHECK:         cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.call @onTrue()
-  // CHECK:       ^[[FALSE]]:
-  // CHECK-NEXT:    cir.call @onFalse()
+  // CHECK-NEXT:    cir.ternary(%arg0, true {
+  // CHECK-NEXT:      cir.call @onFalse()
+  // CHECK-NEXT:      cir.yield
+  // CHECK-NEXT:    }, false {
+  // CHECK-NEXT:      cir.call @onTrue()
 
   cir.func @ternary_not_value(%arg0: !cir.bool, %arg1: !s32i, %arg2: !s32i) -> !s32i {
     %0 = cir.not %arg0 : !cir.bool
@@ -74,37 +70,46 @@ module {
   }
 
   // CHECK-LABEL: cir.func @ternary_not_value(%arg0: !cir.bool, %arg1: !s32i, %arg2: !s32i) -> !s32i
-  // CHECK-NOT:     cir.not
-  // CHECK:         cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.br ^[[CONT:bb[0-9]+]](%arg1 : !s32i)
-  // CHECK:       ^[[FALSE]]:
-  // CHECK-NEXT:    cir.br ^[[CONT]](%arg2 : !s32i)
-  // CHECK:       ^[[CONT]](%[[RES:.*]]: !s32i):
-  // CHECK:         cir.return %[[RES]] : !s32i
-
-  cir.func @while_not() {
-    cir.while {
-      %0 = cir.call @cond() : () -> !cir.bool
-      %1 = cir.not %0 : !cir.bool
-      cir.condition(%1)
-    } do {
-      cir.call @body() : () -> ()
-      cir.yield
+  // CHECK-NEXT:    %[[RES:.*]] = cir.ternary(%arg0, true {
+  // CHECK-NEXT:      cir.yield %arg2 : !s32i
+  // CHECK-NEXT:    }, false {
+  // CHECK-NEXT:      cir.yield %arg1 : !s32i
+  // CHECK-NEXT:    }) : (!cir.bool) -> !s32i
+  // CHECK-NEXT:    cir.return %[[RES]] : !s32i
+
+  cir.func @if_double_not_no_else(%arg0: !cir.bool) {
+    %0 = cir.not %arg0 : !cir.bool
+    %1 = cir.not %0 : !cir.bool
+    cir.if %1 {
+      cir.call @onTrue() : () -> ()
     }
     cir.return
   }
 
-  // The cir.not feeding a loop condition is not folded into the branch.
-  // CHECK-LABEL: cir.func @while_not()
-  // CHECK:         %[[C:.*]] = cir.call @cond()
-  // CHECK-NEXT:    %[[N:.*]] = cir.not %[[C]] : !cir.bool
-  // CHECK-NEXT:    cir.brcond %[[N]] ^[[BODY:bb[0-9]+]], ^[[EXIT:bb[0-9]+]]
-  // CHECK:       ^[[BODY]]:
-  // CHECK-NEXT:    cir.call @body()
-  // CHECK:       ^[[EXIT]]:
+  // CHECK-LABEL: cir.func @if_double_not_no_else(%arg0: !cir.bool)
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onTrue()
+  // CHECK-NEXT:    }
   // CHECK-NEXT:    cir.return
 
+  cir.func @if_triple_not(%arg0: !cir.bool) {
+    %0 = cir.not %arg0 : !cir.bool
+    %1 = cir.not %0 : !cir.bool
+    %2 = cir.not %1 : !cir.bool
+    cir.if %2 {
+      cir.call @onTrue() : () -> ()
+    } else {
+      cir.call @onFalse() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_triple_not(%arg0: !cir.bool)
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onFalse()
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onTrue()
+
   cir.func @if_double_not(%arg0: !cir.bool) {
     %0 = cir.not %arg0 : !cir.bool
     %1 = cir.not %0 : !cir.bool
@@ -117,33 +122,33 @@ module {
   }
 
   // CHECK-LABEL: cir.func @if_double_not(%arg0: !cir.bool)
-  // CHECK-NOT:     cir.not
-  // CHECK:         cir.brcond %arg0 ^[[TRUE:bb[0-9]+]], ^[[FALSE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.call @onTrue()
-  // CHECK:       ^[[FALSE]]:
-  // CHECK-NEXT:    cir.call @onFalse()
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onTrue()
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onFalse()
 
-  cir.func @if_double_not_inner_used(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>) {
+  cir.func @two_ifs_share_not(%arg0: !cir.bool) {
     %0 = cir.not %arg0 : !cir.bool
-    cir.store %0, %arg1 : !cir.bool, !cir.ptr<!cir.bool>
-    %1 = cir.not %0 : !cir.bool
-    cir.if %1 {
+    cir.if %0 {
       cir.call @onTrue() : () -> ()
     } else {
       cir.call @onFalse() : () -> ()
     }
+    cir.if %0 {
+      cir.call @body() : () -> ()
+    }
     cir.return
   }
 
-  // CHECK-LABEL: cir.func @if_double_not_inner_used(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>)
-  // CHECK-NEXT:    %[[N:.*]] = cir.not %arg0 : !cir.bool
-  // CHECK-NEXT:    cir.store %[[N]], %arg1
-  // CHECK-NEXT:    cir.brcond %arg0 ^[[TRUE:bb[0-9]+]], ^[[FALSE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.call @onTrue()
-  // CHECK:       ^[[FALSE]]:
-  // CHECK-NEXT:    cir.call @onFalse()
+  // CHECK-LABEL: cir.func @two_ifs_share_not(%arg0: !cir.bool)
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onFalse()
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onTrue()
+  // CHECK-NEXT:    }
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @body()
 
   cir.func @if_not_other_use(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>) {
     %0 = cir.not %arg0 : !cir.bool
@@ -159,9 +164,46 @@ module {
   // CHECK-LABEL: cir.func @if_not_other_use(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>)
   // CHECK-NEXT:    %[[N:.*]] = cir.not %arg0 : !cir.bool
   // CHECK-NEXT:    cir.store %[[N]], %arg1
-  // CHECK-NEXT:    cir.brcond %arg0 ^[[FALSE:bb[0-9]+]], ^[[TRUE:bb[0-9]+]]
-  // CHECK:       ^[[TRUE]]:
-  // CHECK-NEXT:    cir.call @onTrue()
-  // CHECK:       ^[[FALSE]]:
-  // CHECK-NEXT:    cir.call @onFalse()
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onFalse()
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onTrue()
+
+  cir.func @if_double_not_inner_used(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>) {
+    %0 = cir.not %arg0 : !cir.bool
+    cir.store %0, %arg1 : !cir.bool, !cir.ptr<!cir.bool>
+    %1 = cir.not %0 : !cir.bool
+    cir.if %1 {
+      cir.call @onTrue() : () -> ()
+    } else {
+      cir.call @onFalse() : () -> ()
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @if_double_not_inner_used(%arg0: !cir.bool, %arg1: !cir.ptr<!cir.bool>)
+  // CHECK-NEXT:    %[[N:.*]] = cir.not %arg0 : !cir.bool
+  // CHECK-NEXT:    cir.store %[[N]], %arg1
+  // CHECK-NEXT:    cir.if %arg0 {
+  // CHECK-NEXT:      cir.call @onTrue()
+  // CHECK-NEXT:    } else {
+  // CHECK-NEXT:      cir.call @onFalse()
+
+  cir.func @while_not() {
+    cir.while {
+      %0 = cir.call @cond() : () -> !cir.bool
+      %1 = cir.not %0 : !cir.bool
+      cir.condition(%1)
+    } do {
+      cir.call @body() : () -> ()
+      cir.yield
+    }
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @while_not()
+  // CHECK-NEXT:    cir.while {
+  // CHECK-NEXT:      %[[C:.*]] = cir.call @cond()
+  // CHECK-NEXT:      %[[N:.*]] = cir.not %[[C]] : !cir.bool
+  // CHECK-NEXT:      cir.condition(%[[N]])
 }



More information about the cfe-commits mailing list