~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/VMCore/Metadata.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// This file implements the Metadata classes.
 
11
//
 
12
//===----------------------------------------------------------------------===//
 
13
 
 
14
#include "llvm/Metadata.h"
 
15
#include "LLVMContextImpl.h"
 
16
#include "llvm/LLVMContext.h"
 
17
#include "llvm/Module.h"
 
18
#include "llvm/Instruction.h"
 
19
#include "llvm/ADT/DenseMap.h"
 
20
#include "llvm/ADT/StringMap.h"
 
21
#include "llvm/ADT/SmallString.h"
 
22
#include "SymbolTableListTraitsImpl.h"
 
23
#include "llvm/Support/ValueHandle.h"
 
24
using namespace llvm;
 
25
 
 
26
//===----------------------------------------------------------------------===//
 
27
// MDString implementation.
 
28
//
 
29
 
 
30
MDString::MDString(LLVMContext &C, StringRef S)
 
31
  : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
 
32
 
 
33
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
 
34
  LLVMContextImpl *pImpl = Context.pImpl;
 
35
  StringMapEntry<MDString *> &Entry =
 
36
    pImpl->MDStringCache.GetOrCreateValue(Str);
 
37
  MDString *&S = Entry.getValue();
 
38
  if (!S) S = new MDString(Context, Entry.getKey());
 
39
  return S;
 
40
}
 
41
 
 
42
MDString *MDString::get(LLVMContext &Context, const char *Str) {
 
43
  LLVMContextImpl *pImpl = Context.pImpl;
 
44
  StringMapEntry<MDString *> &Entry =
 
45
    pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
 
46
  MDString *&S = Entry.getValue();
 
47
  if (!S) S = new MDString(Context, Entry.getKey());
 
48
  return S;
 
49
}
 
50
 
 
51
//===----------------------------------------------------------------------===//
 
52
// MDNodeOperand implementation.
 
53
//
 
54
 
 
55
// Use CallbackVH to hold MDNode operands.
 
56
namespace llvm {
 
57
class MDNodeOperand : public CallbackVH {
 
58
  MDNode *Parent;
 
59
public:
 
60
  MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
 
61
  ~MDNodeOperand() {}
 
62
 
 
63
  void set(Value *V) {
 
64
    setValPtr(V);
 
65
  }
 
66
 
 
67
  virtual void deleted();
 
68
  virtual void allUsesReplacedWith(Value *NV);
 
69
};
 
70
} // end namespace llvm.
 
71
 
 
72
 
 
73
void MDNodeOperand::deleted() {
 
74
  Parent->replaceOperand(this, 0);
 
75
}
 
76
 
 
77
void MDNodeOperand::allUsesReplacedWith(Value *NV) {
 
78
  Parent->replaceOperand(this, NV);
 
79
}
 
80
 
 
81
 
 
82
 
 
83
//===----------------------------------------------------------------------===//
 
84
// MDNode implementation.
 
85
//
 
86
 
 
87
/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
 
88
/// the end of the MDNode.
 
89
static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
 
90
  assert(Op < N->getNumOperands() && "Invalid operand number");
 
91
  return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
 
92
}
 
93
 
 
94
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
 
95
               bool isFunctionLocal)
 
96
: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
 
97
  NumOperands = NumVals;
 
98
 
 
99
  if (isFunctionLocal)
 
100
    setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
 
101
 
 
102
  // Initialize the operand list, which is co-allocated on the end of the node.
 
103
  for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
 
104
       Op != E; ++Op, ++Vals)
 
105
    new (Op) MDNodeOperand(*Vals, this);
 
106
}
 
107
 
 
108
 
 
109
/// ~MDNode - Destroy MDNode.
 
110
MDNode::~MDNode() {
 
111
  assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
 
112
         "Not being destroyed through destroy()?");
 
113
  if (!isNotUniqued()) {
 
114
    LLVMContextImpl *pImpl = getType()->getContext().pImpl;
 
115
    pImpl->MDNodeSet.RemoveNode(this);
 
116
  }
 
117
 
 
118
  // Destroy the operands.
 
119
  for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
 
120
       Op != E; ++Op)
 
