https://github.com/bcardosolopes created https://github.com/llvm/llvm-project/pull/241645
…gions CastOp::fold now folds constant `int_to_bool` and `bool_to_int` casts, so canonicalize turns them into a `cir.const #true`/`#false` or the matching integer constant. The folder reads its constant operand from the fold adaptor instead of looking for a defining `cir.const`, so it also works on attributes a caller supplies, as SCCP does. `cir.ternary` implements `getEntrySuccessorRegions`: a constant condition enters only one region, which lets SCCP and dead code analysis see through a constant ternary. To declare it, the region-branch base classes take an optional list of extra `RegionBranchOpInterface` methods. Besides general simplification, it also remove the need for custom folding in https://github.com/llvm/llvm-project/pull/239774 >From 3f6a4798a1f97d8887c4eb267246786c0bb072e6 Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes <[email protected]> Date: Wed, 11 Nov 2026 14:55:53 -0800 Subject: [PATCH] [CIR] Fold constant int/bool casts and narrow a constant ternary's regions CastOp::fold now folds constant `int_to_bool` and `bool_to_int` casts, so canonicalize turns them into a `cir.const #true`/`#false` or the matching integer constant. The folder reads its constant operand from the fold adaptor instead of looking for a defining `cir.const`, so it also works on attributes a caller supplies, as SCCP does. `cir.ternary` implements `getEntrySuccessorRegions`: a constant condition enters only one region, which lets SCCP and dead code analysis see through a constant ternary. To declare it, the region-branch base classes take an optional list of extra `RegionBranchOpInterface` methods. CodeGen tests that printed a constant followed by an `int_to_bool` cast now check the folded boolean constant. Assisted-by: Claude Code (Opus 5.5). --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 16 +++++---- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 23 +++++++++++-- clang/test/CIR/CodeGen/if.cpp | 6 ++-- clang/test/CIR/CodeGen/loop.cpp | 3 +- .../CIR/CodeGenCoroutines/coro-builtins.cpp | 3 +- clang/test/CIR/CodeGenOpenACC/combined.cpp | 3 +- clang/test/CIR/CodeGenOpenACC/data.c | 3 +- clang/test/CIR/CodeGenOpenACC/init.c | 3 +- clang/test/CIR/CodeGenOpenACC/kernels.c | 9 ++--- clang/test/CIR/CodeGenOpenACC/parallel.c | 9 ++--- clang/test/CIR/CodeGenOpenACC/serial.c | 9 ++--- clang/test/CIR/CodeGenOpenACC/shutdown.c | 3 +- clang/test/CIR/CodeGenOpenMP/parallel.c | 4 +-- clang/test/CIR/Transforms/canonicalize.cir | 18 ++++++++++ clang/test/CIR/Transforms/ternary-sccp.cir | 34 +++++++++++++++++++ 15 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 clang/test/CIR/Transforms/ternary-sccp.cir diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 8823b4fe0e5604..95c4a5b14f8a7a 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -136,11 +136,14 @@ class CIR_Op<string mnemonic, list<Trait> traits = []> : // Base class for structured control flow ops whose regions take no inputs and // yield the parent's results on the way out. Users still have to define -// `getSuccessorRegions` themselves. -class CIR_RegionBranchOpBase<string mnemonic, list<Trait> traits = []> +// `getSuccessorRegions` themselves, and the optional `RegionBranchOpInterface` +// methods they list in `regionBranchMethods`. +class CIR_RegionBranchOpBase<string mnemonic, list<Trait> traits = [], + list<string> regionBranchMethods = []> : CIR_Op<mnemonic, !listconcat([ DeclareOpInterfaceMethods<RegionBranchOpInterface, - ["getSuccessorInputs"]>], traits)> { + !listconcat(["getSuccessorInputs"], regionBranchMethods)>], + traits)> { let extraClassDefinition = [{ ValueRange $cppClass::getSuccessorInputs( mlir::RegionSuccessor successor) { @@ -155,8 +158,9 @@ class CIR_RegionBranchOpBase<string mnemonic, list<Trait> traits = []> // Base class for the region-branch ops whose successors do not depend on the // operation: any of their regions may be entered from the parent operation, and // every region returns to it on exit. -class CIR_EnterAnyRegionBranchOpBase<string mnemonic, list<Trait> traits = []> - : CIR_RegionBranchOpBase<mnemonic, traits> { +class CIR_EnterAnyRegionBranchOpBase<string mnemonic, list<Trait> traits = [], + list<string> regionBranchMethods = []> + : CIR_RegionBranchOpBase<mnemonic, traits, regionBranchMethods> { let append extraClassDefinition = [{ void $cppClass::getSuccessorRegions( mlir::RegionBranchPoint point, @@ -3405,7 +3409,7 @@ def CIR_SelectOp : CIR_Op<"select", [ def CIR_TernaryOp : CIR_EnterAnyRegionBranchOpBase<"ternary", [ RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments -]> { +], ["getEntrySuccessorRegions"]> { let summary = "The `cond ? a : b` C/C++ ternary operation"; let description = [{ The `cir.ternary` operation represents C/C++ ternary, much like a `select` diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 83d1adb9605a60..f81f61b6e11f3f 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -1340,7 +1340,7 @@ OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) { return result; // Handle simple constant casts. - if (auto srcConst = getSrc().getDefiningOp<cir::ConstantOp>()) { + if (mlir::Attribute src = adaptor.getSrc()) { switch (getKind()) { case cir::CastKind::integral: { mlir::Type srcTy = getSrc().getType(); @@ -1352,7 +1352,7 @@ OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) { auto srcIntTy = mlir::cast<cir::IntType>(srcTy); auto dstIntTy = mlir::cast<cir::IntType>(getType()); - auto constIntAttr = srcConst.getValueAttr<cir::IntAttr>(); + auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(src); if (!constIntAttr) return {}; @@ -1362,6 +1362,14 @@ OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) { : srcValue.zextOrTrunc(dstIntTy.getWidth()); return cir::IntAttr::get(dstIntTy, newVal); } + case cir::CastKind::int_to_bool: + if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(src)) + return cir::BoolAttr::get(getContext(), !intAttr.getValue().isZero()); + break; + case cir::CastKind::bool_to_int: + if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(src)) + return cir::IntAttr::get(getType(), boolAttr.getValue() ? 1 : 0); + break; default: break; } @@ -3541,6 +3549,17 @@ void cir::TernaryOp::build( result.addTypes(TypeRange{yield.getOperandTypes().front()}); } +void cir::TernaryOp::getEntrySuccessorRegions( + ArrayRef<Attribute> operands, SmallVectorImpl<RegionSuccessor> ®ions) { + // A constant condition selects one region. + FoldAdaptor adaptor(operands, *this); + auto cond = mlir::dyn_cast_if_present<cir::BoolAttr>(adaptor.getCond()); + if (!cond || cond.getValue()) + regions.emplace_back(&getTrueRegion()); + if (!cond || !cond.getValue()) + regions.emplace_back(&getFalseRegion()); +} + //===----------------------------------------------------------------------===// // SelectOp //===----------------------------------------------------------------------===// diff --git a/clang/test/CIR/CodeGen/if.cpp b/clang/test/CIR/CodeGen/if.cpp index 143ddcc33d2fce..fa4eaa3d16ce1e 100644 --- a/clang/test/CIR/CodeGen/if.cpp +++ b/clang/test/CIR/CodeGen/if.cpp @@ -322,8 +322,7 @@ void if3() { } goto foo; } // CIR: cir.func{{.*}} @_Z3if3v() -// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i -// CIR: %[[ZERO_BOOL:.*]] = cir.cast int_to_bool %[[ZERO]] : !s32i -> !cir.bool +// CIR: %[[ZERO_BOOL:.*]] = cir.const #false // CIR: cir.if %[[ZERO_BOOL]] // CIR: cir.call @_Z7if_initv() : () -> (!s32i {llvm.noundef}) @@ -349,8 +348,7 @@ void if4(int a) { // CIR: cir.switch(%[[LOAD_ARG]] : !s32i) { // CIR: cir.case(equal, [#cir.int<0> : !s32i]) { // CIR: cir.scope { -// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i -// CIR: %[[ZERO_BOOL:.*]] = cir.cast int_to_bool %[[ZERO]] : !s32i -> !cir.bool +// CIR: %[[ZERO_BOOL:.*]] = cir.const #false // CIR: cir.if %[[ZERO_BOOL]] // CIR: cir.call @_Z3if0b( diff --git a/clang/test/CIR/CodeGen/loop.cpp b/clang/test/CIR/CodeGen/loop.cpp index 89e5f69273044e..c49b0ddcb473aa 100644 --- a/clang/test/CIR/CodeGen/loop.cpp +++ b/clang/test/CIR/CodeGen/loop.cpp @@ -411,8 +411,7 @@ void test_do_while_false() { // CIR-NEXT: cir.do { // CIR-NEXT: cir.yield // CIR-NEXT: } while { -// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i -// CIR-NEXT: %[[FALSE:.*]] = cir.cast int_to_bool %[[ZERO]] : !s32i -> !cir.bool +// CIR-NEXT: %[[FALSE:.*]] = cir.const #false // CIR-NEXT: cir.condition(%[[FALSE]]) // LLVM: define{{.*}} void @_Z19test_do_while_falsev(){{.*}} diff --git a/clang/test/CIR/CodeGenCoroutines/coro-builtins.cpp b/clang/test/CIR/CodeGenCoroutines/coro-builtins.cpp index 28e015f8e01c34..1d377bfdb573ce 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-builtins.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-builtins.cpp @@ -76,8 +76,7 @@ void f(int n) { __builtin_coro_promise(__builtin_coro_frame(), 48, 0); // CIR: %[[ALIGN:.*]] = cir.const #cir.int<48> : !s32i - // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i - // CIR: %[[FALSE:.*]] = cir.cast int_to_bool %[[ZERO]] : !s32i -> !cir.bool + // CIR: %[[FALSE:.*]] = cir.const #false // CIR: cir.coro.intrinsic.promise(%[[FRAME]], %[[ALIGN]], %[[FALSE]]) // LLVM: call ptr @llvm.coro.promise(ptr %[[FRAME]], i32 48, i1 false) diff --git a/clang/test/CIR/CodeGenOpenACC/combined.cpp b/clang/test/CIR/CodeGenOpenACC/combined.cpp index b6ba08993a5836..adb98a2b287a5e 100644 --- a/clang/test/CIR/CodeGenOpenACC/combined.cpp +++ b/clang/test/CIR/CodeGenOpenACC/combined.cpp @@ -214,8 +214,7 @@ extern "C" void acc_combined(int N, int cond) { #pragma acc serial loop if(1) for(unsigned I = 0; I < N; ++I); - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.serial combined(loop) if(%[[CONV_CAST]]) { // CHECK-NEXT: acc.loop combined(serial) { diff --git a/clang/test/CIR/CodeGenOpenACC/data.c b/clang/test/CIR/CodeGenOpenACC/data.c index 2e7b08f0551637..336225ec52f772 100644 --- a/clang/test/CIR/CodeGenOpenACC/data.c +++ b/clang/test/CIR/CodeGenOpenACC/data.c @@ -95,8 +95,7 @@ void acc_data(int cond) { #pragma acc data default(none) if(1) {} - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.data if(%[[CONV_CAST]]) { // CHECK-NEXT: acc.terminator diff --git a/clang/test/CIR/CodeGenOpenACC/init.c b/clang/test/CIR/CodeGenOpenACC/init.c index 7c959acd74f0c8..c999ffcfe6f5b3 100644 --- a/clang/test/CIR/CodeGenOpenACC/init.c +++ b/clang/test/CIR/CodeGenOpenACC/init.c @@ -23,8 +23,7 @@ void acc_init(int cond) { // CHECK-NEXT: acc.init if(%[[BOOL_CONV]]) #pragma acc init if(1) - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[ONE_TO_BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[ONE_TO_BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[BOOL_CONV:.*]] = builtin.unrealized_conversion_cast %[[ONE_TO_BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.init if(%[[BOOL_CONV]]) diff --git a/clang/test/CIR/CodeGenOpenACC/kernels.c b/clang/test/CIR/CodeGenOpenACC/kernels.c index 2111643cb9b8f3..35969324db18a8 100644 --- a/clang/test/CIR/CodeGenOpenACC/kernels.c +++ b/clang/test/CIR/CodeGenOpenACC/kernels.c @@ -28,8 +28,7 @@ void acc_kernels(int cond) { // CHECK-NEXT: acc.kernels { // CHECK-NEXT: cir.scope { // CHECK-NEXT: cir.while { - // CHECK-NEXT: %[[INT:.*]] = cir.const #cir.int<1> - // CHECK-NEXT: %[[CAST:.*]] = cir.cast int_to_bool %[[INT]] + // CHECK-NEXT: %[[CAST:.*]] = cir.const #true // CHECK-NEXT: cir.condition(%[[CAST]]) // CHECK-NEXT: } do { // CHECK-NEXT: cir.yield @@ -57,8 +56,7 @@ void acc_kernels(int cond) { #pragma acc kernels self(0) {} - // CHECK-NEXT: %[[ZERO_LITERAL:.*]] = cir.const #cir.int<0> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ZERO_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #false // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.kernels self(%[[CONV_CAST]]) { // CHECK-NEXT: acc.terminator @@ -75,8 +73,7 @@ void acc_kernels(int cond) { #pragma acc kernels if(1) {} - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.kernels if(%[[CONV_CAST]]) { // CHECK-NEXT: acc.terminator diff --git a/clang/test/CIR/CodeGenOpenACC/parallel.c b/clang/test/CIR/CodeGenOpenACC/parallel.c index 28c9f5127b9890..9a859ed424b1a2 100644 --- a/clang/test/CIR/CodeGenOpenACC/parallel.c +++ b/clang/test/CIR/CodeGenOpenACC/parallel.c @@ -27,8 +27,7 @@ void acc_parallel(int cond) { // CHECK-NEXT: acc.parallel { // CHECK-NEXT: cir.scope { // CHECK-NEXT: cir.while { - // CHECK-NEXT: %[[INT:.*]] = cir.const #cir.int<1> - // CHECK-NEXT: %[[CAST:.*]] = cir.cast int_to_bool %[[INT]] + // CHECK-NEXT: %[[CAST:.*]] = cir.const #true // CHECK-NEXT: cir.condition(%[[CAST]]) // CHECK-NEXT: } do { // CHECK-NEXT: cir.yield @@ -56,8 +55,7 @@ void acc_parallel(int cond) { #pragma acc parallel self(0) {} - // CHECK-NEXT: %[[ZERO_LITERAL:.*]] = cir.const #cir.int<0> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ZERO_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #false // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.parallel self(%[[CONV_CAST]]) { // CHECK-NEXT: acc.yield @@ -74,8 +72,7 @@ void acc_parallel(int cond) { #pragma acc parallel if(1) {} - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.parallel if(%[[CONV_CAST]]) { // CHECK-NEXT: acc.yield diff --git a/clang/test/CIR/CodeGenOpenACC/serial.c b/clang/test/CIR/CodeGenOpenACC/serial.c index e3098f63f242ae..cf43439d3acb4f 100644 --- a/clang/test/CIR/CodeGenOpenACC/serial.c +++ b/clang/test/CIR/CodeGenOpenACC/serial.c @@ -28,8 +28,7 @@ void acc_serial(int cond) { // CHECK-NEXT: acc.serial { // CHECK-NEXT: cir.scope { // CHECK-NEXT: cir.while { - // CHECK-NEXT: %[[INT:.*]] = cir.const #cir.int<1> - // CHECK-NEXT: %[[CAST:.*]] = cir.cast int_to_bool %[[INT]] + // CHECK-NEXT: %[[CAST:.*]] = cir.const #true // CHECK-NEXT: cir.condition(%[[CAST]]) // CHECK-NEXT: } do { // CHECK-NEXT: cir.yield @@ -57,8 +56,7 @@ void acc_serial(int cond) { #pragma acc serial self(0) {} - // CHECK-NEXT: %[[ZERO_LITERAL:.*]] = cir.const #cir.int<0> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ZERO_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #false // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.serial self(%[[CONV_CAST]]) { // CHECK-NEXT: acc.yield @@ -75,8 +73,7 @@ void acc_serial(int cond) { #pragma acc serial if(1) {} - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[CONV_CAST:.*]] = builtin.unrealized_conversion_cast %[[BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.serial if(%[[CONV_CAST]]) { // CHECK-NEXT: acc.yield diff --git a/clang/test/CIR/CodeGenOpenACC/shutdown.c b/clang/test/CIR/CodeGenOpenACC/shutdown.c index 53ec4976e34dee..8a5c57045b44d6 100644 --- a/clang/test/CIR/CodeGenOpenACC/shutdown.c +++ b/clang/test/CIR/CodeGenOpenACC/shutdown.c @@ -23,8 +23,7 @@ void acc_shutdown(int cond) { // CHECK-NEXT: acc.shutdown if(%[[BOOL_CONV]]) #pragma acc shutdown if(1) - // CHECK-NEXT: %[[ONE_LITERAL:.*]] = cir.const #cir.int<1> : !s32i - // CHECK-NEXT: %[[ONE_TO_BOOL_CAST:.*]] = cir.cast int_to_bool %[[ONE_LITERAL]] : !s32i -> !cir.bool + // CHECK-NEXT: %[[ONE_TO_BOOL_CAST:.*]] = cir.const #true // CHECK-NEXT: %[[BOOL_CONV:.*]] = builtin.unrealized_conversion_cast %[[ONE_TO_BOOL_CAST]] : !cir.bool to i1 // CHECK-NEXT: acc.shutdown if(%[[BOOL_CONV]]) diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c index 6a9a349c59489f..678a4d9491529b 100644 --- a/clang/test/CIR/CodeGenOpenMP/parallel.c +++ b/clang/test/CIR/CodeGenOpenMP/parallel.c @@ -95,9 +95,7 @@ void if_parallel() { #pragma omp parallel if (1) {} - // CHECK: %[[ONE_CONST:.*]] = cir.const #cir.int<1> - // CHECK-NEXT: %[[ONE_BOOL:.*]] = cir.cast int_to_bool %[[ONE_CONST]] - // CHECK-NEXT: %[[ONE_U1:.*]] = cir.cast bool_to_int %[[ONE_BOOL]] + // CHECK: %[[ONE_U1:.*]] = cir.const #cir.int<1> : !cir.int<u, 1> // CHECK-NEXT: %[[ONE_I1:.*]] = cir.builtin_int_cast %[[ONE_U1]] // CHECK-NEXT: omp.parallel if(%[[ONE_I1]]) { // CHECK-NEXT: omp.terminator diff --git a/clang/test/CIR/Transforms/canonicalize.cir b/clang/test/CIR/Transforms/canonicalize.cir index a65df32833f196..8f7a5da3083c03 100644 --- a/clang/test/CIR/Transforms/canonicalize.cir +++ b/clang/test/CIR/Transforms/canonicalize.cir @@ -204,6 +204,24 @@ module { // CHECK-NEXT: %[[CAST:.*]] = cir.cast int_to_bool %[[ARG0]] : !s32i -> !cir.bool // CHECK-NEXT: cir.return %[[CAST]] : !cir.bool + cir.func @cast_const_int_to_bool() -> !cir.bool { + %0 = cir.const #cir.int<2> : !s32i + %1 = cir.cast int_to_bool %0 : !s32i -> !cir.bool + cir.return %1 : !cir.bool + } + // CHECK: cir.func{{.*}} @cast_const_int_to_bool() -> !cir.bool + // CHECK-NEXT: %[[TRUE:.*]] = cir.const #true + // CHECK-NEXT: cir.return %[[TRUE]] : !cir.bool + + cir.func @cast_const_bool_to_int() -> !s32i { + %0 = cir.const #false + %1 = cir.cast bool_to_int %0 : !cir.bool -> !s32i + cir.return %1 : !s32i + } + // CHECK: cir.func{{.*}} @cast_const_bool_to_int() -> !s32i + // CHECK-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i + // CHECK-NEXT: cir.return %[[ZERO]] : !s32i + cir.func @no_fold_cast(%arg0: !s32i) -> !s64i { %0 = cir.cast int_to_bool %arg0 : !s32i -> !cir.bool %1 = cir.cast bool_to_int %0 : !cir.bool -> !s32i diff --git a/clang/test/CIR/Transforms/ternary-sccp.cir b/clang/test/CIR/Transforms/ternary-sccp.cir new file mode 100644 index 00000000000000..46c11c4c9cfc51 --- /dev/null +++ b/clang/test/CIR/Transforms/ternary-sccp.cir @@ -0,0 +1,34 @@ +// RUN: cir-opt %s --sccp | FileCheck %s + +!s32i = !cir.int<s, 32> + +module { + // A constant condition enters only one region, so the other arm's value does + // not reach the result. + cir.func @const_cond(%arg0: !s32i) -> !s32i { + %0 = cir.const #cir.bool<true> : !cir.bool + %1 = cir.ternary (%0, true { + %2 = cir.const #cir.int<1> : !s32i + cir.yield %2 : !s32i + }, false { + cir.yield %arg0 : !s32i + }) : (!cir.bool) -> !s32i + cir.return %1 : !s32i + } + // CHECK: cir.func{{.*}} @const_cond + // CHECK: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i + // CHECK: cir.return %[[ONE]] : !s32i + + cir.func @unknown_cond(%arg0: !cir.bool, %arg1: !s32i) -> !s32i { + %0 = cir.ternary (%arg0, true { + %1 = cir.const #cir.int<1> : !s32i + cir.yield %1 : !s32i + }, false { + cir.yield %arg1 : !s32i + }) : (!cir.bool) -> !s32i + cir.return %0 : !s32i + } + // CHECK: cir.func{{.*}} @unknown_cond + // CHECK: %[[RESULT:.*]] = cir.ternary + // CHECK: cir.return %[[RESULT]] : !s32i +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
