Skip to content

Commit a0f4ac6

Browse files
committed
Rework to adjust m_ImmConstant
1 parent 25172e1 commit a0f4ac6

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -858,18 +858,51 @@ inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
858858
return V;
859859
}
860860

861+
// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
862+
// and use m_Unless(m_ConstantExpr).
863+
struct immconstant_ty {
864+
template <typename ITy> static bool isImmConstant(ITy *V) {
865+
if (auto *CV = dyn_cast<Constant>(V)) {
866+
if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
867+
return true;
868+
869+
if (CV->getType()->isVectorTy()) {
870+
if (auto *Splat = CV->getSplatValue(/* AllowPoison */ true)) {
871+
if (!isa<ConstantExpr>(Splat) &&
872+
!Splat->containsConstantExpression()) {
873+
return true;
874+
}
875+
}
876+
}
877+
}
878+
return false;
879+
}
880+
};
881+
882+
struct match_immconstant_ty : immconstant_ty {
883+
template <typename ITy> bool match(ITy *V) { return isImmConstant(V); }
884+
};
885+
861886
/// Match an arbitrary immediate Constant and ignore it.
862-
inline match_combine_and<class_match<Constant>,
863-
match_unless<constantexpr_match>>
864-
m_ImmConstant() {
865-
return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr()));
866-
}
887+
inline match_immconstant_ty m_ImmConstant() { return match_immconstant_ty(); }
888+
889+
struct bind_immconstant_ty : immconstant_ty {
890+
Constant *&VR;
891+
892+
bind_immconstant_ty(Constant *&V) : VR(V) {}
893+
894+
template <typename ITy> bool match(ITy *V) {
895+
if (isImmConstant(V)) {
896+
VR = cast<Constant>(V);
897+
return true;
898+
}
899+
return false;
900+
}
901+
};
867902

868903
/// Match an immediate Constant, capturing the value if we match.
869-
inline match_combine_and<bind_ty<Constant>,
870-
match_unless<constantexpr_match>>
871-
m_ImmConstant(Constant *&C) {
872-
return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr()));
904+
inline bind_immconstant_ty m_ImmConstant(Constant *&C) {
905+
return bind_immconstant_ty(C);
873906
}
874907

875908
/// Match a specified Value*.

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,7 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
428428
return R;
429429

430430
Constant *CUI;
431-
if (match(Op1, m_Constant(CUI)) &&
432-
(!isa<ConstantExpr>(CUI) ||
433-
(Ty->isVectorTy() &&
434-
isa_and_present<ConstantInt>(CUI->getSplatValue()))))
431+
if (match(Op1, m_ImmConstant(CUI)))
435432
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
436433
return Res;
437434

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,8 +3519,7 @@ define <vscale x 2 x i32> @scalable_sign_bits(<vscale x 2 x i8> %x) {
35193519

35203520
define <vscale x 2 x i1> @scalable_non_zero(<vscale x 2 x i32> %x) {
35213521
; CHECK-LABEL: @scalable_non_zero(
3522-
; CHECK-NEXT: [[A:%.*]] = or <vscale x 2 x i32> [[X:%.*]], splat (i32 1)
3523-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <vscale x 2 x i32> [[A]], splat (i32 57)
3522+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <vscale x 2 x i32> [[X:%.*]], splat (i32 56)
35243523
; CHECK-NEXT: ret <vscale x 2 x i1> [[CMP]]
35253524
;
35263525
%a = or <vscale x 2 x i32> %x, splat (i32 1)

llvm/test/Transforms/InstCombine/sub.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ define <2 x i16> @test44vecminval(<2 x i16> %x) {
861861
; uses m_ImmConstant which matches Constant but (explicitly) not ConstantExpr.
862862
define <vscale x 2 x i16> @test44scalablevecminval(<vscale x 2 x i16> %x) {
863863
; CHECK-LABEL: @test44scalablevecminval(
864-
; CHECK-NEXT: [[SUB:%.*]] = add <vscale x 2 x i16> [[X:%.*]], splat (i16 -32768)
864+
; CHECK-NEXT: [[SUB:%.*]] = xor <vscale x 2 x i16> [[X:%.*]], splat (i16 -32768)
865865
; CHECK-NEXT: ret <vscale x 2 x i16> [[SUB]]
866866
;
867867
%sub = sub nsw <vscale x 2 x i16> %x, splat (i16 -32768)

0 commit comments

Comments
 (0)