LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExecutionEngine.cpp
Go to the documentation of this file.
1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the common interface used by the various execution engine
11 // subclasses.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "jit"
19 #include "llvm/ADT/SmallString.h"
20 #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/Module.h"
26 #include "llvm/IR/Operator.h"
27 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Host.h"
36 #include <cmath>
37 #include <cstring>
38 using namespace llvm;
39 
40 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
41 STATISTIC(NumGlobals , "Number of global vars initialized");
42 
43 // Pin the vtable to this file.
44 void ObjectCache::anchor() {}
45 void ObjectBuffer::anchor() {}
46 void ObjectBufferStream::anchor() {}
47 
48 ExecutionEngine *(*ExecutionEngine::JITCtor)(
49  Module *M,
50  std::string *ErrorStr,
51  JITMemoryManager *JMM,
52  bool GVsWithCode,
53  TargetMachine *TM) = 0;
54 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
55  Module *M,
56  std::string *ErrorStr,
57  RTDyldMemoryManager *MCJMM,
58  bool GVsWithCode,
59  TargetMachine *TM) = 0;
60 ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
61  std::string *ErrorStr) = 0;
62 
64  : EEState(*this),
65  LazyFunctionCreator(0) {
66  CompilingLazily = false;
67  GVCompilationDisabled = false;
68  SymbolSearchingDisabled = false;
69  Modules.push_back(M);
70  assert(M && "Module is null?");
71 }
72 
75  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
76  delete Modules[i];
77 }
78 
79 namespace {
80 /// \brief Helper class which uses a value handler to automatically deletes the
81 /// memory block when the GlobalVariable is destroyed.
82 class GVMemoryBlock : public CallbackVH {
83  GVMemoryBlock(const GlobalVariable *GV)
84  : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
85 
86 public:
87  /// \brief Returns the address the GlobalVariable should be written into. The
88  /// GVMemoryBlock object prefixes that.
89  static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
90  Type *ElTy = GV->getType()->getElementType();
91  size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
92  void *RawMemory = ::operator new(
93  DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
94  TD.getPreferredAlignment(GV))
95  + GVSize);
96  new(RawMemory) GVMemoryBlock(GV);
97  return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
98  }
99 
100  virtual void deleted() {
101  // We allocated with operator new and with some extra memory hanging off the
102  // end, so don't just delete this. I'm not sure if this is actually
103  // required.
104  this->~GVMemoryBlock();
105  ::operator delete(this);
106  }
107 };
108 } // anonymous namespace
109 
111  return GVMemoryBlock::Create(GV, *getDataLayout());
112 }
113 
116  E = Modules.end(); I != E; ++I) {
117  Module *Found = *I;
118  if (Found == M) {
119  Modules.erase(I);
121  return true;
122  }
123  }
124  return false;
125 }
126 
128  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
129  if (Function *F = Modules[i]->getFunction(FnName))
130  return F;
131  }
132  return 0;
133 }
134 
135 
137  const GlobalValue *ToUnmap) {
138  GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
139  void *OldVal;
140 
141  // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
142  // GlobalAddressMap.
143  if (I == GlobalAddressMap.end())
144  OldVal = 0;
145  else {
146  OldVal = I->second;
147  GlobalAddressMap.erase(I);
148  }
149 
150  GlobalAddressReverseMap.erase(OldVal);
151  return OldVal;
152 }
153 
154 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
155  MutexGuard locked(lock);
156 
157  DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
158  << "\' to [" << Addr << "]\n";);
159  void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
160  assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
161  CurVal = Addr;
162 
163  // If we are using the reverse mapping, add it too.
164  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
166  EEState.getGlobalAddressReverseMap(locked)[Addr];
167  assert((V == 0 || GV == 0) && "GlobalMapping already established!");
168  V = GV;
169  }
170 }
171 
173  MutexGuard locked(lock);
174 
175  EEState.getGlobalAddressMap(locked).clear();
176  EEState.getGlobalAddressReverseMap(locked).clear();
177 }
178 
180  MutexGuard locked(lock);
181 
182  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
183  EEState.RemoveMapping(locked, FI);
184  for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
185  GI != GE; ++GI)
186  EEState.RemoveMapping(locked, GI);
187 }
188 
189 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
190  MutexGuard locked(lock);
191 
193  EEState.getGlobalAddressMap(locked);
194 
195  // Deleting from the mapping?
196  if (Addr == 0)
197  return EEState.RemoveMapping(locked, GV);
198 
199  void *&CurVal = Map[GV];
200  void *OldVal = CurVal;
201 
202  if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
203  EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
204  CurVal = Addr;
205 
206  // If we are using the reverse mapping, add it too.
207  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
209  EEState.getGlobalAddressReverseMap(locked)[Addr];
210  assert((V == 0 || GV == 0) && "GlobalMapping already established!");
211  V = GV;
212  }
213  return OldVal;
214 }
215 
217  MutexGuard locked(lock);
218 
220  EEState.getGlobalAddressMap(locked).find(GV);
221  return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
222 }
223 
225  MutexGuard locked(lock);
226 
227  // If we haven't computed the reverse mapping yet, do so first.
228  if (EEState.getGlobalAddressReverseMap(locked).empty()) {
230  I = EEState.getGlobalAddressMap(locked).begin(),
231  E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
232  EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
233  I->second, I->first));
234  }
235 
236  std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
237  EEState.getGlobalAddressReverseMap(locked).find(Addr);
238  return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
239 }
240 
241 namespace {
242 class ArgvArray {
243  char *Array;
244  std::vector<char*> Values;
245 public:
246  ArgvArray() : Array(NULL) {}
247  ~ArgvArray() { clear(); }
248  void clear() {
249  delete[] Array;
250  Array = NULL;
251  for (size_t I = 0, E = Values.size(); I != E; ++I) {
252  delete[] Values[I];
253  }
254  Values.clear();
255  }
256  /// Turn a vector of strings into a nice argv style array of pointers to null
257  /// terminated strings.
258  void *reset(LLVMContext &C, ExecutionEngine *EE,
259  const std::vector<std::string> &InputArgv);
260 };
261 } // anonymous namespace
262 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
263  const std::vector<std::string> &InputArgv) {
264  clear(); // Free the old contents.
265  unsigned PtrSize = EE->getDataLayout()->getPointerSize();
266  Array = new char[(InputArgv.size()+1)*PtrSize];
267 
268  DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
269  Type *SBytePtr = Type::getInt8PtrTy(C);
270 
271  for (unsigned i = 0; i != InputArgv.size(); ++i) {
272  unsigned Size = InputArgv[i].size()+1;
273  char *Dest = new char[Size];
274  Values.push_back(Dest);
275  DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
276 
277  std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
278  Dest[Size-1] = 0;
279 
280  // Endian safe: Array[i] = (PointerTy)Dest;
281  EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
282  SBytePtr);
283  }
284 
285  // Null terminate it
286  EE->StoreValueToMemory(PTOGV(0),
287  (GenericValue*)(Array+InputArgv.size()*PtrSize),
288  SBytePtr);
289  return Array;
290 }
291 
293  bool isDtors) {
294  const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
295  GlobalVariable *GV = module->getNamedGlobal(Name);
296 
297  // If this global has internal linkage, or if it has a use, then it must be
298  // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
299  // this is the case, don't execute any of the global ctors, __main will do
300  // it.
301  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
302 
303  // Should be an array of '{ i32, void ()* }' structs. The first value is
304  // the init priority, which we ignore.
306  if (InitList == 0)
307  return;
308  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
309  ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
310  if (CS == 0) continue;
311 
312  Constant *FP = CS->getOperand(1);
313  if (FP->isNullValue())
314  continue; // Found a sentinal value, ignore.
315 
316  // Strip off constant expression casts.
317  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
318  if (CE->isCast())
319  FP = CE->getOperand(0);
320 
321  // Execute the ctor/dtor function!
322  if (Function *F = dyn_cast<Function>(FP))
323  runFunction(F, std::vector<GenericValue>());
324 
325  // FIXME: It is marginally lame that we just do nothing here if we see an
326  // entry we don't recognize. It might not be unreasonable for the verifier
327  // to not even allow this and just assert here.
328  }
329 }
330 
332  // Execute global ctors/dtors for each module in the program.
333  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
335 }
336 
337 #ifndef NDEBUG
338 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
339 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
340  unsigned PtrSize = EE->getDataLayout()->getPointerSize();
341  for (unsigned i = 0; i < PtrSize; ++i)
342  if (*(i + (uint8_t*)Loc))
343  return false;
344  return true;
345 }
346 #endif
347 
349  const std::vector<std::string> &argv,
350  const char * const * envp) {
351  std::vector<GenericValue> GVArgs;
352  GenericValue GVArgc;
353  GVArgc.IntVal = APInt(32, argv.size());
354 
355  // Check main() type
356  unsigned NumArgs = Fn->getFunctionType()->getNumParams();
357  FunctionType *FTy = Fn->getFunctionType();
358  Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
359 
360  // Check the argument types.
361  if (NumArgs > 3)
362  report_fatal_error("Invalid number of arguments of main() supplied");
363  if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
364  report_fatal_error("Invalid type for third argument of main() supplied");
365  if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
366  report_fatal_error("Invalid type for second argument of main() supplied");
367  if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
368  report_fatal_error("Invalid type for first argument of main() supplied");
369  if (!FTy->getReturnType()->isIntegerTy() &&
370  !FTy->getReturnType()->isVoidTy())
371  report_fatal_error("Invalid return type of main() supplied");
372 
373  ArgvArray CArgv;
374  ArgvArray CEnv;
375  if (NumArgs) {
376  GVArgs.push_back(GVArgc); // Arg #0 = argc.
377  if (NumArgs > 1) {
378  // Arg #1 = argv.
379  GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
380  assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
381  "argv[0] was null after CreateArgv");
382  if (NumArgs > 2) {
383  std::vector<std::string> EnvVars;
384  for (unsigned i = 0; envp[i]; ++i)
385  EnvVars.push_back(envp[i]);
386  // Arg #2 = envp.
387  GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
388  }
389  }
390  }
391 
392  return runFunction(Fn, GVArgs).IntVal.getZExtValue();
393 }
394 
396  bool ForceInterpreter,
397  std::string *ErrorStr,
398  CodeGenOpt::Level OptLevel,
399  bool GVsWithCode) {
401  .setEngineKind(ForceInterpreter
403  : EngineKind::JIT)
404  .setErrorStr(ErrorStr)
405  .setOptLevel(OptLevel)
406  .setAllocateGVsWithCode(GVsWithCode);
407 
408  return EB.create();
409 }
410 
411 /// createJIT - This is the factory method for creating a JIT for the current
412 /// machine, it does not fall back to the interpreter. This takes ownership
413 /// of the module.
415  std::string *ErrorStr,
416  JITMemoryManager *JMM,
418  bool GVsWithCode,
420  CodeModel::Model CMM) {
421  if (ExecutionEngine::JITCtor == 0) {
422  if (ErrorStr)
423  *ErrorStr = "JIT has not been linked in.";
424  return 0;
425  }
426 
427  // Use the defaults for extra parameters. Users can use EngineBuilder to
428  // set them.
429  EngineBuilder EB(M);
431  EB.setErrorStr(ErrorStr);
432  EB.setRelocationModel(RM);
433  EB.setCodeModel(CMM);
434  EB.setAllocateGVsWithCode(GVsWithCode);
435  EB.setOptLevel(OL);
436  EB.setJITMemoryManager(JMM);
437 
438  // TODO: permit custom TargetOptions here
439  TargetMachine *TM = EB.selectTarget();
440  if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
441 
442  return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
443 }
444 
446  OwningPtr<TargetMachine> TheTM(TM); // Take ownership.
447 
448  // Make sure we can resolve symbols in the program as well. The zero arg
449  // to the function tells DynamicLibrary to load the program, not a library.
451  return 0;
452 
453  assert(!(JMM && MCJMM));
454 
455  // If the user specified a memory manager but didn't specify which engine to
456  // create, we assume they only want the JIT, and we fail if they only want
457  // the interpreter.
458  if (JMM || MCJMM) {
459  if (WhichEngine & EngineKind::JIT)
460  WhichEngine = EngineKind::JIT;
461  else {
462  if (ErrorStr)
463  *ErrorStr = "Cannot create an interpreter with a memory manager.";
464  return 0;
465  }
466  }
467 
468  if (MCJMM && ! UseMCJIT) {
469  if (ErrorStr)
470  *ErrorStr =
471  "Cannot create a legacy JIT with a runtime dyld memory "
472  "manager.";
473  return 0;
474  }
475 
476  // Unless the interpreter was explicitly selected or the JIT is not linked,
477  // try making a JIT.
478  if ((WhichEngine & EngineKind::JIT) && TheTM) {
479  Triple TT(M->getTargetTriple());
480  if (!TM->getTarget().hasJIT()) {
481  errs() << "WARNING: This target JIT is not designed for the host"
482  << " you are running. If bad things happen, please choose"
483  << " a different -march switch.\n";
484  }
485 
486  if (UseMCJIT && ExecutionEngine::MCJITCtor) {
487  ExecutionEngine *EE =
488  ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
489  AllocateGVsWithCode, TheTM.take());
490  if (EE) return EE;
491  } else if (ExecutionEngine::JITCtor) {
492  ExecutionEngine *EE =
493  ExecutionEngine::JITCtor(M, ErrorStr, JMM,
494  AllocateGVsWithCode, TheTM.take());
495  if (EE) return EE;
496  }
497  }
498 
499  // If we can't make a JIT and we didn't request one specifically, try making
500  // an interpreter instead.
501  if (WhichEngine & EngineKind::Interpreter) {
503  return ExecutionEngine::InterpCtor(M, ErrorStr);
504  if (ErrorStr)
505  *ErrorStr = "Interpreter has not been linked in.";
506  return 0;
507  }
508 
509  if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0 &&
511  if (ErrorStr)
512  *ErrorStr = "JIT has not been linked in.";
513  }
514 
515  return 0;
516 }
517 
519  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
520  return getPointerToFunction(F);
521 
522  MutexGuard locked(lock);
523  if (void *P = EEState.getGlobalAddressMap(locked)[GV])
524  return P;
525 
526  // Global variable might have been added since interpreter started.
527  if (GlobalVariable *GVar =
528  const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
529  EmitGlobalVariable(GVar);
530  else
531  llvm_unreachable("Global hasn't had an address allocated yet!");
532 
533  return EEState.getGlobalAddressMap(locked)[GV];
534 }
535 
536 /// \brief Converts a Constant* into a GenericValue, including handling of
537 /// ConstantExpr values.
539  // If its undefined, return the garbage.
540  if (isa<UndefValue>(C)) {
541  GenericValue Result;
542  switch (C->getType()->getTypeID()) {
543  default:
544  break;
545  case Type::IntegerTyID:
546  case Type::X86_FP80TyID:
547  case Type::FP128TyID:
548  case Type::PPC_FP128TyID:
549  // Although the value is undefined, we still have to construct an APInt
550  // with the correct bit width.
551  Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
552  break;
553  case Type::StructTyID: {
554  // if the whole struct is 'undef' just reserve memory for the value.
555  if(StructType *STy = dyn_cast<StructType>(C->getType())) {
556  unsigned int elemNum = STy->getNumElements();
557  Result.AggregateVal.resize(elemNum);
558  for (unsigned int i = 0; i < elemNum; ++i) {
559  Type *ElemTy = STy->getElementType(i);
560  if (ElemTy->isIntegerTy())
561  Result.AggregateVal[i].IntVal =
562  APInt(ElemTy->getPrimitiveSizeInBits(), 0);
563  else if (ElemTy->isAggregateType()) {
564  const Constant *ElemUndef = UndefValue::get(ElemTy);
565  Result.AggregateVal[i] = getConstantValue(ElemUndef);
566  }
567  }
568  }
569  }
570  break;
571  case Type::VectorTyID:
572  // if the whole vector is 'undef' just reserve memory for the value.
573  const VectorType* VTy = dyn_cast<VectorType>(C->getType());
574  const Type *ElemTy = VTy->getElementType();
575  unsigned int elemNum = VTy->getNumElements();
576  Result.AggregateVal.resize(elemNum);
577  if (ElemTy->isIntegerTy())
578  for (unsigned int i = 0; i < elemNum; ++i)
579  Result.AggregateVal[i].IntVal =
580  APInt(ElemTy->getPrimitiveSizeInBits(), 0);
581  break;
582  }
583  return Result;
584  }
585 
586  // Otherwise, if the value is a ConstantExpr...
587  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
588  Constant *Op0 = CE->getOperand(0);
589  switch (CE->getOpcode()) {
590  case Instruction::GetElementPtr: {
591  // Compute the index
592  GenericValue Result = getConstantValue(Op0);
593  APInt Offset(TD->getPointerSizeInBits(), 0);
594  cast<GEPOperator>(CE)->accumulateConstantOffset(*TD, Offset);
595 
596  char* tmp = (char*) Result.PointerVal;
597  Result = PTOGV(tmp + Offset.getSExtValue());
598  return Result;
599  }
600  case Instruction::Trunc: {
601  GenericValue GV = getConstantValue(Op0);
602  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
603  GV.IntVal = GV.IntVal.trunc(BitWidth);
604  return GV;
605  }
606  case Instruction::ZExt: {
607  GenericValue GV = getConstantValue(Op0);
608  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
609  GV.IntVal = GV.IntVal.zext(BitWidth);
610  return GV;
611  }
612  case Instruction::SExt: {
613  GenericValue GV = getConstantValue(Op0);
614  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
615  GV.IntVal = GV.IntVal.sext(BitWidth);
616  return GV;
617  }
618  case Instruction::FPTrunc: {
619  // FIXME long double
620  GenericValue GV = getConstantValue(Op0);
621  GV.FloatVal = float(GV.DoubleVal);
622  return GV;
623  }
624  case Instruction::FPExt:{
625  // FIXME long double
626  GenericValue GV = getConstantValue(Op0);
627  GV.DoubleVal = double(GV.FloatVal);
628  return GV;
629  }
630  case Instruction::UIToFP: {
631  GenericValue GV = getConstantValue(Op0);
632  if (CE->getType()->isFloatTy())
633  GV.FloatVal = float(GV.IntVal.roundToDouble());
634  else if (CE->getType()->isDoubleTy())
635  GV.DoubleVal = GV.IntVal.roundToDouble();
636  else if (CE->getType()->isX86_FP80Ty()) {
638  (void)apf.convertFromAPInt(GV.IntVal,
639  false,
641  GV.IntVal = apf.bitcastToAPInt();
642  }
643  return GV;
644  }
645  case Instruction::SIToFP: {
646  GenericValue GV = getConstantValue(Op0);
647  if (CE->getType()->isFloatTy())
648  GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
649  else if (CE->getType()->isDoubleTy())
651  else if (CE->getType()->isX86_FP80Ty()) {
653  (void)apf.convertFromAPInt(GV.IntVal,
654  true,
656  GV.IntVal = apf.bitcastToAPInt();
657  }
658  return GV;
659  }
660  case Instruction::FPToUI: // double->APInt conversion handles sign
661  case Instruction::FPToSI: {
662  GenericValue GV = getConstantValue(Op0);
663  uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
664  if (Op0->getType()->isFloatTy())
665  GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
666  else if (Op0->getType()->isDoubleTy())
667  GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
668  else if (Op0->getType()->isX86_FP80Ty()) {
670  uint64_t v;
671  bool ignored;
672  (void)apf.convertToInteger(&v, BitWidth,
673  CE->getOpcode()==Instruction::FPToSI,
674  APFloat::rmTowardZero, &ignored);
675  GV.IntVal = v; // endian?
676  }
677  return GV;
678  }
679  case Instruction::PtrToInt: {
680  GenericValue GV = getConstantValue(Op0);
681  uint32_t PtrWidth = TD->getTypeSizeInBits(Op0->getType());
682  assert(PtrWidth <= 64 && "Bad pointer width");
683  GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
684  uint32_t IntWidth = TD->getTypeSizeInBits(CE->getType());
685  GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
686  return GV;
687  }
688  case Instruction::IntToPtr: {
689  GenericValue GV = getConstantValue(Op0);
690  uint32_t PtrWidth = TD->getTypeSizeInBits(CE->getType());
691  GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
692  assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
693  GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
694  return GV;
695  }
696  case Instruction::BitCast: {
697  GenericValue GV = getConstantValue(Op0);
698  Type* DestTy = CE->getType();
699  switch (Op0->getType()->getTypeID()) {
700  default: llvm_unreachable("Invalid bitcast operand");
701  case Type::IntegerTyID:
702  assert(DestTy->isFloatingPointTy() && "invalid bitcast");
703  if (DestTy->isFloatTy())
704  GV.FloatVal = GV.IntVal.bitsToFloat();
705  else if (DestTy->isDoubleTy())
706  GV.DoubleVal = GV.IntVal.bitsToDouble();
707  break;
708  case Type::FloatTyID:
709  assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
711  break;
712  case Type::DoubleTyID:
713  assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
715  break;
716  case Type::PointerTyID:
717  assert(DestTy->isPointerTy() && "Invalid bitcast");
718  break; // getConstantValue(Op0) above already converted it
719  }
720  return GV;
721  }
722  case Instruction::Add:
723  case Instruction::FAdd:
724  case Instruction::Sub:
725  case Instruction::FSub:
726  case Instruction::Mul:
727  case Instruction::FMul:
728  case Instruction::UDiv:
729  case Instruction::SDiv:
730  case Instruction::URem:
731  case Instruction::SRem:
732  case Instruction::And:
733  case Instruction::Or:
734  case Instruction::Xor: {
735  GenericValue LHS = getConstantValue(Op0);
736  GenericValue RHS = getConstantValue(CE->getOperand(1));
737  GenericValue GV;
738  switch (CE->getOperand(0)->getType()->getTypeID()) {
739  default: llvm_unreachable("Bad add type!");
740  case Type::IntegerTyID:
741  switch (CE->getOpcode()) {
742  default: llvm_unreachable("Invalid integer opcode");
743  case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
744  case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
745  case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
746  case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
747  case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
748  case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
749  case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
750  case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
751  case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
752  case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
753  }
754  break;
755  case Type::FloatTyID:
756  switch (CE->getOpcode()) {
757  default: llvm_unreachable("Invalid float opcode");
758  case Instruction::FAdd:
759  GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
760  case Instruction::FSub:
761  GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
762  case Instruction::FMul:
763  GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
764  case Instruction::FDiv:
765  GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
766  case Instruction::FRem:
767  GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
768  }
769  break;
770  case Type::DoubleTyID:
771  switch (CE->getOpcode()) {
772  default: llvm_unreachable("Invalid double opcode");
773  case Instruction::FAdd:
774  GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
775  case Instruction::FSub:
776  GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
777  case Instruction::FMul:
778  GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
779  case Instruction::FDiv:
780  GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
781  case Instruction::FRem:
782  GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
783  }
784  break;
785  case Type::X86_FP80TyID:
786  case Type::PPC_FP128TyID:
787  case Type::FP128TyID: {
788  const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
789  APFloat apfLHS = APFloat(Sem, LHS.IntVal);
790  switch (CE->getOpcode()) {
791  default: llvm_unreachable("Invalid long double opcode");
792  case Instruction::FAdd:
793  apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
794  GV.IntVal = apfLHS.bitcastToAPInt();
795  break;
796  case Instruction::FSub:
797  apfLHS.subtract(APFloat(Sem, RHS.IntVal),
799  GV.IntVal = apfLHS.bitcastToAPInt();
800  break;
801  case Instruction::FMul:
802  apfLHS.multiply(APFloat(Sem, RHS.IntVal),
804  GV.IntVal = apfLHS.bitcastToAPInt();
805  break;
806  case Instruction::FDiv:
807  apfLHS.divide(APFloat(Sem, RHS.IntVal),
809  GV.IntVal = apfLHS.bitcastToAPInt();
810  break;
811  case Instruction::FRem:
812  apfLHS.mod(APFloat(Sem, RHS.IntVal),
814  GV.IntVal = apfLHS.bitcastToAPInt();
815  break;
816  }
817  }
818  break;
819  }
820  return GV;
821  }
822  default:
823  break;
824  }
825 
826  SmallString<256> Msg;
827  raw_svector_ostream OS(Msg);
828  OS << "ConstantExpr not handled: " << *CE;
829  report_fatal_error(OS.str());
830  }
831 
832  // Otherwise, we have a simple constant.
833  GenericValue Result;
834  switch (C->getType()->getTypeID()) {
835  case Type::FloatTyID:
836  Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
837  break;
838  case Type::DoubleTyID:
839  Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
840  break;
841  case Type::X86_FP80TyID:
842  case Type::FP128TyID:
843  case Type::PPC_FP128TyID:
844  Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
845  break;
846  case Type::IntegerTyID:
847  Result.IntVal = cast<ConstantInt>(C)->getValue();
848  break;
849  case Type::PointerTyID:
850  if (isa<ConstantPointerNull>(C))
851  Result.PointerVal = 0;
852  else if (const Function *F = dyn_cast<Function>(C))
853  Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
854  else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
855  Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
856  else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
857  Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
858  BA->getBasicBlock())));
859  else
860  llvm_unreachable("Unknown constant pointer type!");
861  break;
862  case Type::VectorTyID: {
863  unsigned elemNum;
864  Type* ElemTy;
866  const ConstantVector *CV = dyn_cast<ConstantVector>(C);
868 
869  if (CDV) {
870  elemNum = CDV->getNumElements();
871  ElemTy = CDV->getElementType();
872  } else if (CV || CAZ) {
873  VectorType* VTy = dyn_cast<VectorType>(C->getType());
874  elemNum = VTy->getNumElements();
875  ElemTy = VTy->getElementType();
876  } else {
877  llvm_unreachable("Unknown constant vector type!");
878  }
879 
880  Result.AggregateVal.resize(elemNum);
881  // Check if vector holds floats.
882  if(ElemTy->isFloatTy()) {
883  if (CAZ) {
884  GenericValue floatZero;
885  floatZero.FloatVal = 0.f;
886  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
887  floatZero);
888  break;
889  }
890  if(CV) {
891  for (unsigned i = 0; i < elemNum; ++i)
892  if (!isa<UndefValue>(CV->getOperand(i)))
893  Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
894  CV->getOperand(i))->getValueAPF().convertToFloat();
895  break;
896  }
897  if(CDV)
898  for (unsigned i = 0; i < elemNum; ++i)
899  Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
900 
901  break;
902  }
903  // Check if vector holds doubles.
904  if (ElemTy->isDoubleTy()) {
905  if (CAZ) {
906  GenericValue doubleZero;
907  doubleZero.DoubleVal = 0.0;
908  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
909  doubleZero);
910  break;
911  }
912  if(CV) {
913  for (unsigned i = 0; i < elemNum; ++i)
914  if (!isa<UndefValue>(CV->getOperand(i)))
915  Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
916  CV->getOperand(i))->getValueAPF().convertToDouble();
917  break;
918  }
919  if(CDV)
920  for (unsigned i = 0; i < elemNum; ++i)
921  Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
922 
923  break;
924  }
925  // Check if vector holds integers.
926  if (ElemTy->isIntegerTy()) {
927  if (CAZ) {
928  GenericValue intZero;
929  intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
930  std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
931  intZero);
932  break;
933  }
934  if(CV) {
935  for (unsigned i = 0; i < elemNum; ++i)
936  if (!isa<UndefValue>(CV->getOperand(i)))
937  Result.AggregateVal[i].IntVal = cast<ConstantInt>(
938  CV->getOperand(i))->getValue();
939  else {
940  Result.AggregateVal[i].IntVal =
941  APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
942  }
943  break;
944  }
945  if(CDV)
946  for (unsigned i = 0; i < elemNum; ++i)
947  Result.AggregateVal[i].IntVal = APInt(
949  CDV->getElementAsInteger(i));
950 
951  break;
952  }
953  llvm_unreachable("Unknown constant pointer type!");
954  }
955  break;
956 
957  default:
958  SmallString<256> Msg;
959  raw_svector_ostream OS(Msg);
960  OS << "ERROR: Constant unimplemented for type: " << *C->getType();
961  report_fatal_error(OS.str());
962  }
963 
964  return Result;
965 }
966 
967 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
968 /// with the integer held in IntVal.
969 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
970  unsigned StoreBytes) {
971  assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
972  const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
973 
975  // Little-endian host - the source is ordered from LSB to MSB. Order the
976  // destination from LSB to MSB: Do a straight copy.
977  memcpy(Dst, Src, StoreBytes);
978  } else {
979  // Big-endian host - the source is an array of 64 bit words ordered from
980  // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
981  // from MSB to LSB: Reverse the word order, but not the bytes in a word.
982  while (StoreBytes > sizeof(uint64_t)) {
983  StoreBytes -= sizeof(uint64_t);
984  // May not be aligned so use memcpy.
985  memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
986  Src += sizeof(uint64_t);
987  }
988 
989  memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
990  }
991 }
992 
994  GenericValue *Ptr, Type *Ty) {
995  const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
996 
997  switch (Ty->getTypeID()) {
998  default:
999  dbgs() << "Cannot store value of type " << *Ty << "!\n";
1000  break;
1001  case Type::IntegerTyID:
1002  StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
1003  break;
1004  case Type::FloatTyID:
1005  *((float*)Ptr) = Val.FloatVal;
1006  break;
1007  case Type::DoubleTyID:
1008  *((double*)Ptr) = Val.DoubleVal;
1009  break;
1010  case Type::X86_FP80TyID:
1011  memcpy(Ptr, Val.IntVal.getRawData(), 10);
1012  break;
1013  case Type::PointerTyID:
1014  // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1015  if (StoreBytes != sizeof(PointerTy))
1016  memset(&(Ptr->PointerVal), 0, StoreBytes);
1017 
1018  *((PointerTy*)Ptr) = Val.PointerVal;
1019  break;
1020  case Type::VectorTyID:
1021  for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1022  if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1023  *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1024  if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1025  *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1026  if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1027  unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1028  StoreIntToMemory(Val.AggregateVal[i].IntVal,
1029  (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1030  }
1031  }
1032  break;
1033  }
1034 
1035  if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
1036  // Host and target are different endian - reverse the stored bytes.
1037  std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1038 }
1039 
1040 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1041 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1042 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1043  assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
1044  uint8_t *Dst = reinterpret_cast<uint8_t *>(
1045  const_cast<uint64_t *>(IntVal.getRawData()));
1046 
1048  // Little-endian host - the destination must be ordered from LSB to MSB.
1049  // The source is ordered from LSB to MSB: Do a straight copy.
1050  memcpy(Dst, Src, LoadBytes);
1051  else {
1052  // Big-endian - the destination is an array of 64 bit words ordered from
1053  // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1054  // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1055  // a word.
1056  while (LoadBytes > sizeof(uint64_t)) {
1057  LoadBytes -= sizeof(uint64_t);
1058  // May not be aligned so use memcpy.
1059  memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1060  Dst += sizeof(uint64_t);
1061  }
1062 
1063  memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1064  }
1065 }
1066 
1067 /// FIXME: document
1068 ///
1070  GenericValue *Ptr,
1071  Type *Ty) {
1072  const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
1073 
1074  switch (Ty->getTypeID()) {
1075  case Type::IntegerTyID:
1076  // An APInt with all words initially zero.
1077  Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1078  LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1079  break;
1080  case Type::FloatTyID:
1081  Result.FloatVal = *((float*)Ptr);
1082  break;
1083  case Type::DoubleTyID:
1084  Result.DoubleVal = *((double*)Ptr);
1085  break;
1086  case Type::PointerTyID:
1087  Result.PointerVal = *((PointerTy*)Ptr);
1088  break;
1089  case Type::X86_FP80TyID: {
1090  // This is endian dependent, but it will only work on x86 anyway.
1091  // FIXME: Will not trap if loading a signaling NaN.
1092  uint64_t y[2];
1093  memcpy(y, Ptr, 10);
1094  Result.IntVal = APInt(80, y);
1095  break;
1096  }
1097  case Type::VectorTyID: {
1098  const VectorType *VT = cast<VectorType>(Ty);
1099  const Type *ElemT = VT->getElementType();
1100  const unsigned numElems = VT->getNumElements();
1101  if (ElemT->isFloatTy()) {
1102  Result.AggregateVal.resize(numElems);
1103  for (unsigned i = 0; i < numElems; ++i)
1104  Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1105  }
1106  if (ElemT->isDoubleTy()) {
1107  Result.AggregateVal.resize(numElems);
1108  for (unsigned i = 0; i < numElems; ++i)
1109  Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1110  }
1111  if (ElemT->isIntegerTy()) {
1112  GenericValue intZero;
1113  const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1114  intZero.IntVal = APInt(elemBitWidth, 0);
1115  Result.AggregateVal.resize(numElems, intZero);
1116  for (unsigned i = 0; i < numElems; ++i)
1117  LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1118  (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1119  }
1120  break;
1121  }
1122  default:
1123  SmallString<256> Msg;
1124  raw_svector_ostream OS(Msg);
1125  OS << "Cannot load value of type " << *Ty << "!";
1126  report_fatal_error(OS.str());
1127  }
1128 }
1129 
1131  DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1132  DEBUG(Init->dump());
1133  if (isa<UndefValue>(Init))
1134  return;
1135 
1136  if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1137  unsigned ElementSize =
1138  getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
1139  for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1140  InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1141  return;
1142  }
1143 
1144  if (isa<ConstantAggregateZero>(Init)) {
1145  memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
1146  return;
1147  }
1148 
1149  if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1150  unsigned ElementSize =
1151  getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
1152  for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1153  InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1154  return;
1155  }
1156 
1157  if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1158  const StructLayout *SL =
1159  getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
1160  for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1161  InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1162  return;
1163  }
1164 
1165  if (const ConstantDataSequential *CDS =
1166  dyn_cast<ConstantDataSequential>(Init)) {
1167  // CDS is already laid out in host memory order.
1168  StringRef Data = CDS->getRawDataValues();
1169  memcpy(Addr, Data.data(), Data.size());
1170  return;
1171  }
1172 
1173  if (Init->getType()->isFirstClassType()) {
1174  GenericValue Val = getConstantValue(Init);
1175  StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1176  return;
1177  }
1178 
1179  DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1180  llvm_unreachable("Unknown constant type to initialize memory with!");
1181 }
1182 
1183 /// EmitGlobals - Emit all of the global variables to memory, storing their
1184 /// addresses into GlobalAddress. This must make sure to copy the contents of
1185 /// their initializers into the memory.
1187  // Loop over all of the global variables in the program, allocating the memory
1188  // to hold them. If there is more than one module, do a prepass over globals
1189  // to figure out how the different modules should link together.
1190  std::map<std::pair<std::string, Type*>,
1191  const GlobalValue*> LinkedGlobalsMap;
1192 
1193  if (Modules.size() != 1) {
1194  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1195  Module &M = *Modules[m];
1197  E = M.global_end(); I != E; ++I) {
1198  const GlobalValue *GV = I;
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  GVEntry->hasDLLImportLinkage() ||
1216  GVEntry->hasDLLExportLinkage())
1217  continue;
1218 
1219  // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1220  // symbol. FIXME is this right for common?
1221  if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1222  GVEntry = GV;
1223  }
1224  }
1225  }
1226 
1227  std::vector<const GlobalValue*> NonCanonicalGlobals;
1228  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1229  Module &M = *Modules[m];
1231  I != E; ++I) {
1232  // In the multi-module case, see what this global maps to.
1233  if (!LinkedGlobalsMap.empty()) {
1234  if (const GlobalValue *GVEntry =
1235  LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1236  // If something else is the canonical global, ignore this one.
1237  if (GVEntry != &*I) {
1238  NonCanonicalGlobals.push_back(I);
1239  continue;
1240  }
1241  }
1242  }
1243 
1244  if (!I->isDeclaration()) {
1246  } else {
1247  // External variable reference. Try to use the dynamic loader to
1248  // get a pointer to it.
1249  if (void *SymAddr =
1251  addGlobalMapping(I, SymAddr);
1252  else {
1253  report_fatal_error("Could not resolve external global address: "
1254  +I->getName());
1255  }
1256  }
1257  }
1258 
1259  // If there are multiple modules, map the non-canonical globals to their
1260  // canonical location.
1261  if (!NonCanonicalGlobals.empty()) {
1262  for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1263  const GlobalValue *GV = NonCanonicalGlobals[i];
1264  const GlobalValue *CGV =
1265  LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1266  void *Ptr = getPointerToGlobalIfAvailable(CGV);
1267  assert(Ptr && "Canonical global wasn't codegen'd!");
1268  addGlobalMapping(GV, Ptr);
1269  }
1270  }
1271 
1272  // Now that all of the globals are set up in memory, loop through them all
1273  // and initialize their contents.
1275  I != E; ++I) {
1276  if (!I->isDeclaration()) {
1277  if (!LinkedGlobalsMap.empty()) {
1278  if (const GlobalValue *GVEntry =
1279  LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1280  if (GVEntry != &*I) // Not the canonical variable.
1281  continue;
1282  }
1284  }
1285  }
1286  }
1287 }
1288 
1289 // EmitGlobalVariable - This method emits the specified global variable to the
1290 // address specified in GlobalAddresses, or allocates new memory if it's not
1291 // already in the map.
1293  void *GA = getPointerToGlobalIfAvailable(GV);
1294 
1295  if (GA == 0) {
1296  // If it's not already specified, allocate memory for the global.
1297  GA = getMemoryForGV(GV);
1298 
1299  // If we failed to allocate memory for this global, return.
1300  if (GA == 0) return;
1301 
1302  addGlobalMapping(GV, GA);
1303  }
1304 
1305  // Don't initialize if it's thread local, let the client do it.
1306  if (!GV->isThreadLocal())
1307  InitializeMemory(GV->getInitializer(), GA);
1308 
1309  Type *ElTy = GV->getType()->getElementType();
1310  size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
1311  NumInitBytes += (unsigned)GVSize;
1312  ++NumGlobals;
1313 }
1314 
1316  : EE(EE), GlobalAddressMap(this) {
1317 }
1318 
1319 sys::Mutex *
1321  return &EES->EE.lock;
1322 }
1323 
1325  const GlobalValue *Old) {
1326  void *OldVal = EES->GlobalAddressMap.lookup(Old);
1327  EES->GlobalAddressReverseMap.erase(OldVal);
1328 }
1329 
1331  const GlobalValue *,
1332  const GlobalValue *) {
1333  llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1334  " RAUW on a value it has a global mapping for.");
1335 }
void clear()
Definition: ValueMap.h:109
void * RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap)
Erase an entry from the mapping table.
opStatus divide(const APFloat &, roundingMode)
Definition: APFloat.cpp:1675
static ExecutionEngine *(* MCJITCtor)(Module *M, std::string *ErrorStr, RTDyldMemoryManager *MCJMM, bool GVsWithCode, TargetMachine *TM)
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:181
PointerTy PointerVal
Definition: GenericValue.h:34
std::vector< GenericValue > AggregateVal
Definition: GenericValue.h:40
static void * SearchForAddressOfSymbol(const char *symbolName)
Search through libraries for address of a symbol.
double signedRoundToDouble() const
Converts this signed APInt to a double value.
Definition: APInt.h:1439
raw_ostream & errs()
virtual void * getOrEmitGlobalVariable(const GlobalVariable *GV)
LLVMContext & getContext() const
Definition: Function.cpp:167
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1306
bool hasName() const
Definition: Value.h:117
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
unsigned getScalarSizeInBits()
Definition: Type.cpp:135
int runFunctionAsMain(Function *Fn, const std::vector< std::string > &argv, const char *const *envp)
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getNumParams() const
Definition: DerivedTypes.h:133
virtual bool removeModule(Module *M)
2: 32-bit floating point type
Definition: Type.h:57
ValueT lookup(const KeyT &Val) const
Definition: ValueMap.h:125
unsigned getPointerSize(unsigned AS=0) const
Definition: DataLayout.h:261
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
unsigned getNumOperands() const
Definition: User.h:108
bool hasAppendingLinkage() const
Definition: GlobalValue.h:204
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:149
void EmitGlobalVariable(const GlobalVariable *GV)
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:173
virtual void * getPointerToFunctionOrStub(Function *F)
12: Structures
Definition: Type.h:70
F(f)
4: 80-bit floating point type (X87)
Definition: Type.h:59
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Definition: APFloat.h:212
double roundToDouble(bool isSigned) const
Converts this APInt to a double value.
Definition: APInt.cpp:858
14: Pointers
Definition: Type.h:72
const Constant * getInitializer() const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
const std::string & getTargetTriple() const
Definition: Module.h:237
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
GlobalAddressMapTy & getGlobalAddressMap(const MutexGuard &)
StringRef getName() const
Definition: Value.cpp:167
static ExecutionEngine *(* InterpCtor)(Module *M, std::string *ErrorStr)
static ExecutionEngine * create(Module *M, bool ForceInterpreter=false, std::string *ErrorStr=0, CodeGenOpt::Level OptLevel=CodeGenOpt::Default, bool GVsWithCode=true)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1890
static const fltSemantics x87DoubleExtended
Definition: APFloat.h:136
void dump() const
dump - Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:2212
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
const StructLayout * getStructLayout(StructType *Ty) const
Definition: DataLayout.cpp:445
virtual void runStaticConstructorsDestructors(bool isDtors)
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
void * getPointerToGlobal(const GlobalValue *GV)
void InitializeMemory(const Constant *Init, void *Addr)
opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, bool *) const
Definition: APFloat.cpp:2157
#define llvm_unreachable(msg)
double getElementAsDouble(unsigned i) const
Definition: Constants.cpp:2493
global_iterator global_begin()
Definition: Module.h:521
static const bool IsLittleEndianHost
Definition: Host.h:38
virtual GenericValue runFunction(Function *F, const std::vector< GenericValue > &ArgValues)=0
static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes)
const char * data() const
Definition: StringRef.h:107
virtual char * getMemoryForGV(const GlobalVariable *GV)
getMemoryforGV - Allocate memory for a global variable.
void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, Type *Ty)
static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment)
Definition: DataLayout.h:407
static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc)
isTargetNullPtr - Return whether the target pointer stored at Loc is null.
iterator find(const KeyT &Val)
Definition: ValueMap.h:116
bool isFirstClassType() const
Definition: Type.h:251
double fmod(double x, double y);
TypeID getTypeID() const
Definition: Type.h:137
bool isFloatingPointTy() const
Definition: Type.h:162
static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old)
EngineBuilder & setCodeModel(CodeModel::Model M)
GenericValue getConstantValue(const Constant *C)
Converts a Constant* into a GenericValue, including handling of ConstantExpr values.
static void onRAUW(ExecutionEngineState *, const GlobalValue *, const GlobalValue *)
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:408
EngineBuilder & setAllocateGVsWithCode(bool a)
void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, Type *Ty)
Type * getElementType() const
Definition: DerivedTypes.h:319
EngineBuilder & setEngineKind(EngineKind::Kind w)
opStatus mod(const APFloat &, roundingMode)
C fmod, or llvm frem.
Definition: APFloat.cpp:1731
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition: APFloat.cpp:2238
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:442
10: Arbitrary bit width integers
Definition: Type.h:68
bool hasJIT() const
hasJIT - Check if this targets supports the just-in-time compilation.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
Guard a section of code with a Mutex.
Definition: MutexGuard.h:27
#define P(N)
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:128
* if(!EatIfPresent(lltok::kw_thread_local)) return false
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:919
virtual void * getPointerToBasicBlock(BasicBlock *BB)=0
STATISTIC(NumInitBytes,"Number of bytes of global vars initialized")
LLVM Constant Representation.
Definition: Constant.h:41
bool hasDLLExportLinkage() const
Definition: GlobalValue.h:213
static ExecutionEngine * createJIT(Module *M, std::string *ErrorStr=0, JITMemoryManager *JMM=0, CodeGenOpt::Level OptLevel=CodeGenOpt::Default, bool GVsWithCode=true, Reloc::Model RM=Reloc::Default, CodeModel::Model CMM=CodeModel::JITDefault)
EngineBuilder & setErrorStr(std::string *e)
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:146
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1845
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1850
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=0)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:942
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1252
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:61
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1879
Value * getOperand(unsigned i) const
Definition: User.h:88
static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes)
void * getPointerToGlobalIfAvailable(const GlobalValue *GV)
uint64_t getElementAsInteger(unsigned i) const
Definition: Constants.cpp:2443
bool isPointerTy() const
Definition: Type.h:220
static UndefValue * get(Type *T)
Definition: Constants.cpp:1334
void clearGlobalMappingsFromModule(Module *M)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:284
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Definition: DataLayout.cpp:679
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
EngineBuilder & setJITMemoryManager(JITMemoryManager *jmm)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1927
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
void * updateGlobalMapping(const GlobalValue *GV, void *Addr)
global_iterator global_end()
Definition: Module.h:523
iterator end()
Definition: ValueMap.h:99
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:214
bool hasExternalLinkage() const
Definition: GlobalValue.h:194
static APInt LLVM_ATTRIBUTE_UNUSED_RESULT doubleToBits(double V)
Converts a double to APInt bits.
Definition: APInt.h:1473
EngineBuilder & setRelocationModel(Reloc::Model RM)
15: SIMD 'packed' format, or other vector type
Definition: Type.h:73
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:326
opStatus add(const APFloat &, roundingMode)
Definition: APFloat.cpp:1642
static sys::Mutex * getMutex(ExecutionEngineState *EES)
Type * getType() const
Definition: Value.h:111
opStatus multiply(const APFloat &, roundingMode)
Definition: APFloat.cpp:1656
GlobalVariable * getNamedGlobal(StringRef Name)
Definition: Module.h:365
virtual Function * FindFunctionNamed(const char *FnName)
bool isNullValue() const
Definition: Constants.cpp:75
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
Class for arbitrary precision integers.
Definition: APInt.h:75
virtual void * getPointerToFunction(Function *F)=0
bool isIntegerTy() const
Definition: Type.h:196
APInt bitcastToAPInt() const
Definition: APFloat.cpp:3050
double bitsToDouble() const
Converts APInt bits to a double.
Definition: APInt.h:1446
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1840
APInt RoundFloatToAPInt(float Float, unsigned width)
Converts a float value into a APInt.
Definition: APInt.h:1776
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
bool isX86_FP80Ty() const
isX86_FP80Ty - Return true if this is x86 long double.
Definition: Type.h:152
void addGlobalMapping(const GlobalValue *GV, void *Addr)
iterator end()
Definition: Module.h:533
bool isAggregateType() const
Definition: Type.h:270
const uint64_t * getRawData() const
Definition: APInt.h:570
APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1842
bool isDeclaration() const
Definition: Globals.cpp:66
float getElementAsFloat(unsigned i) const
Definition: Constants.cpp:2484
const DataLayout * getDataLayout() const
static APInt LLVM_ATTRIBUTE_UNUSED_RESULT floatToBits(float V)
Converts a float to APInt bits.
Definition: APInt.h:1486
void * PointerTy
Definition: GenericValue.h:23
#define I(x, y, z)
Definition: MD5.cpp:54
FunctionType * getFunctionType() const
Definition: Function.cpp:171
unsigned getPointerSizeInBits(unsigned AS=0) const
Definition: DataLayout.h:271
unsigned getNumElements() const
getNumElements - Return the number of elements in the array or vector.
Definition: Constants.cpp:2217
iterator begin()
Definition: Module.h:531
SmallVector< Module *, 1 > Modules
unsigned getPrimitiveSizeInBits() const
Definition: Type.cpp:117
const GlobalValue * getGlobalValueAtAddress(void *Addr)
uint64_t getTypeStoreSize(Type *Ty) const
Definition: DataLayout.h:311
Helper class for helping synchronize access to the global address map table.
bool hasLocalLinkage() const
Definition: GlobalValue.h:211
TargetMachine * selectTarget()
Type * getElementType() const
getElementType - Return the element type of the array/vector.
Definition: Constants.cpp:2189
3: 64-bit floating point type
Definition: Type.h:58
Type * getReturnType() const
Definition: DerivedTypes.h:121
ExecutionEngineState(ExecutionEngine &EE)
bool hasDLLImportLinkage() const
Definition: GlobalValue.h:212
uint64_t getTypeSizeInBits(Type *Ty) const
Definition: DataLayout.h:459
#define DEBUG(X)
Definition: Debug.h:97
const Target & getTarget() const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:983
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
Definition: APInt.cpp:815
std::map< void *, AssertingVH< const GlobalValue > > & getGlobalAddressReverseMap(const MutexGuard &)
opStatus subtract(const APFloat &, roundingMode)
Definition: APFloat.cpp:1649
static ExecutionEngine *(* JITCtor)(Module *M, std::string *ErrorStr, JITMemoryManager *JMM, bool GVsWithCode, TargetMachine *TM)
iterator begin()
Definition: ValueMap.h:98
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
EngineBuilder & setOptLevel(CodeGenOpt::Level l)
float bitsToFloat() const
Converts APInt bits to a double.
Definition: APInt.h:1460
bool erase(const KeyT &Val)
Definition: ValueMap.h:147
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:140
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:60