Skip to content

Commit 65a06bf

Browse files
committed
[InstCombine] Add support for cast instructions in getFreelyInvertedImpl
1 parent ded0d6f commit 65a06bf

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,20 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
23872387
return NonNull;
23882388
}
23892389

2390+
if (match(V, m_SExtLike(m_Value(A)))) {
2391+
if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2392+
DoesConsume, Depth))
2393+
return Builder ? Builder->CreateSExt(AV, V->getType()) : NonNull;
2394+
return nullptr;
2395+
}
2396+
2397+
if (match(V, m_Trunc(m_Value(A)))) {
2398+
if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2399+
DoesConsume, Depth))
2400+
return Builder ? Builder->CreateTrunc(AV, V->getType()) : NonNull;
2401+
return nullptr;
2402+
}
2403+
23902404
return nullptr;
23912405
}
23922406

llvm/test/Transforms/InstCombine/not.ll

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,9 @@ entry:
772772

773773
define i32 @test_sext(i32 %a, i32 %b){
774774
; CHECK-LABEL: @test_sext(
775-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
776-
; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[CMP]] to i32
777-
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[SEXT]], [[B:%.*]]
778-
; CHECK-NEXT: [[NOT:%.*]] = xor i32 [[ADD]], -1
775+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
776+
; CHECK-NEXT: [[TMP2:%.*]] = sext i1 [[TMP1]] to i32
777+
; CHECK-NEXT: [[NOT:%.*]] = sub i32 [[TMP2]], [[B:%.*]]
779778
; CHECK-NEXT: ret i32 [[NOT]]
780779
;
781780
%cmp = icmp eq i32 %a, 0
@@ -787,10 +786,9 @@ define i32 @test_sext(i32 %a, i32 %b){
787786

788787
define <2 x i32> @test_sext_vec(<2 x i32> %a, <2 x i32> %b){
789788
; CHECK-LABEL: @test_sext_vec(
790-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], zeroinitializer
791-
; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[CMP]] to <2 x i32>
792-
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[SEXT]], [[B:%.*]]
793-
; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i32> [[ADD]], <i32 -1, i32 -1>
789+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i32> [[A:%.*]], zeroinitializer
790+
; CHECK-NEXT: [[TMP2:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i32>
791+
; CHECK-NEXT: [[NOT:%.*]] = sub <2 x i32> [[TMP2]], [[B:%.*]]
794792
; CHECK-NEXT: ret <2 x i32> [[NOT]]
795793
;
796794
%cmp = icmp eq <2 x i32> %a, zeroinitializer
@@ -814,11 +812,8 @@ define i32 @test_zext_nneg(i32 %a, i32 %b){
814812

815813
define i8 @test_trunc(i8 %a){
816814
; CHECK-LABEL: @test_trunc(
817-
; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[A:%.*]] to i32
818-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[ZEXT]], -1
819-
; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31
820-
; CHECK-NEXT: [[CONV:%.*]] = trunc i32 [[SHR]] to i8
821-
; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[CONV]], -1
815+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[A:%.*]], 0
816+
; CHECK-NEXT: [[NOT:%.*]] = sext i1 [[TMP1]] to i8
822817
; CHECK-NEXT: ret i8 [[NOT]]
823818
;
824819
%zext = zext i8 %a to i32
@@ -831,11 +826,8 @@ define i8 @test_trunc(i8 %a){
831826

832827
define <2 x i8> @test_trunc_vec(<2 x i8> %a){
833828
; CHECK-LABEL: @test_trunc_vec(
834-
; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A:%.*]] to <2 x i32>
835-
; CHECK-NEXT: [[SUB:%.*]] = add nsw <2 x i32> [[ZEXT]], <i32 -1, i32 -1>
836-
; CHECK-NEXT: [[SHR:%.*]] = ashr <2 x i32> [[SUB]], <i32 31, i32 31>
837-
; CHECK-NEXT: [[CONV:%.*]] = trunc <2 x i32> [[SHR]] to <2 x i8>
838-
; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i8> [[CONV]], <i8 -1, i8 -1>
829+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i8> [[A:%.*]], zeroinitializer
830+
; CHECK-NEXT: [[NOT:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
839831
; CHECK-NEXT: ret <2 x i8> [[NOT]]
840832
;
841833
%zext = zext <2 x i8> %a to <2 x i32>

0 commit comments

Comments
 (0)