IRBuilder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
  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 implements the IRBuilder class, which is used as a convenient way
  11. // to create LLVM instructions with a consistent and simplified interface.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/Function.h"
  15. #include "llvm/IR/GlobalVariable.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/IR/Intrinsics.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/Statepoint.h"
  20. using namespace llvm;
  21. /// CreateGlobalString - Make a new global variable with an initializer that
  22. /// has array of i8 type filled in with the nul terminated string value
  23. /// specified. If Name is specified, it is the name of the global variable
  24. /// created.
  25. GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
  26. const Twine &Name,
  27. unsigned AddressSpace) {
  28. Constant *StrConstant = ConstantDataArray::getString(Context, Str);
  29. Module &M = *BB->getParent()->getParent();
  30. GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
  31. true, GlobalValue::PrivateLinkage,
  32. StrConstant, Name, nullptr,
  33. GlobalVariable::NotThreadLocal,
  34. AddressSpace);
  35. GV->setUnnamedAddr(true);
  36. return GV;
  37. }
  38. Type *IRBuilderBase::getCurrentFunctionReturnType() const {
  39. assert(BB && BB->getParent() && "No current function!");
  40. return BB->getParent()->getReturnType();
  41. }
  42. Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
  43. PointerType *PT = cast<PointerType>(Ptr->getType());
  44. if (PT->getElementType()->isIntegerTy(8))
  45. return Ptr;
  46. // Otherwise, we need to insert a bitcast.
  47. PT = getInt8PtrTy(PT->getAddressSpace());
  48. BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
  49. BB->getInstList().insert(InsertPt, BCI);
  50. SetInstDebugLocation(BCI);
  51. return BCI;
  52. }
  53. static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops,
  54. IRBuilderBase *Builder,
  55. const Twine& Name="") {
  56. CallInst *CI = CallInst::Create(Callee, Ops, Name);
  57. Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
  58. Builder->SetInstDebugLocation(CI);
  59. return CI;
  60. }
  61. static InvokeInst *createInvokeHelper(Value *Invokee, BasicBlock *NormalDest,
  62. BasicBlock *UnwindDest,
  63. ArrayRef<Value *> Ops,
  64. IRBuilderBase *Builder,
  65. const Twine &Name = "") {
  66. InvokeInst *II =
  67. InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name);
  68. Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),
  69. II);
  70. Builder->SetInstDebugLocation(II);
  71. return II;
  72. }
  73. CallInst *IRBuilderBase::
  74. CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
  75. bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
  76. MDNode *NoAliasTag) {
  77. Ptr = getCastedInt8PtrValue(Ptr);
  78. Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) };
  79. Type *Tys[] = { Ptr->getType(), Size->getType() };
  80. Module *M = BB->getParent()->getParent();
  81. Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
  82. CallInst *CI = createCallHelper(TheFn, Ops, this);
  83. // Set the TBAA info if present.
  84. if (TBAATag)
  85. CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
  86. if (ScopeTag)
  87. CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
  88. if (NoAliasTag)
  89. CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
  90. return CI;
  91. }
  92. CallInst *IRBuilderBase::
  93. CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
  94. bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag,
  95. MDNode *ScopeTag, MDNode *NoAliasTag) {
  96. Dst = getCastedInt8PtrValue(Dst);
  97. Src = getCastedInt8PtrValue(Src);
  98. Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
  99. Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
  100. Module *M = BB->getParent()->getParent();
  101. Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys);
  102. CallInst *CI = createCallHelper(TheFn, Ops, this);
  103. // Set the TBAA info if present.
  104. if (TBAATag)
  105. CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
  106. // Set the TBAA Struct info if present.
  107. if (TBAAStructTag)
  108. CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag);
  109. if (ScopeTag)
  110. CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
  111. if (NoAliasTag)
  112. CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
  113. return CI;
  114. }
  115. CallInst *IRBuilderBase::
  116. CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
  117. bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag,
  118. MDNode *NoAliasTag) {
  119. Dst = getCastedInt8PtrValue(Dst);
  120. Src = getCastedInt8PtrValue(Src);
  121. Value *Ops[] = { Dst, Src, Size, getInt32(Align), getInt1(isVolatile) };
  122. Type *Tys[] = { Dst->getType(), Src->getType(), Size->getType() };
  123. Module *M = BB->getParent()->getParent();
  124. Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::memmove, Tys);
  125. CallInst *CI = createCallHelper(TheFn, Ops, this);
  126. // Set the TBAA info if present.
  127. if (TBAATag)
  128. CI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
  129. if (ScopeTag)
  130. CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag);
  131. if (NoAliasTag)
  132. CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag);
  133. return CI;
  134. }
  135. CallInst *IRBuilderBase::CreateLifetimeStart(Value *Ptr, ConstantInt *Size) {
  136. assert(isa<PointerType>(Ptr->getType()) &&
  137. "lifetime.start only applies to pointers.");
  138. Ptr = getCastedInt8PtrValue(Ptr);
  139. if (!Size)
  140. Size = getInt64(-1);
  141. else
  142. assert(Size->getType() == getInt64Ty() &&
  143. "lifetime.start requires the size to be an i64");
  144. Value *Ops[] = { Size, Ptr };
  145. Module *M = BB->getParent()->getParent();
  146. Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_start);
  147. return createCallHelper(TheFn, Ops, this);
  148. }
  149. CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) {
  150. assert(isa<PointerType>(Ptr->getType()) &&
  151. "lifetime.end only applies to pointers.");
  152. Ptr = getCastedInt8PtrValue(Ptr);
  153. if (!Size)
  154. Size = getInt64(-1);
  155. else
  156. assert(Size->getType() == getInt64Ty() &&
  157. "lifetime.end requires the size to be an i64");
  158. Value *Ops[] = { Size, Ptr };
  159. Module *M = BB->getParent()->getParent();
  160. Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end);
  161. return createCallHelper(TheFn, Ops, this);
  162. }
  163. CallInst *IRBuilderBase::CreateAssumption(Value *Cond) {
  164. assert(Cond->getType() == getInt1Ty() &&
  165. "an assumption condition must be of type i1");
  166. Value *Ops[] = { Cond };
  167. Module *M = BB->getParent()->getParent();
  168. Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
  169. return createCallHelper(FnAssume, Ops, this);
  170. }
  171. /// Create a call to a Masked Load intrinsic.
  172. /// Ptr - the base pointer for the load
  173. /// Align - alignment of the source location
  174. /// Mask - an vector of booleans which indicates what vector lanes should
  175. /// be accessed in memory
  176. /// PassThru - a pass-through value that is used to fill the masked-off lanes
  177. /// of the result
  178. /// Name - name of the result variable
  179. CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align,
  180. Value *Mask, Value *PassThru,
  181. const Twine &Name) {
  182. assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type");
  183. // DataTy is the overloaded type
  184. Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
  185. assert(DataTy->isVectorTy() && "Ptr should point to a vector");
  186. if (!PassThru)
  187. PassThru = UndefValue::get(DataTy);
  188. Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru};
  189. return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name);
  190. }
  191. /// Create a call to a Masked Store intrinsic.
  192. /// Val - the data to be stored,
  193. /// Ptr - the base pointer for the store
  194. /// Align - alignment of the destination location
  195. /// Mask - an vector of booleans which indicates what vector lanes should
  196. /// be accessed in memory
  197. CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr,
  198. unsigned Align, Value *Mask) {
  199. Value *Ops[] = { Val, Ptr, getInt32(Align), Mask };
  200. // Type of the data to be stored - the only one overloaded type
  201. return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType());
  202. }
  203. /// Create a call to a Masked intrinsic, with given intrinsic Id,
  204. /// an array of operands - Ops, and one overloaded type - DataTy
  205. CallInst *IRBuilderBase::CreateMaskedIntrinsic(Intrinsic::ID Id,
  206. ArrayRef<Value *> Ops,
  207. Type *DataTy,
  208. const Twine &Name) {
  209. Module *M = BB->getParent()->getParent();
  210. Type *OverloadedTypes[] = { DataTy };
  211. Value *TheFn = Intrinsic::getDeclaration(M, Id, OverloadedTypes);
  212. return createCallHelper(TheFn, Ops, this, Name);
  213. }
  214. static std::vector<Value *>
  215. getStatepointArgs(IRBuilderBase &B, uint64_t ID, uint32_t NumPatchBytes,
  216. Value *ActualCallee, ArrayRef<Value *> CallArgs,
  217. ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs) {
  218. std::vector<Value *> Args;
  219. Args.push_back(B.getInt64(ID));
  220. Args.push_back(B.getInt32(NumPatchBytes));
  221. Args.push_back(ActualCallee);
  222. Args.push_back(B.getInt32(CallArgs.size()));
  223. Args.push_back(B.getInt32((unsigned)StatepointFlags::None));
  224. Args.insert(Args.end(), CallArgs.begin(), CallArgs.end());
  225. Args.push_back(B.getInt32(0 /* no transition args */));
  226. Args.push_back(B.getInt32(DeoptArgs.size()));
  227. Args.insert(Args.end(), DeoptArgs.begin(), DeoptArgs.end());
  228. Args.insert(Args.end(), GCArgs.begin(), GCArgs.end());
  229. return Args;
  230. }
  231. CallInst *IRBuilderBase::CreateGCStatepointCall(
  232. uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
  233. ArrayRef<Value *> CallArgs, ArrayRef<Value *> DeoptArgs,
  234. ArrayRef<Value *> GCArgs, const Twine &Name) {
  235. // Extract out the type of the callee.
  236. PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType());
  237. assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
  238. "actual callee must be a callable value");
  239. Module *M = BB->getParent()->getParent();
  240. // Fill in the one generic type'd argument (the function is also vararg)
  241. Type *ArgTypes[] = { FuncPtrType };
  242. Function *FnStatepoint =
  243. Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint,
  244. ArgTypes);
  245. std::vector<llvm::Value *> Args = getStatepointArgs(
  246. *this, ID, NumPatchBytes, ActualCallee, CallArgs, DeoptArgs, GCArgs);
  247. return createCallHelper(FnStatepoint, Args, this, Name);
  248. }
  249. CallInst *IRBuilderBase::CreateGCStatepointCall(
  250. uint64_t ID, uint32_t NumPatchBytes, Value *ActualCallee,
  251. ArrayRef<Use> CallArgs, ArrayRef<Value *> DeoptArgs,
  252. ArrayRef<Value *> GCArgs, const Twine &Name) {
  253. std::vector<Value *> VCallArgs;
  254. for (auto &U : CallArgs)
  255. VCallArgs.push_back(U.get());
  256. return CreateGCStatepointCall(ID, NumPatchBytes, ActualCallee, VCallArgs,
  257. DeoptArgs, GCArgs, Name);
  258. }
  259. InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
  260. uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
  261. BasicBlock *NormalDest, BasicBlock *UnwindDest,
  262. ArrayRef<Value *> InvokeArgs, ArrayRef<Value *> DeoptArgs,
  263. ArrayRef<Value *> GCArgs, const Twine &Name) {
  264. // Extract out the type of the callee.
  265. PointerType *FuncPtrType = cast<PointerType>(ActualInvokee->getType());
  266. assert(isa<FunctionType>(FuncPtrType->getElementType()) &&
  267. "actual callee must be a callable value");
  268. Module *M = BB->getParent()->getParent();
  269. // Fill in the one generic type'd argument (the function is also vararg)
  270. Function *FnStatepoint = Intrinsic::getDeclaration(
  271. M, Intrinsic::experimental_gc_statepoint, {FuncPtrType});
  272. std::vector<llvm::Value *> Args = getStatepointArgs(
  273. *this, ID, NumPatchBytes, ActualInvokee, InvokeArgs, DeoptArgs, GCArgs);
  274. return createInvokeHelper(FnStatepoint, NormalDest, UnwindDest, Args, this,
  275. Name);
  276. }
  277. InvokeInst *IRBuilderBase::CreateGCStatepointInvoke(
  278. uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
  279. BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
  280. ArrayRef<Value *> DeoptArgs, ArrayRef<Value *> GCArgs, const Twine &Name) {
  281. std::vector<Value *> VCallArgs;
  282. for (auto &U : InvokeArgs)
  283. VCallArgs.push_back(U.get());
  284. return CreateGCStatepointInvoke(ID, NumPatchBytes, ActualInvokee, NormalDest,
  285. UnwindDest, VCallArgs, DeoptArgs, GCArgs,
  286. Name);
  287. }
  288. CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint,
  289. Type *ResultType,
  290. const Twine &Name) {
  291. Intrinsic::ID ID = Intrinsic::experimental_gc_result;
  292. Module *M = BB->getParent()->getParent();
  293. Type *Types[] = {ResultType};
  294. Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types);
  295. Value *Args[] = {Statepoint};
  296. return createCallHelper(FnGCResult, Args, this, Name);
  297. }
  298. CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint,
  299. int BaseOffset,
  300. int DerivedOffset,
  301. Type *ResultType,
  302. const Twine &Name) {
  303. Module *M = BB->getParent()->getParent();
  304. Type *Types[] = {ResultType};
  305. Value *FnGCRelocate =
  306. Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
  307. Value *Args[] = {Statepoint,
  308. getInt32(BaseOffset),
  309. getInt32(DerivedOffset)};
  310. return createCallHelper(FnGCRelocate, Args, this, Name);
  311. }