Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ---------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Module class for the IR library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "SymbolTableListTraitsImpl.h" |
| 16 | #include "llvm/ADT/DenseSet.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallString.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | d1163aa | 2014-03-06 03:50:29 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GVMaterializer.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstrTypes.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 4b6845c | 2014-03-04 12:46:06 +0000 | [diff] [blame] | 25 | #include "llvm/IR/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 27 | #include <cstdarg> |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 28 | #include <cstdlib> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3bcead7 | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 32 | // Methods to implement the globals and functions lists. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 33 | // |
| 34 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 35 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 36 | // are not in the public header file. |
John McCall | 086bb4e | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 37 | template class llvm::SymbolTableListTraits<Function, Module>; |
Nick Lewycky | 2be81ac | 2011-08-13 01:04:44 +0000 | [diff] [blame] | 38 | template class llvm::SymbolTableListTraits<GlobalVariable, Module>; |
John McCall | 086bb4e | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 39 | template class llvm::SymbolTableListTraits<GlobalAlias, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // Primitive Module methods. |
| 43 | // |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 44 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 45 | Module::Module(StringRef MID, LLVMContext &C) |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 46 | : Context(C), Materializer(), ModuleID(MID), DL("") { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 47 | ValSymTab = new ValueSymbolTable(); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 48 | NamedMDSymTab = new StringMap<NamedMDNode *>(); |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 49 | Context.addModule(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Module::~Module() { |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 53 | Context.removeModule(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 55 | GlobalList.clear(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 56 | FunctionList.clear(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 57 | AliasList.clear(); |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 58 | NamedMDList.clear(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 59 | delete ValSymTab; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 60 | delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 63 | /// getNamedValue - Return the first global value in the module with |
| 64 | /// the specified name, of arbitrary type. This method returns null |
| 65 | /// if a global with the specified name is not found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 66 | GlobalValue *Module::getNamedValue(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 67 | return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); |
| 68 | } |
| 69 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 70 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 71 | /// This ID is uniqued across modules in the current LLVMContext. |
| 72 | unsigned Module::getMDKindID(StringRef Name) const { |
| 73 | return Context.getMDKindID(Name); |
| 74 | } |
| 75 | |
| 76 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
| 77 | /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, |
| 78 | /// so it is filled in as an empty string. |
| 79 | void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { |
| 80 | return Context.getMDKindNames(Result); |
| 81 | } |
| 82 | |
| 83 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 84 | //===----------------------------------------------------------------------===// |
| 85 | // Methods for easy access to the functions in the module. |
| 86 | // |
| 87 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 88 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 89 | // table. If it does not exist, add a prototype for the function and return |
| 90 | // it. This is nice because it allows most passes to get away with not handling |
| 91 | // the symbol table directly for this common task. |
| 92 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 93 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 94 | FunctionType *Ty, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 95 | AttributeSet AttributeList) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 96 | // See if we have a definition for the specified function already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 97 | GlobalValue *F = getNamedValue(Name); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 98 | if (!F) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 99 | // Nope, add it |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 100 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 101 | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
| 102 | New->setAttributes(AttributeList); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 103 | FunctionList.push_back(New); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 104 | return New; // Return the new prototype. |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 107 | // If the function exists but has the wrong type, return a bitcast to the |
| 108 | // right type. |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 109 | if (F->getType() != PointerType::getUnqual(Ty)) |
| 110 | return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 112 | // Otherwise, we just found the existing function or a prototype. |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 113 | return F; |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 116 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 117 | FunctionType *Ty) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 118 | return getOrInsertFunction(Name, Ty, AttributeSet()); |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 121 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 122 | // table. If it does not exist, add a prototype for the function and return it. |
| 123 | // This version of the method takes a null terminated list of function |
| 124 | // arguments, which makes it easier for clients to use. |
| 125 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 126 | Constant *Module::getOrInsertFunction(StringRef Name, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 127 | AttributeSet AttributeList, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 128 | Type *RetTy, ...) { |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 129 | va_list Args; |
| 130 | va_start(Args, RetTy); |
| 131 | |
| 132 | // Build the list of argument types... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 133 | std::vector<Type*> ArgTys; |
| 134 | while (Type *ArgTy = va_arg(Args, Type*)) |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 135 | ArgTys.push_back(ArgTy); |
| 136 | |
| 137 | va_end(Args); |
| 138 | |
| 139 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 140 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 141 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 142 | AttributeList); |
| 143 | } |
| 144 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 145 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 146 | Type *RetTy, ...) { |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 147 | va_list Args; |
| 148 | va_start(Args, RetTy); |
| 149 | |
| 150 | // Build the list of argument types... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 151 | std::vector<Type*> ArgTys; |
| 152 | while (Type *ArgTy = va_arg(Args, Type*)) |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 153 | ArgTys.push_back(ArgTy); |
| 154 | |
| 155 | va_end(Args); |
| 156 | |
| 157 | // Build the function type and chain to the other getOrInsertFunction... |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 158 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 159 | FunctionType::get(RetTy, ArgTys, false), |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 160 | AttributeSet()); |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 163 | // getFunction - Look up the specified function in the module symbol table. |
| 164 | // If it does not exist, return null. |
| 165 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 166 | Function *Module::getFunction(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 167 | return dyn_cast_or_null<Function>(getNamedValue(Name)); |
Chris Lattner | e43649f | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // Methods for easy access to the global variables in the module. |
| 172 | // |
| 173 | |
| 174 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 175 | /// symbol table. If it does not exist, return null. The type argument |
| 176 | /// should be the underlying type of the global, i.e., it should not have |
| 177 | /// the top-level PointerType, which represents the address of the global. |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 178 | /// If AllowLocal is set to true, this function will return types that |
| 179 | /// have an local. By default, these types are not returned. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 180 | /// |
Rafael Espindola | ec2375f | 2013-07-25 02:50:08 +0000 | [diff] [blame] | 181 | GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) { |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 182 | if (GlobalVariable *Result = |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 183 | dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) |
| 184 | if (AllowLocal || !Result->hasLocalLinkage()) |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 185 | return Result; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 186 | return nullptr; |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 189 | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
| 190 | /// 1. If it does not exist, add a declaration of the global and return it. |
| 191 | /// 2. Else, the global exists but has the wrong type: return the function |
| 192 | /// with a constantexpr cast to the right type. |
Matt Arsenault | 5200fdf | 2013-09-28 01:08:00 +0000 | [diff] [blame] | 193 | /// 3. Finally, if the existing global is the correct declaration, return the |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 194 | /// existing global. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 195 | Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 196 | // See if we have a definition for the specified global already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 197 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 198 | if (!GV) { |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 199 | // Nope, add it |
| 200 | GlobalVariable *New = |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 201 | new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 202 | nullptr, Name); |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 203 | return New; // Return the new declaration. |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // If the variable exists but has the wrong type, return a bitcast to the |
| 207 | // right type. |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 208 | Type *GVTy = GV->getType(); |
| 209 | PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace()); |
Matt Arsenault | a90a340 | 2013-09-30 23:31:50 +0000 | [diff] [blame] | 210 | if (GVTy != PTy) |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 211 | return ConstantExpr::getBitCast(GV, PTy); |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 212 | |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 213 | // Otherwise, we just found the existing function or a prototype. |
| 214 | return GV; |
| 215 | } |
| 216 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 217 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 218 | // Methods for easy access to the global variables in the module. |
| 219 | // |
| 220 | |
| 221 | // getNamedAlias - Look up the specified global in the module symbol table. |
| 222 | // If it does not exist, return null. |
| 223 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 224 | GlobalAlias *Module::getNamedAlias(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 225 | return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 228 | /// getNamedMetadata - Return the first NamedMDNode in the module with the |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 229 | /// specified name. This method returns null if a NamedMDNode with the |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 230 | /// specified name is not found. |
Devang Patel | b6e058d | 2010-06-22 01:19:38 +0000 | [diff] [blame] | 231 | NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 232 | SmallString<256> NameData; |
| 233 | StringRef NameRef = Name.toStringRef(NameData); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 234 | return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef); |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 237 | /// getOrInsertNamedMetadata - Return the first named MDNode in the module |
| 238 | /// with the specified name. This method returns a new NamedMDNode if a |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 239 | /// NamedMDNode with the specified name is not found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 240 | NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 241 | NamedMDNode *&NMD = |
| 242 | (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name]; |
| 243 | if (!NMD) { |
| 244 | NMD = new NamedMDNode(Name); |
| 245 | NMD->setParent(this); |
| 246 | NamedMDList.push_back(NMD); |
| 247 | } |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 248 | return NMD; |
| 249 | } |
| 250 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 251 | /// eraseNamedMetadata - Remove the given NamedMDNode from this module and |
| 252 | /// delete it. |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 253 | void Module::eraseNamedMetadata(NamedMDNode *NMD) { |
| 254 | static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName()); |
| 255 | NamedMDList.erase(NMD); |
| 256 | } |
| 257 | |
Bill Wendling | 0294932 | 2012-02-15 22:34:20 +0000 | [diff] [blame] | 258 | /// getModuleFlagsMetadata - Returns the module flags in the provided vector. |
| 259 | void Module:: |
| 260 | getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { |
| 261 | const NamedMDNode *ModFlags = getModuleFlagsMetadata(); |
| 262 | if (!ModFlags) return; |
| 263 | |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 264 | for (const MDNode *Flag : ModFlags->operands()) { |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 265 | if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) && |
| 266 | isa<MDString>(Flag->getOperand(1))) { |
| 267 | // Check the operands of the MDNode before accessing the operands. |
| 268 | // The verifier will actually catch these failures. |
| 269 | ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0)); |
| 270 | MDString *Key = cast<MDString>(Flag->getOperand(1)); |
| 271 | Value *Val = Flag->getOperand(2); |
| 272 | Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()), |
| 273 | Key, Val)); |
| 274 | } |
Bill Wendling | 0294932 | 2012-02-15 22:34:20 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 278 | /// Return the corresponding value if Key appears in module flags, otherwise |
| 279 | /// return null. |
| 280 | Value *Module::getModuleFlag(StringRef Key) const { |
| 281 | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
| 282 | getModuleFlagsMetadata(ModuleFlags); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 283 | for (const ModuleFlagEntry &MFE : ModuleFlags) { |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 284 | if (Key == MFE.Key->getString()) |
| 285 | return MFE.Val; |
| 286 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 287 | return nullptr; |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 290 | /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that |
| 291 | /// represents module-level flags. This method returns null if there are no |
| 292 | /// module-level flags. |
| 293 | NamedMDNode *Module::getModuleFlagsMetadata() const { |
| 294 | return getNamedMetadata("llvm.module.flags"); |
| 295 | } |
| 296 | |
| 297 | /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that |
| 298 | /// represents module-level flags. If module-level flags aren't found, it |
| 299 | /// creates the named metadata that contains them. |
| 300 | NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { |
| 301 | return getOrInsertNamedMetadata("llvm.module.flags"); |
| 302 | } |
| 303 | |
| 304 | /// addModuleFlag - Add a module-level flag to the module-level flags |
| 305 | /// metadata. It will create the module-level flags named metadata if it doesn't |
| 306 | /// already exist. |
Bill Wendling | 89cc166 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 307 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 308 | Value *Val) { |
| 309 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 310 | Value *Ops[3] = { |
| 311 | ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val |
| 312 | }; |
| 313 | getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); |
| 314 | } |
Bill Wendling | 89cc166 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 315 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 316 | uint32_t Val) { |
| 317 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 318 | addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); |
| 319 | } |
| 320 | void Module::addModuleFlag(MDNode *Node) { |
| 321 | assert(Node->getNumOperands() == 3 && |
| 322 | "Invalid number of operands for module flag!"); |
| 323 | assert(isa<ConstantInt>(Node->getOperand(0)) && |
| 324 | isa<MDString>(Node->getOperand(1)) && |
| 325 | "Invalid operand types for module flag!"); |
| 326 | getOrInsertModuleFlagsMetadata()->addOperand(Node); |
| 327 | } |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 328 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 329 | void Module::setDataLayout(StringRef Desc) { |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 330 | DL.reset(Desc); |
| 331 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 332 | if (Desc.empty()) { |
| 333 | DataLayoutStr = ""; |
| 334 | } else { |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 335 | DataLayoutStr = DL.getStringRepresentation(); |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 336 | // DataLayoutStr is now equivalent to Desc, but since the representation |
| 337 | // is not unique, they may not be identical. |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | void Module::setDataLayout(const DataLayout *Other) { |
| 342 | if (!Other) { |
| 343 | DataLayoutStr = ""; |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 344 | DL.reset(""); |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 345 | } else { |
| 346 | DL = *Other; |
| 347 | DataLayoutStr = DL.getStringRepresentation(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | const DataLayout *Module::getDataLayout() const { |
| 352 | if (DataLayoutStr.empty()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 353 | return nullptr; |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 354 | return &DL; |
| 355 | } |
| 356 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 357 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 358 | // Methods to control the materialization of GlobalValues in the Module. |
| 359 | // |
| 360 | void Module::setMaterializer(GVMaterializer *GVM) { |
| 361 | assert(!Materializer && |
| 362 | "Module already has a GVMaterializer. Call MaterializeAllPermanently" |
| 363 | " to clear it out before setting another one."); |
| 364 | Materializer.reset(GVM); |
| 365 | } |
| 366 | |
| 367 | bool Module::isMaterializable(const GlobalValue *GV) const { |
| 368 | if (Materializer) |
| 369 | return Materializer->isMaterializable(GV); |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | bool Module::isDematerializable(const GlobalValue *GV) const { |
| 374 | if (Materializer) |
| 375 | return Materializer->isDematerializable(GV); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) { |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 380 | if (!Materializer) |
| 381 | return false; |
| 382 | |
| 383 | error_code EC = Materializer->Materialize(GV); |
| 384 | if (!EC) |
| 385 | return false; |
| 386 | if (ErrInfo) |
| 387 | *ErrInfo = EC.message(); |
| 388 | return true; |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void Module::Dematerialize(GlobalValue *GV) { |
| 392 | if (Materializer) |
| 393 | return Materializer->Dematerialize(GV); |
| 394 | } |
| 395 | |
Rafael Espindola | 1d06f72 | 2014-01-14 23:02:01 +0000 | [diff] [blame] | 396 | error_code Module::materializeAll() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 397 | if (!Materializer) |
Rafael Espindola | 1d06f72 | 2014-01-14 23:02:01 +0000 | [diff] [blame] | 398 | return error_code::success(); |
| 399 | return Materializer->MaterializeModule(this); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 402 | error_code Module::materializeAllPermanently() { |
| 403 | if (error_code EC = materializeAll()) |
| 404 | return EC; |
| 405 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 406 | Materializer.reset(); |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 407 | return error_code::success(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | //===----------------------------------------------------------------------===// |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 411 | // Other module related stuff. |
| 412 | // |
| 413 | |
| 414 | |
Eric Christopher | 7df0240 | 2012-04-16 23:54:31 +0000 | [diff] [blame] | 415 | // dropAllReferences() - This function causes all the subelements to "let go" |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 416 | // of all references that they are maintaining. This allows one to 'delete' a |
| 417 | // whole module at a time, even though there may be circular references... first |
| 418 | // all references are dropped, and all use counts go to zero. Then everything |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 419 | // is deleted for real. Note that no operations are valid on an object that |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 420 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 421 | // |
| 422 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 423 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 424 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 426 | for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 427 | I->dropAllReferences(); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 428 | |
| 429 | for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) |
| 430 | I->dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 431 | } |