Nick Lewycky | fca2acb | 2012-12-26 22:00:49 +0000 | [diff] [blame] | 1 | //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements LLVMContext, as a wrapper around the opaque |
Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 11 | // class LLVMContextImpl. |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DebugLoc.h" |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticInfo.h" |
| 20 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Instruction.h" |
| 22 | #include "llvm/IR/Metadata.h" |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SourceMgr.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 25 | #include <cctype> |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 28 | static ManagedStatic<LLVMContext> GlobalContext; |
| 29 | |
Owen Anderson | 2a15443 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 30 | LLVMContext& llvm::getGlobalContext() { |
Owen Anderson | 1cf085d | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 31 | return *GlobalContext; |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 34 | LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 35 | // Create the fixed metadata kinds. This is done in the same order as the |
| 36 | // MD_* enum values so that they correspond. |
| 37 | |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 38 | // Create the 'dbg' metadata kind. |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 39 | unsigned DbgID = getMDKindID("dbg"); |
| 40 | assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 41 | |
| 42 | // Create the 'tbaa' metadata kind. |
| 43 | unsigned TBAAID = getMDKindID("tbaa"); |
| 44 | assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; |
Jakub Staszak | 3f158fd | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 45 | |
| 46 | // Create the 'prof' metadata kind. |
| 47 | unsigned ProfID = getMDKindID("prof"); |
| 48 | assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; |
Peter Collingbourne | f7d1e7b | 2011-10-27 19:19:14 +0000 | [diff] [blame] | 49 | |
Duncan Sands | 34bd91a | 2012-04-14 12:36:06 +0000 | [diff] [blame] | 50 | // Create the 'fpmath' metadata kind. |
| 51 | unsigned FPAccuracyID = getMDKindID("fpmath"); |
| 52 | assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); |
Peter Collingbourne | f7d1e7b | 2011-10-27 19:19:14 +0000 | [diff] [blame] | 53 | (void)FPAccuracyID; |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 54 | |
| 55 | // Create the 'range' metadata kind. |
| 56 | unsigned RangeID = getMDKindID("range"); |
| 57 | assert(RangeID == MD_range && "range kind id drifted"); |
| 58 | (void)RangeID; |
Dan Gohman | 3effe81 | 2012-09-13 17:56:17 +0000 | [diff] [blame] | 59 | |
| 60 | // Create the 'tbaa.struct' metadata kind. |
| 61 | unsigned TBAAStructID = getMDKindID("tbaa.struct"); |
| 62 | assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); |
| 63 | (void)TBAAStructID; |
Shuxin Yang | 408bdad | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 64 | |
| 65 | // Create the 'invariant.load' metadata kind. |
| 66 | unsigned InvariantLdId = getMDKindID("invariant.load"); |
| 67 | assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted"); |
| 68 | (void)InvariantLdId; |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 69 | } |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 70 | LLVMContext::~LLVMContext() { delete pImpl; } |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 71 | |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 72 | void LLVMContext::addModule(Module *M) { |
| 73 | pImpl->OwnedModules.insert(M); |
| 74 | } |
| 75 | |
| 76 | void LLVMContext::removeModule(Module *M) { |
| 77 | pImpl->OwnedModules.erase(M); |
| 78 | } |
| 79 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
| 81 | // Recoverable Backend Errors |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | |
Bob Wilson | a594fab | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 84 | void LLVMContext:: |
| 85 | setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, |
| 86 | void *DiagContext) { |
| 87 | pImpl->InlineAsmDiagHandler = DiagHandler; |
| 88 | pImpl->InlineAsmDiagContext = DiagContext; |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Bob Wilson | a594fab | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 91 | /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by |
| 92 | /// setInlineAsmDiagnosticHandler. |
| 93 | LLVMContext::InlineAsmDiagHandlerTy |
| 94 | LLVMContext::getInlineAsmDiagnosticHandler() const { |
| 95 | return pImpl->InlineAsmDiagHandler; |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Bob Wilson | a594fab | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 98 | /// getInlineAsmDiagnosticContext - Return the diagnostic context set by |
| 99 | /// setInlineAsmDiagnosticHandler. |
| 100 | void *LLVMContext::getInlineAsmDiagnosticContext() const { |
| 101 | return pImpl->InlineAsmDiagContext; |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 103 | |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 104 | void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, |
| 105 | void *DiagnosticContext) { |
| 106 | pImpl->DiagnosticHandler = DiagnosticHandler; |
| 107 | pImpl->DiagnosticContext = DiagnosticContext; |
| 108 | } |
| 109 | |
| 110 | LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { |
| 111 | return pImpl->DiagnosticHandler; |
| 112 | } |
| 113 | |
| 114 | void *LLVMContext::getDiagnosticContext() const { |
| 115 | return pImpl->DiagnosticContext; |
| 116 | } |
| 117 | |
Chris Lattner | e22e613 | 2012-01-03 23:47:05 +0000 | [diff] [blame] | 118 | void LLVMContext::emitError(const Twine &ErrorStr) { |
Quentin Colombet | dec8d55 | 2014-02-22 00:34:11 +0000 | [diff] [blame] | 119 | diagnose(DiagnosticInfoInlineAsm(ErrorStr)); |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Bob Wilson | bfb44ef | 2013-02-08 21:48:29 +0000 | [diff] [blame] | 122 | void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { |
Quentin Colombet | dec8d55 | 2014-02-22 00:34:11 +0000 | [diff] [blame] | 123 | assert (I && "Invalid instruction"); |
| 124 | diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 127 | void LLVMContext::diagnose(const DiagnosticInfo &DI) { |
| 128 | // If there is a report handler, use it. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 129 | if (pImpl->DiagnosticHandler) { |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 130 | pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); |
| 131 | return; |
| 132 | } |
Diego Novillo | df65501 | 2014-04-16 16:53:41 +0000 | [diff] [blame^] | 133 | |
| 134 | // Optimization remarks are selective. They need to check whether |
| 135 | // the regexp pattern, passed via -pass-remarks, matches the name |
| 136 | // of the pass that is emitting the diagnostic. If there is no match, |
| 137 | // ignore the diagnostic and return. |
| 138 | if (DI.getKind() == llvm::DK_OptimizationRemark && |
| 139 | !pImpl->optimizationRemarksEnabledFor( |
| 140 | cast<DiagnosticInfoOptimizationRemark>(DI).getPassName())) |
| 141 | return; |
| 142 | |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 143 | // Otherwise, print the message with a prefix based on the severity. |
| 144 | std::string MsgStorage; |
| 145 | raw_string_ostream Stream(MsgStorage); |
| 146 | DiagnosticPrinterRawOStream DP(Stream); |
| 147 | DI.print(DP); |
| 148 | Stream.flush(); |
| 149 | switch (DI.getSeverity()) { |
| 150 | case DS_Error: |
| 151 | errs() << "error: " << MsgStorage << "\n"; |
| 152 | exit(1); |
| 153 | case DS_Warning: |
| 154 | errs() << "warning: " << MsgStorage << "\n"; |
| 155 | break; |
Tobias Grosser | e8d4c9a | 2014-02-28 09:08:45 +0000 | [diff] [blame] | 156 | case DS_Remark: |
| 157 | errs() << "remark: " << MsgStorage << "\n"; |
| 158 | break; |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 159 | case DS_Note: |
| 160 | errs() << "note: " << MsgStorage << "\n"; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
Chris Lattner | e22e613 | 2012-01-03 23:47:05 +0000 | [diff] [blame] | 165 | void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { |
Quentin Colombet | dec8d55 | 2014-02-22 00:34:11 +0000 | [diff] [blame] | 166 | diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 169 | void LLVMContext::emitOptimizationRemark(const char *PassName, |
| 170 | const Function &Fn, |
| 171 | const DebugLoc &DLoc, |
| 172 | const Twine &Msg) { |
Diego Novillo | df65501 | 2014-04-16 16:53:41 +0000 | [diff] [blame^] | 173 | diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg)); |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Chris Lattner | 1e45789 | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 176 | //===----------------------------------------------------------------------===// |
| 177 | // Metadata Kind Uniquing |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 180 | #ifndef NDEBUG |
| 181 | /// isValidName - Return true if Name is a valid custom metadata handler name. |
| 182 | static bool isValidName(StringRef MDName) { |
| 183 | if (MDName.empty()) |
| 184 | return false; |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 185 | |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 186 | if (!std::isalpha(static_cast<unsigned char>(MDName[0]))) |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 187 | return false; |
Bob Wilson | ec3ff9c | 2010-12-17 23:06:32 +0000 | [diff] [blame] | 188 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 189 | for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; |
| 190 | ++I) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 191 | if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' && |
| 192 | *I != '-' && *I != '.') |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | return true; |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 197 | #endif |
| 198 | |
| 199 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 200 | unsigned LLVMContext::getMDKindID(StringRef Name) const { |
| 201 | assert(isValidName(Name) && "Invalid MDNode name"); |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 202 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 203 | // If this is new, assign it its ID. |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 204 | return |
| 205 | pImpl->CustomMDKindNames.GetOrCreateValue( |
| 206 | Name, pImpl->CustomMDKindNames.size()).second; |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 210 | /// metadata name and ID. |
| 211 | void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { |
Dan Gohman | 43aa8f0 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 212 | Names.resize(pImpl->CustomMDKindNames.size()); |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 213 | for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), |
| 214 | E = pImpl->CustomMDKindNames.end(); I != E; ++I) |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 215 | Names[I->second] = I->first(); |
| 216 | } |