From 337854270023c95401b7718f586b9759891caa06 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 1 Jun 2021 12:05:36 -0400 Subject: [PATCH] [InstCombine] add tests for cast folding; NFC https://llvm.org/PR49543 --- .../Transforms/InstCombine/sext-of-trunc-nsw.ll | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll b/llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll index 10098d9..8aa2449 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 +} -- 2.7.4