Unverified Commit 01aefba7 authored by Florian Hahn's avatar Florian Hahn Committed by GitHub
Browse files

[VPlan] Get GEP wrap flags from VPInstructions (NFCI). (#195730)

Add helper to retrieve GEP no-wrap flags from VPInstructions, looking
through zero-index GEPs and pointer casts, like
Value::stripPointerCasts. Removes an access to underlying IR.
parent 9792c778
Loading
Loading
Loading
Loading
+6 −10
Original line number Diff line number Diff line
@@ -6423,25 +6423,21 @@ VPRecipeBase *VPRecipeBuilder::tryToWidenMemory(VPInstruction *VPI,
  VPValue *Ptr = VPI->getOpcode() == Instruction::Load ? VPI->getOperand(0)
                                                       : VPI->getOperand(1);
  if (Consecutive) {
    auto *GEP = dyn_cast<GetElementPtrInst>(
        Ptr->getUnderlyingValue()->stripPointerCasts());
    GEPNoWrapFlags Flags = vputils::getGEPFlagsForPtr(Ptr);
    VPSingleDefRecipe *VectorPtr;
    if (Reverse) {
      // When folding the tail, we may compute an address that we don't in the
      // original scalar loop: drop the GEP no-wrap flags in this case.
      // Otherwise preserve existing flags without no-unsigned-wrap, as we will
      // emit negative indices.
      GEPNoWrapFlags Flags =
          CM.foldTailByMasking() || !GEP
      GEPNoWrapFlags ReverseFlags = CM.foldTailByMasking()
                                        ? GEPNoWrapFlags::none()
              : GEP->getNoWrapFlags().withoutNoUnsignedWrap();
                                        : Flags.withoutNoUnsignedWrap();
      VectorPtr = new VPVectorEndPointerRecipe(
          Ptr, &Plan.getVF(), getLoadStoreType(I),
          /*Stride*/ -1, Flags, VPI->getDebugLoc());
          /*Stride*/ -1, ReverseFlags, VPI->getDebugLoc());
    } else {
      VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I),
                                            GEP ? GEP->getNoWrapFlags()
                                                : GEPNoWrapFlags::none(),
      VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I), Flags,
                                            VPI->getDebugLoc());
    }
    Builder.setInsertPoint(VPI);
+19 −0
Original line number Diff line number Diff line
@@ -138,6 +138,25 @@ static bool poisonGuaranteesUB(const VPValue *V) {
  return false;
}

GEPNoWrapFlags vputils::getGEPFlagsForPtr(VPValue *Ptr) {
  // Like IR stripPointerCasts, look through GEPs with all-zero indices and
  // casts to find a root GEP VPInstruction.
  while (auto *PtrVPI = dyn_cast<VPInstruction>(Ptr)) {
    unsigned Opcode = PtrVPI->getOpcode();
    if (Opcode == Instruction::GetElementPtr) {
      if (any_of(drop_begin(PtrVPI->operands()),
                 [](VPValue *Op) { return !match(Op, m_ZeroInt()); }))
        return PtrVPI->getGEPNoWrapFlags();
      Ptr = PtrVPI->getOperand(0);
      continue;
    }
    if (Opcode != Instruction::BitCast && Opcode != Instruction::AddrSpaceCast)
      break;
    Ptr = PtrVPI->getOperand(0);
  }
  return GEPNoWrapFlags::none();
}

const SCEV *vputils::getSCEVExprForVPValue(const VPValue *V,
                                           PredicatedScalarEvolution &PSE,
                                           const Loop *L) {
+4 −0
Original line number Diff line number Diff line
@@ -155,6 +155,10 @@ template <typename RecipeTy> static RecipeTy *findUserOf(VPValue *V) {
/// nullptr if not found.
VPInstruction *findCanonicalIVIncrement(VPlan &Plan);

/// Returns the GEP nowrap flags for \p Ptr, looking through pointer casts
/// mirroring Value::stripPointerCasts.
GEPNoWrapFlags getGEPFlagsForPtr(VPValue *Ptr);

/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
/// inserted for predicated reductions or tail folding.
VPInstruction *findComputeReductionResult(VPReductionPHIRecipe *PhiR);