[clang] [CIR] Upstream lowering of conditional operators to TernaryOp (PR #138156)

Morris Hafner via cfe-commits cfe-commits at lists.llvm.org
Tue May 20 08:49:47 PDT 2025


================
@@ -1658,6 +1828,170 @@ mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
                cgf.cgm.UInt64Ty, e->EvaluateKnownConstInt(cgf.getContext())));
 }
 
+/// Return true if the specified expression is cheap enough and side-effect-free
+/// enough to evaluate unconditionally instead of conditionally.  This is used
+/// to convert control flow into selects in some cases.
+/// TODO(cir): can be shared with LLVM codegen.
+static bool isCheapEnoughToEvaluateUnconditionally(const Expr *e,
+                                                   CIRGenFunction &cgf) {
+  // Anything that is an integer or floating point constant is fine.
+  return e->IgnoreParens()->isEvaluatable(cgf.getContext());
----------------
mmha wrote:

FWIW even if `isCheapEnoughToEvaluateUnconditionally` returns true we still emit a `TernaryOp` instead of a `SelectOp`. This needs to be fixed here and in the incubator.

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


More information about the cfe-commits mailing list