2
0

Instructions.h 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125
  1. //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file exposes the class definitions of all of the subclasses of the
  11. // Instruction class. This is meant to be an easy way to get access to all
  12. // instruction subclasses.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_IR_INSTRUCTIONS_H
  16. #define LLVM_IR_INSTRUCTIONS_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/iterator_range.h"
  20. #include "llvm/IR/Attributes.h"
  21. #include "llvm/IR/CallingConv.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/InstrTypes.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include <iterator>
  27. namespace llvm {
  28. class APInt;
  29. class ConstantInt;
  30. class ConstantRange;
  31. class DataLayout;
  32. class LLVMContext;
  33. enum AtomicOrdering {
  34. NotAtomic = 0,
  35. Unordered = 1,
  36. Monotonic = 2,
  37. // Consume = 3, // Not specified yet.
  38. Acquire = 4,
  39. Release = 5,
  40. AcquireRelease = 6,
  41. SequentiallyConsistent = 7
  42. };
  43. enum SynchronizationScope {
  44. SingleThread = 0,
  45. CrossThread = 1
  46. };
  47. /// Returns true if the ordering is at least as strong as acquire
  48. /// (i.e. acquire, acq_rel or seq_cst)
  49. inline bool isAtLeastAcquire(AtomicOrdering Ord) {
  50. return (Ord == Acquire ||
  51. Ord == AcquireRelease ||
  52. Ord == SequentiallyConsistent);
  53. }
  54. /// Returns true if the ordering is at least as strong as release
  55. /// (i.e. release, acq_rel or seq_cst)
  56. inline bool isAtLeastRelease(AtomicOrdering Ord) {
  57. return (Ord == Release ||
  58. Ord == AcquireRelease ||
  59. Ord == SequentiallyConsistent);
  60. }
  61. //===----------------------------------------------------------------------===//
  62. // AllocaInst Class
  63. //===----------------------------------------------------------------------===//
  64. /// AllocaInst - an instruction to allocate memory on the stack
  65. ///
  66. class AllocaInst : public UnaryInstruction {
  67. Type *AllocatedType;
  68. protected:
  69. // Note: Instruction needs to be a friend here to call cloneImpl.
  70. friend class Instruction;
  71. AllocaInst *cloneImpl() const;
  72. public:
  73. explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
  74. const Twine &Name = "",
  75. Instruction *InsertBefore = nullptr);
  76. AllocaInst(Type *Ty, Value *ArraySize,
  77. const Twine &Name, BasicBlock *InsertAtEnd);
  78. AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
  79. AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
  80. AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
  81. const Twine &Name = "", Instruction *InsertBefore = nullptr);
  82. AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
  83. const Twine &Name, BasicBlock *InsertAtEnd);
  84. // Out of line virtual method, so the vtable, etc. has a home.
  85. ~AllocaInst() override;
  86. /// isArrayAllocation - Return true if there is an allocation size parameter
  87. /// to the allocation instruction that is not 1.
  88. ///
  89. bool isArrayAllocation() const;
  90. /// getArraySize - Get the number of elements allocated. For a simple
  91. /// allocation of a single element, this will return a constant 1 value.
  92. ///
  93. const Value *getArraySize() const { return getOperand(0); }
  94. Value *getArraySize() { return getOperand(0); }
  95. /// getType - Overload to return most specific pointer type
  96. ///
  97. PointerType *getType() const {
  98. return cast<PointerType>(Instruction::getType());
  99. }
  100. /// getAllocatedType - Return the type that is being allocated by the
  101. /// instruction.
  102. ///
  103. Type *getAllocatedType() const { return AllocatedType; }
  104. /// \brief for use only in special circumstances that need to generically
  105. /// transform a whole instruction (eg: IR linking and vectorization).
  106. void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
  107. /// getAlignment - Return the alignment of the memory that is being allocated
  108. /// by the instruction.
  109. ///
  110. unsigned getAlignment() const {
  111. return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
  112. }
  113. void setAlignment(unsigned Align);
  114. /// isStaticAlloca - Return true if this alloca is in the entry block of the
  115. /// function and is a constant size. If so, the code generator will fold it
  116. /// into the prolog/epilog code, so it is basically free.
  117. bool isStaticAlloca() const;
  118. /// \brief Return true if this alloca is used as an inalloca argument to a
  119. /// call. Such allocas are never considered static even if they are in the
  120. /// entry block.
  121. bool isUsedWithInAlloca() const {
  122. return getSubclassDataFromInstruction() & 32;
  123. }
  124. /// \brief Specify whether this alloca is used to represent the arguments to
  125. /// a call.
  126. void setUsedWithInAlloca(bool V) {
  127. setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
  128. (V ? 32 : 0));
  129. }
  130. // Methods for support type inquiry through isa, cast, and dyn_cast:
  131. static inline bool classof(const Instruction *I) {
  132. return (I->getOpcode() == Instruction::Alloca);
  133. }
  134. static inline bool classof(const Value *V) {
  135. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  136. }
  137. private:
  138. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  139. // method so that subclasses cannot accidentally use it.
  140. void setInstructionSubclassData(unsigned short D) {
  141. Instruction::setInstructionSubclassData(D);
  142. }
  143. };
  144. //===----------------------------------------------------------------------===//
  145. // LoadInst Class
  146. //===----------------------------------------------------------------------===//
  147. /// LoadInst - an instruction for reading from memory. This uses the
  148. /// SubclassData field in Value to store whether or not the load is volatile.
  149. ///
  150. class LoadInst : public UnaryInstruction {
  151. void AssertOK();
  152. protected:
  153. // Note: Instruction needs to be a friend here to call cloneImpl.
  154. friend class Instruction;
  155. LoadInst *cloneImpl() const;
  156. public:
  157. LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
  158. LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
  159. LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false,
  160. Instruction *InsertBefore = nullptr);
  161. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
  162. Instruction *InsertBefore = nullptr)
  163. : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
  164. NameStr, isVolatile, InsertBefore) {}
  165. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  166. BasicBlock *InsertAtEnd);
  167. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
  168. Instruction *InsertBefore = nullptr)
  169. : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
  170. NameStr, isVolatile, Align, InsertBefore) {}
  171. LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
  172. unsigned Align, Instruction *InsertBefore = nullptr);
  173. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  174. unsigned Align, BasicBlock *InsertAtEnd);
  175. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
  176. AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
  177. Instruction *InsertBefore = nullptr)
  178. : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
  179. NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
  180. LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
  181. unsigned Align, AtomicOrdering Order,
  182. SynchronizationScope SynchScope = CrossThread,
  183. Instruction *InsertBefore = nullptr);
  184. LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
  185. unsigned Align, AtomicOrdering Order,
  186. SynchronizationScope SynchScope,
  187. BasicBlock *InsertAtEnd);
  188. LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
  189. LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
  190. LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr,
  191. bool isVolatile = false, Instruction *InsertBefore = nullptr);
  192. explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
  193. bool isVolatile = false,
  194. Instruction *InsertBefore = nullptr)
  195. : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
  196. NameStr, isVolatile, InsertBefore) {}
  197. LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
  198. BasicBlock *InsertAtEnd);
  199. /// isVolatile - Return true if this is a load from a volatile memory
  200. /// location.
  201. ///
  202. bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
  203. /// setVolatile - Specify whether this is a volatile load or not.
  204. ///
  205. void setVolatile(bool V) {
  206. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  207. (V ? 1 : 0));
  208. }
  209. /// getAlignment - Return the alignment of the access that is being performed
  210. ///
  211. unsigned getAlignment() const {
  212. return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
  213. }
  214. void setAlignment(unsigned Align);
  215. /// Returns the ordering effect of this fence.
  216. AtomicOrdering getOrdering() const {
  217. return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
  218. }
  219. /// Set the ordering constraint on this load. May not be Release or
  220. /// AcquireRelease.
  221. void setOrdering(AtomicOrdering Ordering) {
  222. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
  223. (Ordering << 7));
  224. }
  225. SynchronizationScope getSynchScope() const {
  226. return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
  227. }
  228. /// Specify whether this load is ordered with respect to all
  229. /// concurrently executing threads, or only with respect to signal handlers
  230. /// executing in the same thread.
  231. void setSynchScope(SynchronizationScope xthread) {
  232. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
  233. (xthread << 6));
  234. }
  235. void setAtomic(AtomicOrdering Ordering,
  236. SynchronizationScope SynchScope = CrossThread) {
  237. setOrdering(Ordering);
  238. setSynchScope(SynchScope);
  239. }
  240. bool isSimple() const { return !isAtomic() && !isVolatile(); }
  241. bool isUnordered() const {
  242. return getOrdering() <= Unordered && !isVolatile();
  243. }
  244. Value *getPointerOperand() { return getOperand(0); }
  245. const Value *getPointerOperand() const { return getOperand(0); }
  246. static unsigned getPointerOperandIndex() { return 0U; }
  247. /// \brief Returns the address space of the pointer operand.
  248. unsigned getPointerAddressSpace() const {
  249. return getPointerOperand()->getType()->getPointerAddressSpace();
  250. }
  251. // Methods for support type inquiry through isa, cast, and dyn_cast:
  252. static inline bool classof(const Instruction *I) {
  253. return I->getOpcode() == Instruction::Load;
  254. }
  255. static inline bool classof(const Value *V) {
  256. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  257. }
  258. private:
  259. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  260. // method so that subclasses cannot accidentally use it.
  261. void setInstructionSubclassData(unsigned short D) {
  262. Instruction::setInstructionSubclassData(D);
  263. }
  264. };
  265. //===----------------------------------------------------------------------===//
  266. // StoreInst Class
  267. //===----------------------------------------------------------------------===//
  268. /// StoreInst - an instruction for storing to memory
  269. ///
  270. class StoreInst : public Instruction {
  271. void *operator new(size_t, unsigned) = delete;
  272. void AssertOK();
  273. protected:
  274. // Note: Instruction needs to be a friend here to call cloneImpl.
  275. friend class Instruction;
  276. StoreInst *cloneImpl() const;
  277. public:
  278. // allocate space for exactly two operands
  279. void *operator new(size_t s) {
  280. return User::operator new(s, 2);
  281. }
  282. StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
  283. StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
  284. StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
  285. Instruction *InsertBefore = nullptr);
  286. StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
  287. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  288. unsigned Align, Instruction *InsertBefore = nullptr);
  289. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  290. unsigned Align, BasicBlock *InsertAtEnd);
  291. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  292. unsigned Align, AtomicOrdering Order,
  293. SynchronizationScope SynchScope = CrossThread,
  294. Instruction *InsertBefore = nullptr);
  295. StoreInst(Value *Val, Value *Ptr, bool isVolatile,
  296. unsigned Align, AtomicOrdering Order,
  297. SynchronizationScope SynchScope,
  298. BasicBlock *InsertAtEnd);
  299. /// isVolatile - Return true if this is a store to a volatile memory
  300. /// location.
  301. ///
  302. bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
  303. /// setVolatile - Specify whether this is a volatile store or not.
  304. ///
  305. void setVolatile(bool V) {
  306. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  307. (V ? 1 : 0));
  308. }
  309. /// Transparently provide more efficient getOperand methods.
  310. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  311. /// getAlignment - Return the alignment of the access that is being performed
  312. ///
  313. unsigned getAlignment() const {
  314. return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
  315. }
  316. void setAlignment(unsigned Align);
  317. /// Returns the ordering effect of this store.
  318. AtomicOrdering getOrdering() const {
  319. return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
  320. }
  321. /// Set the ordering constraint on this store. May not be Acquire or
  322. /// AcquireRelease.
  323. void setOrdering(AtomicOrdering Ordering) {
  324. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
  325. (Ordering << 7));
  326. }
  327. SynchronizationScope getSynchScope() const {
  328. return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
  329. }
  330. /// Specify whether this store instruction is ordered with respect to all
  331. /// concurrently executing threads, or only with respect to signal handlers
  332. /// executing in the same thread.
  333. void setSynchScope(SynchronizationScope xthread) {
  334. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
  335. (xthread << 6));
  336. }
  337. void setAtomic(AtomicOrdering Ordering,
  338. SynchronizationScope SynchScope = CrossThread) {
  339. setOrdering(Ordering);
  340. setSynchScope(SynchScope);
  341. }
  342. bool isSimple() const { return !isAtomic() && !isVolatile(); }
  343. bool isUnordered() const {
  344. return getOrdering() <= Unordered && !isVolatile();
  345. }
  346. Value *getValueOperand() { return getOperand(0); }
  347. const Value *getValueOperand() const { return getOperand(0); }
  348. Value *getPointerOperand() { return getOperand(1); }
  349. const Value *getPointerOperand() const { return getOperand(1); }
  350. static unsigned getPointerOperandIndex() { return 1U; }
  351. /// \brief Returns the address space of the pointer operand.
  352. unsigned getPointerAddressSpace() const {
  353. return getPointerOperand()->getType()->getPointerAddressSpace();
  354. }
  355. // Methods for support type inquiry through isa, cast, and dyn_cast:
  356. static inline bool classof(const Instruction *I) {
  357. return I->getOpcode() == Instruction::Store;
  358. }
  359. static inline bool classof(const Value *V) {
  360. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  361. }
  362. private:
  363. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  364. // method so that subclasses cannot accidentally use it.
  365. void setInstructionSubclassData(unsigned short D) {
  366. Instruction::setInstructionSubclassData(D);
  367. }
  368. };
  369. template <>
  370. struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
  371. };
  372. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
  373. //===----------------------------------------------------------------------===//
  374. // FenceInst Class
  375. //===----------------------------------------------------------------------===//
  376. /// FenceInst - an instruction for ordering other memory operations
  377. ///
  378. class FenceInst : public Instruction {
  379. void *operator new(size_t, unsigned) = delete;
  380. void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
  381. protected:
  382. // Note: Instruction needs to be a friend here to call cloneImpl.
  383. friend class Instruction;
  384. FenceInst *cloneImpl() const;
  385. public:
  386. // allocate space for exactly zero operands
  387. void *operator new(size_t s) {
  388. return User::operator new(s, 0);
  389. }
  390. // Ordering may only be Acquire, Release, AcquireRelease, or
  391. // SequentiallyConsistent.
  392. FenceInst(LLVMContext &C, AtomicOrdering Ordering,
  393. SynchronizationScope SynchScope = CrossThread,
  394. Instruction *InsertBefore = nullptr);
  395. FenceInst(LLVMContext &C, AtomicOrdering Ordering,
  396. SynchronizationScope SynchScope,
  397. BasicBlock *InsertAtEnd);
  398. /// Returns the ordering effect of this fence.
  399. AtomicOrdering getOrdering() const {
  400. return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
  401. }
  402. /// Set the ordering constraint on this fence. May only be Acquire, Release,
  403. /// AcquireRelease, or SequentiallyConsistent.
  404. void setOrdering(AtomicOrdering Ordering) {
  405. setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
  406. (Ordering << 1));
  407. }
  408. SynchronizationScope getSynchScope() const {
  409. return SynchronizationScope(getSubclassDataFromInstruction() & 1);
  410. }
  411. /// Specify whether this fence orders other operations with respect to all
  412. /// concurrently executing threads, or only with respect to signal handlers
  413. /// executing in the same thread.
  414. void setSynchScope(SynchronizationScope xthread) {
  415. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  416. xthread);
  417. }
  418. // Methods for support type inquiry through isa, cast, and dyn_cast:
  419. static inline bool classof(const Instruction *I) {
  420. return I->getOpcode() == Instruction::Fence;
  421. }
  422. static inline bool classof(const Value *V) {
  423. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  424. }
  425. private:
  426. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  427. // method so that subclasses cannot accidentally use it.
  428. void setInstructionSubclassData(unsigned short D) {
  429. Instruction::setInstructionSubclassData(D);
  430. }
  431. };
  432. //===----------------------------------------------------------------------===//
  433. // AtomicCmpXchgInst Class
  434. //===----------------------------------------------------------------------===//
  435. /// AtomicCmpXchgInst - an instruction that atomically checks whether a
  436. /// specified value is in a memory location, and, if it is, stores a new value
  437. /// there. Returns the value that was loaded.
  438. ///
  439. class AtomicCmpXchgInst : public Instruction {
  440. void *operator new(size_t, unsigned) = delete;
  441. void Init(Value *Ptr, Value *Cmp, Value *NewVal,
  442. AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
  443. SynchronizationScope SynchScope);
  444. protected:
  445. // Note: Instruction needs to be a friend here to call cloneImpl.
  446. friend class Instruction;
  447. AtomicCmpXchgInst *cloneImpl() const;
  448. public:
  449. // allocate space for exactly three operands
  450. void *operator new(size_t s) {
  451. return User::operator new(s, 3);
  452. }
  453. AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
  454. AtomicOrdering SuccessOrdering,
  455. AtomicOrdering FailureOrdering,
  456. SynchronizationScope SynchScope,
  457. Instruction *InsertBefore = nullptr);
  458. AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
  459. AtomicOrdering SuccessOrdering,
  460. AtomicOrdering FailureOrdering,
  461. SynchronizationScope SynchScope,
  462. BasicBlock *InsertAtEnd);
  463. /// isVolatile - Return true if this is a cmpxchg from a volatile memory
  464. /// location.
  465. ///
  466. bool isVolatile() const {
  467. return getSubclassDataFromInstruction() & 1;
  468. }
  469. /// setVolatile - Specify whether this is a volatile cmpxchg.
  470. ///
  471. void setVolatile(bool V) {
  472. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  473. (unsigned)V);
  474. }
  475. /// Return true if this cmpxchg may spuriously fail.
  476. bool isWeak() const {
  477. return getSubclassDataFromInstruction() & 0x100;
  478. }
  479. void setWeak(bool IsWeak) {
  480. setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
  481. (IsWeak << 8));
  482. }
  483. /// Transparently provide more efficient getOperand methods.
  484. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  485. /// Set the ordering constraint on this cmpxchg.
  486. void setSuccessOrdering(AtomicOrdering Ordering) {
  487. assert(Ordering != NotAtomic &&
  488. "CmpXchg instructions can only be atomic.");
  489. setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
  490. (Ordering << 2));
  491. }
  492. void setFailureOrdering(AtomicOrdering Ordering) {
  493. assert(Ordering != NotAtomic &&
  494. "CmpXchg instructions can only be atomic.");
  495. setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
  496. (Ordering << 5));
  497. }
  498. /// Specify whether this cmpxchg is atomic and orders other operations with
  499. /// respect to all concurrently executing threads, or only with respect to
  500. /// signal handlers executing in the same thread.
  501. void setSynchScope(SynchronizationScope SynchScope) {
  502. setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
  503. (SynchScope << 1));
  504. }
  505. /// Returns the ordering constraint on this cmpxchg.
  506. AtomicOrdering getSuccessOrdering() const {
  507. return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
  508. }
  509. /// Returns the ordering constraint on this cmpxchg.
  510. AtomicOrdering getFailureOrdering() const {
  511. return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
  512. }
  513. /// Returns whether this cmpxchg is atomic between threads or only within a
  514. /// single thread.
  515. SynchronizationScope getSynchScope() const {
  516. return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
  517. }
  518. Value *getPointerOperand() { return getOperand(0); }
  519. const Value *getPointerOperand() const { return getOperand(0); }
  520. static unsigned getPointerOperandIndex() { return 0U; }
  521. Value *getCompareOperand() { return getOperand(1); }
  522. const Value *getCompareOperand() const { return getOperand(1); }
  523. Value *getNewValOperand() { return getOperand(2); }
  524. const Value *getNewValOperand() const { return getOperand(2); }
  525. /// \brief Returns the address space of the pointer operand.
  526. unsigned getPointerAddressSpace() const {
  527. return getPointerOperand()->getType()->getPointerAddressSpace();
  528. }
  529. /// \brief Returns the strongest permitted ordering on failure, given the
  530. /// desired ordering on success.
  531. ///
  532. /// If the comparison in a cmpxchg operation fails, there is no atomic store
  533. /// so release semantics cannot be provided. So this function drops explicit
  534. /// Release requests from the AtomicOrdering. A SequentiallyConsistent
  535. /// operation would remain SequentiallyConsistent.
  536. static AtomicOrdering
  537. getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
  538. switch (SuccessOrdering) {
  539. default: llvm_unreachable("invalid cmpxchg success ordering");
  540. case Release:
  541. case Monotonic:
  542. return Monotonic;
  543. case AcquireRelease:
  544. case Acquire:
  545. return Acquire;
  546. case SequentiallyConsistent:
  547. return SequentiallyConsistent;
  548. }
  549. }
  550. // Methods for support type inquiry through isa, cast, and dyn_cast:
  551. static inline bool classof(const Instruction *I) {
  552. return I->getOpcode() == Instruction::AtomicCmpXchg;
  553. }
  554. static inline bool classof(const Value *V) {
  555. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  556. }
  557. private:
  558. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  559. // method so that subclasses cannot accidentally use it.
  560. void setInstructionSubclassData(unsigned short D) {
  561. Instruction::setInstructionSubclassData(D);
  562. }
  563. };
  564. template <>
  565. struct OperandTraits<AtomicCmpXchgInst> :
  566. public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
  567. };
  568. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
  569. //===----------------------------------------------------------------------===//
  570. // AtomicRMWInst Class
  571. //===----------------------------------------------------------------------===//
  572. /// AtomicRMWInst - an instruction that atomically reads a memory location,
  573. /// combines it with another value, and then stores the result back. Returns
  574. /// the old value.
  575. ///
  576. class AtomicRMWInst : public Instruction {
  577. void *operator new(size_t, unsigned) = delete;
  578. protected:
  579. // Note: Instruction needs to be a friend here to call cloneImpl.
  580. friend class Instruction;
  581. AtomicRMWInst *cloneImpl() const;
  582. public:
  583. /// This enumeration lists the possible modifications atomicrmw can make. In
  584. /// the descriptions, 'p' is the pointer to the instruction's memory location,
  585. /// 'old' is the initial value of *p, and 'v' is the other value passed to the
  586. /// instruction. These instructions always return 'old'.
  587. enum BinOp {
  588. /// *p = v
  589. Xchg,
  590. /// *p = old + v
  591. Add,
  592. /// *p = old - v
  593. Sub,
  594. /// *p = old & v
  595. And,
  596. /// *p = ~(old & v)
  597. Nand,
  598. /// *p = old | v
  599. Or,
  600. /// *p = old ^ v
  601. Xor,
  602. /// *p = old >signed v ? old : v
  603. Max,
  604. /// *p = old <signed v ? old : v
  605. Min,
  606. /// *p = old >unsigned v ? old : v
  607. UMax,
  608. /// *p = old <unsigned v ? old : v
  609. UMin,
  610. FIRST_BINOP = Xchg,
  611. LAST_BINOP = UMin,
  612. BAD_BINOP
  613. };
  614. // allocate space for exactly two operands
  615. void *operator new(size_t s) {
  616. return User::operator new(s, 2);
  617. }
  618. AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
  619. AtomicOrdering Ordering, SynchronizationScope SynchScope,
  620. Instruction *InsertBefore = nullptr);
  621. AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
  622. AtomicOrdering Ordering, SynchronizationScope SynchScope,
  623. BasicBlock *InsertAtEnd);
  624. BinOp getOperation() const {
  625. return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
  626. }
  627. void setOperation(BinOp Operation) {
  628. unsigned short SubclassData = getSubclassDataFromInstruction();
  629. setInstructionSubclassData((SubclassData & 31) |
  630. (Operation << 5));
  631. }
  632. /// isVolatile - Return true if this is a RMW on a volatile memory location.
  633. ///
  634. bool isVolatile() const {
  635. return getSubclassDataFromInstruction() & 1;
  636. }
  637. /// setVolatile - Specify whether this is a volatile RMW or not.
  638. ///
  639. void setVolatile(bool V) {
  640. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  641. (unsigned)V);
  642. }
  643. /// Transparently provide more efficient getOperand methods.
  644. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  645. /// Set the ordering constraint on this RMW.
  646. void setOrdering(AtomicOrdering Ordering) {
  647. assert(Ordering != NotAtomic &&
  648. "atomicrmw instructions can only be atomic.");
  649. setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
  650. (Ordering << 2));
  651. }
  652. /// Specify whether this RMW orders other operations with respect to all
  653. /// concurrently executing threads, or only with respect to signal handlers
  654. /// executing in the same thread.
  655. void setSynchScope(SynchronizationScope SynchScope) {
  656. setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
  657. (SynchScope << 1));
  658. }
  659. /// Returns the ordering constraint on this RMW.
  660. AtomicOrdering getOrdering() const {
  661. return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
  662. }
  663. /// Returns whether this RMW is atomic between threads or only within a
  664. /// single thread.
  665. SynchronizationScope getSynchScope() const {
  666. return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
  667. }
  668. Value *getPointerOperand() { return getOperand(0); }
  669. const Value *getPointerOperand() const { return getOperand(0); }
  670. static unsigned getPointerOperandIndex() { return 0U; }
  671. Value *getValOperand() { return getOperand(1); }
  672. const Value *getValOperand() const { return getOperand(1); }
  673. /// \brief Returns the address space of the pointer operand.
  674. unsigned getPointerAddressSpace() const {
  675. return getPointerOperand()->getType()->getPointerAddressSpace();
  676. }
  677. // Methods for support type inquiry through isa, cast, and dyn_cast:
  678. static inline bool classof(const Instruction *I) {
  679. return I->getOpcode() == Instruction::AtomicRMW;
  680. }
  681. static inline bool classof(const Value *V) {
  682. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  683. }
  684. private:
  685. void Init(BinOp Operation, Value *Ptr, Value *Val,
  686. AtomicOrdering Ordering, SynchronizationScope SynchScope);
  687. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  688. // method so that subclasses cannot accidentally use it.
  689. void setInstructionSubclassData(unsigned short D) {
  690. Instruction::setInstructionSubclassData(D);
  691. }
  692. };
  693. template <>
  694. struct OperandTraits<AtomicRMWInst>
  695. : public FixedNumOperandTraits<AtomicRMWInst,2> {
  696. };
  697. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
  698. //===----------------------------------------------------------------------===//
  699. // GetElementPtrInst Class
  700. //===----------------------------------------------------------------------===//
  701. // checkGEPType - Simple wrapper function to give a better assertion failure
  702. // message on bad indexes for a gep instruction.
  703. //
  704. inline Type *checkGEPType(Type *Ty) {
  705. assert(Ty && "Invalid GetElementPtrInst indices for type!");
  706. return Ty;
  707. }
  708. /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
  709. /// access elements of arrays and structs
  710. ///
  711. class GetElementPtrInst : public Instruction {
  712. Type *SourceElementType;
  713. Type *ResultElementType;
  714. GetElementPtrInst(const GetElementPtrInst &GEPI);
  715. void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
  716. /// Constructors - Create a getelementptr instruction with a base pointer an
  717. /// list of indices. The first ctor can optionally insert before an existing
  718. /// instruction, the second appends the new instruction to the specified
  719. /// BasicBlock.
  720. inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
  721. ArrayRef<Value *> IdxList, unsigned Values,
  722. const Twine &NameStr, Instruction *InsertBefore);
  723. inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
  724. ArrayRef<Value *> IdxList, unsigned Values,
  725. const Twine &NameStr, BasicBlock *InsertAtEnd);
  726. protected:
  727. // Note: Instruction needs to be a friend here to call cloneImpl.
  728. friend class Instruction;
  729. GetElementPtrInst *cloneImpl() const;
  730. public:
  731. static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
  732. ArrayRef<Value *> IdxList,
  733. const Twine &NameStr = "",
  734. Instruction *InsertBefore = nullptr) {
  735. unsigned Values = 1 + unsigned(IdxList.size());
  736. if (!PointeeType)
  737. PointeeType =
  738. cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
  739. else
  740. assert(
  741. PointeeType ==
  742. cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
  743. return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
  744. NameStr, InsertBefore);
  745. }
  746. static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
  747. ArrayRef<Value *> IdxList,
  748. const Twine &NameStr,
  749. BasicBlock *InsertAtEnd) {
  750. unsigned Values = 1 + unsigned(IdxList.size());
  751. if (!PointeeType)
  752. PointeeType =
  753. cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
  754. else
  755. assert(
  756. PointeeType ==
  757. cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
  758. return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
  759. NameStr, InsertAtEnd);
  760. }
  761. /// Create an "inbounds" getelementptr. See the documentation for the
  762. /// "inbounds" flag in LangRef.html for details.
  763. static GetElementPtrInst *CreateInBounds(Value *Ptr,
  764. ArrayRef<Value *> IdxList,
  765. const Twine &NameStr = "",
  766. Instruction *InsertBefore = nullptr){
  767. return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
  768. }
  769. static GetElementPtrInst *
  770. CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
  771. const Twine &NameStr = "",
  772. Instruction *InsertBefore = nullptr) {
  773. GetElementPtrInst *GEP =
  774. Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
  775. GEP->setIsInBounds(true);
  776. return GEP;
  777. }
  778. static GetElementPtrInst *CreateInBounds(Value *Ptr,
  779. ArrayRef<Value *> IdxList,
  780. const Twine &NameStr,
  781. BasicBlock *InsertAtEnd) {
  782. return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
  783. }
  784. static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
  785. ArrayRef<Value *> IdxList,
  786. const Twine &NameStr,
  787. BasicBlock *InsertAtEnd) {
  788. GetElementPtrInst *GEP =
  789. Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
  790. GEP->setIsInBounds(true);
  791. return GEP;
  792. }
  793. /// Transparently provide more efficient getOperand methods.
  794. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  795. // getType - Overload to return most specific sequential type.
  796. SequentialType *getType() const {
  797. return cast<SequentialType>(Instruction::getType());
  798. }
  799. Type *getSourceElementType() const { return SourceElementType; }
  800. void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
  801. void setResultElementType(Type *Ty) { ResultElementType = Ty; }
  802. Type *getResultElementType() const {
  803. assert(ResultElementType ==
  804. cast<PointerType>(getType()->getScalarType())->getElementType());
  805. return ResultElementType;
  806. }
  807. /// \brief Returns the address space of this instruction's pointer type.
  808. unsigned getAddressSpace() const {
  809. // Note that this is always the same as the pointer operand's address space
  810. // and that is cheaper to compute, so cheat here.
  811. return getPointerAddressSpace();
  812. }
  813. /// getIndexedType - Returns the type of the element that would be loaded with
  814. /// a load instruction with the specified parameters.
  815. ///
  816. /// Null is returned if the indices are invalid for the specified
  817. /// pointer type.
  818. ///
  819. static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
  820. static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
  821. static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
  822. inline op_iterator idx_begin() { return op_begin()+1; }
  823. inline const_op_iterator idx_begin() const { return op_begin()+1; }
  824. inline op_iterator idx_end() { return op_end(); }
  825. inline const_op_iterator idx_end() const { return op_end(); }
  826. Value *getPointerOperand() {
  827. return getOperand(0);
  828. }
  829. const Value *getPointerOperand() const {
  830. return getOperand(0);
  831. }
  832. static unsigned getPointerOperandIndex() {
  833. return 0U; // get index for modifying correct operand.
  834. }
  835. /// getPointerOperandType - Method to return the pointer operand as a
  836. /// PointerType.
  837. Type *getPointerOperandType() const {
  838. return getPointerOperand()->getType();
  839. }
  840. /// \brief Returns the address space of the pointer operand.
  841. unsigned getPointerAddressSpace() const {
  842. return getPointerOperandType()->getPointerAddressSpace();
  843. }
  844. /// GetGEPReturnType - Returns the pointer type returned by the GEP
  845. /// instruction, which may be a vector of pointers.
  846. static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
  847. return getGEPReturnType(
  848. cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
  849. Ptr, IdxList);
  850. }
  851. static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
  852. ArrayRef<Value *> IdxList) {
  853. Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
  854. Ptr->getType()->getPointerAddressSpace());
  855. // Vector GEP
  856. if (Ptr->getType()->isVectorTy()) {
  857. unsigned NumElem = Ptr->getType()->getVectorNumElements();
  858. return VectorType::get(PtrTy, NumElem);
  859. }
  860. for (Value *Index : IdxList)
  861. if (Index->getType()->isVectorTy()) {
  862. unsigned NumElem = Index->getType()->getVectorNumElements();
  863. return VectorType::get(PtrTy, NumElem);
  864. }
  865. // Scalar GEP
  866. return PtrTy;
  867. }
  868. unsigned getNumIndices() const { // Note: always non-negative
  869. return getNumOperands() - 1;
  870. }
  871. bool hasIndices() const {
  872. return getNumOperands() > 1;
  873. }
  874. /// hasAllZeroIndices - Return true if all of the indices of this GEP are
  875. /// zeros. If so, the result pointer and the first operand have the same
  876. /// value, just potentially different types.
  877. bool hasAllZeroIndices() const;
  878. /// hasAllConstantIndices - Return true if all of the indices of this GEP are
  879. /// constant integers. If so, the result pointer and the first operand have
  880. /// a constant offset between them.
  881. bool hasAllConstantIndices() const;
  882. /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
  883. /// See LangRef.html for the meaning of inbounds on a getelementptr.
  884. void setIsInBounds(bool b = true);
  885. /// isInBounds - Determine whether the GEP has the inbounds flag.
  886. bool isInBounds() const;
  887. /// \brief Accumulate the constant address offset of this GEP if possible.
  888. ///
  889. /// This routine accepts an APInt into which it will accumulate the constant
  890. /// offset of this GEP if the GEP is in fact constant. If the GEP is not
  891. /// all-constant, it returns false and the value of the offset APInt is
  892. /// undefined (it is *not* preserved!). The APInt passed into this routine
  893. /// must be at least as wide as the IntPtr type for the address space of
  894. /// the base GEP pointer.
  895. bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
  896. // Methods for support type inquiry through isa, cast, and dyn_cast:
  897. static inline bool classof(const Instruction *I) {
  898. return (I->getOpcode() == Instruction::GetElementPtr);
  899. }
  900. static inline bool classof(const Value *V) {
  901. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  902. }
  903. };
  904. template <>
  905. struct OperandTraits<GetElementPtrInst> :
  906. public VariadicOperandTraits<GetElementPtrInst, 1> {
  907. };
  908. GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
  909. ArrayRef<Value *> IdxList, unsigned Values,
  910. const Twine &NameStr,
  911. Instruction *InsertBefore)
  912. : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
  913. OperandTraits<GetElementPtrInst>::op_end(this) - Values,
  914. Values, InsertBefore),
  915. SourceElementType(PointeeType),
  916. ResultElementType(getIndexedType(PointeeType, IdxList)) {
  917. assert(ResultElementType ==
  918. cast<PointerType>(getType()->getScalarType())->getElementType());
  919. init(Ptr, IdxList, NameStr);
  920. }
  921. GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
  922. ArrayRef<Value *> IdxList, unsigned Values,
  923. const Twine &NameStr,
  924. BasicBlock *InsertAtEnd)
  925. : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
  926. OperandTraits<GetElementPtrInst>::op_end(this) - Values,
  927. Values, InsertAtEnd),
  928. SourceElementType(PointeeType),
  929. ResultElementType(getIndexedType(PointeeType, IdxList)) {
  930. assert(ResultElementType ==
  931. cast<PointerType>(getType()->getScalarType())->getElementType());
  932. init(Ptr, IdxList, NameStr);
  933. }
  934. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
  935. //===----------------------------------------------------------------------===//
  936. // ICmpInst Class
  937. //===----------------------------------------------------------------------===//
  938. /// This instruction compares its operands according to the predicate given
  939. /// to the constructor. It only operates on integers or pointers. The operands
  940. /// must be identical types.
  941. /// \brief Represent an integer comparison operator.
  942. class ICmpInst: public CmpInst {
  943. void AssertOK() {
  944. assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
  945. getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
  946. "Invalid ICmp predicate value");
  947. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  948. "Both operands to ICmp instruction are not of the same type!");
  949. // Check that the operands are the right type
  950. assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
  951. getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
  952. "Invalid operand types for ICmp instruction");
  953. }
  954. protected:
  955. // Note: Instruction needs to be a friend here to call cloneImpl.
  956. friend class Instruction;
  957. /// \brief Clone an identical ICmpInst
  958. ICmpInst *cloneImpl() const;
  959. public:
  960. /// \brief Constructor with insert-before-instruction semantics.
  961. ICmpInst(
  962. Instruction *InsertBefore, ///< Where to insert
  963. Predicate pred, ///< The predicate to use for the comparison
  964. Value *LHS, ///< The left-hand-side of the expression
  965. Value *RHS, ///< The right-hand-side of the expression
  966. const Twine &NameStr = "" ///< Name of the instruction
  967. ) : CmpInst(makeCmpResultType(LHS->getType()),
  968. Instruction::ICmp, pred, LHS, RHS, NameStr,
  969. InsertBefore) {
  970. #ifndef NDEBUG
  971. AssertOK();
  972. #endif
  973. }
  974. /// \brief Constructor with insert-at-end semantics.
  975. ICmpInst(
  976. BasicBlock &InsertAtEnd, ///< Block to insert into.
  977. Predicate pred, ///< The predicate to use for the comparison
  978. Value *LHS, ///< The left-hand-side of the expression
  979. Value *RHS, ///< The right-hand-side of the expression
  980. const Twine &NameStr = "" ///< Name of the instruction
  981. ) : CmpInst(makeCmpResultType(LHS->getType()),
  982. Instruction::ICmp, pred, LHS, RHS, NameStr,
  983. &InsertAtEnd) {
  984. #ifndef NDEBUG
  985. AssertOK();
  986. #endif
  987. }
  988. /// \brief Constructor with no-insertion semantics
  989. ICmpInst(
  990. Predicate pred, ///< The predicate to use for the comparison
  991. Value *LHS, ///< The left-hand-side of the expression
  992. Value *RHS, ///< The right-hand-side of the expression
  993. const Twine &NameStr = "" ///< Name of the instruction
  994. ) : CmpInst(makeCmpResultType(LHS->getType()),
  995. Instruction::ICmp, pred, LHS, RHS, NameStr) {
  996. #ifndef NDEBUG
  997. AssertOK();
  998. #endif
  999. }
  1000. /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
  1001. /// @returns the predicate that would be the result if the operand were
  1002. /// regarded as signed.
  1003. /// \brief Return the signed version of the predicate
  1004. Predicate getSignedPredicate() const {
  1005. return getSignedPredicate(getPredicate());
  1006. }
  1007. /// This is a static version that you can use without an instruction.
  1008. /// \brief Return the signed version of the predicate.
  1009. static Predicate getSignedPredicate(Predicate pred);
  1010. /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
  1011. /// @returns the predicate that would be the result if the operand were
  1012. /// regarded as unsigned.
  1013. /// \brief Return the unsigned version of the predicate
  1014. Predicate getUnsignedPredicate() const {
  1015. return getUnsignedPredicate(getPredicate());
  1016. }
  1017. /// This is a static version that you can use without an instruction.
  1018. /// \brief Return the unsigned version of the predicate.
  1019. static Predicate getUnsignedPredicate(Predicate pred);
  1020. /// isEquality - Return true if this predicate is either EQ or NE. This also
  1021. /// tests for commutativity.
  1022. static bool isEquality(Predicate P) {
  1023. return P == ICMP_EQ || P == ICMP_NE;
  1024. }
  1025. /// isEquality - Return true if this predicate is either EQ or NE. This also
  1026. /// tests for commutativity.
  1027. bool isEquality() const {
  1028. return isEquality(getPredicate());
  1029. }
  1030. /// @returns true if the predicate of this ICmpInst is commutative
  1031. /// \brief Determine if this relation is commutative.
  1032. bool isCommutative() const { return isEquality(); }
  1033. /// isRelational - Return true if the predicate is relational (not EQ or NE).
  1034. ///
  1035. bool isRelational() const {
  1036. return !isEquality();
  1037. }
  1038. /// isRelational - Return true if the predicate is relational (not EQ or NE).
  1039. ///
  1040. static bool isRelational(Predicate P) {
  1041. return !isEquality(P);
  1042. }
  1043. /// Initialize a set of values that all satisfy the predicate with C.
  1044. /// \brief Make a ConstantRange for a relation with a constant value.
  1045. static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
  1046. /// Exchange the two operands to this instruction in such a way that it does
  1047. /// not modify the semantics of the instruction. The predicate value may be
  1048. /// changed to retain the same result if the predicate is order dependent
  1049. /// (e.g. ult).
  1050. /// \brief Swap operands and adjust predicate.
  1051. void swapOperands() {
  1052. setPredicate(getSwappedPredicate());
  1053. Op<0>().swap(Op<1>());
  1054. }
  1055. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1056. static inline bool classof(const Instruction *I) {
  1057. return I->getOpcode() == Instruction::ICmp;
  1058. }
  1059. static inline bool classof(const Value *V) {
  1060. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1061. }
  1062. };
  1063. //===----------------------------------------------------------------------===//
  1064. // FCmpInst Class
  1065. //===----------------------------------------------------------------------===//
  1066. /// This instruction compares its operands according to the predicate given
  1067. /// to the constructor. It only operates on floating point values or packed
  1068. /// vectors of floating point values. The operands must be identical types.
  1069. /// \brief Represents a floating point comparison operator.
  1070. class FCmpInst: public CmpInst {
  1071. protected:
  1072. // Note: Instruction needs to be a friend here to call cloneImpl.
  1073. friend class Instruction;
  1074. /// \brief Clone an identical FCmpInst
  1075. FCmpInst *cloneImpl() const;
  1076. public:
  1077. /// \brief Constructor with insert-before-instruction semantics.
  1078. FCmpInst(
  1079. Instruction *InsertBefore, ///< Where to insert
  1080. Predicate pred, ///< The predicate to use for the comparison
  1081. Value *LHS, ///< The left-hand-side of the expression
  1082. Value *RHS, ///< The right-hand-side of the expression
  1083. const Twine &NameStr = "" ///< Name of the instruction
  1084. ) : CmpInst(makeCmpResultType(LHS->getType()),
  1085. Instruction::FCmp, pred, LHS, RHS, NameStr,
  1086. InsertBefore) {
  1087. assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
  1088. "Invalid FCmp predicate value");
  1089. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  1090. "Both operands to FCmp instruction are not of the same type!");
  1091. // Check that the operands are the right type
  1092. assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
  1093. "Invalid operand types for FCmp instruction");
  1094. }
  1095. /// \brief Constructor with insert-at-end semantics.
  1096. FCmpInst(
  1097. BasicBlock &InsertAtEnd, ///< Block to insert into.
  1098. Predicate pred, ///< The predicate to use for the comparison
  1099. Value *LHS, ///< The left-hand-side of the expression
  1100. Value *RHS, ///< The right-hand-side of the expression
  1101. const Twine &NameStr = "" ///< Name of the instruction
  1102. ) : CmpInst(makeCmpResultType(LHS->getType()),
  1103. Instruction::FCmp, pred, LHS, RHS, NameStr,
  1104. &InsertAtEnd) {
  1105. assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
  1106. "Invalid FCmp predicate value");
  1107. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  1108. "Both operands to FCmp instruction are not of the same type!");
  1109. // Check that the operands are the right type
  1110. assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
  1111. "Invalid operand types for FCmp instruction");
  1112. }
  1113. /// \brief Constructor with no-insertion semantics
  1114. FCmpInst(
  1115. Predicate pred, ///< The predicate to use for the comparison
  1116. Value *LHS, ///< The left-hand-side of the expression
  1117. Value *RHS, ///< The right-hand-side of the expression
  1118. const Twine &NameStr = "" ///< Name of the instruction
  1119. ) : CmpInst(makeCmpResultType(LHS->getType()),
  1120. Instruction::FCmp, pred, LHS, RHS, NameStr) {
  1121. assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
  1122. "Invalid FCmp predicate value");
  1123. assert(getOperand(0)->getType() == getOperand(1)->getType() &&
  1124. "Both operands to FCmp instruction are not of the same type!");
  1125. // Check that the operands are the right type
  1126. assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
  1127. "Invalid operand types for FCmp instruction");
  1128. }
  1129. /// @returns true if the predicate of this instruction is EQ or NE.
  1130. /// \brief Determine if this is an equality predicate.
  1131. static bool isEquality(Predicate Pred) {
  1132. return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
  1133. Pred == FCMP_UNE;
  1134. }
  1135. /// @returns true if the predicate of this instruction is EQ or NE.
  1136. /// \brief Determine if this is an equality predicate.
  1137. bool isEquality() const { return isEquality(getPredicate()); }
  1138. /// @returns true if the predicate of this instruction is commutative.
  1139. /// \brief Determine if this is a commutative predicate.
  1140. bool isCommutative() const {
  1141. return isEquality() ||
  1142. getPredicate() == FCMP_FALSE ||
  1143. getPredicate() == FCMP_TRUE ||
  1144. getPredicate() == FCMP_ORD ||
  1145. getPredicate() == FCMP_UNO;
  1146. }
  1147. /// @returns true if the predicate is relational (not EQ or NE).
  1148. /// \brief Determine if this a relational predicate.
  1149. bool isRelational() const { return !isEquality(); }
  1150. /// Exchange the two operands to this instruction in such a way that it does
  1151. /// not modify the semantics of the instruction. The predicate value may be
  1152. /// changed to retain the same result if the predicate is order dependent
  1153. /// (e.g. ult).
  1154. /// \brief Swap operands and adjust predicate.
  1155. void swapOperands() {
  1156. setPredicate(getSwappedPredicate());
  1157. Op<0>().swap(Op<1>());
  1158. }
  1159. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  1160. static inline bool classof(const Instruction *I) {
  1161. return I->getOpcode() == Instruction::FCmp;
  1162. }
  1163. static inline bool classof(const Value *V) {
  1164. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1165. }
  1166. };
  1167. //===----------------------------------------------------------------------===//
  1168. /// CallInst - This class represents a function call, abstracting a target
  1169. /// machine's calling convention. This class uses low bit of the SubClassData
  1170. /// field to indicate whether or not this is a tail call. The rest of the bits
  1171. /// hold the calling convention of the call.
  1172. ///
  1173. class CallInst : public Instruction {
  1174. AttributeSet AttributeList; ///< parameter attributes for call
  1175. FunctionType *FTy;
  1176. CallInst(const CallInst &CI);
  1177. void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
  1178. init(cast<FunctionType>(
  1179. cast<PointerType>(Func->getType())->getElementType()),
  1180. Func, Args, NameStr);
  1181. }
  1182. void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
  1183. const Twine &NameStr);
  1184. void init(Value *Func, const Twine &NameStr);
  1185. /// Construct a CallInst given a range of arguments.
  1186. /// \brief Construct a CallInst from a range of arguments
  1187. inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
  1188. const Twine &NameStr, Instruction *InsertBefore);
  1189. inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
  1190. Instruction *InsertBefore)
  1191. : CallInst(cast<FunctionType>(
  1192. cast<PointerType>(Func->getType())->getElementType()),
  1193. Func, Args, NameStr, InsertBefore) {}
  1194. /// Construct a CallInst given a range of arguments.
  1195. /// \brief Construct a CallInst from a range of arguments
  1196. inline CallInst(Value *Func, ArrayRef<Value *> Args,
  1197. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1198. explicit CallInst(Value *F, const Twine &NameStr,
  1199. Instruction *InsertBefore);
  1200. CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
  1201. protected:
  1202. // Note: Instruction needs to be a friend here to call cloneImpl.
  1203. friend class Instruction;
  1204. CallInst *cloneImpl() const;
  1205. public:
  1206. static CallInst *Create(Value *Func,
  1207. ArrayRef<Value *> Args,
  1208. const Twine &NameStr = "",
  1209. Instruction *InsertBefore = nullptr) {
  1210. return Create(cast<FunctionType>(
  1211. cast<PointerType>(Func->getType())->getElementType()),
  1212. Func, Args, NameStr, InsertBefore);
  1213. }
  1214. static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
  1215. const Twine &NameStr = "",
  1216. Instruction *InsertBefore = nullptr) {
  1217. return new (unsigned(Args.size() + 1))
  1218. CallInst(Ty, Func, Args, NameStr, InsertBefore);
  1219. }
  1220. static CallInst *Create(Value *Func,
  1221. ArrayRef<Value *> Args,
  1222. const Twine &NameStr, BasicBlock *InsertAtEnd) {
  1223. return new(unsigned(Args.size() + 1))
  1224. CallInst(Func, Args, NameStr, InsertAtEnd);
  1225. }
  1226. static CallInst *Create(Value *F, const Twine &NameStr = "",
  1227. Instruction *InsertBefore = nullptr) {
  1228. return new(1) CallInst(F, NameStr, InsertBefore);
  1229. }
  1230. static CallInst *Create(Value *F, const Twine &NameStr,
  1231. BasicBlock *InsertAtEnd) {
  1232. return new(1) CallInst(F, NameStr, InsertAtEnd);
  1233. }
  1234. /// CreateMalloc - Generate the IR for a call to malloc:
  1235. /// 1. Compute the malloc call's argument as the specified type's size,
  1236. /// possibly multiplied by the array size if the array size is not
  1237. /// constant 1.
  1238. /// 2. Call malloc with that argument.
  1239. /// 3. Bitcast the result of the malloc call to the specified type.
  1240. static Instruction *CreateMalloc(Instruction *InsertBefore,
  1241. Type *IntPtrTy, Type *AllocTy,
  1242. Value *AllocSize, Value *ArraySize = nullptr,
  1243. Function* MallocF = nullptr,
  1244. const Twine &Name = "");
  1245. static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
  1246. Type *IntPtrTy, Type *AllocTy,
  1247. Value *AllocSize, Value *ArraySize = nullptr,
  1248. Function* MallocF = nullptr,
  1249. const Twine &Name = "");
  1250. /// CreateFree - Generate the IR for a call to the builtin free function.
  1251. static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
  1252. static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
  1253. ~CallInst() override;
  1254. FunctionType *getFunctionType() const { return FTy; }
  1255. void mutateFunctionType(FunctionType *FTy) {
  1256. mutateType(FTy->getReturnType());
  1257. this->FTy = FTy;
  1258. }
  1259. // Note that 'musttail' implies 'tail'.
  1260. enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
  1261. TailCallKind getTailCallKind() const {
  1262. return TailCallKind(getSubclassDataFromInstruction() & 3);
  1263. }
  1264. bool isTailCall() const {
  1265. return (getSubclassDataFromInstruction() & 3) != TCK_None;
  1266. }
  1267. bool isMustTailCall() const {
  1268. return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
  1269. }
  1270. void setTailCall(bool isTC = true) {
  1271. setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
  1272. unsigned(isTC ? TCK_Tail : TCK_None));
  1273. }
  1274. void setTailCallKind(TailCallKind TCK) {
  1275. setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
  1276. unsigned(TCK));
  1277. }
  1278. /// Provide fast operand accessors
  1279. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1280. /// getNumArgOperands - Return the number of call arguments.
  1281. ///
  1282. unsigned getNumArgOperands() const { return getNumOperands() - 1; }
  1283. /// getArgOperand/setArgOperand - Return/set the i-th call argument.
  1284. ///
  1285. Value *getArgOperand(unsigned i) const { return getOperand(i); }
  1286. void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
  1287. /// arg_operands - iteration adapter for range-for loops.
  1288. iterator_range<op_iterator> arg_operands() {
  1289. // The last operand in the op list is the callee - it's not one of the args
  1290. // so we don't want to iterate over it.
  1291. return iterator_range<op_iterator>(op_begin(), op_end() - 1);
  1292. }
  1293. /// arg_operands - iteration adapter for range-for loops.
  1294. iterator_range<const_op_iterator> arg_operands() const {
  1295. return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
  1296. }
  1297. /// \brief Wrappers for getting the \c Use of a call argument.
  1298. const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
  1299. Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
  1300. /// getCallingConv/setCallingConv - Get or set the calling convention of this
  1301. /// function call.
  1302. CallingConv::ID getCallingConv() const {
  1303. return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
  1304. }
  1305. void setCallingConv(CallingConv::ID CC) {
  1306. setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
  1307. (static_cast<unsigned>(CC) << 2));
  1308. }
  1309. /// getAttributes - Return the parameter attributes for this call.
  1310. ///
  1311. const AttributeSet &getAttributes() const { return AttributeList; }
  1312. /// setAttributes - Set the parameter attributes for this call.
  1313. ///
  1314. void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
  1315. /// addAttribute - adds the attribute to the list of attributes.
  1316. void addAttribute(unsigned i, Attribute::AttrKind attr);
  1317. /// addAttribute - adds the attribute to the list of attributes.
  1318. void addAttribute(unsigned i, StringRef Kind, StringRef Value);
  1319. /// removeAttribute - removes the attribute from the list of attributes.
  1320. void removeAttribute(unsigned i, Attribute attr);
  1321. /// \brief adds the dereferenceable attribute to the list of attributes.
  1322. void addDereferenceableAttr(unsigned i, uint64_t Bytes);
  1323. /// \brief adds the dereferenceable_or_null attribute to the list of
  1324. /// attributes.
  1325. void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
  1326. /// \brief Determine whether this call has the given attribute.
  1327. bool hasFnAttr(Attribute::AttrKind A) const {
  1328. assert(A != Attribute::NoBuiltin &&
  1329. "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
  1330. return hasFnAttrImpl(A);
  1331. }
  1332. /// \brief Determine whether this call has the given attribute.
  1333. bool hasFnAttr(StringRef A) const {
  1334. return hasFnAttrImpl(A);
  1335. }
  1336. /// \brief Determine whether the call or the callee has the given attributes.
  1337. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
  1338. /// \brief Extract the alignment for a call or parameter (0=unknown).
  1339. unsigned getParamAlignment(unsigned i) const {
  1340. return AttributeList.getParamAlignment(i);
  1341. }
  1342. /// \brief Extract the number of dereferenceable bytes for a call or
  1343. /// parameter (0=unknown).
  1344. uint64_t getDereferenceableBytes(unsigned i) const {
  1345. return AttributeList.getDereferenceableBytes(i);
  1346. }
  1347. /// \brief Extract the number of dereferenceable_or_null bytes for a call or
  1348. /// parameter (0=unknown).
  1349. uint64_t getDereferenceableOrNullBytes(unsigned i) const {
  1350. return AttributeList.getDereferenceableOrNullBytes(i);
  1351. }
  1352. /// \brief Return true if the call should not be treated as a call to a
  1353. /// builtin.
  1354. bool isNoBuiltin() const {
  1355. return hasFnAttrImpl(Attribute::NoBuiltin) &&
  1356. !hasFnAttrImpl(Attribute::Builtin);
  1357. }
  1358. /// \brief Return true if the call should not be inlined.
  1359. bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
  1360. void setIsNoInline() {
  1361. addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
  1362. }
  1363. /// \brief Return true if the call can return twice
  1364. bool canReturnTwice() const {
  1365. return hasFnAttr(Attribute::ReturnsTwice);
  1366. }
  1367. void setCanReturnTwice() {
  1368. addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
  1369. }
  1370. /// \brief Determine if the call does not access memory.
  1371. bool doesNotAccessMemory() const {
  1372. return hasFnAttr(Attribute::ReadNone);
  1373. }
  1374. void setDoesNotAccessMemory() {
  1375. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
  1376. }
  1377. /// \brief Determine if the call does not access or only reads memory.
  1378. bool onlyReadsMemory() const {
  1379. return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
  1380. }
  1381. void setOnlyReadsMemory() {
  1382. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
  1383. }
  1384. /// @brief Determine if the call can access memmory only using pointers based
  1385. /// on its arguments.
  1386. bool onlyAccessesArgMemory() const {
  1387. return hasFnAttr(Attribute::ArgMemOnly);
  1388. }
  1389. void setOnlyAccessesArgMemory() {
  1390. addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
  1391. }
  1392. /// \brief Determine if the call cannot return.
  1393. bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
  1394. void setDoesNotReturn() {
  1395. addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
  1396. }
  1397. /// \brief Determine if the call cannot unwind.
  1398. bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
  1399. void setDoesNotThrow() {
  1400. addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
  1401. }
  1402. /// \brief Determine if the call cannot be duplicated.
  1403. bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
  1404. void setCannotDuplicate() {
  1405. addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
  1406. }
  1407. /// \brief Determine if the call returns a structure through first
  1408. /// pointer argument.
  1409. bool hasStructRetAttr() const {
  1410. // Be friendly and also check the callee.
  1411. return paramHasAttr(1, Attribute::StructRet);
  1412. }
  1413. /// \brief Determine if any call argument is an aggregate passed by value.
  1414. bool hasByValArgument() const {
  1415. return AttributeList.hasAttrSomewhere(Attribute::ByVal);
  1416. }
  1417. /// getCalledFunction - Return the function called, or null if this is an
  1418. /// indirect function invocation.
  1419. ///
  1420. Function *getCalledFunction() const {
  1421. return dyn_cast<Function>(Op<-1>());
  1422. }
  1423. /// getCalledValue - Get a pointer to the function that is invoked by this
  1424. /// instruction.
  1425. const Value *getCalledValue() const { return Op<-1>(); }
  1426. Value *getCalledValue() { return Op<-1>(); }
  1427. /// setCalledFunction - Set the function called.
  1428. void setCalledFunction(Value* Fn) {
  1429. setCalledFunction(
  1430. cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
  1431. Fn);
  1432. }
  1433. void setCalledFunction(FunctionType *FTy, Value *Fn) {
  1434. this->FTy = FTy;
  1435. assert(FTy == cast<FunctionType>(
  1436. cast<PointerType>(Fn->getType())->getElementType()));
  1437. Op<-1>() = Fn;
  1438. }
  1439. /// isInlineAsm - Check if this call is an inline asm statement.
  1440. bool isInlineAsm() const {
  1441. return isa<InlineAsm>(Op<-1>());
  1442. }
  1443. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1444. static inline bool classof(const Instruction *I) {
  1445. return I->getOpcode() == Instruction::Call;
  1446. }
  1447. static inline bool classof(const Value *V) {
  1448. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1449. }
  1450. private:
  1451. template<typename AttrKind>
  1452. bool hasFnAttrImpl(AttrKind A) const {
  1453. if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
  1454. return true;
  1455. if (const Function *F = getCalledFunction())
  1456. return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
  1457. return false;
  1458. }
  1459. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  1460. // method so that subclasses cannot accidentally use it.
  1461. void setInstructionSubclassData(unsigned short D) {
  1462. Instruction::setInstructionSubclassData(D);
  1463. }
  1464. };
  1465. template <>
  1466. struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
  1467. };
  1468. CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
  1469. const Twine &NameStr, BasicBlock *InsertAtEnd)
  1470. : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
  1471. ->getElementType())->getReturnType(),
  1472. Instruction::Call,
  1473. OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
  1474. unsigned(Args.size() + 1), InsertAtEnd) {
  1475. init(Func, Args, NameStr);
  1476. }
  1477. CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
  1478. const Twine &NameStr, Instruction *InsertBefore)
  1479. : Instruction(Ty->getReturnType(), Instruction::Call,
  1480. OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
  1481. unsigned(Args.size() + 1), InsertBefore) {
  1482. init(Ty, Func, Args, NameStr);
  1483. }
  1484. // Note: if you get compile errors about private methods then
  1485. // please update your code to use the high-level operand
  1486. // interfaces. See line 943 above.
  1487. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
  1488. //===----------------------------------------------------------------------===//
  1489. // SelectInst Class
  1490. //===----------------------------------------------------------------------===//
  1491. /// SelectInst - This class represents the LLVM 'select' instruction.
  1492. ///
  1493. class SelectInst : public Instruction {
  1494. void init(Value *C, Value *S1, Value *S2) {
  1495. assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
  1496. Op<0>() = C;
  1497. Op<1>() = S1;
  1498. Op<2>() = S2;
  1499. }
  1500. SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
  1501. Instruction *InsertBefore)
  1502. : Instruction(S1->getType(), Instruction::Select,
  1503. &Op<0>(), 3, InsertBefore) {
  1504. init(C, S1, S2);
  1505. setName(NameStr);
  1506. }
  1507. SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
  1508. BasicBlock *InsertAtEnd)
  1509. : Instruction(S1->getType(), Instruction::Select,
  1510. &Op<0>(), 3, InsertAtEnd) {
  1511. init(C, S1, S2);
  1512. setName(NameStr);
  1513. }
  1514. protected:
  1515. // Note: Instruction needs to be a friend here to call cloneImpl.
  1516. friend class Instruction;
  1517. SelectInst *cloneImpl() const;
  1518. public:
  1519. static SelectInst *Create(Value *C, Value *S1, Value *S2,
  1520. const Twine &NameStr = "",
  1521. Instruction *InsertBefore = nullptr) {
  1522. return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
  1523. }
  1524. static SelectInst *Create(Value *C, Value *S1, Value *S2,
  1525. const Twine &NameStr,
  1526. BasicBlock *InsertAtEnd) {
  1527. return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
  1528. }
  1529. const Value *getCondition() const { return Op<0>(); }
  1530. const Value *getTrueValue() const { return Op<1>(); }
  1531. const Value *getFalseValue() const { return Op<2>(); }
  1532. Value *getCondition() { return Op<0>(); }
  1533. Value *getTrueValue() { return Op<1>(); }
  1534. Value *getFalseValue() { return Op<2>(); }
  1535. /// areInvalidOperands - Return a string if the specified operands are invalid
  1536. /// for a select operation, otherwise return null.
  1537. static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
  1538. /// Transparently provide more efficient getOperand methods.
  1539. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1540. OtherOps getOpcode() const {
  1541. return static_cast<OtherOps>(Instruction::getOpcode());
  1542. }
  1543. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1544. static inline bool classof(const Instruction *I) {
  1545. return I->getOpcode() == Instruction::Select;
  1546. }
  1547. static inline bool classof(const Value *V) {
  1548. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1549. }
  1550. };
  1551. template <>
  1552. struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
  1553. };
  1554. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
  1555. //===----------------------------------------------------------------------===//
  1556. // VAArgInst Class
  1557. //===----------------------------------------------------------------------===//
  1558. /// VAArgInst - This class represents the va_arg llvm instruction, which returns
  1559. /// an argument of the specified type given a va_list and increments that list
  1560. ///
  1561. class VAArgInst : public UnaryInstruction {
  1562. protected:
  1563. // Note: Instruction needs to be a friend here to call cloneImpl.
  1564. friend class Instruction;
  1565. VAArgInst *cloneImpl() const;
  1566. public:
  1567. VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
  1568. Instruction *InsertBefore = nullptr)
  1569. : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
  1570. setName(NameStr);
  1571. }
  1572. VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
  1573. BasicBlock *InsertAtEnd)
  1574. : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
  1575. setName(NameStr);
  1576. }
  1577. Value *getPointerOperand() { return getOperand(0); }
  1578. const Value *getPointerOperand() const { return getOperand(0); }
  1579. static unsigned getPointerOperandIndex() { return 0U; }
  1580. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1581. static inline bool classof(const Instruction *I) {
  1582. return I->getOpcode() == VAArg;
  1583. }
  1584. static inline bool classof(const Value *V) {
  1585. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1586. }
  1587. };
  1588. //===----------------------------------------------------------------------===//
  1589. // ExtractElementInst Class
  1590. //===----------------------------------------------------------------------===//
  1591. /// ExtractElementInst - This instruction extracts a single (scalar)
  1592. /// element from a VectorType value
  1593. ///
  1594. class ExtractElementInst : public Instruction {
  1595. ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
  1596. Instruction *InsertBefore = nullptr);
  1597. ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
  1598. BasicBlock *InsertAtEnd);
  1599. protected:
  1600. // Note: Instruction needs to be a friend here to call cloneImpl.
  1601. friend class Instruction;
  1602. ExtractElementInst *cloneImpl() const;
  1603. public:
  1604. static ExtractElementInst *Create(Value *Vec, Value *Idx,
  1605. const Twine &NameStr = "",
  1606. Instruction *InsertBefore = nullptr) {
  1607. return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
  1608. }
  1609. static ExtractElementInst *Create(Value *Vec, Value *Idx,
  1610. const Twine &NameStr,
  1611. BasicBlock *InsertAtEnd) {
  1612. return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
  1613. }
  1614. /// isValidOperands - Return true if an extractelement instruction can be
  1615. /// formed with the specified operands.
  1616. static bool isValidOperands(const Value *Vec, const Value *Idx);
  1617. Value *getVectorOperand() { return Op<0>(); }
  1618. Value *getIndexOperand() { return Op<1>(); }
  1619. const Value *getVectorOperand() const { return Op<0>(); }
  1620. const Value *getIndexOperand() const { return Op<1>(); }
  1621. VectorType *getVectorOperandType() const {
  1622. return cast<VectorType>(getVectorOperand()->getType());
  1623. }
  1624. /// Transparently provide more efficient getOperand methods.
  1625. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1626. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1627. static inline bool classof(const Instruction *I) {
  1628. return I->getOpcode() == Instruction::ExtractElement;
  1629. }
  1630. static inline bool classof(const Value *V) {
  1631. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1632. }
  1633. };
  1634. template <>
  1635. struct OperandTraits<ExtractElementInst> :
  1636. public FixedNumOperandTraits<ExtractElementInst, 2> {
  1637. };
  1638. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
  1639. //===----------------------------------------------------------------------===//
  1640. // InsertElementInst Class
  1641. //===----------------------------------------------------------------------===//
  1642. /// InsertElementInst - This instruction inserts a single (scalar)
  1643. /// element into a VectorType value
  1644. ///
  1645. class InsertElementInst : public Instruction {
  1646. InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
  1647. const Twine &NameStr = "",
  1648. Instruction *InsertBefore = nullptr);
  1649. InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
  1650. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1651. protected:
  1652. // Note: Instruction needs to be a friend here to call cloneImpl.
  1653. friend class Instruction;
  1654. InsertElementInst *cloneImpl() const;
  1655. public:
  1656. static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
  1657. const Twine &NameStr = "",
  1658. Instruction *InsertBefore = nullptr) {
  1659. return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
  1660. }
  1661. static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
  1662. const Twine &NameStr,
  1663. BasicBlock *InsertAtEnd) {
  1664. return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
  1665. }
  1666. /// isValidOperands - Return true if an insertelement instruction can be
  1667. /// formed with the specified operands.
  1668. static bool isValidOperands(const Value *Vec, const Value *NewElt,
  1669. const Value *Idx);
  1670. /// getType - Overload to return most specific vector type.
  1671. ///
  1672. VectorType *getType() const {
  1673. return cast<VectorType>(Instruction::getType());
  1674. }
  1675. /// Transparently provide more efficient getOperand methods.
  1676. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1677. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1678. static inline bool classof(const Instruction *I) {
  1679. return I->getOpcode() == Instruction::InsertElement;
  1680. }
  1681. static inline bool classof(const Value *V) {
  1682. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1683. }
  1684. };
  1685. template <>
  1686. struct OperandTraits<InsertElementInst> :
  1687. public FixedNumOperandTraits<InsertElementInst, 3> {
  1688. };
  1689. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
  1690. //===----------------------------------------------------------------------===//
  1691. // ShuffleVectorInst Class
  1692. //===----------------------------------------------------------------------===//
  1693. /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
  1694. /// input vectors.
  1695. ///
  1696. class ShuffleVectorInst : public Instruction {
  1697. protected:
  1698. // Note: Instruction needs to be a friend here to call cloneImpl.
  1699. friend class Instruction;
  1700. ShuffleVectorInst *cloneImpl() const;
  1701. public:
  1702. // allocate space for exactly three operands
  1703. void *operator new(size_t s) {
  1704. return User::operator new(s, 3);
  1705. }
  1706. ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
  1707. const Twine &NameStr = "",
  1708. Instruction *InsertBefor = nullptr);
  1709. ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
  1710. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1711. /// isValidOperands - Return true if a shufflevector instruction can be
  1712. /// formed with the specified operands.
  1713. static bool isValidOperands(const Value *V1, const Value *V2,
  1714. const Value *Mask);
  1715. /// getType - Overload to return most specific vector type.
  1716. ///
  1717. VectorType *getType() const {
  1718. return cast<VectorType>(Instruction::getType());
  1719. }
  1720. /// Transparently provide more efficient getOperand methods.
  1721. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1722. Constant *getMask() const {
  1723. return cast<Constant>(getOperand(2));
  1724. }
  1725. /// getMaskValue - Return the index from the shuffle mask for the specified
  1726. /// output result. This is either -1 if the element is undef or a number less
  1727. /// than 2*numelements.
  1728. static int getMaskValue(Constant *Mask, unsigned i);
  1729. int getMaskValue(unsigned i) const {
  1730. return getMaskValue(getMask(), i);
  1731. }
  1732. /// getShuffleMask - Return the full mask for this instruction, where each
  1733. /// element is the element number and undef's are returned as -1.
  1734. static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
  1735. void getShuffleMask(SmallVectorImpl<int> &Result) const {
  1736. return getShuffleMask(getMask(), Result);
  1737. }
  1738. SmallVector<int, 16> getShuffleMask() const {
  1739. SmallVector<int, 16> Mask;
  1740. getShuffleMask(Mask);
  1741. return Mask;
  1742. }
  1743. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1744. static inline bool classof(const Instruction *I) {
  1745. return I->getOpcode() == Instruction::ShuffleVector;
  1746. }
  1747. static inline bool classof(const Value *V) {
  1748. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1749. }
  1750. };
  1751. template <>
  1752. struct OperandTraits<ShuffleVectorInst> :
  1753. public FixedNumOperandTraits<ShuffleVectorInst, 3> {
  1754. };
  1755. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
  1756. //===----------------------------------------------------------------------===//
  1757. // ExtractValueInst Class
  1758. //===----------------------------------------------------------------------===//
  1759. /// ExtractValueInst - This instruction extracts a struct member or array
  1760. /// element value from an aggregate value.
  1761. ///
  1762. class ExtractValueInst : public UnaryInstruction {
  1763. SmallVector<unsigned, 4> Indices;
  1764. ExtractValueInst(const ExtractValueInst &EVI);
  1765. void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
  1766. /// Constructors - Create a extractvalue instruction with a base aggregate
  1767. /// value and a list of indices. The first ctor can optionally insert before
  1768. /// an existing instruction, the second appends the new instruction to the
  1769. /// specified BasicBlock.
  1770. inline ExtractValueInst(Value *Agg,
  1771. ArrayRef<unsigned> Idxs,
  1772. const Twine &NameStr,
  1773. Instruction *InsertBefore);
  1774. inline ExtractValueInst(Value *Agg,
  1775. ArrayRef<unsigned> Idxs,
  1776. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1777. // allocate space for exactly one operand
  1778. void *operator new(size_t s) {
  1779. return User::operator new(s, 1);
  1780. }
  1781. protected:
  1782. // Note: Instruction needs to be a friend here to call cloneImpl.
  1783. friend class Instruction;
  1784. ExtractValueInst *cloneImpl() const;
  1785. public:
  1786. static ExtractValueInst *Create(Value *Agg,
  1787. ArrayRef<unsigned> Idxs,
  1788. const Twine &NameStr = "",
  1789. Instruction *InsertBefore = nullptr) {
  1790. return new
  1791. ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
  1792. }
  1793. static ExtractValueInst *Create(Value *Agg,
  1794. ArrayRef<unsigned> Idxs,
  1795. const Twine &NameStr,
  1796. BasicBlock *InsertAtEnd) {
  1797. return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
  1798. }
  1799. /// getIndexedType - Returns the type of the element that would be extracted
  1800. /// with an extractvalue instruction with the specified parameters.
  1801. ///
  1802. /// Null is returned if the indices are invalid for the specified type.
  1803. static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
  1804. typedef const unsigned* idx_iterator;
  1805. inline idx_iterator idx_begin() const { return Indices.begin(); }
  1806. inline idx_iterator idx_end() const { return Indices.end(); }
  1807. inline iterator_range<idx_iterator> indices() const {
  1808. return iterator_range<idx_iterator>(idx_begin(), idx_end());
  1809. }
  1810. Value *getAggregateOperand() {
  1811. return getOperand(0);
  1812. }
  1813. const Value *getAggregateOperand() const {
  1814. return getOperand(0);
  1815. }
  1816. static unsigned getAggregateOperandIndex() {
  1817. return 0U; // get index for modifying correct operand
  1818. }
  1819. ArrayRef<unsigned> getIndices() const {
  1820. return Indices;
  1821. }
  1822. unsigned getNumIndices() const {
  1823. return (unsigned)Indices.size();
  1824. }
  1825. bool hasIndices() const {
  1826. return true;
  1827. }
  1828. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1829. static inline bool classof(const Instruction *I) {
  1830. return I->getOpcode() == Instruction::ExtractValue;
  1831. }
  1832. static inline bool classof(const Value *V) {
  1833. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1834. }
  1835. };
  1836. ExtractValueInst::ExtractValueInst(Value *Agg,
  1837. ArrayRef<unsigned> Idxs,
  1838. const Twine &NameStr,
  1839. Instruction *InsertBefore)
  1840. : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
  1841. ExtractValue, Agg, InsertBefore) {
  1842. init(Idxs, NameStr);
  1843. }
  1844. ExtractValueInst::ExtractValueInst(Value *Agg,
  1845. ArrayRef<unsigned> Idxs,
  1846. const Twine &NameStr,
  1847. BasicBlock *InsertAtEnd)
  1848. : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
  1849. ExtractValue, Agg, InsertAtEnd) {
  1850. init(Idxs, NameStr);
  1851. }
  1852. //===----------------------------------------------------------------------===//
  1853. // InsertValueInst Class
  1854. //===----------------------------------------------------------------------===//
  1855. /// InsertValueInst - This instruction inserts a struct field of array element
  1856. /// value into an aggregate value.
  1857. ///
  1858. class InsertValueInst : public Instruction {
  1859. SmallVector<unsigned, 4> Indices;
  1860. void *operator new(size_t, unsigned) = delete;
  1861. InsertValueInst(const InsertValueInst &IVI);
  1862. void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
  1863. const Twine &NameStr);
  1864. /// Constructors - Create a insertvalue instruction with a base aggregate
  1865. /// value, a value to insert, and a list of indices. The first ctor can
  1866. /// optionally insert before an existing instruction, the second appends
  1867. /// the new instruction to the specified BasicBlock.
  1868. inline InsertValueInst(Value *Agg, Value *Val,
  1869. ArrayRef<unsigned> Idxs,
  1870. const Twine &NameStr,
  1871. Instruction *InsertBefore);
  1872. inline InsertValueInst(Value *Agg, Value *Val,
  1873. ArrayRef<unsigned> Idxs,
  1874. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1875. /// Constructors - These two constructors are convenience methods because one
  1876. /// and two index insertvalue instructions are so common.
  1877. InsertValueInst(Value *Agg, Value *Val,
  1878. unsigned Idx, const Twine &NameStr = "",
  1879. Instruction *InsertBefore = nullptr);
  1880. InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
  1881. const Twine &NameStr, BasicBlock *InsertAtEnd);
  1882. protected:
  1883. // Note: Instruction needs to be a friend here to call cloneImpl.
  1884. friend class Instruction;
  1885. InsertValueInst *cloneImpl() const;
  1886. public:
  1887. // allocate space for exactly two operands
  1888. void *operator new(size_t s) {
  1889. return User::operator new(s, 2);
  1890. }
  1891. static InsertValueInst *Create(Value *Agg, Value *Val,
  1892. ArrayRef<unsigned> Idxs,
  1893. const Twine &NameStr = "",
  1894. Instruction *InsertBefore = nullptr) {
  1895. return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
  1896. }
  1897. static InsertValueInst *Create(Value *Agg, Value *Val,
  1898. ArrayRef<unsigned> Idxs,
  1899. const Twine &NameStr,
  1900. BasicBlock *InsertAtEnd) {
  1901. return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
  1902. }
  1903. /// Transparently provide more efficient getOperand methods.
  1904. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  1905. typedef const unsigned* idx_iterator;
  1906. inline idx_iterator idx_begin() const { return Indices.begin(); }
  1907. inline idx_iterator idx_end() const { return Indices.end(); }
  1908. inline iterator_range<idx_iterator> indices() const {
  1909. return iterator_range<idx_iterator>(idx_begin(), idx_end());
  1910. }
  1911. Value *getAggregateOperand() {
  1912. return getOperand(0);
  1913. }
  1914. const Value *getAggregateOperand() const {
  1915. return getOperand(0);
  1916. }
  1917. static unsigned getAggregateOperandIndex() {
  1918. return 0U; // get index for modifying correct operand
  1919. }
  1920. Value *getInsertedValueOperand() {
  1921. return getOperand(1);
  1922. }
  1923. const Value *getInsertedValueOperand() const {
  1924. return getOperand(1);
  1925. }
  1926. static unsigned getInsertedValueOperandIndex() {
  1927. return 1U; // get index for modifying correct operand
  1928. }
  1929. ArrayRef<unsigned> getIndices() const {
  1930. return Indices;
  1931. }
  1932. unsigned getNumIndices() const {
  1933. return (unsigned)Indices.size();
  1934. }
  1935. bool hasIndices() const {
  1936. return true;
  1937. }
  1938. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1939. static inline bool classof(const Instruction *I) {
  1940. return I->getOpcode() == Instruction::InsertValue;
  1941. }
  1942. static inline bool classof(const Value *V) {
  1943. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  1944. }
  1945. };
  1946. template <>
  1947. struct OperandTraits<InsertValueInst> :
  1948. public FixedNumOperandTraits<InsertValueInst, 2> {
  1949. };
  1950. InsertValueInst::InsertValueInst(Value *Agg,
  1951. Value *Val,
  1952. ArrayRef<unsigned> Idxs,
  1953. const Twine &NameStr,
  1954. Instruction *InsertBefore)
  1955. : Instruction(Agg->getType(), InsertValue,
  1956. OperandTraits<InsertValueInst>::op_begin(this),
  1957. 2, InsertBefore) {
  1958. init(Agg, Val, Idxs, NameStr);
  1959. }
  1960. InsertValueInst::InsertValueInst(Value *Agg,
  1961. Value *Val,
  1962. ArrayRef<unsigned> Idxs,
  1963. const Twine &NameStr,
  1964. BasicBlock *InsertAtEnd)
  1965. : Instruction(Agg->getType(), InsertValue,
  1966. OperandTraits<InsertValueInst>::op_begin(this),
  1967. 2, InsertAtEnd) {
  1968. init(Agg, Val, Idxs, NameStr);
  1969. }
  1970. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
  1971. //===----------------------------------------------------------------------===//
  1972. // PHINode Class
  1973. //===----------------------------------------------------------------------===//
  1974. // PHINode - The PHINode class is used to represent the magical mystical PHI
  1975. // node, that can not exist in nature, but can be synthesized in a computer
  1976. // scientist's overactive imagination.
  1977. //
  1978. class PHINode : public Instruction {
  1979. void *operator new(size_t, unsigned) = delete;
  1980. /// ReservedSpace - The number of operands actually allocated. NumOperands is
  1981. /// the number actually in use.
  1982. unsigned ReservedSpace;
  1983. PHINode(const PHINode &PN);
  1984. // allocate space for exactly zero operands
  1985. void *operator new(size_t s) {
  1986. return User::operator new(s);
  1987. }
  1988. explicit PHINode(Type *Ty, unsigned NumReservedValues,
  1989. const Twine &NameStr = "",
  1990. Instruction *InsertBefore = nullptr)
  1991. : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
  1992. ReservedSpace(NumReservedValues) {
  1993. setName(NameStr);
  1994. allocHungoffUses(ReservedSpace);
  1995. }
  1996. PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
  1997. BasicBlock *InsertAtEnd)
  1998. : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
  1999. ReservedSpace(NumReservedValues) {
  2000. setName(NameStr);
  2001. allocHungoffUses(ReservedSpace);
  2002. }
  2003. protected:
  2004. // allocHungoffUses - this is more complicated than the generic
  2005. // User::allocHungoffUses, because we have to allocate Uses for the incoming
  2006. // values and pointers to the incoming blocks, all in one allocation.
  2007. void allocHungoffUses(unsigned N) {
  2008. User::allocHungoffUses(N, /* IsPhi */ true);
  2009. }
  2010. // Note: Instruction needs to be a friend here to call cloneImpl.
  2011. friend class Instruction;
  2012. PHINode *cloneImpl() const;
  2013. public:
  2014. /// Constructors - NumReservedValues is a hint for the number of incoming
  2015. /// edges that this phi node will have (use 0 if you really have no idea).
  2016. static PHINode *Create(Type *Ty, unsigned NumReservedValues,
  2017. const Twine &NameStr = "",
  2018. Instruction *InsertBefore = nullptr) {
  2019. return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
  2020. }
  2021. static PHINode *Create(Type *Ty, unsigned NumReservedValues,
  2022. const Twine &NameStr, BasicBlock *InsertAtEnd) {
  2023. return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
  2024. }
  2025. /// Provide fast operand accessors
  2026. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2027. // Block iterator interface. This provides access to the list of incoming
  2028. // basic blocks, which parallels the list of incoming values.
  2029. typedef BasicBlock **block_iterator;
  2030. typedef BasicBlock * const *const_block_iterator;
  2031. block_iterator block_begin() {
  2032. Use::UserRef *ref =
  2033. reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
  2034. return reinterpret_cast<block_iterator>(ref + 1);
  2035. }
  2036. const_block_iterator block_begin() const {
  2037. const Use::UserRef *ref =
  2038. reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
  2039. return reinterpret_cast<const_block_iterator>(ref + 1);
  2040. }
  2041. block_iterator block_end() {
  2042. return block_begin() + getNumOperands();
  2043. }
  2044. const_block_iterator block_end() const {
  2045. return block_begin() + getNumOperands();
  2046. }
  2047. op_range incoming_values() { return operands(); }
  2048. const_op_range incoming_values() const { return operands(); }
  2049. /// getNumIncomingValues - Return the number of incoming edges
  2050. ///
  2051. unsigned getNumIncomingValues() const { return getNumOperands(); }
  2052. /// getIncomingValue - Return incoming value number x
  2053. ///
  2054. Value *getIncomingValue(unsigned i) const {
  2055. return getOperand(i);
  2056. }
  2057. void setIncomingValue(unsigned i, Value *V) {
  2058. setOperand(i, V);
  2059. }
  2060. static unsigned getOperandNumForIncomingValue(unsigned i) {
  2061. return i;
  2062. }
  2063. static unsigned getIncomingValueNumForOperand(unsigned i) {
  2064. return i;
  2065. }
  2066. /// getIncomingBlock - Return incoming basic block number @p i.
  2067. ///
  2068. BasicBlock *getIncomingBlock(unsigned i) const {
  2069. return block_begin()[i];
  2070. }
  2071. /// getIncomingBlock - Return incoming basic block corresponding
  2072. /// to an operand of the PHI.
  2073. ///
  2074. BasicBlock *getIncomingBlock(const Use &U) const {
  2075. assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
  2076. return getIncomingBlock(unsigned(&U - op_begin()));
  2077. }
  2078. /// getIncomingBlock - Return incoming basic block corresponding
  2079. /// to value use iterator.
  2080. ///
  2081. BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
  2082. return getIncomingBlock(I.getUse());
  2083. }
  2084. void setIncomingBlock(unsigned i, BasicBlock *BB) {
  2085. block_begin()[i] = BB;
  2086. }
  2087. /// addIncoming - Add an incoming value to the end of the PHI list
  2088. ///
  2089. void addIncoming(Value *V, BasicBlock *BB) {
  2090. assert(V && "PHI node got a null value!");
  2091. assert(BB && "PHI node got a null basic block!");
  2092. assert(getType() == V->getType() &&
  2093. "All operands to PHI node must be the same type as the PHI node!");
  2094. if (getNumOperands() == ReservedSpace)
  2095. growOperands(); // Get more space!
  2096. // Initialize some new operands.
  2097. setNumHungOffUseOperands(getNumOperands() + 1);
  2098. setIncomingValue(getNumOperands() - 1, V);
  2099. setIncomingBlock(getNumOperands() - 1, BB);
  2100. }
  2101. /// removeIncomingValue - Remove an incoming value. This is useful if a
  2102. /// predecessor basic block is deleted. The value removed is returned.
  2103. ///
  2104. /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
  2105. /// is true), the PHI node is destroyed and any uses of it are replaced with
  2106. /// dummy values. The only time there should be zero incoming values to a PHI
  2107. /// node is when the block is dead, so this strategy is sound.
  2108. ///
  2109. Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
  2110. Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
  2111. int Idx = getBasicBlockIndex(BB);
  2112. assert(Idx >= 0 && "Invalid basic block argument to remove!");
  2113. return removeIncomingValue(Idx, DeletePHIIfEmpty);
  2114. }
  2115. /// getBasicBlockIndex - Return the first index of the specified basic
  2116. /// block in the value list for this PHI. Returns -1 if no instance.
  2117. ///
  2118. int getBasicBlockIndex(const BasicBlock *BB) const {
  2119. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  2120. if (block_begin()[i] == BB)
  2121. return i;
  2122. return -1;
  2123. }
  2124. Value *getIncomingValueForBlock(const BasicBlock *BB) const {
  2125. int Idx = getBasicBlockIndex(BB);
  2126. assert(Idx >= 0 && "Invalid basic block argument!");
  2127. return getIncomingValue(Idx);
  2128. }
  2129. /// hasConstantValue - If the specified PHI node always merges together the
  2130. /// same value, return the value, otherwise return null.
  2131. Value *hasConstantValue() const;
  2132. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  2133. static inline bool classof(const Instruction *I) {
  2134. return I->getOpcode() == Instruction::PHI;
  2135. }
  2136. static inline bool classof(const Value *V) {
  2137. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2138. }
  2139. private:
  2140. void growOperands();
  2141. };
  2142. template <>
  2143. struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
  2144. };
  2145. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
  2146. //===----------------------------------------------------------------------===//
  2147. // LandingPadInst Class
  2148. //===----------------------------------------------------------------------===//
  2149. //===---------------------------------------------------------------------------
  2150. /// LandingPadInst - The landingpad instruction holds all of the information
  2151. /// necessary to generate correct exception handling. The landingpad instruction
  2152. /// cannot be moved from the top of a landing pad block, which itself is
  2153. /// accessible only from the 'unwind' edge of an invoke. This uses the
  2154. /// SubclassData field in Value to store whether or not the landingpad is a
  2155. /// cleanup.
  2156. ///
  2157. class LandingPadInst : public Instruction {
  2158. /// ReservedSpace - The number of operands actually allocated. NumOperands is
  2159. /// the number actually in use.
  2160. unsigned ReservedSpace;
  2161. LandingPadInst(const LandingPadInst &LP);
  2162. public:
  2163. enum ClauseType { Catch, Filter };
  2164. private:
  2165. void *operator new(size_t, unsigned) = delete;
  2166. // Allocate space for exactly zero operands.
  2167. void *operator new(size_t s) {
  2168. return User::operator new(s);
  2169. }
  2170. void growOperands(unsigned Size);
  2171. void init(unsigned NumReservedValues, const Twine &NameStr);
  2172. explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
  2173. const Twine &NameStr, Instruction *InsertBefore);
  2174. explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
  2175. const Twine &NameStr, BasicBlock *InsertAtEnd);
  2176. protected:
  2177. // Note: Instruction needs to be a friend here to call cloneImpl.
  2178. friend class Instruction;
  2179. LandingPadInst *cloneImpl() const;
  2180. public:
  2181. /// Constructors - NumReservedClauses is a hint for the number of incoming
  2182. /// clauses that this landingpad will have (use 0 if you really have no idea).
  2183. static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
  2184. const Twine &NameStr = "",
  2185. Instruction *InsertBefore = nullptr);
  2186. static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
  2187. const Twine &NameStr, BasicBlock *InsertAtEnd);
  2188. /// Provide fast operand accessors
  2189. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2190. /// isCleanup - Return 'true' if this landingpad instruction is a
  2191. /// cleanup. I.e., it should be run when unwinding even if its landing pad
  2192. /// doesn't catch the exception.
  2193. bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
  2194. /// setCleanup - Indicate that this landingpad instruction is a cleanup.
  2195. void setCleanup(bool V) {
  2196. setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
  2197. (V ? 1 : 0));
  2198. }
  2199. /// Add a catch or filter clause to the landing pad.
  2200. void addClause(Constant *ClauseVal);
  2201. /// Get the value of the clause at index Idx. Use isCatch/isFilter to
  2202. /// determine what type of clause this is.
  2203. Constant *getClause(unsigned Idx) const {
  2204. return cast<Constant>(getOperandList()[Idx]);
  2205. }
  2206. /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
  2207. bool isCatch(unsigned Idx) const {
  2208. return !isa<ArrayType>(getOperandList()[Idx]->getType());
  2209. }
  2210. /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
  2211. bool isFilter(unsigned Idx) const {
  2212. return isa<ArrayType>(getOperandList()[Idx]->getType());
  2213. }
  2214. /// getNumClauses - Get the number of clauses for this landing pad.
  2215. unsigned getNumClauses() const { return getNumOperands(); }
  2216. /// reserveClauses - Grow the size of the operand list to accommodate the new
  2217. /// number of clauses.
  2218. void reserveClauses(unsigned Size) { growOperands(Size); }
  2219. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2220. static inline bool classof(const Instruction *I) {
  2221. return I->getOpcode() == Instruction::LandingPad;
  2222. }
  2223. static inline bool classof(const Value *V) {
  2224. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2225. }
  2226. };
  2227. template <>
  2228. struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
  2229. };
  2230. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
  2231. //===----------------------------------------------------------------------===//
  2232. // ReturnInst Class
  2233. //===----------------------------------------------------------------------===//
  2234. //===---------------------------------------------------------------------------
  2235. /// ReturnInst - Return a value (possibly void), from a function. Execution
  2236. /// does not continue in this function any longer.
  2237. ///
  2238. class ReturnInst : public TerminatorInst {
  2239. ReturnInst(const ReturnInst &RI);
  2240. private:
  2241. // ReturnInst constructors:
  2242. // ReturnInst() - 'ret void' instruction
  2243. // ReturnInst( null) - 'ret void' instruction
  2244. // ReturnInst(Value* X) - 'ret X' instruction
  2245. // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
  2246. // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
  2247. // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
  2248. // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
  2249. //
  2250. // NOTE: If the Value* passed is of type void then the constructor behaves as
  2251. // if it was passed NULL.
  2252. explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
  2253. Instruction *InsertBefore = nullptr);
  2254. ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
  2255. explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
  2256. protected:
  2257. // Note: Instruction needs to be a friend here to call cloneImpl.
  2258. friend class Instruction;
  2259. ReturnInst *cloneImpl() const;
  2260. public:
  2261. static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
  2262. Instruction *InsertBefore = nullptr) {
  2263. return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
  2264. }
  2265. static ReturnInst* Create(LLVMContext &C, Value *retVal,
  2266. BasicBlock *InsertAtEnd) {
  2267. return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
  2268. }
  2269. static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
  2270. return new(0) ReturnInst(C, InsertAtEnd);
  2271. }
  2272. ~ReturnInst() override;
  2273. /// Provide fast operand accessors
  2274. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2275. /// Convenience accessor. Returns null if there is no return value.
  2276. Value *getReturnValue() const {
  2277. return getNumOperands() != 0 ? getOperand(0) : nullptr;
  2278. }
  2279. unsigned getNumSuccessors() const { return 0; }
  2280. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2281. static inline bool classof(const Instruction *I) {
  2282. return (I->getOpcode() == Instruction::Ret);
  2283. }
  2284. static inline bool classof(const Value *V) {
  2285. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2286. }
  2287. private:
  2288. BasicBlock *getSuccessorV(unsigned idx) const override;
  2289. unsigned getNumSuccessorsV() const override;
  2290. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  2291. };
  2292. template <>
  2293. struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
  2294. };
  2295. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
  2296. //===----------------------------------------------------------------------===//
  2297. // BranchInst Class
  2298. //===----------------------------------------------------------------------===//
  2299. //===---------------------------------------------------------------------------
  2300. /// BranchInst - Conditional or Unconditional Branch instruction.
  2301. ///
  2302. class BranchInst : public TerminatorInst {
  2303. /// Ops list - Branches are strange. The operands are ordered:
  2304. /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
  2305. /// they don't have to check for cond/uncond branchness. These are mostly
  2306. /// accessed relative from op_end().
  2307. BranchInst(const BranchInst &BI);
  2308. void AssertOK();
  2309. // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
  2310. // BranchInst(BB *B) - 'br B'
  2311. // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
  2312. // BranchInst(BB* B, Inst *I) - 'br B' insert before I
  2313. // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
  2314. // BranchInst(BB* B, BB *I) - 'br B' insert at end
  2315. // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
  2316. explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
  2317. BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
  2318. Instruction *InsertBefore = nullptr);
  2319. BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
  2320. BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
  2321. BasicBlock *InsertAtEnd);
  2322. protected:
  2323. // Note: Instruction needs to be a friend here to call cloneImpl.
  2324. friend class Instruction;
  2325. BranchInst *cloneImpl() const;
  2326. public:
  2327. static BranchInst *Create(BasicBlock *IfTrue,
  2328. Instruction *InsertBefore = nullptr) {
  2329. return new(1) BranchInst(IfTrue, InsertBefore);
  2330. }
  2331. static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
  2332. Value *Cond, Instruction *InsertBefore = nullptr) {
  2333. return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
  2334. }
  2335. static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
  2336. return new(1) BranchInst(IfTrue, InsertAtEnd);
  2337. }
  2338. static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
  2339. Value *Cond, BasicBlock *InsertAtEnd) {
  2340. return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
  2341. }
  2342. /// Transparently provide more efficient getOperand methods.
  2343. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2344. bool isUnconditional() const { return getNumOperands() == 1; }
  2345. bool isConditional() const { return getNumOperands() == 3; }
  2346. Value *getCondition() const {
  2347. assert(isConditional() && "Cannot get condition of an uncond branch!");
  2348. return Op<-3>();
  2349. }
  2350. void setCondition(Value *V) {
  2351. assert(isConditional() && "Cannot set condition of unconditional branch!");
  2352. Op<-3>() = V;
  2353. }
  2354. unsigned getNumSuccessors() const { return 1+isConditional(); }
  2355. BasicBlock *getSuccessor(unsigned i) const {
  2356. assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
  2357. return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
  2358. }
  2359. void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
  2360. assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
  2361. *(&Op<-1>() - idx) = (Value*)NewSucc;
  2362. }
  2363. /// \brief Swap the successors of this branch instruction.
  2364. ///
  2365. /// Swaps the successors of the branch instruction. This also swaps any
  2366. /// branch weight metadata associated with the instruction so that it
  2367. /// continues to map correctly to each operand.
  2368. void swapSuccessors();
  2369. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2370. static inline bool classof(const Instruction *I) {
  2371. return (I->getOpcode() == Instruction::Br);
  2372. }
  2373. static inline bool classof(const Value *V) {
  2374. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2375. }
  2376. private:
  2377. BasicBlock *getSuccessorV(unsigned idx) const override;
  2378. unsigned getNumSuccessorsV() const override;
  2379. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  2380. };
  2381. template <>
  2382. struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
  2383. };
  2384. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
  2385. //===----------------------------------------------------------------------===//
  2386. // SwitchInst Class
  2387. //===----------------------------------------------------------------------===//
  2388. //===---------------------------------------------------------------------------
  2389. /// SwitchInst - Multiway switch
  2390. ///
  2391. class SwitchInst : public TerminatorInst {
  2392. void *operator new(size_t, unsigned) = delete;
  2393. unsigned ReservedSpace;
  2394. // Operand[0] = Value to switch on
  2395. // Operand[1] = Default basic block destination
  2396. // Operand[2n ] = Value to match
  2397. // Operand[2n+1] = BasicBlock to go to on match
  2398. SwitchInst(const SwitchInst &SI);
  2399. void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
  2400. void growOperands();
  2401. // allocate space for exactly zero operands
  2402. void *operator new(size_t s) {
  2403. return User::operator new(s);
  2404. }
  2405. /// SwitchInst ctor - Create a new switch instruction, specifying a value to
  2406. /// switch on and a default destination. The number of additional cases can
  2407. /// be specified here to make memory allocation more efficient. This
  2408. /// constructor can also autoinsert before another instruction.
  2409. SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
  2410. Instruction *InsertBefore);
  2411. /// SwitchInst ctor - Create a new switch instruction, specifying a value to
  2412. /// switch on and a default destination. The number of additional cases can
  2413. /// be specified here to make memory allocation more efficient. This
  2414. /// constructor also autoinserts at the end of the specified BasicBlock.
  2415. SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
  2416. BasicBlock *InsertAtEnd);
  2417. protected:
  2418. // Note: Instruction needs to be a friend here to call cloneImpl.
  2419. friend class Instruction;
  2420. SwitchInst *cloneImpl() const;
  2421. public:
  2422. // -2
  2423. static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
  2424. template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
  2425. class CaseIteratorT {
  2426. protected:
  2427. SwitchInstTy *SI;
  2428. unsigned Index;
  2429. public:
  2430. typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
  2431. /// Initializes case iterator for given SwitchInst and for given
  2432. /// case number.
  2433. CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
  2434. this->SI = SI;
  2435. Index = CaseNum;
  2436. }
  2437. /// Initializes case iterator for given SwitchInst and for given
  2438. /// TerminatorInst's successor index.
  2439. static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
  2440. assert(SuccessorIndex < SI->getNumSuccessors() &&
  2441. "Successor index # out of range!");
  2442. return SuccessorIndex != 0 ?
  2443. Self(SI, SuccessorIndex - 1) :
  2444. Self(SI, DefaultPseudoIndex);
  2445. }
  2446. /// Resolves case value for current case.
  2447. ConstantIntTy *getCaseValue() {
  2448. assert(Index < SI->getNumCases() && "Index out the number of cases.");
  2449. return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
  2450. }
  2451. /// Resolves successor for current case.
  2452. BasicBlockTy *getCaseSuccessor() {
  2453. assert((Index < SI->getNumCases() ||
  2454. Index == DefaultPseudoIndex) &&
  2455. "Index out the number of cases.");
  2456. return SI->getSuccessor(getSuccessorIndex());
  2457. }
  2458. /// Returns number of current case.
  2459. unsigned getCaseIndex() const { return Index; }
  2460. /// Returns TerminatorInst's successor index for current case successor.
  2461. unsigned getSuccessorIndex() const {
  2462. assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
  2463. "Index out the number of cases.");
  2464. return Index != DefaultPseudoIndex ? Index + 1 : 0;
  2465. }
  2466. Self operator++() {
  2467. // Check index correctness after increment.
  2468. // Note: Index == getNumCases() means end().
  2469. assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
  2470. ++Index;
  2471. return *this;
  2472. }
  2473. Self operator++(int) {
  2474. Self tmp = *this;
  2475. ++(*this);
  2476. return tmp;
  2477. }
  2478. Self operator--() {
  2479. // Check index correctness after decrement.
  2480. // Note: Index == getNumCases() means end().
  2481. // Also allow "-1" iterator here. That will became valid after ++.
  2482. assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
  2483. "Index out the number of cases.");
  2484. --Index;
  2485. return *this;
  2486. }
  2487. Self operator--(int) {
  2488. Self tmp = *this;
  2489. --(*this);
  2490. return tmp;
  2491. }
  2492. bool operator==(const Self& RHS) const {
  2493. assert(RHS.SI == SI && "Incompatible operators.");
  2494. return RHS.Index == Index;
  2495. }
  2496. bool operator!=(const Self& RHS) const {
  2497. assert(RHS.SI == SI && "Incompatible operators.");
  2498. return RHS.Index != Index;
  2499. }
  2500. Self &operator*() {
  2501. return *this;
  2502. }
  2503. };
  2504. typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
  2505. ConstCaseIt;
  2506. class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
  2507. typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
  2508. public:
  2509. CaseIt(const ParentTy& Src) : ParentTy(Src) {}
  2510. CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
  2511. /// Sets the new value for current case.
  2512. void setValue(ConstantInt *V) {
  2513. assert(Index < SI->getNumCases() && "Index out the number of cases.");
  2514. SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
  2515. }
  2516. /// Sets the new successor for current case.
  2517. void setSuccessor(BasicBlock *S) {
  2518. SI->setSuccessor(getSuccessorIndex(), S);
  2519. }
  2520. };
  2521. static SwitchInst *Create(Value *Value, BasicBlock *Default,
  2522. unsigned NumCases,
  2523. Instruction *InsertBefore = nullptr) {
  2524. return new SwitchInst(Value, Default, NumCases, InsertBefore);
  2525. }
  2526. static SwitchInst *Create(Value *Value, BasicBlock *Default,
  2527. unsigned NumCases, BasicBlock *InsertAtEnd) {
  2528. return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
  2529. }
  2530. /// Provide fast operand accessors
  2531. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2532. // Accessor Methods for Switch stmt
  2533. Value *getCondition() const { return getOperand(0); }
  2534. void setCondition(Value *V) { setOperand(0, V); }
  2535. BasicBlock *getDefaultDest() const {
  2536. return cast<BasicBlock>(getOperand(1));
  2537. }
  2538. void setDefaultDest(BasicBlock *DefaultCase) {
  2539. setOperand(1, reinterpret_cast<Value*>(DefaultCase));
  2540. }
  2541. /// getNumCases - return the number of 'cases' in this switch instruction,
  2542. /// except the default case
  2543. unsigned getNumCases() const {
  2544. return getNumOperands()/2 - 1;
  2545. }
  2546. /// Returns a read/write iterator that points to the first
  2547. /// case in SwitchInst.
  2548. CaseIt case_begin() {
  2549. return CaseIt(this, 0);
  2550. }
  2551. /// Returns a read-only iterator that points to the first
  2552. /// case in the SwitchInst.
  2553. ConstCaseIt case_begin() const {
  2554. return ConstCaseIt(this, 0);
  2555. }
  2556. /// Returns a read/write iterator that points one past the last
  2557. /// in the SwitchInst.
  2558. CaseIt case_end() {
  2559. return CaseIt(this, getNumCases());
  2560. }
  2561. /// Returns a read-only iterator that points one past the last
  2562. /// in the SwitchInst.
  2563. ConstCaseIt case_end() const {
  2564. return ConstCaseIt(this, getNumCases());
  2565. }
  2566. /// cases - iteration adapter for range-for loops.
  2567. iterator_range<CaseIt> cases() {
  2568. return iterator_range<CaseIt>(case_begin(), case_end());
  2569. }
  2570. /// cases - iteration adapter for range-for loops.
  2571. iterator_range<ConstCaseIt> cases() const {
  2572. return iterator_range<ConstCaseIt>(case_begin(), case_end());
  2573. }
  2574. /// Returns an iterator that points to the default case.
  2575. /// Note: this iterator allows to resolve successor only. Attempt
  2576. /// to resolve case value causes an assertion.
  2577. /// Also note, that increment and decrement also causes an assertion and
  2578. /// makes iterator invalid.
  2579. CaseIt case_default() {
  2580. return CaseIt(this, DefaultPseudoIndex);
  2581. }
  2582. ConstCaseIt case_default() const {
  2583. return ConstCaseIt(this, DefaultPseudoIndex);
  2584. }
  2585. /// findCaseValue - Search all of the case values for the specified constant.
  2586. /// If it is explicitly handled, return the case iterator of it, otherwise
  2587. /// return default case iterator to indicate
  2588. /// that it is handled by the default handler.
  2589. CaseIt findCaseValue(const ConstantInt *C) {
  2590. for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
  2591. if (i.getCaseValue() == C)
  2592. return i;
  2593. return case_default();
  2594. }
  2595. ConstCaseIt findCaseValue(const ConstantInt *C) const {
  2596. for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
  2597. if (i.getCaseValue() == C)
  2598. return i;
  2599. return case_default();
  2600. }
  2601. /// findCaseDest - Finds the unique case value for a given successor. Returns
  2602. /// null if the successor is not found, not unique, or is the default case.
  2603. ConstantInt *findCaseDest(BasicBlock *BB) {
  2604. if (BB == getDefaultDest()) return nullptr;
  2605. ConstantInt *CI = nullptr;
  2606. for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
  2607. if (i.getCaseSuccessor() == BB) {
  2608. if (CI) return nullptr; // Multiple cases lead to BB.
  2609. else CI = i.getCaseValue();
  2610. }
  2611. }
  2612. return CI;
  2613. }
  2614. /// addCase - Add an entry to the switch instruction...
  2615. /// Note:
  2616. /// This action invalidates case_end(). Old case_end() iterator will
  2617. /// point to the added case.
  2618. void addCase(ConstantInt *OnVal, BasicBlock *Dest);
  2619. /// removeCase - This method removes the specified case and its successor
  2620. /// from the switch instruction. Note that this operation may reorder the
  2621. /// remaining cases at index idx and above.
  2622. /// Note:
  2623. /// This action invalidates iterators for all cases following the one removed,
  2624. /// including the case_end() iterator.
  2625. void removeCase(CaseIt i);
  2626. unsigned getNumSuccessors() const { return getNumOperands()/2; }
  2627. BasicBlock *getSuccessor(unsigned idx) const {
  2628. assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
  2629. return cast<BasicBlock>(getOperand(idx*2+1));
  2630. }
  2631. void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
  2632. assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
  2633. setOperand(idx*2+1, (Value*)NewSucc);
  2634. }
  2635. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2636. static inline bool classof(const Instruction *I) {
  2637. return I->getOpcode() == Instruction::Switch;
  2638. }
  2639. static inline bool classof(const Value *V) {
  2640. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2641. }
  2642. private:
  2643. BasicBlock *getSuccessorV(unsigned idx) const override;
  2644. unsigned getNumSuccessorsV() const override;
  2645. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  2646. };
  2647. template <>
  2648. struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
  2649. };
  2650. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
  2651. //===----------------------------------------------------------------------===//
  2652. // IndirectBrInst Class
  2653. //===----------------------------------------------------------------------===//
  2654. //===---------------------------------------------------------------------------
  2655. /// IndirectBrInst - Indirect Branch Instruction.
  2656. ///
  2657. class IndirectBrInst : public TerminatorInst {
  2658. void *operator new(size_t, unsigned) = delete;
  2659. unsigned ReservedSpace;
  2660. // Operand[0] = Value to switch on
  2661. // Operand[1] = Default basic block destination
  2662. // Operand[2n ] = Value to match
  2663. // Operand[2n+1] = BasicBlock to go to on match
  2664. IndirectBrInst(const IndirectBrInst &IBI);
  2665. void init(Value *Address, unsigned NumDests);
  2666. void growOperands();
  2667. // allocate space for exactly zero operands
  2668. void *operator new(size_t s) {
  2669. return User::operator new(s);
  2670. }
  2671. /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
  2672. /// Address to jump to. The number of expected destinations can be specified
  2673. /// here to make memory allocation more efficient. This constructor can also
  2674. /// autoinsert before another instruction.
  2675. IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
  2676. /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
  2677. /// Address to jump to. The number of expected destinations can be specified
  2678. /// here to make memory allocation more efficient. This constructor also
  2679. /// autoinserts at the end of the specified BasicBlock.
  2680. IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
  2681. protected:
  2682. // Note: Instruction needs to be a friend here to call cloneImpl.
  2683. friend class Instruction;
  2684. IndirectBrInst *cloneImpl() const;
  2685. public:
  2686. static IndirectBrInst *Create(Value *Address, unsigned NumDests,
  2687. Instruction *InsertBefore = nullptr) {
  2688. return new IndirectBrInst(Address, NumDests, InsertBefore);
  2689. }
  2690. static IndirectBrInst *Create(Value *Address, unsigned NumDests,
  2691. BasicBlock *InsertAtEnd) {
  2692. return new IndirectBrInst(Address, NumDests, InsertAtEnd);
  2693. }
  2694. /// Provide fast operand accessors.
  2695. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2696. // Accessor Methods for IndirectBrInst instruction.
  2697. Value *getAddress() { return getOperand(0); }
  2698. const Value *getAddress() const { return getOperand(0); }
  2699. void setAddress(Value *V) { setOperand(0, V); }
  2700. /// getNumDestinations - return the number of possible destinations in this
  2701. /// indirectbr instruction.
  2702. unsigned getNumDestinations() const { return getNumOperands()-1; }
  2703. /// getDestination - Return the specified destination.
  2704. BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
  2705. const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
  2706. /// addDestination - Add a destination.
  2707. ///
  2708. void addDestination(BasicBlock *Dest);
  2709. /// removeDestination - This method removes the specified successor from the
  2710. /// indirectbr instruction.
  2711. void removeDestination(unsigned i);
  2712. unsigned getNumSuccessors() const { return getNumOperands()-1; }
  2713. BasicBlock *getSuccessor(unsigned i) const {
  2714. return cast<BasicBlock>(getOperand(i+1));
  2715. }
  2716. void setSuccessor(unsigned i, BasicBlock *NewSucc) {
  2717. setOperand(i+1, (Value*)NewSucc);
  2718. }
  2719. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2720. static inline bool classof(const Instruction *I) {
  2721. return I->getOpcode() == Instruction::IndirectBr;
  2722. }
  2723. static inline bool classof(const Value *V) {
  2724. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2725. }
  2726. private:
  2727. BasicBlock *getSuccessorV(unsigned idx) const override;
  2728. unsigned getNumSuccessorsV() const override;
  2729. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  2730. };
  2731. template <>
  2732. struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
  2733. };
  2734. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
  2735. //===----------------------------------------------------------------------===//
  2736. // InvokeInst Class
  2737. //===----------------------------------------------------------------------===//
  2738. /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
  2739. /// calling convention of the call.
  2740. ///
  2741. class InvokeInst : public TerminatorInst {
  2742. AttributeSet AttributeList;
  2743. FunctionType *FTy;
  2744. InvokeInst(const InvokeInst &BI);
  2745. void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
  2746. ArrayRef<Value *> Args, const Twine &NameStr) {
  2747. init(cast<FunctionType>(
  2748. cast<PointerType>(Func->getType())->getElementType()),
  2749. Func, IfNormal, IfException, Args, NameStr);
  2750. }
  2751. void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
  2752. BasicBlock *IfException, ArrayRef<Value *> Args,
  2753. const Twine &NameStr);
  2754. /// Construct an InvokeInst given a range of arguments.
  2755. ///
  2756. /// \brief Construct an InvokeInst from a range of arguments
  2757. inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
  2758. ArrayRef<Value *> Args, unsigned Values,
  2759. const Twine &NameStr, Instruction *InsertBefore)
  2760. : InvokeInst(cast<FunctionType>(
  2761. cast<PointerType>(Func->getType())->getElementType()),
  2762. Func, IfNormal, IfException, Args, Values, NameStr,
  2763. InsertBefore) {}
  2764. inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
  2765. BasicBlock *IfException, ArrayRef<Value *> Args,
  2766. unsigned Values, const Twine &NameStr,
  2767. Instruction *InsertBefore);
  2768. /// Construct an InvokeInst given a range of arguments.
  2769. ///
  2770. /// \brief Construct an InvokeInst from a range of arguments
  2771. inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
  2772. ArrayRef<Value *> Args, unsigned Values,
  2773. const Twine &NameStr, BasicBlock *InsertAtEnd);
  2774. protected:
  2775. // Note: Instruction needs to be a friend here to call cloneImpl.
  2776. friend class Instruction;
  2777. InvokeInst *cloneImpl() const;
  2778. public:
  2779. static InvokeInst *Create(Value *Func,
  2780. BasicBlock *IfNormal, BasicBlock *IfException,
  2781. ArrayRef<Value *> Args, const Twine &NameStr = "",
  2782. Instruction *InsertBefore = nullptr) {
  2783. return Create(cast<FunctionType>(
  2784. cast<PointerType>(Func->getType())->getElementType()),
  2785. Func, IfNormal, IfException, Args, NameStr, InsertBefore);
  2786. }
  2787. static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
  2788. BasicBlock *IfException, ArrayRef<Value *> Args,
  2789. const Twine &NameStr = "",
  2790. Instruction *InsertBefore = nullptr) {
  2791. unsigned Values = unsigned(Args.size()) + 3;
  2792. return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args,
  2793. Values, NameStr, InsertBefore);
  2794. }
  2795. static InvokeInst *Create(Value *Func,
  2796. BasicBlock *IfNormal, BasicBlock *IfException,
  2797. ArrayRef<Value *> Args, const Twine &NameStr,
  2798. BasicBlock *InsertAtEnd) {
  2799. unsigned Values = unsigned(Args.size()) + 3;
  2800. return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
  2801. Values, NameStr, InsertAtEnd);
  2802. }
  2803. /// Provide fast operand accessors
  2804. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  2805. FunctionType *getFunctionType() const { return FTy; }
  2806. void mutateFunctionType(FunctionType *FTy) {
  2807. mutateType(FTy->getReturnType());
  2808. this->FTy = FTy;
  2809. }
  2810. /// getNumArgOperands - Return the number of invoke arguments.
  2811. ///
  2812. unsigned getNumArgOperands() const { return getNumOperands() - 3; }
  2813. /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
  2814. ///
  2815. Value *getArgOperand(unsigned i) const { return getOperand(i); }
  2816. void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
  2817. /// arg_operands - iteration adapter for range-for loops.
  2818. iterator_range<op_iterator> arg_operands() {
  2819. return iterator_range<op_iterator>(op_begin(), op_end() - 3);
  2820. }
  2821. /// arg_operands - iteration adapter for range-for loops.
  2822. iterator_range<const_op_iterator> arg_operands() const {
  2823. return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
  2824. }
  2825. /// \brief Wrappers for getting the \c Use of a invoke argument.
  2826. const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
  2827. Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
  2828. /// getCallingConv/setCallingConv - Get or set the calling convention of this
  2829. /// function call.
  2830. CallingConv::ID getCallingConv() const {
  2831. return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
  2832. }
  2833. void setCallingConv(CallingConv::ID CC) {
  2834. setInstructionSubclassData(static_cast<unsigned>(CC));
  2835. }
  2836. /// getAttributes - Return the parameter attributes for this invoke.
  2837. ///
  2838. const AttributeSet &getAttributes() const { return AttributeList; }
  2839. /// setAttributes - Set the parameter attributes for this invoke.
  2840. ///
  2841. void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
  2842. /// addAttribute - adds the attribute to the list of attributes.
  2843. void addAttribute(unsigned i, Attribute::AttrKind attr);
  2844. /// removeAttribute - removes the attribute from the list of attributes.
  2845. void removeAttribute(unsigned i, Attribute attr);
  2846. /// \brief adds the dereferenceable attribute to the list of attributes.
  2847. void addDereferenceableAttr(unsigned i, uint64_t Bytes);
  2848. /// \brief adds the dereferenceable_or_null attribute to the list of
  2849. /// attributes.
  2850. void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
  2851. /// \brief Determine whether this call has the given attribute.
  2852. bool hasFnAttr(Attribute::AttrKind A) const {
  2853. assert(A != Attribute::NoBuiltin &&
  2854. "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
  2855. return hasFnAttrImpl(A);
  2856. }
  2857. /// \brief Determine whether the call or the callee has the given attributes.
  2858. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
  2859. /// \brief Extract the alignment for a call or parameter (0=unknown).
  2860. unsigned getParamAlignment(unsigned i) const {
  2861. return AttributeList.getParamAlignment(i);
  2862. }
  2863. /// \brief Extract the number of dereferenceable bytes for a call or
  2864. /// parameter (0=unknown).
  2865. uint64_t getDereferenceableBytes(unsigned i) const {
  2866. return AttributeList.getDereferenceableBytes(i);
  2867. }
  2868. /// \brief Extract the number of dereferenceable_or_null bytes for a call or
  2869. /// parameter (0=unknown).
  2870. uint64_t getDereferenceableOrNullBytes(unsigned i) const {
  2871. return AttributeList.getDereferenceableOrNullBytes(i);
  2872. }
  2873. /// \brief Return true if the call should not be treated as a call to a
  2874. /// builtin.
  2875. bool isNoBuiltin() const {
  2876. // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
  2877. // to check it by hand.
  2878. return hasFnAttrImpl(Attribute::NoBuiltin) &&
  2879. !hasFnAttrImpl(Attribute::Builtin);
  2880. }
  2881. /// \brief Return true if the call should not be inlined.
  2882. bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
  2883. void setIsNoInline() {
  2884. addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
  2885. }
  2886. /// \brief Determine if the call does not access memory.
  2887. bool doesNotAccessMemory() const {
  2888. return hasFnAttr(Attribute::ReadNone);
  2889. }
  2890. void setDoesNotAccessMemory() {
  2891. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
  2892. }
  2893. /// \brief Determine if the call does not access or only reads memory.
  2894. bool onlyReadsMemory() const {
  2895. return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
  2896. }
  2897. void setOnlyReadsMemory() {
  2898. addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
  2899. }
  2900. /// @brief Determine if the call access memmory only using it's pointer
  2901. /// arguments.
  2902. bool onlyAccessesArgMemory() const {
  2903. return hasFnAttr(Attribute::ArgMemOnly);
  2904. }
  2905. void setOnlyAccessesArgMemory() {
  2906. addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
  2907. }
  2908. /// \brief Determine if the call cannot return.
  2909. bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
  2910. void setDoesNotReturn() {
  2911. addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
  2912. }
  2913. /// \brief Determine if the call cannot unwind.
  2914. bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
  2915. void setDoesNotThrow() {
  2916. addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
  2917. }
  2918. /// \brief Determine if the invoke cannot be duplicated.
  2919. bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
  2920. void setCannotDuplicate() {
  2921. addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
  2922. }
  2923. /// \brief Determine if the call returns a structure through first
  2924. /// pointer argument.
  2925. bool hasStructRetAttr() const {
  2926. // Be friendly and also check the callee.
  2927. return paramHasAttr(1, Attribute::StructRet);
  2928. }
  2929. /// \brief Determine if any call argument is an aggregate passed by value.
  2930. bool hasByValArgument() const {
  2931. return AttributeList.hasAttrSomewhere(Attribute::ByVal);
  2932. }
  2933. /// getCalledFunction - Return the function called, or null if this is an
  2934. /// indirect function invocation.
  2935. ///
  2936. Function *getCalledFunction() const {
  2937. return dyn_cast<Function>(Op<-3>());
  2938. }
  2939. /// getCalledValue - Get a pointer to the function that is invoked by this
  2940. /// instruction
  2941. const Value *getCalledValue() const { return Op<-3>(); }
  2942. Value *getCalledValue() { return Op<-3>(); }
  2943. /// setCalledFunction - Set the function called.
  2944. void setCalledFunction(Value* Fn) {
  2945. setCalledFunction(
  2946. cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
  2947. Fn);
  2948. }
  2949. void setCalledFunction(FunctionType *FTy, Value *Fn) {
  2950. this->FTy = FTy;
  2951. assert(FTy == cast<FunctionType>(
  2952. cast<PointerType>(Fn->getType())->getElementType()));
  2953. Op<-3>() = Fn;
  2954. }
  2955. // get*Dest - Return the destination basic blocks...
  2956. BasicBlock *getNormalDest() const {
  2957. return cast<BasicBlock>(Op<-2>());
  2958. }
  2959. BasicBlock *getUnwindDest() const {
  2960. return cast<BasicBlock>(Op<-1>());
  2961. }
  2962. void setNormalDest(BasicBlock *B) {
  2963. Op<-2>() = reinterpret_cast<Value*>(B);
  2964. }
  2965. void setUnwindDest(BasicBlock *B) {
  2966. Op<-1>() = reinterpret_cast<Value*>(B);
  2967. }
  2968. /// getLandingPadInst - Get the landingpad instruction from the landing pad
  2969. /// block (the unwind destination).
  2970. LandingPadInst *getLandingPadInst() const;
  2971. BasicBlock *getSuccessor(unsigned i) const {
  2972. assert(i < 2 && "Successor # out of range for invoke!");
  2973. return i == 0 ? getNormalDest() : getUnwindDest();
  2974. }
  2975. void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
  2976. assert(idx < 2 && "Successor # out of range for invoke!");
  2977. *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
  2978. }
  2979. unsigned getNumSuccessors() const { return 2; }
  2980. // Methods for support type inquiry through isa, cast, and dyn_cast:
  2981. static inline bool classof(const Instruction *I) {
  2982. return (I->getOpcode() == Instruction::Invoke);
  2983. }
  2984. static inline bool classof(const Value *V) {
  2985. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  2986. }
  2987. private:
  2988. BasicBlock *getSuccessorV(unsigned idx) const override;
  2989. unsigned getNumSuccessorsV() const override;
  2990. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  2991. bool hasFnAttrImpl(Attribute::AttrKind A) const;
  2992. // Shadow Instruction::setInstructionSubclassData with a private forwarding
  2993. // method so that subclasses cannot accidentally use it.
  2994. void setInstructionSubclassData(unsigned short D) {
  2995. Instruction::setInstructionSubclassData(D);
  2996. }
  2997. };
  2998. template <>
  2999. struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
  3000. };
  3001. InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
  3002. BasicBlock *IfException, ArrayRef<Value *> Args,
  3003. unsigned Values, const Twine &NameStr,
  3004. Instruction *InsertBefore)
  3005. : TerminatorInst(Ty->getReturnType(), Instruction::Invoke,
  3006. OperandTraits<InvokeInst>::op_end(this) - Values, Values,
  3007. InsertBefore) {
  3008. init(Ty, Func, IfNormal, IfException, Args, NameStr);
  3009. }
  3010. InvokeInst::InvokeInst(Value *Func,
  3011. BasicBlock *IfNormal, BasicBlock *IfException,
  3012. ArrayRef<Value *> Args, unsigned Values,
  3013. const Twine &NameStr, BasicBlock *InsertAtEnd)
  3014. : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
  3015. ->getElementType())->getReturnType(),
  3016. Instruction::Invoke,
  3017. OperandTraits<InvokeInst>::op_end(this) - Values,
  3018. Values, InsertAtEnd) {
  3019. init(Func, IfNormal, IfException, Args, NameStr);
  3020. }
  3021. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
  3022. //===----------------------------------------------------------------------===//
  3023. // ResumeInst Class
  3024. //===----------------------------------------------------------------------===//
  3025. //===---------------------------------------------------------------------------
  3026. /// ResumeInst - Resume the propagation of an exception.
  3027. ///
  3028. class ResumeInst : public TerminatorInst {
  3029. ResumeInst(const ResumeInst &RI);
  3030. explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
  3031. ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
  3032. protected:
  3033. // Note: Instruction needs to be a friend here to call cloneImpl.
  3034. friend class Instruction;
  3035. ResumeInst *cloneImpl() const;
  3036. public:
  3037. static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
  3038. return new(1) ResumeInst(Exn, InsertBefore);
  3039. }
  3040. static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
  3041. return new(1) ResumeInst(Exn, InsertAtEnd);
  3042. }
  3043. /// Provide fast operand accessors
  3044. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  3045. /// Convenience accessor.
  3046. Value *getValue() const { return Op<0>(); }
  3047. unsigned getNumSuccessors() const { return 0; }
  3048. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3049. static inline bool classof(const Instruction *I) {
  3050. return I->getOpcode() == Instruction::Resume;
  3051. }
  3052. static inline bool classof(const Value *V) {
  3053. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3054. }
  3055. private:
  3056. BasicBlock *getSuccessorV(unsigned idx) const override;
  3057. unsigned getNumSuccessorsV() const override;
  3058. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  3059. };
  3060. template <>
  3061. struct OperandTraits<ResumeInst> :
  3062. public FixedNumOperandTraits<ResumeInst, 1> {
  3063. };
  3064. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
  3065. //===----------------------------------------------------------------------===//
  3066. // UnreachableInst Class
  3067. //===----------------------------------------------------------------------===//
  3068. //===---------------------------------------------------------------------------
  3069. /// UnreachableInst - This function has undefined behavior. In particular, the
  3070. /// presence of this instruction indicates some higher level knowledge that the
  3071. /// end of the block cannot be reached.
  3072. ///
  3073. class UnreachableInst : public TerminatorInst {
  3074. void *operator new(size_t, unsigned) = delete;
  3075. protected:
  3076. // Note: Instruction needs to be a friend here to call cloneImpl.
  3077. friend class Instruction;
  3078. UnreachableInst *cloneImpl() const;
  3079. public:
  3080. // allocate space for exactly zero operands
  3081. void *operator new(size_t s) {
  3082. return User::operator new(s, 0);
  3083. }
  3084. explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
  3085. explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
  3086. unsigned getNumSuccessors() const { return 0; }
  3087. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3088. static inline bool classof(const Instruction *I) {
  3089. return I->getOpcode() == Instruction::Unreachable;
  3090. }
  3091. static inline bool classof(const Value *V) {
  3092. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3093. }
  3094. private:
  3095. BasicBlock *getSuccessorV(unsigned idx) const override;
  3096. unsigned getNumSuccessorsV() const override;
  3097. void setSuccessorV(unsigned idx, BasicBlock *B) override;
  3098. };
  3099. //===----------------------------------------------------------------------===//
  3100. // TruncInst Class
  3101. //===----------------------------------------------------------------------===//
  3102. /// \brief This class represents a truncation of integer types.
  3103. class TruncInst : public CastInst {
  3104. protected:
  3105. // Note: Instruction needs to be a friend here to call cloneImpl.
  3106. friend class Instruction;
  3107. /// \brief Clone an identical TruncInst
  3108. TruncInst *cloneImpl() const;
  3109. public:
  3110. /// \brief Constructor with insert-before-instruction semantics
  3111. TruncInst(
  3112. Value *S, ///< The value to be truncated
  3113. Type *Ty, ///< The (smaller) type to truncate to
  3114. const Twine &NameStr = "", ///< A name for the new instruction
  3115. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3116. );
  3117. /// \brief Constructor with insert-at-end-of-block semantics
  3118. TruncInst(
  3119. Value *S, ///< The value to be truncated
  3120. Type *Ty, ///< The (smaller) type to truncate to
  3121. const Twine &NameStr, ///< A name for the new instruction
  3122. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3123. );
  3124. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3125. static inline bool classof(const Instruction *I) {
  3126. return I->getOpcode() == Trunc;
  3127. }
  3128. static inline bool classof(const Value *V) {
  3129. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3130. }
  3131. };
  3132. //===----------------------------------------------------------------------===//
  3133. // ZExtInst Class
  3134. //===----------------------------------------------------------------------===//
  3135. /// \brief This class represents zero extension of integer types.
  3136. class ZExtInst : public CastInst {
  3137. protected:
  3138. // Note: Instruction needs to be a friend here to call cloneImpl.
  3139. friend class Instruction;
  3140. /// \brief Clone an identical ZExtInst
  3141. ZExtInst *cloneImpl() const;
  3142. public:
  3143. /// \brief Constructor with insert-before-instruction semantics
  3144. ZExtInst(
  3145. Value *S, ///< The value to be zero extended
  3146. Type *Ty, ///< The type to zero extend to
  3147. const Twine &NameStr = "", ///< A name for the new instruction
  3148. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3149. );
  3150. /// \brief Constructor with insert-at-end semantics.
  3151. ZExtInst(
  3152. Value *S, ///< The value to be zero extended
  3153. Type *Ty, ///< The type to zero extend to
  3154. const Twine &NameStr, ///< A name for the new instruction
  3155. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3156. );
  3157. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3158. static inline bool classof(const Instruction *I) {
  3159. return I->getOpcode() == ZExt;
  3160. }
  3161. static inline bool classof(const Value *V) {
  3162. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3163. }
  3164. };
  3165. //===----------------------------------------------------------------------===//
  3166. // SExtInst Class
  3167. //===----------------------------------------------------------------------===//
  3168. /// \brief This class represents a sign extension of integer types.
  3169. class SExtInst : public CastInst {
  3170. protected:
  3171. // Note: Instruction needs to be a friend here to call cloneImpl.
  3172. friend class Instruction;
  3173. /// \brief Clone an identical SExtInst
  3174. SExtInst *cloneImpl() const;
  3175. public:
  3176. /// \brief Constructor with insert-before-instruction semantics
  3177. SExtInst(
  3178. Value *S, ///< The value to be sign extended
  3179. Type *Ty, ///< The type to sign extend to
  3180. const Twine &NameStr = "", ///< A name for the new instruction
  3181. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3182. );
  3183. /// \brief Constructor with insert-at-end-of-block semantics
  3184. SExtInst(
  3185. Value *S, ///< The value to be sign extended
  3186. Type *Ty, ///< The type to sign extend to
  3187. const Twine &NameStr, ///< A name for the new instruction
  3188. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3189. );
  3190. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3191. static inline bool classof(const Instruction *I) {
  3192. return I->getOpcode() == SExt;
  3193. }
  3194. static inline bool classof(const Value *V) {
  3195. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3196. }
  3197. };
  3198. //===----------------------------------------------------------------------===//
  3199. // FPTruncInst Class
  3200. //===----------------------------------------------------------------------===//
  3201. /// \brief This class represents a truncation of floating point types.
  3202. class FPTruncInst : public CastInst {
  3203. protected:
  3204. // Note: Instruction needs to be a friend here to call cloneImpl.
  3205. friend class Instruction;
  3206. /// \brief Clone an identical FPTruncInst
  3207. FPTruncInst *cloneImpl() const;
  3208. public:
  3209. /// \brief Constructor with insert-before-instruction semantics
  3210. FPTruncInst(
  3211. Value *S, ///< The value to be truncated
  3212. Type *Ty, ///< The type to truncate to
  3213. const Twine &NameStr = "", ///< A name for the new instruction
  3214. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3215. );
  3216. /// \brief Constructor with insert-before-instruction semantics
  3217. FPTruncInst(
  3218. Value *S, ///< The value to be truncated
  3219. Type *Ty, ///< The type to truncate to
  3220. const Twine &NameStr, ///< A name for the new instruction
  3221. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3222. );
  3223. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3224. static inline bool classof(const Instruction *I) {
  3225. return I->getOpcode() == FPTrunc;
  3226. }
  3227. static inline bool classof(const Value *V) {
  3228. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3229. }
  3230. };
  3231. //===----------------------------------------------------------------------===//
  3232. // FPExtInst Class
  3233. //===----------------------------------------------------------------------===//
  3234. /// \brief This class represents an extension of floating point types.
  3235. class FPExtInst : public CastInst {
  3236. protected:
  3237. // Note: Instruction needs to be a friend here to call cloneImpl.
  3238. friend class Instruction;
  3239. /// \brief Clone an identical FPExtInst
  3240. FPExtInst *cloneImpl() const;
  3241. public:
  3242. /// \brief Constructor with insert-before-instruction semantics
  3243. FPExtInst(
  3244. Value *S, ///< The value to be extended
  3245. Type *Ty, ///< The type to extend to
  3246. const Twine &NameStr = "", ///< A name for the new instruction
  3247. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3248. );
  3249. /// \brief Constructor with insert-at-end-of-block semantics
  3250. FPExtInst(
  3251. Value *S, ///< The value to be extended
  3252. Type *Ty, ///< The type to extend to
  3253. const Twine &NameStr, ///< A name for the new instruction
  3254. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3255. );
  3256. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3257. static inline bool classof(const Instruction *I) {
  3258. return I->getOpcode() == FPExt;
  3259. }
  3260. static inline bool classof(const Value *V) {
  3261. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3262. }
  3263. };
  3264. //===----------------------------------------------------------------------===//
  3265. // UIToFPInst Class
  3266. //===----------------------------------------------------------------------===//
  3267. /// \brief This class represents a cast unsigned integer to floating point.
  3268. class UIToFPInst : public CastInst {
  3269. protected:
  3270. // Note: Instruction needs to be a friend here to call cloneImpl.
  3271. friend class Instruction;
  3272. /// \brief Clone an identical UIToFPInst
  3273. UIToFPInst *cloneImpl() const;
  3274. public:
  3275. /// \brief Constructor with insert-before-instruction semantics
  3276. UIToFPInst(
  3277. Value *S, ///< The value to be converted
  3278. Type *Ty, ///< The type to convert to
  3279. const Twine &NameStr = "", ///< A name for the new instruction
  3280. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3281. );
  3282. /// \brief Constructor with insert-at-end-of-block semantics
  3283. UIToFPInst(
  3284. Value *S, ///< The value to be converted
  3285. Type *Ty, ///< The type to convert to
  3286. const Twine &NameStr, ///< A name for the new instruction
  3287. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3288. );
  3289. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3290. static inline bool classof(const Instruction *I) {
  3291. return I->getOpcode() == UIToFP;
  3292. }
  3293. static inline bool classof(const Value *V) {
  3294. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3295. }
  3296. };
  3297. //===----------------------------------------------------------------------===//
  3298. // SIToFPInst Class
  3299. //===----------------------------------------------------------------------===//
  3300. /// \brief This class represents a cast from signed integer to floating point.
  3301. class SIToFPInst : public CastInst {
  3302. protected:
  3303. // Note: Instruction needs to be a friend here to call cloneImpl.
  3304. friend class Instruction;
  3305. /// \brief Clone an identical SIToFPInst
  3306. SIToFPInst *cloneImpl() const;
  3307. public:
  3308. /// \brief Constructor with insert-before-instruction semantics
  3309. SIToFPInst(
  3310. Value *S, ///< The value to be converted
  3311. Type *Ty, ///< The type to convert to
  3312. const Twine &NameStr = "", ///< A name for the new instruction
  3313. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3314. );
  3315. /// \brief Constructor with insert-at-end-of-block semantics
  3316. SIToFPInst(
  3317. Value *S, ///< The value to be converted
  3318. Type *Ty, ///< The type to convert to
  3319. const Twine &NameStr, ///< A name for the new instruction
  3320. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3321. );
  3322. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3323. static inline bool classof(const Instruction *I) {
  3324. return I->getOpcode() == SIToFP;
  3325. }
  3326. static inline bool classof(const Value *V) {
  3327. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3328. }
  3329. };
  3330. //===----------------------------------------------------------------------===//
  3331. // FPToUIInst Class
  3332. //===----------------------------------------------------------------------===//
  3333. /// \brief This class represents a cast from floating point to unsigned integer
  3334. class FPToUIInst : public CastInst {
  3335. protected:
  3336. // Note: Instruction needs to be a friend here to call cloneImpl.
  3337. friend class Instruction;
  3338. /// \brief Clone an identical FPToUIInst
  3339. FPToUIInst *cloneImpl() const;
  3340. public:
  3341. /// \brief Constructor with insert-before-instruction semantics
  3342. FPToUIInst(
  3343. Value *S, ///< The value to be converted
  3344. Type *Ty, ///< The type to convert to
  3345. const Twine &NameStr = "", ///< A name for the new instruction
  3346. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3347. );
  3348. /// \brief Constructor with insert-at-end-of-block semantics
  3349. FPToUIInst(
  3350. Value *S, ///< The value to be converted
  3351. Type *Ty, ///< The type to convert to
  3352. const Twine &NameStr, ///< A name for the new instruction
  3353. BasicBlock *InsertAtEnd ///< Where to insert the new instruction
  3354. );
  3355. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3356. static inline bool classof(const Instruction *I) {
  3357. return I->getOpcode() == FPToUI;
  3358. }
  3359. static inline bool classof(const Value *V) {
  3360. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3361. }
  3362. };
  3363. //===----------------------------------------------------------------------===//
  3364. // FPToSIInst Class
  3365. //===----------------------------------------------------------------------===//
  3366. /// \brief This class represents a cast from floating point to signed integer.
  3367. class FPToSIInst : public CastInst {
  3368. protected:
  3369. // Note: Instruction needs to be a friend here to call cloneImpl.
  3370. friend class Instruction;
  3371. /// \brief Clone an identical FPToSIInst
  3372. FPToSIInst *cloneImpl() const;
  3373. public:
  3374. /// \brief Constructor with insert-before-instruction semantics
  3375. FPToSIInst(
  3376. Value *S, ///< The value to be converted
  3377. Type *Ty, ///< The type to convert to
  3378. const Twine &NameStr = "", ///< A name for the new instruction
  3379. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3380. );
  3381. /// \brief Constructor with insert-at-end-of-block semantics
  3382. FPToSIInst(
  3383. Value *S, ///< The value to be converted
  3384. Type *Ty, ///< The type to convert to
  3385. const Twine &NameStr, ///< A name for the new instruction
  3386. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3387. );
  3388. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  3389. static inline bool classof(const Instruction *I) {
  3390. return I->getOpcode() == FPToSI;
  3391. }
  3392. static inline bool classof(const Value *V) {
  3393. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3394. }
  3395. };
  3396. //===----------------------------------------------------------------------===//
  3397. // IntToPtrInst Class
  3398. //===----------------------------------------------------------------------===//
  3399. /// \brief This class represents a cast from an integer to a pointer.
  3400. class IntToPtrInst : public CastInst {
  3401. public:
  3402. /// \brief Constructor with insert-before-instruction semantics
  3403. IntToPtrInst(
  3404. Value *S, ///< The value to be converted
  3405. Type *Ty, ///< The type to convert to
  3406. const Twine &NameStr = "", ///< A name for the new instruction
  3407. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3408. );
  3409. /// \brief Constructor with insert-at-end-of-block semantics
  3410. IntToPtrInst(
  3411. Value *S, ///< The value to be converted
  3412. Type *Ty, ///< The type to convert to
  3413. const Twine &NameStr, ///< A name for the new instruction
  3414. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3415. );
  3416. // Note: Instruction needs to be a friend here to call cloneImpl.
  3417. friend class Instruction;
  3418. /// \brief Clone an identical IntToPtrInst
  3419. IntToPtrInst *cloneImpl() const;
  3420. /// \brief Returns the address space of this instruction's pointer type.
  3421. unsigned getAddressSpace() const {
  3422. return getType()->getPointerAddressSpace();
  3423. }
  3424. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3425. static inline bool classof(const Instruction *I) {
  3426. return I->getOpcode() == IntToPtr;
  3427. }
  3428. static inline bool classof(const Value *V) {
  3429. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3430. }
  3431. };
  3432. //===----------------------------------------------------------------------===//
  3433. // PtrToIntInst Class
  3434. //===----------------------------------------------------------------------===//
  3435. /// \brief This class represents a cast from a pointer to an integer
  3436. class PtrToIntInst : public CastInst {
  3437. protected:
  3438. // Note: Instruction needs to be a friend here to call cloneImpl.
  3439. friend class Instruction;
  3440. /// \brief Clone an identical PtrToIntInst
  3441. PtrToIntInst *cloneImpl() const;
  3442. public:
  3443. /// \brief Constructor with insert-before-instruction semantics
  3444. PtrToIntInst(
  3445. Value *S, ///< The value to be converted
  3446. Type *Ty, ///< The type to convert to
  3447. const Twine &NameStr = "", ///< A name for the new instruction
  3448. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3449. );
  3450. /// \brief Constructor with insert-at-end-of-block semantics
  3451. PtrToIntInst(
  3452. Value *S, ///< The value to be converted
  3453. Type *Ty, ///< The type to convert to
  3454. const Twine &NameStr, ///< A name for the new instruction
  3455. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3456. );
  3457. /// \brief Gets the pointer operand.
  3458. Value *getPointerOperand() { return getOperand(0); }
  3459. /// \brief Gets the pointer operand.
  3460. const Value *getPointerOperand() const { return getOperand(0); }
  3461. /// \brief Gets the operand index of the pointer operand.
  3462. static unsigned getPointerOperandIndex() { return 0U; }
  3463. /// \brief Returns the address space of the pointer operand.
  3464. unsigned getPointerAddressSpace() const {
  3465. return getPointerOperand()->getType()->getPointerAddressSpace();
  3466. }
  3467. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3468. static inline bool classof(const Instruction *I) {
  3469. return I->getOpcode() == PtrToInt;
  3470. }
  3471. static inline bool classof(const Value *V) {
  3472. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3473. }
  3474. };
  3475. //===----------------------------------------------------------------------===//
  3476. // BitCastInst Class
  3477. //===----------------------------------------------------------------------===//
  3478. /// \brief This class represents a no-op cast from one type to another.
  3479. class BitCastInst : public CastInst {
  3480. protected:
  3481. // Note: Instruction needs to be a friend here to call cloneImpl.
  3482. friend class Instruction;
  3483. /// \brief Clone an identical BitCastInst
  3484. BitCastInst *cloneImpl() const;
  3485. public:
  3486. /// \brief Constructor with insert-before-instruction semantics
  3487. BitCastInst(
  3488. Value *S, ///< The value to be casted
  3489. Type *Ty, ///< The type to casted to
  3490. const Twine &NameStr = "", ///< A name for the new instruction
  3491. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3492. );
  3493. /// \brief Constructor with insert-at-end-of-block semantics
  3494. BitCastInst(
  3495. Value *S, ///< The value to be casted
  3496. Type *Ty, ///< The type to casted to
  3497. const Twine &NameStr, ///< A name for the new instruction
  3498. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3499. );
  3500. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3501. static inline bool classof(const Instruction *I) {
  3502. return I->getOpcode() == BitCast;
  3503. }
  3504. static inline bool classof(const Value *V) {
  3505. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3506. }
  3507. };
  3508. //===----------------------------------------------------------------------===//
  3509. // AddrSpaceCastInst Class
  3510. // //
  3511. ///////////////////////////////////////////////////////////////////////////////
  3512. /// \brief This class represents a conversion between pointers from
  3513. /// one address space to another.
  3514. class AddrSpaceCastInst : public CastInst {
  3515. protected:
  3516. // Note: Instruction needs to be a friend here to call cloneImpl.
  3517. friend class Instruction;
  3518. /// \brief Clone an identical AddrSpaceCastInst
  3519. AddrSpaceCastInst *cloneImpl() const;
  3520. public:
  3521. /// \brief Constructor with insert-before-instruction semantics
  3522. AddrSpaceCastInst(
  3523. Value *S, ///< The value to be casted
  3524. Type *Ty, ///< The type to casted to
  3525. const Twine &NameStr = "", ///< A name for the new instruction
  3526. Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
  3527. );
  3528. /// \brief Constructor with insert-at-end-of-block semantics
  3529. AddrSpaceCastInst(
  3530. Value *S, ///< The value to be casted
  3531. Type *Ty, ///< The type to casted to
  3532. const Twine &NameStr, ///< A name for the new instruction
  3533. BasicBlock *InsertAtEnd ///< The block to insert the instruction into
  3534. );
  3535. // Methods for support type inquiry through isa, cast, and dyn_cast:
  3536. static inline bool classof(const Instruction *I) {
  3537. return I->getOpcode() == AddrSpaceCast;
  3538. }
  3539. static inline bool classof(const Value *V) {
  3540. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  3541. }
  3542. };
  3543. } // End llvm namespace
  3544. #endif