llvm.org GIT mirror llvm / dec628e
Switch ValueSymbolTable to use StringMap<Value*> instead of std::map<std::string, Value*> as its main datastructure. There are many improvements yet to be made, but this speeds up opt --std-compile-opts on 447.dealII by 7.3%. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34193 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
16 changed file(s) with 174 addition(s) and 128 deletion(s). Raw diff Collapse all Expand all
3838 void operator=(const Constant &); // Do not implement
3939 Constant(const Constant &); // Do not implement
4040 protected:
41 Constant(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
42 const std::string& Name = "")
43 : User(Ty, vty, Ops, NumOps, Name) {}
41 Constant(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps)
42 : User(Ty, vty, Ops, NumOps) {}
4443
4544 void destroyConstantImpl();
4645 public:
4848 protected:
4949 GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
5050 LinkageTypes linkage, const std::string &name = "")
51 : Constant(Ty, vty, Ops, NumOps, name), Parent(0),
52 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) { }
51 : Constant(Ty, vty, Ops, NumOps), Parent(0),
52 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
53 if (!name.empty()) setName(name);
54 }
5355
5456 Module *Parent;
5557 LinkageTypes Linkage; // The linkage of this global
1919 #define LLVM_USER_H
2020
2121 #include "llvm/Value.h"
22 #include
2322
2423 namespace llvm {
2524
3837 unsigned NumOperands;
3938
4039 public:
41 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps,
42 const std::string &name = "")
43 : Value(Ty, vty, name), OperandList(OpList), NumOperands(NumOps) {}
40 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
41 : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
4442
4543 Value *getOperand(unsigned i) const {
4644 assert(i < NumOperands && "getOperand() out of range!");
3232 class InlineAsm;
3333 class ValueSymbolTable;
3434 class TypeSymbolTable;
35 template class StringMapEntry;
36 typedef StringMapEntry ValueName;
3537
3638 //===----------------------------------------------------------------------===//
3739 // Value Class
6062
6163 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
6264 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
63 std::string Name;
65 ValueName *Name;
6466
6567 void operator=(const Value &); // Do not implement
6668 Value(const Value &); // Do not implement
6769
6870 public:
69 Value(const Type *Ty, unsigned scid, const std::string &name = "");
71 Value(const Type *Ty, unsigned scid);
7072 virtual ~Value();
7173
7274 /// dump - Support for debugging, callable in GDB: V->dump()
8385 inline const Type *getType() const { return Ty; }
8486
8587 // All values can potentially be named...
86 inline bool hasName() const { return !Name.empty(); }
87 inline const std::string &getName() const { return Name; }
88 inline bool hasName() const { return Name != 0; }
89 std::string getName() const;
90 ValueName *getValueName() const { return Name; }
8891
8992 void setName(const std::string &name);
9093
1616 #define LLVM_VALUE_SYMBOL_TABLE_H
1717
1818 #include "llvm/Value.h"
19 #include
19 #include "llvm/ADT/StringMap.h"
2020
2121 namespace llvm {
2222 template
4646 /// @name Types
4747 /// @{
4848 public:
49
5049 /// @brief A mapping of names to values.
51 typedef std::map*> ValueMap;
50 typedef StringMap*> ValueMap;
5251
5352 /// @brief An iterator over a ValueMap.
5453 typedef ValueMap::iterator iterator;
8887 /// @brief Get a name unique to this symbol table
8988 std::string getUniqueName(const std::string &BaseName) const;
9089
91 /// @return 1 if the name is in the symbol table, 0 otherwise
92 /// @brief Determine if a name is in the symbol table
93 bool count(const std::string &name) const {
94 return vmap.count(name);
95 }
96
9790 /// This function can be used from the debugger to display the
9891 /// content of the symbol table while debugging.
9992 /// @brief Print out symbol table on stderr
10396 /// @name Iteration
10497 /// @{
10598 public:
106
10799 /// @brief Get an iterator that from the beginning of the symbol table.
108100 inline iterator begin() { return vmap.begin(); }
109101
115107
116108 /// @brief Get a const_iterator to the end of the symbol table.
117109 inline const_iterator end() const { return vmap.end(); }
118
110
119111 /// @}
120112 /// @name Mutators
121113 /// @{
122114 private:
123115 /// This method adds the provided value \p N to the symbol table. The Value
124116 /// must have a name which is used to place the value in the symbol table.
117 /// If the inserted name conflicts, this renames the value.
125118 /// @brief Add a named value to the symbol table
126 void insert(Value *Val);
127
128 /// This method removes a value from the symbol table. The name of the
129 /// Value is extracted from \p Val and used to lookup the Value in the
130 /// symbol table. \p Val is not deleted, just removed from the symbol table.
131 /// @brief Remove a value from the symbol table.
132 void remove(Value* Val);
119 void reinsertValue(Value *V);
120
121 /// createValueName - This method attempts to create a value name and insert
122 /// it into the symbol table with the specified name. If it conflicts, it
123 /// auto-renames the name and returns that instead.
124 ValueName *createValueName(const char *NameStart, unsigned NameLen, Value *V);
125
126 /// This method removes a value from the symbol table. It leaves the
127 /// ValueName attached to the value, but it is no longer inserted in the
128 /// symtab.
129 void removeValueName(ValueName *V);
133130
134131 /// @}
135132 /// @name Internal Data
198198 void SlotCalculator::processValueSymbolTable(const ValueSymbolTable *VST) {
199199 for (ValueSymbolTable::const_iterator VI = VST->begin(), VE = VST->end();
200200 VI != VE; ++VI)
201 CreateSlotIfNeeded(VI->second);
201 CreateSlotIfNeeded(VI->getValue());
202202 }
203203
204204 void SlotCalculator::CreateSlotIfNeeded(const Value *V) {
131131 output_vbr((unsigned)i << 1); // Low order bit is clear.
132132 }
133133
134 inline void BytecodeWriter::output(const std::string &s) {
135 unsigned Len = s.length();
134 inline void BytecodeWriter::output_str(const char *Str, unsigned Len) {
136135 output_vbr(Len); // Strings may have an arbitrary length.
137 Out.insert(Out.end(), s.begin(), s.end());
136 Out.insert(Out.end(), Str, Str+Len);
138137 }
139138
140139 inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
10871086 true/*ElideIfEmpty*/);
10881087
10891088 // Organize the symbol table by type
1090 typedef std::pair PlaneMapEntry;
1091 typedef SmallVector<PlaneMapEntry, 8> PlaneMapVector;
1089 typedef SmallVector<const ValueName*, 8> PlaneMapVector;
10921090 typedef DenseMap PlaneMap;
10931091 PlaneMap Planes;
10941092 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
10951093 SI != SE; ++SI)
1096 Planes[SI->second->getType()]
1097 .push_back(std::make_pair(&SI->first, SI->second));
1094 Planes[SI->getValue()->getType()].push_back(&*SI);
10981095
10991096 for (PlaneMap::iterator PI = Planes.begin(), PE = Planes.end();
11001097 PI != PE; ++PI) {
11121109 // Write each of the values in this plane
11131110 for (; I != End; ++I) {
11141111 // Symtab entry: [def slot #][name]
1115 output_vbr(Table.getSlot(I->second));
1116 output(*I->first);
1112 output_vbr(Table.getSlot((*I)->getValue()));
1113 output_str((*I)->getKeyData(), (*I)->getKeyLength());
11171114 }
11181115 }
11191116 }
8484 /// @brief Signed 32-bit variable bit rate output primitive.
8585 inline void output_vbr(int i);
8686
87 inline void output(const std::string &s);
87 inline void output_str(const char *Str, unsigned Len);
88 inline void output(const std::string &s) {
89 output_str(&s[0], s.size());
90 }
8891
8992 inline void output_data(const void *Ptr, const void *End);
9093
7676 //
7777 static void StripSymtab(ValueSymbolTable &ST) {
7878 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
79 Value *V = VI->second;
79 Value *V = VI->getValue();
8080 ++VI;
8181 if (!isa(V) || cast(V)->hasInternalLinkage()) {
8282 // Set name to "", removing from symbol table!
6161
6262 BasicBlock::BasicBlock(const std::string &Name, Function *Parent,
6363 BasicBlock *InsertBefore)
64 : Value(Type::LabelTy, Value::BasicBlockVal, Name) {
64 : Value(Type::LabelTy, Value::BasicBlockVal) {
6565 // Initialize the instlist...
6666 InstList.setItemParent(this);
6767
7575 } else if (Parent) {
7676 Parent->getBasicBlockList().push_back(this);
7777 }
78
79 setName(Name);
7880 }
7981
8082
5050 //===----------------------------------------------------------------------===//
5151
5252 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
53 : Value(Ty, Value::ArgumentVal, Name) {
53 : Value(Ty, Value::ArgumentVal) {
5454 Parent = 0;
5555
5656 // Make sure that we get added to a function
5858
5959 if (Par)
6060 Par->getArgumentList().push_back(this);
61 setName(Name);
6162 }
6263
6364 void Argument::setParent(Function *parent) {
1818
1919 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
2020 const std::string &Name, Instruction *InsertBefore)
21 : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) {
21 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
2222 // Make sure that we get added to a basicblock
2323 LeakDetector::addGarbageObject(this);
2424
2828 "Instruction to insert before is not in a basic block!");
2929 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
3030 }
31 setName(Name);
3132 }
3233
3334 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
3435 const std::string &Name, BasicBlock *InsertAtEnd)
35 : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) {
36 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
3637 // Make sure that we get added to a basicblock
3738 LeakDetector::addGarbageObject(this);
3839
3940 // append this instruction into the basic block
4041 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
4142 InsertAtEnd->getInstList().push_back(this);
43 setName(Name);
4244 }
4345
4446 // Out of line virtual method, so the vtable, etc has a home.
3131 ValueSymbolTable &SymTab = SymTabObject->getValueSymbolTable();
3232 for (typename iplist::iterator I = List.begin();
3333 I != List.end(); ++I)
34 if (I->hasName()) SymTab.remove(I);
34 if (I->hasName()) SymTab.removeValueName(I->getValueName());
3535 }
3636
3737 SymTabObject = STO;
4141 ValueSymbolTable &SymTab = SymTabObject->getValueSymbolTable();
4242 for (typename iplist::iterator I = List.begin();
4343 I != List.end(); ++I)
44 if (I->hasName()) SymTab.insert(I);
44 if (I->hasName()) SymTab.reinsertValue(I);
4545 }
4646 }
4747
5252 assert(V->getParent() == 0 && "Value already in a container!!");
5353 V->setParent(ItemParent);
5454 if (V->hasName() && SymTabObject)
55 SymTabObject->getValueSymbolTable().insert(V);
55 SymTabObject->getValueSymbolTable().reinsertValue(V);
5656 }
5757
5858 template
6161 ::removeNodeFromList(ValueSubClass *V) {
6262 V->setParent(0);
6363 if (V->hasName() && SymTabObject)
64 SymTabObject->getValueSymbolTable().remove(V);
64 SymTabObject->getValueSymbolTable().removeValueName(V->getValueName());
6565 }
6666
6767 template
8282 ValueSubClass &V = *first;
8383 bool HasName = V.hasName();
8484 if (OldSTO && HasName)
85 OldSTO->getValueSymbolTable().remove(&V);
85 OldSTO->getValueSymbolTable().removeValueName(V.getValueName());
8686 V.setParent(NewIP);
8787 if (NewSTO && HasName)
88 NewSTO->getValueSymbolTable().insert(&V);
88 NewSTO->getValueSymbolTable().reinsertValue(&V);
8989 }
9090 } else {
9191 // Just transferring between blocks in the same function, simply update the
2929 return Ty;
3030 }
3131
32 Value::Value(const Type *ty, unsigned scid, const std::string &name)
32 Value::Value(const Type *ty, unsigned scid)
3333 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
34 UseList(0), Name(name) {
34 UseList(0), Name(0) {
3535 if (!isa(this) && !isa(this))
3636 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
3737 isa(ty)) &&
3838 "Cannot create non-first-class values except for constants!");
39 if (ty == Type::VoidTy)
40 assert(name.empty() && "Cannot have named void values!");
4139 }
4240
4341 Value::~Value() {
113111 return false;
114112 }
115113
114 std::string Value::getName() const {
115 if (Name == 0) return "";
116 return std::string(Name->getKeyData(),
117 Name->getKeyData()+Name->getKeyLength());
118 }
119
116120 void Value::setName(const std::string &name) {
117 if (Name == name) return; // Name is already set.
118
121 if (name.empty() && !hasName()) return;
122 if (getType() != Type::VoidTy && "Cannot assign a name to void values!");
123
124
119125 // Get the symbol table to update for this object.
120126 ValueSymbolTable *ST;
121127 if (getSymTab(this, ST))
122128 return; // Cannot set a name on this value (e.g. constant).
123129
124 if (!ST) // No symbol table to update? Just do the change.
125 Name = name;
126 else if (hasName()) {
127 if (!name.empty()) { // Replacing name.
128 ST->remove(this);
129 Name = name;
130 ST->insert(this);
131 } else { // Transitioning from hasName -> noname.
132 ST->remove(this);
133 Name.clear();
130 if (!ST) { // No symbol table to update? Just do the change.
131 if (name.empty()) {
132 // Free the name for this value.
133 Name->Destroy();
134 Name = 0;
135 } else {
136 if (Name) {
137 // Name isn't changing.
138 if (name.size() == Name->getKeyLength() &&
139 !memcmp(Name->getKeyData(), &name[0], name.size()))
140 return;
141 Name->Destroy();
142 }
143
144 // Create the new name.
145 Name = ValueName::Create(&name[0], &name[name.size()]);
146 Name->setValue(this);
134147 }
135 } else { // Transitioning from noname -> hasName.
136 Name = name;
137 ST->insert(this);
138 }
148 return;
149 }
150
151 // NOTE: Could optimize for the case the name is shrinking to not deallocate
152 // then reallocated.
153 if (hasName()) {
154 // Name isn't changing?
155 if (name.size() == Name->getKeyLength() &&
156 !memcmp(Name->getKeyData(), &name[0], name.size()))
157 return;
158
159 // Remove old name.
160 ST->removeValueName(Name);
161 Name->Destroy();
162 Name = 0;
163
164 if (name.empty())
165 return;
166 }
167
168 // Name is changing to something new.
169 Name = ST->createValueName(&name[0], name.size(), this);
139170 }
140171
141172 /// takeName - transfer the name from V to this value, setting V's name to
1616 #include "llvm/ValueSymbolTable.h"
1717 #include "llvm/ADT/StringExtras.h"
1818 #include "llvm/Support/Debug.h"
19 #include
2019 using namespace llvm;
2120
2221 // Class destructor
2423 #ifndef NDEBUG // Only do this in -g mode...
2524 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
2625 DEBUG(DOUT << "Value still in symbol table! Type = '"
27 << VI->second->getType()->getDescription() << "' Name = '"
28 << VI->first << "'\n");
26 << VI->getValue()->getType()->getDescription() << "' Name = '"
27 << VI->getKeyData() << "'\n");
2928 assert(vmap.empty() && "Values remain in symbol table!");
3029 #endif
3130 }
3635 //
3736 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
3837 std::string TryName = BaseName;
39 const_iterator End = vmap.end();
4038
4139 // See if the name exists
42 while (vmap.find(TryName) != End) // Loop until we find a free
43 TryName = BaseName + utostr(++LastUnique); // name in the symbol table
40 while (vmap.find(&TryName[0], &TryName[TryName.size()]) != vmap.end())
41 // Loop until we find a free name in the symbol table.
42 TryName = BaseName + utostr(++LastUnique);
4443 return TryName;
4544 }
4645
4847 // lookup a value - Returns null on failure...
4948 //
5049 Value *ValueSymbolTable::lookup(const std::string &Name) const {
51 const_iterator VI = vmap.find(Name);
50 const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
5251 if (VI != vmap.end()) // We found the symbol
53 return const_cast(VI->second);
52 return VI->getValue();
5453 return 0;
5554 }
5655
5756 // Insert a value into the symbol table with the specified name...
5857 //
59 void ValueSymbolTable::insert(Value* V) {
60 assert(V && "Can't insert null Value into symbol table!");
58 void ValueSymbolTable::reinsertValue(Value* V) {
6159 assert(V->hasName() && "Can't insert nameless Value into symbol table");
6260
6361 // Try inserting the name, assuming it won't conflict.
64 if (vmap.insert(make_pair(V->Name, V)).second) {
62 if (vmap.insert(V->Name)) {
6563 DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
6664 return;
6765 }
6866
67 // FIXME: this could be much more efficient.
68
6969 // Otherwise, there is a naming conflict. Rename this value.
7070 std::string UniqueName = V->getName();
71
72 V->Name->Destroy();
73
7174 unsigned BaseSize = UniqueName.size();
72 do {
75 while (1) {
7376 // Trim any suffix off.
7477 UniqueName.resize(BaseSize);
7578 UniqueName += utostr(++LastUnique);
7679 // Try insert the vmap entry with this suffix.
77 } while (!vmap.insert(make_pair(UniqueName, V)).second);
78
79 V->Name = UniqueName;
80
81 DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
80 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
81 &UniqueName[UniqueName.size()]);
82 if (NewName.getValue() == 0) {
83 // Newly inserted name. Success!
84 NewName.setValue(V);
85 V->Name = &NewName;
86 DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
87 return;
88 }
89 }
8290 }
8391
84 // Remove a value
85 void ValueSymbolTable::remove(Value *V) {
86 assert(V->hasName() && "Value doesn't have name!");
87 iterator Entry = vmap.find(V->getName());
88 assert(Entry != vmap.end() && "Entry was not in the symtab!");
89
90 DEBUG(DOUT << " Removing Value: " << Entry->second->getName() << "\n");
91
92 // Remove the value from the plane...
93 vmap.erase(Entry);
92 void ValueSymbolTable::removeValueName(ValueName *V) {
93 DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
94 // Remove the value from the plane.
95 vmap.remove(V);
9496 }
9597
96 // DumpVal - a std::for_each function for dumping a value
97 //
98 static void DumpVal(const std::pair &V) {
99 DOUT << " '" << V.first << "' = ";
100 V.second->dump();
101 DOUT << "\n";
98 /// createValueName - This method attempts to create a value name and insert
99 /// it into the symbol table with the specified name. If it conflicts, it
100 /// auto-renames the name and returns that instead.
101 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
102 unsigned NameLen, Value *V) {
103 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
104 if (Entry.getValue() == 0) {
105 Entry.setValue(V);
106 DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
107 << *V << "\n");
108 return &Entry;
109 }
110
111 // FIXME: this could be much more efficient.
112
113 // Otherwise, there is a naming conflict. Rename this value.
114 std::string UniqueName(NameStart, NameStart+NameLen);
115 while (1) {
116 // Trim any suffix off.
117 UniqueName.resize(NameLen);
118 UniqueName += utostr(++LastUnique);
119 // Try insert the vmap entry with this suffix.
120 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
121 &UniqueName[UniqueName.size()]);
122 if (NewName.getValue() == 0) {
123 // Newly inserted name. Success!
124 NewName.setValue(V);
125 DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
126 return &NewName;
127 }
128 }
102129 }
130
103131
104132 // dump - print out the symbol table
105133 //
106134 void ValueSymbolTable::dump() const {
107135 DOUT << "ValueSymbolTable:\n";
108 for_each(vmap.begin(), vmap.end(), DumpVal);
136 for (const_iterator I = begin(), E = end(); I != E; ++I) {
137 DOUT << " '" << I->getKeyData() << "' = ";
138 I->getValue()->dump();
139 DOUT << "\n";
140 }
109141 }
5050 #include "llvm/Instructions.h"
5151 #include "llvm/Intrinsics.h"
5252 #include "llvm/PassManager.h"
53 #include "llvm/ValueSymbolTable.h"
5453 #include "llvm/Analysis/Dominators.h"
5554 #include "llvm/Support/CFG.h"
5655 #include "llvm/Support/InstVisitor.h"
101100 bool doInitialization(Module &M) {
102101 Mod = &M;
103102 verifyTypeSymbolTable(M.getTypeSymbolTable());
104 verifyValueSymbolTable(M.getValueSymbolTable());
105103
106104 // If this is a real pass, in a pass manager, we must abort before
107105 // returning back to the pass manager, or else the pass manager may try to
176174
177175 // Verification methods...
178176 void verifyTypeSymbolTable(TypeSymbolTable &ST);
179 void verifyValueSymbolTable(ValueSymbolTable &ST);
180177 void visitGlobalValue(GlobalValue &GV);
181178 void visitGlobalVariable(GlobalVariable &GV);
182179 void visitFunction(Function &F);
306303 void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) {
307304 }
308305
309 // verifySymbolTable - Verify that a function or module symbol table is ok
310 //
311 void Verifier::verifyValueSymbolTable(ValueSymbolTable &ST) {
312
313 // Loop over all of the values in the symbol table.
314 for (ValueSymbolTable::const_iterator VI = ST.begin(), VE = ST.end();
315 VI != VE; ++VI) {
316 Value *V = VI->second;
317 // Check that there are no void typed values in the symbol table. Values
318 // with a void type cannot be put into symbol tables because they cannot
319 // have names!
320 Assert1(V->getType() != Type::VoidTy,
321 "Values with void type are not allowed to have names!", V);
322 }
323 }
324
325306 // visitFunction - Verify that a function is ok.
326307 //
327308 void Verifier::visitFunction(Function &F) {
374355 Assert1(F.getName().substr(0, 5) != "llvm.",
375356 "llvm intrinsics cannot be defined!", &F);
376357
377 verifyValueSymbolTable(F.getValueSymbolTable());
378
379358 // Check the entry node
380359 BasicBlock *Entry = &F.getEntryBlock();
381360 Assert1(pred_begin(Entry) == pred_end(Entry),