Skip to content

Commit c15f9c0

Browse files
nikicakiramenai
authored andcommitted
[InstCombine] Fix incorrect zero ext in select of lshr/ashr fold
The -1 constant should be sign extended, not zero extended. Split out from llvm/llvm-project#80309.
1 parent 81454f0 commit c15f9c0

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,11 @@ static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
681681
Value *X, *Y;
682682
unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits();
683683
if ((Pred != ICmpInst::ICMP_SGT ||
684-
!match(CmpRHS,
685-
m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) &&
684+
!match(CmpRHS, m_SpecificInt_ICMP(ICmpInst::ICMP_SGE,
685+
APInt::getAllOnes(Bitwidth)))) &&
686686
(Pred != ICmpInst::ICMP_SLT ||
687-
!match(CmpRHS,
688-
m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, 0)))))
687+
!match(CmpRHS, m_SpecificInt_ICMP(ICmpInst::ICMP_SGE,
688+
APInt::getZero(Bitwidth)))))
689689
return nullptr;
690690

691691
// Canonicalize so that ashr is in FalseVal.

llvm/test/Transforms/InstCombine/ashr-lshr.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@ define i32 @ashr_lshr2(i32 %x, i32 %y) {
6363

6464
define i128 @ashr_lshr2_i128(i128 %x, i128 %y) {
6565
; CHECK-LABEL: @ashr_lshr2_i128(
66-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i128 [[X:%.*]], 5
67-
; CHECK-NEXT: [[L:%.*]] = lshr i128 [[X]], [[Y:%.*]]
68-
; CHECK-NEXT: [[R:%.*]] = ashr exact i128 [[X]], [[Y]]
69-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[CMP]], i128 [[L]], i128 [[R]]
70-
; CHECK-NEXT: ret i128 [[RET]]
66+
; CHECK-NEXT: [[CMP1:%.*]] = ashr i128 [[X:%.*]], [[Y:%.*]]
67+
; CHECK-NEXT: ret i128 [[CMP1]]
7168
;
7269
%cmp = icmp sgt i128 %x, 5
7370
%l = lshr i128 %x, %y

0 commit comments

Comments
 (0)