-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CIR][Transform] Add ternary simplification
This patch adds a new transformation that transform suitable ternary operations into select operations.
- Loading branch information
Showing
3 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// RUN: cir-opt -cir-simplify -o %t.cir %s | ||
// RUN: FileCheck --input-file=%t.cir %s | ||
|
||
!s32i = !cir.int<s, 32> | ||
|
||
module { | ||
cir.func @fold_ternary(%arg0: !s32i, %arg1: !s32i) -> !s32i { | ||
%0 = cir.const #cir.bool<false> : !cir.bool | ||
%1 = cir.ternary (%0, true { | ||
cir.yield %arg0 : !s32i | ||
}, false { | ||
cir.yield %arg1 : !s32i | ||
}) : (!cir.bool) -> !s32i | ||
cir.return %1 : !s32i | ||
} | ||
|
||
// CHECK: cir.func @fold_ternary(%{{.+}}: !s32i, %[[ARG:.+]]: !s32i) -> !s32i { | ||
// CHECK-NEXT: cir.return %[[ARG]] : !s32i | ||
// CHECK-NEXT: } | ||
|
||
cir.func @simplify_ternary(%arg0 : !cir.bool, %arg1 : !s32i) -> !s32i { | ||
%0 = cir.ternary (%arg0, true { | ||
%1 = cir.const #cir.int<42> : !s32i | ||
cir.yield %1 : !s32i | ||
}, false { | ||
cir.yield %arg1 : !s32i | ||
}) : (!cir.bool) -> !s32i | ||
cir.return %0 : !s32i | ||
} | ||
|
||
// CHECK: cir.func @simplify_ternary(%[[ARG0:.+]]: !cir.bool, %[[ARG1:.+]]: !s32i) -> !s32i { | ||
// CHECK-NEXT: %[[#A:]] = cir.const #cir.int<42> : !s32i | ||
// CHECK-NEXT: %[[#B:]] = cir.select if %[[ARG0]] then %[[#A]] else %[[ARG1]] : (!cir.bool, !s32i, !s32i) -> !s32i | ||
// CHECK-NEXT: cir.return %[[#B]] : !s32i | ||
// CHECK-NEXT: } | ||
|
||
cir.func @non_simplifiable_ternary(%arg0 : !cir.bool, %arg1 : !cir.ptr<!s32i>) -> !s32i { | ||
// Not simplifiable, should keep as-is. | ||
%0 = cir.ternary (%arg0, true { | ||
%1 = cir.load %arg1 : !cir.ptr<!s32i>, !s32i | ||
cir.yield %1 : !s32i | ||
}, false { | ||
%2 = cir.const #cir.int<42> : !s32i | ||
cir.yield %2 : !s32i | ||
}) : (!cir.bool) -> !s32i | ||
cir.return %0 : !s32i | ||
} | ||
|
||
// CHECK: cir.func @non_simplifiable_ternary(%[[ARG0:.+]]: !cir.bool, %[[ARG1:.+]]: !cir.ptr<!s32i>) -> !s32i { | ||
// CHECK-NEXT: %[[#A:]] = cir.ternary(%[[ARG0]], true { | ||
// CHECK-NEXT: %[[#B:]] = cir.load %[[ARG1]] : !cir.ptr<!s32i>, !s32i | ||
// CHECK-NEXT: cir.yield %[[#B]] : !s32i | ||
// CHECK-NEXT: }, false { | ||
// CHECK-NEXT: %[[#C:]] = cir.const #cir.int<42> : !s32i | ||
// CHECK-NEXT: cir.yield %[[#C]] : !s32i | ||
// CHECK-NEXT: }) : (!cir.bool) -> !s32i | ||
// CHECK-NEXT: cir.return %[[#A]] : !s32i | ||
// CHECK-NEXT: } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -fclangir-mem2reg -mmlir --mlir-print-ir-before=cir-simplify %s -o %t1.cir 2>&1 | FileCheck -check-prefix=CIR-BEFORE %s | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -fclangir-mem2reg -mmlir --mlir-print-ir-after=cir-simplify %s -o %t2.cir 2>&1 | FileCheck -check-prefix=CIR-AFTER %s | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll | ||
// RUN: FileCheck --input-file=%t.ll --check-prefix=LLVM %s | ||
|
||
int test1(bool x) { | ||
return x ? 1 : 2; | ||
} | ||
|
||
// CIR-BEFORE: cir.func @_Z5test1b | ||
// CIR-BEFORE: %{{.+}} = cir.ternary(%{{.+}}, true { | ||
// CIR-BEFORE-NEXT: %[[#A:]] = cir.const #cir.int<1> : !s32i | ||
// CIR-BEFORE-NEXT: cir.yield %[[#A]] : !s32i | ||
// CIR-BEFORE-NEXT: }, false { | ||
// CIR-BEFORE-NEXT: %[[#B:]] = cir.const #cir.int<2> : !s32i | ||
// CIR-BEFORE-NEXT: cir.yield %[[#B]] : !s32i | ||
// CIR-BEFORE-NEXT: }) : (!cir.bool) -> !s32i | ||
// CIR-BEFORE: } | ||
|
||
// CIR-AFTER: cir.func @_Z5test1b | ||
// CIR-AFTER: %[[#A:]] = cir.const #cir.int<1> : !s32i | ||
// CIR-AFTER-NEXT: %[[#B:]] = cir.const #cir.int<2> : !s32i | ||
// CIR-AFTER-NEXT: %{{.+}} = cir.select if %{{.+}} then %[[#A]] else %[[#B]] : (!cir.bool, !s32i, !s32i) -> !s32i | ||
// CIR-AFTER: } | ||
|
||
// LLVM: define dso_local i32 @_Z5test1b | ||
// LLVM: %{{.+}} = select i1 %{{.+}}, i32 1, i32 2 | ||
// LLVM: } | ||
|
||
// The following test does not work yet because mem2reg does not happen before | ||
// ternary simplify. | ||
|
||
// int test2(bool x, int a, int b) { | ||
// return x ? a : b; | ||
// } |