LLVM  14.0.0git
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 // FIXME: This file needs to be updated to support scalable vectors
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Operator.h"
30 #include "llvm/IR/ValueHandle.h"
31 #include "llvm/Object/Archive.h"
32 #include "llvm/Object/ObjectFile.h"
33 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/Host.h"
40 #include <cmath>
41 #include <cstring>
42 #include <mutex>
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "jit"
46 
47 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
48 STATISTIC(NumGlobals , "Number of global vars initialized");
49 
50 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
51  std::unique_ptr<Module> M, std::string *ErrorStr,
52  std::shared_ptr<MCJITMemoryManager> MemMgr,
53  std::shared_ptr<LegacyJITSymbolResolver> Resolver,
54  std::unique_ptr<TargetMachine> TM) = nullptr;
55 
56 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
57  std::string *ErrorStr) =nullptr;
58 
59 void JITEventListener::anchor() {}
60 
61 void ObjectCache::anchor() {}
62 
63 void ExecutionEngine::Init(std::unique_ptr<Module> M) {
64  CompilingLazily = false;
65  GVCompilationDisabled = false;
66  SymbolSearchingDisabled = false;
67 
68  // IR module verification is enabled by default in debug builds, and disabled
69  // by default in release builds.
70 #ifndef NDEBUG
71  VerifyModules = true;
72 #else
73  VerifyModules = false;
74 #endif
75 
76  assert(M && "Module is null?");
77  Modules.push_back(std::move(M));
78 }
79 
80 ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
81  : DL(M->getDataLayout()), LazyFunctionCreator(nullptr) {
82  Init(std::move(M));
83 }
84 
85 ExecutionEngine::ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M)
86  : DL(std::move(DL)), LazyFunctionCreator(nullptr) {
87  Init(std::move(M));
88 }
89 
92 }
93 
94 namespace {
95 /// Helper class which uses a value handler to automatically deletes the
96 /// memory block when the GlobalVariable is destroyed.
97 class GVMemoryBlock final : public CallbackVH {
98  GVMemoryBlock(const GlobalVariable *GV)
99  : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
100 
101 public:
102  /// Returns the address the GlobalVariable should be written into. The
103  /// GVMemoryBlock object prefixes that.
104  static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
105  Type *ElTy = GV->getValueType();
106  size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
107  void *RawMemory = ::operator new(
108  alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlign(GV)) + GVSize);
109  new(RawMemory) GVMemoryBlock(GV);
110  return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
111  }
112 
113  void deleted() override {
114  // We allocated with operator new and with some extra memory hanging off the
115  // end, so don't just delete this. I'm not sure if this is actually
116  // required.
117  this->~GVMemoryBlock();
118  ::operator delete(this);
119  }
120 };
121 } // anonymous namespace
122 
124  return GVMemoryBlock::Create(GV, getDataLayout());
125 }
126 
127 void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
128  llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
129 }
130 
131 void
133  llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
134 }
135 
137  llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
138 }
139 
141  for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
142  Module *Found = I->get();
143  if (Found == M) {
144  I->release();
145  Modules.erase(I);
147  return true;
148  }
149  }
150  return false;
151 }
152 
154  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
155  Function *F = Modules[i]->getFunction(FnName);
156  if (F && !F->isDeclaration())
157  return F;
158  }
159  return nullptr;
160 }
161 
163  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
164  GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal);
165  if (GV && !GV->isDeclaration())
166  return GV;
167  }
168  return nullptr;
169 }
170 
172  GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
173  uint64_t OldVal;
174 
175  // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
176  // GlobalAddressMap.
177  if (I == GlobalAddressMap.end())
178  OldVal = 0;
179  else {
180  GlobalAddressReverseMap.erase(I->second);
181  OldVal = I->second;
182  GlobalAddressMap.erase(I);
183  }
184 
185  return OldVal;
186 }
187 
189  assert(GV->hasName() && "Global must have name.");
190 
191  std::lock_guard<sys::Mutex> locked(lock);
192  SmallString<128> FullName;
193 
194  const DataLayout &DL =
196  ? getDataLayout()
197  : GV->getParent()->getDataLayout();
198 
199  Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
200  return std::string(FullName.str());
201 }
202 
204  std::lock_guard<sys::Mutex> locked(lock);
205  addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
206 }
207 
209  std::lock_guard<sys::Mutex> locked(lock);
210 
211  assert(!Name.empty() && "Empty GlobalMapping symbol name!");
212 
213  LLVM_DEBUG(dbgs() << "JIT: Map \'" << Name << "\' to [" << Addr << "]\n";);
214  uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
215  assert((!CurVal || !Addr) && "GlobalMapping already established!");
216  CurVal = Addr;
217 
218  // If we are using the reverse mapping, add it too.
219  if (!EEState.getGlobalAddressReverseMap().empty()) {
220  std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
221  assert((!V.empty() || !Name.empty()) &&
222  "GlobalMapping already established!");
223  V = std::string(Name);
224  }
225 }
226 
228  std::lock_guard<sys::Mutex> locked(lock);
229 
230  EEState.getGlobalAddressMap().clear();
231  EEState.getGlobalAddressReverseMap().clear();
232 }
233 
235  std::lock_guard<sys::Mutex> locked(lock);
236 
237  for (GlobalObject &GO : M->global_objects())
238  EEState.RemoveMapping(getMangledName(&GO));
239 }
240 
242  void *Addr) {
243  std::lock_guard<sys::Mutex> locked(lock);
244  return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
245 }
246 
248  std::lock_guard<sys::Mutex> locked(lock);
249 
251  EEState.getGlobalAddressMap();
252 
253  // Deleting from the mapping?
254  if (!Addr)
255  return EEState.RemoveMapping(Name);
256 
257  uint64_t &CurVal = Map[Name];
258  uint64_t OldVal = CurVal;
259 
260  if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
261  EEState.getGlobalAddressReverseMap().erase(CurVal);
262  CurVal = Addr;
263 
264  // If we are using the reverse mapping, add it too.
265  if (!EEState.getGlobalAddressReverseMap().empty()) {
266  std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
267  assert((!V.empty() || !Name.empty()) &&
268  "GlobalMapping already established!");
269  V = std::string(Name);
270  }
271  return OldVal;
272 }
273 
275  std::lock_guard<sys::Mutex> locked(lock);
276  uint64_t Address = 0;
278  EEState.getGlobalAddressMap().find(S);
279  if (I != EEState.getGlobalAddressMap().end())
280  Address = I->second;
281  return Address;
282 }
283 
284 
286  std::lock_guard<sys::Mutex> locked(lock);
287  if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
288  return Address;
289  return nullptr;
290 }
291 
293  std::lock_guard<sys::Mutex> locked(lock);
295 }
296 
298  std::lock_guard<sys::Mutex> locked(lock);
299 
300  // If we haven't computed the reverse mapping yet, do so first.
301  if (EEState.getGlobalAddressReverseMap().empty()) {
303  I = EEState.getGlobalAddressMap().begin(),
304  E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
305  StringRef Name = I->first();
306  uint64_t Addr = I->second;
307  EEState.getGlobalAddressReverseMap().insert(
308  std::make_pair(Addr, std::string(Name)));
309  }
310  }
311 
312  std::map<uint64_t, std::string>::iterator I =
313  EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
314 
315  if (I != EEState.getGlobalAddressReverseMap().end()) {
316  StringRef Name = I->second;
317  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
318  if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
319  return GV;
320  }
321  return nullptr;
322 }
323 
324 namespace {
325 class ArgvArray {
326  std::unique_ptr<char[]> Array;
327  std::vector<std::unique_ptr<char[]>> Values;
328 public:
329  /// Turn a vector of strings into a nice argv style array of pointers to null
330  /// terminated strings.
331  void *reset(LLVMContext &C, ExecutionEngine *EE,
332  const std::vector<std::string> &InputArgv);
333 };
334 } // anonymous namespace
335 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
336  const std::vector<std::string> &InputArgv) {
337  Values.clear(); // Free the old contents.
338  Values.reserve(InputArgv.size());
339  unsigned PtrSize = EE->getDataLayout().getPointerSize();
340  Array = std::make_unique<char[]>((InputArgv.size()+1)*PtrSize);
341 
342  LLVM_DEBUG(dbgs() << "JIT: ARGV = " << (void *)Array.get() << "\n");
343  Type *SBytePtr = Type::getInt8PtrTy(C);
344 
345  for (unsigned i = 0; i != InputArgv.size(); ++i) {
346  unsigned Size = InputArgv[i].size()+1;
347  auto Dest = std::make_unique<char[]>(Size);
348  LLVM_DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void *)Dest.get()
349  << "\n");
350 
351  std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
352  Dest[Size-1] = 0;
353 
354  // Endian safe: Array[i] = (PointerTy)Dest;
355  EE->StoreValueToMemory(PTOGV(Dest.get()),
356  (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
357  Values.push_back(std::move(Dest));
358  }
359 
360  // Null terminate it
361  EE->StoreValueToMemory(PTOGV(nullptr),
362  (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
363  SBytePtr);
364  return Array.get();
365 }
366 
368  bool isDtors) {
369  StringRef Name(isDtors ? "llvm.global_dtors" : "llvm.global_ctors");
370  GlobalVariable *GV = module.getNamedGlobal(Name);
371 
372  // If this global has internal linkage, or if it has a use, then it must be
373  // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
374  // this is the case, don't execute any of the global ctors, __main will do
375  // it.
376  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
377 
378  // Should be an array of '{ i32, void ()* }' structs. The first value is
379  // the init priority, which we ignore.
380  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
381  if (!InitList)
382  return;
383  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
384  ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
385  if (!CS) continue;
386 
387  Constant *FP = CS->getOperand(1);
388  if (FP->isNullValue())
389  continue; // Found a sentinal value, ignore.
390 
391  // Strip off constant expression casts.
392  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
393  if (CE->isCast())
394  FP = CE->getOperand(0);
395 
396  // Execute the ctor/dtor function!
397  if (Function *F = dyn_cast<Function>(FP))
398  runFunction(F, None);
399 
400  // FIXME: It is marginally lame that we just do nothing here if we see an
401  // entry we don't recognize. It might not be unreasonable for the verifier
402  // to not even allow this and just assert here.
403  }
404 }
405 
407  // Execute global ctors/dtors for each module in the program.
408  for (std::unique_ptr<Module> &M : Modules)
410 }
411 
412 #ifndef NDEBUG
413 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
414 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
415  unsigned PtrSize = EE->getDataLayout().getPointerSize();
416  for (unsigned i = 0; i < PtrSize; ++i)
417  if (*(i + (uint8_t*)Loc))
418  return false;
419  return true;
420 }
421 #endif
422 
424  const std::vector<std::string> &argv,
425  const char * const * envp) {
426  std::vector<GenericValue> GVArgs;
427  GenericValue GVArgc;
428  GVArgc.IntVal = APInt(32, argv.size());
429 
430  // Check main() type
431  unsigned NumArgs = Fn->getFunctionType()->getNumParams();
432  FunctionType *FTy = Fn->getFunctionType();
433  Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
434 
435  // Check the argument types.
436  if (NumArgs > 3)
437  report_fatal_error("Invalid number of arguments of main() supplied");
438  if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
439  report_fatal_error("Invalid type for third argument of main() supplied");
440  if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
441  report_fatal_error("Invalid type for second argument of main() supplied");
442  if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
443  report_fatal_error("Invalid type for first argument of main() supplied");
444  if (!FTy->getReturnType()->isIntegerTy() &&
445  !FTy->getReturnType()->isVoidTy())
446  report_fatal_error("Invalid return type of main() supplied");
447 
448  ArgvArray CArgv;
449  ArgvArray CEnv;
450  if (NumArgs) {
451  GVArgs.push_back(GVArgc); // Arg #0 = argc.
452  if (NumArgs > 1) {
453  // Arg #1 = argv.
454  GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
455  assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
456  "argv[0] was null after CreateArgv");
457  if (NumArgs > 2) {
458  std::vector<std::string> EnvVars;
459  for (unsigned i = 0; envp[i]; ++i)
460  EnvVars.emplace_back(envp[i]);
461  // Arg #2 = envp.
462  GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
463  }
464  }
465  }
466 
467  return runFunction(Fn, GVArgs).IntVal.getZExtValue();
468 }
469 
471 
472 EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
473  : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
474  OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr) {
475 // IR module verification is enabled by default in debug builds, and disabled
476 // by default in release builds.
477 #ifndef NDEBUG
478  VerifyModules = true;
479 #else
480  VerifyModules = false;
481 #endif
482 }
483 
485 
487  std::unique_ptr<RTDyldMemoryManager> mcjmm) {
488  auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
489  MemMgr = SharedMM;
490  Resolver = SharedMM;
491  return *this;
492 }
493 
495 EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
496  MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
497  return *this;
498 }
499 
501 EngineBuilder::setSymbolResolver(std::unique_ptr<LegacyJITSymbolResolver> SR) {
502  Resolver = std::shared_ptr<LegacyJITSymbolResolver>(std::move(SR));
503  return *this;
504 }
505 
507  std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
508 
509  // Make sure we can resolve symbols in the program as well. The zero arg
510  // to the function tells DynamicLibrary to load the program, not a library.
511  if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
512  return nullptr;
513 
514  // If the user specified a memory manager but didn't specify which engine to
515  // create, we assume they only want the JIT, and we fail if they only want
516  // the interpreter.
517  if (MemMgr) {
518  if (WhichEngine & EngineKind::JIT)
519  WhichEngine = EngineKind::JIT;
520  else {
521  if (ErrorStr)
522  *ErrorStr = "Cannot create an interpreter with a memory manager.";
523  return nullptr;
524  }
525  }
526 
527  // Unless the interpreter was explicitly selected or the JIT is not linked,
528  // try making a JIT.
529  if ((WhichEngine & EngineKind::JIT) && TheTM) {
530  if (!TM->getTarget().hasJIT()) {
531  errs() << "WARNING: This target JIT is not designed for the host"
532  << " you are running. If bad things happen, please choose"
533  << " a different -march switch.\n";
534  }
535 
536  ExecutionEngine *EE = nullptr;
538  EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
539  std::move(Resolver), std::move(TheTM));
540 
541  if (EE) {
542  EE->setVerifyModules(VerifyModules);
543  return EE;
544  }
545  }
546 
547  // If we can't make a JIT and we didn't request one specifically, try making
548  // an interpreter instead.
549  if (WhichEngine & EngineKind::Interpreter) {
551  return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
552  if (ErrorStr)
553  *ErrorStr = "Interpreter has not been linked in.";
554  return nullptr;
555  }
556 
557  if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
558  if (ErrorStr)
559  *ErrorStr = "JIT has not been linked in.";
560  }
561 
562  return nullptr;
563 }
564 
566  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
567  return getPointerToFunction(F);
568 
569  std::lock_guard<sys::Mutex> locked(lock);
570  if (void* P = getPointerToGlobalIfAvailable(GV))
571  return P;
572 
573  // Global variable might have been added since interpreter started.
574  if (GlobalVariable *GVar =
575  const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
576  emitGlobalVariable(GVar);
577  else
578  llvm_unreachable("Global hasn't had an address allocated yet!");
579 
581 }
582 
583 /// Converts a Constant* into a GenericValue, including handling of
584 /// ConstantExpr values.
586  // If its undefined, return the garbage.
587  if (isa<UndefValue>(C)) {
588  GenericValue Result;
589  switch (C->getType()->getTypeID()) {
590  default:
591  break;
592  case Type::IntegerTyID:
593  case Type::X86_FP80TyID:
594  case Type::FP128TyID:
595  case Type::PPC_FP128TyID:
596  // Although the value is undefined, we still have to construct an APInt
597  // with the correct bit width.
598  Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
599  break;
600  case Type::StructTyID: {
601  // if the whole struct is 'undef' just reserve memory for the value.
602  if(StructType *STy = dyn_cast<StructType>(C->getType())) {
603  unsigned int elemNum = STy->getNumElements();
604  Result.AggregateVal.resize(elemNum);
605  for (unsigned int i = 0; i < elemNum; ++i) {
606  Type *ElemTy = STy->getElementType(i);
607  if (ElemTy->isIntegerTy())
608  Result.AggregateVal[i].IntVal =
609  APInt(ElemTy->getPrimitiveSizeInBits(), 0);
610  else if (ElemTy->isAggregateType()) {
611  const Constant *ElemUndef = UndefValue::get(ElemTy);
612  Result.AggregateVal[i] = getConstantValue(ElemUndef);
613  }
614  }
615  }
616  }
617  break;
620  "Scalable vector support not yet implemented in ExecutionEngine");
622  // if the whole vector is 'undef' just reserve memory for the value.
623  auto *VTy = cast<FixedVectorType>(C->getType());
624  Type *ElemTy = VTy->getElementType();
625  unsigned int elemNum = VTy->getNumElements();
626  Result.AggregateVal.resize(elemNum);
627  if (ElemTy->isIntegerTy())
628  for (unsigned int i = 0; i < elemNum; ++i)
629  Result.AggregateVal[i].IntVal =
630  APInt(ElemTy->getPrimitiveSizeInBits(), 0);
631  break;
632  }
633  return Result;
634  }
635 
636  // Otherwise, if the value is a ConstantExpr...
637  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
638  Constant *Op0 = CE->getOperand(0);
639  switch (CE->getOpcode()) {
640  case Instruction::GetElementPtr: {
641  // Compute the index
642  GenericValue Result = getConstantValue(Op0);
644  cast<GEPOperator>(CE)->accumulateConstantOffset(DL, Offset);
645 
646  char* tmp = (char*) Result.PointerVal;
647  Result = PTOGV(tmp + Offset.getSExtValue());
648  return Result;
649  }
650  case Instruction::Trunc: {
651  GenericValue GV = getConstantValue(Op0);
652  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
653  GV.IntVal = GV.IntVal.trunc(BitWidth);
654  return GV;
655  }
656  case Instruction::ZExt: {
657  GenericValue GV = getConstantValue(Op0);
658  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
659  GV.IntVal = GV.IntVal.zext(BitWidth);
660  return GV;
661  }
662  case Instruction::SExt: {
663  GenericValue GV = getConstantValue(Op0);
664  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
665  GV.IntVal = GV.IntVal.sext(BitWidth);
666  return GV;
667  }
668  case Instruction::FPTrunc: {
669  // FIXME long double
670  GenericValue GV = getConstantValue(Op0);
671  GV.FloatVal = float(GV.DoubleVal);
672  return GV;
673  }
674  case Instruction::FPExt:{
675  // FIXME long double
676  GenericValue GV = getConstantValue(Op0);
677  GV.DoubleVal = double(GV.FloatVal);
678  return GV;
679  }
680  case Instruction::UIToFP: {
681  GenericValue GV = getConstantValue(Op0);
682  if (CE->getType()->isFloatTy())
683  GV.FloatVal = float(GV.IntVal.roundToDouble());
684  else if (CE->getType()->isDoubleTy())
685  GV.DoubleVal = GV.IntVal.roundToDouble();
686  else if (CE->getType()->isX86_FP80Ty()) {
688  (void)apf.convertFromAPInt(GV.IntVal,
689  false,
691  GV.IntVal = apf.bitcastToAPInt();
692  }
693  return GV;
694  }
695  case Instruction::SIToFP: {
696  GenericValue GV = getConstantValue(Op0);
697  if (CE->getType()->isFloatTy())
698  GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
699  else if (CE->getType()->isDoubleTy())
701  else if (CE->getType()->isX86_FP80Ty()) {
703  (void)apf.convertFromAPInt(GV.IntVal,
704  true,
706  GV.IntVal = apf.bitcastToAPInt();
707  }
708  return GV;
709  }
710  case Instruction::FPToUI: // double->APInt conversion handles sign
711  case Instruction::FPToSI: {
712  GenericValue GV = getConstantValue(Op0);
713  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
714  if (Op0->getType()->isFloatTy())
716  else if (Op0->getType()->isDoubleTy())
718  else if (Op0->getType()->isX86_FP80Ty()) {
720  uint64_t v;
721  bool ignored;
723  CE->getOpcode()==Instruction::FPToSI,
724  APFloat::rmTowardZero, &ignored);
725  GV.IntVal = v; // endian?
726  }
727  return GV;
728  }
729  case Instruction::PtrToInt: {
730  GenericValue GV = getConstantValue(Op0);
731  uint32_t PtrWidth = DL.getTypeSizeInBits(Op0->getType());
732  assert(PtrWidth <= 64 && "Bad pointer width");
733  GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
734  uint32_t IntWidth = DL.getTypeSizeInBits(CE->getType());
735  GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
736  return GV;
737  }
738  case Instruction::IntToPtr: {
739  GenericValue GV = getConstantValue(Op0);
740  uint32_t PtrWidth = DL.getTypeSizeInBits(CE->getType());
741  GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
742  assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
743  GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
744  return GV;
745  }
746  case Instruction::BitCast: {
747  GenericValue GV = getConstantValue(Op0);
748  Type* DestTy = CE->getType();
749  switch (Op0->getType()->getTypeID()) {
750  default: llvm_unreachable("Invalid bitcast operand");
751  case Type::IntegerTyID:
752  assert(DestTy->isFloatingPointTy() && "invalid bitcast");
753  if (DestTy->isFloatTy())
754  GV.FloatVal = GV.IntVal.bitsToFloat();
755  else if (DestTy->isDoubleTy())
756  GV.DoubleVal = GV.IntVal.bitsToDouble();
757  break;
758  case Type::FloatTyID:
759  assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
761  break;
762  case Type::DoubleTyID:
763  assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
765  break;
766  case Type::PointerTyID:
767  assert(DestTy->isPointerTy() && "Invalid bitcast");
768  break; // getConstantValue(Op0) above already converted it
769  }
770  return GV;
771  }
772  case Instruction::Add:
773  case Instruction::FAdd:
774  case Instruction::Sub:
775  case Instruction::FSub:
776  case Instruction::Mul:
777  case Instruction::FMul:
778  case Instruction::UDiv:
779  case Instruction::SDiv:
780  case Instruction::URem:
781  case Instruction::SRem:
782  case Instruction::And:
783  case Instruction::Or:
784  case Instruction::Xor: {
785  GenericValue LHS = getConstantValue(Op0);
786  GenericValue RHS = getConstantValue(CE->getOperand(1));
787  GenericValue GV;
788  switch (CE->getOperand(0)->getType()->getTypeID()) {
789  default: llvm_unreachable("Bad add type!");
790  case Type::IntegerTyID:
791  switch (CE->getOpcode()) {
792  default: llvm_unreachable("Invalid integer opcode");
793  case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
794  case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
795  case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
796  case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
797  case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
798  case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
799  case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
800  case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
801  case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
802  case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
803  }
804  break;
805  case Type::FloatTyID:
806  switch (CE->getOpcode()) {
807  default: llvm_unreachable("Invalid float opcode");
808  case Instruction::FAdd:
809  GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
810  case Instruction::FSub:
811  GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
812  case Instruction::FMul:
813  GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
814  case Instruction::FDiv:
815  GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
816  case Instruction::FRem:
817  GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
818  }
819  break;
820  case Type::DoubleTyID:
821  switch (CE->getOpcode()) {
822  default: llvm_unreachable("Invalid double opcode");
823  case Instruction::FAdd:
824  GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
825  case Instruction::FSub:
826  GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
827  case Instruction::FMul:
828  GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
829  case Instruction::FDiv:
830  GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
831  case Instruction::FRem:
832  GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
833  }
834  break;
835  case Type::X86_FP80TyID:
836  case Type::PPC_FP128TyID:
837  case Type::FP128TyID: {
838  const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
839  APFloat apfLHS = APFloat(Sem, LHS.IntVal);
840  switch (CE->getOpcode()) {
841  default: llvm_unreachable("Invalid long double opcode");
842  case Instruction::FAdd:
843  apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
844  GV.IntVal = apfLHS.bitcastToAPInt();
845  break;
846  case Instruction::FSub:
847  apfLHS.subtract(APFloat(Sem, RHS.IntVal),
849  GV.IntVal = apfLHS.bitcastToAPInt();
850  break;
851  case Instruction::FMul:
852  apfLHS.multiply(APFloat(Sem, RHS.IntVal),
854  GV.IntVal = apfLHS.bitcastToAPInt();
855  break;
856  case Instruction::FDiv:
857  apfLHS.divide(APFloat(Sem, RHS.IntVal),
859  GV.IntVal = apfLHS.bitcastToAPInt();
860  break;
861  case Instruction::FRem:
862  apfLHS.mod(APFloat(Sem, RHS.IntVal));
863  GV.IntVal = apfLHS.bitcastToAPInt();
864  break;
865  }
866  }
867  break;
868  }
869  return GV;
870  }
871  default:
872  break;
873  }
874 
875  SmallString<256> Msg;
876  raw_svector_ostream OS(Msg);
877  OS << "ConstantExpr not handled: " << *CE;
878  report_fatal_error(OS.str());
879  }
880 
881  // Otherwise, we have a simple constant.
882  GenericValue Result;
883  switch (C->getType()->getTypeID()) {
884  case Type::FloatTyID:
885  Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
886  break;
887  case Type::DoubleTyID:
888  Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
889  break;
890  case Type::X86_FP80TyID:
891  case Type::FP128TyID:
892  case Type::PPC_FP128TyID:
893  Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
894  break;
895  case Type::IntegerTyID:
896  Result.IntVal = cast<ConstantInt>(C)->getValue();
897  break;
898  case Type::PointerTyID:
899  while (auto *A = dyn_cast<GlobalAlias>(C)) {
900  C = A->getAliasee();
901  }
902  if (isa<ConstantPointerNull>(C))
903  Result.PointerVal = nullptr;
904  else if (const Function *F = dyn_cast<Function>(C))
905  Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
906  else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
907  Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
908  else
909  llvm_unreachable("Unknown constant pointer type!");
910  break;
913  "Scalable vector support not yet implemented in ExecutionEngine");
914  case Type::FixedVectorTyID: {
915  unsigned elemNum;
916  Type* ElemTy;
917  const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
918  const ConstantVector *CV = dyn_cast<ConstantVector>(C);
919  const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
920 
921  if (CDV) {
922  elemNum = CDV->getNumElements();
923  ElemTy = CDV->getElementType();
924  } else if (CV || CAZ) {
925  auto *VTy = cast<FixedVectorType>(C->getType());
926  elemNum = VTy->getNumElements();
927  ElemTy = VTy->getElementType();
928  } else {
929  llvm_unreachable("Unknown constant vector type!");
930  }
931 
932  Result.AggregateVal.resize(elemNum);
933  // Check if vector holds floats.
934  if(ElemTy->isFloatTy()) {
935  if (CAZ) {
936  GenericValue floatZero;
937  floatZero.FloatVal = 0.f;
938  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
939  floatZero);
940  break;
941  }
942  if(CV) {
943  for (unsigned i = 0; i < elemNum; ++i)
944  if (!isa<UndefValue>(CV->getOperand(i)))
945  Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
946  CV->getOperand(i))->getValueAPF().convertToFloat();
947  break;
948  }
949  if(CDV)
950  for (unsigned i = 0; i < elemNum; ++i)
951  Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
952 
953  break;
954  }
955  // Check if vector holds doubles.
956  if (ElemTy->isDoubleTy()) {
957  if (CAZ) {
958  GenericValue doubleZero;
959  doubleZero.DoubleVal = 0.0;
960  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
961  doubleZero);
962  break;
963  }
964  if(CV) {
965  for (unsigned i = 0; i < elemNum; ++i)
966  if (!isa<UndefValue>(CV->getOperand(i)))
967  Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
968  CV->getOperand(i))->getValueAPF().convertToDouble();
969  break;
970  }
971  if(CDV)
972  for (unsigned i = 0; i < elemNum; ++i)
973  Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
974 
975  break;
976  }
977  // Check if vector holds integers.
978  if (ElemTy->isIntegerTy()) {
979  if (CAZ) {
980  GenericValue intZero;
981  intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
982  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
983  intZero);
984  break;
985  }
986  if(CV) {
987  for (unsigned i = 0; i < elemNum; ++i)
988  if (!isa<UndefValue>(CV->getOperand(i)))
989  Result.AggregateVal[i].IntVal = cast<ConstantInt>(
990  CV->getOperand(i))->getValue();
991  else {
992  Result.AggregateVal[i].IntVal =
994  }
995  break;
996  }
997  if(CDV)
998  for (unsigned i = 0; i < elemNum; ++i)
999  Result.AggregateVal[i].IntVal = APInt(
1001  CDV->getElementAsInteger(i));
1002 
1003  break;
1004  }
1005  llvm_unreachable("Unknown constant pointer type!");
1006  } break;
1007 
1008  default:
1009  SmallString<256> Msg;
1010  raw_svector_ostream OS(Msg);
1011  OS << "ERROR: Constant unimplemented for type: " << *C->getType();
1012  report_fatal_error(OS.str());
1013  }
1014 
1015  return Result;
1016 }
1017 
1019  GenericValue *Ptr, Type *Ty) {
1020  const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);
1021 
1022  switch (Ty->getTypeID()) {
1023  default:
1024  dbgs() << "Cannot store value of type " << *Ty << "!\n";
1025  break;
1026  case Type::IntegerTyID:
1027  StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
1028  break;
1029  case Type::FloatTyID:
1030  *((float*)Ptr) = Val.FloatVal;
1031  break;
1032  case Type::DoubleTyID:
1033  *((double*)Ptr) = Val.DoubleVal;
1034  break;
1035  case Type::X86_FP80TyID:
1036  memcpy(Ptr, Val.IntVal.getRawData(), 10);
1037  break;
1038  case Type::PointerTyID:
1039  // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1040  if (StoreBytes != sizeof(PointerTy))
1041  memset(&(Ptr->PointerVal), 0, StoreBytes);
1042 
1043  *((PointerTy*)Ptr) = Val.PointerVal;
1044  break;
1045  case Type::FixedVectorTyID:
1047  for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1048  if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1049  *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1050  if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1051  *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1052  if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1053  unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1054  StoreIntToMemory(Val.AggregateVal[i].IntVal,
1055  (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1056  }
1057  }
1058  break;
1059  }
1060 
1061  if (sys::IsLittleEndianHost != getDataLayout().isLittleEndian())
1062  // Host and target are different endian - reverse the stored bytes.
1063  std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1064 }
1065 
1066 /// FIXME: document
1067 ///
1069  GenericValue *Ptr,
1070  Type *Ty) {
1071  const unsigned LoadBytes = getDataLayout().getTypeStoreSize(Ty);
1072 
1073  switch (Ty->getTypeID()) {
1074  case Type::IntegerTyID:
1075  // An APInt with all words initially zero.
1076  Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1077  LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1078  break;
1079  case Type::FloatTyID:
1080  Result.FloatVal = *((float*)Ptr);
1081  break;
1082  case Type::DoubleTyID:
1083  Result.DoubleVal = *((double*)Ptr);
1084  break;
1085  case Type::PointerTyID:
1086  Result.PointerVal = *((PointerTy*)Ptr);
1087  break;
1088  case Type::X86_FP80TyID: {
1089  // This is endian dependent, but it will only work on x86 anyway.
1090  // FIXME: Will not trap if loading a signaling NaN.
1091  uint64_t y[2];
1092  memcpy(y, Ptr, 10);
1093  Result.IntVal = APInt(80, y);
1094  break;
1095  }
1098  "Scalable vector support not yet implemented in ExecutionEngine");
1099  case Type::FixedVectorTyID: {
1100  auto *VT = cast<FixedVectorType>(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()) {
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 = LinkedGlobalsMap[std::make_pair(
1204  std::string(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 = LinkedGlobalsMap[std::make_pair(
1232  std::string(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.
1247  std::string(GV.getName())))
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 = LinkedGlobalsMap[std::make_pair(
1262  std::string(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 = LinkedGlobalsMap[std::make_pair(
1275  std::string(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 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
llvm::lltok::APFloat
@ APFloat
Definition: LLToken.h:491
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
CmpMode::FP
@ FP
llvm::Type::FloatTyID
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
llvm::ExecutionEngine::getMangledName
std::string getMangledName(const GlobalValue *GV)
getMangledName - Get mangled name.
Definition: ExecutionEngine.cpp:188
llvm::Type::DoubleTyID
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::Module::getNamedGlobal
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:426
llvm::ExecutionEngine::setVerifyModules
void setVerifyModules(bool Verify)
Enable/Disable IR module verification.
Definition: ExecutionEngine.h:490
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::GenericValue::AggregateVal
std::vector< GenericValue > AggregateVal
Definition: GenericValue.h:37
llvm::ExecutionEngine::getPointerToFunctionOrStub
virtual void * getPointerToFunctionOrStub(Function *F)
getPointerToFunctionOrStub - If the specified function has been code-gen'd, return a pointer to the f...
Definition: ExecutionEngine.h:368
llvm::ExecutionEngine::~ExecutionEngine
virtual ~ExecutionEngine()
Definition: ExecutionEngine.cpp:90
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::Type::getInt8PtrTy
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:255
llvm::DataLayout::getPreferredAlign
Align getPreferredAlign(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Definition: DataLayout.cpp:901
llvm::GlobalValue::hasExternalLinkage
bool hasExternalLinkage() const
Definition: GlobalValue.h:431
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:228
llvm::APFloat::add
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:972
llvm::EngineBuilder::create
ExecutionEngine * create()
Definition: ExecutionEngine.h:658
llvm::ExecutionEngine::updateGlobalMapping
uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr)
updateGlobalMapping - Replace an existing mapping for GV with a new address.
Definition: ExecutionEngine.cpp:241
llvm::Function
Definition: Function.h:61
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
ExecutionEngine.h
llvm::DataLayout::getTypeSizeInBits
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:655
double
into xmm2 addss xmm2 xmm1 xmm3 addss xmm3 movaps xmm0 unpcklps xmm0 ret seems silly when it could just be one addps Expand libm rounding functions main should enable SSE DAZ mode and other fast SSE modes Think about doing i64 math in SSE regs on x86 This testcase should have no SSE instructions in and only one load from a constant double
Definition: README-SSE.txt:85
llvm::EngineBuilder::setMCJITMemoryManager
EngineBuilder & setMCJITMemoryManager(std::unique_ptr< RTDyldMemoryManager > mcjmm)
setMCJITMemoryManager - Sets the MCJIT memory manager to use.
Definition: ExecutionEngine.cpp:486
Host.h
Statistic.h
llvm::GenericValue::PointerVal
PointerTy PointerVal
Definition: GenericValue.h:31
llvm::DataLayout::getTypeStoreSize
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
Definition: DataLayout.h:466
llvm::ConstantDataSequential::getElementAsInteger
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:3178
llvm::Value::hasName
bool hasName() const
Definition: Value.h:262
ErrorHandling.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::APInt::zextOrTrunc
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:952
llvm::ExecutionEngine::clearGlobalMappingsFromModule
void clearGlobalMappingsFromModule(Module *M)
clearGlobalMappingsFromModule - Clear all global mappings that came from a particular module,...
Definition: ExecutionEngine.cpp:234
llvm::ExecutionEngine::runFunctionAsMain
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...
Definition: ExecutionEngine.cpp:423
llvm::Type::getTypeID
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:135
llvm::APFloatBase::x87DoubleExtended
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:179
llvm::DataLayout::getStructLayout
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:671
llvm::ExecutionEngine::clearAllGlobalMappings
void clearAllGlobalMappings()
clearAllGlobalMappings - Clear all global mappings and start over again, for use in dynamic compilati...
Definition: ExecutionEngine.cpp:227
llvm::ExecutionEngine::emitGlobalVariable
void emitGlobalVariable(const GlobalVariable *GV)
Definition: ExecutionEngine.cpp:1288
llvm::APFloat::divide
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:999
llvm::ConstantDataSequential::getElementAsFloat
float getElementAsFloat(unsigned i) const
If this is an sequential container of floats, return the specified element as a float.
Definition: Constants.cpp:3251
DynamicLibrary.h
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:321
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::APInt::getBitWidth
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1581
Module.h
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:329
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
llvm::ConstantAggregateZero
All zero aggregate value.
Definition: Constants.h:336
llvm::EngineBuilder
Builder class for ExecutionEngines.
Definition: ExecutionEngine.h:535
llvm::Type::isX86_FP80Ty
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition: Type.h:153
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::msgpack::Type::Array
@ Array
Operator.h
getBitWidth
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Definition: ValueTracking.cpp:89
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:138
llvm::APInt::roundToDouble
double roundToDouble(bool isSigned) const
Converts this APInt to a double value.
Definition: APInt.cpp:805
STLExtras.h
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
tmp
alloca< 16 x float >, align 16 %tmp2=alloca< 16 x float >, align 16 store< 16 x float > %A,< 16 x float > *%tmp %s=bitcast< 16 x float > *%tmp to i8 *%s2=bitcast< 16 x float > *%tmp2 to i8 *call void @llvm.memcpy.i64(i8 *%s, i8 *%s2, i64 64, i32 16) %R=load< 16 x float > *%tmp2 ret< 16 x float > %R } declare void @llvm.memcpy.i64(i8 *nocapture, i8 *nocapture, i64, i32) nounwind which compiles to:_foo:subl $140, %esp movaps %xmm3, 112(%esp) movaps %xmm2, 96(%esp) movaps %xmm1, 80(%esp) movaps %xmm0, 64(%esp) movl 60(%esp), %eax movl %eax, 124(%esp) movl 56(%esp), %eax movl %eax, 120(%esp) movl 52(%esp), %eax< many many more 32-bit copies > movaps(%esp), %xmm0 movaps 16(%esp), %xmm1 movaps 32(%esp), %xmm2 movaps 48(%esp), %xmm3 addl $140, %esp ret On Nehalem, it may even be cheaper to just use movups when unaligned than to fall back to lower-granularity chunks. Implement processor-specific optimizations for parity with GCC on these processors. GCC does two optimizations:1. ix86_pad_returns inserts a noop before ret instructions if immediately preceded by a conditional branch or is the target of a jump. 2. ix86_avoid_jump_misspredicts inserts noops in cases where a 16-byte block of code contains more than 3 branches. The first one is done for all AMDs, Core2, and "Generic" The second one is done for:Atom, Pentium Pro, all AMDs, Pentium 4, Nocona, Core 2, and "Generic" Testcase:int x(int a) { return(a &0xf0)> >4 tmp
Definition: README.txt:1347
llvm::Type::isFloatingPointTy
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
llvm::APFloat::mod
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1017
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::GlobalValue::hasExternalWeakLinkage
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:446
llvm::ExecutionEngine::Modules
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
Definition: ExecutionEngine.h:134
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::ExecutionEngine::MCJITCtor
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)
Definition: ExecutionEngine.h:139
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::ExecutionEngine::StoreValueToMemory
void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, Type *Ty)
StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
Definition: ExecutionEngine.cpp:1018
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2000
llvm::ExecutionEngine::lock
sys::Mutex lock
lock - This lock protects the ExecutionEngine and MCJIT classes.
Definition: ExecutionEngine.h:161
llvm::ExecutionEngine::LoadValueFromMemory
void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, Type *Ty)
FIXME: document.
Definition: ExecutionEngine.cpp:1068
llvm::APFloat::getZero
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:885
llvm::GenericValue
Definition: GenericValue.h:23
ObjectCache.h
llvm::ExecutionEngine::addObjectFile
virtual void addObjectFile(std::unique_ptr< object::ObjectFile > O)
addObjectFile - Add an ObjectFile to the execution engine.
Definition: ExecutionEngine.cpp:127
llvm::GenericValue::IntVal
APInt IntVal
Definition: GenericValue.h:35
llvm::APFloat::convertToInteger
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1107
llvm::ConstantDataSequential
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:569
TargetMachine.h
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
llvm::ExecutionEngine::getAddressToGlobalIfAvailable
uint64_t getAddressToGlobalIfAvailable(StringRef S)
getAddressToGlobalIfAvailable - This returns the address of the specified global symbol.
Definition: ExecutionEngine.cpp:274
llvm::GlobalValue::hasAppendingLinkage
bool hasAppendingLinkage() const
Definition: GlobalValue.h:442
Constants.h
SmallString.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ExecutionEngine::getOrEmitGlobalVariable
virtual void * getOrEmitGlobalVariable(const GlobalVariable *GV)
getOrEmitGlobalVariable - Return the address of the specified global variable, possibly emitting it t...
Definition: ExecutionEngine.h:411
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::GlobalObject
Definition: GlobalObject.h:28
llvm::APInt::signedRoundToDouble
double signedRoundToDouble() const
Converts this signed APInt to a double value.
Definition: APInt.h:1770
llvm::ExecutionEngine::FindGlobalVariableNamed
virtual GlobalVariable * FindGlobalVariableNamed(StringRef Name, bool AllowInternal=false)
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
Definition: ExecutionEngine.cpp:162
llvm::GVTOP
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
llvm::Init::dump
void dump() const
Debugging method that may be called through a debugger; just invokes print on stderr.
Definition: Record.cpp:283
llvm::Mangler::getNameWithPrefix
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:114
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition: Type.cpp:153
llvm::ExecutionEngine::FindFunctionNamed
virtual Function * FindFunctionNamed(StringRef FnName)
FindFunctionNamed - Search all of the active modules to find the function that defines FnName.
Definition: ExecutionEngine.cpp:153
llvm::ExecutionEngine::InterpCtor
static ExecutionEngine *(* InterpCtor)(std::unique_ptr< Module > M, std::string *ErrorStr)
Definition: ExecutionEngine.h:145
llvm::StringMap::clear
void clear()
Definition: StringMap.h:333
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1631
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::ExecutionEngineState::RemoveMapping
uint64_t RemoveMapping(StringRef Name)
Erase an entry from the mapping table.
Definition: ExecutionEngine.cpp:171
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1784
llvm::ExecutionEngine::getConstantValue
GenericValue getConstantValue(const Constant *C)
Converts a Constant* into a GenericValue, including handling of ConstantExpr values.
Definition: ExecutionEngine.cpp:585
GenericValue.h
llvm::Type::PointerTyID
@ PointerTyID
Pointers.
Definition: Type.h:73
llvm::EngineBuilder::setMemoryManager
EngineBuilder & setMemoryManager(std::unique_ptr< MCJITMemoryManager > MM)
Definition: ExecutionEngine.cpp:495
llvm::APFloat::bitcastToAPInt
APInt bitcastToAPInt() const
Definition: APFloat.h:1132
llvm::sys::DynamicLibrary::SearchForAddressOfSymbol
static void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
Definition: DynamicLibrary.cpp:177
llvm::None
const NoneType None
Definition: None.h:23
llvm::APFloat::subtract
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:981
llvm::APInt::srem
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1767
llvm::StringMap< uint64_t >
llvm::SmallString< 128 >
llvm::APFloat::multiply
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:990
llvm::ExecutionEngine::removeModule
virtual bool removeModule(Module *M)
removeModule - Removes a Module from the list of modules, but does not free the module's memory.
Definition: ExecutionEngine.cpp:140
llvm::ExecutionEngine::runStaticConstructorsDestructors
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: ExecutionEngine.cpp:406
llvm::ExecutionEngine::getDataLayout
const DataLayout & getDataLayout() const
Definition: ExecutionEngine.h:197
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:201
llvm::APFloat
Definition: APFloat.h:701
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:179
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:136
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::StringMapIterator
Definition: StringMap.h:25
llvm::StringMap::erase
void erase(iterator I)
Definition: StringMap.h:355
llvm::APInt::sdiv
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1675
llvm::StructLayout
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:604
llvm::StoreIntToMemory
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:3060
llvm::APInt::getRawData
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:694
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:572
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:525
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::ConstantDataVector
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:752
llvm::FunctionType::getParamType
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:134
llvm::APIntOps::RoundFloatToAPInt
APInt RoundFloatToAPInt(float Float, unsigned width)
Converts a float value into a APInt.
Definition: APInt.h:2229
llvm::ExecutionEngineState::getGlobalAddressMap
GlobalAddressMapTy & getGlobalAddressMap()
Definition: ExecutionEngine.h:81
Archive.h
llvm::DataLayout::isDefault
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
Definition: DataLayout.h:254
llvm::ConstantDataSequential::getElementType
Type * getElementType() const
Return the element type of the array/vector.
Definition: Constants.cpp:2895
llvm::HighlightColor::Address
@ Address
llvm::ConstantVector
Constant Vector Declarations.
Definition: Constants.h:493
isTargetNullPtr
static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc)
isTargetNullPtr - Return whether the target pointer stored at Loc is null.
Definition: ExecutionEngine.cpp:414
llvm::StringMap::begin
iterator begin()
Definition: StringMap.h:204
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1605
ObjectFile.h
llvm::EngineBuilder::EngineBuilder
EngineBuilder()
Default constructor for EngineBuilder.
Definition: ExecutionEngine.cpp:470
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::ExecutionEngine::getPointerToFunction
virtual void * getPointerToFunction(Function *F)=0
getPointerToFunction - The different EE's represent function bodies in different ways.
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
llvm::GlobalValue::isThreadLocal
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:244
llvm::Type::isVoidTy
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:138
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::ExecutionEngine::getGlobalValueAtAddress
const GlobalValue * getGlobalValueAtAddress(void *Addr)
getGlobalValueAtAddress - Return the LLVM global value object that starts at the specified address.
Definition: ExecutionEngine.cpp:297
llvm::EngineKind::Either
const static Kind Either
Definition: ExecutionEngine.h:528
llvm::ConstantStruct
Definition: Constants.h:441
llvm::APInt::urem
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1697
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
Mangler.h
llvm::Type::FP128TyID
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition: Type.h:61
llvm::ExecutionEngineState::getGlobalAddressReverseMap
std::map< uint64_t, std::string > & getGlobalAddressReverseMap()
Definition: ExecutionEngine.h:85
llvm::ExecutionEngine::ExecutionEngine
ExecutionEngine(DataLayout DL)
Definition: ExecutionEngine.h:505
llvm::ExecutionEngine::getPointerToGlobal
void * getPointerToGlobal(const GlobalValue *GV)
getPointerToGlobal - This returns the address of the specified global value.
Definition: ExecutionEngine.cpp:565
llvm::ExecutionEngine::addGlobalMapping
void addGlobalMapping(const GlobalValue *GV, void *Addr)
addGlobalMapping - Tell the execution engine that the specified global is at the specified location.
Definition: ExecutionEngine.cpp:203
DataLayout.h
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:212
llvm::Type::PPC_FP128TyID
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::raw_svector_ostream::str
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:681
llvm::PointerTy
void * PointerTy
Definition: GenericValue.h:21
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
uint32_t
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::APInt::doubleToBits
static APInt doubleToBits(double V)
Converts a double to APInt bits.
Definition: APInt.h:1794
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:297
ValueHandle.h
llvm::Init
Definition: Record.h:271
llvm::GenericValue::DoubleVal
double DoubleVal
Definition: GenericValue.h:29
llvm::APInt::udiv
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1604
llvm::APInt::zext
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:934
llvm::Type::IntegerTyID
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:71
llvm::EngineBuilder::~EngineBuilder
~EngineBuilder()
llvm::APInt::bitsToDouble
double bitsToDouble() const
Converts APInt bits to a double.
Definition: APInt.h:1777
llvm::HexagonISD::CP
@ CP
Definition: HexagonISelLowering.h:53
RTDyldMemoryManager.h
llvm::GenericValue::FloatVal
float FloatVal
Definition: GenericValue.h:30
llvm::APFloatBase::rmTowardZero
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:194
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:936
llvm::ExecutionEngine
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
Definition: ExecutionEngine.h:100
llvm::ExecutionEngine::InitializeMemory
void InitializeMemory(const Constant *Init, void *Addr)
Definition: ExecutionEngine.cpp:1132
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:838
llvm::APInt::trunc
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:862
llvm::Type::isFloatTy
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:147
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::ConstantDataSequential::getElementAsDouble
double getElementAsDouble(unsigned i) const
If this is an sequential container of doubles, return the specified element as a double.
Definition: Constants.cpp:3257
llvm::StructLayout::getElementOffset
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:634
llvm::Type::FixedVectorTyID
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
llvm::fltSemantics
Definition: APFloat.cpp:54
llvm::ExecutionEngine::emitGlobals
void emitGlobals()
EmitGlobals - Emit all of the global variables to memory, storing their addresses into GlobalAddress.
Definition: ExecutionEngine.cpp:1188
llvm::BitWidth
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
llvm::ExecutionEngine::runFunction
virtual GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues)=0
runFunction - Execute the specified function with the specified arguments, and return the result.
llvm::Type::getPointerTo
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:738
llvm::ExecutionEngine::getPointerToGlobalIfAvailable
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
Definition: ExecutionEngine.cpp:285
llvm::Type::isDoubleTy
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:150
llvm::LoadIntFromMemory
void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes)
LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting from Src into IntVal,...
Definition: APInt.cpp:3086
y
into llvm powi allowing the code generator to produce balanced multiplication trees the intrinsic needs to be extended to support and second the code generator needs to be enhanced to lower these to multiplication trees Interesting testcase for add shift mul int y
Definition: README.txt:61
llvm::Function::getFunctionType
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:175
llvm::MCID::Add
@ Add
Definition: MCInstrDesc.h:183
llvm::APIntOps::RoundDoubleToAPInt
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
Definition: APInt.cpp:766
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:259
llvm::EngineBuilder::setSymbolResolver
EngineBuilder & setSymbolResolver(std::unique_ptr< LegacyJITSymbolResolver > SR)
Definition: ExecutionEngine.cpp:501
llvm::CallbackVH
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:383
llvm::APInt::sext
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:910
llvm::ExecutionEngine::getMemoryForGV
virtual char * getMemoryForGV(const GlobalVariable *GV)
getMemoryforGV - Allocate memory for a global variable.
Definition: ExecutionEngine.cpp:123
llvm::Type::StructTyID
@ StructTyID
Structures.
Definition: Type.h:74
llvm::APFloatBase::rmNearestTiesToEven
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:190
llvm::User::getNumOperands
unsigned getNumOperands() const
Definition: User.h:191
llvm::PTOGV
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
llvm::sys::IsLittleEndianHost
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:101
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:656
llvm::APInt::bitsToFloat
float bitsToFloat() const
Converts APInt bits to a float.
Definition: APInt.h:1786
llvm::Type::ScalableVectorTyID
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:77
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:401
DerivedTypes.h
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::DataLayout::getPointerSizeInBits
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:404
llvm::sys::DynamicLibrary::LoadLibraryPermanently
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
Definition: DynamicLibrary.h:85
llvm::Type::isAggregateType
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition: Type.h:267
llvm::makeMutableArrayRef
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
Definition: ArrayRef.h:534
llvm::APFloat::convertFromAPInt
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1115
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::EngineKind::Interpreter
@ Interpreter
Definition: ExecutionEngine.h:526
raw_ostream.h
copy
we should consider alternate ways to model stack dependencies Lots of things could be done in WebAssemblyTargetTransformInfo cpp there are numerous optimization related hooks that can be overridden in WebAssemblyTargetLowering Instead of the OptimizeReturned which should consider preserving the returned attribute through to MachineInstrs and extending the MemIntrinsicResults pass to do this optimization on calls too That would also let the WebAssemblyPeephole pass clean up dead defs for such as it does for stores Consider implementing and or getMachineCombinerPatterns Find a clean way to fix the problem which leads to the Shrink Wrapping pass being run after the WebAssembly PEI pass When setting multiple variables to the same we currently get code like const It could be done with a smaller encoding like local tee $pop5 local copy
Definition: README.txt:101
TargetRegistry.h
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:123
Debug.h
llvm::Type::X86_FP80TyID
@ X86_FP80TyID
80-bit floating point type (X87)
Definition: Type.h:60
JITEventListener.h
llvm::APInt::floatToBits
static APInt floatToBits(float V)
Converts a float to APInt bits.
Definition: APInt.h:1802
llvm::DataLayout::getPointerSize
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:701
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:102
llvm::Type::getPrimitiveSizeInBits
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:128
llvm::DataLayout::getTypeAllocSize
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:498
llvm::ConstantDataSequential::getNumElements
unsigned getNumElements() const
Return the number of elements in the array or vector.
Definition: Constants.cpp:2921
llvm::ExecutionEngine::addArchive
virtual void addArchive(object::OwningBinary< object::Archive > A)
addArchive - Add an Archive to the execution engine.
Definition: ExecutionEngine.cpp:136