[llvm] r244122 - -Wdeprecated cleanup: Make CallGraph movable by default by using unique_ptr members rather than raw pointers.

David Blaikie dblaikie at gmail.com
Wed Aug 5 13:55:50 PDT 2015


Author: dblaikie
Date: Wed Aug  5 15:55:50 2015
New Revision: 244122

URL: http://llvm.org/viewvc/llvm-project?rev=244122&view=rev
Log:
-Wdeprecated cleanup: Make CallGraph movable by default by using unique_ptr members rather than raw pointers.

The only place that tries to return a CallGraph by value
(CallGraphAnalysis::run) doesn't seem to be used right now, but it's a
reasonable bit of cleanup anyway.

Modified:
    llvm/trunk/include/llvm/Analysis/CallGraph.h
    llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
    llvm/trunk/lib/Transforms/IPO/Inliner.cpp

Modified: llvm/trunk/include/llvm/Analysis/CallGraph.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/CallGraph.h?rev=244122&r1=244121&r2=244122&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/CallGraph.h (original)
+++ llvm/trunk/include/llvm/Analysis/CallGraph.h Wed Aug  5 15:55:50 2015
@@ -75,7 +75,8 @@ class CallGraphNode;
 class CallGraph {
   Module &M;
 
-  typedef std::map<const Function *, CallGraphNode *> FunctionMapTy;
+  typedef std::map<const Function *, std::unique_ptr<CallGraphNode>>
+      FunctionMapTy;
 
   /// \brief A map from \c Function* to \c CallGraphNode*.
   FunctionMapTy FunctionMap;
@@ -90,7 +91,7 @@ class CallGraph {
 
   /// \brief This node has edges to it from all functions making indirect calls
   /// or calling an external function.
-  CallGraphNode *CallsExternalNode;
+  std::unique_ptr<CallGraphNode> CallsExternalNode;
 
   /// \brief Replace the function represented by this node by another.
   ///
@@ -105,6 +106,10 @@ class CallGraph {
 
 public:
   CallGraph(Module &M);
+  // Default move ctor is fine, the dtor just does things to CallsExternalNode
+  // (if non-null) and the values in the FunctionMap, all of which should be
+  // null post-move, so no-op the dtor on the moved-from side.
+  CallGraph(CallGraph &&) = default;
   ~CallGraph();
 
   void print(raw_ostream &OS) const;
@@ -125,21 +130,23 @@ public:
   inline const CallGraphNode *operator[](const Function *F) const {
     const_iterator I = FunctionMap.find(F);
     assert(I != FunctionMap.end() && "Function not in callgraph!");
-    return I->second;
+    return I->second.get();
   }
 
   /// \brief Returns the call graph node for the provided function.
   inline CallGraphNode *operator[](const Function *F) {
     const_iterator I = FunctionMap.find(F);
     assert(I != FunctionMap.end() && "Function not in callgraph!");
-    return I->second;
+    return I->second.get();
   }
 
   /// \brief Returns the \c CallGraphNode which is used to represent
   /// undetermined calls into the callgraph.
   CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
 
-  CallGraphNode *getCallsExternalNode() const { return CallsExternalNode; }
+  CallGraphNode *getCallsExternalNode() const {
+    return CallsExternalNode.get();
+  }
 
   //===---------------------------------------------------------------------
   // Functions to keep a call graph up to date with a function that has been
@@ -444,8 +451,10 @@ struct GraphTraits<CallGraph *> : public
   static NodeType *getEntryNode(CallGraph *CGN) {
     return CGN->getExternalCallingNode(); // Start at the external node!
   }
-  typedef std::pair<const Function *, CallGraphNode *> PairTy;
-  typedef std::pointer_to_unary_function<PairTy, CallGraphNode &> DerefFun;
+  typedef std::pair<const Function *const, std::unique_ptr<CallGraphNode>>
+      PairTy;
+  typedef std::pointer_to_unary_function<const PairTy &, CallGraphNode &>
+      DerefFun;
 
   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
   typedef mapped_iterator<CallGraph::iterator, DerefFun> nodes_iterator;
@@ -456,7 +465,7 @@ struct GraphTraits<CallGraph *> : public
     return map_iterator(CG->end(), DerefFun(CGdereference));
   }
 
-  static CallGraphNode &CGdereference(PairTy P) { return *P.second; }
+  static CallGraphNode &CGdereference(const PairTy &P) { return *P.second; }
 };
 
 template <>
@@ -465,8 +474,9 @@ struct GraphTraits<const CallGraph *> :
   static NodeType *getEntryNode(const CallGraph *CGN) {
     return CGN->getExternalCallingNode(); // Start at the external node!
   }
-  typedef std::pair<const Function *, const CallGraphNode *> PairTy;
-  typedef std::pointer_to_unary_function<PairTy, const CallGraphNode &>
+  typedef std::pair<const Function *const, std::unique_ptr<CallGraphNode>>
+      PairTy;
+  typedef std::pointer_to_unary_function<const PairTy &, const CallGraphNode &>
       DerefFun;
 
   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
@@ -478,7 +488,9 @@ struct GraphTraits<const CallGraph *> :
     return map_iterator(CG->end(), DerefFun(CGdereference));
   }
 
