[llvm] [InstCombine] ptrmask of gep for dynamic pointer aligment (PR #80002)

Jon Chesterfield via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 12 14:51:55 PST 2024


================
@@ -966,6 +966,41 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
                 I, 1, (DemandedMask & ~LHSKnown.Zero).zextOrTrunc(MaskWidth)))
           return I;
 
+        // Combine:
+        // (ptrmask (getelementptr i8, ptr p, imm i), imm mask)
+        //   -> (ptrmask (getelementptr i8, ptr p, imm (i & mask)), imm mask)
+        // where only the low bits known to be zero in the pointer are changed
+        Value *InnerPtr;
+        uint64_t GEPIndex;
+        uint64_t PtrMaskImmediate;
+        if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
+                         m_GEP(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
+                         m_ConstantInt(PtrMaskImmediate)))) {
+          auto *GEP = cast<GetElementPtrInst>(II->getArgOperand(0));
+
+          if (GEP->getSourceElementType() == Type::getInt8Ty(I->getContext())) {
+            unsigned Log2Align = llvm::Log2(InnerPtr->getPointerAlignment(DL));
----------------
JonChesterfield wrote:

This confused me - LHSKnown refers to the first argument to ptrmask, but it's the argument to the gep that we want. Current patch avoids overwriting the LHSKnown variable, though it looks like that's a mutable local which doesn't particularly need to correspond to `I->getOperand(0)`

https://github.com/llvm/llvm-project/pull/80002


More information about the llvm-commits mailing list