LLVM API Documentation
00001 //===-- Metadata.cpp - Implement Metadata classes -------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Metadata classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/IR/Metadata.h" 00015 #include "LLVMContextImpl.h" 00016 #include "SymbolTableListTraitsImpl.h" 00017 #include "llvm/ADT/DenseMap.h" 00018 #include "llvm/ADT/STLExtras.h" 00019 #include "llvm/ADT/SmallSet.h" 00020 #include "llvm/ADT/SmallString.h" 00021 #include "llvm/ADT/StringMap.h" 00022 #include "llvm/IR/ConstantRange.h" 00023 #include "llvm/IR/Instruction.h" 00024 #include "llvm/IR/LLVMContext.h" 00025 #include "llvm/IR/LeakDetector.h" 00026 #include "llvm/IR/Module.h" 00027 #include "llvm/IR/ValueHandle.h" 00028 using namespace llvm; 00029 00030 //===----------------------------------------------------------------------===// 00031 // MDString implementation. 00032 // 00033 00034 void MDString::anchor() { } 00035 00036 MDString::MDString(LLVMContext &C) 00037 : Value(Type::getMetadataTy(C), Value::MDStringVal) {} 00038 00039 MDString *MDString::get(LLVMContext &Context, StringRef Str) { 00040 LLVMContextImpl *pImpl = Context.pImpl; 00041 StringMapEntry<Value*> &Entry = 00042 pImpl->MDStringCache.GetOrCreateValue(Str); 00043 Value *&S = Entry.getValue(); 00044 if (!S) S = new MDString(Context); 00045 S->setValueName(&Entry); 00046 return cast<MDString>(S); 00047 } 00048 00049 //===----------------------------------------------------------------------===// 00050 // MDNodeOperand implementation. 00051 // 00052 00053 // Use CallbackVH to hold MDNode operands. 00054 namespace llvm { 00055 class MDNodeOperand : public CallbackVH { 00056 MDNode *getParent() { 00057 MDNodeOperand *Cur = this; 00058 00059 while (Cur->getValPtrInt() != 1) 00060 --Cur; 00061 00062 assert(Cur->getValPtrInt() == 1 && 00063 "Couldn't find the beginning of the operand list!"); 00064 return reinterpret_cast<MDNode*>(Cur) - 1; 00065 } 00066 00067 public: 00068 MDNodeOperand(Value *V) : CallbackVH(V) {} 00069 virtual ~MDNodeOperand(); 00070 00071 void set(Value *V) { 00072 unsigned IsFirst = this->getValPtrInt(); 00073 this->setValPtr(V); 00074 this->setAsFirstOperand(IsFirst); 00075 } 00076 00077 /// setAsFirstOperand - Accessor method to mark the operand as the first in 00078 /// the list. 00079 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); } 00080 00081 void deleted() override; 00082 void allUsesReplacedWith(Value *NV) override; 00083 }; 00084 } // end namespace llvm. 00085 00086 // Provide out-of-line definition to prevent weak vtable. 00087 MDNodeOperand::~MDNodeOperand() {} 00088 00089 void MDNodeOperand::deleted() { 00090 getParent()->replaceOperand(this, nullptr); 00091 } 00092 00093 void MDNodeOperand::allUsesReplacedWith(Value *NV) { 00094 getParent()->replaceOperand(this, NV); 00095 } 00096 00097 //===----------------------------------------------------------------------===// 00098 // MDNode implementation. 00099 // 00100 00101 /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on 00102 /// the end of the MDNode. 00103 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) { 00104 // Use <= instead of < to permit a one-past-the-end address. 00105 assert(Op <= N->getNumOperands() && "Invalid operand number"); 00106 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op; 00107 } 00108 00109 void MDNode::replaceOperandWith(unsigned i, Value *Val) { 00110 MDNodeOperand *Op = getOperandPtr(this, i); 00111 replaceOperand(Op, Val); 00112 } 00113 00114 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal) 00115 : Value(Type::getMetadataTy(C), Value::MDNodeVal) { 00116 NumOperands = Vals.size(); 00117 00118 if (isFunctionLocal) 00119 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit); 00120 00121 // Initialize the operand list, which is co-allocated on the end of the node. 00122 unsigned i = 0; 00123 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; 00124 Op != E; ++Op, ++i) { 00125 new (Op) MDNodeOperand(Vals[i]); 00126 00127 // Mark the first MDNodeOperand as being the first in the list of operands. 00128 if (i == 0) 00129 Op->setAsFirstOperand(1); 00130 } 00131 } 00132 00133 /// ~MDNode - Destroy MDNode. 00134 MDNode::~MDNode() { 00135 assert((getSubclassDataFromValue() & DestroyFlag) != 0 && 00136 "Not being destroyed through destroy()?"); 00137 LLVMContextImpl *pImpl = getType()->getContext().pImpl; 00138 if (isNotUniqued()) { 00139 pImpl->NonUniquedMDNodes.erase(this); 00140 } else { 00141 pImpl->MDNodeSet.RemoveNode(this); 00142 } 00143 00144 // Destroy the operands. 00145 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; 00146 Op != E; ++Op) 00147 Op->~MDNodeOperand(); 00148 } 00149 00150 static const Function *getFunctionForValue(Value *V) { 00151 if (!V) return nullptr; 00152 if (Instruction *I = dyn_cast<Instruction>(V)) { 00153 BasicBlock *BB = I->getParent(); 00154 return BB ? BB->getParent() : nullptr; 00155 } 00156 if (Argument *A = dyn_cast<Argument>(V)) 00157 return A->getParent(); 00158 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) 00159 return BB->getParent(); 00160 if (MDNode *MD = dyn_cast<MDNode>(V)) 00161 return MD->getFunction(); 00162 return nullptr; 00163 } 00164 00165 #ifndef NDEBUG 00166 static const Function *assertLocalFunction(const MDNode *N) { 00167 if (!N->isFunctionLocal()) return nullptr; 00168 00169 // FIXME: This does not handle cyclic function local metadata. 00170 const Function *F = nullptr, *NewF = nullptr; 00171 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 00172 if (Value *V = N->getOperand(i)) { 00173 if (MDNode *MD = dyn_cast<MDNode>(V)) 00174 NewF = assertLocalFunction(MD); 00175 else 00176 NewF = getFunctionForValue(V); 00177 } 00178 if (!F) 00179 F = NewF; 00180 else 00181 assert((NewF == nullptr || F == NewF) && 00182 "inconsistent function-local metadata"); 00183 } 00184 return F; 00185 } 00186 #endif 00187 00188 // getFunction - If this metadata is function-local and recursively has a 00189 // function-local operand, return the first such operand's parent function. 00190 // Otherwise, return null. getFunction() should not be used for performance- 00191 // critical code because it recursively visits all the MDNode's operands. 00192 const Function *MDNode::getFunction() const { 00193 #ifndef NDEBUG 00194 return assertLocalFunction(this); 00195 #else 00196 if (!isFunctionLocal()) return nullptr; 00197 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00198 if (const Function *F = getFunctionForValue(getOperand(i))) 00199 return F; 00200 return nullptr; 00201 #endif 00202 } 00203 00204 // destroy - Delete this node. Only when there are no uses. 00205 void MDNode::destroy() { 00206 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag); 00207 // Placement delete, then free the memory. 00208 this->~MDNode(); 00209 free(this); 00210 } 00211 00212 /// isFunctionLocalValue - Return true if this is a value that would require a 00213 /// function-local MDNode. 00214 static bool isFunctionLocalValue(Value *V) { 00215 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) || 00216 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()); 00217 } 00218 00219 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals, 00220 FunctionLocalness FL, bool Insert) { 00221 LLVMContextImpl *pImpl = Context.pImpl; 00222 00223 // Add all the operand pointers. Note that we don't have to add the 00224 // isFunctionLocal bit because that's implied by the operands. 00225 // Note that if the operands are later nulled out, the node will be 00226 // removed from the uniquing map. 00227 FoldingSetNodeID ID; 00228 for (Value *V : Vals) 00229 ID.AddPointer(V); 00230 00231 void *InsertPoint; 00232 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); 00233 00234 if (N || !Insert) 00235 return N; 00236 00237 bool isFunctionLocal = false; 00238 switch (FL) { 00239 case FL_Unknown: 00240 for (Value *V : Vals) { 00241 if (!V) continue; 00242 if (isFunctionLocalValue(V)) { 00243 isFunctionLocal = true; 00244 break; 00245 } 00246 } 00247 break; 00248 case FL_No: 00249 isFunctionLocal = false; 00250 break; 00251 case FL_Yes: 00252 isFunctionLocal = true; 00253 break; 00254 } 00255 00256 // Coallocate space for the node and Operands together, then placement new. 00257 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand)); 00258 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal); 00259 00260 // Cache the operand hash. 00261 N->Hash = ID.ComputeHash(); 00262 00263 // InsertPoint will have been set by the FindNodeOrInsertPos call. 00264 pImpl->MDNodeSet.InsertNode(N, InsertPoint); 00265 00266 return N; 00267 } 00268 00269 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) { 00270 return getMDNode(Context, Vals, FL_Unknown); 00271 } 00272 00273 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, 00274 ArrayRef<Value*> Vals, 00275 bool isFunctionLocal) { 00276 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No); 00277 } 00278 00279 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) { 00280 return getMDNode(Context, Vals, FL_Unknown, false); 00281 } 00282 00283 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) { 00284 MDNode *N = 00285 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand)); 00286 N = new (N) MDNode(Context, Vals, FL_No); 00287 N->setValueSubclassData(N->getSubclassDataFromValue() | 00288 NotUniquedBit); 00289 LeakDetector::addGarbageObject(N); 00290 return N; 00291 } 00292 00293 void MDNode::deleteTemporary(MDNode *N) { 00294 assert(N->use_empty() && "Temporary MDNode has uses!"); 00295 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) && 00296 "Deleting a non-temporary uniqued node!"); 00297 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) && 00298 "Deleting a non-temporary non-uniqued node!"); 00299 assert((N->getSubclassDataFromValue() & NotUniquedBit) && 00300 "Temporary MDNode does not have NotUniquedBit set!"); 00301 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 && 00302 "Temporary MDNode has DestroyFlag set!"); 00303 LeakDetector::removeGarbageObject(N); 00304 N->destroy(); 00305 } 00306 00307 /// getOperand - Return specified operand. 00308 Value *MDNode::getOperand(unsigned i) const { 00309 assert(i < getNumOperands() && "Invalid operand number"); 00310 return *getOperandPtr(const_cast<MDNode*>(this), i); 00311 } 00312 00313 void MDNode::Profile(FoldingSetNodeID &ID) const { 00314 // Add all the operand pointers. Note that we don't have to add the 00315 // isFunctionLocal bit because that's implied by the operands. 00316 // Note that if the operands are later nulled out, the node will be 00317 // removed from the uniquing map. 00318 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00319 ID.AddPointer(getOperand(i)); 00320 } 00321 00322 void MDNode::setIsNotUniqued() { 00323 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit); 00324 LLVMContextImpl *pImpl = getType()->getContext().pImpl; 00325 pImpl->NonUniquedMDNodes.insert(this); 00326 } 00327 00328 // Replace value from this node's operand list. 00329 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { 00330 Value *From = *Op; 00331 00332 // If is possible that someone did GV->RAUW(inst), replacing a global variable 00333 // with an instruction or some other function-local object. If this is a 00334 // non-function-local MDNode, it can't point to a function-local object. 00335 // Handle this case by implicitly dropping the MDNode reference to null. 00336 // Likewise if the MDNode is function-local but for a different function. 00337 if (To && isFunctionLocalValue(To)) { 00338 if (!isFunctionLocal()) 00339 To = nullptr; 00340 else { 00341 const Function *F = getFunction(); 00342 const Function *FV = getFunctionForValue(To); 00343 // Metadata can be function-local without having an associated function. 00344 // So only consider functions to have changed if non-null. 00345 if (F && FV && F != FV) 00346 To = nullptr; 00347 } 00348 } 00349 00350 if (From == To) 00351 return; 00352 00353 // Update the operand. 00354 Op->set(To); 00355 00356 // If this node is already not being uniqued (because one of the operands 00357 // already went to null), then there is nothing else to do here. 00358 if (isNotUniqued()) return; 00359 00360 LLVMContextImpl *pImpl = getType()->getContext().pImpl; 00361 00362 // Remove "this" from the context map. FoldingSet doesn't have to reprofile 00363 // this node to remove it, so we don't care what state the operands are in. 00364 pImpl->MDNodeSet.RemoveNode(this); 00365 00366 // If we are dropping an argument to null, we choose to not unique the MDNode 00367 // anymore. This commonly occurs during destruction, and uniquing these 00368 // brings little reuse. Also, this means we don't need to include 00369 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes. 00370 if (!To) { 00371 setIsNotUniqued(); 00372 return; 00373 } 00374 00375 // Now that the node is out of the folding set, get ready to reinsert it. 00376 // First, check to see if another node with the same operands already exists 00377 // in the set. If so, then this node is redundant. 00378 FoldingSetNodeID ID; 00379 Profile(ID); 00380 void *InsertPoint; 00381 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) { 00382 replaceAllUsesWith(N); 00383 destroy(); 00384 return; 00385 } 00386 00387 // Cache the operand hash. 00388 Hash = ID.ComputeHash(); 00389 // InsertPoint will have been set by the FindNodeOrInsertPos call. 00390 pImpl->MDNodeSet.InsertNode(this, InsertPoint); 00391 00392 // If this MDValue was previously function-local but no longer is, clear 00393 // its function-local flag. 00394 if (isFunctionLocal() && !isFunctionLocalValue(To)) { 00395 bool isStillFunctionLocal = false; 00396 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00397 Value *V = getOperand(i); 00398 if (!V) continue; 00399 if (isFunctionLocalValue(V)) { 00400 isStillFunctionLocal = true; 00401 break; 00402 } 00403 } 00404 if (!isStillFunctionLocal) 00405 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit); 00406 } 00407 } 00408 00409 MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { 00410 if (!A) 00411 return B; 00412 if (!B) 00413 return A; 00414 00415 SmallVector<Value *, 4> Vals(A->getNumOperands() + 00416 B->getNumOperands()); 00417 00418 unsigned j = 0; 00419 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) 00420 Vals[j++] = A->getOperand(i); 00421 for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i) 00422 Vals[j++] = B->getOperand(i); 00423 00424 return MDNode::get(A->getContext(), Vals); 00425 } 00426 00427 MDNode *MDNode::intersect(MDNode *A, MDNode *B) { 00428 if (!A || !B) 00429 return nullptr; 00430 00431 SmallVector<Value *, 4> Vals; 00432 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) { 00433 Value *V = A->getOperand(i); 00434 for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j) 00435 if (V == B->getOperand(j)) { 00436 Vals.push_back(V); 00437 break; 00438 } 00439 } 00440 00441 return MDNode::get(A->getContext(), Vals); 00442 } 00443 00444 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { 00445 if (!A || !B) 00446 return nullptr; 00447 00448 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF(); 00449 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF(); 00450 if (AVal.compare(BVal) == APFloat::cmpLessThan) 00451 return A; 00452 return B; 00453 } 00454 00455 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { 00456 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); 00457 } 00458 00459 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { 00460 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); 00461 } 00462 00463 static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low, 00464 ConstantInt *High) { 00465 ConstantRange NewRange(Low->getValue(), High->getValue()); 00466 unsigned Size = EndPoints.size(); 00467 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue(); 00468 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue(); 00469 ConstantRange LastRange(LB, LE); 00470 if (canBeMerged(NewRange, LastRange)) { 00471 ConstantRange Union = LastRange.unionWith(NewRange); 00472 Type *Ty = High->getType(); 00473 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower()); 00474 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper()); 00475 return true; 00476 } 00477 return false; 00478 } 00479 00480 static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low, 00481 ConstantInt *High) { 00482 if (!EndPoints.empty()) 00483 if (tryMergeRange(EndPoints, Low, High)) 00484 return; 00485 00486 EndPoints.push_back(Low); 00487 EndPoints.push_back(High); 00488 } 00489 00490 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { 00491 // Given two ranges, we want to compute the union of the ranges. This 00492 // is slightly complitade by having to combine the intervals and merge 00493 // the ones that overlap. 00494 00495 if (!A || !B) 00496 return nullptr; 00497 00498 if (A == B) 00499 return A; 00500 00501 // First, walk both lists in older of the lower boundary of each interval. 00502 // At each step, try to merge the new interval to the last one we adedd. 00503 SmallVector<Value*, 4> EndPoints; 00504 int AI = 0; 00505 int BI = 0; 00506 int AN = A->getNumOperands() / 2; 00507 int BN = B->getNumOperands() / 2; 00508 while (AI < AN && BI < BN) { 00509 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI)); 00510 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI)); 00511 00512 if (ALow->getValue().slt(BLow->getValue())) { 00513 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1))); 00514 ++AI; 00515 } else { 00516 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1))); 00517 ++BI; 00518 } 00519 } 00520 while (AI < AN) { 00521 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)), 00522 cast<ConstantInt>(A->getOperand(2 * AI + 1))); 00523 ++AI; 00524 } 00525 while (BI < BN) { 00526 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)), 00527 cast<ConstantInt>(B->getOperand(2 * BI + 1))); 00528 ++BI; 00529 } 00530 00531 // If we have more than 2 ranges (4 endpoints) we have to try to merge 00532 // the last and first ones. 00533 unsigned Size = EndPoints.size(); 00534 if (Size > 4) { 00535 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]); 00536 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]); 00537 if (tryMergeRange(EndPoints, FB, FE)) { 00538 for (unsigned i = 0; i < Size - 2; ++i) { 00539 EndPoints[i] = EndPoints[i + 2]; 00540 } 00541 EndPoints.resize(Size - 2); 00542 } 00543 } 00544 00545 // If in the end we have a single range, it is possible that it is now the 00546 // full range. Just drop the metadata in that case. 00547 if (EndPoints.size() == 2) { 00548 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(), 00549 cast<ConstantInt>(EndPoints[1])->getValue()); 00550 if (Range.isFullSet()) 00551 return nullptr; 00552 } 00553 00554 return MDNode::get(A->getContext(), EndPoints); 00555 } 00556 00557 //===----------------------------------------------------------------------===// 00558 // NamedMDNode implementation. 00559 // 00560 00561 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) { 00562 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands; 00563 } 00564 00565 NamedMDNode::NamedMDNode(const Twine &N) 00566 : Name(N.str()), Parent(nullptr), 00567 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) { 00568 } 00569 00570 NamedMDNode::~NamedMDNode() { 00571 dropAllReferences(); 00572 delete &getNMDOps(Operands); 00573 } 00574 00575 /// getNumOperands - Return number of NamedMDNode operands. 00576 unsigned NamedMDNode::getNumOperands() const { 00577 return (unsigned)getNMDOps(Operands).size(); 00578 } 00579 00580 /// getOperand - Return specified operand. 00581 MDNode *NamedMDNode::getOperand(unsigned i) const { 00582 assert(i < getNumOperands() && "Invalid Operand number!"); 00583 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]); 00584 } 00585 00586 /// addOperand - Add metadata Operand. 00587 void NamedMDNode::addOperand(MDNode *M) { 00588 assert(!M->isFunctionLocal() && 00589 "NamedMDNode operands must not be function-local!"); 00590 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M)); 00591 } 00592 00593 /// eraseFromParent - Drop all references and remove the node from parent 00594 /// module. 00595 void NamedMDNode::eraseFromParent() { 00596 getParent()->eraseNamedMetadata(this); 00597 } 00598 00599 /// dropAllReferences - Remove all uses and clear node vector. 00600 void NamedMDNode::dropAllReferences() { 00601 getNMDOps(Operands).clear(); 00602 } 00603 00604 /// getName - Return a constant reference to this named metadata's name. 00605 StringRef NamedMDNode::getName() const { 00606 return StringRef(Name); 00607 } 00608 00609 //===----------------------------------------------------------------------===// 00610 // Instruction Metadata method implementations. 00611 // 00612 00613 void Instruction::setMetadata(StringRef Kind, MDNode *Node) { 00614 if (!Node && !hasMetadata()) return; 00615 setMetadata(getContext().getMDKindID(Kind), Node); 00616 } 00617 00618 MDNode *Instruction::getMetadataImpl(StringRef Kind) const { 00619 return getMetadataImpl(getContext().getMDKindID(Kind)); 00620 } 00621 00622 void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { 00623 SmallSet<unsigned, 5> KnownSet; 00624 KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); 00625 00626 // Drop debug if needed 00627 if (KnownSet.erase(LLVMContext::MD_dbg)) 00628 DbgLoc = DebugLoc(); 00629 00630 if (!hasMetadataHashEntry()) 00631 return; // Nothing to remove! 00632 00633 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore = 00634 getContext().pImpl->MetadataStore; 00635 00636 if (KnownSet.empty()) { 00637 // Just drop our entry at the store. 00638 MetadataStore.erase(this); 00639 setHasMetadataHashEntry(false); 00640 return; 00641 } 00642 00643 LLVMContextImpl::MDMapTy &Info = MetadataStore[this]; 00644 unsigned I; 00645 unsigned E; 00646 // Walk the array and drop any metadata we don't know. 00647 for (I = 0, E = Info.size(); I != E;) { 00648 if (KnownSet.count(Info[I].first)) { 00649 ++I; 00650 continue; 00651 } 00652 00653 Info[I] = Info.back(); 00654 Info.pop_back(); 00655 --E; 00656 } 00657 assert(E == Info.size()); 00658 00659 if (E == 0) { 00660 // Drop our entry at the store. 00661 MetadataStore.erase(this); 00662 setHasMetadataHashEntry(false); 00663 } 00664 } 00665 00666 /// setMetadata - Set the metadata of of the specified kind to the specified 00667 /// node. This updates/replaces metadata if already present, or removes it if 00668 /// Node is null. 00669 void Instruction::setMetadata(unsigned KindID, MDNode *Node) { 00670 if (!Node && !hasMetadata()) return; 00671 00672 // Handle 'dbg' as a special case since it is not stored in the hash table. 00673 if (KindID == LLVMContext::MD_dbg) { 00674 DbgLoc = DebugLoc::getFromDILocation(Node); 00675 return; 00676 } 00677 00678 // Handle the case when we're adding/updating metadata on an instruction. 00679 if (Node) { 00680 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; 00681 assert(!Info.empty() == hasMetadataHashEntry() && 00682 "HasMetadata bit is wonked"); 00683 if (Info.empty()) { 00684 setHasMetadataHashEntry(true); 00685 } else { 00686 // Handle replacement of an existing value. 00687 for (auto &P : Info) 00688 if (P.first == KindID) { 00689 P.second = Node; 00690 return; 00691 } 00692 } 00693 00694 // No replacement, just add it to the list. 00695 Info.push_back(std::make_pair(KindID, Node)); 00696 return; 00697 } 00698 00699 // Otherwise, we're removing metadata from an instruction. 00700 assert((hasMetadataHashEntry() == 00701 (getContext().pImpl->MetadataStore.count(this) > 0)) && 00702 "HasMetadata bit out of date!"); 00703 if (!hasMetadataHashEntry()) 00704 return; // Nothing to remove! 00705 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; 00706 00707 // Common case is removing the only entry. 00708 if (Info.size() == 1 && Info[0].first == KindID) { 00709 getContext().pImpl->MetadataStore.erase(this); 00710 setHasMetadataHashEntry(false); 00711 return; 00712 } 00713 00714 // Handle removal of an existing value. 00715 for (unsigned i = 0, e = Info.size(); i != e; ++i) 00716 if (Info[i].first == KindID) { 00717 Info[i] = Info.back(); 00718 Info.pop_back(); 00719 assert(!Info.empty() && "Removing last entry should be handled above"); 00720 return; 00721 } 00722 // Otherwise, removing an entry that doesn't exist on the instruction. 00723 } 00724 00725 void Instruction::setAAMetadata(const AAMDNodes &N) { 00726 setMetadata(LLVMContext::MD_tbaa, N.TBAA); 00727 setMetadata(LLVMContext::MD_alias_scope, N.Scope); 00728 setMetadata(LLVMContext::MD_noalias, N.NoAlias); 00729 } 00730 00731 MDNode *Instruction::getMetadataImpl(unsigned KindID) const { 00732 // Handle 'dbg' as a special case since it is not stored in the hash table. 00733 if (KindID == LLVMContext::MD_dbg) 00734 return DbgLoc.getAsMDNode(getContext()); 00735 00736 if (!hasMetadataHashEntry()) return nullptr; 00737 00738 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; 00739 assert(!Info.empty() && "bit out of sync with hash table"); 00740 00741 for (const auto &I : Info) 00742 if (I.first == KindID) 00743 return I.second; 00744 return nullptr; 00745 } 00746 00747 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, 00748 MDNode*> > &Result) const { 00749 Result.clear(); 00750 00751 // Handle 'dbg' as a special case since it is not stored in the hash table. 00752 if (!DbgLoc.isUnknown()) { 00753 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, 00754 DbgLoc.getAsMDNode(getContext()))); 00755 if (!hasMetadataHashEntry()) return; 00756 } 00757 00758 assert(hasMetadataHashEntry() && 00759 getContext().pImpl->MetadataStore.count(this) && 00760 "Shouldn't have called this"); 00761 const LLVMContextImpl::MDMapTy &Info = 00762 getContext().pImpl->MetadataStore.find(this)->second; 00763 assert(!Info.empty() && "Shouldn't have called this"); 00764 00765 Result.append(Info.begin(), Info.end()); 00766 00767 // Sort the resulting array so it is stable. 00768 if (Result.size() > 1) 00769 array_pod_sort(Result.begin(), Result.end()); 00770 } 00771 00772 void Instruction:: 00773 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, 00774 MDNode*> > &Result) const { 00775 Result.clear(); 00776 assert(hasMetadataHashEntry() && 00777 getContext().pImpl->MetadataStore.count(this) && 00778 "Shouldn't have called this"); 00779 const LLVMContextImpl::MDMapTy &Info = 00780 getContext().pImpl->MetadataStore.find(this)->second; 00781 assert(!Info.empty() && "Shouldn't have called this"); 00782 Result.append(Info.begin(), Info.end()); 00783 00784 // Sort the resulting array so it is stable. 00785 if (Result.size() > 1) 00786 array_pod_sort(Result.begin(), Result.end()); 00787 } 00788 00789 /// clearMetadataHashEntries - Clear all hashtable-based metadata from 00790 /// this instruction. 00791 void Instruction::clearMetadataHashEntries() { 00792 assert(hasMetadataHashEntry() && "Caller should check"); 00793 getContext().pImpl->MetadataStore.erase(this); 00794 setHasMetadataHashEntry(false); 00795 } 00796