From fe595b3752e781ea58d0b36786baea11f09489df Mon Sep 17 00:00:00 2001 From: Serguei Katkov Date: Fri, 26 Jul 2019 07:04:34 +0000 Subject: [PATCH] [Loop Utils] Extend the scope of addStringMetadataToLoop. To avoid duplicates in loop metadata, if the string to add is already there, just update the value. Reviewers: reames, Ashutosh Reviewed By: reames Subscribers: hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D65265 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367087 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/Utils/LoopUtils.h | 2 ++ lib/Transforms/Utils/LoopUtils.cpp | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/llvm/Transforms/Utils/LoopUtils.h b/include/llvm/Transforms/Utils/LoopUtils.h index 68bdded5cf9..533cde11b27 100644 --- a/include/llvm/Transforms/Utils/LoopUtils.h +++ b/include/llvm/Transforms/Utils/LoopUtils.h @@ -252,6 +252,8 @@ TransformationMode hasLICMVersioningTransformation(Loop *L); /// @} /// Set input string into loop metadata by keeping other values intact. +/// If the string is already in loop metadata update value if it is +/// different. void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V = 0); diff --git a/lib/Transforms/Utils/LoopUtils.cpp b/lib/Transforms/Utils/LoopUtils.cpp index fef10352482..ce296b9d4dd 100644 --- a/lib/Transforms/Utils/LoopUtils.cpp +++ b/lib/Transforms/Utils/LoopUtils.cpp @@ -201,7 +201,9 @@ static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) { } /// Set input string into loop metadata by keeping other values intact. -void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *MDString, +/// If the string is already in loop metadata update value if it is +/// different. +void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD, unsigned V) { SmallVector MDs(1); // If the loop already has metadata, retain it. @@ -209,11 +211,25 @@ void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *MDString, if (LoopID) { for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { MDNode *Node = cast(LoopID->getOperand(i)); + // If it is of form key = value, try to parse it. + if (Node->getNumOperands() == 2) { + MDString *S = dyn_cast(Node->getOperand(0)); + if (S && S->getString().equals(StringMD)) { + ConstantInt *IntMD = + mdconst::extract_or_null(Node->getOperand(1)); + if (IntMD && IntMD->getSExtValue() == V) + // It is already in place. Do nothing. + return; + // We need to update the value, so just skip it here and it will + // be added after copying other existed nodes. + continue; + } + } MDs.push_back(Node); } } // Add new metadata. - MDs.push_back(createStringMetadata(TheLoop, MDString, V)); + MDs.push_back(createStringMetadata(TheLoop, StringMD, V)); // Replace current metadata node with new one. LLVMContext &Context = TheLoop->getHeader()->getContext(); MDNode *NewLoopID = MDNode::get(Context, MDs); -- 2.40.0