+MDTuple *MDTuple::uniquifyImpl() {
+ recalculateHash();
+ MDTupleInfo::KeyTy Key(this);
+
+ auto &Store = getContext().pImpl->MDTuples;
+ auto I = Store.find_as(Key);
+ if (I == Store.end()) {
+ Store.insert(this);
+ return this;
+ }
+ return *I;
+}
+
+void MDTuple::eraseFromStoreImpl() { getContext().pImpl->MDTuples.erase(this); }
+
+MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
+ unsigned Column, ArrayRef<Metadata *> MDs)
+ : UniquableMDNode(C, MDLocationKind, Storage, MDs) {
+ assert((MDs.size() == 1 || MDs.size() == 2) &&
+ "Expected a scope and optional inlined-at");
+
+ // Set line and column.
+ assert(Line < (1u << 24) && "Expected 24-bit line");
+ assert(Column < (1u << 16) && "Expected 16-bit column");
+
+ MDNodeSubclassData = Line;
+ SubclassData16 = Column;
+}
+
+MDLocation *MDLocation::constructHelper(LLVMContext &Context,
+ StorageType Storage, unsigned Line,
+ unsigned Column, Metadata *Scope,
+ Metadata *InlinedAt) {
+ SmallVector<Metadata *, 2> Ops;
+ Ops.push_back(Scope);
+ if (InlinedAt)
+ Ops.push_back(InlinedAt);
+ return new (Ops.size()) MDLocation(Context, Storage, Line, Column, Ops);
+}
+
+static void adjustLine(unsigned &Line) {
+ // Set to unknown on overflow. Still use 24 bits for now.
+ if (Line >= (1u << 24))
+ Line = 0;
+}
+
+static void adjustColumn(unsigned &Column) {
+ // Set to unknown on overflow. We only have 16 bits to play with here.
+ if (Column >= (1u << 16))
+ Column = 0;
+}
+
+MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
+ unsigned Column, Metadata *Scope,
+ Metadata *InlinedAt, bool ShouldCreate) {
+ // Fixup line/column.
+ adjustLine(Line);
+ adjustColumn(Column);
+
+ MDLocationInfo::KeyTy Key(Line, Column, Scope, InlinedAt);
+
+ auto &Store = Context.pImpl->MDLocations;
+ auto I = Store.find_as(Key);
+ if (I != Store.end())
+ return *I;
+ if (!ShouldCreate)
+ return nullptr;
+
+ auto *N = constructHelper(Context, Uniqued, Line, Column, Scope, InlinedAt);
+ Store.insert(N);