llvm.org GIT mirror llvm / 3ecb447
The MDString class stored a StringRef to the string which was already in a StringMap. This was redundant and unnecessarily bloated the MDString class. Because the MDString class is a "Value" and will never have a "name", and because the Name field in the Value class is a pointer to a StringMap entry, we repurpose the Name field for an MDString. It stores the StringMap entry in the Name field, and uses the normal methods to get the string (name) back. PR12474 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154429 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
5 changed file(s) with 23 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
3838 virtual void anchor();
3939 MDString(const MDString &); // DO NOT IMPLEMENT
4040
41 StringRef Str;
42 explicit MDString(LLVMContext &C, StringRef S);
43
41 explicit MDString(LLVMContext &C);
4442 public:
4543 static MDString *get(LLVMContext &Context, StringRef Str);
4644 static MDString *get(LLVMContext &Context, const char *Str) {
4745 return get(Context, Str ? StringRef(Str) : StringRef());
4846 }
4947
50 StringRef getString() const { return Str; }
51
52 unsigned getLength() const { return (unsigned)Str.size(); }
48 StringRef getString() const { return getName(); }
49
50 unsigned getLength() const { return (unsigned)getName().size(); }
5351
5452 typedef StringRef::iterator iterator;
5553
5654 /// begin() - Pointer to the first byte of the string.
57 ///
58 iterator begin() const { return Str.begin(); }
55 iterator begin() const { return getName().begin(); }
5956
6057 /// end() - Pointer to one byte past the end of the string.
61 ///
62 iterator end() const { return Str.end(); }
58 iterator end() const { return getName().end(); }
6359
6460 /// Methods for support type inquiry through isa, cast, and dyn_cast:
6561 static inline bool classof(const MDString *) { return true; }
106106 /// All values hold a context through their type.
107107 LLVMContext &getContext() const;
108108
109 // All values can potentially be named...
110 bool hasName() const { return Name != 0; }
109 // All values can potentially be named.
110 bool hasName() const { return Name != 0 && SubclassID != MDStringVal; }
111111 ValueName *getValueName() const { return Name; }
112 void setValueName(ValueName *VN) { Name = VN; }
112113
113114 /// getName() - Return a constant reference to the value's name. This is cheap
114115 /// and guaranteed to return the same reference as long as the value is not
233233 DenseMapAPFloatKeyInfo> FPMapTy;
234234 FPMapTy FPConstants;
235235
236 StringMap<MDString*> MDStringCache;
236 StringMap<Value*> MDStringCache;
237237
238238 FoldingSet MDNodeSet;
239239 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
3030
3131 void MDString::anchor() { }
3232
33 MDString::MDString(LLVMContext &C, StringRef S)
34 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
33 MDString::MDString(LLVMContext &C)
34 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
3535
3636 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
3737 LLVMContextImpl *pImpl = Context.pImpl;
38 StringMapEntry<MDString *> &Entry =
38 StringMapEntry<Value*> &Entry =
3939 pImpl->MDStringCache.GetOrCreateValue(Str);
40 MDString *&S = Entry.getValue();
41 if (!S) S = new MDString(Context, Entry.getKey());
42 return S;
40 Value *&S = Entry.getValue();
41 if (!S) S = new MDString(Context);
42 S->setValueName(&Entry);
43 return cast(S);
4344 }
4445
4546 //===----------------------------------------------------------------------===//
7575
7676 // If this value is named, destroy the name. This should not be in a symtab
7777 // at this point.
78 if (Name)
78 if (Name && SubclassID != MDStringVal)
7979 Name->Destroy();
8080
8181 // There should be no uses of this object anymore, remove it.
169169 }
170170
171171 void Value::setName(const Twine &NewName) {
172 assert(SubclassID != MDStringVal &&
173 "Cannot set the name of MDString with this method!");
174
172175 // Fast path for common IRBuilder case of setName("") when there is no name.
173176 if (NewName.isTriviallyEmpty() && !hasName())
174177 return;
227230 /// takeName - transfer the name from V to this value, setting V's name to
228231 /// empty. It is an error to call V->takeName(V).
229232 void Value::takeName(Value *V) {
233 assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
234
230235 ValueSymbolTable *ST = 0;
231236 // If this value has a name, drop it.
232237 if (hasName()) {