121
    Op->~MDNodeOperand();
 
122
}
 
123
 
 
124
static const Function *getFunctionForValue(Value *V) {
 
125
  assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
 
126
  if (!V) return NULL;
 
127
  if (Instruction *I = dyn_cast<Instruction>(V))
 
128
    return I->getParent()->getParent();
 
129
  if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
 
130
    return BB->getParent();
 
131
  if (Argument *A = dyn_cast<Argument>(V))
 
132
    return A->getParent();
 
133
  return NULL;
 
134
}
 
135
 
 
136
#ifndef NDEBUG
 
137
static const Function *assertLocalFunction(const MDNode *N) {
 
138
  if (!N->isFunctionLocal()) return 0;
 
139
 
 
140
  const Function *F = 0, *NewF = 0;
 
141
  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
 
142
    if (Value *V = N->getOperand(i)) {
 
143
      if (MDNode *MD = dyn_cast<MDNode>(V))
 
144
        NewF = assertLocalFunction(MD);
 
145
      else
 
146
        NewF = getFunctionForValue(V);
 
147
    }
 
148
    if (F == 0)
 
149
      F = NewF;
 
150
    else 
 
151
      assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
 
152
  }
 
153
  return F;
 
154
}
 
155
#endif
 
156
 
 
157
// getFunction - If this metadata is function-local and recursively has a
 
158
// function-local operand, return the first such operand's parent function.
 
159
// Otherwise, return null. getFunction() should not be used for performance-
 
160
// critical code because it recursively visits all the MDNode's operands.  
 
161
const Function *MDNode::getFunction() const {
 
162
#ifndef NDEBUG
 
163
  return assertLocalFunction(this);
 
164
#endif
 
165
  if (!isFunctionLocal()) return NULL;
 
166
 
 
167
  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
 
168
    if (Value *V = getOperand(i)) {
 
169
      if (MDNode *MD = dyn_cast<MDNode>(V)) {
 
170
        if (const Function *F = MD->getFunction())
 
171
          return F;
 
172
      } else {
 
173
        return getFunctionForValue(V);
 
174
      }
 
175
    }
 
176
  }
 
177
  return NULL;
 
178
}
 
179
 
 
180
// destroy - Delete this node.  Only when there are no uses.
 
181
void MDNode::destroy() {
 
182
  setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
 
183
  // Placement delete, the free the memory.
 
184
  this->~MDNode();
 
185
  free(this);
 
186
}
 
187
 
 
188
MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
 
189
                          unsigned NumVals, FunctionLocalness FL,
 
190
                          bool Insert) {
 
191
  LLVMContextImpl *pImpl = Context.pImpl;
 
192
  FoldingSetNodeID ID;
 
193
  for (unsigned i = 0; i != NumVals; ++i)
 
194
    ID.AddPointer(Vals[i]);
 
195
 
 
196
  void *InsertPoint;
 
197
  MDNode *N = NULL;
 
198
  
 
199
  if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
 
200
    return N;
 
201
    
 
202
  if (!Insert)
 
203
    return NULL;
 
204
    
 
205
  bool isFunctionLocal = false;
 
206
  switch (FL) {
 
207
  case FL_Unknown:
 
208
    for (unsigned i = 0; i != NumVals; ++i) {
 
209
      Value *V = Vals[i];
 
210
      if (!V) continue;
 
211
      if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
 
212
          (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
 
213
        isFunctionLocal = true;
 
214
        break;
 
215
      }
 
216
    }
 
217
    break;
 
218
  case FL_No:
 
219
    isFunctionLocal = false;
 
220
    break;
 
221
  case FL_Yes:
 
222
    isFunctionLocal = true;
 
223
    break;
 
224
  }
 
225
 
 
226
  // Coallocate space for the node and Operands together, then placement new.
 
227
  void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
 
228
  N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
 
229
 
 
230
  // InsertPoint will have been set by the FindNodeOrInsertPos call.
 
231
  pImpl->MDNodeSet.InsertNode(N, InsertPoint);
 
232
 
 
233
  return N;
 
234
}
 
235
 
 
236
MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
 