-  static const CallGraphNode &CGdereference(PairTy P) { return *P.second; }
+  static const CallGraphNode &CGdereference(const PairTy &P) {
+    return *P.second;
+  }
 };
 
 } // End llvm namespace

Modified: llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/IPA/CallGraph.cpp?rev=244122&r1=244121&r2=244122&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/IPA/CallGraph.cpp (original)
+++ llvm/trunk/lib/Analysis/IPA/CallGraph.cpp Wed Aug  5 15:55:50 2015
@@ -22,7 +22,7 @@ using namespace llvm;
 
 CallGraph::CallGraph(Module &M)
     : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)),
-      CallsExternalNode(new CallGraphNode(nullptr)) {
+      CallsExternalNode(llvm::make_unique<CallGraphNode>(nullptr)) {
   // Add every function to the call graph.
   for (Function &F : M)
     addToCallGraph(&F);
@@ -34,8 +34,8 @@ CallGraph::CallGraph(Module &M)
 
 CallGraph::~CallGraph() {
   // CallsExternalNode is not in the function map, delete it explicitly.
-  CallsExternalNode->allReferencesDropped();
-  delete CallsExternalNode;
+  if (CallsExternalNode)
+    CallsExternalNode->allReferencesDropped();
 
 // Reset all node's use counts to zero before deleting them to prevent an
 // assertion from firing.
@@ -43,8 +43,6 @@ CallGraph::~CallGraph() {
   for (auto &I : FunctionMap)
     I.second->allReferencesDropped();
 #endif
-  for (auto &I : FunctionMap)
-    delete I.second;
 }
 
 void CallGraph::addToCallGraph(Function *F) {
@@ -70,7 +68,7 @@ void CallGraph::addToCallGraph(Function
   // If this function is not defined in this translation unit, it could call
   // anything.
   if (F->isDeclaration() && !F->isIntrinsic())
-    Node->addCalledFunction(CallSite(), CallsExternalNode);
+    Node->addCalledFunction(CallSite(), CallsExternalNode.get());
 
   // Look for calls by this function.
   for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
@@ -83,7 +81,7 @@ void CallGraph::addToCallGraph(Function
           // Indirect calls of intrinsics are not allowed so no need to check.
           // We can be more precise here by using TargetArg returned by
           // Intrinsic::isLeaf.
-          Node->addCalledFunction(CS, CallsExternalNode);
+          Node->addCalledFunction(CS, CallsExternalNode.get());
         else if (!Callee->isIntrinsic())
           Node->addCalledFunction(CS, getOrInsertFunction(Callee));
       }
@@ -105,7 +103,7 @@ void CallGraph::print(raw_ostream &OS) c
   Nodes.reserve(FunctionMap.size());
 
   for (auto I = begin(), E = end(); I != E; ++I)
-    Nodes.push_back(I->second);
+    Nodes.push_back(I->second.get());
 
   std::sort(Nodes.begin(), Nodes.end(),
             [](CallGraphNode *LHS, CallGraphNode *RHS) {
@@ -134,7 +132,6 @@ Function *CallGraph::removeFunctionFromM
   assert(CGN->empty() && "Cannot remove function from call "
          "graph if it references other functions!");
   Function *F = CGN->getFunction(); // Get the function for the call graph node
-  delete CGN;                       // Delete the call graph node for this func
   FunctionMap.erase(F);             // Remove the call graph node from the map
 
   M.getFunctionList().remove(F);
@@ -152,7 +149,7 @@ void CallGraph::spliceFunction(const Fun
          "Pointing CallGraphNode at a function that already exists");
   FunctionMapTy::iterator I = FunctionMap.find(From);
   I->second->F = const_cast<Function*>(To);
-  FunctionMap[To] = I->second;
+  FunctionMap[To] = std::move(I->second);
   FunctionMap.erase(I);
 }
 
@@ -160,12 +157,13 @@ void CallGraph::spliceFunction(const Fun
 // it will insert a new CallGraphNode for the specified function if one does
 // not already exist.
 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
-  CallGraphNode *&CGN = FunctionMap[F];
+  auto &CGN = FunctionMap[F];
   if (CGN)
-    return CGN;
+    return CGN.get();
 
   assert((!F || F->getParent() == &M) && "Function not in current module!");
-  return CGN = new CallGraphNode(const_cast<Function*>(F));
+  CGN = llvm::make_unique<CallGraphNode>(const_cast<Function *>(F));
+  return CGN.get();
 }
 
 //===----------------------------------------------------------------------===//

Modified: llvm/trunk/lib/Transforms/IPO/Inliner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/Inliner.cpp?rev=244122&r1=244121&r2=244122&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/Inliner.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/Inliner.cpp Wed Aug  5 15:55:50 2015
@@ -649,8 +649,8 @@ bool Inliner::removeDeadFunctions(CallGr
 
   // Scan for all of the functions, looking for ones that should now be removed
   // from the program.  Insert the dead ones in the FunctionsToRemove set.
-  for (auto I : CG) {
-    CallGraphNode *CGN = I.second;
+  for (const auto &I : CG) {
+    CallGraphNode *CGN = I.second.get();
     Function *F = CGN->getFunction();
     if (!F || F->isDeclaration())
       continue;




More information about the llvm-commits mailing list