/JIT/llvm_state.h

http://unladen-swallow.googlecode.com/ · C Header · 217 lines · 144 code · 29 blank · 44 comment · 5 complexity · d4a53824d4d3cdcd7db8d198a7375a96 MD5 · raw file

  1. // -*- C++ -*-
  2. #ifndef PYTHON_LLVM_STATE_H
  3. #define PYTHON_LLVM_STATE_H
  4. #ifndef __cplusplus
  5. #error This header expects to be included only in C++ source
  6. #endif
  7. #include "Util/EventTimer.h"
  8. #include "JIT/PyTypeBuilder.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/Analysis/DebugInfo.h"
  11. #include "llvm/Constants.h"
  12. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  13. #include "llvm/GlobalVariable.h"
  14. #include "llvm/Support/IRBuilder.h"
  15. #include "llvm/Support/TargetFolder.h"
  16. #include "llvm/Type.h"
  17. struct PyCodeObject;
  18. struct PyGlobalLlvmData;
  19. namespace py {
  20. llvm::CallInst *
  21. TransferAttributes(llvm::CallInst *callsite, const llvm::Value* callee);
  22. // LlvmFunctionState contains objects which must only be created once
  23. // for every LLVM function. A compiled python function may contain
  24. // code from more than one code object.
  25. class LlvmFunctionState {
  26. LlvmFunctionState(const LlvmFunctionState &);
  27. void operator=(const LlvmFunctionState &);
  28. public:
  29. LlvmFunctionState(PyGlobalLlvmData *global_data, PyCodeObject *code);
  30. llvm::Function *function() { return function_; }
  31. typedef llvm::IRBuilder<true, llvm::TargetFolder> BuilderT;
  32. BuilderT& builder() { return builder_; }
  33. llvm::LLVMContext& context() { return this->context_; }
  34. llvm::Module *module() const { return this->module_; }
  35. PyGlobalLlvmData *llvm_data() const { return this->llvm_data_; }
  36. // Copies the elements from array[0] to array[N-1] to target, bytewise.
  37. void MemCpy(llvm::Value *target, llvm::Value *array, llvm::Value *N);
  38. template<typename T>
  39. llvm::Constant *GetSigned(int64_t val) {
  40. return llvm::ConstantInt::getSigned(
  41. PyTypeBuilder<T>::get(this->context_), val);
  42. }
  43. /// Get the LLVM NULL Value for the given type.
  44. template<typename T>
  45. llvm::Value *GetNull()
  46. {
  47. return llvm::Constant::getNullValue(
  48. PyTypeBuilder<T>::get(this->context_));
  49. }
  50. /// Convenience wrapper for creating named basic blocks using the current
  51. /// context and function.
  52. llvm::BasicBlock *CreateBasicBlock(const llvm::Twine &name);
  53. /// These two functions increment or decrement the reference count
  54. /// of a PyObject*. The behavior is undefined if the Value's type
  55. /// isn't PyObject* or a subclass.
  56. void IncRef(llvm::Value *value);
  57. void DecRef(llvm::Value *value);
  58. void XDecRef(llvm::Value *value);
  59. // These are just like the CreateCall* calls on IRBuilder, except they also
  60. // apply callee's calling convention and attributes to the call site.
  61. llvm::CallInst *CreateCall(llvm::Value *callee,
  62. const char *name = "");
  63. llvm::CallInst *CreateCall(llvm::Value *callee,
  64. llvm::Value *arg1,
  65. const char *name = "");
  66. llvm::CallInst *CreateCall(llvm::Value *callee,
  67. llvm::Value *arg1,
  68. llvm::Value *arg2,
  69. const char *name = "");
  70. llvm::CallInst *CreateCall(llvm::Value *callee,
  71. llvm::Value *arg1,
  72. llvm::Value *arg2,
  73. llvm::Value *arg3,
  74. const char *name = "");
  75. llvm::CallInst *CreateCall(llvm::Value *callee,
  76. llvm::Value *arg1,
  77. llvm::Value *arg2,
  78. llvm::Value *arg3,
  79. llvm::Value *arg4,
  80. const char *name = "");
  81. template<typename InputIterator>
  82. llvm::CallInst *CreateCall(llvm::Value *callee,
  83. InputIterator begin,
  84. InputIterator end,
  85. const char *name = "")
  86. {
  87. llvm::CallInst *call =
  88. this->builder().CreateCall(callee, begin, end, name);
  89. return TransferAttributes(call, callee);
  90. }
  91. // Returns the global variable with type T, address 'var_address',
  92. // and name 'name'. If the ExecutionEngine already knows of a
  93. // variable with the given address, we name and return it.
  94. // Otherwise the variable will be looked up in Python's C runtime.
  95. template<typename VariableType>
  96. llvm::Constant *GetGlobalVariable(
  97. void *var_address, const std::string &name);
  98. // Returns the global function with type T and name 'name'. The
  99. // function will be looked up in Python's C runtime.
  100. template<typename FunctionType>
  101. llvm::Function *GetGlobalFunction(const std::string &name)
  102. {
  103. return llvm::cast<llvm::Function>(
  104. this->module()->getOrInsertFunction(name,
  105. PyTypeBuilder<FunctionType>::get(this->context_)));
  106. }
  107. // Returns a global variable that represents 'obj'. These get
  108. // cached in the ExecutionEngine's global mapping table, and they
  109. // incref the object so its address doesn't get re-used while the
  110. // GlobalVariable is still alive. See JIT/ConstantMirror.h for
  111. // more details. Use this in preference to GetGlobalVariable()
  112. // for PyObjects that may be immutable.
  113. llvm::Constant *GetGlobalVariableFor(PyObject *obj);
  114. // Returns an i1, true if value represents a NULL pointer.
  115. llvm::Value *IsNull(llvm::Value *value);
  116. // Returns an i1, true if value is a negative integer.
  117. llvm::Value *IsNegative(llvm::Value *value);
  118. // Returns an i1, true if value is a non-zero integer.
  119. llvm::Value *IsNonZero(llvm::Value *value);
  120. // Returns an i1, true if value is a positive (>0) integer.
  121. llvm::Value *IsPositive(llvm::Value *value);
  122. // Returns an i1, true if value is an instance of the class
  123. // represented by the flag argument. flag should be something
  124. // like Py_TPFLAGS_INT_SUBCLASS.
  125. llvm::Value *IsInstanceOfFlagClass(llvm::Value *value, int flag);
  126. /// Implements something like the C assert statement. If
  127. /// should_be_true (an i1) is false, prints failure_message (with
  128. /// puts) and aborts. Compiles to nothing in optimized mode.
  129. void Assert(llvm::Value *should_be_true,
  130. const std::string &failure_message);
  131. /// Prints failure_message (with puts) and aborts.
  132. void Abort(const std::string &failure_message);
  133. // Get the address of the idx'th item in a list or tuple object.
  134. llvm::Value *GetListItemSlot(llvm::Value *lst, int idx);
  135. llvm::Value *GetTupleItemSlot(llvm::Value *tup, int idx);
  136. #ifdef WITH_TSC
  137. // Emit code to record a given event with the TSC EventTimer.h system.
  138. void LogTscEvent(_PyTscEventId event_id);
  139. #endif
  140. // Create an alloca in the entry block, so that LLVM can optimize
  141. // it more easily, and return the resulting address. The signature
  142. // matches IRBuilder.CreateAlloca()'s.
  143. llvm::Value *CreateAllocaInEntryBlock(
  144. const llvm::Type *alloca_type,
  145. llvm::Value *array_size,
  146. const char *name);
  147. /// Embed a pointer of some type directly into the LLVM IR.
  148. template <typename T>
  149. llvm::Value *EmbedPointer(void *ptr)
  150. {
  151. // We assume that the caller has ensured that ptr will stay live for the
  152. // life of this native code object.
  153. return this->builder_.CreateIntToPtr(
  154. llvm::ConstantInt::get(llvm::Type::getInt64Ty(this->context_),
  155. reinterpret_cast<intptr_t>(ptr)),
  156. PyTypeBuilder<T>::get(this->context_));
  157. }
  158. private:
  159. PyGlobalLlvmData *const llvm_data_;
  160. llvm::LLVMContext &context_;
  161. llvm::Module *const module_;
  162. llvm::Function *const function_;
  163. BuilderT builder_;
  164. };
  165. template<typename VariableType> llvm::Constant *
  166. LlvmFunctionState::GetGlobalVariable(void *var_address, const std::string &name)
  167. {
  168. const llvm::Type *expected_type =
  169. PyTypeBuilder<VariableType>::get(this->context_);
  170. if (llvm::GlobalVariable *global = this->module_->getNamedGlobal(name)) {
  171. assert (expected_type == global->getType()->getElementType());
  172. return global;
  173. }
  174. if (llvm::GlobalValue *global = const_cast<llvm::GlobalValue*>(
  175. this->llvm_data_->getExecutionEngine()->
  176. getGlobalValueAtAddress(var_address))) {
  177. assert (expected_type == global->getType()->getElementType());
  178. if (!global->hasName())
  179. global->setName(name);
  180. return global;
  181. }
  182. return new llvm::GlobalVariable(*this->module_, expected_type,
  183. /*isConstant=*/false,
  184. llvm::GlobalValue::ExternalLinkage,
  185. NULL, name);
  186. }
  187. } // namespace py
  188. #endif // PYTHON_LLVM_STATE_H