Author: Bruno Cardoso Lopes Date: 2026-11-13T00:57:56Z New Revision: 4f7b1171acb6d00a773f93730fc97bcbb59fd328
URL: https://github.com/llvm/llvm-project/commit/4f7b1171acb6d00a773f93730fc97bcbb59fd328 DIFF: https://github.com/llvm/llvm-project/commit/4f7b1171acb6d00a773f93730fc97bcbb59fd328.diff LOG: [CIR] Fold constant int/bool casts and narrow a constant ternary's re… (#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 Added: clang/test/CIR/Transforms/ternary-sccp.cir Modified: clang/include/clang/CIR/Dialect/IR/CIROps.td clang/lib/CIR/Dialect/IR/CIRDialect.cpp clang/test/CIR/CodeGen/if.cpp clang/test/CIR/CodeGen/loop.cpp clang/test/CIR/CodeGenCoroutines/coro-builtins.cpp clang/test/CIR/CodeGenOpenACC/combined.cpp clang/test/CIR/CodeGenOpenACC/data.c clang/test/CIR/CodeGenOpenACC/init.c clang/test/CIR/CodeGenOpenACC/kernels.c clang/test/CIR/CodeGenOpenACC/parallel.c clang/test/CIR/CodeGenOpenACC/serial.c clang/test/CIR/CodeGenOpenACC/shutdown.c clang/test/CIR/CodeGenOpenMP/parallel.c clang/test/CIR/Transforms/canonicalize.cir Removed: ################################################################################ diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 50e92344a227c..906620a75df6f 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) { @@ -154,9 +157,11 @@ 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> { +// every region returns to it on exit. Subclasses may narrow which regions are +// entered for constant operands, through `getEntrySuccessorRegions`. +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, @@ -3404,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 66fc49c1f18f2..257c16a3e8ccd 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -1350,7 +1350,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(); @@ -1362,7 +1362,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 {}; @@ -1372,6 +1372,18 @@ 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: + // Zero-extend the bit, which also fits a signed one-bit integer. + if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(src)) + return cir::IntAttr::get( + getType(), + llvm::APInt(mlir::cast<cir::IntType>(getType()).getWidth(), + boolAttr.getValue())); + break; default: break; } @@ -3551,6 +3563,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 143ddcc33d2fc..fa4eaa3d16ce1 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 89e5f69273044..c49b0ddcb473a 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 28e015f8e01c3..1d377bfdb573c 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 b6ba08993a583..adb98a2b287a5 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 2e7b08f055163..336225ec52f77 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 7c959acd74f0c..c999ffcfe6f5b 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 2111643cb9b8f..35969324db18a 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 28c9f5127b989..9a859ed424b1a 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 e3098f63f242a..cf43439d3acb4 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 53ec4976e34de..8a5c57045b44d 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 6a9a349c59489..678a4d9491529 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 a65df32833f19..3da6c9df577a8 100644 --- a/clang/test/CIR/Transforms/canonicalize.cir +++ b/clang/test/CIR/Transforms/canonicalize.cir @@ -204,6 +204,33 @@ 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 @cast_const_bool_to_signed_bit() -> !cir.int<s, 1> { + %0 = cir.const #true + %1 = cir.cast bool_to_int %0 : !cir.bool -> !cir.int<s, 1> + cir.return %1 : !cir.int<s, 1> + } + // CHECK: cir.func{{.*}} @cast_const_bool_to_signed_bit() -> !cir.int<s, 1> + // CHECK-NEXT: %[[BIT:.*]] = cir.const #cir.int<-1> : !cir.int<s, 1> + // CHECK-NEXT: cir.return %[[BIT]] : !cir.int<s, 1> + 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 0000000000000..39d6a0e503999 --- /dev/null +++ b/clang/test/CIR/Transforms/ternary-sccp.cir @@ -0,0 +1,74 @@ +// RUN: cir-opt %s --sccp | FileCheck %s + +!s32i = !cir.int<s, 32> + +#true = #cir.bool<true> : !cir.bool +#false = #cir.bool<false> : !cir.bool + +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 #true + %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 @const_false_cond(%arg0: !s32i) -> !s32i { + %0 = cir.const #false + %1 = cir.ternary (%0, true { + cir.yield %arg0 : !s32i + }, false { + %2 = cir.const #cir.int<2> : !s32i + cir.yield %2 : !s32i + }) : (!cir.bool) -> !s32i + cir.return %1 : !s32i + } + // CHECK: cir.func{{.*}} @const_false_cond + // CHECK: %[[TWO:.*]] = cir.const #cir.int<2> : !s32i + // CHECK: cir.return %[[TWO]] : !s32i + + // The int_to_bool folds on the constant SCCP finds for the first ternary, + // which has no cir.const defining it, and so selects the second's region. + cir.func @cast_of_folded_result(%arg0: !s32i, %arg1: !s32i) -> !s32i { + %0 = cir.const #true + %1 = cir.ternary (%0, true { + %4 = cir.const #cir.int<1> : !s32i + cir.yield %4 : !s32i + }, false { + cir.yield %arg0 : !s32i + }) : (!cir.bool) -> !s32i + %2 = cir.cast int_to_bool %1 : !s32i -> !cir.bool + %3 = cir.ternary (%2, true { + %4 = cir.const #cir.int<7> : !s32i + cir.yield %4 : !s32i + }, false { + cir.yield %arg1 : !s32i + }) : (!cir.bool) -> !s32i + cir.return %3 : !s32i + } + // CHECK: cir.func{{.*}} @cast_of_folded_result + // CHECK: %[[SEVEN:.*]] = cir.const #cir.int<7> : !s32i + // CHECK: cir.return %[[SEVEN]] : !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
