IRBuilder.h 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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. bool AllowFolding = true; // HLSL Change - Runtime flag on whether to do folding
  436. IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
  437. MDNode *FPMathTag = nullptr)
  438. : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
  439. }
  440. explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
  441. : IRBuilderBase(C, FPMathTag), Folder() {
  442. }
  443. explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
  444. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
  445. SetInsertPoint(TheBB);
  446. }
  447. explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
  448. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
  449. SetInsertPoint(TheBB);
  450. }
  451. explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
  452. : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
  453. SetInsertPoint(IP);
  454. }
  455. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
  456. MDNode *FPMathTag = nullptr)
  457. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
  458. SetInsertPoint(TheBB, IP);
  459. }
  460. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
  461. MDNode *FPMathTag = nullptr)
  462. : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
  463. SetInsertPoint(TheBB, IP);
  464. }
  465. /// \brief Get the constant folder being used.
  466. const T &getFolder() { return Folder; }
  467. /// \brief Return true if this builder is configured to actually add the
  468. /// requested names to IR created through it.
  469. bool isNamePreserving() const { return preserveNames; }
  470. /// \brief Insert and return the specified instruction.
  471. template<typename InstTy>
  472. InstTy *Insert(InstTy *I, const Twine &Name = "") const {
  473. this->InsertHelper(I, Name, BB, InsertPt);
  474. this->SetInstDebugLocation(I);
  475. return I;
  476. }
  477. /// \brief No-op overload to handle constants.
  478. Constant *Insert(Constant *C, const Twine& = "") const {
  479. return C;
  480. }
  481. //===--------------------------------------------------------------------===//
  482. // Instruction creation methods: Terminators
  483. //===--------------------------------------------------------------------===//
  484. private:
  485. /// \brief Helper to add branch weight metadata onto an instruction.
  486. /// \returns The annotated instruction.
  487. template <typename InstTy>
  488. InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
  489. if (Weights)
  490. I->setMetadata(LLVMContext::MD_prof, Weights);
  491. return I;
  492. }
  493. public:
  494. /// \brief Create a 'ret void' instruction.
  495. ReturnInst *CreateRetVoid() {
  496. return Insert(ReturnInst::Create(Context));
  497. }
  498. /// \brief Create a 'ret <val>' instruction.
  499. ReturnInst *CreateRet(Value *V) {
  500. return Insert(ReturnInst::Create(Context, V));
  501. }
  502. /// \brief Create a sequence of N insertvalue instructions,
  503. /// with one Value from the retVals array each, that build a aggregate
  504. /// return value one value at a time, and a ret instruction to return
  505. /// the resulting aggregate value.
  506. ///
  507. /// This is a convenience function for code that uses aggregate return values
  508. /// as a vehicle for having multiple return values.
  509. ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
  510. Value *V = UndefValue::get(getCurrentFunctionReturnType());
  511. for (unsigned i = 0; i != N; ++i)
  512. V = CreateInsertValue(V, retVals[i], i, "mrv");
  513. return Insert(ReturnInst::Create(Context, V));
  514. }
  515. /// \brief Create an unconditional 'br label X' instruction.
  516. BranchInst *CreateBr(BasicBlock *Dest) {
  517. return Insert(BranchInst::Create(Dest));
  518. }
  519. /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
  520. /// instruction.
  521. BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
  522. MDNode *BranchWeights = nullptr) {
  523. return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
  524. BranchWeights));
  525. }
  526. /// \brief Create a switch instruction with the specified value, default dest,
  527. /// and with a hint for the number of cases that will be added (for efficient
  528. /// allocation).
  529. SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
  530. MDNode *BranchWeights = nullptr) {
  531. return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
  532. BranchWeights));
  533. }
  534. /// \brief Create an indirect branch instruction with the specified address
  535. /// operand, with an optional hint for the number of destinations that will be
  536. /// added (for efficient allocation).
  537. IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
  538. return Insert(IndirectBrInst::Create(Addr, NumDests));
  539. }
  540. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  541. BasicBlock *UnwindDest, const Twine &Name = "") {
  542. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
  543. Name);
  544. }
  545. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  546. BasicBlock *UnwindDest, Value *Arg1,
  547. const Twine &Name = "") {
  548. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
  549. Name);
  550. }
  551. InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
  552. BasicBlock *UnwindDest, Value *Arg1,
  553. Value *Arg2, Value *Arg3,
  554. const Twine &Name = "") {
  555. Value *Args[] = { Arg1, Arg2, Arg3 };
  556. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
  557. Name);
  558. }
  559. /// \brief Create an invoke instruction.
  560. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  561. BasicBlock *UnwindDest, ArrayRef<Value *> Args,
  562. const Twine &Name = "") {
  563. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
  564. Name);
  565. }
  566. ResumeInst *CreateResume(Value *Exn) {
  567. return Insert(ResumeInst::Create(Exn));
  568. }
  569. UnreachableInst *CreateUnreachable() {
  570. return Insert(new UnreachableInst(Context));
  571. }
  572. //===--------------------------------------------------------------------===//
  573. // Instruction creation methods: Binary Operators
  574. //===--------------------------------------------------------------------===//
  575. private:
  576. BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
  577. Value *LHS, Value *RHS,
  578. const Twine &Name,
  579. bool HasNUW, bool HasNSW) {
  580. BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
  581. if (HasNUW) BO->setHasNoUnsignedWrap();
  582. if (HasNSW) BO->setHasNoSignedWrap();
  583. return BO;
  584. }
  585. Instruction *AddFPMathAttributes(Instruction *I,
  586. MDNode *FPMathTag,
  587. FastMathFlags FMF) const {
  588. if (!FPMathTag)
  589. FPMathTag = DefaultFPMathTag;
  590. if (FPMathTag)
  591. I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
  592. I->setFastMathFlags(FMF);
  593. return I;
  594. }
  595. public:
  596. Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  597. bool HasNUW = false, bool HasNSW = false) {
  598. if (AllowFolding)
  599. if (Constant *LC = dyn_cast<Constant>(LHS))
  600. if (Constant *RC = dyn_cast<Constant>(RHS))
  601. return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
  602. return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
  603. HasNUW, HasNSW);
  604. }
  605. Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  606. return CreateAdd(LHS, RHS, Name, false, true);
  607. }
  608. Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  609. return CreateAdd(LHS, RHS, Name, true, false);
  610. }
  611. Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  612. MDNode *FPMathTag = nullptr) {
  613. if (AllowFolding)
  614. if (Constant *LC = dyn_cast<Constant>(LHS))
  615. if (Constant *RC = dyn_cast<Constant>(RHS))
  616. return Insert(Folder.CreateFAdd(LC, RC), Name);
  617. return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
  618. FPMathTag, FMF), Name);
  619. }
  620. Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
  621. bool HasNUW = false, bool HasNSW = false) {
  622. if (AllowFolding)
  623. if (Constant *LC = dyn_cast<Constant>(LHS))
  624. if (Constant *RC = dyn_cast<Constant>(RHS))
  625. return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
  626. return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
  627. HasNUW, HasNSW);
  628. }
  629. Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  630. return CreateSub(LHS, RHS, Name, false, true);
  631. }
  632. Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  633. return CreateSub(LHS, RHS, Name, true, false);
  634. }
  635. Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
  636. MDNode *FPMathTag = nullptr) {
  637. if (AllowFolding)
  638. if (Constant *LC = dyn_cast<Constant>(LHS))
  639. if (Constant *RC = dyn_cast<Constant>(RHS))
  640. return Insert(Folder.CreateFSub(LC, RC), Name);
  641. return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
  642. FPMathTag, FMF), Name);
  643. }
  644. Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
  645. bool HasNUW = false, bool HasNSW = false) {
  646. if (AllowFolding)
  647. if (Constant *LC = dyn_cast<Constant>(LHS))
  648. if (Constant *RC = dyn_cast<Constant>(RHS))
  649. return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
  650. return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
  651. HasNUW, HasNSW);
  652. }
  653. Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  654. return CreateMul(LHS, RHS, Name, false, true);
  655. }
  656. Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  657. return CreateMul(LHS, RHS, Name, true, false);
  658. }
  659. Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
  660. MDNode *FPMathTag = nullptr) {
  661. if (AllowFolding)
  662. if (Constant *LC = dyn_cast<Constant>(LHS))
  663. if (Constant *RC = dyn_cast<Constant>(RHS))
  664. return Insert(Folder.CreateFMul(LC, RC), Name);
  665. return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
  666. FPMathTag, FMF), Name);
  667. }
  668. Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  669. bool isExact = false) {
  670. if (AllowFolding)
  671. if (Constant *LC = dyn_cast<Constant>(LHS))
  672. if (Constant *RC = dyn_cast<Constant>(RHS))
  673. return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
  674. if (!isExact)
  675. return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
  676. return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
  677. }
  678. Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  679. return CreateUDiv(LHS, RHS, Name, true);
  680. }
  681. Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  682. bool isExact = false) {
  683. if (AllowFolding)
  684. if (Constant *LC = dyn_cast<Constant>(LHS))
  685. if (Constant *RC = dyn_cast<Constant>(RHS))
  686. return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
  687. if (!isExact)
  688. return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
  689. return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
  690. }
  691. Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  692. return CreateSDiv(LHS, RHS, Name, true);
  693. }
  694. Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  695. MDNode *FPMathTag = nullptr) {
  696. if (AllowFolding)
  697. if (Constant *LC = dyn_cast<Constant>(LHS))
  698. if (Constant *RC = dyn_cast<Constant>(RHS))
  699. return Insert(Folder.CreateFDiv(LC, RC), Name);
  700. return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
  701. FPMathTag, FMF), Name);
  702. }
  703. Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
  704. if (AllowFolding)
  705. if (Constant *LC = dyn_cast<Constant>(LHS))
  706. if (Constant *RC = dyn_cast<Constant>(RHS))
  707. return Insert(Folder.CreateURem(LC, RC), Name);
  708. return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
  709. }
  710. Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
  711. if (AllowFolding)
  712. if (Constant *LC = dyn_cast<Constant>(LHS))
  713. if (Constant *RC = dyn_cast<Constant>(RHS))
  714. return Insert(Folder.CreateSRem(LC, RC), Name);
  715. return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
  716. }
  717. Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
  718. MDNode *FPMathTag = nullptr) {
  719. if (AllowFolding)
  720. if (Constant *LC = dyn_cast<Constant>(LHS))
  721. if (Constant *RC = dyn_cast<Constant>(RHS))
  722. return Insert(Folder.CreateFRem(LC, RC), Name);
  723. return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
  724. FPMathTag, FMF), Name);
  725. }
  726. Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
  727. bool HasNUW = false, bool HasNSW = false) {
  728. if (AllowFolding)
  729. if (Constant *LC = dyn_cast<Constant>(LHS))
  730. if (Constant *RC = dyn_cast<Constant>(RHS))
  731. return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
  732. return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
  733. HasNUW, HasNSW);
  734. }
  735. Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
  736. bool HasNUW = false, bool HasNSW = false) {
  737. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  738. HasNUW, HasNSW);
  739. }
  740. Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
  741. bool HasNUW = false, bool HasNSW = false) {
  742. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  743. HasNUW, HasNSW);
  744. }
  745. Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
  746. bool isExact = false) {
  747. if (AllowFolding)
  748. if (Constant *LC = dyn_cast<Constant>(LHS))
  749. if (Constant *RC = dyn_cast<Constant>(RHS))
  750. return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
  751. if (!isExact)
  752. return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
  753. return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
  754. }
  755. Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  756. bool isExact = false) {
  757. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  758. }
  759. Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  760. bool isExact = false) {
  761. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  762. }
  763. Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
  764. bool isExact = false) {
  765. if (AllowFolding)
  766. if (Constant *LC = dyn_cast<Constant>(LHS))
  767. if (Constant *RC = dyn_cast<Constant>(RHS))
  768. return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
  769. if (!isExact)
  770. return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
  771. return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
  772. }
  773. Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  774. bool isExact = false) {
  775. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  776. }
  777. Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  778. bool isExact = false) {
  779. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  780. }
  781. Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
  782. if (AllowFolding)
  783. if (Constant *RC = dyn_cast<Constant>(RHS)) {
  784. if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
  785. return LHS; // LHS & -1 -> LHS
  786. if (Constant *LC = dyn_cast<Constant>(LHS))
  787. return Insert(Folder.CreateAnd(LC, RC), Name);
  788. }
  789. return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
  790. }
  791. Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  792. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  793. }
  794. Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  795. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  796. }
  797. Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
  798. if (AllowFolding)
  799. if (Constant *RC = dyn_cast<Constant>(RHS)) {
  800. if (RC->isNullValue())
  801. return LHS; // LHS | 0 -> LHS
  802. if (Constant *LC = dyn_cast<Constant>(LHS))
  803. return Insert(Folder.CreateOr(LC, RC), Name);
  804. }
  805. return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
  806. }
  807. Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  808. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  809. }
  810. Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  811. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  812. }
  813. Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
  814. if (AllowFolding)
  815. if (Constant *LC = dyn_cast<Constant>(LHS))
  816. if (Constant *RC = dyn_cast<Constant>(RHS))
  817. return Insert(Folder.CreateXor(LC, RC), Name);
  818. return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
  819. }
  820. Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  821. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  822. }
  823. Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  824. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  825. }
  826. Value *CreateBinOp(Instruction::BinaryOps Opc,
  827. Value *LHS, Value *RHS, const Twine &Name = "",
  828. MDNode *FPMathTag = nullptr) {
  829. if (AllowFolding)
  830. if (Constant *LC = dyn_cast<Constant>(LHS))
  831. if (Constant *RC = dyn_cast<Constant>(RHS))
  832. return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
  833. llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
  834. if (isa<FPMathOperator>(BinOp))
  835. BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
  836. return Insert(BinOp, Name);
  837. }
  838. Value *CreateNeg(Value *V, const Twine &Name = "",
  839. bool HasNUW = false, bool HasNSW = false) {
  840. if (AllowFolding)
  841. if (Constant *VC = dyn_cast<Constant>(V))
  842. return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
  843. BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
  844. if (HasNUW) BO->setHasNoUnsignedWrap();
  845. if (HasNSW) BO->setHasNoSignedWrap();
  846. return BO;
  847. }
  848. Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
  849. return CreateNeg(V, Name, false, true);
  850. }
  851. Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
  852. return CreateNeg(V, Name, true, false);
  853. }
  854. Value *CreateFNeg(Value *V, const Twine &Name = "",
  855. MDNode *FPMathTag = nullptr) {
  856. if (AllowFolding)
  857. if (Constant *VC = dyn_cast<Constant>(V))
  858. return Insert(Folder.CreateFNeg(VC), Name);
  859. return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
  860. FPMathTag, FMF), Name);
  861. }
  862. Value *CreateNot(Value *V, const Twine &Name = "") {
  863. if (AllowFolding)
  864. if (Constant *VC = dyn_cast<Constant>(V))
  865. return Insert(Folder.CreateNot(VC), Name);
  866. return Insert(BinaryOperator::CreateNot(V), Name);
  867. }
  868. //===--------------------------------------------------------------------===//
  869. // Instruction creation methods: Memory Instructions
  870. //===--------------------------------------------------------------------===//
  871. AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
  872. const Twine &Name = "") {
  873. return Insert(new AllocaInst(Ty, ArraySize), Name);
  874. }
  875. // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
  876. // converting the string to 'bool' for the isVolatile parameter.
  877. LoadInst *CreateLoad(Value *Ptr, const char *Name) {
  878. return Insert(new LoadInst(Ptr), Name);
  879. }
  880. LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
  881. return Insert(new LoadInst(Ptr), Name);
  882. }
  883. LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
  884. return Insert(new LoadInst(Ty, Ptr), Name);
  885. }
  886. LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
  887. return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
  888. }
  889. StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
  890. return Insert(new StoreInst(Val, Ptr, isVolatile));
  891. }
  892. // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
  893. // correctly, instead of converting the string to 'bool' for the isVolatile
  894. // parameter.
  895. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
  896. LoadInst *LI = CreateLoad(Ptr, Name);
  897. LI->setAlignment(Align);
  898. return LI;
  899. }
  900. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
  901. const Twine &Name = "") {
  902. LoadInst *LI = CreateLoad(Ptr, Name);
  903. LI->setAlignment(Align);
  904. return LI;
  905. }
  906. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
  907. const Twine &Name = "") {
  908. LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
  909. LI->setAlignment(Align);
  910. return LI;
  911. }
  912. StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
  913. bool isVolatile = false) {
  914. StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
  915. SI->setAlignment(Align);
  916. return SI;
  917. }
  918. FenceInst *CreateFence(AtomicOrdering Ordering,
  919. SynchronizationScope SynchScope = CrossThread,
  920. const Twine &Name = "") {
  921. return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
  922. }
  923. AtomicCmpXchgInst *
  924. CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
  925. AtomicOrdering SuccessOrdering,
  926. AtomicOrdering FailureOrdering,
  927. SynchronizationScope SynchScope = CrossThread) {
  928. return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
  929. FailureOrdering, SynchScope));
  930. }
  931. AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
  932. AtomicOrdering Ordering,
  933. SynchronizationScope SynchScope = CrossThread) {
  934. return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
  935. }
  936. Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  937. const Twine &Name = "") {
  938. return CreateGEP(nullptr, Ptr, IdxList, Name);
  939. }
  940. Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  941. const Twine &Name = "") {
  942. if (AllowFolding)
  943. if (Constant *PC = dyn_cast<Constant>(Ptr)) {
  944. // Every index must be constant.
  945. size_t i, e;
  946. for (i = 0, e = IdxList.size(); i != e; ++i)
  947. if (!isa<Constant>(IdxList[i]))
  948. break;
  949. if (i == e)
  950. return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
  951. }
  952. return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
  953. }
  954. Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  955. const Twine &Name = "") {
  956. return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
  957. }
  958. Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  959. const Twine &Name = "") {
  960. if (AllowFolding)
  961. if (Constant *PC = dyn_cast<Constant>(Ptr)) {
  962. // Every index must be constant.
  963. size_t i, e;
  964. for (i = 0, e = IdxList.size(); i != e; ++i)
  965. if (!isa<Constant>(IdxList[i]))
  966. break;
  967. if (i == e)
  968. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
  969. Name);
  970. }
  971. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
  972. }
  973. Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
  974. return CreateGEP(nullptr, Ptr, Idx, Name);
  975. }
  976. Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
  977. if (AllowFolding)
  978. if (Constant *PC = dyn_cast<Constant>(Ptr))
  979. if (Constant *IC = dyn_cast<Constant>(Idx))
  980. return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
  981. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  982. }
  983. Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
  984. const Twine &Name = "") {
  985. if (AllowFolding)
  986. if (Constant *PC = dyn_cast<Constant>(Ptr))
  987. if (Constant *IC = dyn_cast<Constant>(Idx))
  988. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
  989. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  990. }
  991. Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
  992. return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
  993. }
  994. Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
  995. const Twine &Name = "") {
  996. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  997. if (AllowFolding)
  998. if (Constant *PC = dyn_cast<Constant>(Ptr))
  999. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
  1000. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  1001. }
  1002. Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
  1003. const Twine &Name = "") {
  1004. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  1005. if (AllowFolding)
  1006. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1007. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
  1008. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  1009. }
  1010. Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
  1011. const Twine &Name = "") {
  1012. Value *Idxs[] = {
  1013. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  1014. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  1015. };
  1016. if (AllowFolding)
  1017. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1018. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
  1019. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
  1020. }
  1021. Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
  1022. unsigned Idx1, const Twine &Name = "") {
  1023. Value *Idxs[] = {
  1024. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  1025. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  1026. };
  1027. if (AllowFolding)
  1028. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1029. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
  1030. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
  1031. }
  1032. Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
  1033. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  1034. if (AllowFolding)
  1035. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1036. return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
  1037. return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
  1038. }
  1039. Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
  1040. const Twine &Name = "") {
  1041. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  1042. if (AllowFolding)
  1043. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1044. return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
  1045. return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
  1046. }
  1047. Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1048. const Twine &Name = "") {
  1049. Value *Idxs[] = {
  1050. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  1051. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  1052. };
  1053. if (AllowFolding)
  1054. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1055. return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
  1056. return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
  1057. }
  1058. Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1059. const Twine &Name = "") {
  1060. Value *Idxs[] = {
  1061. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  1062. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  1063. };
  1064. if (AllowFolding)
  1065. if (Constant *PC = dyn_cast<Constant>(Ptr))
  1066. return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
  1067. Name);
  1068. return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
  1069. }
  1070. Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
  1071. const Twine &Name = "") {
  1072. return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
  1073. }
  1074. /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
  1075. /// instead of a pointer to array of i8.
  1076. Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
  1077. unsigned AddressSpace = 0) {
  1078. GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
  1079. Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
  1080. Value *Args[] = { zero, zero };
  1081. return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
  1082. }
  1083. //===--------------------------------------------------------------------===//
  1084. // Instruction creation methods: Cast/Conversion Operators
  1085. //===--------------------------------------------------------------------===//
  1086. Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
  1087. return CreateCast(Instruction::Trunc, V, DestTy, Name);
  1088. }
  1089. Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1090. return CreateCast(Instruction::ZExt, V, DestTy, Name);
  1091. }
  1092. Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1093. return CreateCast(Instruction::SExt, V, DestTy, Name);
  1094. }
  1095. /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
  1096. /// the value untouched if the type of V is already DestTy.
  1097. Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
  1098. const Twine &Name = "") {
  1099. assert(V->getType()->isIntOrIntVectorTy() &&
  1100. DestTy->isIntOrIntVectorTy() &&
  1101. "Can only zero extend/truncate integers!");
  1102. Type *VTy = V->getType();
  1103. if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
  1104. return CreateZExt(V, DestTy, Name);
  1105. if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
  1106. return CreateTrunc(V, DestTy, Name);
  1107. return V;
  1108. }
  1109. /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
  1110. /// the value untouched if the type of V is already DestTy.
  1111. Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
  1112. const Twine &Name = "") {
  1113. assert(V->getType()->isIntOrIntVectorTy() &&
  1114. DestTy->isIntOrIntVectorTy() &&
  1115. "Can only sign extend/truncate integers!");
  1116. Type *VTy = V->getType();
  1117. if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
  1118. return CreateSExt(V, DestTy, Name);
  1119. if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
  1120. return CreateTrunc(V, DestTy, Name);
  1121. return V;
  1122. }
  1123. Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
  1124. return CreateCast(Instruction::FPToUI, V, DestTy, Name);
  1125. }
  1126. Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
  1127. return CreateCast(Instruction::FPToSI, V, DestTy, Name);
  1128. }
  1129. Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  1130. return CreateCast(Instruction::UIToFP, V, DestTy, Name);
  1131. }
  1132. Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  1133. return CreateCast(Instruction::SIToFP, V, DestTy, Name);
  1134. }
  1135. Value *CreateFPTrunc(Value *V, Type *DestTy,
  1136. const Twine &Name = "") {
  1137. return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
  1138. }
  1139. Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1140. return CreateCast(Instruction::FPExt, V, DestTy, Name);
  1141. }
  1142. Value *CreatePtrToInt(Value *V, Type *DestTy,
  1143. const Twine &Name = "") {
  1144. return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
  1145. }
  1146. Value *CreateIntToPtr(Value *V, Type *DestTy,
  1147. const Twine &Name = "") {
  1148. return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
  1149. }
  1150. Value *CreateBitCast(Value *V, Type *DestTy,
  1151. const Twine &Name = "") {
  1152. return CreateCast(Instruction::BitCast, V, DestTy, Name);
  1153. }
  1154. Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
  1155. const Twine &Name = "") {
  1156. return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
  1157. }
  1158. Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
  1159. const Twine &Name = "") {
  1160. if (V->getType() == DestTy)
  1161. return V;
  1162. if (AllowFolding)
  1163. if (Constant *VC = dyn_cast<Constant>(V))
  1164. return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
  1165. return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
  1166. }
  1167. Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
  1168. const Twine &Name = "") {
  1169. if (V->getType() == DestTy)
  1170. return V;
  1171. if (AllowFolding)
  1172. if (Constant *VC = dyn_cast<Constant>(V))
  1173. return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
  1174. return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
  1175. }
  1176. Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
  1177. const Twine &Name = "") {
  1178. if (V->getType() == DestTy)
  1179. return V;
  1180. if (AllowFolding)
  1181. if (Constant *VC = dyn_cast<Constant>(V))
  1182. return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
  1183. return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
  1184. }
  1185. Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
  1186. const Twine &Name = "") {
  1187. if (V->getType() == DestTy)
  1188. return V;
  1189. if (AllowFolding)
  1190. if (Constant *VC = dyn_cast<Constant>(V))
  1191. return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
  1192. return Insert(CastInst::Create(Op, V, DestTy), Name);
  1193. }
  1194. Value *CreatePointerCast(Value *V, Type *DestTy,
  1195. const Twine &Name = "") {
  1196. if (V->getType() == DestTy)
  1197. return V;
  1198. if (AllowFolding)
  1199. if (Constant *VC = dyn_cast<Constant>(V))
  1200. return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
  1201. return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
  1202. }
  1203. Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
  1204. const Twine &Name = "") {
  1205. if (V->getType() == DestTy)
  1206. return V;
  1207. if (AllowFolding)
  1208. if (Constant *VC = dyn_cast<Constant>(V)) {
  1209. return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
  1210. Name);
  1211. }
  1212. return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
  1213. Name);
  1214. }
  1215. Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
  1216. const Twine &Name = "") {
  1217. if (V->getType() == DestTy)
  1218. return V;
  1219. if (AllowFolding)
  1220. if (Constant *VC = dyn_cast<Constant>(V))
  1221. return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
  1222. return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
  1223. }
  1224. Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
  1225. const Twine &Name = "") {
  1226. if (V->getType() == DestTy)
  1227. return V;
  1228. if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
  1229. return CreatePtrToInt(V, DestTy, Name);
  1230. if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
  1231. return CreateIntToPtr(V, DestTy, Name);
  1232. return CreateBitCast(V, DestTy, Name);
  1233. }
  1234. private:
  1235. // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
  1236. // compile time error, instead of converting the string to bool for the
  1237. // isSigned parameter.
  1238. Value *CreateIntCast(Value *, Type *, const char *) = delete;
  1239. public:
  1240. Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
  1241. if (V->getType() == DestTy)
  1242. return V;
  1243. if (AllowFolding)
  1244. if (Constant *VC = dyn_cast<Constant>(V))
  1245. return Insert(Folder.CreateFPCast(VC, DestTy), Name);
  1246. return Insert(CastInst::CreateFPCast(V, DestTy), Name);
  1247. }
  1248. //===--------------------------------------------------------------------===//
  1249. // Instruction creation methods: Compare Instructions
  1250. //===--------------------------------------------------------------------===//
  1251. Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
  1252. return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
  1253. }
  1254. Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1255. return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
  1256. }
  1257. Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1258. return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
  1259. }
  1260. Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1261. return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
  1262. }
  1263. Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1264. return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
  1265. }
  1266. Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1267. return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
  1268. }
  1269. Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1270. return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
  1271. }
  1272. Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1273. return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
  1274. }
  1275. Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1276. return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
  1277. }
  1278. Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1279. return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
  1280. }
  1281. Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
  1282. MDNode *FPMathTag = nullptr) {
  1283. return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
  1284. }
  1285. Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
  1286. MDNode *FPMathTag = nullptr) {
  1287. return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
  1288. }
  1289. Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
  1290. MDNode *FPMathTag = nullptr) {
  1291. return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
  1292. }
  1293. Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
  1294. MDNode *FPMathTag = nullptr) {
  1295. return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
  1296. }
  1297. Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
  1298. MDNode *FPMathTag = nullptr) {
  1299. return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
  1300. }
  1301. Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
  1302. MDNode *FPMathTag = nullptr) {
  1303. return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
  1304. }
  1305. Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
  1306. MDNode *FPMathTag = nullptr) {
  1307. return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
  1308. }
  1309. Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
  1310. MDNode *FPMathTag = nullptr) {
  1311. return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
  1312. }
  1313. Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
  1314. MDNode *FPMathTag = nullptr) {
  1315. return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
  1316. }
  1317. Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
  1318. MDNode *FPMathTag = nullptr) {
  1319. return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
  1320. }
  1321. Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
  1322. MDNode *FPMathTag = nullptr) {
  1323. return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
  1324. }
  1325. Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
  1326. MDNode *FPMathTag = nullptr) {
  1327. return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
  1328. }
  1329. Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
  1330. MDNode *FPMathTag = nullptr) {
  1331. return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
  1332. }
  1333. Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
  1334. MDNode *FPMathTag = nullptr) {
  1335. return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
  1336. }
  1337. Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1338. const Twine &Name = "") {
  1339. if (AllowFolding)
  1340. if (Constant *LC = dyn_cast<Constant>(LHS))
  1341. if (Constant *RC = dyn_cast<Constant>(RHS))
  1342. return Insert(Folder.CreateICmp(P, LC, RC), Name);
  1343. return Insert(new ICmpInst(P, LHS, RHS), Name);
  1344. }
  1345. Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1346. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1347. if (AllowFolding)
  1348. if (Constant *LC = dyn_cast<Constant>(LHS))
  1349. if (Constant *RC = dyn_cast<Constant>(RHS))
  1350. return Insert(Folder.CreateFCmp(P, LC, RC), Name);
  1351. return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
  1352. FPMathTag, FMF), Name);
  1353. }
  1354. //===--------------------------------------------------------------------===//
  1355. // Instruction creation methods: Other Instructions
  1356. //===--------------------------------------------------------------------===//
  1357. PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
  1358. const Twine &Name = "") {
  1359. return Insert(PHINode::Create(Ty, NumReservedValues), Name);
  1360. }
  1361. CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
  1362. const Twine &Name = "") {
  1363. return Insert(CallInst::Create(Callee, Args), Name);
  1364. }
  1365. CallInst *CreateCall(llvm::FunctionType *FTy, Value *Callee,
  1366. ArrayRef<Value *> Args, const Twine &Name = "") {
  1367. return Insert(CallInst::Create(FTy, Callee, Args), Name);
  1368. }
  1369. CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
  1370. const Twine &Name = "") {
  1371. return CreateCall(Callee->getFunctionType(), Callee, Args, Name);
  1372. }
  1373. Value *CreateSelect(Value *C, Value *True, Value *False,
  1374. const Twine &Name = "") {
  1375. if (AllowFolding)
  1376. if (Constant *CC = dyn_cast<Constant>(C))
  1377. if (Constant *TC = dyn_cast<Constant>(True))
  1378. if (Constant *FC = dyn_cast<Constant>(False))
  1379. return Insert(Folder.CreateSelect(CC, TC, FC), Name);
  1380. return Insert(SelectInst::Create(C, True, False), Name);
  1381. }
  1382. VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
  1383. return Insert(new VAArgInst(List, Ty), Name);
  1384. }
  1385. Value *CreateExtractElement(Value *Vec, Value *Idx,
  1386. const Twine &Name = "") {
  1387. if (AllowFolding)
  1388. if (Constant *VC = dyn_cast<Constant>(Vec))
  1389. if (Constant *IC = dyn_cast<Constant>(Idx))
  1390. return Insert(Folder.CreateExtractElement(VC, IC), Name);
  1391. return Insert(ExtractElementInst::Create(Vec, Idx), Name);
  1392. }
  1393. Value *CreateExtractElement(Value *Vec, uint64_t Idx,
  1394. const Twine &Name = "") {
  1395. return CreateExtractElement(Vec, getInt64(Idx), Name);
  1396. }
  1397. Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
  1398. const Twine &Name = "") {
  1399. if (AllowFolding)
  1400. if (Constant *VC = dyn_cast<Constant>(Vec))
  1401. if (Constant *NC = dyn_cast<Constant>(NewElt))
  1402. if (Constant *IC = dyn_cast<Constant>(Idx))
  1403. return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
  1404. return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
  1405. }
  1406. Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
  1407. const Twine &Name = "") {
  1408. return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
  1409. }
  1410. Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
  1411. const Twine &Name = "") {
  1412. if (AllowFolding)
  1413. if (Constant *V1C = dyn_cast<Constant>(V1))
  1414. if (Constant *V2C = dyn_cast<Constant>(V2))
  1415. if (Constant *MC = dyn_cast<Constant>(Mask))
  1416. return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
  1417. return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
  1418. }
  1419. Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
  1420. const Twine &Name = "") {
  1421. size_t MaskSize = IntMask.size();
  1422. SmallVector<Constant*, 8> MaskVec(MaskSize);
  1423. for (size_t i = 0; i != MaskSize; ++i)
  1424. MaskVec[i] = getInt32(IntMask[i]);
  1425. Value *Mask = ConstantVector::get(MaskVec);
  1426. return CreateShuffleVector(V1, V2, Mask, Name);
  1427. }
  1428. Value *CreateExtractValue(Value *Agg,
  1429. ArrayRef<unsigned> Idxs,
  1430. const Twine &Name = "") {
  1431. if (AllowFolding)
  1432. if (Constant *AggC = dyn_cast<Constant>(Agg))
  1433. return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
  1434. return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
  1435. }
  1436. Value *CreateInsertValue(Value *Agg, Value *Val,
  1437. ArrayRef<unsigned> Idxs,
  1438. const Twine &Name = "") {
  1439. if (AllowFolding)
  1440. if (Constant *AggC = dyn_cast<Constant>(Agg))
  1441. if (Constant *ValC = dyn_cast<Constant>(Val))
  1442. return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
  1443. return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
  1444. }
  1445. LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
  1446. const Twine &Name = "") {
  1447. return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
  1448. }
  1449. //===--------------------------------------------------------------------===//
  1450. // Utility creation methods
  1451. //===--------------------------------------------------------------------===//
  1452. /// \brief Return an i1 value testing if \p Arg is null.
  1453. Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
  1454. return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
  1455. Name);
  1456. }
  1457. /// \brief Return an i1 value testing if \p Arg is not null.
  1458. Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
  1459. return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
  1460. Name);
  1461. }
  1462. /// \brief Return the i64 difference between two pointer values, dividing out
  1463. /// the size of the pointed-to objects.
  1464. ///
  1465. /// This is intended to implement C-style pointer subtraction. As such, the
  1466. /// pointers must be appropriately aligned for their element types and
  1467. /// pointing into the same object.
  1468. Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
  1469. assert(LHS->getType() == RHS->getType() &&
  1470. "Pointer subtraction operand types must match!");
  1471. PointerType *ArgType = cast<PointerType>(LHS->getType());
  1472. Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
  1473. Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
  1474. Value *Difference = CreateSub(LHS_int, RHS_int);
  1475. return CreateExactSDiv(Difference,
  1476. ConstantExpr::getSizeOf(ArgType->getElementType()),
  1477. Name);
  1478. }
  1479. /// \brief Return a vector value that contains \arg V broadcasted to \p
  1480. /// NumElts elements.
  1481. Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
  1482. assert(NumElts > 0 && "Cannot splat to an empty vector!");
  1483. // First insert it into an undef vector so we can shuffle it.
  1484. Type *I32Ty = getInt32Ty();
  1485. Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
  1486. V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
  1487. Name + ".splatinsert");
  1488. // Shuffle the value across the desired number of elements.
  1489. Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
  1490. return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
  1491. }
  1492. /// \brief Return a value that has been extracted from a larger integer type.
  1493. Value *CreateExtractInteger(const DataLayout &DL, Value *From,
  1494. IntegerType *ExtractedTy, uint64_t Offset,
  1495. const Twine &Name) {
  1496. IntegerType *IntTy = cast<IntegerType>(From->getType());
  1497. assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
  1498. DL.getTypeStoreSize(IntTy) &&
  1499. "Element extends past full value");
  1500. uint64_t ShAmt = 8 * Offset;
  1501. Value *V = From;
  1502. if (DL.isBigEndian())
  1503. ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
  1504. DL.getTypeStoreSize(ExtractedTy) - Offset);
  1505. if (ShAmt) {
  1506. V = CreateLShr(V, ShAmt, Name + ".shift");
  1507. }
  1508. assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
  1509. "Cannot extract to a larger integer!");
  1510. if (ExtractedTy != IntTy) {
  1511. V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
  1512. }
  1513. return V;
  1514. }
  1515. /// \brief Create an assume intrinsic call that represents an alignment
  1516. /// assumption on the provided pointer.
  1517. ///
  1518. /// An optional offset can be provided, and if it is provided, the offset
  1519. /// must be subtracted from the provided pointer to get the pointer with the
  1520. /// specified alignment.
  1521. CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
  1522. unsigned Alignment,
  1523. Value *OffsetValue = nullptr) {
  1524. assert(isa<PointerType>(PtrValue->getType()) &&
  1525. "trying to create an alignment assumption on a non-pointer?");
  1526. PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
  1527. Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
  1528. Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
  1529. Value *Mask = ConstantInt::get(IntPtrTy,
  1530. Alignment > 0 ? Alignment - 1 : 0);
  1531. if (OffsetValue) {
  1532. bool IsOffsetZero = false;
  1533. if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
  1534. IsOffsetZero = CI->isZero();
  1535. if (!IsOffsetZero) {
  1536. if (OffsetValue->getType() != IntPtrTy)
  1537. OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
  1538. "offsetcast");
  1539. PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
  1540. }
  1541. }
  1542. Value *Zero = ConstantInt::get(IntPtrTy, 0);
  1543. Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
  1544. Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
  1545. return CreateAssumption(InvCond);
  1546. }
  1547. };
  1548. // Create wrappers for C Binding types (see CBindingWrapping.h).
  1549. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
  1550. }
  1551. #endif