12f1efd639c8659582d0df2b8f927a018b057037fJeffrey Yasskin//===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
22bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//
32bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//                     The LLVM Compiler Infrastructure
42bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//
52bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson// This file is distributed under the University of Illinois Open Source
62bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson// License. See LICENSE.TXT for details.
72bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//
82bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//===----------------------------------------------------------------------===//
95217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//
105217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//  This file declares LLVMContextImpl, the opaque implementation
115217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//  of LLVMContext.
125217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//
135217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//===----------------------------------------------------------------------===//
142bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
152bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson#ifndef LLVM_LLVMCONTEXT_IMPL_H
162bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson#define LLVM_LLVMCONTEXT_IMPL_H
172bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
18f6670729aabc1fab85238d2b306a1c1767a807bbBill Wendling#include "AttributeImpl.h"
1948b2f3e4850cd27d54224cd42da8a160d6b95984Owen Anderson#include "ConstantsContext.h"
20c34ebf65af0139eaf5cb0969fabcd32c0b6e1710Owen Anderson#include "LeaksContext.h"
21914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson#include "llvm/ADT/APFloat.h"
22001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson#include "llvm/ADT/APInt.h"
232a4a6fecf0b8c92223f8fdf19545b564b7d3fcdeJay Foad#include "llvm/ADT/ArrayRef.h"
24001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson#include "llvm/ADT/DenseMap.h"
25ce032b483ca96093b84f69178cdb2d047e124332Owen Anderson#include "llvm/ADT/FoldingSet.h"
26a1514e24cc24b050f53a12650e047799358833a1Chandler Carruth#include "llvm/ADT/Hashing.h"
27ad715f86c90b06cc4ab9e1336d1bc3bf13ecb16dJeffrey Yasskin#include "llvm/ADT/SmallPtrSet.h"
28aad3fb7362aff151e97ad457005ea3f2872fe868Owen Anderson#include "llvm/ADT/StringMap.h"
290b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
300b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
310b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
320b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Metadata.h"
3336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/ValueHandle.h"
34006c77df8cc7f6a9dac575600b797b8ba32b29ebOwen Anderson#include <vector>
3516e298f98024bcff5c7219a96cac216114c30dadOwen Anderson
362bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Andersonnamespace llvm {
37eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson
38001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Andersonclass ConstantInt;
39914e50c841bbc248ab94144c11813b5785b1292dOwen Andersonclass ConstantFP;
40dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass DiagnosticInfoOptimizationRemark;
41dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass DiagnosticInfoOptimizationRemarkMissed;
42dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass DiagnosticInfoOptimizationRemarkAnalysis;
4312ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext;
44001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Andersonclass Type;
45ce032b483ca96093b84f69178cdb2d047e124332Owen Andersonclass Value;
46001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson
4755c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerstruct DenseMapAPIntKeyInfo {
48001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  struct KeyTy {
49001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson    APInt val;
50db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type* type;
51db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
52001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson    bool operator==(const KeyTy& that) const {
53001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson      return type == that.type && this->val == that.val;
54001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson    }
55001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson    bool operator!=(const KeyTy& that) const {
56001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson      return !this->operator==(that);
57001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson    }
58ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    friend hash_code hash_value(const KeyTy &Key) {
59ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth      return hash_combine(Key.type, Key.val);
60ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    }
61001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  };
62dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), nullptr); }
63dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), nullptr); }
64001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  static unsigned getHashValue(const KeyTy &Key) {
65ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    return static_cast<unsigned>(hash_value(Key));
66001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  }
67001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
68001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson    return LHS == RHS;
69001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  }
70001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson};
71001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson
7255c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerstruct DenseMapAPFloatKeyInfo {
73914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  struct KeyTy {
74914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    APFloat val;
75914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    KeyTy(const APFloat& V) : val(V){}
76914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    bool operator==(const KeyTy& that) const {
77914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson      return this->val.bitwiseIsEqual(that.val);
78914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    }
79914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    bool operator!=(const KeyTy& that) const {
80914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson      return !this->operator==(that);
81914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    }
82ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    friend hash_code hash_value(const KeyTy &Key) {
83ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth      return hash_combine(Key.val);
84ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    }
85914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  };
86914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  static inline KeyTy getEmptyKey() {
87914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    return KeyTy(APFloat(APFloat::Bogus,1));
88914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  }
89914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  static inline KeyTy getTombstoneKey() {
90914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    return KeyTy(APFloat(APFloat::Bogus,2));
91914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  }
92914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  static unsigned getHashValue(const KeyTy &Key) {
93ed7692a136a9bcf513b91b7b5eb33a1e2d83e7eeChandler Carruth    return static_cast<unsigned>(hash_value(Key));
94914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  }
95914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
96914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson    return LHS == RHS;
97914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  }
98914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson};
99914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson
10055c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerstruct AnonStructTypeKeyInfo {
101bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  struct KeyTy {
102bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    ArrayRef<Type*> ETypes;
103bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    bool isPacked;
104bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    KeyTy(const ArrayRef<Type*>& E, bool P) :
105bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      ETypes(E), isPacked(P) {}
106bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    KeyTy(const StructType* ST) :
107bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
108bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      isPacked(ST->isPacked()) {}
109bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    bool operator==(const KeyTy& that) const {
110bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      if (isPacked != that.isPacked)
111bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad        return false;
112bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      if (ETypes != that.ETypes)
113bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad        return false;
114bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      return true;
115bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    }
116bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    bool operator!=(const KeyTy& that) const {
117bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      return !this->operator==(that);
118bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    }
119bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  };
120bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static inline StructType* getEmptyKey() {
121bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return DenseMapInfo<StructType*>::getEmptyKey();
122bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
123bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static inline StructType* getTombstoneKey() {
124bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return DenseMapInfo<StructType*>::getTombstoneKey();
125bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
126bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static unsigned getHashValue(const KeyTy& Key) {
1270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_combine(hash_combine_range(Key.ETypes.begin(),
1280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                           Key.ETypes.end()),
1290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                        Key.isPacked);
130bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
131bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static unsigned getHashValue(const StructType *ST) {
132bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return getHashValue(KeyTy(ST));
133bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
134bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
135bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    if (RHS == getEmptyKey() || RHS == getTombstoneKey())
136bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      return false;
137bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return LHS == KeyTy(RHS);
138bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
139bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static bool isEqual(const StructType *LHS, const StructType *RHS) {
140bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return LHS == RHS;
141bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
142bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad};
143bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad
14455c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerstruct FunctionTypeKeyInfo {
145bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  struct KeyTy {
146bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    const Type *ReturnType;
147bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    ArrayRef<Type*> Params;
148bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    bool isVarArg;
149bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
150bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      ReturnType(R), Params(P), isVarArg(V) {}
151bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    KeyTy(const FunctionType* FT) :
152bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      ReturnType(FT->getReturnType()),
153bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
154bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      isVarArg(FT->isVarArg()) {}
155bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    bool operator==(const KeyTy& that) const {
156bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      if (ReturnType != that.ReturnType)
157bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad        return false;
158bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      if (isVarArg != that.isVarArg)
159bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad        return false;
160bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      if (Params != that.Params)
161bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad        return false;
162bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      return true;
163bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    }
164bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    bool operator!=(const KeyTy& that) const {
165bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      return !this->operator==(that);
166bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    }
167bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  };
168bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static inline FunctionType* getEmptyKey() {
169bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return DenseMapInfo<FunctionType*>::getEmptyKey();
170bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
171bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static inline FunctionType* getTombstoneKey() {
172bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return DenseMapInfo<FunctionType*>::getTombstoneKey();
173bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
174bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static unsigned getHashValue(const KeyTy& Key) {
1750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth    return hash_combine(Key.ReturnType,
1760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                        hash_combine_range(Key.Params.begin(),
1770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                                           Key.Params.end()),
1780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth                        Key.isVarArg);
179bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
180bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static unsigned getHashValue(const FunctionType *FT) {
181bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return getHashValue(KeyTy(FT));
182bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
183bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
184bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    if (RHS == getEmptyKey() || RHS == getTombstoneKey())
185bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad      return false;
186bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return LHS == KeyTy(RHS);
187bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
188bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
189bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad    return LHS == RHS;
190bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  }
191bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad};
192bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad
193611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer// Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
194611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer// shortcut to avoid comparing all operands.
195611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramertemplate<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
196611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer  static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
197611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer                     unsigned IDHash, FoldingSetNodeID &TempID) {
198611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
199611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    // First, check if the cached hashes match.  If they don't we can skip the
200611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    // expensive operand walk.
201611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    if (X.Hash != IDHash)
202611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer      return false;
203611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer
204611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    // If they match we have to compare the operands.
205611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    X.Profile(TempID);
206611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    return TempID == ID;
207611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer  }
208611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer  static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
209611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer    return X.Hash; // Return cached hash.
210611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer  }
211611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer};
212611afc0620aed7827b5fdf9072a1827952b684b6Benjamin Kramer
213b227925fa313428045f554187b0136d084d723f6Chris Lattner/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
214b227925fa313428045f554187b0136d084d723f6Chris Lattner/// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
21555c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerclass DebugRecVH : public CallbackVH {
216b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// Ctx - This is the LLVM Context being referenced.
217b227925fa313428045f554187b0136d084d723f6Chris Lattner  LLVMContextImpl *Ctx;
218b227925fa313428045f554187b0136d084d723f6Chris Lattner
219b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
220b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// this reference lives in.  If this is zero, then it represents a
221b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// non-canonical entry that has no DenseMap value.  This can happen due to
222b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// RAUW.
223b227925fa313428045f554187b0136d084d723f6Chris Lattner  int Idx;
224b227925fa313428045f554187b0136d084d723f6Chris Lattnerpublic:
225b227925fa313428045f554187b0136d084d723f6Chris Lattner  DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
226b227925fa313428045f554187b0136d084d723f6Chris Lattner    : CallbackVH(n), Ctx(ctx), Idx(idx) {}
227b227925fa313428045f554187b0136d084d723f6Chris Lattner
228b227925fa313428045f554187b0136d084d723f6Chris Lattner  MDNode *get() const {
229b227925fa313428045f554187b0136d084d723f6Chris Lattner    return cast_or_null<MDNode>(getValPtr());
230b227925fa313428045f554187b0136d084d723f6Chris Lattner  }
23136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
23236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void deleted() override;
23336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void allUsesReplacedWith(Value *VNew) override;
234b227925fa313428045f554187b0136d084d723f6Chris Lattner};
235b227925fa313428045f554187b0136d084d723f6Chris Lattner
23655c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerclass LLVMContextImpl {
23712ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerpublic:
23830268be89df6444f5ffb585439b3fbfec9055197Owen Anderson  /// OwnedModules - The set of modules instantiated in this context, and which
23930268be89df6444f5ffb585439b3fbfec9055197Owen Anderson  /// will be automatically deleted if this context is deleted.
24030268be89df6444f5ffb585439b3fbfec9055197Owen Anderson  SmallPtrSet<Module*, 4> OwnedModules;
24130268be89df6444f5ffb585439b3fbfec9055197Owen Anderson
242f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
243f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  void *InlineAsmDiagContext;
24436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
24536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
24636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void *DiagnosticContext;
24736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
248dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  LLVMContext::YieldCallbackTy YieldCallback;
249dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void *YieldOpaqueHandle;
250dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
25136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt *,
25236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   DenseMapAPIntKeyInfo> IntMapTy;
253001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson  IntMapTy IntConstants;
254001dbfebcbbded8c8e74b19e838b50da2b6c6fb5Owen Anderson
255914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
25648b2f3e4850cd27d54224cd42da8a160d6b95984Owen Anderson                         DenseMapAPFloatKeyInfo> FPMapTy;
257914e50c841bbc248ab94144c11813b5785b1292dOwen Anderson  FPMapTy FPConstants;
2582c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling
259f6670729aabc1fab85238d2b306a1c1767a807bbBill Wendling  FoldingSet<AttributeImpl> AttrsSet;
26018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  FoldingSet<AttributeSetImpl> AttrsLists;
2613467e30edf63b6d8a8d446186674ba9e4b7885a9Bill Wendling  FoldingSet<AttributeSetNode> AttrsSetNodes;
2620976e00fd1cbf4128daeb72efd8957d00383fda9Bill Wendling
2633ecb447f52d169dea6663b95b5b5b43e9bb5826bBill Wendling  StringMap<Value*> MDStringCache;
2642c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling
2655f4ac848d94b0a92e19ac7f2b3d0284d7d323173Devang Patel  FoldingSet<MDNode> MDNodeSet;
2662c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling
2676f555ca2cd0bba50542adcbb131f541ae70d34cdJeffrey Yasskin  // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
2686f555ca2cd0bba50542adcbb131f541ae70d34cdJeffrey Yasskin  // aren't in the MDNodeSet, but they're still shared between objects, so no
2696f555ca2cd0bba50542adcbb131f541ae70d34cdJeffrey Yasskin  // one object can destroy them.  This set allows us to at least destroy them
2706f555ca2cd0bba50542adcbb131f541ae70d34cdJeffrey Yasskin  // on Context destruction.
2716f555ca2cd0bba50542adcbb131f541ae70d34cdJeffrey Yasskin  SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
2725f4ac848d94b0a92e19ac7f2b3d0284d7d323173Devang Patel
2739df0fb4e8176325d713ba4eb67791e36cb833432Chris Lattner  DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
2740631fce85050f8f633e5fd032875d9151e0db4daOwen Anderson
2752cb395eae71dacda49ca3fe758618fc3e0701659Talin  typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
276eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  ArrayConstantsTy ArrayConstants;
27716e298f98024bcff5c7219a96cac216114c30dadOwen Anderson
2782cb395eae71dacda49ca3fe758618fc3e0701659Talin  typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
279eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  StructConstantsTy StructConstants;
280006c77df8cc7f6a9dac575600b797b8ba32b29ebOwen Anderson
2812cb395eae71dacda49ca3fe758618fc3e0701659Talin  typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
282eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  VectorConstantsTy VectorConstants;
2835bd68393ed87bcedc53f5998f1af9c906f5a1b4eOwen Anderson
2849df0fb4e8176325d713ba4eb67791e36cb833432Chris Lattner  DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
2859df0fb4e8176325d713ba4eb67791e36cb833432Chris Lattner
2869df0fb4e8176325d713ba4eb67791e36cb833432Chris Lattner  DenseMap<Type*, UndefValue*> UVConstants;
2877e3142b0126abc86dc4da350e8b84b001c3eeddeOwen Anderson
28827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  StringMap<ConstantDataSequential*> CDSConstants;
28927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
29036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
29136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    BlockAddresses;
2922a4a6fecf0b8c92223f8fdf19545b564b7d3fcdeJay Foad  ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
2932a4a6fecf0b8c92223f8fdf19545b564b7d3fcdeJay Foad    ExprConstants;
294bf48a9b6db111fc14a8faef1adefbce5d807aaefJeffrey Yasskin
2952a4a6fecf0b8c92223f8fdf19545b564b7d3fcdeJay Foad  ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
2962a4a6fecf0b8c92223f8fdf19545b564b7d3fcdeJay Foad                    InlineAsm> InlineAsms;
297d03eecd063a18ce0c505a87afcb04db26c035bc9Owen Anderson
298f53c371983908f02678b0e12c5d18466dcc70ffdOwen Anderson  ConstantInt *TheTrueVal;
299f53c371983908f02678b0e12c5d18466dcc70ffdOwen Anderson  ConstantInt *TheFalseVal;
300f53c371983908f02678b0e12c5d18466dcc70ffdOwen Anderson
301c34ebf65af0139eaf5cb0969fabcd32c0b6e1710Owen Anderson  LeakDetectorImpl<Value> LLVMObjects;
302c34ebf65af0139eaf5cb0969fabcd32c0b6e1710Owen Anderson
30363a03cf58505aa839f721f212cd1518ebf133979Dan Gohman  // Basic type instances.
304ce16339930a2b03e53b4e6399ef59c092a7f2cfaDan Gohman  Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
3051afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
3061afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
3079233b15d01ca62445bfc638f782243988c672e01Jeffrey Yasskin
308ba3ddf391f5149b8fca073adc3cbca361353929cChris Lattner
309ba3ddf391f5149b8fca073adc3cbca361353929cChris Lattner  /// TypeAllocator - All dynamically allocated types are allocated from this.
310ba3ddf391f5149b8fca073adc3cbca361353929cChris Lattner  /// They live forever until the context is torn down.
311ba3ddf391f5149b8fca073adc3cbca361353929cChris Lattner  BumpPtrAllocator TypeAllocator;
312ba3ddf391f5149b8fca073adc3cbca361353929cChris Lattner
3131afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  DenseMap<unsigned, IntegerType*> IntegerTypes;
3141afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner
315bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
316bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  FunctionTypeMap FunctionTypes;
317bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
318bdf1748b12fa44b18b74ae71d68a9b964c4cb8c6Jay Foad  StructTypeMap AnonStructTypes;
3191afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  StringMap<StructType*> NamedStructTypes;
3201afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  unsigned NamedStructTypesUniqueID;
3211afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner
3221afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
3231afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
3241afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
3251afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
3269233b15d01ca62445bfc638f782243988c672e01Jeffrey Yasskin
327ad715f86c90b06cc4ab9e1336d1bc3bf13ecb16dJeffrey Yasskin
3284d919438898d542850449235da7f6dc55e6ca152Owen Anderson  /// ValueHandles - This map keeps track of all of the value handles that are
3294d919438898d542850449235da7f6dc55e6ca152Owen Anderson  /// watching a Value*.  The Value::HasValueHandle bit is used to know
3304c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  /// whether or not a value has an entry in this map.
3314d919438898d542850449235da7f6dc55e6ca152Owen Anderson  typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
3324d919438898d542850449235da7f6dc55e6ca152Owen Anderson  ValueHandlesTy ValueHandles;
3334d919438898d542850449235da7f6dc55e6ca152Owen Anderson
334081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
335081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  StringMap<unsigned> CustomMDKindNames;
336081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner
337081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
338081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  typedef SmallVector<MDPairTy, 2> MDMapTy;
339081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner
340081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  /// MetadataStore - Collection of per-instruction metadata used in this
341081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  /// context.
342081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  DenseMap<const Instruction *, MDMapTy> MetadataStore;
343081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner
344b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
345b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// entry with no "inlined at" element.
346b227925fa313428045f554187b0136d084d723f6Chris Lattner  DenseMap<MDNode*, int> ScopeRecordIdx;
347b227925fa313428045f554187b0136d084d723f6Chris Lattner
348b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
349b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
350b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// the MDNode is RAUW'd.
351b227925fa313428045f554187b0136d084d723f6Chris Lattner  std::vector<DebugRecVH> ScopeRecords;
352b227925fa313428045f554187b0136d084d723f6Chris Lattner
353b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
354b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// scope/inlined-at pair.
355b227925fa313428045f554187b0136d084d723f6Chris Lattner  DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
356b227925fa313428045f554187b0136d084d723f6Chris Lattner
357b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
358b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
359b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// to date.
360b227925fa313428045f554187b0136d084d723f6Chris Lattner  std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
36136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
36236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// DiscriminatorTable - This table maps file:line locations to an
36336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// integer representing the next DWARF path discriminator to assign to
36436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// instructions in different blocks at the same location.
36536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
36636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
3674c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
3684c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  /// requested in this context
3694c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
3704c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  IntrinsicIDCacheTy IntrinsicIDCache;
3714c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman
3721e3037f0be430ef2339838bbdede11f45658bd82Peter Collingbourne  /// \brief Mapping from a function to its prefix data, which is stored as the
3731e3037f0be430ef2339838bbdede11f45658bd82Peter Collingbourne  /// operand of an unparented ReturnInst so that the prefix data has a Use.
3741e3037f0be430ef2339838bbdede11f45658bd82Peter Collingbourne  typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
3751e3037f0be430ef2339838bbdede11f45658bd82Peter Collingbourne  PrefixDataMapTy PrefixDataMap;
3761e3037f0be430ef2339838bbdede11f45658bd82Peter Collingbourne
377b227925fa313428045f554187b0136d084d723f6Chris Lattner  int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
378b227925fa313428045f554187b0136d084d723f6Chris Lattner  int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
379b227925fa313428045f554187b0136d084d723f6Chris Lattner
3802f1efd639c8659582d0df2b8f927a018b057037fJeffrey Yasskin  LLVMContextImpl(LLVMContext &C);
3812f1efd639c8659582d0df2b8f927a018b057037fJeffrey Yasskin  ~LLVMContextImpl();
3822bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson};
3832bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
3842bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson}
3852bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
3865217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson#endif
387