Skip to content

Commit 7b7d698

Browse files
committed
Apply transformations
1 parent b5893dc commit 7b7d698

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/ConstantRange.h"
2525
#include "llvm/IR/DataLayout.h"
2626
#include "llvm/IR/InstrTypes.h"
27+
#include "llvm/IR/Instructions.h"
2728
#include "llvm/IR/IntrinsicInst.h"
2829
#include "llvm/IR/PatternMatch.h"
2930
#include "llvm/Support/KnownBits.h"
@@ -7687,6 +7688,29 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
76877688
if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
76887689
return Res;
76897690

7691+
{
7692+
Value *A, *B;
7693+
const APInt *C;
7694+
ICmpInst::Predicate PredEq = ICmpInst::ICMP_EQ;
7695+
if (I.getPredicate() == PredEq) {
7696+
if (match(Op0, m_And(m_Value(A), m_APInt(C))) && match(Op1, m_One())) {
7697+
if (*C == APInt::getSignedMinValue(C->getBitWidth()) + 1) {
7698+
if (match(A, m_SExt(m_Value(B))) &&
7699+
B->getType()->getIntegerBitWidth() > 2) {
7700+
auto *InputTy = B->getType();
7701+
Value *AndInst = Builder.CreateAnd(
7702+
B,
7703+
ConstantInt::get(InputTy, APInt::getSignedMinValue(
7704+
InputTy->getIntegerBitWidth()) +
7705+
1));
7706+
return CmpInst::Create(Instruction::ICmp, PredEq, AndInst,
7707+
ConstantInt::get(InputTy, 1));
7708+
}
7709+
}
7710+
}
7711+
}
7712+
}
7713+
76907714
return Changed ? &I : nullptr;
76917715
}
76927716

llvm/test/Transforms/InstCombine/sext-and.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ declare void @use(i8)
66
define i1 @fold_sext_to_and(i8 %x) {
77
; CHECK-LABEL: define i1 @fold_sext_to_and(
88
; CHECK-SAME: i8 [[X:%.*]]) {
9-
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X]] to i32
10-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
11-
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
9+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], -127
10+
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1
1211
; CHECK-NEXT: ret i1 [[TMP3]]
1312
;
1413
%1 = sext i8 %x to i32
@@ -22,8 +21,8 @@ define i1 @fold_sext_to_and_multi_use(i8 %x) {
2221
; CHECK-SAME: i8 [[X:%.*]]) {
2322
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X]] to i32
2423
; CHECK-NEXT: call void @use(i32 [[TMP1]])
25-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
26-
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
24+
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X]], -127
25+
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP2]], 1
2726
; CHECK-NEXT: ret i1 [[TMP3]]
2827
;
2928
%1 = sext i8 %x to i32

0 commit comments

Comments
 (0)