[llvm] 9594c0d - [InstCombine] Move sext(trunc(...)) combines before EvaluateInDifferentType (#227321)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 15:56:30 PDT 2026
Author: Luke Lau
Date: 2026-10-07T23:56:14+01:00
New Revision: 9594c0d77e42ceeeeb1d75c87ed76f9954067b22
URL: https://github.com/llvm/llvm-project/commit/9594c0d77e42ceeeeb1d75c87ed76f9954067b22
DIFF: https://github.com/llvm/llvm-project/commit/9594c0d77e42ceeeeb1d75c87ed76f9954067b22.diff
LOG: [InstCombine] Move sext(trunc(...)) combines before EvaluateInDifferentType (#227321)
EvaluateInDifferentType loses some of the sign bit information which the
sext(trunc(...)) combines can use to remove intermediate casts, so run
the latter earlier. This helps towards fixing a codegen pattern in x264
Tests written with opus 5.5
Added:
llvm/test/Transforms/InstCombine/AArch64/lshr-trunc-sext-to-ashr-sext.ll
llvm/test/Transforms/InstCombine/RISCV/lshr-trunc-sext-to-ashr-sext.ll
llvm/test/Transforms/InstCombine/X86/lshr-trunc-sext-to-ashr-sext.ll
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 0275bf52a13f25..f3741e9b16b94e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1945,37 +1945,8 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
return CI;
}
- // Try to extend the entire expression tree to the wide destination type.
- bool ShouldExtendExpression = true;
- Value *TruncSrc = nullptr;
- // It is not desirable to extend expression in the trunc + sext pattern when
- // destination type is narrower than original (pre-trunc) type.
- if (match(Src, m_Trunc(m_Value(TruncSrc))))
- if (TruncSrc->getType()->getScalarSizeInBits() > DestBitSize)
- ShouldExtendExpression = false;
- if (ShouldExtendExpression && shouldChangeType(SrcTy, DestTy) &&
- TypeEvaluationHelper::canEvaluateSExtd(Src, DestTy)) {
- // Okay, we can transform this! Insert the new expression now.
- LLVM_DEBUG(
- dbgs() << "ICE: EvaluateInDifferentType converting expression type"
- " to avoid sign extend: "
- << Sext << '\n');
- Value *Res = EvaluateInDifferentType(Src, DestTy, true);
- assert(Res->getType() == DestTy);
-
- // If the high bits are already filled with sign bit, just replace this
- // cast with the result.
- if (ComputeNumSignBits(Res, &Sext) > DestBitSize - SrcBitSize)
- return replaceInstUsesWith(Sext, Res);
-
- // We need to emit a shl + ashr to do the sign extend.
- Value *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
- return BinaryOperator::CreateAShr(Builder.CreateShl(Res, ShAmt, "sext"),
- ShAmt);
- }
-
- Value *X = TruncSrc;
- if (X) {
+ Value *X;
+ if (match(Src, m_Trunc(m_Value(X)))) {
// If the input has more sign bits than bits truncated, then convert
// directly to final type.
unsigned XBitSize = X->getType()->getScalarSizeInBits();
@@ -2012,6 +1983,35 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
}
}
+ // Try to extend the entire expression tree to the wide destination type.
+ bool ShouldExtendExpression = true;
+ Value *TruncSrc = nullptr;
+ // It is not desirable to extend expression in the trunc + sext pattern when
+ // destination type is narrower than original (pre-trunc) type.
+ if (match(Src, m_Trunc(m_Value(TruncSrc))))
+ if (TruncSrc->getType()->getScalarSizeInBits() > DestBitSize)
+ ShouldExtendExpression = false;
+ if (ShouldExtendExpression && shouldChangeType(SrcTy, DestTy) &&
+ TypeEvaluationHelper::canEvaluateSExtd(Src, DestTy)) {
+ // Okay, we can transform this! Insert the new expression now.
+ LLVM_DEBUG(
+ dbgs() << "ICE: EvaluateInDifferentType converting expression type"
+ " to avoid sign extend: "
+ << Sext << '\n');
+ Value *Res = EvaluateInDifferentType(Src, DestTy, true);
+ assert(Res->getType() == DestTy);
+
+ // If the high bits are already filled with sign bit, just replace this
+ // cast with the result.
+ if (ComputeNumSignBits(Res, &Sext) > DestBitSize - SrcBitSize)
+ return replaceInstUsesWith(Sext, Res);
+
+ // We need to emit a shl + ashr to do the sign extend.
+ Value *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
+ return BinaryOperator::CreateAShr(Builder.CreateShl(Res, ShAmt, "sext"),
+ ShAmt);
+ }
+
if (auto *Cmp = dyn_cast<ICmpInst>(Src))
return transformSExtICmp(Cmp, Sext);
diff --git a/llvm/test/Transforms/InstCombine/AArch64/lshr-trunc-sext-to-ashr-sext.ll b/llvm/test/Transforms/InstCombine/AArch64/lshr-trunc-sext-to-ashr-sext.ll
new file mode 100644
index 00000000000000..6451486688a123
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/AArch64/lshr-trunc-sext-to-ashr-sext.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -p instcombine -mtriple aarch64 -S | FileCheck %s
+
+define i64 @t0(i32 %x) {
+; CHECK-LABEL: define i64 @t0(
+; CHECK-SAME: i32 [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X]], 16
+; CHECK-NEXT: [[C:%.*]] = sext i32 [[TMP1]] to i64
+; CHECK-NEXT: ret i64 [[C]]
+;
+ %a = lshr i32 %x, 16
+ %b = trunc i32 %a to i16
+ %c = sext i16 %b to i64
+ ret i64 %c
+}
+
+define i32 @t1(i16 %x) {
+; CHECK-LABEL: define i32 @t1(
+; CHECK-SAME: i16 [[X:%.*]]) {
+; CHECK-NEXT: [[S:%.*]] = sext i16 [[X]] to i32
+; CHECK-NEXT: [[Y:%.*]] = shl nsw i32 [[S]], 10
+; CHECK-NEXT: [[TMP1:%.*]] = add nsw i32 [[Y]], 32768
+; CHECK-NEXT: [[C:%.*]] = ashr i32 [[TMP1]], 16
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %s = sext i16 %x to i32
+ %y = add i32 %s, 32
+ %a = ashr i32 %y, 6
+ %b = trunc i32 %a to i16
+ %c = sext i16 %b to i32
+ ret i32 %c
+}
diff --git a/llvm/test/Transforms/InstCombine/RISCV/lshr-trunc-sext-to-ashr-sext.ll b/llvm/test/Transforms/InstCombine/RISCV/lshr-trunc-sext-to-ashr-sext.ll
new file mode 100644
index 00000000000000..09d35c2cfa2c3b
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/RISCV/lshr-trunc-sext-to-ashr-sext.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -p instcombine -mtriple riscv64 -S | FileCheck %s
+
+define i64 @t0(i32 %x) {
+; CHECK-LABEL: define i64 @t0(
+; CHECK-SAME: i32 [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X]], 16
+; CHECK-NEXT: [[C:%.*]] = sext i32 [[TMP1]] to i64
+; CHECK-NEXT: ret i64 [[C]]
+;
+ %a = lshr i32 %x, 16
+ %b = trunc i32 %a to i16
+ %c = sext i16 %b to i64
+ ret i64 %c
+}
+
+define i32 @t1(i16 %x) {
+; CHECK-LABEL: define i32 @t1(
+; CHECK-SAME: i16 [[X:%.*]]) {
+; CHECK-NEXT: [[S:%.*]] = sext i16 [[X]] to i32
+; CHECK-NEXT: [[Y:%.*]] = shl nsw i32 [[S]], 10
+; CHECK-NEXT: [[TMP1:%.*]] = add nsw i32 [[Y]], 32768
+; CHECK-NEXT: [[C:%.*]] = ashr i32 [[TMP1]], 16
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %s = sext i16 %x to i32
+ %y = add i32 %s, 32
+ %a = ashr i32 %y, 6
+ %b = trunc i32 %a to i16
+ %c = sext i16 %b to i32
+ ret i32 %c
+}
diff --git a/llvm/test/Transforms/InstCombine/X86/lshr-trunc-sext-to-ashr-sext.ll b/llvm/test/Transforms/InstCombine/X86/lshr-trunc-sext-to-ashr-sext.ll
new file mode 100644
index 00000000000000..852b6bc0daefea
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/X86/lshr-trunc-sext-to-ashr-sext.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -p instcombine -mtriple x86_64 -S | FileCheck %s
+
+define i64 @t0(i32 %x) {
+; CHECK-LABEL: define i64 @t0(
+; CHECK-SAME: i32 [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X]], 16
+; CHECK-NEXT: [[C:%.*]] = sext i32 [[TMP1]] to i64
+; CHECK-NEXT: ret i64 [[C]]
+;
+ %a = lshr i32 %x, 16
+ %b = trunc i32 %a to i16
+ %c = sext i16 %b to i64
+ ret i64 %c
+}
+
+define i32 @t1(i16 %x) {
+; CHECK-LABEL: define i32 @t1(
+; CHECK-SAME: i16 [[X:%.*]]) {
+; CHECK-NEXT: [[S:%.*]] = sext i16 [[X]] to i32
+; CHECK-NEXT: [[Y:%.*]] = shl nsw i32 [[S]], 10
+; CHECK-NEXT: [[TMP1:%.*]] = add nsw i32 [[Y]], 32768
+; CHECK-NEXT: [[C:%.*]] = ashr i32 [[TMP1]], 16
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %s = sext i16 %x to i32
+ %y = add i32 %s, 32
+ %a = ashr i32 %y, 6
+ %b = trunc i32 %a to i16
+ %c = sext i16 %b to i32
+ ret i32 %c
+}
More information about the llvm-commits
mailing list