Use CallbackVH, instead of WeakVH, to hold MDNode elements.
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 LLVMContext, as a wrapper around the opaque
11 //  class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Metadata.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "LLVMContextImpl.h"
21 #include <set>
22
23 using namespace llvm;
24
25 static ManagedStatic<LLVMContext> GlobalContext;
26
27 LLVMContext& llvm::getGlobalContext() {
28   return *GlobalContext;
29 }
30
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
32 LLVMContext::~LLVMContext() { delete pImpl; }
33
34 GetElementPtrConstantExpr::GetElementPtrConstantExpr
35   (Constant *C,
36    const std::vector<Constant*> &IdxList,
37    const Type *DestTy)
38     : ConstantExpr(DestTy, Instruction::GetElementPtr,
39                    OperandTraits<GetElementPtrConstantExpr>::op_end(this)
40                    - (IdxList.size()+1),
41                    IdxList.size()+1) {
42   OperandList[0] = C;
43   for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
44     OperandList[i+1] = IdxList[i];
45 }
46
47 bool LLVMContext::RemoveDeadMetadata() {
48   std::vector<const MDNode *> DeadMDNodes;
49   bool Changed = false;
50   while (1) {
51
52     for (FoldingSet<MDNode>::iterator 
53            I = pImpl->MDNodeSet.begin(),
54            E = pImpl->MDNodeSet.end(); I != E; ++I) {
55       const MDNode *N = &(*I);
56       if (N->use_empty()) 
57         DeadMDNodes.push_back(N);
58     }
59     
60     if (DeadMDNodes.empty())
61       return Changed;
62
63     while (!DeadMDNodes.empty()) {
64       const MDNode *N = DeadMDNodes.back(); DeadMDNodes.pop_back();
65       delete N;
66     }
67   }
68   return Changed;
69 }