Revert "[InstCombineCalls] Use isKnownNonNullAt() to check nullness of passing argume...
[oota-llvm.git] / lib / IR / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
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 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements LLVMContext, as a wrapper around the opaque
11 //  class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugLoc.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include <cctype>
26 using namespace llvm;
27
28 static ManagedStatic<LLVMContext> GlobalContext;
29
30 LLVMContext& llvm::getGlobalContext() {
31   return *GlobalContext;
32 }
33
34 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
35   // Create the fixed metadata kinds. This is done in the same order as the
36   // MD_* enum values so that they correspond.
37
38   // Create the 'dbg' metadata kind.
39   unsigned DbgID = getMDKindID("dbg");
40   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
41
42   // Create the 'tbaa' metadata kind.
43   unsigned TBAAID = getMDKindID("tbaa");
44   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
45
46   // Create the 'prof' metadata kind.
47   unsigned ProfID = getMDKindID("prof");
48   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
49
50   // Create the 'fpmath' metadata kind.
51   unsigned FPAccuracyID = getMDKindID("fpmath");
52   assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
53   (void)FPAccuracyID;
54
55   // Create the 'range' metadata kind.
56   unsigned RangeID = getMDKindID("range");
57   assert(RangeID == MD_range && "range kind id drifted");
58   (void)RangeID;
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;
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;
69
70   // Create the 'alias.scope' metadata kind.
71   unsigned AliasScopeID = getMDKindID("alias.scope");
72   assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
73   (void)AliasScopeID;
74
75   // Create the 'noalias' metadata kind.
76   unsigned NoAliasID = getMDKindID("noalias");
77   assert(NoAliasID == MD_noalias && "noalias kind id drifted");
78   (void)NoAliasID;
79
80   // Create the 'nontemporal' metadata kind.
81   unsigned NonTemporalID = getMDKindID("nontemporal");
82   assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
83   (void)NonTemporalID;
84
85   // Create the 'llvm.mem.parallel_loop_access' metadata kind.
86   unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access");
87   assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access &&
88          "mem_parallel_loop_access kind id drifted");
89   (void)MemParallelLoopAccessID;
90
91   // Create the 'nonnull' metadata kind.
92   unsigned NonNullID = getMDKindID("nonnull");
93   assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
94   (void)NonNullID;
95   
96   // Create the 'dereferenceable' metadata kind.
97   unsigned DereferenceableID = getMDKindID("dereferenceable");
98   assert(DereferenceableID == MD_dereferenceable && 
99          "dereferenceable kind id drifted");
100   (void)DereferenceableID;
101   
102   // Create the 'dereferenceable_or_null' metadata kind.
103   unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null");
104   assert(DereferenceableOrNullID == MD_dereferenceable_or_null && 
105          "dereferenceable_or_null kind id drifted");
106   (void)DereferenceableOrNullID;
107
108   // Create the 'make.implicit' metadata kind.
109   unsigned MakeImplicitID = getMDKindID("make.implicit");
110   assert(MakeImplicitID == MD_make_implicit &&
111          "make.implicit kind id drifted");
112   (void)MakeImplicitID;
113
114   // Create the 'unpredictable' metadata kind.
115   unsigned UnpredictableID = getMDKindID("unpredictable");
116   assert(UnpredictableID == MD_unpredictable &&
117          "unpredictable kind id drifted");
118   (void)UnpredictableID;
119 }
120 LLVMContext::~LLVMContext() { delete pImpl; }
121
122 void LLVMContext::addModule(Module *M) {
123   pImpl->OwnedModules.insert(M);
124 }
125
126 void LLVMContext::removeModule(Module *M) {
127   pImpl->OwnedModules.erase(M);
128 }
129
130 //===----------------------------------------------------------------------===//
131 // Recoverable Backend Errors
132 //===----------------------------------------------------------------------===//
133
134 void LLVMContext::
135 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
136                               void *DiagContext) {
137   pImpl->InlineAsmDiagHandler = DiagHandler;
138   pImpl->InlineAsmDiagContext = DiagContext;
139 }
140
141 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
142 /// setInlineAsmDiagnosticHandler.
143 LLVMContext::InlineAsmDiagHandlerTy
144 LLVMContext::getInlineAsmDiagnosticHandler() const {
145   return pImpl->InlineAsmDiagHandler;
146 }
147
148 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
149 /// setInlineAsmDiagnosticHandler.
150 void *LLVMContext::getInlineAsmDiagnosticContext() const {
151   return pImpl->InlineAsmDiagContext;
152 }
153
154 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
155                                        void *DiagnosticContext,
156                                        bool RespectFilters) {
157   pImpl->DiagnosticHandler = DiagnosticHandler;
158   pImpl->DiagnosticContext = DiagnosticContext;
159   pImpl->RespectDiagnosticFilters = RespectFilters;
160 }
161
162 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
163   return pImpl->DiagnosticHandler;
164 }
165
166 void *LLVMContext::getDiagnosticContext() const {
167   return pImpl->DiagnosticContext;
168 }
169
170 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
171 {
172   pImpl->YieldCallback = Callback;
173   pImpl->YieldOpaqueHandle = OpaqueHandle;
174 }
175
176 void LLVMContext::yield() {
177   if (pImpl->YieldCallback)
178     pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
179 }
180
181 void LLVMContext::emitError(const Twine &ErrorStr) {
182   diagnose(DiagnosticInfoInlineAsm(ErrorStr));
183 }
184
185 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
186   assert (I && "Invalid instruction");
187   diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
188 }
189
190 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
191   // Optimization remarks are selective. They need to check whether the regexp
192   // pattern, passed via one of the -pass-remarks* flags, matches the name of
193   // the pass that is emitting the diagnostic. If there is no match, ignore the
194   // diagnostic and return.
195   switch (DI.getKind()) {
196   case llvm::DK_OptimizationRemark:
197     if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
198       return false;
199     break;
200   case llvm::DK_OptimizationRemarkMissed:
201     if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
202       return false;
203     break;
204   case llvm::DK_OptimizationRemarkAnalysis:
205     if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
206       return false;
207     break;
208   case llvm::DK_OptimizationRemarkAnalysisFPCommute:
209     if (!cast<DiagnosticInfoOptimizationRemarkAnalysisFPCommute>(DI)
210              .isEnabled())
211       return false;
212     break;
213   default:
214     break;
215   }
216   return true;
217 }
218
219 static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
220   switch (Severity) {
221   case DS_Error:
222     return "error";
223   case DS_Warning:
224     return "warning";
225   case DS_Remark:
226     return "remark";
227   case DS_Note:
228     return "note";
229   }
230   llvm_unreachable("Unknown DiagnosticSeverity");
231 }
232
233 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
234   // If there is a report handler, use it.
235   if (pImpl->DiagnosticHandler) {
236     if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
237       pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
238     return;
239   }
240
241   if (!isDiagnosticEnabled(DI))
242     return;
243
244   // Otherwise, print the message with a prefix based on the severity.
245   DiagnosticPrinterRawOStream DP(errs());
246   errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
247   DI.print(DP);
248   errs() << "\n";
249   if (DI.getSeverity() == DS_Error)
250     exit(1);
251 }
252
253 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
254   diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
255 }
256
257 //===----------------------------------------------------------------------===//
258 // Metadata Kind Uniquing
259 //===----------------------------------------------------------------------===//
260
261 /// Return a unique non-zero ID for the specified metadata kind.
262 unsigned LLVMContext::getMDKindID(StringRef Name) const {
263   // If this is new, assign it its ID.
264   return pImpl->CustomMDKindNames.insert(
265                                      std::make_pair(
266                                          Name, pImpl->CustomMDKindNames.size()))
267       .first->second;
268 }
269
270 /// getHandlerNames - Populate client-supplied smallvector using custom
271 /// metadata name and ID.
272 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
273   Names.resize(pImpl->CustomMDKindNames.size());
274   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
275        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
276     Names[I->second] = I->first();
277 }