[PATCH] D65265: [Loop Utils] Extend the scope of addStringMetadataToLoop

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 01:27:17 PDT 2019


skatkov created this revision.
skatkov added reviewers: reames, Ashutosh.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
skatkov added a parent revision: D65264: [Loop Utils] Move utilty addStringMetadataToLoop to LoopUtils.cpp. NFC..

To avoid duplicates in loop metadata, if the string to add is
already there, just update the value.


https://reviews.llvm.org/D65265

Files:
  llvm/include/llvm/Transforms/Utils/LoopUtils.h
  llvm/lib/Transforms/Utils/LoopUtils.cpp


Index: llvm/lib/Transforms/Utils/LoopUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -201,7 +201,9 @@
 }
 
 /// 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<Metadata *, 4> MDs(1);
   // If the loop already has metadata, retain it.
@@ -209,11 +211,25 @@
   if (LoopID) {
     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
       MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
+      // If it is of form key = value, try to parse it.
+      if (Node->getNumOperands() == 2) {
+        MDString *S = dyn_cast<MDString>(Node->getOperand(0));
+        if (S && S->getString().equals(StringMD)) {
+          ConstantInt *IntMD =
+              mdconst::extract_or_null<ConstantInt>(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);
Index: llvm/include/llvm/Transforms/Utils/LoopUtils.h
===================================================================
--- llvm/include/llvm/Transforms/Utils/LoopUtils.h
+++ llvm/include/llvm/Transforms/Utils/LoopUtils.h
@@ -252,6 +252,8 @@
 /// @}
 
 /// 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);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65265.211687.patch
Type: text/x-patch
Size: 2342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190725/5210a03a/attachment.bin>


More information about the llvm-commits mailing list