Switch inliner over to use DenseMap instead of std::map for ValueMap. This
authorChris Lattner <sabre@nondot.org>
Sat, 3 Feb 2007 00:08:31 +0000 (00:08 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 3 Feb 2007 00:08:31 +0000 (00:08 +0000)
speeds up the inliner 16%.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33801 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Transforms/Utils/Cloning.h
lib/Transforms/Scalar/LoopUnroll.cpp
lib/Transforms/Scalar/LoopUnswitch.cpp
lib/Transforms/Utils/CloneFunction.cpp
lib/Transforms/Utils/CloneModule.cpp
lib/Transforms/Utils/CloneTrace.cpp
lib/Transforms/Utils/InlineFunction.cpp
lib/Transforms/Utils/ValueMapper.cpp
lib/Transforms/Utils/ValueMapper.h

index f925c63468db7ae2c51520fee6ffaf0c41ecb70d..9f1aad7db821921f34a5830a43b0582be8df41c4 100644 (file)
@@ -19,7 +19,7 @@
 #define LLVM_TRANSFORMS_UTILS_CLONING_H
 
 #include <vector>
-#include <map>
+#include "llvm/ADT/DenseMap.h"
 
 namespace llvm {
 
@@ -38,7 +38,7 @@ class TargetData;
 /// CloneModule - Return an exact copy of the specified module
 ///
 Module *CloneModule(const Module *M);
-Module *CloneModule(const Module *M, std::map<const Value*, Value*> &ValueMap);
+Module *CloneModule(const Module *M, DenseMap<const Value*, Value*> &ValueMap);
 
 /// ClonedCodeInfo - This struct can be used to capture information about code
 /// being cloned, while it is being cloned.
@@ -94,7 +94,7 @@ struct ClonedCodeInfo {
 /// parameter.
 ///
 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
-                            std::map<const Value*, Value*> &ValueMap,
+                            DenseMap<const Value*, Value*> &ValueMap,
                             const char *NameSuffix = "", Function *F = 0,
                             ClonedCodeInfo *CodeInfo = 0);
 
@@ -109,13 +109,13 @@ BasicBlock *CloneBasicBlock(const BasicBlock *BB,
 /// information about the cloned code if non-null.
 ///
 Function *CloneFunction(const Function *F,
-                        std::map<const Value*, Value*> &ValueMap,
+                        DenseMap<const Value*, Value*> &ValueMap,
                         ClonedCodeInfo *CodeInfo = 0);
 
 /// CloneFunction - Version of the function that doesn't need the ValueMap.
 ///
 inline Function *CloneFunction(const Function *F, ClonedCodeInfo *CodeInfo = 0){
-  std::map<const Value*, Value*> ValueMap;
+  DenseMap<const Value*, Value*> ValueMap;
   return CloneFunction(F, ValueMap, CodeInfo);
 }
 
@@ -126,7 +126,7 @@ inline Function *CloneFunction(const Function *F, ClonedCodeInfo *CodeInfo = 0){
 /// specified suffix to all values cloned.
 ///
 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
-                       std::map<const Value*, Value*> &ValueMap,
+                       DenseMap<const Value*, Value*> &ValueMap,
                        std::vector<ReturnInst*> &Returns,
                        const char *NameSuffix = "", 
                        ClonedCodeInfo *CodeInfo = 0);
@@ -139,7 +139,7 @@ void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
 /// dead.  Since this doesn't produce an exactly copy of the input, it can't be
 /// used for things like CloneFunction or CloneModule.
 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
-                               std::map<const Value*, Value*> &ValueMap,
+                               DenseMap<const Value*, Value*> &ValueMap,
                                std::vector<ReturnInst*> &Returns,
                                const char *NameSuffix = "", 
                                ClonedCodeInfo *CodeInfo = 0,
@@ -150,7 +150,7 @@ void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
 /// saved in ValueMap.
 ///
 void CloneTraceInto(Function *NewFunc, Trace &T,
-                    std::map<const Value*, Value*> &ValueMap,
+                    DenseMap<const Value*, Value*> &ValueMap,
                     const char *NameSuffix);
 
 /// CloneTrace - Returns a copy of the specified trace.
index 655ddc4179244d6e5cfc52b2cb3f5c61e211774c..c6d8853831001f5a4ac776f5484ac634cbf42a3a 100644 (file)
@@ -31,9 +31,9 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/IntrinsicInst.h"
 #include <cstdio>
-#include <set>
 #include <algorithm>
 using namespace llvm;
 
@@ -111,10 +111,10 @@ static unsigned ApproximateLoopSize(const Loop *L) {
 // current values into those specified by ValueMap.
 //
 static inline void RemapInstruction(Instruction *I,
-                                    std::map<const Value *, Value*> &ValueMap) {
+                                    DenseMap<const Value *, Value*> &ValueMap) {
   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
     Value *Op = I->getOperand(op);
-    std::map<const Value *, Value*>::iterator It = ValueMap.find(Op);
+    DenseMap<const Value *, Value*>::iterator It = ValueMap.find(Op);
     if (It != ValueMap.end()) Op = It->second;
     I->setOperand(op, Op);
   }
@@ -212,7 +212,7 @@ bool LoopUnroll::visitLoop(Loop *L) {
 
   // For the first iteration of the loop, we should use the precloned values for
   // PHI nodes.  Insert associations now.
-  std::map<const Value*, Value*> LastValueMap;
+  DenseMap<const Value*, Value*> LastValueMap;
   std::vector<PHINode*> OrigPHINode;
   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
     PHINode *PN = cast<PHINode>(I);
@@ -240,7 +240,7 @@ bool LoopUnroll::visitLoop(Loop *L) {
     
     for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(),
          E = LoopBlocks.end(); BB != E; ++BB) {
-      std::map<const Value*, Value*> ValueMap;
+      DenseMap<const Value*, Value*> ValueMap;
       BasicBlock *New = CloneBasicBlock(*BB, ValueMap, SuffixBuffer);
       Header->getParent()->getBasicBlockList().push_back(New);
 
@@ -259,7 +259,7 @@ bool LoopUnroll::visitLoop(Loop *L) {
 
       // Update our running map of newest clones
       LastValueMap[*BB] = New;
-      for (std::map<const Value*, Value*>::iterator VI = ValueMap.begin(),
+      for (DenseMap<const Value*, Value*>::iterator VI = ValueMap.begin(),
            VE = ValueMap.end(); VI != VE; ++VI)
         LastValueMap[VI->first] = VI->second;
 
@@ -303,13 +303,13 @@ bool LoopUnroll::visitLoop(Loop *L) {
  
   // Update PHI nodes that reference the final latch block
   if (TripCount > 1) {
-    std::set<PHINode*> Users;
+    SmallPtrSet<PHINode*, 8> Users;
     for (Value::use_iterator UI = LatchBlock->use_begin(),
          UE = LatchBlock->use_end(); UI != UE; ++UI)
       if (PHINode* phi = dyn_cast<PHINode>(*UI))
         Users.insert(phi);
         
-    for (std::set<PHINode*>::iterator SI = Users.begin(), SE = Users.end();
+    for (SmallPtrSet<PHINode*,8>::iterator SI = Users.begin(), SE = Users.end();
          SI != SE; ++SI) {
       Value* InVal = (*SI)->getIncomingValueForBlock(LatchBlock);
       if (isa<Instruction>(InVal))
index 00979a67403144cee2ecb6349f3c1a114fbb95ff..9492cefb6a17be4557913f1f162165373998f47b 100644 (file)
@@ -445,10 +445,10 @@ BasicBlock *LoopUnswitch::SplitEdge(BasicBlock *BB, BasicBlock *Succ) {
 // current values into those specified by ValueMap.
 //
 static inline void RemapInstruction(Instruction *I,
-                                    std::map<const Value *, Value*> &ValueMap) {
+                                    DenseMap<const Value *, Value*> &ValueMap) {
   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
     Value *Op = I->getOperand(op);
-    std::map<const Value *, Value*>::iterator It = ValueMap.find(Op);
+    DenseMap<const Value *, Value*>::iterator It = ValueMap.find(Op);
     if (It != ValueMap.end()) Op = It->second;
     I->setOperand(op, Op);
   }
@@ -456,7 +456,7 @@ static inline void RemapInstruction(Instruction *I,
 
 /// CloneLoop - Recursively clone the specified loop and all of its children,
 /// mapping the blocks with the specified map.
-static Loop *CloneLoop(Loop *L, Loop *PL, std::map<const Value*, Value*> &VM,
+static Loop *CloneLoop(Loop *L, Loop *PL, DenseMap<const Value*, Value*> &VM,
                        LoopInfo *LI) {
   Loop *New = new Loop();
 
@@ -632,7 +632,7 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
   // the instructions and blocks.
   std::vector<BasicBlock*> NewBlocks;
   NewBlocks.reserve(LoopBlocks.size());
-  std::map<const Value*, Value*> ValueMap;
+  DenseMap<const Value*, Value*> ValueMap;
   for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
     BasicBlock *New = CloneBasicBlock(LoopBlocks[i], ValueMap, ".us", F);
     NewBlocks.push_back(New);
@@ -669,7 +669,7 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
     for (BasicBlock::iterator I = ExitSucc->begin();
          (PN = dyn_cast<PHINode>(I)); ++I) {
       Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]);
-      std::map<const Value *, Value*>::iterator It = ValueMap.find(V);
+      DenseMap<const Value *, Value*>::iterator It = ValueMap.find(V);
       if (It != ValueMap.end()) V = It->second;
       PN->addIncoming(V, NewExit);
     }
index 2dd8ec06973198b290a46899fbc135284e189de0..5db664f443c45d2c49b9ec12284c07c54271365f 100644 (file)
 #include "ValueMapper.h"
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/ADT/SmallVector.h"
+#include <map>
 using namespace llvm;
 
 // CloneBasicBlock - See comments in Cloning.h
 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
-                                  std::map<const Value*, Value*> &ValueMap,
+                                  DenseMap<const Value*, Value*> &ValueMap,
                                   const char *NameSuffix, Function *F,
                                   ClonedCodeInfo *CodeInfo) {
   BasicBlock *NewBB = new BasicBlock("", F);
@@ -66,7 +67,7 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
 // ArgMap values.
 //
 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
-                             std::map<const Value*, Value*> &ValueMap,
+                             DenseMap<const Value*, Value*> &ValueMap,
                              std::vector<ReturnInst*> &Returns,
                              const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
   assert(NameSuffix && "NameSuffix cannot be null!");
@@ -113,7 +114,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
 /// the function from their old to new values.
 ///
 Function *llvm::CloneFunction(const Function *F,
-                              std::map<const Value*, Value*> &ValueMap,
+                              DenseMap<const Value*, Value*> &ValueMap,
                               ClonedCodeInfo *CodeInfo) {
   std::vector<const Type*> ArgTypes;
 
@@ -154,7 +155,7 @@ namespace {
   struct PruningFunctionCloner {
     Function *NewFunc;
     const Function *OldFunc;
-    std::map<const Value*, Value*> &ValueMap;
+    DenseMap<const Value*, Value*> &ValueMap;
     std::vector<ReturnInst*> &Returns;
     const char *NameSuffix;
     ClonedCodeInfo *CodeInfo;
@@ -162,7 +163,7 @@ namespace {
 
   public:
     PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
-                          std::map<const Value*, Value*> &valueMap,
+                          DenseMap<const Value*, Value*> &valueMap,
                           std::vector<ReturnInst*> &returns,
                           const char *nameSuffix, 
                           ClonedCodeInfo *codeInfo,
@@ -303,7 +304,7 @@ ConstantFoldMappedInstruction(const Instruction *I) {
 /// dead.  Since this doesn't produce an exactly copy of the input, it can't be
 /// used for things like CloneFunction or CloneModule.
 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
-                                     std::map<const Value*, Value*> &ValueMap,
+                                     DenseMap<const Value*, Value*> &ValueMap,
                                      std::vector<ReturnInst*> &Returns,
                                      const char *NameSuffix, 
                                      ClonedCodeInfo *CodeInfo,
index a4460fa69b1b7139675bb1107f241e7ac6cde483..d481aea0e536d8406e081d4cb6ca88fc296c2d90 100644 (file)
@@ -29,12 +29,12 @@ using namespace llvm;
 Module *llvm::CloneModule(const Module *M) {
   // Create the value map that maps things from the old module over to the new
   // module.
-  std::map<const Value*, Value*> ValueMap;
-
+  DenseMap<const Value*, Value*> ValueMap;
   return CloneModule(M, ValueMap);
 }
 
-Module *llvm::CloneModule(const Module *M, std::map<const Value*, Value*> &ValueMap) {
+Module *llvm::CloneModule(const Module *M,
+                          DenseMap<const Value*, Value*> &ValueMap) {
   // First off, we need to create the new module...
   Module *New = new Module(M->getModuleIdentifier());
   New->setDataLayout(M->getDataLayout());
index 5bfd9893d708c85a1a03df65b5a8f364576727a4..97e57b2ca4f28812a3879a87e9499fc94389b8f8 100644 (file)
@@ -26,7 +26,7 @@ using namespace llvm;
 std::vector<BasicBlock *>
 llvm::CloneTrace(const std::vector<BasicBlock*> &origTrace) {
   std::vector<BasicBlock *> clonedTrace;
-  std::map<const Value*, Value*> ValueMap;
+  DenseMap<const Value*, Value*> ValueMap;
 
   //First, loop over all the Basic Blocks in the trace and copy
   //them using CloneBasicBlock. Also fix the phi nodes during
@@ -92,7 +92,7 @@ llvm::CloneTrace(const std::vector<BasicBlock*> &origTrace) {
 /// saved in ValueMap.
 ///
 void llvm::CloneTraceInto(Function *NewFunc, Trace &T,
-                          std::map<const Value*, Value*> &ValueMap,
+                          DenseMap<const Value*, Value*> &ValueMap,
                           const char *NameSuffix) {
   assert(NameSuffix && "NameSuffix cannot be null!");
 
index 911bfe0a39a6640ebd2e16d3329ee6d85f1b906b..054c1c7712392f3a65a0335663e5b7776aaa8492 100644 (file)
@@ -143,7 +143,7 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
 static void UpdateCallGraphAfterInlining(const Function *Caller,
                                          const Function *Callee,
                                          Function::iterator FirstNewBlock,
-                                       std::map<const Value*, Value*> &ValueMap,
+                                       DenseMap<const Value*, Value*> &ValueMap,
                                          CallGraph &CG) {
   // Update the call graph by deleting the edge from Callee to Caller
   CallGraphNode *CalleeNode = CG[Callee];
@@ -156,7 +156,7 @@ static void UpdateCallGraphAfterInlining(const Function *Caller,
        E = CalleeNode->end(); I != E; ++I) {
     const Instruction *OrigCall = I->first.getInstruction();
     
-    std::map<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
+    DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
     // Only copy the edge if the call was inlined!
     if (VMI != ValueMap.end() && VMI->second) {
       // If the call was inlined, but then constant folded, there is no edge to
@@ -208,7 +208,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
   Function::iterator FirstNewBlock;
   
   { // Scope to destroy ValueMap after cloning.
-    std::map<const Value*, Value*> ValueMap;
+    DenseMap<const Value*, Value*> ValueMap;
 
     // Calculate the vector of arguments to pass into the function cloner, which
     // matches up the formal to the actual argument values.
index 371bee195a61027ea73d3c0c956dfd3c346aab2c..f90716ca260d2a0aa5a1fd9de4a864c0f30eaddb 100644 (file)
 #include "llvm/Instruction.h"
 using namespace llvm;
 
-Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
+Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
   Value *&VMSlot = VM[V];
   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
+  
+  // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
+  // DenseMap.  This includes any recursive calls to MapValue.
 
   // Global values do not need to be seeded into the ValueMap if they are using
   // the identity mapping.
@@ -46,10 +49,10 @@ Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
           Values.push_back(cast<Constant>(MV));
           for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
-          return VMSlot = ConstantArray::get(CA->getType(), Values);
+          return VM[V] = ConstantArray::get(CA->getType(), Values);
         }
       }
-      return VMSlot = C;
+      return VM[V] = C;
 
     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
       for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
@@ -65,16 +68,16 @@ Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
           Values.push_back(cast<Constant>(MV));
           for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
-          return VMSlot = ConstantStruct::get(CS->getType(), Values);
+          return VM[V] = ConstantStruct::get(CS->getType(), Values);
         }
       }
-      return VMSlot = C;
+      return VM[V] = C;
 
     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
       std::vector<Constant*> Ops;
       for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
         Ops.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
-      return VMSlot = CE->getWithOperands(Ops);
+      return VM[V] = CE->getWithOperands(Ops);
     } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
         Value *MV = MapValue(CP->getOperand(i), VM);
@@ -89,7 +92,7 @@ Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
           Values.push_back(cast<Constant>(MV));
           for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
-          return VMSlot = ConstantPacked::get(Values);
+          return VM[V] = ConstantPacked::get(Values);
         }
       }
       return VMSlot = C;
@@ -105,8 +108,7 @@ Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
 /// RemapInstruction - Convert the instruction operands from referencing the
 /// current values into those specified by ValueMap.
 ///
-void llvm::RemapInstruction(Instruction *I,
-                            std::map<const Value *, Value*> &ValueMap) {
+void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
     const Value *Op = I->getOperand(op);
     Value *V = MapValue(Op, ValueMap);
index c768671f2ec5e4f31d7f145686fcfae5e3628577..51319db55cebfb286d0147b229078808249f2540 100644 (file)
 #ifndef VALUEMAPPER_H
 #define VALUEMAPPER_H
 
-#include <map>
+#include "llvm/ADT/DenseMap.h"
 
 namespace llvm {
   class Value;
   class Instruction;
-  typedef std::map<const Value *, Value *> ValueMapTy;
+  typedef DenseMap<const Value *, Value *> ValueMapTy;
 
   Value *MapValue(const Value *V, ValueMapTy &VM);
   void RemapInstruction(Instruction *I, ValueMapTy &VM);