LLVM  6.0.0svn
ExecutionUtils.cpp
Go to the documentation of this file.
1 //===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===//
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 
11 
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/GlobalVariable.h"
15 #include "llvm/IR/Module.h"
16 
17 namespace llvm {
18 namespace orc {
19 
21  : InitList(
22  GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
23  I((InitList && End) ? InitList->getNumOperands() : 0) {
24 }
25 
27  assert(InitList == Other.InitList && "Incomparable iterators.");
28  return I == Other.I;
29 }
30 
32  return !(*this == Other);
33 }
34 
36  ++I;
37  return *this;
38 }
39 
41  CtorDtorIterator Temp = *this;
42  ++I;
43  return Temp;
44 }
45 
47  ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
48  assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors");
49 
50  Constant *FuncC = CS->getOperand(1);
51  Function *Func = nullptr;
52 
53  // Extract function pointer, pulling off any casts.
54  while (FuncC) {
55  if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
56  Func = F;
57  break;
58  } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
59  if (CE->isCast())
60  FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
61  else
62  break;
63  } else {
64  // This isn't anything we recognize. Bail out with Func left set to null.
65  break;
66  }
67  }
68 
69  ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
70  Value *Data = CS->getOperand(2);
71  return Element(Priority->getZExtValue(), Func, Data);
72 }
73 
75  const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
76  return make_range(CtorDtorIterator(CtorsList, false),
77  CtorDtorIterator(CtorsList, true));
78 }
79 
81  const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
82  return make_range(CtorDtorIterator(DtorsList, false),
83  CtorDtorIterator(DtorsList, true));
84 }
85 
87  auto& CXXDestructorDataPairs = DSOHandleOverride;
88  for (auto &P : CXXDestructorDataPairs)
89  P.first(P.second);
90  CXXDestructorDataPairs.clear();
91 }
92 
93 int LocalCXXRuntimeOverrides::CXAAtExitOverride(DestructorPtr Destructor,
94  void *Arg, void *DSOHandle) {
95  auto& CXXDestructorDataPairs =
96  *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
97  CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
98  return 0;
99 }
100 
101 } // End namespace orc.
102 } // End namespace llvm.
Accessor for an element of the global_ctors/global_dtors array.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool operator==(const CtorDtorIterator &Other) const
Test iterators for equality.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:383
F(f)
void runDestructors()
Run any destructors recorded by the overriden __cxa_atexit function (CXAAtExitOverride).
CtorDtorIterator & operator++()
Pre-increment iterator.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:862
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
CtorDtorIterator(const GlobalVariable *GV, bool End)
Construct an iterator instance.
Value * getOperand(unsigned i) const
Definition: User.h:154
#define P(N)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static const unsigned End
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast_or_null(const Y &Val)
Definition: Casting.h:344
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
This iterator provides a convenient way to iterate over the elements of an llvm.global_ctors/llvm.global_dtors instance.
Module.h This file contains the declarations for the Module class.
A range adaptor for a pair of iterators.
ConstantArray - Constant Array Declarations.
Definition: Constants.h:405
amdgpu Simplify well known AMD library false Value Value * Arg
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Element operator*() const
Dereference iterator.
bool operator!=(const CtorDtorIterator &Other) const
Test iterators for inequality.