1cf3056db0fee1db7921214b1f25cea04e959e105Chris Lattner//===-- Module.cpp - Implement the Module class ---------------------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
10c2c50cdcdc19a1bca993c06d13d8cdca87083ce4Chandler Carruth// This file implements the Module class for the IR library.
11009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
12009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
140b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
15d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "SymbolTableListTraitsImpl.h"
16d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/DenseSet.h"
17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/STLExtras.h"
18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallString.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/StringExtras.h"
20f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin#include "llvm/GVMaterializer.h"
210b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/InstrTypes.h"
240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
25551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/LeakDetector.h"
267e70829632f82de15db187845666aaca6e04b792Chris Lattner#include <algorithm>
270ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner#include <cstdarg>
281d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson#include <cstdlib>
2931f8499e83dc4dccbb57ea7e76d1fd49b7010d0cChris Lattnerusing namespace llvm;
30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3160837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
32af76cfb0fb70f0bec8581c2be1a60dc56bd3f285Misha Brukman// Methods to implement the globals and functions lists.
3360837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
3460837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
357e70829632f82de15db187845666aaca6e04b792Chris Lattner// Explicit instantiations of SymbolTableListTraits since some of the methods
36cc041ba03aed685400197fb938b7a583713d25afChris Lattner// are not in the public header file.
37c63ca0a71b299ee0b8fc7dc8405d7f3c856ecfa3John McCalltemplate class llvm::SymbolTableListTraits<Function, Module>;
3853b2b7364385b2f2d98c0052df73a637a81c2288Nick Lewyckytemplate class llvm::SymbolTableListTraits<GlobalVariable, Module>;
39c63ca0a71b299ee0b8fc7dc8405d7f3c856ecfa3John McCalltemplate class llvm::SymbolTableListTraits<GlobalAlias, Module>;
40009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
4160837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
4260837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner// Primitive Module methods.
4360837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
44dd6dfbc8cc008faca03b6ac9b904f263205a418aChris Lattner
452928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarModule::Module(StringRef MID, LLVMContext& C)
46d98af0a5b86425fdc723bb54fc59247c585d63abDan Gohman  : Context(C), Materializer(NULL), ModuleID(MID) {
47ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer  ValSymTab = new ValueSymbolTable();
4817aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDSymTab = new StringMap<NamedMDNode *>();
4930268be89df6444f5ffb585439b3fbfec9055197Owen Anderson  Context.addModule(this);
50009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
51009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
52009505452b713ed2e3a8e99c5545a6e721c65495Chris LattnerModule::~Module() {
5330268be89df6444f5ffb585439b3fbfec9055197Owen Anderson  Context.removeModule(this);
54009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  dropAllReferences();
557e70829632f82de15db187845666aaca6e04b792Chris Lattner  GlobalList.clear();
567e70829632f82de15db187845666aaca6e04b792Chris Lattner  FunctionList.clear();
578b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov  AliasList.clear();
5828bc9d88260a3e153ead4311c9129e3d3ad07736Devang Patel  NamedMDList.clear();
5978d033e086e19e016273de014f9214aa6f3f844bReid Spencer  delete ValSymTab;
6017aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
61009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
62009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
631afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner/// Target endian information.
641d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen AndersonModule::Endianness Module::getEndianness() const {
65d4f195999a7774611e5f9e457a86f14d5e257324Benjamin Kramer  StringRef temp = DataLayout;
66ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson  Module::Endianness ret = AnyEndianness;
6785d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
68ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson  while (!temp.empty()) {
69c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    std::pair<StringRef, StringRef> P = getToken(temp, "-");
7085d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
71c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    StringRef token = P.first;
72c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    temp = P.second;
7385d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
741d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson    if (token[0] == 'e') {
75ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson      ret = LittleEndian;
761d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson    } else if (token[0] == 'E') {
77ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson      ret = BigEndian;
781d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson    }
791d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson  }
8085d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
81ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson  return ret;
821d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson}
831d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson
84c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner/// Target Pointer Size information.
851d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen AndersonModule::PointerSize Module::getPointerSize() const {
86d4f195999a7774611e5f9e457a86f14d5e257324Benjamin Kramer  StringRef temp = DataLayout;
87ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson  Module::PointerSize ret = AnyPointerSize;
8885d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
89ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson  while (!temp.empty()) {
90c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    std::pair<StringRef, StringRef> TmpP = getToken(temp, "-");
91c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    temp = TmpP.second;
92c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    TmpP = getToken(TmpP.first, ":");
93c30a38f34bdfecb99ce49e3ffa479039c9bf0209Chris Lattner    StringRef token = TmpP.second, signalToken = TmpP.first;
9485d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
95d4f195999a7774611e5f9e457a86f14d5e257324Benjamin Kramer    if (signalToken[0] == 'p') {
96d4f195999a7774611e5f9e457a86f14d5e257324Benjamin Kramer      int size = 0;
97d4f195999a7774611e5f9e457a86f14d5e257324Benjamin Kramer      getToken(token, ":").first.getAsInteger(10, size);
981d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson      if (size == 32)
99ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson        ret = Pointer32;
1001d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson      else if (size == 64)
101ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson        ret = Pointer64;
1021d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson    }
1031d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson  }
10485d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
105ac4c75ba8253dbd918d0cb5aa8614d8f4f7d7ad7Owen Anderson  return ret;
1061d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson}
1071d8b8535ec6ccbf07b0e83c5be6aec51feed1d45Owen Anderson
108f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar/// getNamedValue - Return the first global value in the module with
109f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar/// the specified name, of arbitrary type.  This method returns null
110f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar/// if a global with the specified name is not found.
1112928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarGlobalValue *Module::getNamedValue(StringRef Name) const {
112f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
113f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar}
114f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar
115081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
116081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// This ID is uniqued across modules in the current LLVMContext.
117081134741b40b342fb2f85722c9cea5d412489a8Chris Lattnerunsigned Module::getMDKindID(StringRef Name) const {
118081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  return Context.getMDKindID(Name);
119081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner}
120081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner
121081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// getMDKindNames - Populate client supplied SmallVector with the name for
122081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
123081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// so it is filled in as an empty string.
124081134741b40b342fb2f85722c9cea5d412489a8Chris Lattnervoid Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
125081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  return Context.getMDKindNames(Result);
126081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner}
127081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner
128081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner
12960837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
13060837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner// Methods for easy access to the functions in the module.
13160837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
13260837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
133ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer// getOrInsertFunction - Look up the specified function in the module symbol
134ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer// table.  If it does not exist, add a prototype for the function and return
135ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer// it.  This is nice because it allows most passes to get away with not handling
136ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer// the symbol table directly for this common task.
137ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer//
1382928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertFunction(StringRef Name,
139db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      FunctionType *Ty,
14099faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling                                      AttributeSet AttributeList) {
141ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer  // See if we have a definition for the specified function already.
142f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  GlobalValue *F = getNamedValue(Name);
14370d130516a38013ef6262dd2da5accbea7932e30Chris Lattner  if (F == 0) {
144ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer    // Nope, add it
145051a950000e21935165db56695e35bade668193bGabor Greif    Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
1461186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky    if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
1471186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky      New->setAttributes(AttributeList);
1486056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner    FunctionList.push_back(New);
14970d130516a38013ef6262dd2da5accbea7932e30Chris Lattner    return New;                    // Return the new prototype.
15070d130516a38013ef6262dd2da5accbea7932e30Chris Lattner  }
15170d130516a38013ef6262dd2da5accbea7932e30Chris Lattner
15270d130516a38013ef6262dd2da5accbea7932e30Chris Lattner  // Okay, the function exists.  Does it have externally visible linkage?
153bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola  if (F->hasLocalLinkage()) {
15448cd712d4c84507d3098e795aebce8bd5ad05b14Chris Lattner    // Clear the function's name.
15548cd712d4c84507d3098e795aebce8bd5ad05b14Chris Lattner    F->setName("");
15670d130516a38013ef6262dd2da5accbea7932e30Chris Lattner    // Retry, now there won't be a conflict.
15748cd712d4c84507d3098e795aebce8bd5ad05b14Chris Lattner    Constant *NewF = getOrInsertFunction(Name, Ty);
15892ccf70ad448eb02f9f273d2c70ae4708b3bd0f2Daniel Dunbar    F->setName(Name);
15948cd712d4c84507d3098e795aebce8bd5ad05b14Chris Lattner    return NewF;
1606056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner  }
16170d130516a38013ef6262dd2da5accbea7932e30Chris Lattner
16270d130516a38013ef6262dd2da5accbea7932e30Chris Lattner  // If the function exists but has the wrong type, return a bitcast to the
16370d130516a38013ef6262dd2da5accbea7932e30Chris Lattner  // right type.
164debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  if (F->getType() != PointerType::getUnqual(Ty))
165debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
16685d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
16770d130516a38013ef6262dd2da5accbea7932e30Chris Lattner  // Otherwise, we just found the existing function or a prototype.
16885d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling  return F;
1696056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner}
1706056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner
1712928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertTargetIntrinsic(StringRef Name,
172db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                             FunctionType *Ty,
17399faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling                                             AttributeSet AttributeList) {
17449de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen  // See if we have a definition for the specified function already.
175f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  GlobalValue *F = getNamedValue(Name);
17649de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen  if (F == 0) {
17749de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen    // Nope, add it
17849de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen    Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
17949de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen    New->setAttributes(AttributeList);
18049de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen    FunctionList.push_back(New);
18149de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen    return New; // Return the new prototype.
18249de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen  }
18349de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen
18449de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen  // Otherwise, we just found the existing function or a prototype.
18585d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling  return F;
18649de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen}
18749de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen
1882928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertFunction(StringRef Name,
189db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      FunctionType *Ty) {
19099faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  return getOrInsertFunction(Name, Ty, AttributeSet());
1911186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky}
1921186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
1930ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// getOrInsertFunction - Look up the specified function in the module symbol
1940ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// table.  If it does not exist, add a prototype for the function and return it.
1950ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// This version of the method takes a null terminated list of function
1960ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// arguments, which makes it easier for clients to use.
1970ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner//
1982928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertFunction(StringRef Name,
19999faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling                                      AttributeSet AttributeList,
200db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      Type *RetTy, ...) {
2010ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  va_list Args;
2020ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  va_start(Args, RetTy);
2030ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2040ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  // Build the list of argument types...
2055fdd6c8793462549e3593890ec61573da06e3346Jay Foad  std::vector<Type*> ArgTys;
2065fdd6c8793462549e3593890ec61573da06e3346Jay Foad  while (Type *ArgTy = va_arg(Args, Type*))
2070ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner    ArgTys.push_back(ArgTy);
2080ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2090ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  va_end(Args);
2100ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2110ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  // Build the function type and chain to the other getOrInsertFunction...
212c341f1c62d1b46b64fb70890fffed87a3136cc2dOwen Anderson  return getOrInsertFunction(Name,
213debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson                             FunctionType::get(RetTy, ArgTys, false),
2141186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky                             AttributeList);
2150ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner}
2160ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2172928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertFunction(StringRef Name,
218db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      Type *RetTy, ...) {
2191186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  va_list Args;
2201186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  va_start(Args, RetTy);
2211186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
2221186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  // Build the list of argument types...
2235fdd6c8793462549e3593890ec61573da06e3346Jay Foad  std::vector<Type*> ArgTys;
2245fdd6c8793462549e3593890ec61573da06e3346Jay Foad  while (Type *ArgTy = va_arg(Args, Type*))
2251186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky    ArgTys.push_back(ArgTy);
2261186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
2271186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  va_end(Args);
2281186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
2291186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  // Build the function type and chain to the other getOrInsertFunction...
23085d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling  return getOrInsertFunction(Name,
231debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson                             FunctionType::get(RetTy, ArgTys, false),
23299faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling                             AttributeSet());
2331186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky}
2340ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2356056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner// getFunction - Look up the specified function in the module symbol table.
2366056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner// If it does not exist, return null.
2376056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner//
2382928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarFunction *Module::getFunction(StringRef Name) const {
239f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  return dyn_cast_or_null<Function>(getNamedValue(Name));
240bb0e2487185e401c7fca63d55e59343e060912a2Chris Lattner}
241bb0e2487185e401c7fca63d55e59343e060912a2Chris Lattner
24260837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
24360837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner// Methods for easy access to the global variables in the module.
24460837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
24560837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
24660837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner/// getGlobalVariable - Look up the specified global variable in the module
24730614675f45809c43d44c7243d8b747b39403155Chris Lattner/// symbol table.  If it does not exist, return null.  The type argument
24830614675f45809c43d44c7243d8b747b39403155Chris Lattner/// should be the underlying type of the global, i.e., it should not have
24930614675f45809c43d44c7243d8b747b39403155Chris Lattner/// the top-level PointerType, which represents the address of the global.
250bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola/// If AllowLocal is set to true, this function will return types that
251bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola/// have an local. By default, these types are not returned.
25260837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner///
2532928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarGlobalVariable *Module::getGlobalVariable(StringRef Name,
254bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola                                          bool AllowLocal) const {
25585d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling  if (GlobalVariable *Result =
256f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar      dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
257f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar    if (AllowLocal || !Result->hasLocalLinkage())
25860837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner      return Result;
25960837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner  return 0;
26060837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner}
26160837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
262b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling/// getOrInsertGlobal - Look up the specified global in the module symbol table.
263b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///   1. If it does not exist, add a declaration of the global and return it.
264b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///   2. Else, the global exists but has the wrong type: return the function
265b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///      with a constantexpr cast to the right type.
266b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///   3. Finally, if the existing global is the correct delclaration, return the
267b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///      existing global.
268db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerConstant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
269b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // See if we have a definition for the specified global already.
270f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
271b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  if (GV == 0) {
272b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling    // Nope, add it
273b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling    GlobalVariable *New =
274e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson      new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
275e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                         0, Name);
276e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson     return New;                    // Return the new declaration.
277b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  }
278b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling
279b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // If the variable exists but has the wrong type, return a bitcast to the
280b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // right type.
281debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  if (GV->getType() != PointerType::getUnqual(Ty))
282debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
28385d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
284b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // Otherwise, we just found the existing function or a prototype.
285b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  return GV;
286b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling}
287b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling
28860837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
2898b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov// Methods for easy access to the global variables in the module.
2908b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov//
2918b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov
2928b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov// getNamedAlias - Look up the specified global in the module symbol table.
2938b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov// If it does not exist, return null.
2948b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov//
2952928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarGlobalAlias *Module::getNamedAlias(StringRef Name) const {
296f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
2978b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov}
2988b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov
29949fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel/// getNamedMetadata - Return the first NamedMDNode in the module with the
30085d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling/// specified name. This method returns null if a NamedMDNode with the
30154eee524f192a0e7395d057481e8d4240729d7b4Bob Wilson/// specified name is not found.
302a762b093107ac3aa438815627006425d0b13a236Devang PatelNamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
3032f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel  SmallString<256> NameData;
3042f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel  StringRef NameRef = Name.toStringRef(NameData);
30517aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
3062f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel}
3072f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel
30885d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling/// getOrInsertNamedMetadata - Return the first named MDNode in the module
30985d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling/// with the specified name. This method returns a new NamedMDNode if a
31049fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel/// NamedMDNode with the specified name is not found.
3112928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarNamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
31217aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDNode *&NMD =
31317aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
31417aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  if (!NMD) {
31517aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NMD = new NamedMDNode(Name);
31617aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NMD->setParent(this);
31717aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NamedMDList.push_back(NMD);
31817aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  }
31949fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel  return NMD;
32049fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel}
32149fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel
322d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
323d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// delete it.
32417aa92c92a925b4a674440c7ef088c223990e854Dan Gohmanvoid Module::eraseNamedMetadata(NamedMDNode *NMD) {
32517aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
32617aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDList.erase(NMD);
32717aa92c92a925b4a674440c7ef088c223990e854Dan Gohman}
32817aa92c92a925b4a674440c7ef088c223990e854Dan Gohman
329f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
330f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendlingvoid Module::
331f20f281368f75239c852fe99c3b3a19278ba38fdBill WendlinggetModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
332f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  const NamedMDNode *ModFlags = getModuleFlagsMetadata();
333f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  if (!ModFlags) return;
334f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling
335f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) {
336f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    MDNode *Flag = ModFlags->getOperand(i);
337f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
338f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    MDString *Key = cast<MDString>(Flag->getOperand(1));
339f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    Value *Val = Flag->getOperand(2);
340e6bd7a805808a05e9869fbf067581855a8b2a2c2Bill Wendling    Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
341426f21573219eeba8f6981d7ddb4f1d2445b6343Bill Wendling                                    Key, Val));
342f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  }
343f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling}
344f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling
345d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
346d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// represents module-level flags. This method returns null if there are no
347d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// module-level flags.
348d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill WendlingNamedMDNode *Module::getModuleFlagsMetadata() const {
349d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  return getNamedMetadata("llvm.module.flags");
350d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
351d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling
352d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
353d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// represents module-level flags. If module-level flags aren't found, it
354d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// creates the named metadata that contains them.
355d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill WendlingNamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
356d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  return getOrInsertNamedMetadata("llvm.module.flags");
357d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
358d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling
359d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// addModuleFlag - Add a module-level flag to the module-level flags
360d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// metadata. It will create the module-level flags named metadata if it doesn't
361d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// already exist.
362e6bd7a805808a05e9869fbf067581855a8b2a2c2Bill Wendlingvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
363d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling                           Value *Val) {
364d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  Type *Int32Ty = Type::getInt32Ty(Context);
365d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  Value *Ops[3] = {
366d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling    ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
367d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  };
368d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
369d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
370e6bd7a805808a05e9869fbf067581855a8b2a2c2Bill Wendlingvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
371d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling                           uint32_t Val) {
372d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  Type *Int32Ty = Type::getInt32Ty(Context);
373d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
374d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
375d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendlingvoid Module::addModuleFlag(MDNode *Node) {
376d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  assert(Node->getNumOperands() == 3 &&
377d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling         "Invalid number of operands for module flag!");
378d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  assert(isa<ConstantInt>(Node->getOperand(0)) &&
379d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling         isa<MDString>(Node->getOperand(1)) &&
380d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling         "Invalid operand types for module flag!");
381d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  getOrInsertModuleFlagsMetadata()->addOperand(Node);
382d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
383f33fa6fb57b9c41e99739eff79b7f185e0df9500Chris Lattner
38460837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
385f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin// Methods to control the materialization of GlobalValues in the Module.
386f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin//
387f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinvoid Module::setMaterializer(GVMaterializer *GVM) {
388f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  assert(!Materializer &&
389f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin         "Module already has a GVMaterializer.  Call MaterializeAllPermanently"
390f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin         " to clear it out before setting another one.");
391f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Materializer.reset(GVM);
392f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
393f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
394f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::isMaterializable(const GlobalValue *GV) const {
395f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
396f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->isMaterializable(GV);
397f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
398f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
399f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
400f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::isDematerializable(const GlobalValue *GV) const {
401f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
402f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->isDematerializable(GV);
403f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
404f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
405f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
406f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
407f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
408f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->Materialize(GV, ErrInfo);
409f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
410f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
411f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
412f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinvoid Module::Dematerialize(GlobalValue *GV) {
413f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
414f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->Dematerialize(GV);
415f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
416f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
417f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::MaterializeAll(std::string *ErrInfo) {
418f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (!Materializer)
419f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return false;
420f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return Materializer->MaterializeModule(this, ErrInfo);
421f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
422f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
423f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::MaterializeAllPermanently(std::string *ErrInfo) {
424f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (MaterializeAll(ErrInfo))
425f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return true;
426f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Materializer.reset();
427f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
428f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
429f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
430f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin//===----------------------------------------------------------------------===//
43160837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner// Other module related stuff.
43260837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
43360837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
43460837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
435cf84d867619b68be17bb1c12bd9831fdd73a88e4Eric Christopher// dropAllReferences() - This function causes all the subelements to "let go"
4369da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// of all references that they are maintaining.  This allows one to 'delete' a
4379da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// whole module at a time, even though there may be circular references... first
4389da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// all references are dropped, and all use counts go to zero.  Then everything
4396b63452c3ad26678b32f93dbca55902a313ee4e9Misha Brukman// is deleted for real.  Note that no operations are valid on an object that
4409da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// has "dropped all references", except operator delete.
441009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
442009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnervoid Module::dropAllReferences() {
4437e70829632f82de15db187845666aaca6e04b792Chris Lattner  for(Module::iterator I = begin(), E = end(); I != E; ++I)
4447e70829632f82de15db187845666aaca6e04b792Chris Lattner    I->dropAllReferences();
445dd6dfbc8cc008faca03b6ac9b904f263205a418aChris Lattner
446e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner  for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
4477e70829632f82de15db187845666aaca6e04b792Chris Lattner    I->dropAllReferences();
448a80e1181b78183dc36ec6568559d38faa86981f0Anton Korobeynikov
449a80e1181b78183dc36ec6568559d38faa86981f0Anton Korobeynikov  for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
450a80e1181b78183dc36ec6568559d38faa86981f0Anton Korobeynikov    I->dropAllReferences();
451009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
452