Index: include/llvm/Analysis/VectorUtils.h =================================================================== --- include/llvm/Analysis/VectorUtils.h +++ include/llvm/Analysis/VectorUtils.h @@ -113,7 +113,10 @@ computeMinimumValueSizes(ArrayRef Blocks, DemandedBits &DB, const TargetTransformInfo *TTI=nullptr); - + +/// \returns \p I after propagating metadata from \p VL. +Instruction *propagateMetadata(Instruction *I, ArrayRef VL); + } // llvm namespace #endif Index: lib/Analysis/VectorUtils.cpp =================================================================== --- lib/Analysis/VectorUtils.cpp +++ lib/Analysis/VectorUtils.cpp @@ -447,3 +447,45 @@ return MinBWs; } + +/// \returns \p I after propagating metadata from \p VL. +Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef VL) { + Instruction *I0 = cast(VL[0]); + SmallVector, 4> Metadata; + I0->getAllMetadataOtherThanDebugLoc(Metadata); + + for (unsigned I = 0, N = Metadata.size(); I != N; ++I) { + unsigned Kind = Metadata[I].first; + MDNode *MD = Metadata[I].second; + + for (int J = 1, E = VL.size(); MD && J != E; ++J) { + const Instruction *IJ = cast(VL[J]); + MDNode *IMD = IJ->getMetadata(Kind); + + switch (Kind) { + case LLVMContext::MD_tbaa: + MD = MDNode::getMostGenericTBAA(MD, IMD); + break; + case LLVMContext::MD_alias_scope: + MD = MDNode::getMostGenericAliasScope(MD, IMD); + break; + case LLVMContext::MD_noalias: + MD = MDNode::intersect(MD, IMD); + break; + case LLVMContext::MD_fpmath: + MD = MDNode::getMostGenericFPMath(MD, IMD); + break; + case LLVMContext::MD_nontemporal: + MD = MDNode::intersect(MD, IMD); + break; + default: + MD = nullptr; // Remove unknown metadata. + break; + } + } + + Inst->setMetadata(Kind, MD); + } + + return Inst; +} Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -472,11 +472,11 @@ /// This includes both the original MDs from \p From and additional ones (\see /// addNewMetadata). Use this for *newly created* instructions in the vector /// loop. - void addMetadata(Instruction *To, const Instruction *From); + void addMetadata(Instruction *To, Instruction *From); /// \brief Similar to the previous function but it adds the metadata to a /// vector of instructions. - void addMetadata(SmallVectorImpl &To, const Instruction *From); + void addMetadata(ArrayRef To, Instruction *From); /// This is a helper class that holds the vectorizer state. It maps scalar /// instructions to vector instructions. When the code is 'unrolled' then @@ -663,28 +663,6 @@ } #endif -/// \brief Propagate known metadata from one instruction to another. -static void propagateMetadata(Instruction *To, const Instruction *From) { - SmallVector, 4> Metadata; - From->getAllMetadataOtherThanDebugLoc(Metadata); - - for (auto M : Metadata) { - unsigned Kind = M.first; - - // These are safe to transfer (this is safe for TBAA, even when we - // if-convert, because should that metadata have had a control dependency - // on the condition, and thus actually aliased with some other - // non-speculated memory access when the condition was false, this would be - // caught by the runtime overlap checks). - if (Kind != LLVMContext::MD_tbaa && Kind != LLVMContext::MD_alias_scope && - Kind != LLVMContext::MD_noalias && Kind != LLVMContext::MD_fpmath && - Kind != LLVMContext::MD_nontemporal) - continue; - - To->setMetadata(Kind, M.second); - } -} - void InnerLoopVectorizer::addNewMetadata(Instruction *To, const Instruction *Orig) { // If the loop was versioned with memchecks, add the corresponding no-alias @@ -694,16 +672,17 @@ } void InnerLoopVectorizer::addMetadata(Instruction *To, - const Instruction *From) { + Instruction *From) { propagateMetadata(To, From); addNewMetadata(To, From); } -void InnerLoopVectorizer::addMetadata(SmallVectorImpl &To, - const Instruction *From) { - for (Value *V : To) +void InnerLoopVectorizer::addMetadata(ArrayRef To, + Instruction *From) { + for (Value *V : To) { if (Instruction *I = dyn_cast(V)) addMetadata(I, From); + } } /// \brief The group of interleaved loads/stores sharing the same stride and Index: lib/Transforms/Vectorize/SLPVectorizer.cpp =================================================================== --- lib/Transforms/Vectorize/SLPVectorizer.cpp +++ lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -227,46 +227,6 @@ } } -/// \returns \p I after propagating metadata from \p VL. -static Instruction *propagateMetadata(Instruction *I, ArrayRef VL) { - Instruction *I0 = cast(VL[0]); - SmallVector, 4> Metadata; - I0->getAllMetadataOtherThanDebugLoc(Metadata); - - for (unsigned i = 0, n = Metadata.size(); i != n; ++i) { - unsigned Kind = Metadata[i].first; - MDNode *MD = Metadata[i].second; - - for (int i = 1, e = VL.size(); MD && i != e; i++) { - Instruction *I = cast(VL[i]); - MDNode *IMD = I->getMetadata(Kind); - - switch (Kind) { - default: - MD = nullptr; // Remove unknown metadata - break; - case LLVMContext::MD_tbaa: - MD = MDNode::getMostGenericTBAA(MD, IMD); - break; - case LLVMContext::MD_alias_scope: - MD = MDNode::getMostGenericAliasScope(MD, IMD); - break; - case LLVMContext::MD_noalias: - MD = MDNode::intersect(MD, IMD); - break; - case LLVMContext::MD_fpmath: - MD = MDNode::getMostGenericFPMath(MD, IMD); - break; - case LLVMContext::MD_nontemporal: - MD = MDNode::intersect(MD, IMD); - break; - } - } - I->setMetadata(Kind, MD); - } - return I; -} - /// \returns The type that all of the values in \p VL have or null if there /// are different types. static Type* getSameType(ArrayRef VL) {