IRBuilder.h 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  1. //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the IRBuilder class, which is used as a convenient way
  10. // to create LLVM instructions with a consistent and simplified interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_IRBUILDER_H
  14. #define LLVM_IR_IRBUILDER_H
  15. #include "llvm-c/Types.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/None.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/Twine.h"
  21. #include "llvm/IR/BasicBlock.h"
  22. #include "llvm/IR/Constant.h"
  23. #include "llvm/IR/ConstantFolder.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/DataLayout.h"
  26. #include "llvm/IR/DebugInfoMetadata.h"
  27. #include "llvm/IR/DebugLoc.h"
  28. #include "llvm/IR/DerivedTypes.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/GlobalVariable.h"
  31. #include "llvm/IR/InstrTypes.h"
  32. #include "llvm/IR/Instruction.h"
  33. #include "llvm/IR/Instructions.h"
  34. #include "llvm/IR/IntrinsicInst.h"
  35. #include "llvm/IR/LLVMContext.h"
  36. #include "llvm/IR/Module.h"
  37. #include "llvm/IR/Operator.h"
  38. #include "llvm/IR/Type.h"
  39. #include "llvm/IR/Value.h"
  40. #include "llvm/IR/ValueHandle.h"
  41. #include "llvm/Support/AtomicOrdering.h"
  42. #include "llvm/Support/CBindingWrapping.h"
  43. #include "llvm/Support/Casting.h"
  44. #include <cassert>
  45. #include <cstddef>
  46. #include <cstdint>
  47. #include <functional>
  48. #include <utility>
  49. namespace llvm {
  50. class APInt;
  51. class MDNode;
  52. class Use;
  53. /// This provides the default implementation of the IRBuilder
  54. /// 'InsertHelper' method that is called whenever an instruction is created by
  55. /// IRBuilder and needs to be inserted.
  56. ///
  57. /// By default, this inserts the instruction at the insertion point.
  58. class IRBuilderDefaultInserter {
  59. public:
  60. virtual ~IRBuilderDefaultInserter();
  61. virtual void InsertHelper(Instruction *I, const Twine &Name,
  62. BasicBlock *BB,
  63. BasicBlock::iterator InsertPt) const {
  64. if (BB) BB->getInstList().insert(InsertPt, I);
  65. I->setName(Name);
  66. }
  67. };
  68. /// Provides an 'InsertHelper' that calls a user-provided callback after
  69. /// performing the default insertion.
  70. class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
  71. std::function<void(Instruction *)> Callback;
  72. public:
  73. virtual ~IRBuilderCallbackInserter();
  74. IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
  75. : Callback(std::move(Callback)) {}
  76. void InsertHelper(Instruction *I, const Twine &Name,
  77. BasicBlock *BB,
  78. BasicBlock::iterator InsertPt) const override {
  79. IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
  80. Callback(I);
  81. }
  82. };
  83. /// Common base class shared among various IRBuilders.
  84. class IRBuilderBase {
  85. /// Pairs of (metadata kind, MDNode *) that should be added to all newly
  86. /// created instructions, like !dbg metadata.
  87. SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
  88. /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
  89. /// null. If \p MD is null, remove the entry with \p Kind.
  90. void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
  91. if (!MD) {
  92. erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
  93. return KV.first == Kind;
  94. });
  95. return;
  96. }
  97. for (auto &KV : MetadataToCopy)
  98. if (KV.first == Kind) {
  99. KV.second = MD;
  100. return;
  101. }
  102. MetadataToCopy.emplace_back(Kind, MD);
  103. }
  104. protected:
  105. BasicBlock *BB;
  106. BasicBlock::iterator InsertPt;
  107. LLVMContext &Context;
  108. const IRBuilderFolder &Folder;
  109. const IRBuilderDefaultInserter &Inserter;
  110. MDNode *DefaultFPMathTag;
  111. FastMathFlags FMF;
  112. bool IsFPConstrained;
  113. fp::ExceptionBehavior DefaultConstrainedExcept;
  114. RoundingMode DefaultConstrainedRounding;
  115. ArrayRef<OperandBundleDef> DefaultOperandBundles;
  116. public:
  117. IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
  118. const IRBuilderDefaultInserter &Inserter,
  119. MDNode *FPMathTag, ArrayRef<OperandBundleDef> OpBundles)
  120. : Context(context), Folder(Folder), Inserter(Inserter),
  121. DefaultFPMathTag(FPMathTag), IsFPConstrained(false),
  122. DefaultConstrainedExcept(fp::ebStrict),
  123. DefaultConstrainedRounding(RoundingMode::Dynamic),
  124. DefaultOperandBundles(OpBundles) {
  125. ClearInsertionPoint();
  126. }
  127. /// Insert and return the specified instruction.
  128. template<typename InstTy>
  129. InstTy *Insert(InstTy *I, const Twine &Name = "") const {
  130. Inserter.InsertHelper(I, Name, BB, InsertPt);
  131. AddMetadataToInst(I);
  132. return I;
  133. }
  134. /// No-op overload to handle constants.
  135. Constant *Insert(Constant *C, const Twine& = "") const {
  136. return C;
  137. }
  138. Value *Insert(Value *V, const Twine &Name = "") const {
  139. if (Instruction *I = dyn_cast<Instruction>(V))
  140. return Insert(I, Name);
  141. assert(isa<Constant>(V));
  142. return V;
  143. }
  144. //===--------------------------------------------------------------------===//
  145. // Builder configuration methods
  146. //===--------------------------------------------------------------------===//
  147. /// Clear the insertion point: created instructions will not be
  148. /// inserted into a block.
  149. void ClearInsertionPoint() {
  150. BB = nullptr;
  151. InsertPt = BasicBlock::iterator();
  152. }
  153. BasicBlock *GetInsertBlock() const { return BB; }
  154. BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
  155. LLVMContext &getContext() const { return Context; }
  156. /// This specifies that created instructions should be appended to the
  157. /// end of the specified block.
  158. void SetInsertPoint(BasicBlock *TheBB) {
  159. BB = TheBB;
  160. InsertPt = BB->end();
  161. }
  162. /// This specifies that created instructions should be inserted before
  163. /// the specified instruction.
  164. void SetInsertPoint(Instruction *I) {
  165. BB = I->getParent();
  166. InsertPt = I->getIterator();
  167. assert(InsertPt != BB->end() && "Can't read debug loc from end()");
  168. SetCurrentDebugLocation(I->getDebugLoc());
  169. }
  170. /// This specifies that created instructions should be inserted at the
  171. /// specified point.
  172. void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
  173. BB = TheBB;
  174. InsertPt = IP;
  175. if (IP != TheBB->end())
  176. SetCurrentDebugLocation(IP->getDebugLoc());
  177. }
  178. /// Set location information used by debugging information.
  179. void SetCurrentDebugLocation(DebugLoc L) {
  180. AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
  181. }
  182. /// Collect metadata with IDs \p MetadataKinds from \p Src which should be
  183. /// added to all created instructions. Entries present in MedataDataToCopy but
  184. /// not on \p Src will be dropped from MetadataToCopy.
  185. void CollectMetadataToCopy(Instruction *Src,
  186. ArrayRef<unsigned> MetadataKinds) {
  187. for (unsigned K : MetadataKinds)
  188. AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
  189. }
  190. /// Get location information used by debugging information.
  191. DebugLoc getCurrentDebugLocation() const {
  192. for (auto &KV : MetadataToCopy)
  193. if (KV.first == LLVMContext::MD_dbg)
  194. return {cast<DILocation>(KV.second)};
  195. return {};
  196. }
  197. /// If this builder has a current debug location, set it on the
  198. /// specified instruction.
  199. void SetInstDebugLocation(Instruction *I) const {
  200. for (const auto &KV : MetadataToCopy)
  201. if (KV.first == LLVMContext::MD_dbg) {
  202. I->setDebugLoc(DebugLoc(KV.second));
  203. return;
  204. }
  205. }
  206. /// Add all entries in MetadataToCopy to \p I.
  207. void AddMetadataToInst(Instruction *I) const {
  208. for (auto &KV : MetadataToCopy)
  209. I->setMetadata(KV.first, KV.second);
  210. }
  211. /// Get the return type of the current function that we're emitting
  212. /// into.
  213. Type *getCurrentFunctionReturnType() const;
  214. /// InsertPoint - A saved insertion point.
  215. class InsertPoint {
  216. BasicBlock *Block = nullptr;
  217. BasicBlock::iterator Point;
  218. public:
  219. /// Creates a new insertion point which doesn't point to anything.
  220. InsertPoint() = default;
  221. /// Creates a new insertion point at the given location.
  222. InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
  223. : Block(InsertBlock), Point(InsertPoint) {}
  224. /// Returns true if this insert point is set.
  225. bool isSet() const { return (Block != nullptr); }
  226. BasicBlock *getBlock() const { return Block; }
  227. BasicBlock::iterator getPoint() const { return Point; }
  228. };
  229. /// Returns the current insert point.
  230. InsertPoint saveIP() const {
  231. return InsertPoint(GetInsertBlock(), GetInsertPoint());
  232. }
  233. /// Returns the current insert point, clearing it in the process.
  234. InsertPoint saveAndClearIP() {
  235. InsertPoint IP(GetInsertBlock(), GetInsertPoint());
  236. ClearInsertionPoint();
  237. return IP;
  238. }
  239. /// Sets the current insert point to a previously-saved location.
  240. void restoreIP(InsertPoint IP) {
  241. if (IP.isSet())
  242. SetInsertPoint(IP.getBlock(), IP.getPoint());
  243. else
  244. ClearInsertionPoint();
  245. }
  246. /// Get the floating point math metadata being used.
  247. MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
  248. /// Get the flags to be applied to created floating point ops
  249. FastMathFlags getFastMathFlags() const { return FMF; }
  250. FastMathFlags &getFastMathFlags() { return FMF; }
  251. /// Clear the fast-math flags.
  252. void clearFastMathFlags() { FMF.clear(); }
  253. /// Set the floating point math metadata to be used.
  254. void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
  255. /// Set the fast-math flags to be used with generated fp-math operators
  256. void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
  257. /// Enable/Disable use of constrained floating point math. When
  258. /// enabled the CreateF<op>() calls instead create constrained
  259. /// floating point intrinsic calls. Fast math flags are unaffected
  260. /// by this setting.
  261. void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
  262. /// Query for the use of constrained floating point math
  263. bool getIsFPConstrained() { return IsFPConstrained; }
  264. /// Set the exception handling to be used with constrained floating point
  265. void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
  266. #ifndef NDEBUG
  267. Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(NewExcept);
  268. assert(ExceptStr.hasValue() && "Garbage strict exception behavior!");
  269. #endif
  270. DefaultConstrainedExcept = NewExcept;
  271. }
  272. /// Set the rounding mode handling to be used with constrained floating point
  273. void setDefaultConstrainedRounding(RoundingMode NewRounding) {
  274. #ifndef NDEBUG
  275. Optional<StringRef> RoundingStr = RoundingModeToStr(NewRounding);
  276. assert(RoundingStr.hasValue() && "Garbage strict rounding mode!");
  277. #endif
  278. DefaultConstrainedRounding = NewRounding;
  279. }
  280. /// Get the exception handling used with constrained floating point
  281. fp::ExceptionBehavior getDefaultConstrainedExcept() {
  282. return DefaultConstrainedExcept;
  283. }
  284. /// Get the rounding mode handling used with constrained floating point
  285. RoundingMode getDefaultConstrainedRounding() {
  286. return DefaultConstrainedRounding;
  287. }
  288. void setConstrainedFPFunctionAttr() {
  289. assert(BB && "Must have a basic block to set any function attributes!");
  290. Function *F = BB->getParent();
  291. if (!F->hasFnAttribute(Attribute::StrictFP)) {
  292. F->addFnAttr(Attribute::StrictFP);
  293. }
  294. }
  295. void setConstrainedFPCallAttr(CallBase *I) {
  296. I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP);
  297. }
  298. void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
  299. DefaultOperandBundles = OpBundles;
  300. }
  301. //===--------------------------------------------------------------------===//
  302. // RAII helpers.
  303. //===--------------------------------------------------------------------===//
  304. // RAII object that stores the current insertion point and restores it
  305. // when the object is destroyed. This includes the debug location.
  306. class InsertPointGuard {
  307. IRBuilderBase &Builder;
  308. AssertingVH<BasicBlock> Block;
  309. BasicBlock::iterator Point;
  310. DebugLoc DbgLoc;
  311. public:
  312. InsertPointGuard(IRBuilderBase &B)
  313. : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
  314. DbgLoc(B.getCurrentDebugLocation()) {}
  315. InsertPointGuard(const InsertPointGuard &) = delete;
  316. InsertPointGuard &operator=(const InsertPointGuard &) = delete;
  317. ~InsertPointGuard() {
  318. Builder.restoreIP(InsertPoint(Block, Point));
  319. Builder.SetCurrentDebugLocation(DbgLoc);
  320. }
  321. };
  322. // RAII object that stores the current fast math settings and restores
  323. // them when the object is destroyed.
  324. class FastMathFlagGuard {
  325. IRBuilderBase &Builder;
  326. FastMathFlags FMF;
  327. MDNode *FPMathTag;
  328. bool IsFPConstrained;
  329. fp::ExceptionBehavior DefaultConstrainedExcept;
  330. RoundingMode DefaultConstrainedRounding;
  331. public:
  332. FastMathFlagGuard(IRBuilderBase &B)
  333. : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
  334. IsFPConstrained(B.IsFPConstrained),
  335. DefaultConstrainedExcept(B.DefaultConstrainedExcept),
  336. DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
  337. FastMathFlagGuard(const FastMathFlagGuard &) = delete;
  338. FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
  339. ~FastMathFlagGuard() {
  340. Builder.FMF = FMF;
  341. Builder.DefaultFPMathTag = FPMathTag;
  342. Builder.IsFPConstrained = IsFPConstrained;
  343. Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
  344. Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
  345. }
  346. };
  347. // RAII object that stores the current default operand bundles and restores
  348. // them when the object is destroyed.
  349. class OperandBundlesGuard {
  350. IRBuilderBase &Builder;
  351. ArrayRef<OperandBundleDef> DefaultOperandBundles;
  352. public:
  353. OperandBundlesGuard(IRBuilderBase &B)
  354. : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
  355. OperandBundlesGuard(const OperandBundlesGuard &) = delete;
  356. OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
  357. ~OperandBundlesGuard() {
  358. Builder.DefaultOperandBundles = DefaultOperandBundles;
  359. }
  360. };
  361. //===--------------------------------------------------------------------===//
  362. // Miscellaneous creation methods.
  363. //===--------------------------------------------------------------------===//
  364. /// Make a new global variable with initializer type i8*
  365. ///
  366. /// Make a new global variable with an initializer that has array of i8 type
  367. /// filled in with the null terminated string value specified. The new global
  368. /// variable will be marked mergable with any others of the same contents. If
  369. /// Name is specified, it is the name of the global variable created.
  370. ///
  371. /// If no module is given via \p M, it is take from the insertion point basic
  372. /// block.
  373. GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
  374. unsigned AddressSpace = 0,
  375. Module *M = nullptr);
  376. /// Get a constant value representing either true or false.
  377. ConstantInt *getInt1(bool V) {
  378. return ConstantInt::get(getInt1Ty(), V);
  379. }
  380. /// Get the constant value for i1 true.
  381. ConstantInt *getTrue() {
  382. return ConstantInt::getTrue(Context);
  383. }
  384. /// Get the constant value for i1 false.
  385. ConstantInt *getFalse() {
  386. return ConstantInt::getFalse(Context);
  387. }
  388. /// Get a constant 8-bit value.
  389. ConstantInt *getInt8(uint8_t C) {
  390. return ConstantInt::get(getInt8Ty(), C);
  391. }
  392. /// Get a constant 16-bit value.
  393. ConstantInt *getInt16(uint16_t C) {
  394. return ConstantInt::get(getInt16Ty(), C);
  395. }
  396. /// Get a constant 32-bit value.
  397. ConstantInt *getInt32(uint32_t C) {
  398. return ConstantInt::get(getInt32Ty(), C);
  399. }
  400. /// Get a constant 64-bit value.
  401. ConstantInt *getInt64(uint64_t C) {
  402. return ConstantInt::get(getInt64Ty(), C);
  403. }
  404. /// Get a constant N-bit value, zero extended or truncated from
  405. /// a 64-bit value.
  406. ConstantInt *getIntN(unsigned N, uint64_t C) {
  407. return ConstantInt::get(getIntNTy(N), C);
  408. }
  409. /// Get a constant integer value.
  410. ConstantInt *getInt(const APInt &AI) {
  411. return ConstantInt::get(Context, AI);
  412. }
  413. //===--------------------------------------------------------------------===//
  414. // Type creation methods
  415. //===--------------------------------------------------------------------===//
  416. /// Fetch the type representing a single bit
  417. IntegerType *getInt1Ty() {
  418. return Type::getInt1Ty(Context);
  419. }
  420. /// Fetch the type representing an 8-bit integer.
  421. IntegerType *getInt8Ty() {
  422. return Type::getInt8Ty(Context);
  423. }
  424. /// Fetch the type representing a 16-bit integer.
  425. IntegerType *getInt16Ty() {
  426. return Type::getInt16Ty(Context);
  427. }
  428. /// Fetch the type representing a 32-bit integer.
  429. IntegerType *getInt32Ty() {
  430. return Type::getInt32Ty(Context);
  431. }
  432. /// Fetch the type representing a 64-bit integer.
  433. IntegerType *getInt64Ty() {
  434. return Type::getInt64Ty(Context);
  435. }
  436. /// Fetch the type representing a 128-bit integer.
  437. IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
  438. /// Fetch the type representing an N-bit integer.
  439. IntegerType *getIntNTy(unsigned N) {
  440. return Type::getIntNTy(Context, N);
  441. }
  442. /// Fetch the type representing a 16-bit floating point value.
  443. Type *getHalfTy() {
  444. return Type::getHalfTy(Context);
  445. }
  446. /// Fetch the type representing a 16-bit brain floating point value.
  447. Type *getBFloatTy() {
  448. return Type::getBFloatTy(Context);
  449. }
  450. /// Fetch the type representing a 32-bit floating point value.
  451. Type *getFloatTy() {
  452. return Type::getFloatTy(Context);
  453. }
  454. /// Fetch the type representing a 64-bit floating point value.
  455. Type *getDoubleTy() {
  456. return Type::getDoubleTy(Context);
  457. }
  458. /// Fetch the type representing void.
  459. Type *getVoidTy() {
  460. return Type::getVoidTy(Context);
  461. }
  462. /// Fetch the type representing a pointer to an 8-bit integer value.
  463. PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
  464. return Type::getInt8PtrTy(Context, AddrSpace);
  465. }
  466. /// Fetch the type representing a pointer to an integer value.
  467. IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
  468. return DL.getIntPtrType(Context, AddrSpace);
  469. }
  470. //===--------------------------------------------------------------------===//
  471. // Intrinsic creation methods
  472. //===--------------------------------------------------------------------===//
  473. /// Create and insert a memset to the specified pointer and the
  474. /// specified value.
  475. ///
  476. /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
  477. /// specified, it will be added to the instruction. Likewise with alias.scope
  478. /// and noalias tags.
  479. CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
  480. MaybeAlign Align, bool isVolatile = false,
  481. MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
  482. MDNode *NoAliasTag = nullptr) {
  483. return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
  484. TBAATag, ScopeTag, NoAliasTag);
  485. }
  486. CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
  487. bool isVolatile = false, MDNode *TBAATag = nullptr,
  488. MDNode *ScopeTag = nullptr,
  489. MDNode *NoAliasTag = nullptr);
  490. /// Create and insert an element unordered-atomic memset of the region of
  491. /// memory starting at the given pointer to the given value.
  492. ///
  493. /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
  494. /// specified, it will be added to the instruction. Likewise with alias.scope
  495. /// and noalias tags.
  496. CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
  497. uint64_t Size, Align Alignment,
  498. uint32_t ElementSize,
  499. MDNode *TBAATag = nullptr,
  500. MDNode *ScopeTag = nullptr,
  501. MDNode *NoAliasTag = nullptr) {
  502. return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
  503. Align(Alignment), ElementSize,
  504. TBAATag, ScopeTag, NoAliasTag);
  505. }
  506. CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
  507. Value *Size, Align Alignment,
  508. uint32_t ElementSize,
  509. MDNode *TBAATag = nullptr,
  510. MDNode *ScopeTag = nullptr,
  511. MDNode *NoAliasTag = nullptr);
  512. /// Create and insert a memcpy between the specified pointers.
  513. ///
  514. /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
  515. /// specified, it will be added to the instruction. Likewise with alias.scope
  516. /// and noalias tags.
  517. CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
  518. MaybeAlign SrcAlign, uint64_t Size,
  519. bool isVolatile = false, MDNode *TBAATag = nullptr,
  520. MDNode *TBAAStructTag = nullptr,
  521. MDNode *ScopeTag = nullptr,
  522. MDNode *NoAliasTag = nullptr) {
  523. return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
  524. isVolatile, TBAATag, TBAAStructTag, ScopeTag,
  525. NoAliasTag);
  526. }
  527. CallInst *CreateMemTransferInst(
  528. Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
  529. MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
  530. MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
  531. MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
  532. CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
  533. MaybeAlign SrcAlign, Value *Size,
  534. bool isVolatile = false, MDNode *TBAATag = nullptr,
  535. MDNode *TBAAStructTag = nullptr,
  536. MDNode *ScopeTag = nullptr,
  537. MDNode *NoAliasTag = nullptr) {
  538. return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
  539. SrcAlign, Size, isVolatile, TBAATag,
  540. TBAAStructTag, ScopeTag, NoAliasTag);
  541. }
  542. CallInst *CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
  543. MaybeAlign SrcAlign, Value *Size);
  544. /// Create and insert an element unordered-atomic memcpy between the
  545. /// specified pointers.
  546. ///
  547. /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
  548. ///
  549. /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
  550. /// specified, it will be added to the instruction. Likewise with alias.scope
  551. /// and noalias tags.
  552. CallInst *CreateElementUnorderedAtomicMemCpy(
  553. Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
  554. uint32_t ElementSize, MDNode *TBAATag = nullptr,
  555. MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
  556. MDNode *NoAliasTag = nullptr);
  557. CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
  558. MaybeAlign SrcAlign, uint64_t Size,
  559. bool isVolatile = false, MDNode *TBAATag = nullptr,
  560. MDNode *ScopeTag = nullptr,
  561. MDNode *NoAliasTag = nullptr) {
  562. return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
  563. isVolatile, TBAATag, ScopeTag, NoAliasTag);
  564. }
  565. CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
  566. MaybeAlign SrcAlign, Value *Size,
  567. bool isVolatile = false, MDNode *TBAATag = nullptr,
  568. MDNode *ScopeTag = nullptr,
  569. MDNode *NoAliasTag = nullptr);
  570. /// \brief Create and insert an element unordered-atomic memmove between the
  571. /// specified pointers.
  572. ///
  573. /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
  574. /// respectively.
  575. ///
  576. /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
  577. /// specified, it will be added to the instruction. Likewise with alias.scope
  578. /// and noalias tags.
  579. CallInst *CreateElementUnorderedAtomicMemMove(
  580. Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
  581. uint32_t ElementSize, MDNode *TBAATag = nullptr,
  582. MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
  583. MDNode *NoAliasTag = nullptr);
  584. /// Create a vector fadd reduction intrinsic of the source vector.
  585. /// The first parameter is a scalar accumulator value for ordered reductions.
  586. CallInst *CreateFAddReduce(Value *Acc, Value *Src);
  587. /// Create a vector fmul reduction intrinsic of the source vector.
  588. /// The first parameter is a scalar accumulator value for ordered reductions.
  589. CallInst *CreateFMulReduce(Value *Acc, Value *Src);
  590. /// Create a vector int add reduction intrinsic of the source vector.
  591. CallInst *CreateAddReduce(Value *Src);
  592. /// Create a vector int mul reduction intrinsic of the source vector.
  593. CallInst *CreateMulReduce(Value *Src);
  594. /// Create a vector int AND reduction intrinsic of the source vector.
  595. CallInst *CreateAndReduce(Value *Src);
  596. /// Create a vector int OR reduction intrinsic of the source vector.
  597. CallInst *CreateOrReduce(Value *Src);
  598. /// Create a vector int XOR reduction intrinsic of the source vector.
  599. CallInst *CreateXorReduce(Value *Src);
  600. /// Create a vector integer max reduction intrinsic of the source
  601. /// vector.
  602. CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
  603. /// Create a vector integer min reduction intrinsic of the source
  604. /// vector.
  605. CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
  606. /// Create a vector float max reduction intrinsic of the source
  607. /// vector.
  608. CallInst *CreateFPMaxReduce(Value *Src);
  609. /// Create a vector float min reduction intrinsic of the source
  610. /// vector.
  611. CallInst *CreateFPMinReduce(Value *Src);
  612. /// Create a lifetime.start intrinsic.
  613. ///
  614. /// If the pointer isn't i8* it will be converted.
  615. CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
  616. /// Create a lifetime.end intrinsic.
  617. ///
  618. /// If the pointer isn't i8* it will be converted.
  619. CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
  620. /// Create a call to invariant.start intrinsic.
  621. ///
  622. /// If the pointer isn't i8* it will be converted.
  623. CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
  624. /// Create a call to Masked Load intrinsic
  625. CallInst *CreateMaskedLoad(Value *Ptr, Align Alignment, Value *Mask,
  626. Value *PassThru = nullptr, const Twine &Name = "");
  627. /// Create a call to Masked Store intrinsic
  628. CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
  629. Value *Mask);
  630. /// Create a call to Masked Gather intrinsic
  631. CallInst *CreateMaskedGather(Value *Ptrs, Align Alignment,
  632. Value *Mask = nullptr, Value *PassThru = nullptr,
  633. const Twine &Name = "");
  634. /// Create a call to Masked Scatter intrinsic
  635. CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
  636. Value *Mask = nullptr);
  637. /// Create an assume intrinsic call that allows the optimizer to
  638. /// assume that the provided condition will be true.
  639. ///
  640. /// The optional argument \p OpBundles specifies operand bundles that are
  641. /// added to the call instruction.
  642. CallInst *CreateAssumption(Value *Cond,
  643. ArrayRef<OperandBundleDef> OpBundles = llvm::None);
  644. /// Create a llvm.experimental.noalias.scope.decl intrinsic call.
  645. Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
  646. Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
  647. return CreateNoAliasScopeDeclaration(
  648. MetadataAsValue::get(Context, ScopeTag));
  649. }
  650. /// Create a call to the experimental.gc.statepoint intrinsic to
  651. /// start a new statepoint sequence.
  652. CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
  653. Value *ActualCallee,
  654. ArrayRef<Value *> CallArgs,
  655. Optional<ArrayRef<Value *>> DeoptArgs,
  656. ArrayRef<Value *> GCArgs,
  657. const Twine &Name = "");
  658. /// Create a call to the experimental.gc.statepoint intrinsic to
  659. /// start a new statepoint sequence.
  660. CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
  661. Value *ActualCallee, uint32_t Flags,
  662. ArrayRef<Value *> CallArgs,
  663. Optional<ArrayRef<Use>> TransitionArgs,
  664. Optional<ArrayRef<Use>> DeoptArgs,
  665. ArrayRef<Value *> GCArgs,
  666. const Twine &Name = "");
  667. /// Conveninence function for the common case when CallArgs are filled
  668. /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
  669. /// .get()'ed to get the Value pointer.
  670. CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
  671. Value *ActualCallee, ArrayRef<Use> CallArgs,
  672. Optional<ArrayRef<Value *>> DeoptArgs,
  673. ArrayRef<Value *> GCArgs,
  674. const Twine &Name = "");
  675. /// Create an invoke to the experimental.gc.statepoint intrinsic to
  676. /// start a new statepoint sequence.
  677. InvokeInst *
  678. CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
  679. Value *ActualInvokee, BasicBlock *NormalDest,
  680. BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
  681. Optional<ArrayRef<Value *>> DeoptArgs,
  682. ArrayRef<Value *> GCArgs, const Twine &Name = "");
  683. /// Create an invoke to the experimental.gc.statepoint intrinsic to
  684. /// start a new statepoint sequence.
  685. InvokeInst *CreateGCStatepointInvoke(
  686. uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
  687. BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
  688. ArrayRef<Value *> InvokeArgs, Optional<ArrayRef<Use>> TransitionArgs,
  689. Optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
  690. const Twine &Name = "");
  691. // Convenience function for the common case when CallArgs are filled in using
  692. // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
  693. // get the Value *.
  694. InvokeInst *
  695. CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
  696. Value *ActualInvokee, BasicBlock *NormalDest,
  697. BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
  698. Optional<ArrayRef<Value *>> DeoptArgs,
  699. ArrayRef<Value *> GCArgs, const Twine &Name = "");
  700. /// Create a call to the experimental.gc.result intrinsic to extract
  701. /// the result from a call wrapped in a statepoint.
  702. CallInst *CreateGCResult(Instruction *Statepoint,
  703. Type *ResultType,
  704. const Twine &Name = "");
  705. /// Create a call to the experimental.gc.relocate intrinsics to
  706. /// project the relocated value of one pointer from the statepoint.
  707. CallInst *CreateGCRelocate(Instruction *Statepoint,
  708. int BaseOffset,
  709. int DerivedOffset,
  710. Type *ResultType,
  711. const Twine &Name = "");
  712. /// Create a call to the experimental.gc.pointer.base intrinsic to get the
  713. /// base pointer for the specified derived pointer.
  714. CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
  715. /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get
  716. /// the offset of the specified derived pointer from its base.
  717. CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
  718. /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
  719. /// will be the same type as that of \p Scaling.
  720. Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
  721. /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
  722. Value *CreateStepVector(Type *DstType, const Twine &Name = "");
  723. /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
  724. /// type.
  725. CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
  726. Instruction *FMFSource = nullptr,
  727. const Twine &Name = "");
  728. /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
  729. /// first type.
  730. CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
  731. Instruction *FMFSource = nullptr,
  732. const Twine &Name = "");
  733. /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
  734. /// \p FMFSource is provided, copy fast-math-flags from that instruction to
  735. /// the intrinsic.
  736. CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
  737. ArrayRef<Value *> Args,
  738. Instruction *FMFSource = nullptr,
  739. const Twine &Name = "");
  740. /// Create call to the minnum intrinsic.
  741. CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
  742. return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
  743. }
  744. /// Create call to the maxnum intrinsic.
  745. CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
  746. return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
  747. }
  748. /// Create call to the minimum intrinsic.
  749. CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
  750. return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
  751. }
  752. /// Create call to the maximum intrinsic.
  753. CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
  754. return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
  755. }
  756. /// Create a call to the experimental.vector.extract intrinsic.
  757. CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
  758. const Twine &Name = "") {
  759. return CreateIntrinsic(Intrinsic::experimental_vector_extract,
  760. {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
  761. Name);
  762. }
  763. /// Create a call to the experimental.vector.insert intrinsic.
  764. CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
  765. Value *Idx, const Twine &Name = "") {
  766. return CreateIntrinsic(Intrinsic::experimental_vector_insert,
  767. {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
  768. nullptr, Name);
  769. }
  770. private:
  771. /// Create a call to a masked intrinsic with given Id.
  772. CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
  773. ArrayRef<Type *> OverloadedTypes,
  774. const Twine &Name = "");
  775. Value *getCastedInt8PtrValue(Value *Ptr);
  776. //===--------------------------------------------------------------------===//
  777. // Instruction creation methods: Terminators
  778. //===--------------------------------------------------------------------===//
  779. private:
  780. /// Helper to add branch weight and unpredictable metadata onto an
  781. /// instruction.
  782. /// \returns The annotated instruction.
  783. template <typename InstTy>
  784. InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
  785. if (Weights)
  786. I->setMetadata(LLVMContext::MD_prof, Weights);
  787. if (Unpredictable)
  788. I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
  789. return I;
  790. }
  791. public:
  792. /// Create a 'ret void' instruction.
  793. ReturnInst *CreateRetVoid() {
  794. return Insert(ReturnInst::Create(Context));
  795. }
  796. /// Create a 'ret <val>' instruction.
  797. ReturnInst *CreateRet(Value *V) {
  798. return Insert(ReturnInst::Create(Context, V));
  799. }
  800. /// Create a sequence of N insertvalue instructions,
  801. /// with one Value from the retVals array each, that build a aggregate
  802. /// return value one value at a time, and a ret instruction to return
  803. /// the resulting aggregate value.
  804. ///
  805. /// This is a convenience function for code that uses aggregate return values
  806. /// as a vehicle for having multiple return values.
  807. ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
  808. Value *V = UndefValue::get(getCurrentFunctionReturnType());
  809. for (unsigned i = 0; i != N; ++i)
  810. V = CreateInsertValue(V, retVals[i], i, "mrv");
  811. return Insert(ReturnInst::Create(Context, V));
  812. }
  813. /// Create an unconditional 'br label X' instruction.
  814. BranchInst *CreateBr(BasicBlock *Dest) {
  815. return Insert(BranchInst::Create(Dest));
  816. }
  817. /// Create a conditional 'br Cond, TrueDest, FalseDest'
  818. /// instruction.
  819. BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
  820. MDNode *BranchWeights = nullptr,
  821. MDNode *Unpredictable = nullptr) {
  822. return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
  823. BranchWeights, Unpredictable));
  824. }
  825. /// Create a conditional 'br Cond, TrueDest, FalseDest'
  826. /// instruction. Copy branch meta data if available.
  827. BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
  828. Instruction *MDSrc) {
  829. BranchInst *Br = BranchInst::Create(True, False, Cond);
  830. if (MDSrc) {
  831. unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
  832. LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
  833. Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
  834. }
  835. return Insert(Br);
  836. }
  837. /// Create a switch instruction with the specified value, default dest,
  838. /// and with a hint for the number of cases that will be added (for efficient
  839. /// allocation).
  840. SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
  841. MDNode *BranchWeights = nullptr,
  842. MDNode *Unpredictable = nullptr) {
  843. return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
  844. BranchWeights, Unpredictable));
  845. }
  846. /// Create an indirect branch instruction with the specified address
  847. /// operand, with an optional hint for the number of destinations that will be
  848. /// added (for efficient allocation).
  849. IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
  850. return Insert(IndirectBrInst::Create(Addr, NumDests));
  851. }
  852. /// Create an invoke instruction.
  853. InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
  854. BasicBlock *NormalDest, BasicBlock *UnwindDest,
  855. ArrayRef<Value *> Args,
  856. ArrayRef<OperandBundleDef> OpBundles,
  857. const Twine &Name = "") {
  858. InvokeInst *II =
  859. InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles);
  860. if (IsFPConstrained)
  861. setConstrainedFPCallAttr(II);
  862. return Insert(II, Name);
  863. }
  864. InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
  865. BasicBlock *NormalDest, BasicBlock *UnwindDest,
  866. ArrayRef<Value *> Args = None,
  867. const Twine &Name = "") {
  868. InvokeInst *II =
  869. InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args);
  870. if (IsFPConstrained)
  871. setConstrainedFPCallAttr(II);
  872. return Insert(II, Name);
  873. }
  874. InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
  875. BasicBlock *UnwindDest, ArrayRef<Value *> Args,
  876. ArrayRef<OperandBundleDef> OpBundles,
  877. const Twine &Name = "") {
  878. return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
  879. NormalDest, UnwindDest, Args, OpBundles, Name);
  880. }
  881. InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
  882. BasicBlock *UnwindDest,
  883. ArrayRef<Value *> Args = None,
  884. const Twine &Name = "") {
  885. return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
  886. NormalDest, UnwindDest, Args, Name);
  887. }
  888. /// \brief Create a callbr instruction.
  889. CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
  890. BasicBlock *DefaultDest,
  891. ArrayRef<BasicBlock *> IndirectDests,
  892. ArrayRef<Value *> Args = None,
  893. const Twine &Name = "") {
  894. return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
  895. Args), Name);
  896. }
  897. CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
  898. BasicBlock *DefaultDest,
  899. ArrayRef<BasicBlock *> IndirectDests,
  900. ArrayRef<Value *> Args,
  901. ArrayRef<OperandBundleDef> OpBundles,
  902. const Twine &Name = "") {
  903. return Insert(
  904. CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
  905. OpBundles), Name);
  906. }
  907. CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
  908. ArrayRef<BasicBlock *> IndirectDests,
  909. ArrayRef<Value *> Args = None,
  910. const Twine &Name = "") {
  911. return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
  912. DefaultDest, IndirectDests, Args, Name);
  913. }
  914. CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
  915. ArrayRef<BasicBlock *> IndirectDests,
  916. ArrayRef<Value *> Args,
  917. ArrayRef<OperandBundleDef> OpBundles,
  918. const Twine &Name = "") {
  919. return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
  920. DefaultDest, IndirectDests, Args, Name);
  921. }
  922. ResumeInst *CreateResume(Value *Exn) {
  923. return Insert(ResumeInst::Create(Exn));
  924. }
  925. CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
  926. BasicBlock *UnwindBB = nullptr) {
  927. return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
  928. }
  929. CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
  930. unsigned NumHandlers,
  931. const Twine &Name = "") {
  932. return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
  933. Name);
  934. }
  935. CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
  936. const Twine &Name = "") {
  937. return Insert(CatchPadInst::Create(ParentPad, Args), Name);
  938. }
  939. CleanupPadInst *CreateCleanupPad(Value *ParentPad,
  940. ArrayRef<Value *> Args = None,
  941. const Twine &Name = "") {
  942. return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
  943. }
  944. CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
  945. return Insert(CatchReturnInst::Create(CatchPad, BB));
  946. }
  947. UnreachableInst *CreateUnreachable() {
  948. return Insert(new UnreachableInst(Context));
  949. }
  950. //===--------------------------------------------------------------------===//
  951. // Instruction creation methods: Binary Operators
  952. //===--------------------------------------------------------------------===//
  953. private:
  954. BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
  955. Value *LHS, Value *RHS,
  956. const Twine &Name,
  957. bool HasNUW, bool HasNSW) {
  958. BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
  959. if (HasNUW) BO->setHasNoUnsignedWrap();
  960. if (HasNSW) BO->setHasNoSignedWrap();
  961. return BO;
  962. }
  963. Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
  964. FastMathFlags FMF) const {
  965. if (!FPMD)
  966. FPMD = DefaultFPMathTag;
  967. if (FPMD)
  968. I->setMetadata(LLVMContext::MD_fpmath, FPMD);
  969. I->setFastMathFlags(FMF);
  970. return I;
  971. }
  972. Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
  973. Value *R, const Twine &Name) const {
  974. auto *LC = dyn_cast<Constant>(L);
  975. auto *RC = dyn_cast<Constant>(R);
  976. return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
  977. }
  978. Value *getConstrainedFPRounding(Optional<RoundingMode> Rounding) {
  979. RoundingMode UseRounding = DefaultConstrainedRounding;
  980. if (Rounding.hasValue())
  981. UseRounding = Rounding.getValue();
  982. Optional<StringRef> RoundingStr = RoundingModeToStr(UseRounding);
  983. assert(RoundingStr.hasValue() && "Garbage strict rounding mode!");
  984. auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue());
  985. return MetadataAsValue::get(Context, RoundingMDS);
  986. }
  987. Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) {
  988. fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept;
  989. if (Except.hasValue())
  990. UseExcept = Except.getValue();
  991. Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(UseExcept);
  992. assert(ExceptStr.hasValue() && "Garbage strict exception behavior!");
  993. auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue());
  994. return MetadataAsValue::get(Context, ExceptMDS);
  995. }
  996. Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
  997. assert(CmpInst::isFPPredicate(Predicate) &&
  998. Predicate != CmpInst::FCMP_FALSE &&
  999. Predicate != CmpInst::FCMP_TRUE &&
  1000. "Invalid constrained FP comparison predicate!");
  1001. StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
  1002. auto *PredicateMDS = MDString::get(Context, PredicateStr);
  1003. return MetadataAsValue::get(Context, PredicateMDS);
  1004. }
  1005. public:
  1006. Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  1007. bool HasNUW = false, bool HasNSW = false) {
  1008. if (auto *LC = dyn_cast<Constant>(LHS))
  1009. if (auto *RC = dyn_cast<Constant>(RHS))
  1010. return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
  1011. return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
  1012. HasNUW, HasNSW);
  1013. }
  1014. Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  1015. return CreateAdd(LHS, RHS, Name, false, true);
  1016. }
  1017. Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  1018. return CreateAdd(LHS, RHS, Name, true, false);
  1019. }
  1020. Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
  1021. bool HasNUW = false, bool HasNSW = false) {
  1022. if (auto *LC = dyn_cast<Constant>(LHS))
  1023. if (auto *RC = dyn_cast<Constant>(RHS))
  1024. return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
  1025. return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
  1026. HasNUW, HasNSW);
  1027. }
  1028. Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  1029. return CreateSub(LHS, RHS, Name, false, true);
  1030. }
  1031. Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  1032. return CreateSub(LHS, RHS, Name, true, false);
  1033. }
  1034. Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
  1035. bool HasNUW = false, bool HasNSW = false) {
  1036. if (auto *LC = dyn_cast<Constant>(LHS))
  1037. if (auto *RC = dyn_cast<Constant>(RHS))
  1038. return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
  1039. return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
  1040. HasNUW, HasNSW);
  1041. }
  1042. Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  1043. return CreateMul(LHS, RHS, Name, false, true);
  1044. }
  1045. Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  1046. return CreateMul(LHS, RHS, Name, true, false);
  1047. }
  1048. Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  1049. bool isExact = false) {
  1050. if (auto *LC = dyn_cast<Constant>(LHS))
  1051. if (auto *RC = dyn_cast<Constant>(RHS))
  1052. return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
  1053. if (!isExact)
  1054. return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
  1055. return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
  1056. }
  1057. Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  1058. return CreateUDiv(LHS, RHS, Name, true);
  1059. }
  1060. Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  1061. bool isExact = false) {
  1062. if (auto *LC = dyn_cast<Constant>(LHS))
  1063. if (auto *RC = dyn_cast<Constant>(RHS))
  1064. return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
  1065. if (!isExact)
  1066. return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
  1067. return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
  1068. }
  1069. Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  1070. return CreateSDiv(LHS, RHS, Name, true);
  1071. }
  1072. Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
  1073. if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
  1074. return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
  1075. }
  1076. Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
  1077. if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
  1078. return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
  1079. }
  1080. Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
  1081. bool HasNUW = false, bool HasNSW = false) {
  1082. if (auto *LC = dyn_cast<Constant>(LHS))
  1083. if (auto *RC = dyn_cast<Constant>(RHS))
  1084. return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
  1085. return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
  1086. HasNUW, HasNSW);
  1087. }
  1088. Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
  1089. bool HasNUW = false, bool HasNSW = false) {
  1090. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  1091. HasNUW, HasNSW);
  1092. }
  1093. Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
  1094. bool HasNUW = false, bool HasNSW = false) {
  1095. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  1096. HasNUW, HasNSW);
  1097. }
  1098. Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
  1099. bool isExact = false) {
  1100. if (auto *LC = dyn_cast<Constant>(LHS))
  1101. if (auto *RC = dyn_cast<Constant>(RHS))
  1102. return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
  1103. if (!isExact)
  1104. return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
  1105. return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
  1106. }
  1107. Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  1108. bool isExact = false) {
  1109. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  1110. }
  1111. Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  1112. bool isExact = false) {
  1113. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  1114. }
  1115. Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
  1116. bool isExact = false) {
  1117. if (auto *LC = dyn_cast<Constant>(LHS))
  1118. if (auto *RC = dyn_cast<Constant>(RHS))
  1119. return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
  1120. if (!isExact)
  1121. return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
  1122. return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
  1123. }
  1124. Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  1125. bool isExact = false) {
  1126. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  1127. }
  1128. Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  1129. bool isExact = false) {
  1130. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  1131. }
  1132. Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
  1133. if (auto *RC = dyn_cast<Constant>(RHS)) {
  1134. if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
  1135. return LHS; // LHS & -1 -> LHS
  1136. if (auto *LC = dyn_cast<Constant>(LHS))
  1137. return Insert(Folder.CreateAnd(LC, RC), Name);
  1138. }
  1139. return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
  1140. }
  1141. Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  1142. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  1143. }
  1144. Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  1145. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  1146. }
  1147. Value *CreateAnd(ArrayRef<Value*> Ops) {
  1148. assert(!Ops.empty());
  1149. Value *Accum = Ops[0];
  1150. for (unsigned i = 1; i < Ops.size(); i++)
  1151. Accum = CreateAnd(Accum, Ops[i]);
  1152. return Accum;
  1153. }
  1154. Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
  1155. if (auto *RC = dyn_cast<Constant>(RHS)) {
  1156. if (RC->isNullValue())
  1157. return LHS; // LHS | 0 -> LHS
  1158. if (auto *LC = dyn_cast<Constant>(LHS))
  1159. return Insert(Folder.CreateOr(LC, RC), Name);
  1160. }
  1161. return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
  1162. }
  1163. Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  1164. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  1165. }
  1166. Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  1167. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  1168. }
  1169. Value *CreateOr(ArrayRef<Value*> Ops) {
  1170. assert(!Ops.empty());
  1171. Value *Accum = Ops[0];
  1172. for (unsigned i = 1; i < Ops.size(); i++)
  1173. Accum = CreateOr(Accum, Ops[i]);
  1174. return Accum;
  1175. }
  1176. Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
  1177. if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
  1178. return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
  1179. }
  1180. Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  1181. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  1182. }
  1183. Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  1184. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  1185. }
  1186. Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
  1187. MDNode *FPMD = nullptr) {
  1188. if (IsFPConstrained)
  1189. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
  1190. L, R, nullptr, Name, FPMD);
  1191. if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
  1192. Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
  1193. return Insert(I, Name);
  1194. }
  1195. /// Copy fast-math-flags from an instruction rather than using the builder's
  1196. /// default FMF.
  1197. Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
  1198. const Twine &Name = "") {
  1199. if (IsFPConstrained)
  1200. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
  1201. L, R, FMFSource, Name);
  1202. if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
  1203. Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
  1204. FMFSource->getFastMathFlags());
  1205. return Insert(I, Name);
  1206. }
  1207. Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
  1208. MDNode *FPMD = nullptr) {
  1209. if (IsFPConstrained)
  1210. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
  1211. L, R, nullptr, Name, FPMD);
  1212. if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
  1213. Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
  1214. return Insert(I, Name);
  1215. }
  1216. /// Copy fast-math-flags from an instruction rather than using the builder's
  1217. /// default FMF.
  1218. Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
  1219. const Twine &Name = "") {
  1220. if (IsFPConstrained)
  1221. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
  1222. L, R, FMFSource, Name);
  1223. if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
  1224. Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
  1225. FMFSource->getFastMathFlags());
  1226. return Insert(I, Name);
  1227. }
  1228. Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
  1229. MDNode *FPMD = nullptr) {
  1230. if (IsFPConstrained)
  1231. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
  1232. L, R, nullptr, Name, FPMD);
  1233. if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
  1234. Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
  1235. return Insert(I, Name);
  1236. }
  1237. /// Copy fast-math-flags from an instruction rather than using the builder's
  1238. /// default FMF.
  1239. Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
  1240. const Twine &Name = "") {
  1241. if (IsFPConstrained)
  1242. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
  1243. L, R, FMFSource, Name);
  1244. if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
  1245. Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
  1246. FMFSource->getFastMathFlags());
  1247. return Insert(I, Name);
  1248. }
  1249. Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
  1250. MDNode *FPMD = nullptr) {
  1251. if (IsFPConstrained)
  1252. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
  1253. L, R, nullptr, Name, FPMD);
  1254. if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
  1255. Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
  1256. return Insert(I, Name);
  1257. }
  1258. /// Copy fast-math-flags from an instruction rather than using the builder's
  1259. /// default FMF.
  1260. Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
  1261. const Twine &Name = "") {
  1262. if (IsFPConstrained)
  1263. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
  1264. L, R, FMFSource, Name);
  1265. if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
  1266. Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
  1267. FMFSource->getFastMathFlags());
  1268. return Insert(I, Name);
  1269. }
  1270. Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
  1271. MDNode *FPMD = nullptr) {
  1272. if (IsFPConstrained)
  1273. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
  1274. L, R, nullptr, Name, FPMD);
  1275. if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
  1276. Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
  1277. return Insert(I, Name);
  1278. }
  1279. /// Copy fast-math-flags from an instruction rather than using the builder's
  1280. /// default FMF.
  1281. Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
  1282. const Twine &Name = "") {
  1283. if (IsFPConstrained)
  1284. return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
  1285. L, R, FMFSource, Name);
  1286. if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
  1287. Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
  1288. FMFSource->getFastMathFlags());
  1289. return Insert(I, Name);
  1290. }
  1291. Value *CreateBinOp(Instruction::BinaryOps Opc,
  1292. Value *LHS, Value *RHS, const Twine &Name = "",
  1293. MDNode *FPMathTag = nullptr) {
  1294. if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
  1295. Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
  1296. if (isa<FPMathOperator>(BinOp))
  1297. setFPAttrs(BinOp, FPMathTag, FMF);
  1298. return Insert(BinOp, Name);
  1299. }
  1300. Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
  1301. assert(Cond2->getType()->isIntOrIntVectorTy(1));
  1302. return CreateSelect(Cond1, Cond2,
  1303. ConstantInt::getNullValue(Cond2->getType()), Name);
  1304. }
  1305. Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
  1306. assert(Cond2->getType()->isIntOrIntVectorTy(1));
  1307. return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
  1308. Cond2, Name);
  1309. }
  1310. CallInst *CreateConstrainedFPBinOp(
  1311. Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
  1312. const Twine &Name = "", MDNode *FPMathTag = nullptr,
  1313. Optional<RoundingMode> Rounding = None,
  1314. Optional<fp::ExceptionBehavior> Except = None);
  1315. Value *CreateNeg(Value *V, const Twine &Name = "",
  1316. bool HasNUW = false, bool HasNSW = false) {
  1317. if (auto *VC = dyn_cast<Constant>(V))
  1318. return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
  1319. BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
  1320. if (HasNUW) BO->setHasNoUnsignedWrap();
  1321. if (HasNSW) BO->setHasNoSignedWrap();
  1322. return BO;
  1323. }
  1324. Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
  1325. return CreateNeg(V, Name, false, true);
  1326. }
  1327. Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
  1328. return CreateNeg(V, Name, true, false);
  1329. }
  1330. Value *CreateFNeg(Value *V, const Twine &Name = "",
  1331. MDNode *FPMathTag = nullptr) {
  1332. if (auto *VC = dyn_cast<Constant>(V))
  1333. return Insert(Folder.CreateFNeg(VC), Name);
  1334. return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF),
  1335. Name);
  1336. }
  1337. /// Copy fast-math-flags from an instruction rather than using the builder's
  1338. /// default FMF.
  1339. Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
  1340. const Twine &Name = "") {
  1341. if (auto *VC = dyn_cast<Constant>(V))
  1342. return Insert(Folder.CreateFNeg(VC), Name);
  1343. return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr,
  1344. FMFSource->getFastMathFlags()),
  1345. Name);
  1346. }
  1347. Value *CreateNot(Value *V, const Twine &Name = "") {
  1348. if (auto *VC = dyn_cast<Constant>(V))
  1349. return Insert(Folder.CreateNot(VC), Name);
  1350. return Insert(BinaryOperator::CreateNot(V), Name);
  1351. }
  1352. Value *CreateUnOp(Instruction::UnaryOps Opc,
  1353. Value *V, const Twine &Name = "",
  1354. MDNode *FPMathTag = nullptr) {
  1355. if (auto *VC = dyn_cast<Constant>(V))
  1356. return Insert(Folder.CreateUnOp(Opc, VC), Name);
  1357. Instruction *UnOp = UnaryOperator::Create(Opc, V);
  1358. if (isa<FPMathOperator>(UnOp))
  1359. setFPAttrs(UnOp, FPMathTag, FMF);
  1360. return Insert(UnOp, Name);
  1361. }
  1362. /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
  1363. /// Correct number of operands must be passed accordingly.
  1364. Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
  1365. const Twine &Name = "", MDNode *FPMathTag = nullptr);
  1366. //===--------------------------------------------------------------------===//
  1367. // Instruction creation methods: Memory Instructions
  1368. //===--------------------------------------------------------------------===//
  1369. AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
  1370. Value *ArraySize = nullptr, const Twine &Name = "") {
  1371. const DataLayout &DL = BB->getModule()->getDataLayout();
  1372. Align AllocaAlign = DL.getPrefTypeAlign(Ty);
  1373. return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
  1374. }
  1375. AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
  1376. const Twine &Name = "") {
  1377. const DataLayout &DL = BB->getModule()->getDataLayout();
  1378. Align AllocaAlign = DL.getPrefTypeAlign(Ty);
  1379. unsigned AddrSpace = DL.getAllocaAddrSpace();
  1380. return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
  1381. }
  1382. /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
  1383. /// converting the string to 'bool' for the isVolatile parameter.
  1384. LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
  1385. return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
  1386. }
  1387. LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
  1388. return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
  1389. }
  1390. LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
  1391. const Twine &Name = "") {
  1392. return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
  1393. }
  1394. // Deprecated [opaque pointer types]
  1395. LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateLoad(Value *Ptr,
  1396. const char *Name),
  1397. "Use the version that explicitly specifies the "
  1398. "loaded type instead") {
  1399. return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
  1400. }
  1401. // Deprecated [opaque pointer types]
  1402. LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateLoad(Value *Ptr,
  1403. const Twine &Name = ""),
  1404. "Use the version that explicitly specifies the "
  1405. "loaded type instead") {
  1406. return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
  1407. }
  1408. // Deprecated [opaque pointer types]
  1409. LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateLoad(Value *Ptr,
  1410. bool isVolatile,
  1411. const Twine &Name = ""),
  1412. "Use the version that explicitly specifies the "
  1413. "loaded type instead") {
  1414. return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile,
  1415. Name);
  1416. }
  1417. StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
  1418. return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
  1419. }
  1420. LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
  1421. const char *Name) {
  1422. return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
  1423. }
  1424. LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
  1425. const Twine &Name = "") {
  1426. return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
  1427. }
  1428. LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
  1429. bool isVolatile, const Twine &Name = "") {
  1430. if (!Align) {
  1431. const DataLayout &DL = BB->getModule()->getDataLayout();
  1432. Align = DL.getABITypeAlign(Ty);
  1433. }
  1434. return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
  1435. }
  1436. // Deprecated [opaque pointer types]
  1437. LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,
  1438. MaybeAlign Align,
  1439. const char *Name),
  1440. "Use the version that explicitly specifies the "
  1441. "loaded type instead") {
  1442. return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
  1443. Align, Name);
  1444. }
  1445. // Deprecated [opaque pointer types]
  1446. LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,
  1447. MaybeAlign Align,
  1448. const Twine &Name = ""),
  1449. "Use the version that explicitly specifies the "
  1450. "loaded type instead") {
  1451. return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
  1452. Align, Name);
  1453. }
  1454. // Deprecated [opaque pointer types]
  1455. LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,
  1456. MaybeAlign Align,
  1457. bool isVolatile,
  1458. const Twine &Name = ""),
  1459. "Use the version that explicitly specifies the "
  1460. "loaded type instead") {
  1461. return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
  1462. Align, isVolatile, Name);
  1463. }
  1464. StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
  1465. bool isVolatile = false) {
  1466. if (!Align) {
  1467. const DataLayout &DL = BB->getModule()->getDataLayout();
  1468. Align = DL.getABITypeAlign(Val->getType());
  1469. }
  1470. return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
  1471. }
  1472. FenceInst *CreateFence(AtomicOrdering Ordering,
  1473. SyncScope::ID SSID = SyncScope::System,
  1474. const Twine &Name = "") {
  1475. return Insert(new FenceInst(Context, Ordering, SSID), Name);
  1476. }
  1477. AtomicCmpXchgInst *
  1478. CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
  1479. AtomicOrdering SuccessOrdering,
  1480. AtomicOrdering FailureOrdering,
  1481. SyncScope::ID SSID = SyncScope::System) {
  1482. if (!Align) {
  1483. const DataLayout &DL = BB->getModule()->getDataLayout();
  1484. Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
  1485. }
  1486. return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
  1487. FailureOrdering, SSID));
  1488. }
  1489. AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
  1490. Value *Val, MaybeAlign Align,
  1491. AtomicOrdering Ordering,
  1492. SyncScope::ID SSID = SyncScope::System) {
  1493. if (!Align) {
  1494. const DataLayout &DL = BB->getModule()->getDataLayout();
  1495. Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
  1496. }
  1497. return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
  1498. }
  1499. Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  1500. const Twine &Name = "") {
  1501. return CreateGEP(nullptr, Ptr, IdxList, Name);
  1502. }
  1503. Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  1504. const Twine &Name = "") {
  1505. if (auto *PC = dyn_cast<Constant>(Ptr)) {
  1506. // Every index must be constant.
  1507. size_t i, e;
  1508. for (i = 0, e = IdxList.size(); i != e; ++i)
  1509. if (!isa<Constant>(IdxList[i]))
  1510. break;
  1511. if (i == e)
  1512. return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
  1513. }
  1514. return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
  1515. }
  1516. Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  1517. const Twine &Name = "") {
  1518. return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
  1519. }
  1520. Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  1521. const Twine &Name = "") {
  1522. if (auto *PC = dyn_cast<Constant>(Ptr)) {
  1523. // Every index must be constant.
  1524. size_t i, e;
  1525. for (i = 0, e = IdxList.size(); i != e; ++i)
  1526. if (!isa<Constant>(IdxList[i]))
  1527. break;
  1528. if (i == e)
  1529. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
  1530. Name);
  1531. }
  1532. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
  1533. }
  1534. Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
  1535. return CreateGEP(nullptr, Ptr, Idx, Name);
  1536. }
  1537. Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
  1538. if (auto *PC = dyn_cast<Constant>(Ptr))
  1539. if (auto *IC = dyn_cast<Constant>(Idx))
  1540. return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
  1541. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  1542. }
  1543. Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
  1544. const Twine &Name = "") {
  1545. if (auto *PC = dyn_cast<Constant>(Ptr))
  1546. if (auto *IC = dyn_cast<Constant>(Idx))
  1547. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
  1548. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  1549. }
  1550. Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
  1551. return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
  1552. }
  1553. Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
  1554. const Twine &Name = "") {
  1555. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  1556. if (auto *PC = dyn_cast<Constant>(Ptr))
  1557. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
  1558. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  1559. }
  1560. Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
  1561. const Twine &Name = "") {
  1562. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  1563. if (auto *PC = dyn_cast<Constant>(Ptr))
  1564. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
  1565. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  1566. }
  1567. Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
  1568. const Twine &Name = "") {
  1569. Value *Idxs[] = {
  1570. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  1571. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  1572. };
  1573. if (auto *PC = dyn_cast<Constant>(Ptr))
  1574. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
  1575. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
  1576. }
  1577. Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
  1578. unsigned Idx1, const Twine &Name = "") {
  1579. Value *Idxs[] = {
  1580. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  1581. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  1582. };
  1583. if (auto *PC = dyn_cast<Constant>(Ptr))
  1584. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
  1585. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
  1586. }
  1587. Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
  1588. const Twine &Name = "") {
  1589. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  1590. if (auto *PC = dyn_cast<Constant>(Ptr))
  1591. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
  1592. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
  1593. }
  1594. Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
  1595. return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name);
  1596. }
  1597. Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
  1598. const Twine &Name = "") {
  1599. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  1600. if (auto *PC = dyn_cast<Constant>(Ptr))
  1601. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
  1602. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
  1603. }
  1604. Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
  1605. const Twine &Name = "") {
  1606. return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name);
  1607. }
  1608. Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1609. const Twine &Name = "") {
  1610. Value *Idxs[] = {
  1611. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  1612. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  1613. };
  1614. if (auto *PC = dyn_cast<Constant>(Ptr))
  1615. return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
  1616. return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
  1617. }
  1618. Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1619. const Twine &Name = "") {
  1620. return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
  1621. }
  1622. Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
  1623. uint64_t Idx1, const Twine &Name = "") {
  1624. Value *Idxs[] = {
  1625. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  1626. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  1627. };
  1628. if (auto *PC = dyn_cast<Constant>(Ptr))
  1629. return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
  1630. return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
  1631. }
  1632. Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  1633. const Twine &Name = "") {
  1634. return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
  1635. }
  1636. Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
  1637. const Twine &Name = "") {
  1638. return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
  1639. }
  1640. Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
  1641. return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
  1642. }
  1643. /// Same as CreateGlobalString, but return a pointer with "i8*" type
  1644. /// instead of a pointer to array of i8.
  1645. ///
  1646. /// If no module is given via \p M, it is take from the insertion point basic
  1647. /// block.
  1648. Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
  1649. unsigned AddressSpace = 0,
  1650. Module *M = nullptr) {
  1651. GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
  1652. Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
  1653. Constant *Indices[] = {Zero, Zero};
  1654. return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
  1655. Indices);
  1656. }
  1657. //===--------------------------------------------------------------------===//
  1658. // Instruction creation methods: Cast/Conversion Operators
  1659. //===--------------------------------------------------------------------===//
  1660. Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
  1661. return CreateCast(Instruction::Trunc, V, DestTy, Name);
  1662. }
  1663. Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1664. return CreateCast(Instruction::ZExt, V, DestTy, Name);
  1665. }
  1666. Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1667. return CreateCast(Instruction::SExt, V, DestTy, Name);
  1668. }
  1669. /// Create a ZExt or Trunc from the integer value V to DestTy. Return
  1670. /// the value untouched if the type of V is already DestTy.
  1671. Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
  1672. const Twine &Name = "") {
  1673. assert(V->getType()->isIntOrIntVectorTy() &&
  1674. DestTy->isIntOrIntVectorTy() &&
  1675. "Can only zero extend/truncate integers!");
  1676. Type *VTy = V->getType();
  1677. if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
  1678. return CreateZExt(V, DestTy, Name);
  1679. if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
  1680. return CreateTrunc(V, DestTy, Name);
  1681. return V;
  1682. }
  1683. /// Create a SExt or Trunc from the integer value V to DestTy. Return
  1684. /// the value untouched if the type of V is already DestTy.
  1685. Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
  1686. const Twine &Name = "") {
  1687. assert(V->getType()->isIntOrIntVectorTy() &&
  1688. DestTy->isIntOrIntVectorTy() &&
  1689. "Can only sign extend/truncate integers!");
  1690. Type *VTy = V->getType();
  1691. if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
  1692. return CreateSExt(V, DestTy, Name);
  1693. if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
  1694. return CreateTrunc(V, DestTy, Name);
  1695. return V;
  1696. }
  1697. Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
  1698. if (IsFPConstrained)
  1699. return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
  1700. V, DestTy, nullptr, Name);
  1701. return CreateCast(Instruction::FPToUI, V, DestTy, Name);
  1702. }
  1703. Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
  1704. if (IsFPConstrained)
  1705. return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
  1706. V, DestTy, nullptr, Name);
  1707. return CreateCast(Instruction::FPToSI, V, DestTy, Name);
  1708. }
  1709. Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  1710. if (IsFPConstrained)
  1711. return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
  1712. V, DestTy, nullptr, Name);
  1713. return CreateCast(Instruction::UIToFP, V, DestTy, Name);
  1714. }
  1715. Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  1716. if (IsFPConstrained)
  1717. return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
  1718. V, DestTy, nullptr, Name);
  1719. return CreateCast(Instruction::SIToFP, V, DestTy, Name);
  1720. }
  1721. Value *CreateFPTrunc(Value *V, Type *DestTy,
  1722. const Twine &Name = "") {
  1723. if (IsFPConstrained)
  1724. return CreateConstrainedFPCast(
  1725. Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr,
  1726. Name);
  1727. return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
  1728. }
  1729. Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
  1730. if (IsFPConstrained)
  1731. return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
  1732. V, DestTy, nullptr, Name);
  1733. return CreateCast(Instruction::FPExt, V, DestTy, Name);
  1734. }
  1735. Value *CreatePtrToInt(Value *V, Type *DestTy,
  1736. const Twine &Name = "") {
  1737. return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
  1738. }
  1739. Value *CreateIntToPtr(Value *V, Type *DestTy,
  1740. const Twine &Name = "") {
  1741. return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
  1742. }
  1743. Value *CreateBitCast(Value *V, Type *DestTy,
  1744. const Twine &Name = "") {
  1745. return CreateCast(Instruction::BitCast, V, DestTy, Name);
  1746. }
  1747. Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
  1748. const Twine &Name = "") {
  1749. return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
  1750. }
  1751. Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
  1752. const Twine &Name = "") {
  1753. if (V->getType() == DestTy)
  1754. return V;
  1755. if (auto *VC = dyn_cast<Constant>(V))
  1756. return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
  1757. return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
  1758. }
  1759. Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
  1760. const Twine &Name = "") {
  1761. if (V->getType() == DestTy)
  1762. return V;
  1763. if (auto *VC = dyn_cast<Constant>(V))
  1764. return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
  1765. return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
  1766. }
  1767. Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
  1768. const Twine &Name = "") {
  1769. if (V->getType() == DestTy)
  1770. return V;
  1771. if (auto *VC = dyn_cast<Constant>(V))
  1772. return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
  1773. return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
  1774. }
  1775. Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
  1776. const Twine &Name = "") {
  1777. if (V->getType() == DestTy)
  1778. return V;
  1779. if (auto *VC = dyn_cast<Constant>(V))
  1780. return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
  1781. return Insert(CastInst::Create(Op, V, DestTy), Name);
  1782. }
  1783. Value *CreatePointerCast(Value *V, Type *DestTy,
  1784. const Twine &Name = "") {
  1785. if (V->getType() == DestTy)
  1786. return V;
  1787. if (auto *VC = dyn_cast<Constant>(V))
  1788. return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
  1789. return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
  1790. }
  1791. Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
  1792. const Twine &Name = "") {
  1793. if (V->getType() == DestTy)
  1794. return V;
  1795. if (auto *VC = dyn_cast<Constant>(V)) {
  1796. return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
  1797. Name);
  1798. }
  1799. return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
  1800. Name);
  1801. }
  1802. Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
  1803. const Twine &Name = "") {
  1804. if (V->getType() == DestTy)
  1805. return V;
  1806. if (auto *VC = dyn_cast<Constant>(V))
  1807. return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
  1808. return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
  1809. }
  1810. Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
  1811. const Twine &Name = "") {
  1812. if (V->getType() == DestTy)
  1813. return V;
  1814. if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
  1815. return CreatePtrToInt(V, DestTy, Name);
  1816. if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
  1817. return CreateIntToPtr(V, DestTy, Name);
  1818. return CreateBitCast(V, DestTy, Name);
  1819. }
  1820. Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
  1821. if (V->getType() == DestTy)
  1822. return V;
  1823. if (auto *VC = dyn_cast<Constant>(V))
  1824. return Insert(Folder.CreateFPCast(VC, DestTy), Name);
  1825. return Insert(CastInst::CreateFPCast(V, DestTy), Name);
  1826. }
  1827. CallInst *CreateConstrainedFPCast(
  1828. Intrinsic::ID ID, Value *V, Type *DestTy,
  1829. Instruction *FMFSource = nullptr, const Twine &Name = "",
  1830. MDNode *FPMathTag = nullptr,
  1831. Optional<RoundingMode> Rounding = None,
  1832. Optional<fp::ExceptionBehavior> Except = None);
  1833. // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
  1834. // compile time error, instead of converting the string to bool for the
  1835. // isSigned parameter.
  1836. Value *CreateIntCast(Value *, Type *, const char *) = delete;
  1837. //===--------------------------------------------------------------------===//
  1838. // Instruction creation methods: Compare Instructions
  1839. //===--------------------------------------------------------------------===//
  1840. Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
  1841. return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
  1842. }
  1843. Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1844. return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
  1845. }
  1846. Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1847. return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
  1848. }
  1849. Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1850. return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
  1851. }
  1852. Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1853. return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
  1854. }
  1855. Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1856. return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
  1857. }
  1858. Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1859. return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
  1860. }
  1861. Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1862. return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
  1863. }
  1864. Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1865. return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
  1866. }
  1867. Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1868. return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
  1869. }
  1870. Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
  1871. MDNode *FPMathTag = nullptr) {
  1872. return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
  1873. }
  1874. Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
  1875. MDNode *FPMathTag = nullptr) {
  1876. return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
  1877. }
  1878. Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
  1879. MDNode *FPMathTag = nullptr) {
  1880. return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
  1881. }
  1882. Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
  1883. MDNode *FPMathTag = nullptr) {
  1884. return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
  1885. }
  1886. Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
  1887. MDNode *FPMathTag = nullptr) {
  1888. return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
  1889. }
  1890. Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
  1891. MDNode *FPMathTag = nullptr) {
  1892. return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
  1893. }
  1894. Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
  1895. MDNode *FPMathTag = nullptr) {
  1896. return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
  1897. }
  1898. Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
  1899. MDNode *FPMathTag = nullptr) {
  1900. return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
  1901. }
  1902. Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
  1903. MDNode *FPMathTag = nullptr) {
  1904. return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
  1905. }
  1906. Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
  1907. MDNode *FPMathTag = nullptr) {
  1908. return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
  1909. }
  1910. Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
  1911. MDNode *FPMathTag = nullptr) {
  1912. return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
  1913. }
  1914. Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
  1915. MDNode *FPMathTag = nullptr) {
  1916. return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
  1917. }
  1918. Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
  1919. MDNode *FPMathTag = nullptr) {
  1920. return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
  1921. }
  1922. Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
  1923. MDNode *FPMathTag = nullptr) {
  1924. return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
  1925. }
  1926. Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1927. const Twine &Name = "") {
  1928. if (auto *LC = dyn_cast<Constant>(LHS))
  1929. if (auto *RC = dyn_cast<Constant>(RHS))
  1930. return Insert(Folder.CreateICmp(P, LC, RC), Name);
  1931. return Insert(new ICmpInst(P, LHS, RHS), Name);
  1932. }
  1933. // Create a quiet floating-point comparison (i.e. one that raises an FP
  1934. // exception only in the case where an input is a signaling NaN).
  1935. // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
  1936. Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1937. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1938. return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false);
  1939. }
  1940. Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
  1941. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1942. return CmpInst::isFPPredicate(Pred)
  1943. ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag)
  1944. : CreateICmp(Pred, LHS, RHS, Name);
  1945. }
  1946. // Create a signaling floating-point comparison (i.e. one that raises an FP
  1947. // exception whenever an input is any NaN, signaling or quiet).
  1948. // Note that this differs from CreateFCmp only if IsFPConstrained is true.
  1949. Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1950. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1951. return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true);
  1952. }
  1953. private:
  1954. // Helper routine to create either a signaling or a quiet FP comparison.
  1955. Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1956. const Twine &Name, MDNode *FPMathTag,
  1957. bool IsSignaling);
  1958. public:
  1959. CallInst *CreateConstrainedFPCmp(
  1960. Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
  1961. const Twine &Name = "", Optional<fp::ExceptionBehavior> Except = None);
  1962. //===--------------------------------------------------------------------===//
  1963. // Instruction creation methods: Other Instructions
  1964. //===--------------------------------------------------------------------===//
  1965. PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
  1966. const Twine &Name = "") {
  1967. PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
  1968. if (isa<FPMathOperator>(Phi))
  1969. setFPAttrs(Phi, nullptr /* MDNode* */, FMF);
  1970. return Insert(Phi, Name);
  1971. }
  1972. CallInst *CreateCall(FunctionType *FTy, Value *Callee,
  1973. ArrayRef<Value *> Args = None, const Twine &Name = "",
  1974. MDNode *FPMathTag = nullptr) {
  1975. CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
  1976. if (IsFPConstrained)
  1977. setConstrainedFPCallAttr(CI);
  1978. if (isa<FPMathOperator>(CI))
  1979. setFPAttrs(CI, FPMathTag, FMF);
  1980. return Insert(CI, Name);
  1981. }
  1982. CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
  1983. ArrayRef<OperandBundleDef> OpBundles,
  1984. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1985. CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
  1986. if (IsFPConstrained)
  1987. setConstrainedFPCallAttr(CI);
  1988. if (isa<FPMathOperator>(CI))
  1989. setFPAttrs(CI, FPMathTag, FMF);
  1990. return Insert(CI, Name);
  1991. }
  1992. CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,
  1993. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  1994. return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
  1995. FPMathTag);
  1996. }
  1997. CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
  1998. ArrayRef<OperandBundleDef> OpBundles,
  1999. const Twine &Name = "", MDNode *FPMathTag = nullptr) {
  2000. return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
  2001. OpBundles, Name, FPMathTag);
  2002. }
  2003. CallInst *CreateConstrainedFPCall(
  2004. Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
  2005. Optional<RoundingMode> Rounding = None,
  2006. Optional<fp::ExceptionBehavior> Except = None);
  2007. Value *CreateSelect(Value *C, Value *True, Value *False,
  2008. const Twine &Name = "", Instruction *MDFrom = nullptr);
  2009. VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
  2010. return Insert(new VAArgInst(List, Ty), Name);
  2011. }
  2012. Value *CreateExtractElement(Value *Vec, Value *Idx,
  2013. const Twine &Name = "") {
  2014. if (auto *VC = dyn_cast<Constant>(Vec))
  2015. if (auto *IC = dyn_cast<Constant>(Idx))
  2016. return Insert(Folder.CreateExtractElement(VC, IC), Name);
  2017. return Insert(ExtractElementInst::Create(Vec, Idx), Name);
  2018. }
  2019. Value *CreateExtractElement(Value *Vec, uint64_t Idx,
  2020. const Twine &Name = "") {
  2021. return CreateExtractElement(Vec, getInt64(Idx), Name);
  2022. }
  2023. Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
  2024. const Twine &Name = "") {
  2025. if (auto *VC = dyn_cast<Constant>(Vec))
  2026. if (auto *NC = dyn_cast<Constant>(NewElt))
  2027. if (auto *IC = dyn_cast<Constant>(Idx))
  2028. return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
  2029. return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
  2030. }
  2031. Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
  2032. const Twine &Name = "") {
  2033. return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
  2034. }
  2035. Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
  2036. const Twine &Name = "") {
  2037. SmallVector<int, 16> IntMask;
  2038. ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask);
  2039. return CreateShuffleVector(V1, V2, IntMask, Name);
  2040. }
  2041. LLVM_ATTRIBUTE_DEPRECATED(Value *CreateShuffleVector(Value *V1, Value *V2,
  2042. ArrayRef<uint32_t> Mask,
  2043. const Twine &Name = ""),
  2044. "Pass indices as 'int' instead") {
  2045. SmallVector<int, 16> IntMask;
  2046. IntMask.assign(Mask.begin(), Mask.end());
  2047. return CreateShuffleVector(V1, V2, IntMask, Name);
  2048. }
  2049. /// See class ShuffleVectorInst for a description of the mask representation.
  2050. Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
  2051. const Twine &Name = "") {
  2052. if (auto *V1C = dyn_cast<Constant>(V1))
  2053. if (auto *V2C = dyn_cast<Constant>(V2))
  2054. return Insert(Folder.CreateShuffleVector(V1C, V2C, Mask), Name);
  2055. return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
  2056. }
  2057. /// Create a unary shuffle. The second vector operand of the IR instruction
  2058. /// is poison.
  2059. Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
  2060. const Twine &Name = "") {
  2061. return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name);
  2062. }
  2063. Value *CreateExtractValue(Value *Agg,
  2064. ArrayRef<unsigned> Idxs,
  2065. const Twine &Name = "") {
  2066. if (auto *AggC = dyn_cast<Constant>(Agg))
  2067. return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
  2068. return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
  2069. }
  2070. Value *CreateInsertValue(Value *Agg, Value *Val,
  2071. ArrayRef<unsigned> Idxs,
  2072. const Twine &Name = "") {
  2073. if (auto *AggC = dyn_cast<Constant>(Agg))
  2074. if (auto *ValC = dyn_cast<Constant>(Val))
  2075. return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
  2076. return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
  2077. }
  2078. LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
  2079. const Twine &Name = "") {
  2080. return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
  2081. }
  2082. Value *CreateFreeze(Value *V, const Twine &Name = "") {
  2083. return Insert(new FreezeInst(V), Name);
  2084. }
  2085. //===--------------------------------------------------------------------===//
  2086. // Utility creation methods
  2087. //===--------------------------------------------------------------------===//
  2088. /// Return an i1 value testing if \p Arg is null.
  2089. Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
  2090. return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
  2091. Name);
  2092. }
  2093. /// Return an i1 value testing if \p Arg is not null.
  2094. Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
  2095. return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
  2096. Name);
  2097. }
  2098. /// Return the i64 difference between two pointer values, dividing out
  2099. /// the size of the pointed-to objects.
  2100. ///
  2101. /// This is intended to implement C-style pointer subtraction. As such, the
  2102. /// pointers must be appropriately aligned for their element types and
  2103. /// pointing into the same object.
  2104. Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "");
  2105. /// Create a launder.invariant.group intrinsic call. If Ptr type is
  2106. /// different from pointer to i8, it's casted to pointer to i8 in the same
  2107. /// address space before call and casted back to Ptr type after call.
  2108. Value *CreateLaunderInvariantGroup(Value *Ptr);
  2109. /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
  2110. /// different from pointer to i8, it's casted to pointer to i8 in the same
  2111. /// address space before call and casted back to Ptr type after call.
  2112. Value *CreateStripInvariantGroup(Value *Ptr);
  2113. /// Return a vector value that contains the vector V reversed
  2114. Value *CreateVectorReverse(Value *V, const Twine &Name = "");
  2115. /// Return a vector splice intrinsic if using scalable vectors, otherwise
  2116. /// return a shufflevector. If the immediate is positive, a vector is
  2117. /// extracted from concat(V1, V2), starting at Imm. If the immediate
  2118. /// is negative, we extract -Imm elements from V1 and the remaining
  2119. /// elements from V2. Imm is a signed integer in the range
  2120. /// -VL <= Imm < VL (where VL is the runtime vector length of the
  2121. /// source/result vector)
  2122. Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm,
  2123. const Twine &Name = "");
  2124. /// Return a vector value that contains \arg V broadcasted to \p
  2125. /// NumElts elements.
  2126. Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
  2127. /// Return a vector value that contains \arg V broadcasted to \p
  2128. /// EC elements.
  2129. Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
  2130. /// Return a value that has been extracted from a larger integer type.
  2131. Value *CreateExtractInteger(const DataLayout &DL, Value *From,
  2132. IntegerType *ExtractedTy, uint64_t Offset,
  2133. const Twine &Name);
  2134. Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
  2135. unsigned Dimension, unsigned LastIndex,
  2136. MDNode *DbgInfo);
  2137. Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
  2138. MDNode *DbgInfo);
  2139. Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
  2140. unsigned Index, unsigned FieldIndex,
  2141. MDNode *DbgInfo);
  2142. private:
  2143. /// Helper function that creates an assume intrinsic call that
  2144. /// represents an alignment assumption on the provided pointer \p PtrValue
  2145. /// with offset \p OffsetValue and alignment value \p AlignValue.
  2146. CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
  2147. Value *PtrValue, Value *AlignValue,
  2148. Value *OffsetValue);
  2149. public:
  2150. /// Create an assume intrinsic call that represents an alignment
  2151. /// assumption on the provided pointer.
  2152. ///
  2153. /// An optional offset can be provided, and if it is provided, the offset
  2154. /// must be subtracted from the provided pointer to get the pointer with the
  2155. /// specified alignment.
  2156. CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
  2157. unsigned Alignment,
  2158. Value *OffsetValue = nullptr);
  2159. /// Create an assume intrinsic call that represents an alignment
  2160. /// assumption on the provided pointer.
  2161. ///
  2162. /// An optional offset can be provided, and if it is provided, the offset
  2163. /// must be subtracted from the provided pointer to get the pointer with the
  2164. /// specified alignment.
  2165. ///
  2166. /// This overload handles the condition where the Alignment is dependent
  2167. /// on an existing value rather than a static value.
  2168. CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
  2169. Value *Alignment,
  2170. Value *OffsetValue = nullptr);
  2171. };
  2172. /// This provides a uniform API for creating instructions and inserting
  2173. /// them into a basic block: either at the end of a BasicBlock, or at a specific
  2174. /// iterator location in a block.
  2175. ///
  2176. /// Note that the builder does not expose the full generality of LLVM
  2177. /// instructions. For access to extra instruction properties, use the mutators
  2178. /// (e.g. setVolatile) on the instructions after they have been
  2179. /// created. Convenience state exists to specify fast-math flags and fp-math
  2180. /// tags.
  2181. ///
  2182. /// The first template argument specifies a class to use for creating constants.
  2183. /// This defaults to creating minimally folded constants. The second template
  2184. /// argument allows clients to specify custom insertion hooks that are called on
  2185. /// every newly created insertion.
  2186. template <typename FolderTy = ConstantFolder,
  2187. typename InserterTy = IRBuilderDefaultInserter>
  2188. class IRBuilder : public IRBuilderBase {
  2189. private:
  2190. FolderTy Folder;
  2191. InserterTy Inserter;
  2192. public:
  2193. IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
  2194. MDNode *FPMathTag = nullptr,
  2195. ArrayRef<OperandBundleDef> OpBundles = None)
  2196. : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
  2197. Folder(Folder), Inserter(Inserter) {}
  2198. explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
  2199. ArrayRef<OperandBundleDef> OpBundles = None)
  2200. : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
  2201. explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
  2202. MDNode *FPMathTag = nullptr,
  2203. ArrayRef<OperandBundleDef> OpBundles = None)
  2204. : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
  2205. FPMathTag, OpBundles), Folder(Folder) {
  2206. SetInsertPoint(TheBB);
  2207. }
  2208. explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
  2209. ArrayRef<OperandBundleDef> OpBundles = None)
  2210. : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
  2211. FPMathTag, OpBundles) {
  2212. SetInsertPoint(TheBB);
  2213. }
  2214. explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
  2215. ArrayRef<OperandBundleDef> OpBundles = None)
  2216. : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter,
  2217. FPMathTag, OpBundles) {
  2218. SetInsertPoint(IP);
  2219. }
  2220. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
  2221. MDNode *FPMathTag = nullptr,
  2222. ArrayRef<OperandBundleDef> OpBundles = None)
  2223. : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
  2224. FPMathTag, OpBundles), Folder(Folder) {
  2225. SetInsertPoint(TheBB, IP);
  2226. }
  2227. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
  2228. MDNode *FPMathTag = nullptr,
  2229. ArrayRef<OperandBundleDef> OpBundles = None)
  2230. : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
  2231. FPMathTag, OpBundles) {
  2232. SetInsertPoint(TheBB, IP);
  2233. }
  2234. /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard
  2235. /// or FastMathFlagGuard instead.
  2236. IRBuilder(const IRBuilder &) = delete;
  2237. InserterTy &getInserter() { return Inserter; }
  2238. };
  2239. // Create wrappers for C Binding types (see CBindingWrapping.h).
  2240. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
  2241. } // end namespace llvm
  2242. #endif // LLVM_IR_IRBUILDER_H