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::getOrInsertFunction(StringRef Name,
172db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      FunctionType *Ty) {
17399faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  return getOrInsertFunction(Name, Ty, AttributeSet());
1741186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky}
1751186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
1760ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// getOrInsertFunction - Look up the specified function in the module symbol
1770ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// table.  If it does not exist, add a prototype for the function and return it.
1780ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// This version of the method takes a null terminated list of function
1790ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner// arguments, which makes it easier for clients to use.
1800ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner//
1812928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertFunction(StringRef Name,
18299faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling                                      AttributeSet AttributeList,
183db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      Type *RetTy, ...) {
1840ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  va_list Args;
1850ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  va_start(Args, RetTy);
1860ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
1870ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  // Build the list of argument types...
1885fdd6c8793462549e3593890ec61573da06e3346Jay Foad  std::vector<Type*> ArgTys;
1895fdd6c8793462549e3593890ec61573da06e3346Jay Foad  while (Type *ArgTy = va_arg(Args, Type*))
1900ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner    ArgTys.push_back(ArgTy);
1910ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
1920ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  va_end(Args);
1930ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
1940ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner  // Build the function type and chain to the other getOrInsertFunction...
195c341f1c62d1b46b64fb70890fffed87a3136cc2dOwen Anderson  return getOrInsertFunction(Name,
196debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson                             FunctionType::get(RetTy, ArgTys, false),
1971186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky                             AttributeList);
1980ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner}
1990ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2002928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarConstant *Module::getOrInsertFunction(StringRef Name,
201db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                      Type *RetTy, ...) {
2021186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  va_list Args;
2031186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  va_start(Args, RetTy);
2041186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
2051186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  // Build the list of argument types...
2065fdd6c8793462549e3593890ec61573da06e3346Jay Foad  std::vector<Type*> ArgTys;
2075fdd6c8793462549e3593890ec61573da06e3346Jay Foad  while (Type *ArgTy = va_arg(Args, Type*))
2081186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky    ArgTys.push_back(ArgTy);
2091186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
2101186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  va_end(Args);
2111186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky
2121186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky  // Build the function type and chain to the other getOrInsertFunction...
21385d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling  return getOrInsertFunction(Name,
214debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson                             FunctionType::get(RetTy, ArgTys, false),
21599faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling                             AttributeSet());
2161186bf1350145474bb7f0ab4d38ec33dae5c79d2Nick Lewycky}
2170ae8e87e53d6f500f8d01785b67c68e2046fb8e8Chris Lattner
2186056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner// getFunction - Look up the specified function in the module symbol table.
2196056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner// If it does not exist, return null.
2206056c49ca0e86e222b4bd7184a4b23c9277ab065Chris Lattner//
2212928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarFunction *Module::getFunction(StringRef Name) const {
222f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  return dyn_cast_or_null<Function>(getNamedValue(Name));
223bb0e2487185e401c7fca63d55e59343e060912a2Chris Lattner}
224bb0e2487185e401c7fca63d55e59343e060912a2Chris Lattner
22560837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
22660837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner// Methods for easy access to the global variables in the module.
22760837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
22860837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
22960837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner/// getGlobalVariable - Look up the specified global variable in the module
23030614675f45809c43d44c7243d8b747b39403155Chris Lattner/// symbol table.  If it does not exist, return null.  The type argument
23130614675f45809c43d44c7243d8b747b39403155Chris Lattner/// should be the underlying type of the global, i.e., it should not have
23230614675f45809c43d44c7243d8b747b39403155Chris Lattner/// the top-level PointerType, which represents the address of the global.
233bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola/// If AllowLocal is set to true, this function will return types that
234bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola/// have an local. By default, these types are not returned.
23560837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner///
2362d680824e3a5272e386aa6c1d2a66676de7899fdRafael EspindolaGlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
23785d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling  if (GlobalVariable *Result =
238f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar      dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
239f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar    if (AllowLocal || !Result->hasLocalLinkage())
24060837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner      return Result;
24160837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner  return 0;
24260837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner}
24360837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
244b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling/// getOrInsertGlobal - Look up the specified global in the module symbol table.
245b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///   1. If it does not exist, add a declaration of the global and return it.
246b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///   2. Else, the global exists but has the wrong type: return the function
247b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///      with a constantexpr cast to the right type.
248b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///   3. Finally, if the existing global is the correct delclaration, return the
249b4b130f0b10184efd34f76c0287943b02da5688bBill Wendling///      existing global.
250db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerConstant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
251b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // See if we have a definition for the specified global already.
252f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
253b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  if (GV == 0) {
254b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling    // Nope, add it
255b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling    GlobalVariable *New =
256e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson      new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
257e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                         0, Name);
258e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson     return New;                    // Return the new declaration.
259b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  }
260b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling
261b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // If the variable exists but has the wrong type, return a bitcast to the
262b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // right type.
263debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  if (GV->getType() != PointerType::getUnqual(Ty))
264debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
26585d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling
266b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  // Otherwise, we just found the existing function or a prototype.
267b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling  return GV;
268b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling}
269b7c2c1246f10972d5a8f55499226872608eb10f9Bill Wendling
27060837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
2718b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov// Methods for easy access to the global variables in the module.
2728b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov//
2738b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov
2748b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov// getNamedAlias - Look up the specified global in the module symbol table.
2758b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov// If it does not exist, return null.
2768b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov//
2772928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarGlobalAlias *Module::getNamedAlias(StringRef Name) const {
278f56ec6423cc8116136cfef314b9f3a519e37d3f9Daniel Dunbar  return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
2798b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov}
2808b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov
28149fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel/// getNamedMetadata - Return the first NamedMDNode in the module with the
28285d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling/// specified name. This method returns null if a NamedMDNode with the
28354eee524f192a0e7395d057481e8d4240729d7b4Bob Wilson/// specified name is not found.
284a762b093107ac3aa438815627006425d0b13a236Devang PatelNamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
2852f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel  SmallString<256> NameData;
2862f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel  StringRef NameRef = Name.toStringRef(NameData);
28717aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
2882f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel}
2892f7d5291de87bfa195884e84dc0efe9705c2215cDevang Patel
29085d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling/// getOrInsertNamedMetadata - Return the first named MDNode in the module
29185d043d6f6b59518bdaa20ae1957cbf645fd0cd6Bill Wendling/// with the specified name. This method returns a new NamedMDNode if a
29249fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel/// NamedMDNode with the specified name is not found.
2932928c83b010f7cfdb0f819199d806f6942a7d995Daniel DunbarNamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
29417aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDNode *&NMD =
29517aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
29617aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  if (!NMD) {
29717aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NMD = new NamedMDNode(Name);
29817aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NMD->setParent(this);
29917aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NamedMDList.push_back(NMD);
30017aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  }
30149fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel  return NMD;
30249fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel}
30349fe6c914c5f356a97619046384d7ceb2cb5187eDevang Patel
304d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
305d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// delete it.
30617aa92c92a925b4a674440c7ef088c223990e854Dan Gohmanvoid Module::eraseNamedMetadata(NamedMDNode *NMD) {
30717aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
30817aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDList.erase(NMD);
30917aa92c92a925b4a674440c7ef088c223990e854Dan Gohman}
31017aa92c92a925b4a674440c7ef088c223990e854Dan Gohman
311f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
312f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendlingvoid Module::
313f20f281368f75239c852fe99c3b3a19278ba38fdBill WendlinggetModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
314f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  const NamedMDNode *ModFlags = getModuleFlagsMetadata();
315f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  if (!ModFlags) return;
316f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling
317f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) {
318f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    MDNode *Flag = ModFlags->getOperand(i);
319f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
320f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    MDString *Key = cast<MDString>(Flag->getOperand(1));
321f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling    Value *Val = Flag->getOperand(2);
322e6bd7a805808a05e9869fbf067581855a8b2a2c2Bill Wendling    Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
323426f21573219eeba8f6981d7ddb4f1d2445b6343Bill Wendling                                    Key, Val));
324f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling  }
325f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling}
326f20f281368f75239c852fe99c3b3a19278ba38fdBill Wendling
327c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren/// Return the corresponding value if Key appears in module flags, otherwise
328c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren/// return null.
329c8cfaa1625a72aa3660a268dae753748cfed67d0Manman RenValue *Module::getModuleFlag(StringRef Key) const {
330c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren  SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
331c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren  getModuleFlagsMetadata(ModuleFlags);
332c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren  for (unsigned I = 0, E = ModuleFlags.size(); I < E; ++I) {
333c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren    const ModuleFlagEntry &MFE = ModuleFlags[I];
334c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren    if (Key == MFE.Key->getString())
335c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren      return MFE.Val;
336c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren  }
337c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren  return 0;
338c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren}
339c8cfaa1625a72aa3660a268dae753748cfed67d0Manman Ren
340d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
341d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// represents module-level flags. This method returns null if there are no
342d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// module-level flags.
343d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill WendlingNamedMDNode *Module::getModuleFlagsMetadata() const {
344d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  return getNamedMetadata("llvm.module.flags");
345d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
346d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling
347d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
348d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// represents module-level flags. If module-level flags aren't found, it
349d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// creates the named metadata that contains them.
350d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill WendlingNamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
351d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  return getOrInsertNamedMetadata("llvm.module.flags");
352d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
353d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling
354d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// addModuleFlag - Add a module-level flag to the module-level flags
355d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// metadata. It will create the module-level flags named metadata if it doesn't
356d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling/// already exist.
357e6bd7a805808a05e9869fbf067581855a8b2a2c2Bill Wendlingvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
358d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling                           Value *Val) {
359d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  Type *Int32Ty = Type::getInt32Ty(Context);
360d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  Value *Ops[3] = {
361d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling    ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
362d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  };
363d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
364d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
365e6bd7a805808a05e9869fbf067581855a8b2a2c2Bill Wendlingvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
366d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling                           uint32_t Val) {
367d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  Type *Int32Ty = Type::getInt32Ty(Context);
368d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
369d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
370d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendlingvoid Module::addModuleFlag(MDNode *Node) {
371d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  assert(Node->getNumOperands() == 3 &&
372d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling         "Invalid number of operands for module flag!");
373d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  assert(isa<ConstantInt>(Node->getOperand(0)) &&
374d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling         isa<MDString>(Node->getOperand(1)) &&
375d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling         "Invalid operand types for module flag!");
376d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling  getOrInsertModuleFlagsMetadata()->addOperand(Node);
377d34cb1e09f5e4b3feb7305346655b83ad2f67773Bill Wendling}
378f33fa6fb57b9c41e99739eff79b7f185e0df9500Chris Lattner
37960837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//===----------------------------------------------------------------------===//
380f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin// Methods to control the materialization of GlobalValues in the Module.
381f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin//
382f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinvoid Module::setMaterializer(GVMaterializer *GVM) {
383f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  assert(!Materializer &&
384f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin         "Module already has a GVMaterializer.  Call MaterializeAllPermanently"
385f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin         " to clear it out before setting another one.");
386f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Materializer.reset(GVM);
387f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
388f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
389f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::isMaterializable(const GlobalValue *GV) const {
390f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
391f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->isMaterializable(GV);
392f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
393f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
394f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
395f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::isDematerializable(const GlobalValue *GV) const {
396f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
397f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->isDematerializable(GV);
398f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
399f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
400f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
401f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
402f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
403f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->Materialize(GV, ErrInfo);
404f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
405f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
406f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
407f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinvoid Module::Dematerialize(GlobalValue *GV) {
408f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (Materializer)
409f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return Materializer->Dematerialize(GV);
410f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
411f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
412f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::MaterializeAll(std::string *ErrInfo) {
413f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (!Materializer)
414f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return false;
415f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return Materializer->MaterializeModule(this, ErrInfo);
416f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
417f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
418f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskinbool Module::MaterializeAllPermanently(std::string *ErrInfo) {
419f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (MaterializeAll(ErrInfo))
420f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    return true;
421f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Materializer.reset();
422f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  return false;
423f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin}
424f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin
425f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin//===----------------------------------------------------------------------===//
42660837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner// Other module related stuff.
42760837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner//
42860837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
42960837821e2bb179c6dd239ecb4d72df37560d3bbChris Lattner
430cf84d867619b68be17bb1c12bd9831fdd73a88e4Eric Christopher// dropAllReferences() - This function causes all the subelements to "let go"
4319da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// of all references that they are maintaining.  This allows one to 'delete' a
4329da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// whole module at a time, even though there may be circular references... first
4339da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// all references are dropped, and all use counts go to zero.  Then everything
4346b63452c3ad26678b32f93dbca55902a313ee4e9Misha Brukman// is deleted for real.  Note that no operations are valid on an object that
4359da0715aa4ec1cc9181b786c100fa3dfabe9a404Chris Lattner// has "dropped all references", except operator delete.
436009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
437009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnervoid Module::dropAllReferences() {
4387e70829632f82de15db187845666aaca6e04b792Chris Lattner  for(Module::iterator I = begin(), E = end(); I != E; ++I)
4397e70829632f82de15db187845666aaca6e04b792Chris Lattner    I->dropAllReferences();
440dd6dfbc8cc008faca03b6ac9b904f263205a418aChris Lattner
441e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner  for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
4427e70829632f82de15db187845666aaca6e04b792Chris Lattner    I->dropAllReferences();
443a80e1181b78183dc36ec6568559d38faa86981f0Anton Korobeynikov
444a80e1181b78183dc36ec6568559d38faa86981f0Anton Korobeynikov  for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
445a80e1181b78183dc36ec6568559d38faa86981f0Anton Korobeynikov    I->dropAllReferences();
446009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
447