LLVM  10.0.0svn
ExecutionEngine.cpp
Go to the documentation of this file.
1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the common interface used by the various execution engine
10 // subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Statistic.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/ValueHandle.h"
29 #include "llvm/Object/Archive.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/Host.h"
39 #include <cmath>
40 #include <cstring>
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "jit"
44 
45 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
46 STATISTIC(NumGlobals , "Number of global vars initialized");
47 
48 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
49  std::unique_ptr<Module> M, std::string *ErrorStr,
50  std::shared_ptr<MCJITMemoryManager> MemMgr,
51  std::shared_ptr<LegacyJITSymbolResolver> Resolver,
52  std::unique_ptr<TargetMachine> TM) = nullptr;
53 
54 ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
55  std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MemMgr,
56  std::shared_ptr<LegacyJITSymbolResolver> Resolver,
57  std::unique_ptr<TargetMachine> TM) = nullptr;
58 
59 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
60  std::string *ErrorStr) =nullptr;
61 
62 void JITEventListener::anchor() {}
63 
64 void ObjectCache::anchor() {}
65 
66 void ExecutionEngine::Init(std::unique_ptr<Module> M) {
67  CompilingLazily = false;
68  GVCompilationDisabled = false;
69  SymbolSearchingDisabled = false;
70 
71  // IR module verification is enabled by default in debug builds, and disabled
72  // by default in release builds.
73 #ifndef NDEBUG
74  VerifyModules = true;
75 #else
76  VerifyModules = false;
77 #endif
78 
79  assert(M && "Module is null?");
80  Modules.push_back(std::move(M));
81 }
82 
83 ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
84  : DL(M->getDataLayout()), LazyFunctionCreator(nullptr) {
85  Init(std::move(M));
86 }
87 
88 ExecutionEngine::ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M)
89  : DL(std::move(DL)), LazyFunctionCreator(nullptr) {
90  Init(std::move(M));
91 }
92 
95 }
96 
97 namespace {
98 /// Helper class which uses a value handler to automatically deletes the
99 /// memory block when the GlobalVariable is destroyed.
100 class GVMemoryBlock final : public CallbackVH {
101  GVMemoryBlock(const GlobalVariable *GV)
102  : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
103 
104 public:
105  /// Returns the address the GlobalVariable should be written into. The
106  /// GVMemoryBlock object prefixes that.
107  static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
108  Type *ElTy = GV->getValueType();
109  size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
110  void *RawMemory = ::operator new(
111  alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlignment(GV)) + GVSize);
112  new(RawMemory) GVMemoryBlock(GV);
113  return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
114  }
115 
116  void deleted() override {
117  // We allocated with operator new and with some extra memory hanging off the
118  // end, so don't just delete this. I'm not sure if this is actually
119  // required.
120  this->~GVMemoryBlock();
121  ::operator delete(this);
122  }
123 };
124 } // anonymous namespace
125 
127  return GVMemoryBlock::Create(GV, getDataLayout());
128 }
129 
130 void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
131  llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
132 }
133 
134 void
136  llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
137 }
138 
140  llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
141 }
142 
144  for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
145  Module *Found = I->get();
146  if (Found == M) {
147  I->release();
148  Modules.erase(I);
150  return true;
151  }
152  }
153  return false;
154 }
155 
157  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
158  Function *F = Modules[i]->getFunction(FnName);
159  if (F && !F->isDeclaration())
160  return F;
161  }
162  return nullptr;
163 }
164 
166  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
167  GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal);
168  if (GV && !GV->isDeclaration())
169  return GV;
170  }
171  return nullptr;
172 }
173 
175  GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
176  uint64_t OldVal;
177 
178  // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
179  // GlobalAddressMap.
180  if (I == GlobalAddressMap.end())
181  OldVal = 0;
182  else {
183  GlobalAddressReverseMap.erase(I->second);
184  OldVal = I->second;
185  GlobalAddressMap.erase(I);
186  }
187 
188  return OldVal;
189 }
190 
192  assert(GV->hasName() && "Global must have name.");
193 
194  MutexGuard locked(lock);
195  SmallString<128> FullName;
196 
197  const DataLayout &DL =
199  ? getDataLayout()
200  : GV->getParent()->getDataLayout();
201 
202  Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
203  return FullName.str();
204 }
205 
206 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
207  MutexGuard locked(lock);
208  addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
209 }
210 
212  MutexGuard locked(lock);
213 
214  assert(!Name.empty() && "Empty GlobalMapping symbol name!");
215 
216  LLVM_DEBUG(dbgs() << "JIT: Map \'" << Name << "\' to [" << Addr << "]\n";);
217  uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
218  assert((!CurVal || !Addr) && "GlobalMapping already established!");
219  CurVal = Addr;
220 
221  // If we are using the reverse mapping, add it too.
222  if (!EEState.getGlobalAddressReverseMap().empty()) {
223  std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
224  assert((!V.empty() || !Name.empty()) &&
225  "GlobalMapping already established!");
226  V = Name;
227  }
228 }
229 
231  MutexGuard locked(lock);
232 
233  EEState.getGlobalAddressMap().clear();
234  EEState.getGlobalAddressReverseMap().clear();
235 }
236 
238  MutexGuard locked(lock);
239 
240  for (GlobalObject &GO : M->global_objects())
241  EEState.RemoveMapping(getMangledName(&GO));
242 }
243 
245  void *Addr) {
246  MutexGuard locked(lock);
247  return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
248 }
249 
251  MutexGuard locked(lock);
252 
254  EEState.getGlobalAddressMap();
255 
256  // Deleting from the mapping?
257  if (!Addr)
258  return EEState.RemoveMapping(Name);
259 
260  uint64_t &CurVal = Map[Name];
261  uint64_t OldVal = CurVal;
262 
263  if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
264  EEState.getGlobalAddressReverseMap().erase(CurVal);
265  CurVal = Addr;
266 
267  // If we are using the reverse mapping, add it too.
268  if (!EEState.getGlobalAddressReverseMap().empty()) {
269  std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
270  assert((!V.empty() || !Name.empty()) &&
271  "GlobalMapping already established!");
272  V = Name;
273  }
274  return OldVal;
275 }
276 
278  MutexGuard locked(lock);
279  uint64_t Address = 0;
281  EEState.getGlobalAddressMap().find(S);
282  if (I != EEState.getGlobalAddressMap().end())
283  Address = I->second;
284  return Address;
285 }
286 
287 
289  MutexGuard locked(lock);
290  if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
291  return Address;
292  return nullptr;
293 }
294 
296  MutexGuard locked(lock);
298 }
299 
301  MutexGuard locked(lock);
302 
303  // If we haven't computed the reverse mapping yet, do so first.
304  if (EEState.getGlobalAddressReverseMap().empty()) {
306  I = EEState.getGlobalAddressMap().begin(),
307  E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
308  StringRef Name = I->first();
309  uint64_t Addr = I->second;
310  EEState.getGlobalAddressReverseMap().insert(std::make_pair(
311  Addr, Name));
312  }
313  }
314 
315  std::map<uint64_t, std::string>::iterator I =
316  EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
317 
318  if (I != EEState.getGlobalAddressReverseMap().end()) {
319  StringRef Name = I->second;
320  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
321  if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
322  return GV;
323  }
324  return nullptr;
325 }
326 
327 namespace {
328 class ArgvArray {
329  std::unique_ptr<char[]> Array;
330  std::vector<std::unique_ptr<char[]>> Values;
331 public:
332  /// Turn a vector of strings into a nice argv style array of pointers to null
333  /// terminated strings.
334  void *reset(LLVMContext &C, ExecutionEngine *EE,
335  const std::vector<std::string> &InputArgv);
336 };
337 } // anonymous namespace
338 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
339  const std::vector<std::string> &InputArgv) {
340  Values.clear(); // Free the old contents.
341  Values.reserve(InputArgv.size());
342  unsigned PtrSize = EE->getDataLayout().getPointerSize();
343  Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
344 
345  LLVM_DEBUG(dbgs() << "JIT: ARGV = " << (void *)Array.get() << "\n");
346  Type *SBytePtr = Type::getInt8PtrTy(C);
347 
348  for (unsigned i = 0; i != InputArgv.size(); ++i) {
349  unsigned Size = InputArgv[i].size()+1;
350  auto Dest = make_unique<char[]>(Size);
351  LLVM_DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void *)Dest.get()
352  << "\n");
353 
354  std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
355  Dest[Size-1] = 0;
356 
357  // Endian safe: Array[i] = (PointerTy)Dest;
358  EE->StoreValueToMemory(PTOGV(Dest.get()),
359  (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
360  Values.push_back(std::move(Dest));
361  }
362 
363  // Null terminate it
364  EE->StoreValueToMemory(PTOGV(nullptr),
365  (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
366  SBytePtr);
367  return Array.get();
368 }
369 
371  bool isDtors) {
372  StringRef Name(isDtors ? "llvm.global_dtors" : "llvm.global_ctors");
373  GlobalVariable *GV = module.getNamedGlobal(Name);
374 
375  // If this global has internal linkage, or if it has a use, then it must be
376  // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
377  // this is the case, don't execute any of the global ctors, __main will do
378  // it.
379  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
380 
381  // Should be an array of '{ i32, void ()* }' structs. The first value is
382  // the init priority, which we ignore.
384  if (!InitList)
385  return;
386  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
387  ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
388  if (!CS) continue;
389 
390  Constant *FP = CS->getOperand(1);
391  if (FP->isNullValue())
392  continue; // Found a sentinal value, ignore.
393 
394  // Strip off constant expression casts.
395  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
396  if (CE->isCast())
397  FP = CE->getOperand(0);
398 
399  // Execute the ctor/dtor function!
400  if (Function *F = dyn_cast<Function>(FP))
401  runFunction(F, None);
402 
403  // FIXME: It is marginally lame that we just do nothing here if we see an
404  // entry we don't recognize. It might not be unreasonable for the verifier
405  // to not even allow this and just assert here.
406  }
407 }
408 
410  // Execute global ctors/dtors for each module in the program.
411  for (std::unique_ptr<Module> &M : Modules)
413 }
414 
415 #ifndef NDEBUG
416 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
417 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
418  unsigned PtrSize = EE->getDataLayout().getPointerSize();
419  for (unsigned i = 0; i < PtrSize; ++i)
420  if (*(i + (uint8_t*)Loc))
421  return false;
422  return true;
423 }
424 #endif
425 
427  const std::vector<std::string> &argv,
428  const char * const * envp) {
429  std::vector<GenericValue> GVArgs;
430  GenericValue GVArgc;
431  GVArgc.IntVal = APInt(32, argv.size());
432 
433  // Check main() type
434  unsigned NumArgs = Fn->getFunctionType()->getNumParams();
435  FunctionType *FTy = Fn->getFunctionType();
436  Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
437 
438  // Check the argument types.
439  if (NumArgs > 3)
440  report_fatal_error("Invalid number of arguments of main() supplied");
441  if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
442  report_fatal_error("Invalid type for third argument of main() supplied");
443  if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
444  report_fatal_error("Invalid type for second argument of main() supplied");
445  if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
446  report_fatal_error("Invalid type for first argument of main() supplied");
447  if (!FTy->getReturnType()->isIntegerTy() &&
448  !FTy->getReturnType()->isVoidTy())
449  report_fatal_error("Invalid return type of main() supplied");
450 
451  ArgvArray CArgv;
452  ArgvArray CEnv;
453  if (NumArgs) {
454  GVArgs.push_back(GVArgc); // Arg #0 = argc.
455  if (NumArgs > 1) {
456  // Arg #1 = argv.
457  GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
458  assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
459  "argv[0] was null after CreateArgv");
460  if (NumArgs > 2) {
461  std::vector<std::string> EnvVars;
462  for (unsigned i = 0; envp[i]; ++i)
463  EnvVars.emplace_back(envp[i]);
464  // Arg #2 = envp.
465  GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
466  }
467  }
468  }
469 
470  return runFunction(Fn, GVArgs).IntVal.getZExtValue();
471 }
472 
474 
475 EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
476  : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
477  OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
478  UseOrcMCJITReplacement(false) {
479 // IR module verification is enabled by default in debug builds, and disabled
480 // by default in release builds.
481 #ifndef NDEBUG
482  VerifyModules = true;
483 #else
484  VerifyModules = false;
485 #endif
486 }
487 
489 
491  std::unique_ptr<RTDyldMemoryManager> mcjmm) {
492  auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
493  MemMgr = SharedMM;
494  Resolver = SharedMM;
495  return *this;
496 }
497 
499 EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
500  MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
501  return *this;
502 }
503 
505 EngineBuilder::setSymbolResolver(std::unique_ptr<LegacyJITSymbolResolver> SR) {
506  Resolver = std::shared_ptr<LegacyJITSymbolResolver>(std::move(SR));
507  return *this;
508 }
509 
511  std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
512 
513  // Make sure we can resolve symbols in the program as well. The zero arg
514  // to the function tells DynamicLibrary to load the program, not a library.
515  if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
516  return nullptr;
517 
518  // If the user specified a memory manager but didn't specify which engine to
519  // create, we assume they only want the JIT, and we fail if they only want
520  // the interpreter.
521  if (MemMgr) {
522  if (WhichEngine & EngineKind::JIT)
523  WhichEngine = EngineKind::JIT;
524  else {
525  if (ErrorStr)
526  *ErrorStr = "Cannot create an interpreter with a memory manager.";
527  return nullptr;
528  }
529  }
530 
531  // Unless the interpreter was explicitly selected or the JIT is not linked,
532  // try making a JIT.
533  if ((WhichEngine & EngineKind::JIT) && TheTM) {
534  if (!TM->getTarget().hasJIT()) {
535  errs() << "WARNING: This target JIT is not designed for the host"
536  << " you are running. If bad things happen, please choose"
537  << " a different -march switch.\n";
538  }
539 
540  ExecutionEngine *EE = nullptr;
541  if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
542  EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
543  std::move(Resolver),
544  std::move(TheTM));
545  EE->addModule(std::move(M));
546  } else if (ExecutionEngine::MCJITCtor)
547  EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
548  std::move(Resolver), std::move(TheTM));
549 
550  if (EE) {
551  EE->setVerifyModules(VerifyModules);
552  return EE;
553  }
554  }
555 
556  // If we can't make a JIT and we didn't request one specifically, try making
557  // an interpreter instead.
558  if (WhichEngine & EngineKind::Interpreter) {
560  return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
561  if (ErrorStr)
562  *ErrorStr = "Interpreter has not been linked in.";
563  return nullptr;
564  }
565 
566  if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
567  if (ErrorStr)
568  *ErrorStr = "JIT has not been linked in.";
569  }
570 
571  return nullptr;
572 }
573 
575  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
576  return getPointerToFunction(F);
577 
578  MutexGuard locked(lock);
579  if (void* P = getPointerToGlobalIfAvailable(GV))
580  return P;
581 
582  // Global variable might have been added since interpreter started.
583  if (GlobalVariable *GVar =
584  const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
585  EmitGlobalVariable(GVar);
586  else
587  llvm_unreachable("Global hasn't had an address allocated yet!");
588 
589  return getPointerToGlobalIfAvailable(GV);
590 }
591 
592 /// Converts a Constant* into a GenericValue, including handling of
593 /// ConstantExpr values.
595  // If its undefined, return the garbage.
596  if (isa<UndefValue>(C)) {
597  GenericValue Result;
598  switch (C->getType()->getTypeID()) {
599  default:
600  break;
601  case Type::IntegerTyID:
602  case Type::X86_FP80TyID:
603  case Type::FP128TyID:
604  case Type::PPC_FP128TyID:
605  // Although the value is undefined, we still have to construct an APInt
606  // with the correct bit width.
607  Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
608  break;
609  case Type::StructTyID: {
610  // if the whole struct is 'undef' just reserve memory for the value.
611  if(StructType *STy = dyn_cast<StructType>(C->getType())) {
612  unsigned int elemNum = STy->getNumElements();
613  Result.AggregateVal.resize(elemNum);
614  for (unsigned int i = 0; i < elemNum; ++i) {
615  Type *ElemTy = STy->getElementType(i);
616  if (ElemTy->isIntegerTy())
617  Result.AggregateVal[i].IntVal =
618  APInt(ElemTy->getPrimitiveSizeInBits(), 0);
619  else if (ElemTy->isAggregateType()) {
620  const Constant *ElemUndef = UndefValue::get(ElemTy);
621  Result.AggregateVal[i] = getConstantValue(ElemUndef);
622  }
623  }
624  }
625  }
626  break;
627  case Type::VectorTyID:
628  // if the whole vector is 'undef' just reserve memory for the value.
629  auto* VTy = dyn_cast<VectorType>(C->getType());
630  Type *ElemTy = VTy->getElementType();
631  unsigned int elemNum = VTy->getNumElements();
632  Result.AggregateVal.resize(elemNum);
633  if (ElemTy->isIntegerTy())
634  for (unsigned int i = 0; i < elemNum; ++i)
635  Result.AggregateVal[i].IntVal =
636  APInt(ElemTy->getPrimitiveSizeInBits(), 0);
637  break;
638  }
639  return Result;
640  }
641 
642  // Otherwise, if the value is a ConstantExpr...
643  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
644  Constant *Op0 = CE->getOperand(0);
645  switch (CE->getOpcode()) {
646  case Instruction::GetElementPtr: {
647  // Compute the index
648  GenericValue Result = getConstantValue(Op0);
649  APInt Offset(DL.getPointerSizeInBits(), 0);
650  cast<GEPOperator>(CE)->accumulateConstantOffset(DL, Offset);
651 
652  char* tmp = (char*) Result.PointerVal;
653  Result = PTOGV(tmp + Offset.getSExtValue());
654  return Result;
655  }
656  case Instruction::Trunc: {
657  GenericValue GV = getConstantValue(Op0);
658  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
659  GV.IntVal = GV.IntVal.trunc(BitWidth);
660  return GV;
661  }
662  case Instruction::ZExt: {
663  GenericValue GV = getConstantValue(Op0);
664  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
665  GV.IntVal = GV.IntVal.zext(BitWidth);
666  return GV;
667  }
668  case Instruction::SExt: {
669  GenericValue GV = getConstantValue(Op0);
670  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
671  GV.IntVal = GV.IntVal.sext(BitWidth);
672  return GV;
673  }
674  case Instruction::FPTrunc: {
675  // FIXME long double
676  GenericValue GV = getConstantValue(Op0);
677  GV.FloatVal = float(GV.DoubleVal);
678  return GV;
679  }
680  case Instruction::FPExt:{
681  // FIXME long double
682  GenericValue GV = getConstantValue(Op0);
683  GV.DoubleVal = double(GV.FloatVal);
684  return GV;
685  }
686  case Instruction::UIToFP: {
687  GenericValue GV = getConstantValue(Op0);
688  if (CE->getType()->isFloatTy())
689  GV.FloatVal = float(GV.IntVal.roundToDouble());
690  else if (CE->getType()->isDoubleTy())
691  GV.DoubleVal = GV.IntVal.roundToDouble();
692  else if (CE->getType()->isX86_FP80Ty()) {
694  (void)apf.convertFromAPInt(GV.IntVal,
695  false,
697  GV.IntVal = apf.bitcastToAPInt();
698  }
699  return GV;
700  }
701  case Instruction::SIToFP: {
702  GenericValue GV = getConstantValue(Op0);
703  if (CE->getType()->isFloatTy())
704  GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
705  else if (CE->getType()->isDoubleTy())
707  else if (CE->getType()->isX86_FP80Ty()) {
709  (void)apf.convertFromAPInt(GV.IntVal,
710  true,
712  GV.IntVal = apf.bitcastToAPInt();
713  }
714  return GV;
715  }
716  case Instruction::FPToUI: // double->APInt conversion handles sign
717  case Instruction::FPToSI: {
718  GenericValue GV = getConstantValue(Op0);
719  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
720  if (Op0->getType()->isFloatTy())
721  GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
722  else if (Op0->getType()->isDoubleTy())
723  GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
724  else if (Op0->getType()->isX86_FP80Ty()) {
726  uint64_t v;
727  bool ignored;
728  (void)apf.convertToInteger(makeMutableArrayRef(v), BitWidth,
729  CE->getOpcode()==Instruction::FPToSI,
730  APFloat::rmTowardZero, &ignored);
731  GV.IntVal = v; // endian?
732  }
733  return GV;
734  }
735  case Instruction::PtrToInt: {
736  GenericValue GV = getConstantValue(Op0);
737  uint32_t PtrWidth = DL.getTypeSizeInBits(Op0->getType());
738  assert(PtrWidth <= 64 && "Bad pointer width");
739  GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
740  uint32_t IntWidth = DL.getTypeSizeInBits(CE->getType());
741  GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
742  return GV;
743  }
744  case Instruction::IntToPtr: {
745  GenericValue GV = getConstantValue(Op0);
746  uint32_t PtrWidth = DL.getTypeSizeInBits(CE->getType());
747  GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
748  assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
749  GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
750  return GV;
751  }
752  case Instruction::BitCast: {
753  GenericValue GV = getConstantValue(Op0);
754  Type* DestTy = CE->getType();
755  switch (Op0->getType()->getTypeID()) {
756  default: llvm_unreachable("Invalid bitcast operand");
757  case Type::IntegerTyID:
758  assert(DestTy->isFloatingPointTy() && "invalid bitcast");
759  if (DestTy->isFloatTy())
760  GV.FloatVal = GV.IntVal.bitsToFloat();
761  else if (DestTy->isDoubleTy())
762  GV.DoubleVal = GV.IntVal.bitsToDouble();
763  break;
764  case Type::FloatTyID:
765  assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
767  break;
768  case Type::DoubleTyID:
769  assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
771  break;
772  case Type::PointerTyID:
773  assert(DestTy->isPointerTy() && "Invalid bitcast");
774  break; // getConstantValue(Op0) above already converted it
775  }
776  return GV;
777  }
778  case Instruction::Add:
779  case Instruction::FAdd:
780  case Instruction::Sub:
781  case Instruction::FSub:
782  case Instruction::Mul:
783  case Instruction::FMul:
784  case Instruction::UDiv:
785  case Instruction::SDiv:
786  case Instruction::URem:
787  case Instruction::SRem:
788  case Instruction::And:
789  case Instruction::Or:
790  case Instruction::Xor: {
791  GenericValue LHS = getConstantValue(Op0);
792  GenericValue RHS = getConstantValue(CE->getOperand(1));
793  GenericValue GV;
794  switch (CE->getOperand(0)->getType()->getTypeID()) {
795  default: llvm_unreachable("Bad add type!");
796  case Type::IntegerTyID:
797  switch (CE->getOpcode()) {
798  default: llvm_unreachable("Invalid integer opcode");
799  case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
800  case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
801  case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
802  case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
803  case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
804  case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
805  case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
806  case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
807  case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
808  case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
809  }
810  break;
811  case Type::FloatTyID:
812  switch (CE->getOpcode()) {
813  default: llvm_unreachable("Invalid float opcode");
814  case Instruction::FAdd:
815  GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
816  case Instruction::FSub:
817  GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
818  case Instruction::FMul:
819  GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
820  case Instruction::FDiv:
821  GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
822  case Instruction::FRem:
823  GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
824  }
825  break;
826  case Type::DoubleTyID:
827  switch (CE->getOpcode()) {
828  default: llvm_unreachable("Invalid double opcode");
829  case Instruction::FAdd:
830  GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
831  case Instruction::FSub:
832  GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
833  case Instruction::FMul:
834  GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
835  case Instruction::FDiv:
836  GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
837  case Instruction::FRem:
838  GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
839  }
840  break;
841  case Type::X86_FP80TyID:
842  case Type::PPC_FP128TyID:
843  case Type::FP128TyID: {
844  const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
845  APFloat apfLHS = APFloat(Sem, LHS.IntVal);
846  switch (CE->getOpcode()) {
847  default: llvm_unreachable("Invalid long double opcode");
848  case Instruction::FAdd:
849  apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
850  GV.IntVal = apfLHS.bitcastToAPInt();
851  break;
852  case Instruction::FSub:
853  apfLHS.subtract(APFloat(Sem, RHS.IntVal),
855  GV.IntVal = apfLHS.bitcastToAPInt();
856  break;
857  case Instruction::FMul:
858  apfLHS.multiply(APFloat(Sem, RHS.IntVal),
860  GV.IntVal = apfLHS.bitcastToAPInt();
861  break;
862  case Instruction::FDiv:
863  apfLHS.divide(APFloat(Sem, RHS.IntVal),
865  GV.IntVal = apfLHS.bitcastToAPInt();
866  break;
867  case Instruction::FRem:
868  apfLHS.mod(APFloat(Sem, RHS.IntVal));
869  GV.IntVal = apfLHS.bitcastToAPInt();
870  break;
871  }
872  }
873  break;
874  }
875  return GV;
876  }
877  default:
878  break;
879  }
880 
881  SmallString<256> Msg;
882  raw_svector_ostream OS(Msg);
883  OS << "ConstantExpr not handled: " << *CE;
884  report_fatal_error(OS.str());
885  }
886 
887  // Otherwise, we have a simple constant.
888  GenericValue Result;
889  switch (C->getType()->getTypeID()) {
890  case Type::FloatTyID:
891  Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
892  break;
893  case Type::DoubleTyID:
894  Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
895  break;
896  case Type::X86_FP80TyID:
897  case Type::FP128TyID:
898  case Type::PPC_FP128TyID:
899  Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
900  break;
901  case Type::IntegerTyID:
902  Result.IntVal = cast<ConstantInt>(C)->getValue();
903  break;
904  case Type::PointerTyID:
905  while (auto *A = dyn_cast<GlobalAlias>(C)) {
906  C = A->getAliasee();
907  }
908  if (isa<ConstantPointerNull>(C))
909  Result.PointerVal = nullptr;
910  else if (const Function *F = dyn_cast<Function>(C))
911  Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
912  else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
913  Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
914  else
915  llvm_unreachable("Unknown constant pointer type!");
916  break;
917  case Type::VectorTyID: {
918  unsigned elemNum;
919  Type* ElemTy;
921  const ConstantVector *CV = dyn_cast<ConstantVector>(C);
923 
924  if (CDV) {
925  elemNum = CDV->getNumElements();
926  ElemTy = CDV->getElementType();
927  } else if (CV || CAZ) {
928  VectorType* VTy = dyn_cast<VectorType>(C->getType());
929  elemNum = VTy->getNumElements();
930  ElemTy = VTy->getElementType();
931  } else {
932  llvm_unreachable("Unknown constant vector type!");
933  }
934 
935  Result.AggregateVal.resize(elemNum);
936  // Check if vector holds floats.
937  if(ElemTy->isFloatTy()) {
938  if (CAZ) {
939  GenericValue floatZero;
940  floatZero.FloatVal = 0.f;
941  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
942  floatZero);
943  break;
944  }
945  if(CV) {
946  for (unsigned i = 0; i < elemNum; ++i)
947  if (!isa<UndefValue>(CV->getOperand(i)))
948  Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
949  CV->getOperand(i))->getValueAPF().convertToFloat();
950  break;
951  }
952  if(CDV)
953  for (unsigned i = 0; i < elemNum; ++i)
954  Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
955 
956  break;
957  }
958  // Check if vector holds doubles.
959  if (ElemTy->isDoubleTy()) {
960  if (CAZ) {
961  GenericValue doubleZero;
962  doubleZero.DoubleVal = 0.0;
963  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
964  doubleZero);
965  break;
966  }
967  if(CV) {
968  for (unsigned i = 0; i < elemNum; ++i)
969  if (!isa<UndefValue>(CV->getOperand(i)))
970  Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
971  CV->getOperand(i))->getValueAPF().convertToDouble();
972  break;
973  }
974  if(CDV)
975  for (unsigned i = 0; i < elemNum; ++i)
976  Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
977 
978  break;
979  }
980  // Check if vector holds integers.
981  if (ElemTy->isIntegerTy()) {
982  if (CAZ) {
983  GenericValue intZero;
984  intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
985  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
986  intZero);
987  break;
988  }
989  if(CV) {
990  for (unsigned i = 0; i < elemNum; ++i)
991  if (!isa<UndefValue>(CV->getOperand(i)))
992  Result.AggregateVal[i].IntVal = cast<ConstantInt>(
993  CV->getOperand(i))->getValue();
994  else {
995  Result.AggregateVal[i].IntVal =
996  APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
997  }
998  break;
999  }
1000  if(CDV)
1001  for (unsigned i = 0; i < elemNum; ++i)
1002  Result.AggregateVal[i].IntVal = APInt(
1004  CDV->getElementAsInteger(i));
1005 
1006  break;
1007  }
1008  llvm_unreachable("Unknown constant pointer type!");
1009  }
1010  break;
1011 
1012  default:
1013  SmallString<256> Msg;
1014  raw_svector_ostream OS(Msg);
1015  OS << "ERROR: Constant unimplemented for type: " << *C->getType();
1016  report_fatal_error(OS.str());
1017  }
1018 
1019  return Result;
1020 }
1021 
1023  GenericValue *Ptr, Type *Ty) {
1024  const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);
1025 
1026  switch (Ty->getTypeID()) {
1027  default:
1028  dbgs() << "Cannot store value of type " << *Ty << "!\n";
1029  break;
1030  case Type::IntegerTyID:
1031  StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
1032  break;
1033  case Type::FloatTyID:
1034  *((float*)Ptr) = Val.FloatVal;
1035  break;
1036  case Type::DoubleTyID:
1037  *((double*)Ptr) = Val.DoubleVal;
1038  break;
1039  case Type::X86_FP80TyID:
1040  memcpy(Ptr, Val.IntVal.getRawData(), 10);
1041  break;
1042  case Type::PointerTyID:
1043  // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1044  if (StoreBytes != sizeof(PointerTy))
1045  memset(&(Ptr->PointerVal), 0, StoreBytes);
1046 
1047  *((PointerTy*)Ptr) = Val.PointerVal;
1048  break;
1049  case Type::VectorTyID:
1050  for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1051  if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1052  *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1053  if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1054  *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1055  if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1056  unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1057  StoreIntToMemory(Val.AggregateVal[i].IntVal,
1058  (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1059  }
1060  }
1061  break;
1062  }
1063 
1064  if (sys::IsLittleEndianHost != getDataLayout().isLittleEndian())
1065  // Host and target are different endian - reverse the stored bytes.
1066  std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1067 }
1068 
1069 /// FIXME: document
1070 ///
1072  GenericValue *Ptr,
1073  Type *Ty) {
1074  const unsigned LoadBytes = getDataLayout().getTypeStoreSize(Ty);
1075 
1076  switch (Ty->getTypeID()) {
1077  case Type::IntegerTyID:
1078  // An APInt with all words initially zero.
1079  Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1080  LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1081  break;
1082  case Type::FloatTyID:
1083  Result.FloatVal = *((float*)Ptr);
1084  break;
1085  case Type::DoubleTyID:
1086  Result.DoubleVal = *((double*)Ptr);
1087  break;
1088  case Type::PointerTyID:
1089  Result.PointerVal = *((PointerTy*)Ptr);
1090  break;
1091  case Type::X86_FP80TyID: {
1092  // This is endian dependent, but it will only work on x86 anyway.
1093  // FIXME: Will not trap if loading a signaling NaN.
1094  uint64_t y[2];
1095  memcpy(y, Ptr, 10);
1096  Result.IntVal = APInt(80, y);
1097  break;
1098  }
1099  case Type::VectorTyID: {
1100  auto *VT = cast<VectorType>(Ty);
1101  Type *ElemT = VT->getElementType();
1102  const unsigned numElems = VT->getNumElements();
1103  if (ElemT->isFloatTy()) {
1104  Result.AggregateVal.resize(numElems);
1105  for (unsigned i = 0; i < numElems; ++i)
1106  Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1107  }
1108  if (ElemT->isDoubleTy()) {
1109  Result.AggregateVal.resize(numElems);
1110  for (unsigned i = 0; i < numElems; ++i)
1111  Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1112  }
1113  if (ElemT->isIntegerTy()) {
1114  GenericValue intZero;
1115  const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1116  intZero.IntVal = APInt(elemBitWidth, 0);
1117  Result.AggregateVal.resize(numElems, intZero);
1118  for (unsigned i = 0; i < numElems; ++i)
1119  LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1120  (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1121  }
1122  break;
1123  }
1124  default:
1125  SmallString<256> Msg;
1126  raw_svector_ostream OS(Msg);
1127  OS << "Cannot load value of type " << *Ty << "!";
1128  report_fatal_error(OS.str());
1129  }
1130 }
1131 
1133  LLVM_DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1134  LLVM_DEBUG(Init->dump());
1135  if (isa<UndefValue>(Init))
1136  return;
1137 
1138  if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1139  unsigned ElementSize =
1140  getDataLayout().getTypeAllocSize(CP->getType()->getElementType());
1141  for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1142  InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1143  return;
1144  }
1145 
1146  if (isa<ConstantAggregateZero>(Init)) {
1147  memset(Addr, 0, (size_t)getDataLayout().getTypeAllocSize(Init->getType()));
1148  return;
1149  }
1150 
1151  if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1152  unsigned ElementSize =
1153  getDataLayout().getTypeAllocSize(CPA->getType()->getElementType());
1154  for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1155  InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1156  return;
1157  }
1158 
1159  if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1160  const StructLayout *SL =
1161  getDataLayout().getStructLayout(cast<StructType>(CPS->getType()));
1162  for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1163  InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1164  return;
1165  }
1166 
1167  if (const ConstantDataSequential *CDS =
1168  dyn_cast<ConstantDataSequential>(Init)) {
1169  // CDS is already laid out in host memory order.
1170  StringRef Data = CDS->getRawDataValues();
1171  memcpy(Addr, Data.data(), Data.size());
1172  return;
1173  }
1174 
1175  if (Init->getType()->isFirstClassType()) {
1176  GenericValue Val = getConstantValue(Init);
1177  StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1178  return;
1179  }
1180 
1181  LLVM_DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1182  llvm_unreachable("Unknown constant type to initialize memory with!");
1183 }
1184 
1185 /// EmitGlobals - Emit all of the global variables to memory, storing their
1186 /// addresses into GlobalAddress. This must make sure to copy the contents of
1187 /// their initializers into the memory.
1189  // Loop over all of the global variables in the program, allocating the memory
1190  // to hold them. If there is more than one module, do a prepass over globals
1191  // to figure out how the different modules should link together.
1192  std::map<std::pair<std::string, Type*>,
1193  const GlobalValue*> LinkedGlobalsMap;
1194 
1195  if (Modules.size() != 1) {
1196  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1197  Module &M = *Modules[m];
1198  for (const auto &GV : M.globals()) {
1199  if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1200  GV.hasAppendingLinkage() || !GV.hasName())
1201  continue;// Ignore external globals and globals with internal linkage.
1202 
1203  const GlobalValue *&GVEntry =
1204  LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
1205 
1206  // If this is the first time we've seen this global, it is the canonical
1207  // version.
1208  if (!GVEntry) {
1209  GVEntry = &GV;
1210  continue;
1211  }
1212 
1213  // If the existing global is strong, never replace it.
1214  if (GVEntry->hasExternalLinkage())
1215  continue;
1216 
1217  // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1218  // symbol. FIXME is this right for common?
1219  if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1220  GVEntry = &GV;
1221  }
1222  }
1223  }
1224 
1225  std::vector<const GlobalValue*> NonCanonicalGlobals;
1226  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1227  Module &M = *Modules[m];
1228  for (const auto &GV : M.globals()) {
1229  // In the multi-module case, see what this global maps to.
1230  if (!LinkedGlobalsMap.empty()) {
1231  if (const GlobalValue *GVEntry =
1232  LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
1233  // If something else is the canonical global, ignore this one.
1234  if (GVEntry != &GV) {
1235  NonCanonicalGlobals.push_back(&GV);
1236  continue;
1237  }
1238  }
1239  }
1240 
1241  if (!GV.isDeclaration()) {
1242  addGlobalMapping(&GV, getMemoryForGV(&GV));
1243  } else {
1244  // External variable reference. Try to use the dynamic loader to
1245  // get a pointer to it.
1246  if (void *SymAddr =
1248  addGlobalMapping(&GV, SymAddr);
1249  else {
1250  report_fatal_error("Could not resolve external global address: "
1251  +GV.getName());
1252  }
1253  }
1254  }
1255 
1256  // If there are multiple modules, map the non-canonical globals to their
1257  // canonical location.
1258  if (!NonCanonicalGlobals.empty()) {
1259  for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1260  const GlobalValue *GV = NonCanonicalGlobals[i];
1261  const GlobalValue *CGV =
1262  LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1263  void *Ptr = getPointerToGlobalIfAvailable(CGV);
1264  assert(Ptr && "Canonical global wasn't codegen'd!");
1265  addGlobalMapping(GV, Ptr);
1266  }
1267  }
1268 
1269  // Now that all of the globals are set up in memory, loop through them all
1270  // and initialize their contents.
1271  for (const auto &GV : M.globals()) {
1272  if (!GV.isDeclaration()) {
1273  if (!LinkedGlobalsMap.empty()) {
1274  if (const GlobalValue *GVEntry =
1275  LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1276  if (GVEntry != &GV) // Not the canonical variable.
1277  continue;
1278  }
1279  EmitGlobalVariable(&GV);
1280  }
1281  }
1282  }
1283 }
1284 
1285 // EmitGlobalVariable - This method emits the specified global variable to the
1286 // address specified in GlobalAddresses, or allocates new memory if it's not
1287 // already in the map.
1289  void *GA = getPointerToGlobalIfAvailable(GV);
1290 
1291  if (!GA) {
1292  // If it's not already specified, allocate memory for the global.
1293  GA = getMemoryForGV(GV);
1294 
1295  // If we failed to allocate memory for this global, return.
1296  if (!GA) return;
1297 
1298  addGlobalMapping(GV, GA);
1299  }
1300 
1301  // Don't initialize if it's thread local, let the client do it.
1302  if (!GV->isThreadLocal())
1303  InitializeMemory(GV->getInitializer(), GA);
1304 
1305  Type *ElTy = GV->getValueType();
1306  size_t GVSize = (size_t)getDataLayout().getTypeAllocSize(ElTy);
1307  NumInitBytes += (unsigned)GVSize;
1308  ++NumGlobals;
1309 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:761
uint64_t CallInst * C
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
PointerTy PointerVal
Definition: GenericValue.h:31
std::vector< GenericValue > AggregateVal
Definition: GenericValue.h:37
static void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1087
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:836
std::string getMangledName(const GlobalValue *GV)
getMangledName - Get mangled name.
void clearAllGlobalMappings()
clearAllGlobalMappings - Clear all global mappings and start over again, for use in dynamic compilati...
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
Definition: ArrayRef.h:502
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static ExecutionEngine *(* MCJITCtor)(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< LegacyJITSymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
int runFunctionAsMain(Function *Fn, const std::vector< std::string > &argv, const char *const *envp)
runFunctionAsMain - This is a helper function which wraps runFunction to handle the common task of st...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
virtual bool removeModule(Module *M)
removeModule - Removes a Module from the list of modules, but does not free the module&#39;s memory...
2: 32-bit floating point type
Definition: Type.h:58
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1595
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
sys::Mutex lock
lock - This lock protects the ExecutionEngine and MCJIT classes.
uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr)
updateGlobalMapping - Replace an existing mapping for GV with a new address.
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:860
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1524
virtual GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues)=0
runFunction - Execute the specified function with the specified arguments, and return the result...
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:405
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
void EmitGlobalVariable(const GlobalVariable *GV)
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:813
13: Structures
Definition: Type.h:72
STATISTIC(NumFunctions, "Total number of functions")
F(f)
4: 80-bit floating point type (X87)
Definition: Type.h:60
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:689
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:865
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:446
const DataLayout & getDataLayout() const
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:878
15: Pointers
Definition: Type.h:74
double roundToDouble(bool isSigned) const
Converts this APInt to a double value.
Definition: APInt.cpp:756
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1508
iterator_range< global_object_iterator > global_objects()
Definition: Module.h:662
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1079
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4424
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:978
void * PointerTy
Definition: GenericValue.h:21
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:562
Definition: BitVector.h:937
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
static APInt doubleToBits(double V)
Converts a double to APInt bits.
Definition: APInt.h:1730
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:137
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:161
void * getPointerToGlobal(const GlobalValue *GV)
getPointerToGlobal - This returns the address of the specified global value.
void InitializeMemory(const Constant *Init, void *Addr)
Class to represent struct types.
Definition: DerivedTypes.h:233
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:196
EngineBuilder()
Default constructor for EngineBuilder.
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
Definition: DerivedTypes.h:393
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
virtual void addModule(std::unique_ptr< Module > M)
Add a Module to the list of modules that we can JIT from.
All zero aggregate value.
Definition: Constants.h:340
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:273
bool hasExternalLinkage() const
Definition: GlobalValue.h:431
static const bool IsLittleEndianHost
Definition: Host.h:49
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:84
Class to represent function types.
Definition: DerivedTypes.h:103
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:888
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value. ...
Definition: Type.h:243
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:573
virtual char * getMemoryForGV(const GlobalVariable *GV)
getMemoryforGV - Allocate memory for a global variable.
void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, Type *Ty)
FIXME: document.
static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc)
isTargetNullPtr - Return whether the target pointer stored at Loc is null.
EngineBuilder & setSymbolResolver(std::unique_ptr< LegacyJITSymbolResolver > SR)
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:960
GenericValue getConstantValue(const Constant *C)
Converts a Constant* into a GenericValue, including handling of ConstantExpr values.
void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes)
StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst with the integer held in In...
Definition: APInt.cpp:2940
void emitGlobals()
EmitGlobals - Emit all of the global variables to memory, storing their addresses into GlobalAddress...
uint64_t RemoveMapping(StringRef Name)
Erase an entry from the mapping table.
void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, Type *Ty)
StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
Type * getElementType() const
Return the element type of the array/vector.
Definition: Constants.cpp:2423
Value * getOperand(unsigned i) const
Definition: User.h:169
11: Arbitrary bit width integers
Definition: Type.h:70
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:140
bool isFloatTy() const
Return true if this is &#39;float&#39;, a 32-bit IEEE fp type.
Definition: Type.h:146
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:26
#define P(N)
void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes)
LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting from Src into IntVal...
Definition: APInt.cpp:2966
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1617
bool hasName() const
Definition: Value.h:250
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:645
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:223
void erase(iterator I)
Definition: StringMap.h:440
EngineBuilder & setMemoryManager(std::unique_ptr< MCJITMemoryManager > MM)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1856
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
virtual void addObjectFile(std::unique_ptr< object::ObjectFile > O)
addObjectFile - Add an ObjectFile to the execution engine.
float getElementAsFloat(unsigned i) const
If this is an sequential container of floats, return the specified element as a float.
Definition: Constants.cpp:2751
static const Kind Either
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:969
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:164
Constant Vector Declarations.
Definition: Constants.h:499
const Target & getTarget() const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:205
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1436
void clearGlobalMappingsFromModule(Module *M)
clearGlobalMappingsFromModule - Clear all global mappings that came from a particular module...
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:219
static ExecutionEngine *(* InterpCtor)(std::unique_ptr< Module > M, std::string *ErrorStr)
virtual Function * FindFunctionNamed(StringRef FnName)
FindFunctionNamed - Search all of the active modules to find the function that defines FnName...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
unsigned getNumOperands() const
Definition: User.h:191
ExecutionEngine(DataLayout DL)
16: SIMD &#39;packed&#39; format, or other vector type
Definition: Type.h:75
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:129
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:2682
Module.h This file contains the declarations for the Module class.
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition: Type.h:257
Type * getReturnType() const
Definition: DerivedTypes.h:124
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
Definition: DataLayout.h:245
void setVerifyModules(bool Verify)
Enable/Disable IR module verification.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Definition: DataLayout.cpp:834
Class to represent vector types.
Definition: DerivedTypes.h:427
virtual void addArchive(object::OwningBinary< object::Archive > A)
addArchive - Add an Archive to the execution engine.
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
Class for arbitrary precision integers.
Definition: APInt.h:69
ConstantArray - Constant Array Declarations.
Definition: Constants.h:413
static ExecutionEngine *(* OrcMCJITReplacementCtor)(std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< LegacyJITSymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:996
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT&#39;ing from.
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition: Type.h:152
APInt RoundFloatToAPInt(float Float, unsigned width)
Converts a float value into a APInt.
Definition: APInt.h:2165
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:674
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:469
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:951
ExecutionEngine * create()
void addGlobalMapping(const GlobalValue *GV, void *Addr)
addGlobalMapping - Tell the execution engine that the specified global is at the specified location...
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:584
double getElementAsDouble(unsigned i) const
If this is an sequential container of doubles, return the specified element as a double.
Definition: Constants.cpp:2757
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1687
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t getAddressToGlobalIfAvailable(StringRef S)
getAddressToGlobalIfAvailable - This returns the address of the specified global symbol.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Type * getValueType() const
Definition: GlobalValue.h:279
uint32_t Size
Definition: Profile.cpp:46
const GlobalValue * getGlobalValueAtAddress(void *Addr)
getGlobalValueAtAddress - Return the LLVM global value object that starts at the specified address...
Builder class for ExecutionEngines.
EngineBuilder & setMCJITMemoryManager(std::unique_ptr< RTDyldMemoryManager > mcjmm)
setMCJITMemoryManager - Sets the MCJIT memory manager to use.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:227
float bitsToFloat() const
Converts APInt bits to a double.
Definition: APInt.h:1722
3: 64-bit floating point type
Definition: Type.h:59
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunctionCreator LazyFunctionCreator
LazyFunctionCreator - If an unknown function is needed, this function pointer is invoked to create it...
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:114
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
bool hasJIT() const
hasJIT - Check if this targets supports the just-in-time compilation.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:111
Type * getElementType() const
Definition: DerivedTypes.h:394
bool isThreadLocal() const
If the value is "Thread Local", its value isn&#39;t shared by the threads.
Definition: GlobalValue.h:250
iterator_range< global_iterator > globals()
Definition: Module.h:587
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:379
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
APInt bitcastToAPInt() const
Definition: APFloat.h:1104
unsigned getNumElements() const
Return the number of elements in the array or vector.
Definition: Constants.cpp:2446
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
Definition: APInt.cpp:717
#define LLVM_DEBUG(X)
Definition: Debug.h:122
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1244
static APInt floatToBits(float V)
Converts a float to APInt bits.
Definition: APInt.h:1738
bool isDoubleTy() const
Return true if this is &#39;double&#39;, a 64-bit IEEE fp type.
Definition: Type.h:149
double bitsToDouble() const
Converts APInt bits to a double.
Definition: APInt.h:1713
double signedRoundToDouble() const
Converts this signed APInt to a double value.
Definition: APInt.h:1706
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:277
virtual GlobalVariable * FindGlobalVariableNamed(StringRef Name, bool AllowInternal=false)
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:61