IRBuilder.h 110 KB

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