IRBuilder.h 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===//
  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 IRBuilder class, which is used as a convenient way
  11. // to create LLVM instructions with a consistent and simplified interface.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_IRBUILDER_H
  15. #define LLVM_IR_IRBUILDER_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Twine.h"
  19. #include "llvm/IR/BasicBlock.h"
  20. #include "llvm/IR/ConstantFolder.h"
  21. #include "llvm/IR/DataLayout.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/GlobalVariable.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/LLVMContext.h"
  26. #include "llvm/IR/Operator.h"
  27. #include "llvm/IR/ValueHandle.h"
  28. #include "llvm/Support/CBindingWrapping.h"
  29. namespace llvm {
  30. class MDNode;
  31. /// \brief This provides the default implementation of the IRBuilder
  32. /// 'InsertHelper' method that is called whenever an instruction is created by
  33. /// IRBuilder and needs to be inserted.
  34. ///
  35. /// By default, this inserts the instruction at the insertion point.
  36. template <bool preserveNames = true>
  37. class IRBuilderDefaultInserter {
  38. protected:
  39. void InsertHelper(Instruction *I, const Twine &Name,
  40. BasicBlock *BB, BasicBlock::iterator InsertPt) const {
  41. if (BB) BB->getInstList().insert(InsertPt, I);
  42. if (preserveNames)
  43. I->setName(Name);
  44. }
  45. };
  46. /// \brief Common base class shared among various IRBuilders.
  47. class IRBuilderBase {
  48. DebugLoc CurDbgLocation;
  49. protected:
  50. BasicBlock *BB;
  51. BasicBlock::iterator InsertPt;
  52. LLVMContext &Context;
  53. MDNode *DefaultFPMathTag;
  54. FastMathFlags FMF;
  55. public:
  56. IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
  57. : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
  58. ClearInsertionPoint();
  59. }
  60. //===--------------------------------------------------------------------===//
  61. // Builder configuration methods
  62. //===--------------------------------------------------------------------===//
  63. /// \brief Clear the insertion point: created instructions will not be
  64. /// inserted into a block.
  65. void ClearInsertionPoint() {
  66. BB = nullptr;
  67. InsertPt = nullptr;
  68. }
  69. BasicBlock *GetInsertBlock() const { return BB; }
  70. BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
  71. LLVMContext &getContext() const { return Context; }
  72. /// \brief This specifies that created instructions should be appended to the
  73. /// end of the specified block.
  74. void SetInsertPoint(BasicBlock *TheBB) {
  75. BB = TheBB;
  76. InsertPt = BB->end();
  77. }
  78. /// \brief This specifies that created instructions should be inserted before
  79. /// the specified instruction.
  80. void SetInsertPoint(Instruction *I) {
  81. BB = I->getParent();
  82. InsertPt = I;
  83. assert(I != BB->end() && "Can't read debug loc from end()");
  84. SetCurrentDebugLocation(I->getDebugLoc());
  85. }
  86. /// \brief This specifies that created instructions should be inserted at the
  87. /// specified point.
  88. void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
  89. BB = TheBB;
  90. InsertPt = IP;
  91. if (IP != TheBB->end())
  92. SetCurrentDebugLocation(IP->getDebugLoc());
  93. }
  94. /// \brief Set location information used by debugging information.
  95. void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
  96. /// \brief Get location information used by debugging information.
  97. const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
  98. /// \brief If this builder has a current debug location, set it on the
  99. /// specified instruction.
  100. void SetInstDebugLocation(Instruction *I) const {
  101. if (CurDbgLocation)
  102. I->setDebugLoc(CurDbgLocation);
  103. }
  104. /// \brief Get the return type of the current function that we're emitting
  105. /// into.
  106. Type *getCurrentFunctionReturnType() const;
  107. /// InsertPoint - A saved insertion point.
  108. class InsertPoint {
  109. BasicBlock *Block;
  110. BasicBlock::iterator Point;
  111. public:
  112. /// \brief Creates a new insertion point which doesn't point to anything.
  113. InsertPoint() : Block(nullptr) {}
  114. /// \brief Creates a new insertion point at the given location.
  115. InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
  116. : Block(InsertBlock), Point(InsertPoint) {}
  117. /// \brief Returns true if this insert point is set.
  118. bool isSet() const { return (Block != nullptr); }
  119. llvm::BasicBlock *getBlock() const { return Block; }
  120. llvm::BasicBlock::iterator getPoint() const { return Point; }
  121. };
  122. /// \brief Returns the current insert point.
  123. InsertPoint saveIP() const {
  124. return InsertPoint(GetInsertBlock(), GetInsertPoint());
  125. }
  126. /// \brief Returns the current insert point, clearing it in the process.
  127. InsertPoint saveAndClearIP() {
  128. InsertPoint IP(GetInsertBlock(), GetInsertPoint());
  129. ClearInsertionPoint();
  130. return IP;
  131. }
  132. /// \brief Sets the current insert point to a previously-saved location.
  133. void restoreIP(InsertPoint IP) {
  134. if (IP.isSet())
  135. SetInsertPoint(IP.getBlock(), IP.getPoint());
  136. else
  137. ClearInsertionPoint();
  138. }
  139. /// \brief Get the floating point math metadata being used.
  140. MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
  141. /// \brief Get the flags to be applied to created floating point ops
  142. FastMathFlags getFastMathFlags() const { return FMF; }
  143. /// \brief Clear the fast-math flags.
  144. void clearFastMathFlags() { FMF.clear(); }
  145. /// \brief Set the floating point math metadata to be used.
  146. void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
  147. /// \brief Set the fast-math flags to be used with generated fp-math operators
  148. void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
  149. //===--------------------------------------------------------------------===//
  150. // RAII helpers.
  151. //===--------------------------------------------------------------------===//
  152. // \brief RAII object that stores the current insertion point and restores it
  153. // when the object is destroyed. This includes the debug location.
  154. class InsertPointGuard {
  155. IRBuilderBase &Builder;
  156. AssertingVH<BasicBlock> Block;
  157. BasicBlock::iterator Point;
  158. DebugLoc DbgLoc;
  159. InsertPointGuard(const InsertPointGuard &) = delete;
  160. InsertPointGuard &operator=(const InsertPointGuard &) = delete;
  161. public:
  162. InsertPointGuard(IRBuilderBase &B)
  163. : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
  164. DbgLoc(B.getCurrentDebugLocation()) {}
  165. ~InsertPointGuard() {
  166. Builder.restoreIP(InsertPoint(Block, Point));
  167. Builder.SetCurrentDebugLocation(DbgLoc);
  168. }
  169. };
  170. // \brief RAII object that stores the current fast math settings and restores
  171. // them when the object is destroyed.
  172. class FastMathFlagGuard {
  173. IRBuilderBase &Builder;
  174. FastMathFlags FMF;
  175. MDNode *FPMathTag;
  176. FastMathFlagGuard(const FastMathFlagGuard &) = delete;
  177. FastMathFlagGuard &operator=(
  178. const FastMathFlagGuard &) = delete;
  179. public:
  180. FastMathFlagGuard(IRBuilderBase &B)
  181. : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
  182. ~FastMathFlagGuard() {
  183. Builder.FMF = FMF;
  184. Builder.DefaultFPMathTag = FPMathTag;
  185. }
  186. };
  187. //===--------------------------------------------------------------------===//
  188. // Miscellaneous creation methods.
  189. //===--------------------------------------------------------------------===//
  190. /// \brief Make a new global variable with initializer type i8*
  191. ///
  192. /// Make a new global variable with an initializer that has array of i8 type
  193. /// filled in with the null terminated string value specified. The new global
  194. /// variable will be marked mergable with any others of the same contents. If
  195. /// Name is specified, it is the name of the global variable created.
  196. GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
  197. unsigned AddressSpace = 0);
  198. /// \brief Get a constant value representing either true or false.
  199. ConstantInt *getInt1(bool V) {
  200. return ConstantInt::get(getInt1Ty(), V);
  201. }
  202. /// \brief Get the constant value for i1 true.
  203. ConstantInt *getTrue() {
  204. return ConstantInt::getTrue(Context);
  205. }
  206. /// \brief Get the constant value for i1 false.
  207. ConstantInt *getFalse() {
  208. return ConstantInt::getFalse(Context);
  209. }
  210. /// \brief Get a constant 8-bit value.
  211. ConstantInt *getInt8(uint8_t C) {
  212. return ConstantInt::get(getInt8Ty(), C);
  213. }
  214. /// \brief Get a constant 16-bit value.
  215. ConstantInt *getInt16(uint16_t C) {
  216. return ConstantInt::get(getInt16Ty(), C);
  217. }
  218. /// \brief Get a constant 32-bit value.
  219. ConstantInt *getInt32(uint32_t C) {
  220. return ConstantInt::get(getInt32Ty(), C);
  221. }
  222. /// \brief Get a constant 64-bit value.
  223. ConstantInt *getInt64(uint64_t C) {
  224. return ConstantInt::get(getInt64Ty(), C);
  225. }
  226. /// \brief Get a constant N-bit value, zero extended or truncated from
  227. /// a 64-bit value.
  228. ConstantInt *getIntN(unsigned N, uint64_t C) {
  229. return ConstantInt::get(getIntNTy(N), C);
  230. }
  231. /// \brief Get a constant integer value.
  232. ConstantInt *getInt(const APInt &AI) {
  233. return ConstantInt::get(Context, AI);
  234. }
  235. //===--------------------------------------------------------------------===//
  236. // Type creation methods
  237. //===--------------------------------------------------------------------===//
  238. /// \brief Fetch the type representing a single bit
  239. IntegerType *getInt1Ty() {
  240. return Type::getInt1Ty(Context);
  241. }
  242. /// \brief Fetch the type representing an 8-bit integer.
  243. IntegerType *getInt8Ty() {
  244. return Type::getInt8Ty(Context);
  245. }
  246. /// \brief Fetch the type representing a 16-bit integer.
  247. IntegerType *getInt16Ty() {
  248. return Type::getInt16Ty(Context);
  249. }
  250. /// \brief Fetch the type representing a 32-bit integer.
  251. IntegerType *getInt32Ty() {
  252. return Type::getInt32Ty(Context);
  253. }
  254. /// \brief Fetch the type representing a 64-bit integer.
  255. IntegerType *getInt64Ty() {
  256. return Type::getInt64Ty(Context);
  257. }
  258. /// \brief Fetch the type representing a 128-bit integer.
  259. IntegerType *getInt128Ty() {
  260. return Type::getInt128Ty(Context);
  261. }
  262. /// \brief Fetch the type representing an N-bit integer.
  263. IntegerType *getIntNTy(unsigned N) {
  264. return Type::getIntNTy(Context, N);
  265. }
  266. /// \brief Fetch the type representing a 16-bit floating point value.
  267. Type *getHalfTy() {
  268. return Type::getHalfTy(Context);
  269. }
  270. /// \brief Fetch the type representing a 32-bit floating point value.
  271. Type *getFloatTy() {
  272. return Type::getFloatTy(Context);
  273. }
  274. /// \brief Fetch the type representing a 64-bit floating point value.
  275. Type *getDoubleTy() {
  276. return Type::getDoubleTy(Context);
  277. }
  278. /// \brief Fetch the type representing void.
  279. Type *getVoidTy() {
  280. return Type::getVoidTy(Context);
  281. }
  282. /// \brief Fetch the type representing a pointer to an 8-bit integer value.
  283. PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
  284. return Type::getInt8PtrTy(Context, AddrSpace);
  285. }
  286. /// \brief Fetch the type representing a pointer to an integer value.
  287. IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
  288. return DL.getIntPtrType(Context, AddrSpace);
  289. }
  290. //===--------------------------------------------------------------------===//
  291. // Intrinsic creation methods
  292. //===--------------------------------------------------------------------===//
  293. /// \brief Create and insert a memset to the specified pointer and the
  294. /// specified value.
  295. ///
  296. /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
  297. /// specified, it will be added to the instruction. Likewise with alias.scope
  298. /// and noalias tags.
  299. CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
  300. bool isVolatile = false, MDNode *TBAATag = nullptr,
  301. MDNode *ScopeTag = nullptr,
  302. MDNode *NoAliasTag = nullptr) {
  303. return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
  304. TBAATag, ScopeTag, NoAliasTag);
  305. }
  306. CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
  307. bool isVolatile = false, MDNode *TBAATag = nullptr,
  308. MDNode *ScopeTag = nullptr,
  309. MDNode *NoAliasTag = nullptr);
  310. /// \brief Create and insert a memcpy between the specified pointers.
  311. ///
  312. /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
  313. /// specified, it will be added to the instruction. Likewise with alias.scope
  314. /// and noalias tags.
  315. CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
  316. bool isVolatile = false, MDNode *TBAATag = nullptr,
  317. MDNode *TBAAStructTag = nullptr,
  318. MDNode *ScopeTag = nullptr,
  319. MDNode *NoAliasTag = nullptr) {
  320. return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
  321. TBAAStructTag, ScopeTag, NoAliasTag);
  322. }
  323. CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
  324. bool isVolatile = false, MDNode *TBAATag = nullptr,
  325. MDNode *TBAAStructTag = nullptr,
  326. MDNode *ScopeTag = nullptr,
  327. MDNode *NoAliasTag = nullptr);
  328. /// \brief Create and insert a memmove between the specified
  329. /// pointers.
  330. ///
  331. /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
  332. /// specified, it will be added to the instruction. Likewise with alias.scope
  333. /// and noalias tags.
  334. CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
  335. bool isVolatile = false, MDNode *TBAATag = nullptr,
  336. MDNode *ScopeTag = nullptr,
  337. MDNode *NoAliasTag = nullptr) {
  338. return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
  339. TBAATag, ScopeTag, NoAliasTag);
  340. }
  341. CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
  342. bool isVolatile = false, MDNode *TBAATag = nullptr,
  343. MDNode *ScopeTag = nullptr,
  344. MDNode *NoAliasTag = nullptr);
  345. /// \brief Create a lifetime.start intrinsic.
  346. ///
  347. /// If the pointer isn't i8* it will be converted.
  348. CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
  349. /// \brief Create a lifetime.end intrinsic.
  350. ///
  351. /// If the pointer isn't i8* it will be converted.
  352. CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
  353. /// \brief Create a call to Masked Load intrinsic
  354. CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
  355. Value *PassThru = 0, const Twine &Name = "");
  356. /// \brief Create a call to Masked Store intrinsic
  357. CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
  358. Value *Mask);
  359. /// \brief Create an assume intrinsic call that allows the optimizer to
  360. /// assume that the provided condition will be true.
  361. CallInst *CreateAssumption(Value *Cond);
  362. /// \brief Create a call to the experimental.gc.statepoint intrinsic to
  363. /// start a new statepoint sequence.
  364. CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
  365. Value *ActualCallee,
  366. ArrayRef<Value *> CallArgs,
  367. ArrayRef<Value *> DeoptArgs,
  368. ArrayRef<Value *> GCArgs,
  369. const Twine &Name = "");
  370. // \brief Conveninence function for the common case when CallArgs are filled
  371. // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
  372. // .get()'ed to get the Value pointer.
  373. CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
  374. Value *ActualCallee, ArrayRef<Use> CallArgs,
  375. ArrayRef<Value *> DeoptArgs,
  376. ArrayRef<Value *> GCArgs,
  377. const Twine &Name = "");
  378. /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
  379. /// start a new statepoint sequence.
  380. InvokeInst *
  381. CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
  382. Value *ActualInvokee, BasicBlock *NormalDest,
  383. BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
  384. ArrayRef<Value *> DeoptArgs,
  385. ArrayRef<Value *> GCArgs, const Twine &Name = "");
  386. // Conveninence function for the common case when CallArgs are filled in using
  387. // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
  388. // get the Value *.
  389. InvokeInst *
  390. CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
  391. Value *ActualInvokee, BasicBlock *NormalDest,
  392. BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
  393. ArrayRef<Value *> DeoptArgs,
  394. ArrayRef<Value *> GCArgs, const Twine &Name = "");
  395. /// \brief Create a call to the experimental.gc.result intrinsic to extract
  396. /// the result from a call wrapped in a statepoint.
  397. CallInst *CreateGCResult(Instruction *Statepoint,
  398. Type *ResultType,
  399. const Twine &Name = "");
  400. /// \brief Create a call to the experimental.gc.relocate intrinsics to
  401. /// project the relocated value of one pointer from the statepoint.
  402. CallInst *CreateGCRelocate(Instruction *Statepoint,
  403. int BaseOffset,
  404. int DerivedOffset,
  405. Type *ResultType,
  406. const Twine &Name = "");
  407. private:
  408. /// \brief Create a call to a masked intrinsic with given Id.
  409. /// Masked intrinsic has only one overloaded type - data type.
  410. CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
  411. Type *DataTy, const Twine &Name = "");
  412. Value *getCastedInt8PtrValue(Value *Ptr);
  413. };
  414. /// \brief This provides a uniform API for creating instructions and inserting
  415. /// them into a basic block: either at the end of a BasicBlock, or at a specific
  416. /// iterator location in a block.
  417. ///
  418. /// Note that the builder does not expose the full generality of LLVM
  419. /// instructions. For access to extra instruction properties, use the mutators
  420. /// (e.g. setVolatile) on the instructions after they have been
  421. /// created. Convenience state exists to specify fast-math flags and fp-math
  422. /// tags.
  423. ///
  424. /// The first template argument handles whether or not to preserve names in the
  425. /// final instruction output. This defaults to on. The second template argument
  426. /// specifies a class to use for creating constants. This defaults to creating
  427. /// minimally folded constants. The third template argument allows clients to
  428. /// specify custom insertion hooks that are called on every newly created
  429. /// insertion.
  430. template<bool preserveNames = true, typename T = ConstantFolder,
  431. typename Inserter = IRBuilderDefaultInserter<preserveNames> >
  432. class IRBuilder : public IRBuilderBase, public Inserter {
  433. T Folder;
  434. public:
  435. IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
  436. MDNode *FPMathTag = nullptr)
  437. : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
  438. }
  439. explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
  440. : IRBuilderBase(C, FPMathTag), Folder() {
  441. }
  442. explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
  443. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
  444. SetInsertPoint(TheBB);
  445. }
  446. explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
  447. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
  448. SetInsertPoint(TheBB);
  449. }
  450. explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
  451. : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
  452. SetInsertPoint(IP);
  453. }
  454. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
  455. MDNode *FPMathTag = nullptr)
  456. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
  457. SetInsertPoint(TheBB, IP);
  458. }
  459. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
  460. MDNode *FPMathTag = nullptr)
  461. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
  462. SetInsertPoint(TheBB, IP);
  463. }
  464. /// \brief Get the constant folder being used.
  465. const T &getFolder() { return Folder; }
  466. /// \brief Return true if this builder is configured to actually add the
  467. /// requested names to IR created through it.
  468. bool isNamePreserving() const { return preserveNames; }
  469. /// \brief Insert and return the specified instruction.
  470. template<typename InstTy>
  471. InstTy *Insert(InstTy *I, const Twine &Name = "") const {
  472. this->InsertHelper(I, Name, BB, InsertPt);
  473. this->SetInstDebugLocation(I);
  474. return I;
  475. }
  476. /// \brief No-op overload to handle constants.
  477. Constant *Insert(Constant *C, const Twine& = "") const {
  478. return C;
  479. }
  480. //===--------------------------------------------------------------------===//
  481. // Instruction creation methods: Terminators
  482. //===--------------------------------------------------------------------===//
  483. private:
  484. /// \brief Helper to add branch weight metadata onto an instruction.
  485. /// \returns The annotated instruction.
  486. template <typename InstTy>
  487. InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
  488. if (Weights)
  489. I->setMetadata(LLVMContext::MD_prof, Weights);
  490. return I;
  491. }
  492. public:
  493. /// \brief Create a 'ret void' instruction.
  494. ReturnInst *CreateRetVoid() {
  495. return Insert(ReturnInst::Create(Context));
  496. }
  497. /// \brief Create a 'ret <val>' instruction.
  498. ReturnInst *CreateRet(Value *V) {
  499. return Insert(ReturnInst::Create(Context, V));
  500. }
  501. /// \brief Create a sequence of N insertvalue instructions,
  502. /// with one Value from the retVals array each, that build a aggregate
  503. /// return value one value at a time, and a ret instruction to return
  504. /// the resulting aggregate value.
  505. ///
  506. /// This is a convenience function for code that uses aggregate return values
  507. /// as a vehicle for having multiple return values.
  508. ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
  509. Value *V = UndefValue::get(getCurrentFunctionReturnType());
  510. for (unsigned i = 0; i != N; ++i)
  511. V = CreateInsertValue(V, retVals[i], i, "mrv");
  512. return Insert(ReturnInst::Create(Context, V));
  513. }
  514. /// \brief Create an unconditional 'br label X' instruction.
  515. BranchInst *CreateBr(BasicBlock *Dest) {
  516. return Insert(BranchInst::Create(Dest));
  517. }
  518. /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
  519. /// instruction.
  520. BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
  521. MDNode *BranchWeights = nullptr) {
  522. return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
  523. BranchWeights));
  524. }
  525. /// \brief Create a switch instruction with the specified value, default dest,
  526. /// and with a hint for the number of cases that will be added (for efficient
  527. /// allocation).
  528. SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
  529. MDNode *BranchWeights = nullptr) {
  530. return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
  531. BranchWeights));
  532. }
  533. /// \brief Create an indirect branch instruction with the specified address
  534. /// operand, with an optional hint for the number of destinations that will be
  535. /// added (for efficient allocation).
  536. IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
  537. return Insert(IndirectBrInst::Create(Addr, NumDests));
  538. }
  539. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  540. BasicBlock *UnwindDest, const Twine &Name = "") {
  541. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
  542. Name);
  543. }
  544. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  545. BasicBlock *UnwindDest, Value *Arg1,
  546. const Twine &Name = "") {
  547. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
  548. Name);
  549. }
  550. InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
  551. BasicBlock *UnwindDest, Value *Arg1,
  552. Value *Arg2, Value *Arg3,
  553. const Twine &Name = "") {
  554. Value *Args[] = { Arg1, Arg2, Arg3 };
  555. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
  556. Name);
  557. }
  558. /// \brief Create an invoke instruction.
  559. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  560. BasicBlock *UnwindDest, ArrayRef<Value *> Args,
  561. const Twine &Name = "") {
  562. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
  563. Name);
  564. }
  565. ResumeInst *CreateResume(Value *Exn) {
  566. return Insert(ResumeInst::Create(Exn));
  567. }
  568. UnreachableInst *CreateUnreachable() {
  569. return Insert(new UnreachableInst(Context));
  570. }
  571. //===--------------------------------------------------------------------===//
  572. // Instruction creation methods: Binary Operators
  573. //===--------------------------------------------------------------------===//
  574. private:
  575. BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
  576. Value *LHS, Value *RHS,
  577. const Twine &Name,
  578. bool HasNUW, bool HasNSW) {
  579. BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
  580. if (HasNUW) BO->setHasNoUnsignedWrap();
  581. if (HasNSW) BO->setHasNoSignedWrap();
  582. return BO;
  583. }
  584. Instruction *AddFPMathAttributes(Instruction *I,
  585. MDNode *FPMathTag,
  586. FastMathFlags FMF) const {
  587. if (!FPMathTag)
  588. FPMathTag = DefaultFPMathTag;
  589. if (FPMathTag)
  590. I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
  591. I->setFastMathFlags(FMF);
  592. return I;
  593. }
  594. public:
  595. Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  596. bool HasNUW = false, bool HasNSW = false) {
  597. if (Constant *LC = dyn_cast<Constant>(LHS))
  598. if (Constant *RC = dyn_cast<Constant>(RHS))
  599. return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
  600. return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
  601. HasNUW, HasNSW);
  602. }
  603. Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  604. return CreateAdd(LHS, RHS, Name, false, true);
  605. }
  606. Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  607. return CreateAdd(LHS, RHS, Name, true, false);
  608. }
  609. Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  610. MDNode *FPMathTag = nullptr) {
  611. if (Constant *LC = dyn_cast<Constant>(LHS))
  612. if (Constant *RC = dyn_cast<Constant>(RHS))
  613. return Insert(Folder.CreateFAdd(LC, RC), Name);
  614. return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
  615. FPMathTag, FMF), Name);
  616. }
  617. Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
  618. bool HasNUW = false, bool HasNSW = false) {
  619. if (Constant *LC = dyn_cast<Constant>(LHS))
  620. if (Constant *RC = dyn_cast<Constant>(RHS))
  621. return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
  622. return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
  623. HasNUW, HasNSW);
  624. }
  625. Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  626. return CreateSub(LHS, RHS, Name, false, true);
  627. }
  628. Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  629. return CreateSub(LHS, RHS, Name, true, false);
  630. }
  631. Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
  632. MDNode *FPMathTag = nullptr) {
  633. if (Constant *LC = dyn_cast<Constant>(LHS))
  634. if (Constant *RC = dyn_cast<Constant>(RHS))
  635. return Insert(Folder.CreateFSub(LC, RC), Name);
  636. return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
  637. FPMathTag, FMF), Name);
  638. }
  639. Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
  640. bool HasNUW = false, bool HasNSW = false) {
  641. if (Constant *LC = dyn_cast<Constant>(LHS))
  642. if (Constant *RC = dyn_cast<Constant>(RHS))
  643. return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
  644. return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
  645. HasNUW, HasNSW);
  646. }
  647. Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  648. return CreateMul(LHS, RHS, Name, false, true);
  649. }
  650. Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  651. return CreateMul(LHS, RHS, Name, true, false);
  652. }
  653. Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
  654. MDNode *FPMathTag = nullptr) {
  655. if (Constant *LC = dyn_cast<Constant>(LHS))
  656. if (Constant *RC = dyn_cast<Constant>(RHS))
  657. return Insert(Folder.CreateFMul(LC, RC), Name);
  658. return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
  659. FPMathTag, FMF), Name);
  660. }
  661. Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  662. bool isExact = false) {
  663. if (Constant *LC = dyn_cast<Constant>(LHS))
  664. if (Constant *RC = dyn_cast<Constant>(RHS))
  665. return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
  666. if (!isExact)
  667. return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
  668. return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
  669. }
  670. Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  671. return CreateUDiv(LHS, RHS, Name, true);
  672. }
  673. Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  674. bool isExact = false) {
  675. if (Constant *LC = dyn_cast<Constant>(LHS))
  676. if (Constant *RC = dyn_cast<Constant>(RHS))
  677. return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
  678. if (!isExact)
  679. return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
  680. return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
  681. }
  682. Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  683. return CreateSDiv(LHS, RHS, Name, true);
  684. }
  685. Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  686. MDNode *FPMathTag = nullptr) {
  687. if (Constant *LC = dyn_cast<Constant>(LHS))
  688. if (Constant *RC = dyn_cast<Constant>(RHS))
  689. return Insert(Folder.CreateFDiv(LC, RC), Name);
  690. return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
  691. FPMathTag, FMF), Name);
  692. }
  693. Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
  694. if (Constant *LC = dyn_cast<Constant>(LHS))
  695. if (Constant *RC = dyn_cast<Constant>(RHS))
  696. return Insert(Folder.CreateURem(LC, RC), Name);
  697. return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
  698. }
  699. Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
  700. if (Constant *LC = dyn_cast<Constant>(LHS))
  701. if (Constant *RC = dyn_cast<Constant>(RHS))
  702. return Insert(Folder.CreateSRem(LC, RC), Name);
  703. return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
  704. }
  705. Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
  706. MDNode *FPMathTag = nullptr) {
  707. if (Constant *LC = dyn_cast<Constant>(LHS))
  708. if (Constant *RC = dyn_cast<Constant>(RHS))
  709. return Insert(Folder.CreateFRem(LC, RC), Name);
  710. return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
  711. FPMathTag, FMF), Name);
  712. }
  713. Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
  714. bool HasNUW = false, bool HasNSW = false) {
  715. if (Constant *LC = dyn_cast<Constant>(LHS))
  716. if (Constant *RC = dyn_cast<Constant>(RHS))
  717. return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
  718. return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
  719. HasNUW, HasNSW);
  720. }
  721. Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
  722. bool HasNUW = false, bool HasNSW = false) {
  723. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  724. HasNUW, HasNSW);
  725. }
  726. Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
  727. bool HasNUW = false, bool HasNSW = false) {
  728. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  729. HasNUW, HasNSW);
  730. }
  731. Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
  732. bool isExact = false) {
  733. if (Constant *LC = dyn_cast<Constant>(LHS))
  734. if (Constant *RC = dyn_cast<Constant>(RHS))
  735. return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
  736. if (!isExact)
  737. return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
  738. return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
  739. }
  740. Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  741. bool isExact = false) {
  742. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  743. }
  744. Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  745. bool isExact = false) {
  746. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  747. }
  748. Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
  749. bool isExact = false) {
  750. if (Constant *LC = dyn_cast<Constant>(LHS))
  751. if (Constant *RC = dyn_cast<Constant>(RHS))
  752. return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
  753. if (!isExact)
  754. return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
  755. return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
  756. }
  757. Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  758. bool isExact = false) {
  759. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  760. }
  761. Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  762. bool isExact = false) {
  763. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  764. }
  765. Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
  766. if (Constant *RC = dyn_cast<Constant>(RHS)) {
  767. if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
  768. return LHS; // LHS & -1 -> LHS
  769. if (Constant *LC = dyn_cast<Constant>(LHS))
  770. return Insert(Folder.CreateAnd(LC, RC), Name);
  771. }
  772. return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
  773. }
  774. Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  775. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  776. }
  777. Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  778. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  779. }
  780. Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
  781. if (Constant *RC = dyn_cast<Constant>(RHS)) {
  782. if (RC->isNullValue())
  783. return LHS; // LHS | 0 -> LHS
  784. if (Constant *LC = dyn_cast<Constant>(LHS))
  785. return Insert(Folder.CreateOr(LC, RC), Name);
  786. }
  787. return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
  788. }
  789. Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  790. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  791. }
  792. Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  793. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  794. }
  795. Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
  796. if (Constant *LC = dyn_cast<Constant>(LHS))
  797. if (Constant *RC = dyn_cast<Constant>(RHS))
  798. return Insert(Folder.CreateXor(LC, RC), Name);
  799. return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
  800. }
  801. Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  802. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  803. }
  804. Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  805. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  806. }
  807. Value *CreateBinOp(Instruction::BinaryOps Opc,
  808. Value *LHS, Value *RHS, const Twine &Name = "",
  809. MDNode *FPMathTag = nullptr) {
  810. if (Constant *LC = dyn_cast<Constant>(LHS))
  811. if (Constant *RC = dyn_cast<Constant>(RHS))
  812. return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
  813. llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
  814. if (isa<FPMathOperator>(BinOp))
  815. BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
  816. return Insert(BinOp, Name);
  817. }
  818. Value *CreateNeg(Value *V, const Twine &Name = "",
  819. bool HasNUW = false, bool HasNSW = false) {
  820. if (Constant *VC = dyn_cast<Constant>(V))
  821. return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
  822. BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
  823. if (HasNUW) BO->setHasNoUnsignedWrap();
  824. if (HasNSW) BO->setHasNoSignedWrap();
  825. return BO;
  826. }
  827. Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
  828. return CreateNeg(V, Name, false, true);
  829. }
  830. Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
  831. return CreateNeg(V, Name, true, false);
  832. }
  833. Value *CreateFNeg(Value *V, const Twine &Name = "",
  834. MDNode *FPMathTag = nullptr) {
  835. if (Constant *VC = dyn_cast<Constant>(V))
  836. return Insert(Folder.CreateFNeg(VC), Name);
  837. return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
  838. FPMathTag, FMF), Name);
  839. }
  840. Value *CreateNot(Value *V, const Twine &Name = "") {
  841. if (Constant *VC = dyn_cast<Constant>(V))
  842. return Insert(Folder.CreateNot(VC), Name);
  843. return Insert(BinaryOperator::CreateNot(V), Name);
  844. }
  845. //===--------------------------------------------------------------------===//
  846. // Instruction creation methods: Memory Instructions
  847. //===--------------------------------------------------------------------===//
  848. AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
  849. const Twine &Name = "") {
  850. return Insert(new AllocaInst(Ty, ArraySize), Name);
  851. }
  852. // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
  853. // converting the string to 'bool' for the isVolatile parameter.
  854. LoadInst *CreateLoad(Value *Ptr, const char *Name) {
  855. return Insert(new LoadInst(Ptr), Name);
  856. }
  857. LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
  858. return Insert(new LoadInst(Ptr), Name);
  859. }
  860. LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
  861. return Insert(new LoadInst(Ty, Ptr), Name);
  862. }
  863. LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
  864. return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
  865. }
  866. StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
  867. return Insert(new StoreInst(Val, Ptr, isVolatile));
  868. }
  869. // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
  870. // correctly, instead of converting the string to 'bool' for the isVolatile
  871. // parameter.
  872. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
  873. LoadInst *LI = CreateLoad(Ptr, Name);
  874. LI->setAlignment(Align);
  875. return LI;
  876. }
  877. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
  878. const Twine &Name = "") {
  879. LoadInst *LI = CreateLoad(Ptr, Name);
  880. LI->setAlignment(Align);
  881. return LI;
  882. }
  883. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
  884. const Twine &Name = "") {
  885. LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
  886. LI->setAlignment(Align);
  887. return LI;
  888. }
  889. StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
  890. bool isVolatile = false) {
  891. StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
  892. SI->setAlignment(Align);
  893. return SI;
  894. }
  895. FenceInst *CreateFence(AtomicOrdering Ordering,
  896. SynchronizationScope SynchScope = CrossThread,
  897. const Twine &Name = "") {
  898. return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
  899. }
  900. AtomicCmpXchgInst *
  901. CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
  902. AtomicOrdering SuccessOrdering,
  903. AtomicOrdering FailureOrdering,
  904. SynchronizationScope SynchScope = CrossThread) {
  905. return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
  906. FailureOrdering, SynchScope));
  907. }
  908. AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
  909. AtomicOrdering Ordering,
  910. SynchronizationScope SynchScope = CrossThread) {
  911. return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
  912. }
  913. Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  914. const Twine &Name = "") {
  915. return CreateGEP(nullptr, Ptr, IdxList, Name);
  916. }
  917. Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  918. const Twine &Name = "") {
  919. if (Constant *PC = dyn_cast<Constant>(Ptr)) {
  920. // Every index must be constant.
  921. size_t i, e;
  922. for (i = 0, e = IdxList.size(); i != e; ++i)
  923. if (!isa<Constant>(IdxList[i]))
  924. break;
  925. if (i == e)
  926. return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
  927. }
  928. return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
  929. }
  930. Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  931. const Twine &Name = "") {
  932. return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
  933. }
  934. Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  935. const Twine &Name = "") {
  936. if (Constant *PC = dyn_cast<Constant>(Ptr)) {
  937. // Every index must be constant.
  938. size_t i, e;
  939. for (i = 0, e = IdxList.size(); i != e; ++i)
  940. if (!isa<Constant>(IdxList[i]))
  941. break;
  942. if (i == e)
  943. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
  944. Name);
  945. }
  946. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
  947. }
  948. Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
  949. return CreateGEP(nullptr, Ptr, Idx, Name);
  950. }
  951. Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
  952. if (Constant *PC = dyn_cast<Constant>(Ptr))
  953. if (Constant *IC = dyn_cast<Constant>(Idx))
  954. return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
  955. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  956. }
  957. Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
  958. const Twine &Name = "") {
  959. if (Constant *PC = dyn_cast<Constant>(Ptr))
  960. if (Constant *IC = dyn_cast<Constant>(Idx))
  961. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
  962. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  963. }
  964. Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
  965. return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
  966. }
  967. Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
  968. const Twine &Name = "") {
  969. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  970. if (Constant *PC = dyn_cast<Constant>(Ptr))
  971. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
  972. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  973. }
  974. Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
  975. const Twine &Name = "") {
  976. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  977. if (Constant *PC = dyn_cast<Constant>(Ptr))
  978. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
  979. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  980. }
  981. Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
  982. const Twine &Name = "") {
  983. Value *Idxs[] = {
  984. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  985. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  986. };
  987. if (Constant *PC = dyn_cast<Constant>(Ptr))
  988. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
  989. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
  990. }
  991. Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
  992. unsigned Idx1, const Twine &Name = "") {
  993. Value *Idxs[] = {
  994. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  995. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  996. };
  997. if (Constant *PC = dyn_cast<Constant>(Ptr))
  998. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
  999. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
  1000. }
  1001. Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
  1002. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  1003. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1004. return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
  1005. return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
  1006. }
  1007. Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
  1008. const Twine &Name = "") {
  1009. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  1010. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1011. return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
  1012. return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
  1013. }
  1014. Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1015. const Twine &Name = "") {
  1016. Value *Idxs[] = {
  1017. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  1018. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  1019. };
  1020. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1021. return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
  1022. return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
  1023. }
  1024. Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1025. const Twine &Name = "") {
  1026. Value *Idxs[] = {
  1027. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  1028. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  1029. };
  1030. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1031. return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
  1032. Name);
  1033. return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
  1034. }
  1035. Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
  1036. const Twine &Name = "") {
  1037. return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
  1038. }
  1039. /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
  1040. /// instead of a pointer to array of i8.
  1041. Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
  1042. unsigned AddressSpace = 0) {
  1043. GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
  1044. Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
  1045. Value *Args[] = { zero, zero };
  1046. return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
  1047. }
  1048. //===--------------------------------------------------------------------===//
  1049. // Instruction creation methods: Cast/Conversion Operators
  1050. //===--------------------------------------------------------------------===//
  1051. Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
  1052. return CreateCast(Instruction::Trunc, V, DestTy, Name);
  1053. }
  1054. Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1055. return CreateCast(Instruction::ZExt, V, DestTy, Name);
  1056. }
  1057. Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1058. return CreateCast(Instruction::SExt, V, DestTy, Name);
  1059. }
  1060. /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
  1061. /// the value untouched if the type of V is already DestTy.
  1062. Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
  1063. const Twine &Name = "") {
  1064. assert(V->getType()->isIntOrIntVectorTy() &&
  1065. DestTy->isIntOrIntVectorTy() &&
  1066. "Can only zero extend/truncate integers!");
  1067. Type *VTy = V->getType();
  1068. if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
  1069. return CreateZExt(V, DestTy, Name);
  1070. if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
  1071. return CreateTrunc(V, DestTy, Name);
  1072. return V;
  1073. }
  1074. /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
  1075. /// the value untouched if the type of V is already DestTy.
  1076. Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
  1077. const Twine &Name = "") {
  1078. assert(V->getType()->isIntOrIntVectorTy() &&
  1079. DestTy->isIntOrIntVectorTy() &&
  1080. "Can only sign extend/truncate integers!");
  1081. Type *VTy = V->getType();
  1082. if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
  1083. return CreateSExt(V, DestTy, Name);
  1084. if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
  1085. return CreateTrunc(V, DestTy, Name);
  1086. return V;
  1087. }
  1088. Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
  1089. return CreateCast(Instruction::FPToUI, V, DestTy, Name);
  1090. }
  1091. Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
  1092. return CreateCast(Instruction::FPToSI, V, DestTy, Name);
  1093. }
  1094. Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  1095. return CreateCast(Instruction::UIToFP, V, DestTy, Name);
  1096. }
  1097. Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  1098. return CreateCast(Instruction::SIToFP, V, DestTy, Name);
  1099. }
  1100. Value *CreateFPTrunc(Value *V, Type *DestTy,
  1101. const Twine &Name = "") {
  1102. return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
  1103. }
  1104. Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1105. return CreateCast(Instruction::FPExt, V, DestTy, Name);
  1106. }
  1107. Value *CreatePtrToInt(Value *V, Type *DestTy,
  1108. const Twine &Name = "") {
  1109. return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
  1110. }
  1111. Value *CreateIntToPtr(Value *V, Type *DestTy,
  1112. const Twine &Name = "") {
  1113. return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
  1114. }
  1115. Value *CreateBitCast(Value *V, Type *DestTy,
  1116. const Twine &Name = "") {
  1117. return CreateCast(Instruction::BitCast, V, DestTy, Name);
  1118. }
  1119. Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
  1120. const Twine &Name = "") {
  1121. return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
  1122. }
  1123. Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
  1124. const Twine &Name = "") {
  1125. if (V->getType() == DestTy)
  1126. return V;
  1127. if (Constant *VC = dyn_cast<Constant>(V))
  1128. return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
  1129. return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
  1130. }
  1131. Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
  1132. const Twine &Name = "") {
  1133. if (V->getType() == DestTy)
  1134. return V;
  1135. if (Constant *VC = dyn_cast<Constant>(V))
  1136. return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
  1137. return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
  1138. }
  1139. Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
  1140. const Twine &Name = "") {
  1141. if (V->getType() == DestTy)
  1142. return V;
  1143. if (Constant *VC = dyn_cast<Constant>(V))
  1144. return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
  1145. return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
  1146. }
  1147. Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
  1148. const Twine &Name = "") {
  1149. if (V->getType() == DestTy)
  1150. return V;
  1151. if (Constant *VC = dyn_cast<Constant>(V))
  1152. return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
  1153. return Insert(CastInst::Create(Op, V, DestTy), Name);
  1154. }
  1155. Value *CreatePointerCast(Value *V, Type *DestTy,
  1156. const Twine &Name = "") {
  1157. if (V->getType() == DestTy)
  1158. return V;
  1159. if (Constant *VC = dyn_cast<Constant>(V))
  1160. return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
  1161. return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
  1162. }
  1163. Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
  1164. const Twine &Name = "") {
  1165. if (V->getType() == DestTy)
  1166. return V;
  1167. if (Constant *VC = dyn_cast<Constant>(V)) {
  1168. return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
  1169. Name);
  1170. }
  1171. return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
  1172. Name);
  1173. }
  1174. Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
  1175. const Twine &Name = "") {
  1176. if (V->getType() == DestTy)
  1177. return V;
  1178. if (Constant *VC = dyn_cast<Constant>(V))
  1179. return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
  1180. return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
  1181. }
  1182. Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
  1183. const Twine &Name = "") {
  1184. if (V->getType() == DestTy)
  1185. return V;
  1186. if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
  1187. return CreatePtrToInt(V, DestTy, Name);
  1188. if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
  1189. return CreateIntToPtr(V, DestTy, Name);
  1190. return CreateBitCast(V, DestTy, Name);
  1191. }
  1192. private:
  1193. // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
  1194. // compile time error, instead of converting the string to bool for the
  1195. // isSigned parameter.
  1196. Value *CreateIntCast(Value *, Type *, const char *) = delete;
  1197. public:
  1198. Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
  1199. if (V->getType() == DestTy)
  1200. return V;
  1201. if (Constant *VC = dyn_cast<Constant>(V))
  1202. return Insert(Folder.CreateFPCast(VC, DestTy), Name);
  1203. return Insert(CastInst::CreateFPCast(V, DestTy), Name);
  1204. }
  1205. //===--------------------------------------------------------------------===//
  1206. // Instruction creation methods: Compare Instructions
  1207. //===--------------------------------------------------------------------===//
  1208. Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
  1209. return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
  1210. }
  1211. Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1212. return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
  1213. }
  1214. Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1215. return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
  1216. }
  1217. Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1218. return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
  1219. }
  1220. Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1221. return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
  1222. }
  1223. Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1224. return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
  1225. }
  1226. Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1227. return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
  1228. }
  1229. Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1230. return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
  1231. }
  1232. Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1233. return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
  1234. }
  1235. Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1236. return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
  1237. }
  1238. Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
  1239. MDNode *FPMathTag = nullptr) {
  1240. return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
  1241. }
  1242. Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
  1243. MDNode *FPMathTag = nullptr) {
  1244. return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
  1245. }
  1246. Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
  1247. MDNode *FPMathTag = nullptr) {
  1248. return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
  1249. }
  1250. Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
  1251. MDNode *FPMathTag = nullptr) {
  1252. return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
  1253. }
  1254. Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
  1255. MDNode *FPMathTag = nullptr) {
  1256. return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
  1257. }
  1258. Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
  1259. MDNode *FPMathTag = nullptr) {
  1260. return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
  1261. }
  1262. Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
  1263. MDNode *FPMathTag = nullptr) {
  1264. return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
  1265. }
  1266. Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
  1267. MDNode *FPMathTag = nullptr) {
  1268. return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
  1269. }
  1270. Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
  1271. MDNode *FPMathTag = nullptr) {
  1272. return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
  1273. }
  1274. Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
  1275. MDNode *FPMathTag = nullptr) {
  1276. return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
  1277. }
  1278. Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
  1279. MDNode *FPMathTag = nullptr) {
  1280. return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
  1281. }
  1282. Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
  1283. MDNode *FPMathTag = nullptr) {
  1284. return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
  1285. }
  1286. Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
  1287. MDNode *FPMathTag = nullptr) {
  1288. return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
  1289. }
  1290. Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
  1291. MDNode *FPMathTag = nullptr) {
  1292. return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
  1293. }
  1294. Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1295. const Twine &Name = "") {
  1296. if (Constant *LC = dyn_cast<Constant>(LHS))
  1297. if (Constant *RC = dyn_cast<Constant>(RHS))
  1298. return Insert(Folder.CreateICmp(P, LC, RC), Name);
  1299. return Insert(new ICmpInst(P, LHS, RHS), Name);
  1300. }
  1301. Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1302. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1303. if (Constant *LC = dyn_cast<Constant>(LHS))
  1304. if (Constant *RC = dyn_cast<Constant>(RHS))
  1305. return Insert(Folder.CreateFCmp(P, LC, RC), Name);
  1306. return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
  1307. FPMathTag, FMF), Name);
  1308. }
  1309. //===--------------------------------------------------------------------===//
  1310. // Instruction creation methods: Other Instructions
  1311. //===--------------------------------------------------------------------===//
  1312. PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
  1313. const Twine &Name = "") {
  1314. return Insert(PHINode::Create(Ty, NumReservedValues), Name);
  1315. }
  1316. CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
  1317. const Twine &Name = "") {
  1318. return Insert(CallInst::Create(Callee, Args), Name);
  1319. }
  1320. CallInst *CreateCall(llvm::FunctionType *FTy, Value *Callee,
  1321. ArrayRef<Value *> Args, const Twine &Name = "") {
  1322. return Insert(CallInst::Create(FTy, Callee, Args), Name);
  1323. }
  1324. CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
  1325. const Twine &Name = "") {
  1326. return CreateCall(Callee->getFunctionType(), Callee, Args, Name);
  1327. }
  1328. Value *CreateSelect(Value *C, Value *True, Value *False,
  1329. const Twine &Name = "") {
  1330. if (Constant *CC = dyn_cast<Constant>(C))
  1331. if (Constant *TC = dyn_cast<Constant>(True))
  1332. if (Constant *FC = dyn_cast<Constant>(False))
  1333. return Insert(Folder.CreateSelect(CC, TC, FC), Name);
  1334. return Insert(SelectInst::Create(C, True, False), Name);
  1335. }
  1336. VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
  1337. return Insert(new VAArgInst(List, Ty), Name);
  1338. }
  1339. Value *CreateExtractElement(Value *Vec, Value *Idx,
  1340. const Twine &Name = "") {
  1341. if (Constant *VC = dyn_cast<Constant>(Vec))
  1342. if (Constant *IC = dyn_cast<Constant>(Idx))
  1343. return Insert(Folder.CreateExtractElement(VC, IC), Name);
  1344. return Insert(ExtractElementInst::Create(Vec, Idx), Name);
  1345. }
  1346. Value *CreateExtractElement(Value *Vec, uint64_t Idx,
  1347. const Twine &Name = "") {
  1348. return CreateExtractElement(Vec, getInt64(Idx), Name);
  1349. }
  1350. Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
  1351. const Twine &Name = "") {
  1352. if (Constant *VC = dyn_cast<Constant>(Vec))
  1353. if (Constant *NC = dyn_cast<Constant>(NewElt))
  1354. if (Constant *IC = dyn_cast<Constant>(Idx))
  1355. return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
  1356. return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
  1357. }
  1358. Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
  1359. const Twine &Name = "") {
  1360. return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
  1361. }
  1362. Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
  1363. const Twine &Name = "") {
  1364. if (Constant *V1C = dyn_cast<Constant>(V1))
  1365. if (Constant *V2C = dyn_cast<Constant>(V2))
  1366. if (Constant *MC = dyn_cast<Constant>(Mask))
  1367. return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
  1368. return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
  1369. }
  1370. Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
  1371. const Twine &Name = "") {
  1372. size_t MaskSize = IntMask.size();
  1373. SmallVector<Constant*, 8> MaskVec(MaskSize);
  1374. for (size_t i = 0; i != MaskSize; ++i)
  1375. MaskVec[i] = getInt32(IntMask[i]);
  1376. Value *Mask = ConstantVector::get(MaskVec);
  1377. return CreateShuffleVector(V1, V2, Mask, Name);
  1378. }
  1379. Value *CreateExtractValue(Value *Agg,
  1380. ArrayRef<unsigned> Idxs,
  1381. const Twine &Name = "") {
  1382. if (Constant *AggC = dyn_cast<Constant>(Agg))
  1383. return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
  1384. return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
  1385. }
  1386. Value *CreateInsertValue(Value *Agg, Value *Val,
  1387. ArrayRef<unsigned> Idxs,
  1388. const Twine &Name = "") {
  1389. if (Constant *AggC = dyn_cast<Constant>(Agg))
  1390. if (Constant *ValC = dyn_cast<Constant>(Val))
  1391. return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
  1392. return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
  1393. }
  1394. LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
  1395. const Twine &Name = "") {
  1396. return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
  1397. }
  1398. //===--------------------------------------------------------------------===//
  1399. // Utility creation methods
  1400. //===--------------------------------------------------------------------===//
  1401. /// \brief Return an i1 value testing if \p Arg is null.
  1402. Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
  1403. return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
  1404. Name);
  1405. }
  1406. /// \brief Return an i1 value testing if \p Arg is not null.
  1407. Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
  1408. return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
  1409. Name);
  1410. }
  1411. /// \brief Return the i64 difference between two pointer values, dividing out
  1412. /// the size of the pointed-to objects.
  1413. ///
  1414. /// This is intended to implement C-style pointer subtraction. As such, the
  1415. /// pointers must be appropriately aligned for their element types and
  1416. /// pointing into the same object.
  1417. Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
  1418. assert(LHS->getType() == RHS->getType() &&
  1419. "Pointer subtraction operand types must match!");
  1420. PointerType *ArgType = cast<PointerType>(LHS->getType());
  1421. Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
  1422. Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
  1423. Value *Difference = CreateSub(LHS_int, RHS_int);
  1424. return CreateExactSDiv(Difference,
  1425. ConstantExpr::getSizeOf(ArgType->getElementType()),
  1426. Name);
  1427. }
  1428. /// \brief Return a vector value that contains \arg V broadcasted to \p
  1429. /// NumElts elements.
  1430. Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
  1431. assert(NumElts > 0 && "Cannot splat to an empty vector!");
  1432. // First insert it into an undef vector so we can shuffle it.
  1433. Type *I32Ty = getInt32Ty();
  1434. Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
  1435. V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
  1436. Name + ".splatinsert");
  1437. // Shuffle the value across the desired number of elements.
  1438. Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
  1439. return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
  1440. }
  1441. /// \brief Return a value that has been extracted from a larger integer type.
  1442. Value *CreateExtractInteger(const DataLayout &DL, Value *From,
  1443. IntegerType *ExtractedTy, uint64_t Offset,
  1444. const Twine &Name) {
  1445. IntegerType *IntTy = cast<IntegerType>(From->getType());
  1446. assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
  1447. DL.getTypeStoreSize(IntTy) &&
  1448. "Element extends past full value");
  1449. uint64_t ShAmt = 8 * Offset;
  1450. Value *V = From;
  1451. if (DL.isBigEndian())
  1452. ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
  1453. DL.getTypeStoreSize(ExtractedTy) - Offset);
  1454. if (ShAmt) {
  1455. V = CreateLShr(V, ShAmt, Name + ".shift");
  1456. }
  1457. assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
  1458. "Cannot extract to a larger integer!");
  1459. if (ExtractedTy != IntTy) {
  1460. V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
  1461. }
  1462. return V;
  1463. }
  1464. /// \brief Create an assume intrinsic call that represents an alignment
  1465. /// assumption on the provided pointer.
  1466. ///
  1467. /// An optional offset can be provided, and if it is provided, the offset
  1468. /// must be subtracted from the provided pointer to get the pointer with the
  1469. /// specified alignment.
  1470. CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
  1471. unsigned Alignment,
  1472. Value *OffsetValue = nullptr) {
  1473. assert(isa<PointerType>(PtrValue->getType()) &&
  1474. "trying to create an alignment assumption on a non-pointer?");
  1475. PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
  1476. Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
  1477. Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
  1478. Value *Mask = ConstantInt::get(IntPtrTy,
  1479. Alignment > 0 ? Alignment - 1 : 0);
  1480. if (OffsetValue) {
  1481. bool IsOffsetZero = false;
  1482. if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
  1483. IsOffsetZero = CI->isZero();
  1484. if (!IsOffsetZero) {
  1485. if (OffsetValue->getType() != IntPtrTy)
  1486. OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
  1487. "offsetcast");
  1488. PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
  1489. }
  1490. }
  1491. Value *Zero = ConstantInt::get(IntPtrTy, 0);
  1492. Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
  1493. Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
  1494. return CreateAssumption(InvCond);
  1495. }
  1496. };
  1497. // Create wrappers for C Binding types (see CBindingWrapping.h).
  1498. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
  1499. }
  1500. #endif