237
  return getMDNode(Context, Vals, NumVals, FL_Unknown);
 
238
}
 
239
 
 
240
MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
 
241
                                      unsigned NumVals, bool isFunctionLocal) {
 
242
  return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
 
243
}
 
244
 
 
245
MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
 
246
                            unsigned NumVals) {
 
247
  return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
 
248
}
 
249
 
 
250
/// getOperand - Return specified operand.
 
251
Value *MDNode::getOperand(unsigned i) const {
 
252
  return *getOperandPtr(const_cast<MDNode*>(this), i);
 
253
}
 
254
 
 
255
void MDNode::Profile(FoldingSetNodeID &ID) const {
 
256
  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
 
257
    ID.AddPointer(getOperand(i));
 
258
}
 
259
 
 
260
// replaceAllOperandsWithNull - This is used while destroying llvm context to 
 
261
// gracefully delete all nodes. This method replaces all operands with null.
 
262
void MDNode::replaceAllOperandsWithNull() {
 
263
  for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
 
264
       Op != E; ++Op)
 
265
    replaceOperand(Op, 0);
 
266
}
 
267
 
 
268
// Replace value from this node's operand list.
 
269
void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
 
270
  Value *From = *Op;
 
271
 
 
272
  if (From == To)
 
273
    return;
 
274
 
 
275
  // Update the operand.
 
276
  Op->set(To);
 
277
 
 
278
  // If this node is already not being uniqued (because one of the operands
 
279
  // already went to null), then there is nothing else to do here.
 
280
  if (isNotUniqued()) return;
 
281
 
 
282
  LLVMContextImpl *pImpl = getType()->getContext().pImpl;
 
283
 
 
284
  // Remove "this" from the context map.  FoldingSet doesn't have to reprofile
 
285
  // this node to remove it, so we don't care what state the operands are in.
 
286
  pImpl->MDNodeSet.RemoveNode(this);
 
287
 
 
288
  // If we are dropping an argument to null, we choose to not unique the MDNode
 
289
  // anymore.  This commonly occurs during destruction, and uniquing these
 
290
  // brings little reuse.
 
291
  if (To == 0) {
 
292
    setIsNotUniqued();
 
293
    return;
 
294
  }
 
295
 
 
296
  // Now that the node is out of the folding set, get ready to reinsert it.
 
297
  // First, check to see if another node with the same operands already exists
 
298
  // in the set.  If it doesn't exist, this returns the position to insert it.
 
299
  FoldingSetNodeID ID;
 
300
  Profile(ID);
 
301
  void *InsertPoint;
 
302
  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
 
303
 
 
304
  if (N) {
 
305
    N->replaceAllUsesWith(this);
 
306
    N->destroy();
 
307
    N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
 
308
    assert(N == 0 && "shouldn't be in the map now!"); (void)N;
 
309
  }
 
310
 
 
311
  // InsertPoint will have been set by the FindNodeOrInsertPos call.
 
312
  pImpl->MDNodeSet.InsertNode(this, InsertPoint);
 
313
}
 
314
 
 
315
//===----------------------------------------------------------------------===//
 
316
// NamedMDNode implementation.
 
317
//
 
318
 
 
319
namespace llvm {
 
320
// SymbolTableListTraits specialization for MDSymbolTable.
 
321
void ilist_traits<NamedMDNode>
 
322
::addNodeToList(NamedMDNode *N) {
 
323
  assert(N->getParent() == 0 && "Value already in a container!!");
 
324
  Module *Owner = getListOwner();
 
325
  N->setParent(Owner);
 
326
  MDSymbolTable &ST = Owner->getMDSymbolTable();
 
327
  ST.insert(N->getName(), N);
 
328
}
 
329
 
 
330
void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
 
331
  N->setParent(0);
 
332
  Module *Owner = getListOwner();
 
333
  MDSymbolTable &ST = Owner->getMDSymbolTable();
 
334
  ST.remove(N->getName());
 
335
}
 
336
}
 
337
 
 
338
static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
 
339
  return *(SmallVector<WeakVH, 4>*)Operands;
 
340
}
 
341
 
 
342
NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
 
343
                         MDNode *const *MDs,
 
