[clang] [CIR] Upstream lowering of conditional operators to TernaryOp (PR #138156)
Morris Hafner via cfe-commits
cfe-commits at lists.llvm.org
Mon May 19 17:53:42 PDT 2025
================
@@ -948,6 +950,165 @@ void CIRGenFunction::emitIgnoredExpr(const Expr *e) {
emitLValue(e);
}
+// Handle the case where the condition is a constant evaluatable simple integer,
+// which means we don't have to separately handle the true/false blocks.
+static std::optional<LValue> handleConditionalOperatorLValueSimpleCase(
+ CIRGenFunction &cgf, const AbstractConditionalOperator *e) {
+ const Expr *condExpr = e->getCond();
+ bool condExprBool;
+ if (cgf.constantFoldsToSimpleInteger(condExpr, condExprBool)) {
+ const Expr *live = e->getTrueExpr(), *dead = e->getFalseExpr();
+ if (!condExprBool)
+ std::swap(live, dead);
+
+ if (!cgf.containsLabel(dead)) {
+ // If the true case is live, we need to track its region.
+ if (condExprBool) {
----------------
mmha wrote:
I don't know much about PGO, but from what I gather this code is correct. Clang/LLVM optimize branch coverage by only emitting counters for true branches. The false branch count is then calculated with `parent_count - true_count` See slide 12 [here](https://llvm.org/devmtg/2020-09/slides/PhippsAlan_BranchCoverage_LLVM_Conf_Talk_final.pdf). It the condition is always false there's no counter to increment.
https://github.com/llvm/llvm-project/pull/138156
More information about the cfe-commits
mailing list