Skip to content

Commit 5e11fe4

Browse files
[InstCombine] Perform trunc transformation for values with >1 users
1 parent c750658 commit 5e11fe4

File tree

7 files changed

+331
-116
lines changed

7 files changed

+331
-116
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 299 additions & 70 deletions
Large diffs are not rendered by default.

llvm/test/Transforms/InstCombine/cast-mul-select.ll

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,18 @@ define i8 @select2(i1 %cond, i8 %x, i8 %y, i8 %z) {
9191

9292
define i32 @eval_trunc_multi_use_in_one_inst(i32 %x) {
9393
; CHECK-LABEL: @eval_trunc_multi_use_in_one_inst(
94-
; CHECK-NEXT: [[Z:%.*]] = zext i32 [[X:%.*]] to i64
95-
; CHECK-NEXT: [[A:%.*]] = add nuw nsw i64 [[Z]], 15
96-
; CHECK-NEXT: [[M:%.*]] = mul i64 [[A]], [[A]]
97-
; CHECK-NEXT: [[T:%.*]] = trunc i64 [[M]] to i32
94+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], 15
95+
; CHECK-NEXT: [[T:%.*]] = mul i32 [[A]], [[A]]
9896
; CHECK-NEXT: ret i32 [[T]]
9997
;
10098
; DBGINFO-LABEL: @eval_trunc_multi_use_in_one_inst(
101-
; DBGINFO-NEXT: [[Z:%.*]] = zext i32 [[X:%.*]] to i64, !dbg [[DBG57:![0-9]+]]
102-
; DBGINFO-NEXT: #dbg_value(i64 [[Z]], [[META52:![0-9]+]], !DIExpression(), [[DBG57]])
103-
; DBGINFO-NEXT: [[A:%.*]] = add nuw nsw i64 [[Z]], 15, !dbg [[DBG58:![0-9]+]]
104-
; DBGINFO-NEXT: #dbg_value(i64 [[A]], [[META54:![0-9]+]], !DIExpression(), [[DBG58]])
105-
; DBGINFO-NEXT: [[M:%.*]] = mul i64 [[A]], [[A]], !dbg [[DBG59:![0-9]+]]
106-
; DBGINFO-NEXT: #dbg_value(i64 [[M]], [[META55:![0-9]+]], !DIExpression(), [[DBG59]])
107-
; DBGINFO-NEXT: [[T:%.*]] = trunc i64 [[M]] to i32, !dbg [[DBG60:![0-9]+]]
108-
; DBGINFO-NEXT: #dbg_value(i32 [[T]], [[META56:![0-9]+]], !DIExpression(), [[DBG60]])
109-
; DBGINFO-NEXT: ret i32 [[T]], !dbg [[DBG61:![0-9]+]]
99+
; DBGINFO-NEXT: #dbg_value(i32 [[X:%.*]], [[META52:![0-9]+]], !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 64, DW_ATE_unsigned, DW_OP_stack_value), [[META57:![0-9]+]])
100+
; DBGINFO-NEXT: [[A:%.*]] = add i32 [[X]], 15, !dbg [[DBG58:![0-9]+]]
101+
; DBGINFO-NEXT: #dbg_value(i32 [[X]], [[META54:![0-9]+]], !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 64, DW_ATE_unsigned, DW_OP_plus_uconst, 15, DW_OP_stack_value), [[DBG58]])
102+
; DBGINFO-NEXT: [[M:%.*]] = mul i32 [[A]], [[A]], !dbg [[DBG59:![0-9]+]]
103+
; DBGINFO-NEXT: #dbg_value(!DIArgList(i32 [[X]], i32 [[X]]), [[META55:![0-9]+]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 64, DW_ATE_unsigned, DW_OP_plus_uconst, 15, DW_OP_LLVM_arg, 1, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 64, DW_ATE_unsigned, DW_OP_plus_uconst, 15, DW_OP_mul, DW_OP_stack_value), [[DBG59]])
104+
; DBGINFO-NEXT: #dbg_value(i32 [[M]], [[META56:![0-9]+]], !DIExpression(), [[META60:![0-9]+]])
105+
; DBGINFO-NEXT: ret i32 [[M]], !dbg [[DBG61:![0-9]+]]
110106
;
111107
%z = zext i32 %x to i64
112108
%a = add nsw nuw i64 %z, 15

llvm/test/Transforms/InstCombine/cast.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,13 +2241,10 @@ define i32 @test95(i32 %x) {
22412241

22422242
define i16 @test96(i16 %x, i16 %y) {
22432243
; ALL-LABEL: @test96(
2244-
; ALL-NEXT: [[ZX:%.*]] = zext i16 [[X:%.*]] to i32
2245-
; ALL-NEXT: [[ZY:%.*]] = zext i16 [[Y:%.*]] to i32
2246-
; ALL-NEXT: [[A:%.*]] = add nuw nsw i32 [[ZX]], [[ZY]]
2247-
; ALL-NEXT: [[B:%.*]] = add nuw nsw i32 [[A]], 5
2248-
; ALL-NEXT: [[C:%.*]] = mul nuw nsw i32 [[A]], 3
2249-
; ALL-NEXT: [[D:%.*]] = or i32 [[B]], [[C]]
2250-
; ALL-NEXT: [[T:%.*]] = trunc i32 [[D]] to i16
2244+
; ALL-NEXT: [[A:%.*]] = add i16 [[X:%.*]], [[Y:%.*]]
2245+
; ALL-NEXT: [[B:%.*]] = add i16 [[A]], 5
2246+
; ALL-NEXT: [[C:%.*]] = mul i16 [[A]], 3
2247+
; ALL-NEXT: [[T:%.*]] = or i16 [[B]], [[C]]
22512248
; ALL-NEXT: ret i16 [[T]]
22522249
;
22532250
%zx = zext i16 %x to i32

llvm/test/Transforms/InstCombine/catchswitch-phi.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,17 @@ define void @test1() personality ptr @__gxx_wasm_personality_v0 {
9494
; CHECK-NEXT: [[CALL:%.*]] = invoke i32 @baz()
9595
; CHECK-NEXT: to label [[INVOKE_CONT1:%.*]] unwind label [[CATCH_DISPATCH:%.*]]
9696
; CHECK: invoke.cont1:
97-
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], 0
98-
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
97+
; CHECK-NEXT: [[TOBOOL_INIT:%.*]] = icmp ne i32 [[CALL]], 0
98+
; CHECK-NEXT: br i1 [[TOBOOL_INIT]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
9999
; CHECK: if.then:
100100
; CHECK-NEXT: br label [[IF_END]]
101101
; CHECK: if.end:
102-
; CHECK-NEXT: [[AP_0:%.*]] = phi i8 [ 1, [[IF_THEN]] ], [ 0, [[INVOKE_CONT1]] ]
103102
; CHECK-NEXT: invoke void @foo()
104103
; CHECK-NEXT: to label [[INVOKE_CONT2:%.*]] unwind label [[CATCH_DISPATCH]]
105104
; CHECK: invoke.cont2:
106105
; CHECK-NEXT: br label [[TRY_CONT:%.*]]
107106
; CHECK: catch.dispatch:
108-
; CHECK-NEXT: [[AP_1:%.*]] = phi i8 [ [[AP_0]], [[IF_END]] ], [ 0, [[INVOKE_CONT]] ]
107+
; CHECK-NEXT: [[AP_1:%.*]] = phi i1 [ [[TOBOOL_INIT]], [[IF_END]] ], [ false, [[INVOKE_CONT]] ]
109108
; CHECK-NEXT: [[TMP0:%.*]] = catchswitch within none [label %catch.start] unwind label [[CATCH_DISPATCH1]]
110109
; CHECK: catch.start:
111110
; CHECK-NEXT: [[TMP1:%.*]] = catchpad within [[TMP0]] [ptr null]
@@ -116,11 +115,10 @@ define void @test1() personality ptr @__gxx_wasm_personality_v0 {
116115
; CHECK-NEXT: invoke void @llvm.wasm.rethrow() #[[ATTR0:[0-9]+]] [ "funclet"(token [[TMP1]]) ]
117116
; CHECK-NEXT: to label [[UNREACHABLE:%.*]] unwind label [[CATCH_DISPATCH1]]
118117
; CHECK: catch.dispatch1:
119-
; CHECK-NEXT: [[AP_2:%.*]] = phi i8 [ [[AP_1]], [[CATCH_DISPATCH]] ], [ [[AP_1]], [[RETHROW]] ], [ 0, [[ENTRY:%.*]] ]
118+
; CHECK-NEXT: [[TOBOOL1:%.*]] = phi i1 [ [[AP_1]], [[CATCH_DISPATCH]] ], [ [[AP_1]], [[RETHROW]] ], [ false, [[ENTRY:%.*]] ]
120119
; CHECK-NEXT: [[TMP2:%.*]] = catchswitch within none [label %catch.start1] unwind to caller
121120
; CHECK: catch.start1:
122121
; CHECK-NEXT: [[TMP3:%.*]] = catchpad within [[TMP2]] [ptr null]
123-
; CHECK-NEXT: [[TOBOOL1:%.*]] = trunc i8 [[AP_2]] to i1
124122
; CHECK-NEXT: br i1 [[TOBOOL1]], label [[IF_THEN1:%.*]], label [[IF_END1:%.*]]
125123
; CHECK: if.then1:
126124
; CHECK-NEXT: br label [[IF_END1]]

llvm/test/Transforms/InstCombine/icmp-mul-zext.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ define void @PR33765(i8 %beth) {
5757
; CHECK-LABEL: @PR33765(
5858
; CHECK-NEXT: br i1 false, label [[IF_THEN9:%.*]], label [[IF_THEN9]]
5959
; CHECK: if.then9:
60-
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[BETH:%.*]] to i32
61-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw nsw i32 [[CONV]], [[CONV]]
60+
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[BETH:%.*]] to i16
61+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i16 [[CONV]], [[CONV]]
6262
; CHECK-NEXT: [[TINKY:%.*]] = load i16, ptr @glob, align 2
63-
; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i32 [[MUL]] to i16
64-
; CHECK-NEXT: [[CONV14:%.*]] = and i16 [[TINKY]], [[TMP1]]
63+
; CHECK-NEXT: [[CONV14:%.*]] = and i16 [[MUL]], [[TINKY]]
6564
; CHECK-NEXT: store i16 [[CONV14]], ptr @glob, align 2
6665
; CHECK-NEXT: ret void
6766
;

llvm/test/Transforms/InstCombine/logical-select-inseltpoison.ll

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -640,14 +640,12 @@ define <2 x i64> @fp_bitcast(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) {
640640
define <4 x i32> @computesignbits_through_shuffles(<4 x float> %x, <4 x float> %y, <4 x float> %z) {
641641
; CHECK-LABEL: @computesignbits_through_shuffles(
642642
; CHECK-NEXT: [[CMP:%.*]] = fcmp ole <4 x float> [[X:%.*]], [[Y:%.*]]
643-
; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> [[CMP]] to <4 x i32>
644-
; CHECK-NEXT: [[S1:%.*]] = shufflevector <4 x i32> [[SEXT]], <4 x i32> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
645-
; CHECK-NEXT: [[S2:%.*]] = shufflevector <4 x i32> [[SEXT]], <4 x i32> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
646-
; CHECK-NEXT: [[SHUF_OR1:%.*]] = or <4 x i32> [[S1]], [[S2]]
647-
; CHECK-NEXT: [[S3:%.*]] = shufflevector <4 x i32> [[SHUF_OR1]], <4 x i32> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
648-
; CHECK-NEXT: [[S4:%.*]] = shufflevector <4 x i32> [[SHUF_OR1]], <4 x i32> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
649-
; CHECK-NEXT: [[SHUF_OR2:%.*]] = or <4 x i32> [[S3]], [[S4]]
650-
; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw <4 x i32> [[SHUF_OR2]] to <4 x i1>
643+
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <4 x i1> [[CMP]], <4 x i1> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
644+
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x i1> [[CMP]], <4 x i1> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
645+
; CHECK-NEXT: [[TMP3:%.*]] = or <4 x i1> [[TMP4]], [[TMP2]]
646+
; CHECK-NEXT: [[S3:%.*]] = shufflevector <4 x i1> [[TMP3]], <4 x i1> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
647+
; CHECK-NEXT: [[S4:%.*]] = shufflevector <4 x i1> [[TMP3]], <4 x i1> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
648+
; CHECK-NEXT: [[TMP1:%.*]] = or <4 x i1> [[S3]], [[S4]]
651649
; CHECK-NEXT: [[SEL_V:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[Z:%.*]], <4 x float> [[X]]
652650
; CHECK-NEXT: [[SEL:%.*]] = bitcast <4 x float> [[SEL_V]] to <4 x i32>
653651
; CHECK-NEXT: ret <4 x i32> [[SEL]]

llvm/test/Transforms/InstCombine/logical-select.ll

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,12 @@ define <2 x i64> @fp_bitcast(<4 x i1> %cmp, <2 x double> %a, <2 x double> %b) {
676676
define <4 x i32> @computesignbits_through_shuffles(<4 x float> %x, <4 x float> %y, <4 x float> %z) {
677677
; CHECK-LABEL: @computesignbits_through_shuffles(
678678
; CHECK-NEXT: [[CMP:%.*]] = fcmp ole <4 x float> [[X:%.*]], [[Y:%.*]]
679-
; CHECK-NEXT: [[SEXT:%.*]] = sext <4 x i1> [[CMP]] to <4 x i32>
680-
; CHECK-NEXT: [[S1:%.*]] = shufflevector <4 x i32> [[SEXT]], <4 x i32> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
681-
; CHECK-NEXT: [[S2:%.*]] = shufflevector <4 x i32> [[SEXT]], <4 x i32> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
682-
; CHECK-NEXT: [[SHUF_OR1:%.*]] = or <4 x i32> [[S1]], [[S2]]
683-
; CHECK-NEXT: [[S3:%.*]] = shufflevector <4 x i32> [[SHUF_OR1]], <4 x i32> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
684-
; CHECK-NEXT: [[S4:%.*]] = shufflevector <4 x i32> [[SHUF_OR1]], <4 x i32> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
685-
; CHECK-NEXT: [[SHUF_OR2:%.*]] = or <4 x i32> [[S3]], [[S4]]
686-
; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw <4 x i32> [[SHUF_OR2]] to <4 x i1>
679+
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <4 x i1> [[CMP]], <4 x i1> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
680+
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x i1> [[CMP]], <4 x i1> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
681+
; CHECK-NEXT: [[TMP3:%.*]] = or <4 x i1> [[TMP4]], [[TMP2]]
682+
; CHECK-NEXT: [[S3:%.*]] = shufflevector <4 x i1> [[TMP3]], <4 x i1> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
683+
; CHECK-NEXT: [[S4:%.*]] = shufflevector <4 x i1> [[TMP3]], <4 x i1> poison, <4 x i32> <i32 2, i32 2, i32 3, i32 3>
684+
; CHECK-NEXT: [[TMP1:%.*]] = or <4 x i1> [[S3]], [[S4]]
687685
; CHECK-NEXT: [[SEL_V:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[Z:%.*]], <4 x float> [[X]]
688686
; CHECK-NEXT: [[SEL:%.*]] = bitcast <4 x float> [[SEL_V]] to <4 x i32>
689687
; CHECK-NEXT: ret <4 x i32> [[SEL]]

0 commit comments

Comments
 (0)