344
                         unsigned NumMDs, Module *ParentModule)
 
345
  : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
 
346
  setName(N);
 
347
  Operands = new SmallVector<WeakVH, 4>();
 
348
 
 
349
  SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
 
350
  for (unsigned i = 0; i != NumMDs; ++i)
 
351
    Node.push_back(WeakVH(MDs[i]));
 
352
 
 
353
  if (ParentModule)
 
354
    ParentModule->getNamedMDList().push_back(this);
 
355
}
 
356
 
 
357
NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
 
358
  assert(NMD && "Invalid source NamedMDNode!");
 
359
  SmallVector<MDNode *, 4> Elems;
 
360
  Elems.reserve(NMD->getNumOperands());
 
361
 
 
362
  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
 
363
    Elems.push_back(NMD->getOperand(i));
 
364
  return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
 
365
                         Elems.data(), Elems.size(), M);
 
366
}
 
367
 
 
368
NamedMDNode::~NamedMDNode() {
 
369
  dropAllReferences();
 
370
  delete &getNMDOps(Operands);
 
371
}
 
372
 
 
373
/// getNumOperands - Return number of NamedMDNode operands.
 
374
unsigned NamedMDNode::getNumOperands() const {
 
375
  return (unsigned)getNMDOps(Operands).size();
 
376
}
 
377
 
 
378
/// getOperand - Return specified operand.
 
379
MDNode *NamedMDNode::getOperand(unsigned i) const {
 
380
  assert(i < getNumOperands() && "Invalid Operand number!");
 
381
  return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
 
382
}
 
383
 
 
384
/// addOperand - Add metadata Operand.
 
385
void NamedMDNode::addOperand(MDNode *M) {
 
386
  getNMDOps(Operands).push_back(WeakVH(M));
 
387
}
 
388
 
 
389
/// eraseFromParent - Drop all references and remove the node from parent
 
390
/// module.
 
391
void NamedMDNode::eraseFromParent() {
 
392
  getParent()->getNamedMDList().erase(this);
 
393
}
 
394
 
 
395
/// dropAllReferences - Remove all uses and clear node vector.
 
396
void NamedMDNode::dropAllReferences() {
 
397
  getNMDOps(Operands).clear();
 
398
}
 
399
 
 
400
/// setName - Set the name of this named metadata.
 
401
void NamedMDNode::setName(const Twine &NewName) {
 
402
  assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
 
403
 
 
404
  SmallString<256> NameData;
 
405
  StringRef NameRef = NewName.toStringRef(NameData);
 
406
 
 
407
  // Name isn't changing?
 
408
  if (getName() == NameRef)
 
409
    return;
 
410
 
 
411
  Name = NameRef.str();
 
412
  if (Parent)
 
413
    Parent->getMDSymbolTable().insert(NameRef, this);
 
414
}
 
415
 
 
416
/// getName - Return a constant reference to this named metadata's name.
 
417
StringRef NamedMDNode::getName() const {
 
418
  return StringRef(Name);
 
419
}
 
420
 
 
421
//===----------------------------------------------------------------------===//
 
422
// LLVMContext MDKind naming implementation.
 
423
//
 
424
 
 
425
#ifndef NDEBUG
 
426
/// isValidName - Return true if Name is a valid custom metadata handler name.
 
427
static bool isValidName(StringRef MDName) {
 
428
  if (MDName.empty())
 
429
    return false;
 
430
 
 
431
  if (!isalpha(MDName[0]))
 
432
    return false;
 
433
 
 
434
  for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
 
435
       ++I) {
 
436
    if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
 
437
        return false;
 
438
  }
 
439
  return true;
 
440
}
 
441
#endif
 
442
 
 
443
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
 
444
unsigned LLVMContext::getMDKindID(StringRef Name) const {
 
445
  assert(isValidName(Name) && "Invalid MDNode name");
 
446
 
 
447
  unsigned &Entry = pImpl->CustomMDKindNames[Name];
 
448
 
 
449
  // If this is new, assign it its ID.
 
450
  if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
 
451
  return Entry;
 
452
}
 
453
 
 
454
/// getHandlerNames - Populate client supplied smallvector using custome
 
