From 8347ca7dc81adf16400306b4fc0702f62e69acd7 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 22 Feb 2023 17:13:00 +0100 Subject: [PATCH] [PatternMatch] Don't require DataLayout for m_VScale() The m_VScale() matcher is unusual in that it requires a DataLayout. It is currently used to determine the size of the GEP type. However, I believe it is sufficient to check for the canonical form here -- I don't think there's a need to recognize exotic variations like as a vscale constant representation as well. Differential Revision: https://reviews.llvm.org/D144566 --- llvm/include/llvm/IR/PatternMatch.h | 16 +++++++--------- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp | 3 +-- llvm/lib/IR/IntrinsicInst.cpp | 10 ++-------- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 6 +++--- llvm/unittests/IR/PatternMatch.cpp | 4 ++-- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h index 8b542b7..32443b0 100644 --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -2476,9 +2476,6 @@ inline InsertValue_match m_InsertValue(const Val_t &Val, /// `ptrtoint(gep , * null, i32 1>` /// under the right conditions determined by DataLayout. struct VScaleVal_match { - const DataLayout &DL; - VScaleVal_match(const DataLayout &DL) : DL(DL) {} - template bool match(ITy *V) { if (m_Intrinsic().match(V)) return true; @@ -2486,11 +2483,12 @@ struct VScaleVal_match { Value *Ptr; if (m_PtrToInt(m_Value(Ptr)).match(V)) { if (auto *GEP = dyn_cast(Ptr)) { - auto *DerefTy = GEP->getSourceElementType(); - if (GEP->getNumIndices() == 1 && isa(DerefTy) && + auto *DerefTy = + dyn_cast(GEP->getSourceElementType()); + if (GEP->getNumIndices() == 1 && DerefTy && + DerefTy->getElementType()->isIntegerTy(8) && m_Zero().match(GEP->getPointerOperand()) && - m_SpecificInt(1).match(GEP->idx_begin()->get()) && - DL.getTypeAllocSizeInBits(DerefTy).getKnownMinValue() == 8) + m_SpecificInt(1).match(GEP->idx_begin()->get())) return true; } } @@ -2499,8 +2497,8 @@ struct VScaleVal_match { } }; -inline VScaleVal_match m_VScale(const DataLayout &DL) { - return VScaleVal_match(DL); +inline VScaleVal_match m_VScale() { + return VScaleVal_match(); } template diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 9d5db58..90dcf7e 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -1607,7 +1607,7 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { TLI.getPointerTy(DAG.getDataLayout(), AS)); } - if (match(C, m_VScale(DAG.getDataLayout()))) + if (match(C, m_VScale())) return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1)); if (const ConstantFP *CFP = dyn_cast(C)) @@ -5868,7 +5868,6 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, visitTargetIntrinsic(I, Intrinsic); return; case Intrinsic::vscale: { - match(&I, m_VScale(DAG.getDataLayout())); EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1))); return; diff --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp index b258e7b..0bafa5f 100644 --- a/llvm/lib/IR/IntrinsicInst.cpp +++ b/llvm/lib/IR/IntrinsicInst.cpp @@ -554,17 +554,11 @@ bool VPIntrinsic::canIgnoreVectorLengthParam() const { // Check whether "W == vscale * EC.getKnownMinValue()" if (EC.isScalable()) { - // Undig the DL - const auto *ParMod = this->getModule(); - if (!ParMod) - return false; - const auto &DL = ParMod->getDataLayout(); - // Compare vscale patterns uint64_t VScaleFactor; - if (match(VLParam, m_c_Mul(m_ConstantInt(VScaleFactor), m_VScale(DL)))) + if (match(VLParam, m_c_Mul(m_ConstantInt(VScaleFactor), m_VScale()))) return VScaleFactor >= EC.getKnownMinValue(); - return (EC.getKnownMinValue() == 1) && match(VLParam, m_VScale(DL)); + return (EC.getKnownMinValue() == 1) && match(VLParam, m_VScale()); } // standard SIMD operation diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index d723b8b..7e53540 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -1012,7 +1012,7 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) { } } - if (match(Src, m_VScale(DL))) { + if (match(Src, m_VScale())) { if (Trunc.getFunction() && Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { Attribute Attr = @@ -1361,7 +1361,7 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) { return BinaryOperator::CreateAnd(X, ZextC); } - if (match(Src, m_VScale(DL))) { + if (match(Src, m_VScale())) { if (Zext.getFunction() && Zext.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { Attribute Attr = @@ -1632,7 +1632,7 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) { } } - if (match(Src, m_VScale(DL))) { + if (match(Src, m_VScale())) { if (Sext.getFunction() && Sext.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { Attribute Attr = diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp index 6c03951..3e37bcb 100644 --- a/llvm/unittests/IR/PatternMatch.cpp +++ b/llvm/unittests/IR/PatternMatch.cpp @@ -1760,7 +1760,7 @@ TEST_F(PatternMatchTest, VScale) { Value *NullPtrVec = Constant::getNullValue(VecPtrTy); Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1)); Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType())); - EXPECT_TRUE(match(PtrToInt, m_VScale(DL))); + EXPECT_TRUE(match(PtrToInt, m_VScale())); // This used to cause assertion failures when attempting to match m_VScale. // With opaque pointers the bitcast is no longer present. @@ -1770,7 +1770,7 @@ TEST_F(PatternMatchTest, VScale) { Value *GEP2 = IRB.CreateGEP(VecTy, BitCast, IRB.getInt64(1)); Value *PtrToInt2 = IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType())); - EXPECT_TRUE(match(PtrToInt2, m_VScale(DL))); + EXPECT_TRUE(match(PtrToInt2, m_VScale())); } TEST_F(PatternMatchTest, NotForbidUndef) { -- 2.7.4