Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,17 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
}

// Try to extend the entire expression tree to the wide destination type.
if (shouldChangeType(SrcTy, DestTy) && canEvaluateSExtd(Src, DestTy)) {
bool shouldExtendExpression = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool shouldExtendExpression = true;
bool ShouldExtendExpression = true;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Value *X = nullptr;
// Do not extend expression in the trunc + sext pattern when destination type
// is narrower than original (pre-trunc) type: modern architectures typically
// provide efficient sign-extend instruction, so preserving the sext is
// preferable here.
if (match(Src, m_Trunc(m_Value(X))))
if (X->getType()->getScalarSizeInBits() > DestBitSize)
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable X is being reused for two different purposes. In the first block (lines 1532-1539), it's used to check the transformation condition, while later at line 1561 it's used to continue the existing trunc optimization. This creates unclear control flow since X's value depends on whether the match succeeded earlier. Consider using a separate variable name for the first check to make the code more readable.

Suggested change
Value *X = nullptr;
// Do not extend expression in the trunc + sext pattern when destination type
// is narrower than original (pre-trunc) type: modern architectures typically
// provide efficient sign-extend instruction, so preserving the sext is
// preferable here.
if (match(Src, m_Trunc(m_Value(X))))
if (X->getType()->getScalarSizeInBits() > DestBitSize)
Value *TruncSrc = nullptr;
// Do not extend expression in the trunc + sext pattern when destination type
// is narrower than original (pre-trunc) type: modern architectures typically
// provide efficient sign-extend instruction, so preserving the sext is
// preferable here.
if (match(Src, m_Trunc(m_Value(TruncSrc))))
if (TruncSrc->getType()->getScalarSizeInBits() > DestBitSize)

Copilot uses AI. Check for mistakes.
shouldExtendExpression = false;
if (shouldExtendExpression && shouldChangeType(SrcTy, DestTy) &&
canEvaluateSExtd(Src, DestTy)) {
// Okay, we can transform this! Insert the new expression now.
LLVM_DEBUG(
dbgs() << "ICE: EvaluateInDifferentType converting expression type"
Expand All @@ -1548,8 +1558,7 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
ShAmt);
}

Value *X;
if (match(Src, m_Trunc(m_Value(X)))) {
if (X) {
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable X is being reused for two different purposes. In the first block (lines 1532-1539), it's used to check the transformation condition, while later at line 1561 it's used to continue the existing trunc optimization. This creates unclear control flow since X's value depends on whether the match succeeded earlier. Consider using a separate variable name for the first check to make the code more readable.

Copilot uses AI. Check for mistakes.
// If the input has more sign bits than bits truncated, then convert
// directly to final type.
unsigned XBitSize = X->getType()->getScalarSizeInBits();
Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Transforms/InstCombine/sext.ll
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ define i10 @test19(i10 %i) {
ret i10 %d
}

define i32 @test20(i64 %i) {
; CHECK-LABEL: @test20(
; CHECK-NEXT: [[A:%.*]] = trunc i64 [[I:%.*]] to i16
; CHECK-NEXT: [[B:%.*]] = sext i16 [[A]] to i32
; CHECK-NEXT: ret i32 [[B]]
;
%a = trunc i64 %i to i16
%b = sext i16 %a to i32
ret i32 %b
}

define i32 @smear_set_bit(i32 %x) {
; CHECK-LABEL: @smear_set_bit(
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[X:%.*]], 24
Expand Down
Loading