From: Chris Lattner Date: Sat, 3 Feb 2007 00:08:31 +0000 (+0000) Subject: Switch inliner over to use DenseMap instead of std::map for ValueMap. This X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5e665f559419c7f58a4fd9360cd488f065505c44;p=oota-llvm.git Switch inliner over to use DenseMap instead of std::map for ValueMap. This speeds up the inliner 16%. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33801 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Transforms/Utils/Cloning.h b/include/llvm/Transforms/Utils/Cloning.h index f925c63468d..9f1aad7db82 100644 --- a/include/llvm/Transforms/Utils/Cloning.h +++ b/include/llvm/Transforms/Utils/Cloning.h @@ -19,7 +19,7 @@ #define LLVM_TRANSFORMS_UTILS_CLONING_H #include -#include +#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 &ValueMap); +Module *CloneModule(const Module *M, DenseMap &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 &ValueMap, + DenseMap &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 &ValueMap, + DenseMap &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 ValueMap; + DenseMap 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 &ValueMap, + DenseMap &ValueMap, std::vector &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 &ValueMap, + DenseMap &ValueMap, std::vector &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 &ValueMap, + DenseMap &ValueMap, const char *NameSuffix); /// CloneTrace - Returns a copy of the specified trace. diff --git a/lib/Transforms/Scalar/LoopUnroll.cpp b/lib/Transforms/Scalar/LoopUnroll.cpp index 655ddc41792..c6d88538310 100644 --- a/lib/Transforms/Scalar/LoopUnroll.cpp +++ b/lib/Transforms/Scalar/LoopUnroll.cpp @@ -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 -#include #include 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 &ValueMap) { + DenseMap &ValueMap) { for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { Value *Op = I->getOperand(op); - std::map::iterator It = ValueMap.find(Op); + DenseMap::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 LastValueMap; + DenseMap LastValueMap; std::vector OrigPHINode; for (BasicBlock::iterator I = Header->begin(); isa(I); ++I) { PHINode *PN = cast(I); @@ -240,7 +240,7 @@ bool LoopUnroll::visitLoop(Loop *L) { for (std::vector::iterator BB = LoopBlocks.begin(), E = LoopBlocks.end(); BB != E; ++BB) { - std::map ValueMap; + DenseMap 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::iterator VI = ValueMap.begin(), + for (DenseMap::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 Users; + SmallPtrSet Users; for (Value::use_iterator UI = LatchBlock->use_begin(), UE = LatchBlock->use_end(); UI != UE; ++UI) if (PHINode* phi = dyn_cast(*UI)) Users.insert(phi); - for (std::set::iterator SI = Users.begin(), SE = Users.end(); + for (SmallPtrSet::iterator SI = Users.begin(), SE = Users.end(); SI != SE; ++SI) { Value* InVal = (*SI)->getIncomingValueForBlock(LatchBlock); if (isa(InVal)) diff --git a/lib/Transforms/Scalar/LoopUnswitch.cpp b/lib/Transforms/Scalar/LoopUnswitch.cpp index 00979a67403..9492cefb6a1 100644 --- a/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -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 &ValueMap) { + DenseMap &ValueMap) { for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { Value *Op = I->getOperand(op); - std::map::iterator It = ValueMap.find(Op); + DenseMap::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 &VM, +static Loop *CloneLoop(Loop *L, Loop *PL, DenseMap &VM, LoopInfo *LI) { Loop *New = new Loop(); @@ -632,7 +632,7 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, // the instructions and blocks. std::vector NewBlocks; NewBlocks.reserve(LoopBlocks.size()); - std::map ValueMap; + DenseMap 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(I)); ++I) { Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]); - std::map::iterator It = ValueMap.find(V); + DenseMap::iterator It = ValueMap.find(V); if (It != ValueMap.end()) V = It->second; PN->addIncoming(V, NewExit); } diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp index 2dd8ec06973..5db664f443c 100644 --- a/lib/Transforms/Utils/CloneFunction.cpp +++ b/lib/Transforms/Utils/CloneFunction.cpp @@ -22,11 +22,12 @@ #include "ValueMapper.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/ADT/SmallVector.h" +#include using namespace llvm; // CloneBasicBlock - See comments in Cloning.h BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, - std::map &ValueMap, + DenseMap &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 &ValueMap, + DenseMap &ValueMap, std::vector &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 &ValueMap, + DenseMap &ValueMap, ClonedCodeInfo *CodeInfo) { std::vector ArgTypes; @@ -154,7 +155,7 @@ namespace { struct PruningFunctionCloner { Function *NewFunc; const Function *OldFunc; - std::map &ValueMap; + DenseMap &ValueMap; std::vector &Returns; const char *NameSuffix; ClonedCodeInfo *CodeInfo; @@ -162,7 +163,7 @@ namespace { public: PruningFunctionCloner(Function *newFunc, const Function *oldFunc, - std::map &valueMap, + DenseMap &valueMap, std::vector &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 &ValueMap, + DenseMap &ValueMap, std::vector &Returns, const char *NameSuffix, ClonedCodeInfo *CodeInfo, diff --git a/lib/Transforms/Utils/CloneModule.cpp b/lib/Transforms/Utils/CloneModule.cpp index a4460fa69b1..d481aea0e53 100644 --- a/lib/Transforms/Utils/CloneModule.cpp +++ b/lib/Transforms/Utils/CloneModule.cpp @@ -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 ValueMap; - + DenseMap ValueMap; return CloneModule(M, ValueMap); } -Module *llvm::CloneModule(const Module *M, std::map &ValueMap) { +Module *llvm::CloneModule(const Module *M, + DenseMap &ValueMap) { // First off, we need to create the new module... Module *New = new Module(M->getModuleIdentifier()); New->setDataLayout(M->getDataLayout()); diff --git a/lib/Transforms/Utils/CloneTrace.cpp b/lib/Transforms/Utils/CloneTrace.cpp index 5bfd9893d70..97e57b2ca4f 100644 --- a/lib/Transforms/Utils/CloneTrace.cpp +++ b/lib/Transforms/Utils/CloneTrace.cpp @@ -26,7 +26,7 @@ using namespace llvm; std::vector llvm::CloneTrace(const std::vector &origTrace) { std::vector clonedTrace; - std::map ValueMap; + DenseMap 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 &origTrace) { /// saved in ValueMap. /// void llvm::CloneTraceInto(Function *NewFunc, Trace &T, - std::map &ValueMap, + DenseMap &ValueMap, const char *NameSuffix) { assert(NameSuffix && "NameSuffix cannot be null!"); diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp index 911bfe0a39a..054c1c77123 100644 --- a/lib/Transforms/Utils/InlineFunction.cpp +++ b/lib/Transforms/Utils/InlineFunction.cpp @@ -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 &ValueMap, + DenseMap &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::iterator VMI = ValueMap.find(OrigCall); + DenseMap::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 ValueMap; + DenseMap ValueMap; // Calculate the vector of arguments to pass into the function cloner, which // matches up the formal to the actual argument values. diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp index 371bee195a6..f90716ca260 100644 --- a/lib/Transforms/Utils/ValueMapper.cpp +++ b/lib/Transforms/Utils/ValueMapper.cpp @@ -18,9 +18,12 @@ #include "llvm/Instruction.h" using namespace llvm; -Value *llvm::MapValue(const Value *V, std::map &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 &VM) { Values.push_back(cast(MV)); for (++i; i != e; ++i) Values.push_back(cast(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(C)) { for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { @@ -65,16 +68,16 @@ Value *llvm::MapValue(const Value *V, std::map &VM) { Values.push_back(cast(MV)); for (++i; i != e; ++i) Values.push_back(cast(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(C)) { std::vector Ops; for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) Ops.push_back(cast(MapValue(CE->getOperand(i), VM))); - return VMSlot = CE->getWithOperands(Ops); + return VM[V] = CE->getWithOperands(Ops); } else if (ConstantPacked *CP = dyn_cast(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 &VM) { Values.push_back(cast(MV)); for (++i; i != e; ++i) Values.push_back(cast(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 &VM) { /// RemapInstruction - Convert the instruction operands from referencing the /// current values into those specified by ValueMap. /// -void llvm::RemapInstruction(Instruction *I, - std::map &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); diff --git a/lib/Transforms/Utils/ValueMapper.h b/lib/Transforms/Utils/ValueMapper.h index c768671f2ec..51319db55ce 100644 --- a/lib/Transforms/Utils/ValueMapper.h +++ b/lib/Transforms/Utils/ValueMapper.h @@ -15,12 +15,12 @@ #ifndef VALUEMAPPER_H #define VALUEMAPPER_H -#include +#include "llvm/ADT/DenseMap.h" namespace llvm { class Value; class Instruction; - typedef std::map ValueMapTy; + typedef DenseMap ValueMapTy; Value *MapValue(const Value *V, ValueMapTy &VM); void RemapInstruction(Instruction *I, ValueMapTy &VM);