[PATCH] D82973: [InstCombine] Try to narrow expr if trunc cannot be removed.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 1 09:10:23 PDT 2020
fhahn created this revision.
fhahn added reviewers: spatel, RKSimon, lebedev.ri, xbolva00.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
Narrowing an input expression of a truncate to a type larger than the
result of the truncate won't allow removing the truncate, but it may
enable further optimizations, e.g. allowing for larger vectorization
factors.
For now this is intentionally limited to integer types only, to avoid
producing new vector ops that might not be suitable for the target.
If we know that the only user is a trunc, we can also be allow more
cases, e.g. also shortening expressions with some additional shifts.
I would appreciate feedback on the best place to do such a narrowing.
This fixes PR43580.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D82973
Files:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/trunc-binop-ext.ll
Index: llvm/test/Transforms/InstCombine/trunc-binop-ext.ll
===================================================================
--- llvm/test/Transforms/InstCombine/trunc-binop-ext.ll
+++ llvm/test/Transforms/InstCombine/trunc-binop-ext.ll
@@ -318,11 +318,11 @@
; Test cases for PR43580
define i8 @narrow_zext_ashr_keep_trunc(i8 %i1, i8 %i2) {
; CHECK-LABEL: @narrow_zext_ashr_keep_trunc(
-; CHECK-NEXT: [[I1_EXT:%.*]] = sext i8 [[I1:%.*]] to i32
-; CHECK-NEXT: [[I2_EXT:%.*]] = sext i8 [[I2:%.*]] to i32
-; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[I1_EXT]], [[I2_EXT]]
-; CHECK-NEXT: [[SHIFT:%.*]] = lshr i32 [[SUB]], 1
-; CHECK-NEXT: [[T:%.*]] = trunc i32 [[SHIFT]] to i8
+; CHECK-NEXT: [[I1_EXT:%.*]] = sext i8 [[I1:%.*]] to i16
+; CHECK-NEXT: [[I2_EXT:%.*]] = sext i8 [[I2:%.*]] to i16
+; CHECK-NEXT: [[SUB:%.*]] = add nsw i16 [[I1_EXT]], [[I2_EXT]]
+; CHECK-NEXT: [[SHIFT:%.*]] = lshr i16 [[SUB]], 1
+; CHECK-NEXT: [[T:%.*]] = trunc i16 [[SHIFT]] to i8
; CHECK-NEXT: ret i8 [[T]]
;
%i1.ext = sext i8 %i1 to i32
@@ -338,8 +338,8 @@
; CHECK-NEXT: [[I1_EXT:%.*]] = sext i8 [[I1:%.*]] to i16
; CHECK-NEXT: [[I2_EXT:%.*]] = sext i8 [[I2:%.*]] to i16
; CHECK-NEXT: [[SUB:%.*]] = add nsw i16 [[I1_EXT]], [[I2_EXT]]
-; CHECK-NEXT: [[SHIFT:%.*]] = lshr i16 [[SUB]], 1
-; CHECK-NEXT: [[T:%.*]] = trunc i16 [[SHIFT]] to i8
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i16 [[SUB]], 1
+; CHECK-NEXT: [[T:%.*]] = trunc i16 [[TMP1]] to i8
; CHECK-NEXT: ret i8 [[T]]
;
%i1.ext = sext i8 %i1 to i16
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -713,6 +713,25 @@
return replaceInstUsesWith(Trunc, Res);
}
+ // For integer types, check if we can shorten the entire input expression to
+ // DestWidth * 2, which won't allow removing the truncate, but reducing the
+ // width may enable further optimizations, e.g. allowing for larger
+ // vectorization factors.
+ if (auto *DestITy = dyn_cast<IntegerType>(DestTy)) {
+ if (DestWidth * 2 < SrcWidth) {
+ auto *NewDestTy = DestITy->getExtendedType();
+ if (shouldChangeType(SrcTy, DestTy) &&
+ canEvaluateTruncated(Src, NewDestTy, *this, &Trunc)) {
+ LLVM_DEBUG(
+ dbgs() << "ICE: EvaluateInDifferentType converting expression type"
+ " to reduce the width of operand of"
+ << Trunc << '\n');
+ Value *Res = EvaluateInDifferentType(Src, NewDestTy, false);
+ return new TruncInst(Res, DestTy);
+ }
+ }
+ }
+
// Test if the trunc is the user of a select which is part of a
// minimum or maximum operation. If so, don't do any more simplification.
// Even simplifying demanded bits can break the canonical form of a
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82973.274831.patch
Type: text/x-patch
Size: 2933 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200701/8f52e1c4/attachment.bin>
More information about the llvm-commits
mailing list