455
/// metadata name and ID.
 
456
void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
 
457
  Names.resize(pImpl->CustomMDKindNames.size()+1);
 
458
  Names[0] = "";
 
459
  for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
 
460
       E = pImpl->CustomMDKindNames.end(); I != E; ++I)
 
461
    // MD Handlers are numbered from 1.
 
462
    Names[I->second] = I->first();
 
463
}
 
464
 
 
465
//===----------------------------------------------------------------------===//
 
466
// Instruction Metadata method implementations.
 
467
//
 
468
 
 
469
void Instruction::setMetadata(const char *Kind, MDNode *Node) {
 
470
  if (Node == 0 && !hasMetadata()) return;
 
471
  setMetadata(getContext().getMDKindID(Kind), Node);
 
472
}
 
473
 
 
474
MDNode *Instruction::getMetadataImpl(const char *Kind) const {
 
475
  return getMetadataImpl(getContext().getMDKindID(Kind));
 
476
}
 
477
 
 
478
/// setMetadata - Set the metadata of of the specified kind to the specified
 
479
/// node.  This updates/replaces metadata if already present, or removes it if
 
480
/// Node is null.
 
481
void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
 
482
  if (Node == 0 && !hasMetadata()) return;
 
483
 
 
484
  // Handle the case when we're adding/updating metadata on an instruction.
 
485
  if (Node) {
 
486
    LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
 
487
    assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
 
488
    if (Info.empty()) {
 
489
      setHasMetadata(true);
 
490
    } else {
 
491
      // Handle replacement of an existing value.
 
492
      for (unsigned i = 0, e = Info.size(); i != e; ++i)
 
493
        if (Info[i].first == KindID) {
 
494
          Info[i].second = Node;
 
495
          return;
 
496
        }
 
497
    }
 
498
 
 
499
    // No replacement, just add it to the list.
 
500
    Info.push_back(std::make_pair(KindID, Node));
 
501
    return;
 
502
  }
 
503
 
 
504
  // Otherwise, we're removing metadata from an instruction.
 
505
  assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
 
506
         "HasMetadata bit out of date!");
 
507
  LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
 
508
 
 
509
  // Common case is removing the only entry.
 
510
  if (Info.size() == 1 && Info[0].first == KindID) {
 
511
    getContext().pImpl->MetadataStore.erase(this);
 
512
    setHasMetadata(false);
 
513
    return;
 
514
  }
 
515
 
 
516
  // Handle replacement of an existing value.
 
517
  for (unsigned i = 0, e = Info.size(); i != e; ++i)
 
518
    if (Info[i].first == KindID) {
 
519
      Info[i] = Info.back();
 
520
      Info.pop_back();
 
521
      assert(!Info.empty() && "Removing last entry should be handled above");
 
522
      return;
 
523
    }
 
524
  // Otherwise, removing an entry that doesn't exist on the instruction.
 
525
}
 
526
 
 
527
MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
 
528
  LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
 
529
  assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
 
530
 
 
531
  for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
 
532
       I != E; ++I)
 
533
    if (I->first == KindID)
 
534
      return I->second;
 
535
  return 0;
 
536
}
 
537
 
 
538
void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
 
539
                                       MDNode*> > &Result)const {
 
540
  assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
 
541
         "Shouldn't have called this");
 
542
  const LLVMContextImpl::MDMapTy &Info =
 
543
    getContext().pImpl->MetadataStore.find(this)->second;
 
544
  assert(!Info.empty() && "Shouldn't have called this");
 
545
 
 
546
  Result.clear();
 
547
  Result.append(Info.begin(), Info.end());
 
548
 
 
549
  // Sort the resulting array so it is stable.
 
550
  if (Result.size() > 1)
 
551
    array_pod_sort(Result.begin(), Result.end());
 
552
}
 
553
 
 
554
/// removeAllMetadata - Remove all metadata from this instruction.
 
555
void Instruction::removeAllMetadata() {
 
556
  assert(hasMetadata() && "Caller should check");
 
557
  getContext().pImpl->MetadataStore.erase(this);
 
558
  setHasMetadata(false);
 
559
}
 
560