diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -485,6 +485,9 @@ /// value into a vector. Value *getOrCreateVectorValue(Value *V, unsigned Part); + void setVectorValue(Value *Scalar, unsigned Part, Value *Vector) { + VectorLoopValueMap.setVectorValue(Scalar, Part, Vector); + } /// Return a value in the new loop corresponding to \p V from the original /// loop at unroll and vector indices \p Instance. If the value has been /// vectorized but not scalarized, the necessary extractelement instruction @@ -507,8 +510,9 @@ /// non-null. Use \p State to translate given VPValues to IR values in the /// vectorized loop. void vectorizeMemoryInstruction(Instruction *Instr, VPTransformState &State, - VPValue *Addr, VPValue *StoredValue, - VPValue *BlockInMask); + + VPValue *Def, VPValue *Addr, + VPValue *StoredValue, VPValue *BlockInMask); /// Set the debug location in the builder using the debug location in /// the instruction. @@ -2383,11 +2387,9 @@ } } -void InnerLoopVectorizer::vectorizeMemoryInstruction(Instruction *Instr, - VPTransformState &State, - VPValue *Addr, - VPValue *StoredValue, - VPValue *BlockInMask) { +void InnerLoopVectorizer::vectorizeMemoryInstruction( + Instruction *Instr, VPTransformState &State, VPValue *Def, VPValue *Addr, + VPValue *StoredValue, VPValue *BlockInMask) { // Attempt to issue a wide load. LoadInst *LI = dyn_cast(Instr); StoreInst *SI = dyn_cast(Instr); @@ -2514,7 +2516,8 @@ if (Reverse) NewLI = reverseVector(NewLI); } - VectorLoopValueMap.setVectorValue(Instr, Part, NewLI); + + State.set(Def, Instr, NewLI, Part); } } @@ -6950,8 +6953,11 @@ Mask = createBlockInMask(I->getParent(), Plan); VPValue *Addr = Plan->getOrAddVPValue(getLoadStorePointerOperand(I)); - if (LoadInst *Load = dyn_cast(I)) - return new VPWidenMemoryInstructionRecipe(*Load, Addr, Mask); + if (LoadInst *Load = dyn_cast(I)) { + auto *WidenLoad = new VPWidenMemoryInstructionRecipe(*Load, Addr, Mask); + Plan->addVPValue(Load, WidenLoad); + return WidenLoad; + } StoreInst *Store = cast(I); VPValue *StoredValue = Plan->getOrAddVPValue(Store->getValueOperand()); @@ -7659,9 +7665,11 @@ } void VPWidenMemoryInstructionRecipe::execute(VPTransformState &State) { + Instruction *Instr = getUnderlyingInstruction(); VPValue *StoredValue = isa(Instr) ? getStoredValue() : nullptr; - State.ILV->vectorizeMemoryInstruction(&Instr, State, getAddr(), StoredValue, - getMask()); + State.ILV->vectorizeMemoryInstruction(Instr, State, + StoredValue ? nullptr : this, getAddr(), + StoredValue, getMask()); } // Determine how to lower the scalar epilogue, which depends on 1) optimising @@ -7702,6 +7710,12 @@ return CM_ScalarEpilogueAllowed; } +void VPTransformState::set(VPValue *Def, Value *IRDef, Value *V, + unsigned Part) { + set(Def, V, Part); + ILV->setVectorValue(IRDef, Part, V); +} + // Process the loop in the VPlan-native vectorization path. This path builds // VPlan upfront in the vectorization pipeline, which allows to apply // VPlan-to-VPlan transformations from the very beginning without modifying the diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h --- a/llvm/lib/Transforms/Vectorize/VPlan.h +++ b/llvm/lib/Transforms/Vectorize/VPlan.h @@ -279,6 +279,10 @@ // delegates the call to ILV below. if (Data.PerPartOutput.count(Def)) { auto *VecPart = Data.PerPartOutput[Def][Instance.Part]; + if (!VecPart->getType()->isVectorTy()) { + assert(Instance.Lane == 0 && "cannot get lane > 0 for scalar"); + return VecPart; + } // TODO: Cache created scalar values. return Builder.CreateExtractElement(VecPart, Builder.getInt32(Instance.Lane)); @@ -295,6 +299,7 @@ } Data.PerPartOutput[Def][Part] = V; } + void set(VPValue *Def, Value *IRDef, Value *V, unsigned Part); /// Hold state information used when constructing the CFG of the output IR, /// traversing the VPBasicBlocks and generating corresponding IR BasicBlocks. @@ -1158,8 +1163,7 @@ /// - For store: Address, stored value, optional mask /// TODO: We currently execute only per-part unless a specific instance is /// provided. -class VPWidenMemoryInstructionRecipe : public VPRecipeBase { - Instruction &Instr; +class VPWidenMemoryInstructionRecipe : public VPRecipeBase, public VPValue { VPUser User; void setMask(VPValue *Mask) { @@ -1169,19 +1173,23 @@ } bool isMasked() const { - return (isa(Instr) && User.getNumOperands() == 2) || - (isa(Instr) && User.getNumOperands() == 3); + return (isa(getUnderlyingInstruction()) && + User.getNumOperands() == 2) || + (isa(getUnderlyingInstruction()) && + User.getNumOperands() == 3); } public: VPWidenMemoryInstructionRecipe(LoadInst &Load, VPValue *Addr, VPValue *Mask) - : VPRecipeBase(VPWidenMemoryInstructionSC), Instr(Load), User({Addr}) { + : VPRecipeBase(VPWidenMemoryInstructionSC), + VPValue(VPValue::VPMemoryInstructionSC, &Load), User({Addr}) { setMask(Mask); } VPWidenMemoryInstructionRecipe(StoreInst &Store, VPValue *Addr, VPValue *StoredValue, VPValue *Mask) - : VPRecipeBase(VPWidenMemoryInstructionSC), Instr(Store), + : VPRecipeBase(VPWidenMemoryInstructionSC), + VPValue(VPValue::VPMemoryInstructionSC, &Store), User({Addr, StoredValue}) { setMask(Mask); } @@ -1205,7 +1213,7 @@ /// Return the address accessed by this recipe. VPValue *getStoredValue() const { - assert(isa(Instr) && + assert(isa(getUnderlyingInstruction()) && "Stored value only available for store instructions"); return User.getOperand(1); // Stored value is the 2nd, mandatory operand. } @@ -1216,6 +1224,13 @@ /// Print the recipe. void print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override; + + Instruction *getUnderlyingInstruction() { + return cast(getUnderlyingValue()); + } + const Instruction *getUnderlyingInstruction() const { + return cast(getUnderlyingValue()); + } }; /// A Recipe for widening the canonical induction variable of the vector loop. @@ -1564,6 +1579,8 @@ /// VPlan. Value2VPValueTy Value2VPValue; + SmallVector VPValuesToFree; + /// Holds the VPLoopInfo analysis for this VPlan. VPLoopInfo VPLInfo; @@ -1579,8 +1596,8 @@ ~VPlan() { if (Entry) VPBlockBase::deleteCFG(Entry); - for (auto &MapEntry : Value2VPValue) - delete MapEntry.second; + for (VPValue *VPV : VPValuesToFree) + delete VPV; if (BackedgeTakenCount) delete BackedgeTakenCount; for (VPValue *Def : VPExternalDefs) @@ -1630,7 +1647,15 @@ void addVPValue(Value *V) { assert(V && "Trying to add a null Value to VPlan"); assert(!Value2VPValue.count(V) && "Value already exists in VPlan"); - Value2VPValue[V] = new VPValue(V); + VPValue *VPV = new VPValue(V); + Value2VPValue[V] = VPV; + VPValuesToFree.push_back(VPV); + } + + void addVPValue(Value *V, VPValue *VPV) { + assert(V && "Trying to add a null Value to VPlan"); + assert(!Value2VPValue.count(V) && "Value already exists in VPlan"); + Value2VPValue[V] = VPV; } VPValue *getVPValue(Value *V) { @@ -1723,13 +1748,13 @@ void dump(); - static void printAsIngredient(raw_ostream &O, Value *V); + static void printAsIngredient(raw_ostream &O, const Value *V); }; struct VPlanIngredient { - Value *V; + const Value *V; - VPlanIngredient(Value *V) : V(V) {} + VPlanIngredient(const Value *V) : V(V) {} }; inline raw_ostream &operator<<(raw_ostream &OS, const VPlanIngredient &I) { diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp --- a/llvm/lib/Transforms/Vectorize/VPlan.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp @@ -715,7 +715,7 @@ dumpEdges(Region); } -void VPlanPrinter::printAsIngredient(raw_ostream &O, Value *V) { +void VPlanPrinter::printAsIngredient(raw_ostream &O, const Value *V) { std::string IngredientString; raw_string_ostream RSO(IngredientString); if (auto *Inst = dyn_cast(V)) { @@ -815,7 +815,7 @@ void VPWidenMemoryInstructionRecipe::print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const { - O << "\"WIDEN " << VPlanIngredient(&Instr); + O << "\"WIDEN " << VPlanIngredient(getUnderlyingInstruction()); O << ", "; getAddr()->printAsOperand(O, SlotTracker); VPValue *Mask = getMask(); diff --git a/llvm/lib/Transforms/Vectorize/VPlanValue.h b/llvm/lib/Transforms/Vectorize/VPlanValue.h --- a/llvm/lib/Transforms/Vectorize/VPlanValue.h +++ b/llvm/lib/Transforms/Vectorize/VPlanValue.h @@ -76,7 +76,7 @@ /// are actually instantiated. Values of this enumeration are kept in the /// SubclassID field of the VPValue objects. They are used for concrete /// type identification. - enum { VPValueSC, VPInstructionSC }; + enum { VPValueSC, VPInstructionSC, VPMemoryInstructionSC }; VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {} VPValue(const VPValue &) = delete;