[llvm] 3378542 - [InstCombine] add tests for cast folding; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 1 13:03:32 PDT 2021


Author: Sanjay Patel
Date: 2021-06-01T16:03:24-04:00
New Revision: 337854270023c95401b7718f586b9759891caa06

URL: https://github.com/llvm/llvm-project/commit/337854270023c95401b7718f586b9759891caa06
DIFF: https://github.com/llvm/llvm-project/commit/337854270023c95401b7718f586b9759891caa06.diff

LOG: [InstCombine] add tests for cast folding; NFC

https://llvm.org/PR49543

Added: 
    

Modified: 
    llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll b/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
index 10098d946bf7a..8aa244971cf41 100644
--- a/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
+++ b/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll
@@ -101,3 +101,125 @@ define i16 @t5_extrause(i8 %x) {
   %c = sext i4 %b to i16
   ret i16 %c
 }
+
+define i64 @narrow_source_matching_signbits(i32 %x) {
+; CHECK-LABEL: @narrow_source_matching_signbits(
+; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
+; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i64
+; CHECK-NEXT:    ret i64 [[C]]
+;
+  %m = and i32 %x, 7
+  %a = shl nsw i32 -1, %m
+  %b = trunc i32 %a to i8
+  %c = sext i8 %b to i64
+  ret i64 %c
+}
+
+define i64 @narrow_source_not_matching_signbits(i32 %x) {
+; CHECK-LABEL: @narrow_source_not_matching_signbits(
+; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 8
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
+; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i64
+; CHECK-NEXT:    ret i64 [[C]]
+;
+  %m = and i32 %x, 8
+  %a = shl nsw i32 -1, %m
+  %b = trunc i32 %a to i8
+  %c = sext i8 %b to i64
+  ret i64 %c
+}
+
+define i24 @wide_source_matching_signbits(i32 %x) {
+; CHECK-LABEL: @wide_source_matching_signbits(
+; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
+; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i24
+; CHECK-NEXT:    ret i24 [[C]]
+;
+  %m = and i32 %x, 7
+  %a = shl nsw i32 -1, %m
+  %b = trunc i32 %a to i8
+  %c = sext i8 %b to i24
+  ret i24 %c
+}
+
+define i24 @wide_source_not_matching_signbits(i32 %x) {
+; CHECK-LABEL: @wide_source_not_matching_signbits(
+; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M2]]
+; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i24
+; CHECK-NEXT:    ret i24 [[C]]
+;
+  %m2 = and i32 %x, 8
+  %a = shl nsw i32 -1, %m2
+  %b = trunc i32 %a to i8
+  %c = sext i8 %b to i24
+  ret i24 %c
+}
+
+define i32 @same_source_matching_signbits(i32 %x) {
+; CHECK-LABEL: @same_source_matching_signbits(
+; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 -16777216, [[M]]
+; CHECK-NEXT:    [[C:%.*]] = ashr exact i32 [[TMP1]], 24
+; CHECK-NEXT:    ret i32 [[C]]
+;
+  %m = and i32 %x, 7
+  %a = shl nsw i32 -1, %m
+  %b = trunc i32 %a to i8
+  %c = sext i8 %b to i32
+  ret i32 %c
+}
+
+define i32 @same_source_not_matching_signbits(i32 %x) {
+; CHECK-LABEL: @same_source_not_matching_signbits(
+; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 -16777216, [[M2]]
+; CHECK-NEXT:    [[C:%.*]] = ashr exact i32 [[TMP1]], 24
+; CHECK-NEXT:    ret i32 [[C]]
+;
+  %m2 = and i32 %x, 8
+  %a = shl nsw i32 -1, %m2
+  %b = trunc i32 %a to i8
+  %c = sext i8 %b to i32
+  ret i32 %c
+}
+
+define i32 @same_source_matching_signbits_extra_use(i32 %x) {
+; CHECK-LABEL: @same_source_matching_signbits_extra_use(
+; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
+; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    call void @use8(i8 [[B]])
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i32
+; CHECK-NEXT:    ret i32 [[C]]
+;
+  %m = and i32 %x, 7
+  %a = shl nsw i32 -1, %m
+  %b = trunc i32 %a to i8
+  call void @use8(i8 %b)
+  %c = sext i8 %b to i32
+  ret i32 %c
+}
+
+define i32 @same_source_not_matching_signbits_extra_use(i32 %x) {
+; CHECK-LABEL: @same_source_not_matching_signbits_extra_use(
+; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
+; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M2]]
+; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
+; CHECK-NEXT:    call void @use8(i8 [[B]])
+; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i32
+; CHECK-NEXT:    ret i32 [[C]]
+;
+  %m2 = and i32 %x, 8
+  %a = shl nsw i32 -1, %m2
+  %b = trunc i32 %a to i8
+  call void @use8(i8 %b)
+  %c = sext i8 %b to i32
+  ret i32 %c
+}


        


More information about the llvm-commits mailing list