AutoUpgrade.cpp 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970
  1. //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the auto-upgrade helper functions.
  10. // This is where deprecated IR intrinsics and other IR features are updated to
  11. // current specifications.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/AutoUpgrade.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/DebugInfo.h"
  19. #include "llvm/IR/DiagnosticInfo.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/IRBuilder.h"
  22. #include "llvm/IR/InstVisitor.h"
  23. #include "llvm/IR/Instruction.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Intrinsics.h"
  26. #include "llvm/IR/IntrinsicsAArch64.h"
  27. #include "llvm/IR/IntrinsicsARM.h"
  28. #include "llvm/IR/IntrinsicsX86.h"
  29. #include "llvm/IR/LLVMContext.h"
  30. #include "llvm/IR/Module.h"
  31. #include "llvm/IR/Verifier.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/Regex.h"
  34. #include <cstring>
  35. using namespace llvm;
  36. static void rename(GlobalValue *GV) { GV->setName(GV->getName() + ".old"); }
  37. // Upgrade the declarations of the SSE4.1 ptest intrinsics whose arguments have
  38. // changed their type from v4f32 to v2i64.
  39. static bool UpgradePTESTIntrinsic(Function* F, Intrinsic::ID IID,
  40. Function *&NewFn) {
  41. // Check whether this is an old version of the function, which received
  42. // v4f32 arguments.
  43. Type *Arg0Type = F->getFunctionType()->getParamType(0);
  44. if (Arg0Type != FixedVectorType::get(Type::getFloatTy(F->getContext()), 4))
  45. return false;
  46. // Yes, it's old, replace it with new version.
  47. rename(F);
  48. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  49. return true;
  50. }
  51. // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
  52. // arguments have changed their type from i32 to i8.
  53. static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
  54. Function *&NewFn) {
  55. // Check that the last argument is an i32.
  56. Type *LastArgType = F->getFunctionType()->getParamType(
  57. F->getFunctionType()->getNumParams() - 1);
  58. if (!LastArgType->isIntegerTy(32))
  59. return false;
  60. // Move this function aside and map down.
  61. rename(F);
  62. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  63. return true;
  64. }
  65. // Upgrade the declaration of fp compare intrinsics that change return type
  66. // from scalar to vXi1 mask.
  67. static bool UpgradeX86MaskedFPCompare(Function *F, Intrinsic::ID IID,
  68. Function *&NewFn) {
  69. // Check if the return type is a vector.
  70. if (F->getReturnType()->isVectorTy())
  71. return false;
  72. rename(F);
  73. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  74. return true;
  75. }
  76. static bool UpgradeX86BF16Intrinsic(Function *F, Intrinsic::ID IID,
  77. Function *&NewFn) {
  78. if (F->getReturnType()->getScalarType()->isBFloatTy())
  79. return false;
  80. rename(F);
  81. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  82. return true;
  83. }
  84. static bool UpgradeX86BF16DPIntrinsic(Function *F, Intrinsic::ID IID,
  85. Function *&NewFn) {
  86. if (F->getFunctionType()->getParamType(1)->getScalarType()->isBFloatTy())
  87. return false;
  88. rename(F);
  89. NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
  90. return true;
  91. }
  92. static bool ShouldUpgradeX86Intrinsic(Function *F, StringRef Name) {
  93. // All of the intrinsics matches below should be marked with which llvm
  94. // version started autoupgrading them. At some point in the future we would
  95. // like to use this information to remove upgrade code for some older
  96. // intrinsics. It is currently undecided how we will determine that future
  97. // point.
  98. if (Name == "addcarryx.u32" || // Added in 8.0
  99. Name == "addcarryx.u64" || // Added in 8.0
  100. Name == "addcarry.u32" || // Added in 8.0
  101. Name == "addcarry.u64" || // Added in 8.0
  102. Name == "subborrow.u32" || // Added in 8.0
  103. Name == "subborrow.u64" || // Added in 8.0
  104. Name.startswith("sse2.padds.") || // Added in 8.0
  105. Name.startswith("sse2.psubs.") || // Added in 8.0
  106. Name.startswith("sse2.paddus.") || // Added in 8.0
  107. Name.startswith("sse2.psubus.") || // Added in 8.0
  108. Name.startswith("avx2.padds.") || // Added in 8.0
  109. Name.startswith("avx2.psubs.") || // Added in 8.0
  110. Name.startswith("avx2.paddus.") || // Added in 8.0
  111. Name.startswith("avx2.psubus.") || // Added in 8.0
  112. Name.startswith("avx512.padds.") || // Added in 8.0
  113. Name.startswith("avx512.psubs.") || // Added in 8.0
  114. Name.startswith("avx512.mask.padds.") || // Added in 8.0
  115. Name.startswith("avx512.mask.psubs.") || // Added in 8.0
  116. Name.startswith("avx512.mask.paddus.") || // Added in 8.0
  117. Name.startswith("avx512.mask.psubus.") || // Added in 8.0
  118. Name=="ssse3.pabs.b.128" || // Added in 6.0
  119. Name=="ssse3.pabs.w.128" || // Added in 6.0
  120. Name=="ssse3.pabs.d.128" || // Added in 6.0
  121. Name.startswith("fma4.vfmadd.s") || // Added in 7.0
  122. Name.startswith("fma.vfmadd.") || // Added in 7.0
  123. Name.startswith("fma.vfmsub.") || // Added in 7.0
  124. Name.startswith("fma.vfmsubadd.") || // Added in 7.0
  125. Name.startswith("fma.vfnmadd.") || // Added in 7.0
  126. Name.startswith("fma.vfnmsub.") || // Added in 7.0
  127. Name.startswith("avx512.mask.vfmadd.") || // Added in 7.0
  128. Name.startswith("avx512.mask.vfnmadd.") || // Added in 7.0
  129. Name.startswith("avx512.mask.vfnmsub.") || // Added in 7.0
  130. Name.startswith("avx512.mask3.vfmadd.") || // Added in 7.0
  131. Name.startswith("avx512.maskz.vfmadd.") || // Added in 7.0
  132. Name.startswith("avx512.mask3.vfmsub.") || // Added in 7.0
  133. Name.startswith("avx512.mask3.vfnmsub.") || // Added in 7.0
  134. Name.startswith("avx512.mask.vfmaddsub.") || // Added in 7.0
  135. Name.startswith("avx512.maskz.vfmaddsub.") || // Added in 7.0
  136. Name.startswith("avx512.mask3.vfmaddsub.") || // Added in 7.0
  137. Name.startswith("avx512.mask3.vfmsubadd.") || // Added in 7.0
  138. Name.startswith("avx512.mask.shuf.i") || // Added in 6.0
  139. Name.startswith("avx512.mask.shuf.f") || // Added in 6.0
  140. Name.startswith("avx512.kunpck") || //added in 6.0
  141. Name.startswith("avx2.pabs.") || // Added in 6.0
  142. Name.startswith("avx512.mask.pabs.") || // Added in 6.0
  143. Name.startswith("avx512.broadcastm") || // Added in 6.0
  144. Name == "sse.sqrt.ss" || // Added in 7.0
  145. Name == "sse2.sqrt.sd" || // Added in 7.0
  146. Name.startswith("avx512.mask.sqrt.p") || // Added in 7.0
  147. Name.startswith("avx.sqrt.p") || // Added in 7.0
  148. Name.startswith("sse2.sqrt.p") || // Added in 7.0
  149. Name.startswith("sse.sqrt.p") || // Added in 7.0
  150. Name.startswith("avx512.mask.pbroadcast") || // Added in 6.0
  151. Name.startswith("sse2.pcmpeq.") || // Added in 3.1
  152. Name.startswith("sse2.pcmpgt.") || // Added in 3.1
  153. Name.startswith("avx2.pcmpeq.") || // Added in 3.1
  154. Name.startswith("avx2.pcmpgt.") || // Added in 3.1
  155. Name.startswith("avx512.mask.pcmpeq.") || // Added in 3.9
  156. Name.startswith("avx512.mask.pcmpgt.") || // Added in 3.9
  157. Name.startswith("avx.vperm2f128.") || // Added in 6.0
  158. Name == "avx2.vperm2i128" || // Added in 6.0
  159. Name == "sse.add.ss" || // Added in 4.0
  160. Name == "sse2.add.sd" || // Added in 4.0
  161. Name == "sse.sub.ss" || // Added in 4.0
  162. Name == "sse2.sub.sd" || // Added in 4.0
  163. Name == "sse.mul.ss" || // Added in 4.0
  164. Name == "sse2.mul.sd" || // Added in 4.0
  165. Name == "sse.div.ss" || // Added in 4.0
  166. Name == "sse2.div.sd" || // Added in 4.0
  167. Name == "sse41.pmaxsb" || // Added in 3.9
  168. Name == "sse2.pmaxs.w" || // Added in 3.9
  169. Name == "sse41.pmaxsd" || // Added in 3.9
  170. Name == "sse2.pmaxu.b" || // Added in 3.9
  171. Name == "sse41.pmaxuw" || // Added in 3.9
  172. Name == "sse41.pmaxud" || // Added in 3.9
  173. Name == "sse41.pminsb" || // Added in 3.9
  174. Name == "sse2.pmins.w" || // Added in 3.9
  175. Name == "sse41.pminsd" || // Added in 3.9
  176. Name == "sse2.pminu.b" || // Added in 3.9
  177. Name == "sse41.pminuw" || // Added in 3.9
  178. Name == "sse41.pminud" || // Added in 3.9
  179. Name == "avx512.kand.w" || // Added in 7.0
  180. Name == "avx512.kandn.w" || // Added in 7.0
  181. Name == "avx512.knot.w" || // Added in 7.0
  182. Name == "avx512.kor.w" || // Added in 7.0
  183. Name == "avx512.kxor.w" || // Added in 7.0
  184. Name == "avx512.kxnor.w" || // Added in 7.0
  185. Name == "avx512.kortestc.w" || // Added in 7.0
  186. Name == "avx512.kortestz.w" || // Added in 7.0
  187. Name.startswith("avx512.mask.pshuf.b.") || // Added in 4.0
  188. Name.startswith("avx2.pmax") || // Added in 3.9
  189. Name.startswith("avx2.pmin") || // Added in 3.9
  190. Name.startswith("avx512.mask.pmax") || // Added in 4.0
  191. Name.startswith("avx512.mask.pmin") || // Added in 4.0
  192. Name.startswith("avx2.vbroadcast") || // Added in 3.8
  193. Name.startswith("avx2.pbroadcast") || // Added in 3.8
  194. Name.startswith("avx.vpermil.") || // Added in 3.1
  195. Name.startswith("sse2.pshuf") || // Added in 3.9
  196. Name.startswith("avx512.pbroadcast") || // Added in 3.9
  197. Name.startswith("avx512.mask.broadcast.s") || // Added in 3.9
  198. Name.startswith("avx512.mask.movddup") || // Added in 3.9
  199. Name.startswith("avx512.mask.movshdup") || // Added in 3.9
  200. Name.startswith("avx512.mask.movsldup") || // Added in 3.9
  201. Name.startswith("avx512.mask.pshuf.d.") || // Added in 3.9
  202. Name.startswith("avx512.mask.pshufl.w.") || // Added in 3.9
  203. Name.startswith("avx512.mask.pshufh.w.") || // Added in 3.9
  204. Name.startswith("avx512.mask.shuf.p") || // Added in 4.0
  205. Name.startswith("avx512.mask.vpermil.p") || // Added in 3.9
  206. Name.startswith("avx512.mask.perm.df.") || // Added in 3.9
  207. Name.startswith("avx512.mask.perm.di.") || // Added in 3.9
  208. Name.startswith("avx512.mask.punpckl") || // Added in 3.9
  209. Name.startswith("avx512.mask.punpckh") || // Added in 3.9
  210. Name.startswith("avx512.mask.unpckl.") || // Added in 3.9
  211. Name.startswith("avx512.mask.unpckh.") || // Added in 3.9
  212. Name.startswith("avx512.mask.pand.") || // Added in 3.9
  213. Name.startswith("avx512.mask.pandn.") || // Added in 3.9
  214. Name.startswith("avx512.mask.por.") || // Added in 3.9
  215. Name.startswith("avx512.mask.pxor.") || // Added in 3.9
  216. Name.startswith("avx512.mask.and.") || // Added in 3.9
  217. Name.startswith("avx512.mask.andn.") || // Added in 3.9
  218. Name.startswith("avx512.mask.or.") || // Added in 3.9
  219. Name.startswith("avx512.mask.xor.") || // Added in 3.9
  220. Name.startswith("avx512.mask.padd.") || // Added in 4.0
  221. Name.startswith("avx512.mask.psub.") || // Added in 4.0
  222. Name.startswith("avx512.mask.pmull.") || // Added in 4.0
  223. Name.startswith("avx512.mask.cvtdq2pd.") || // Added in 4.0
  224. Name.startswith("avx512.mask.cvtudq2pd.") || // Added in 4.0
  225. Name.startswith("avx512.mask.cvtudq2ps.") || // Added in 7.0 updated 9.0
  226. Name.startswith("avx512.mask.cvtqq2pd.") || // Added in 7.0 updated 9.0
  227. Name.startswith("avx512.mask.cvtuqq2pd.") || // Added in 7.0 updated 9.0
  228. Name.startswith("avx512.mask.cvtdq2ps.") || // Added in 7.0 updated 9.0
  229. Name == "avx512.mask.vcvtph2ps.128" || // Added in 11.0
  230. Name == "avx512.mask.vcvtph2ps.256" || // Added in 11.0
  231. Name == "avx512.mask.cvtqq2ps.256" || // Added in 9.0
  232. Name == "avx512.mask.cvtqq2ps.512" || // Added in 9.0
  233. Name == "avx512.mask.cvtuqq2ps.256" || // Added in 9.0
  234. Name == "avx512.mask.cvtuqq2ps.512" || // Added in 9.0
  235. Name == "avx512.mask.cvtpd2dq.256" || // Added in 7.0
  236. Name == "avx512.mask.cvtpd2ps.256" || // Added in 7.0
  237. Name == "avx512.mask.cvttpd2dq.256" || // Added in 7.0
  238. Name == "avx512.mask.cvttps2dq.128" || // Added in 7.0
  239. Name == "avx512.mask.cvttps2dq.256" || // Added in 7.0
  240. Name == "avx512.mask.cvtps2pd.128" || // Added in 7.0
  241. Name == "avx512.mask.cvtps2pd.256" || // Added in 7.0
  242. Name == "avx512.cvtusi2sd" || // Added in 7.0
  243. Name.startswith("avx512.mask.permvar.") || // Added in 7.0
  244. Name == "sse2.pmulu.dq" || // Added in 7.0
  245. Name == "sse41.pmuldq" || // Added in 7.0
  246. Name == "avx2.pmulu.dq" || // Added in 7.0
  247. Name == "avx2.pmul.dq" || // Added in 7.0
  248. Name == "avx512.pmulu.dq.512" || // Added in 7.0
  249. Name == "avx512.pmul.dq.512" || // Added in 7.0
  250. Name.startswith("avx512.mask.pmul.dq.") || // Added in 4.0
  251. Name.startswith("avx512.mask.pmulu.dq.") || // Added in 4.0
  252. Name.startswith("avx512.mask.pmul.hr.sw.") || // Added in 7.0
  253. Name.startswith("avx512.mask.pmulh.w.") || // Added in 7.0
  254. Name.startswith("avx512.mask.pmulhu.w.") || // Added in 7.0
  255. Name.startswith("avx512.mask.pmaddw.d.") || // Added in 7.0
  256. Name.startswith("avx512.mask.pmaddubs.w.") || // Added in 7.0
  257. Name.startswith("avx512.mask.packsswb.") || // Added in 5.0
  258. Name.startswith("avx512.mask.packssdw.") || // Added in 5.0
  259. Name.startswith("avx512.mask.packuswb.") || // Added in 5.0
  260. Name.startswith("avx512.mask.packusdw.") || // Added in 5.0
  261. Name.startswith("avx512.mask.cmp.b") || // Added in 5.0
  262. Name.startswith("avx512.mask.cmp.d") || // Added in 5.0
  263. Name.startswith("avx512.mask.cmp.q") || // Added in 5.0
  264. Name.startswith("avx512.mask.cmp.w") || // Added in 5.0
  265. Name.startswith("avx512.cmp.p") || // Added in 12.0
  266. Name.startswith("avx512.mask.ucmp.") || // Added in 5.0
  267. Name.startswith("avx512.cvtb2mask.") || // Added in 7.0
  268. Name.startswith("avx512.cvtw2mask.") || // Added in 7.0
  269. Name.startswith("avx512.cvtd2mask.") || // Added in 7.0
  270. Name.startswith("avx512.cvtq2mask.") || // Added in 7.0
  271. Name.startswith("avx512.mask.vpermilvar.") || // Added in 4.0
  272. Name.startswith("avx512.mask.psll.d") || // Added in 4.0
  273. Name.startswith("avx512.mask.psll.q") || // Added in 4.0
  274. Name.startswith("avx512.mask.psll.w") || // Added in 4.0
  275. Name.startswith("avx512.mask.psra.d") || // Added in 4.0
  276. Name.startswith("avx512.mask.psra.q") || // Added in 4.0
  277. Name.startswith("avx512.mask.psra.w") || // Added in 4.0
  278. Name.startswith("avx512.mask.psrl.d") || // Added in 4.0
  279. Name.startswith("avx512.mask.psrl.q") || // Added in 4.0
  280. Name.startswith("avx512.mask.psrl.w") || // Added in 4.0
  281. Name.startswith("avx512.mask.pslli") || // Added in 4.0
  282. Name.startswith("avx512.mask.psrai") || // Added in 4.0
  283. Name.startswith("avx512.mask.psrli") || // Added in 4.0
  284. Name.startswith("avx512.mask.psllv") || // Added in 4.0
  285. Name.startswith("avx512.mask.psrav") || // Added in 4.0
  286. Name.startswith("avx512.mask.psrlv") || // Added in 4.0
  287. Name.startswith("sse41.pmovsx") || // Added in 3.8
  288. Name.startswith("sse41.pmovzx") || // Added in 3.9
  289. Name.startswith("avx2.pmovsx") || // Added in 3.9
  290. Name.startswith("avx2.pmovzx") || // Added in 3.9
  291. Name.startswith("avx512.mask.pmovsx") || // Added in 4.0
  292. Name.startswith("avx512.mask.pmovzx") || // Added in 4.0
  293. Name.startswith("avx512.mask.lzcnt.") || // Added in 5.0
  294. Name.startswith("avx512.mask.pternlog.") || // Added in 7.0
  295. Name.startswith("avx512.maskz.pternlog.") || // Added in 7.0
  296. Name.startswith("avx512.mask.vpmadd52") || // Added in 7.0
  297. Name.startswith("avx512.maskz.vpmadd52") || // Added in 7.0
  298. Name.startswith("avx512.mask.vpermi2var.") || // Added in 7.0
  299. Name.startswith("avx512.mask.vpermt2var.") || // Added in 7.0
  300. Name.startswith("avx512.maskz.vpermt2var.") || // Added in 7.0
  301. Name.startswith("avx512.mask.vpdpbusd.") || // Added in 7.0
  302. Name.startswith("avx512.maskz.vpdpbusd.") || // Added in 7.0
  303. Name.startswith("avx512.mask.vpdpbusds.") || // Added in 7.0
  304. Name.startswith("avx512.maskz.vpdpbusds.") || // Added in 7.0
  305. Name.startswith("avx512.mask.vpdpwssd.") || // Added in 7.0
  306. Name.startswith("avx512.maskz.vpdpwssd.") || // Added in 7.0
  307. Name.startswith("avx512.mask.vpdpwssds.") || // Added in 7.0
  308. Name.startswith("avx512.maskz.vpdpwssds.") || // Added in 7.0
  309. Name.startswith("avx512.mask.dbpsadbw.") || // Added in 7.0
  310. Name.startswith("avx512.mask.vpshld.") || // Added in 7.0
  311. Name.startswith("avx512.mask.vpshrd.") || // Added in 7.0
  312. Name.startswith("avx512.mask.vpshldv.") || // Added in 8.0
  313. Name.startswith("avx512.mask.vpshrdv.") || // Added in 8.0
  314. Name.startswith("avx512.maskz.vpshldv.") || // Added in 8.0
  315. Name.startswith("avx512.maskz.vpshrdv.") || // Added in 8.0
  316. Name.startswith("avx512.vpshld.") || // Added in 8.0
  317. Name.startswith("avx512.vpshrd.") || // Added in 8.0
  318. Name.startswith("avx512.mask.add.p") || // Added in 7.0. 128/256 in 4.0
  319. Name.startswith("avx512.mask.sub.p") || // Added in 7.0. 128/256 in 4.0
  320. Name.startswith("avx512.mask.mul.p") || // Added in 7.0. 128/256 in 4.0
  321. Name.startswith("avx512.mask.div.p") || // Added in 7.0. 128/256 in 4.0
  322. Name.startswith("avx512.mask.max.p") || // Added in 7.0. 128/256 in 5.0
  323. Name.startswith("avx512.mask.min.p") || // Added in 7.0. 128/256 in 5.0
  324. Name.startswith("avx512.mask.fpclass.p") || // Added in 7.0
  325. Name.startswith("avx512.mask.vpshufbitqmb.") || // Added in 8.0
  326. Name.startswith("avx512.mask.pmultishift.qb.") || // Added in 8.0
  327. Name.startswith("avx512.mask.conflict.") || // Added in 9.0
  328. Name == "avx512.mask.pmov.qd.256" || // Added in 9.0
  329. Name == "avx512.mask.pmov.qd.512" || // Added in 9.0
  330. Name == "avx512.mask.pmov.wb.256" || // Added in 9.0
  331. Name == "avx512.mask.pmov.wb.512" || // Added in 9.0
  332. Name == "sse.cvtsi2ss" || // Added in 7.0
  333. Name == "sse.cvtsi642ss" || // Added in 7.0
  334. Name == "sse2.cvtsi2sd" || // Added in 7.0
  335. Name == "sse2.cvtsi642sd" || // Added in 7.0
  336. Name == "sse2.cvtss2sd" || // Added in 7.0
  337. Name == "sse2.cvtdq2pd" || // Added in 3.9
  338. Name == "sse2.cvtdq2ps" || // Added in 7.0
  339. Name == "sse2.cvtps2pd" || // Added in 3.9
  340. Name == "avx.cvtdq2.pd.256" || // Added in 3.9
  341. Name == "avx.cvtdq2.ps.256" || // Added in 7.0
  342. Name == "avx.cvt.ps2.pd.256" || // Added in 3.9
  343. Name.startswith("vcvtph2ps.") || // Added in 11.0
  344. Name.startswith("avx.vinsertf128.") || // Added in 3.7
  345. Name == "avx2.vinserti128" || // Added in 3.7
  346. Name.startswith("avx512.mask.insert") || // Added in 4.0
  347. Name.startswith("avx.vextractf128.") || // Added in 3.7
  348. Name == "avx2.vextracti128" || // Added in 3.7
  349. Name.startswith("avx512.mask.vextract") || // Added in 4.0
  350. Name.startswith("sse4a.movnt.") || // Added in 3.9
  351. Name.startswith("avx.movnt.") || // Added in 3.2
  352. Name.startswith("avx512.storent.") || // Added in 3.9
  353. Name == "sse41.movntdqa" || // Added in 5.0
  354. Name == "avx2.movntdqa" || // Added in 5.0
  355. Name == "avx512.movntdqa" || // Added in 5.0
  356. Name == "sse2.storel.dq" || // Added in 3.9
  357. Name.startswith("sse.storeu.") || // Added in 3.9
  358. Name.startswith("sse2.storeu.") || // Added in 3.9
  359. Name.startswith("avx.storeu.") || // Added in 3.9
  360. Name.startswith("avx512.mask.storeu.") || // Added in 3.9
  361. Name.startswith("avx512.mask.store.p") || // Added in 3.9
  362. Name.startswith("avx512.mask.store.b.") || // Added in 3.9
  363. Name.startswith("avx512.mask.store.w.") || // Added in 3.9
  364. Name.startswith("avx512.mask.store.d.") || // Added in 3.9
  365. Name.startswith("avx512.mask.store.q.") || // Added in 3.9
  366. Name == "avx512.mask.store.ss" || // Added in 7.0
  367. Name.startswith("avx512.mask.loadu.") || // Added in 3.9
  368. Name.startswith("avx512.mask.load.") || // Added in 3.9
  369. Name.startswith("avx512.mask.expand.load.") || // Added in 7.0
  370. Name.startswith("avx512.mask.compress.store.") || // Added in 7.0
  371. Name.startswith("avx512.mask.expand.b") || // Added in 9.0
  372. Name.startswith("avx512.mask.expand.w") || // Added in 9.0
  373. Name.startswith("avx512.mask.expand.d") || // Added in 9.0
  374. Name.startswith("avx512.mask.expand.q") || // Added in 9.0
  375. Name.startswith("avx512.mask.expand.p") || // Added in 9.0
  376. Name.startswith("avx512.mask.compress.b") || // Added in 9.0
  377. Name.startswith("avx512.mask.compress.w") || // Added in 9.0
  378. Name.startswith("avx512.mask.compress.d") || // Added in 9.0
  379. Name.startswith("avx512.mask.compress.q") || // Added in 9.0
  380. Name.startswith("avx512.mask.compress.p") || // Added in 9.0
  381. Name == "sse42.crc32.64.8" || // Added in 3.4
  382. Name.startswith("avx.vbroadcast.s") || // Added in 3.5
  383. Name.startswith("avx512.vbroadcast.s") || // Added in 7.0
  384. Name.startswith("avx512.mask.palignr.") || // Added in 3.9
  385. Name.startswith("avx512.mask.valign.") || // Added in 4.0
  386. Name.startswith("sse2.psll.dq") || // Added in 3.7
  387. Name.startswith("sse2.psrl.dq") || // Added in 3.7
  388. Name.startswith("avx2.psll.dq") || // Added in 3.7
  389. Name.startswith("avx2.psrl.dq") || // Added in 3.7
  390. Name.startswith("avx512.psll.dq") || // Added in 3.9
  391. Name.startswith("avx512.psrl.dq") || // Added in 3.9
  392. Name == "sse41.pblendw" || // Added in 3.7
  393. Name.startswith("sse41.blendp") || // Added in 3.7
  394. Name.startswith("avx.blend.p") || // Added in 3.7
  395. Name == "avx2.pblendw" || // Added in 3.7
  396. Name.startswith("avx2.pblendd.") || // Added in 3.7
  397. Name.startswith("avx.vbroadcastf128") || // Added in 4.0
  398. Name == "avx2.vbroadcasti128" || // Added in 3.7
  399. Name.startswith("avx512.mask.broadcastf32x4.") || // Added in 6.0
  400. Name.startswith("avx512.mask.broadcastf64x2.") || // Added in 6.0
  401. Name.startswith("avx512.mask.broadcastf32x8.") || // Added in 6.0
  402. Name.startswith("avx512.mask.broadcastf64x4.") || // Added in 6.0
  403. Name.startswith("avx512.mask.broadcasti32x4.") || // Added in 6.0
  404. Name.startswith("avx512.mask.broadcasti64x2.") || // Added in 6.0
  405. Name.startswith("avx512.mask.broadcasti32x8.") || // Added in 6.0
  406. Name.startswith("avx512.mask.broadcasti64x4.") || // Added in 6.0
  407. Name == "xop.vpcmov" || // Added in 3.8
  408. Name == "xop.vpcmov.256" || // Added in 5.0
  409. Name.startswith("avx512.mask.move.s") || // Added in 4.0
  410. Name.startswith("avx512.cvtmask2") || // Added in 5.0
  411. Name.startswith("xop.vpcom") || // Added in 3.2, Updated in 9.0
  412. Name.startswith("xop.vprot") || // Added in 8.0
  413. Name.startswith("avx512.prol") || // Added in 8.0
  414. Name.startswith("avx512.pror") || // Added in 8.0
  415. Name.startswith("avx512.mask.prorv.") || // Added in 8.0
  416. Name.startswith("avx512.mask.pror.") || // Added in 8.0
  417. Name.startswith("avx512.mask.prolv.") || // Added in 8.0
  418. Name.startswith("avx512.mask.prol.") || // Added in 8.0
  419. Name.startswith("avx512.ptestm") || //Added in 6.0
  420. Name.startswith("avx512.ptestnm") || //Added in 6.0
  421. Name.startswith("avx512.mask.pavg")) // Added in 6.0
  422. return true;
  423. return false;
  424. }
  425. static bool UpgradeX86IntrinsicFunction(Function *F, StringRef Name,
  426. Function *&NewFn) {
  427. // Only handle intrinsics that start with "x86.".
  428. if (!Name.startswith("x86."))
  429. return false;
  430. // Remove "x86." prefix.
  431. Name = Name.substr(4);
  432. if (ShouldUpgradeX86Intrinsic(F, Name)) {
  433. NewFn = nullptr;
  434. return true;
  435. }
  436. if (Name == "rdtscp") { // Added in 8.0
  437. // If this intrinsic has 0 operands, it's the new version.
  438. if (F->getFunctionType()->getNumParams() == 0)
  439. return false;
  440. rename(F);
  441. NewFn = Intrinsic::getDeclaration(F->getParent(),
  442. Intrinsic::x86_rdtscp);
  443. return true;
  444. }
  445. // SSE4.1 ptest functions may have an old signature.
  446. if (Name.startswith("sse41.ptest")) { // Added in 3.2
  447. if (Name.substr(11) == "c")
  448. return UpgradePTESTIntrinsic(F, Intrinsic::x86_sse41_ptestc, NewFn);
  449. if (Name.substr(11) == "z")
  450. return UpgradePTESTIntrinsic(F, Intrinsic::x86_sse41_ptestz, NewFn);
  451. if (Name.substr(11) == "nzc")
  452. return UpgradePTESTIntrinsic(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
  453. }
  454. // Several blend and other instructions with masks used the wrong number of
  455. // bits.
  456. if (Name == "sse41.insertps") // Added in 3.6
  457. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
  458. NewFn);
  459. if (Name == "sse41.dppd") // Added in 3.6
  460. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
  461. NewFn);
  462. if (Name == "sse41.dpps") // Added in 3.6
  463. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
  464. NewFn);
  465. if (Name == "sse41.mpsadbw") // Added in 3.6
  466. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
  467. NewFn);
  468. if (Name == "avx.dp.ps.256") // Added in 3.6
  469. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
  470. NewFn);
  471. if (Name == "avx2.mpsadbw") // Added in 3.6
  472. return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
  473. NewFn);
  474. if (Name == "avx512.mask.cmp.pd.128") // Added in 7.0
  475. return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_pd_128,
  476. NewFn);
  477. if (Name == "avx512.mask.cmp.pd.256") // Added in 7.0
  478. return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_pd_256,
  479. NewFn);
  480. if (Name == "avx512.mask.cmp.pd.512") // Added in 7.0
  481. return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_pd_512,
  482. NewFn);
  483. if (Name == "avx512.mask.cmp.ps.128") // Added in 7.0
  484. return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_ps_128,
  485. NewFn);
  486. if (Name == "avx512.mask.cmp.ps.256") // Added in 7.0
  487. return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_ps_256,
  488. NewFn);
  489. if (Name == "avx512.mask.cmp.ps.512") // Added in 7.0
  490. return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_ps_512,
  491. NewFn);
  492. if (Name == "avx512bf16.cvtne2ps2bf16.128") // Added in 9.0
  493. return UpgradeX86BF16Intrinsic(
  494. F, Intrinsic::x86_avx512bf16_cvtne2ps2bf16_128, NewFn);
  495. if (Name == "avx512bf16.cvtne2ps2bf16.256") // Added in 9.0
  496. return UpgradeX86BF16Intrinsic(
  497. F, Intrinsic::x86_avx512bf16_cvtne2ps2bf16_256, NewFn);
  498. if (Name == "avx512bf16.cvtne2ps2bf16.512") // Added in 9.0
  499. return UpgradeX86BF16Intrinsic(
  500. F, Intrinsic::x86_avx512bf16_cvtne2ps2bf16_512, NewFn);
  501. if (Name == "avx512bf16.mask.cvtneps2bf16.128") // Added in 9.0
  502. return UpgradeX86BF16Intrinsic(
  503. F, Intrinsic::x86_avx512bf16_mask_cvtneps2bf16_128, NewFn);
  504. if (Name == "avx512bf16.cvtneps2bf16.256") // Added in 9.0
  505. return UpgradeX86BF16Intrinsic(
  506. F, Intrinsic::x86_avx512bf16_cvtneps2bf16_256, NewFn);
  507. if (Name == "avx512bf16.cvtneps2bf16.512") // Added in 9.0
  508. return UpgradeX86BF16Intrinsic(
  509. F, Intrinsic::x86_avx512bf16_cvtneps2bf16_512, NewFn);
  510. if (Name == "avx512bf16.dpbf16ps.128") // Added in 9.0
  511. return UpgradeX86BF16DPIntrinsic(
  512. F, Intrinsic::x86_avx512bf16_dpbf16ps_128, NewFn);
  513. if (Name == "avx512bf16.dpbf16ps.256") // Added in 9.0
  514. return UpgradeX86BF16DPIntrinsic(
  515. F, Intrinsic::x86_avx512bf16_dpbf16ps_256, NewFn);
  516. if (Name == "avx512bf16.dpbf16ps.512") // Added in 9.0
  517. return UpgradeX86BF16DPIntrinsic(
  518. F, Intrinsic::x86_avx512bf16_dpbf16ps_512, NewFn);
  519. // frcz.ss/sd may need to have an argument dropped. Added in 3.2
  520. if (Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
  521. rename(F);
  522. NewFn = Intrinsic::getDeclaration(F->getParent(),
  523. Intrinsic::x86_xop_vfrcz_ss);
  524. return true;
  525. }
  526. if (Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
  527. rename(F);
  528. NewFn = Intrinsic::getDeclaration(F->getParent(),
  529. Intrinsic::x86_xop_vfrcz_sd);
  530. return true;
  531. }
  532. // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
  533. if (Name.startswith("xop.vpermil2")) { // Added in 3.9
  534. auto Idx = F->getFunctionType()->getParamType(2);
  535. if (Idx->isFPOrFPVectorTy()) {
  536. rename(F);
  537. unsigned IdxSize = Idx->getPrimitiveSizeInBits();
  538. unsigned EltSize = Idx->getScalarSizeInBits();
  539. Intrinsic::ID Permil2ID;
  540. if (EltSize == 64 && IdxSize == 128)
  541. Permil2ID = Intrinsic::x86_xop_vpermil2pd;
  542. else if (EltSize == 32 && IdxSize == 128)
  543. Permil2ID = Intrinsic::x86_xop_vpermil2ps;
  544. else if (EltSize == 64 && IdxSize == 256)
  545. Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
  546. else
  547. Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
  548. NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
  549. return true;
  550. }
  551. }
  552. if (Name == "seh.recoverfp") {
  553. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_recoverfp);
  554. return true;
  555. }
  556. return false;
  557. }
  558. static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
  559. assert(F && "Illegal to upgrade a non-existent Function.");
  560. // Quickly eliminate it, if it's not a candidate.
  561. StringRef Name = F->getName();
  562. if (Name.size() <= 7 || !Name.startswith("llvm."))
  563. return false;
  564. Name = Name.substr(5); // Strip off "llvm."
  565. switch (Name[0]) {
  566. default: break;
  567. case 'a': {
  568. if (Name.startswith("arm.rbit") || Name.startswith("aarch64.rbit")) {
  569. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::bitreverse,
  570. F->arg_begin()->getType());
  571. return true;
  572. }
  573. if (Name.startswith("aarch64.neon.frintn")) {
  574. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::roundeven,
  575. F->arg_begin()->getType());
  576. return true;
  577. }
  578. if (Name.startswith("aarch64.neon.rbit")) {
  579. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::bitreverse,
  580. F->arg_begin()->getType());
  581. return true;
  582. }
  583. if (Name == "aarch64.sve.bfdot.lane") {
  584. NewFn = Intrinsic::getDeclaration(F->getParent(),
  585. Intrinsic::aarch64_sve_bfdot_lane_v2);
  586. return true;
  587. }
  588. if (Name == "aarch64.sve.bfmlalb.lane") {
  589. NewFn = Intrinsic::getDeclaration(F->getParent(),
  590. Intrinsic::aarch64_sve_bfmlalb_lane_v2);
  591. return true;
  592. }
  593. if (Name == "aarch64.sve.bfmlalt.lane") {
  594. NewFn = Intrinsic::getDeclaration(F->getParent(),
  595. Intrinsic::aarch64_sve_bfmlalt_lane_v2);
  596. return true;
  597. }
  598. static const Regex LdRegex("^aarch64\\.sve\\.ld[234](.nxv[a-z0-9]+|$)");
  599. if (LdRegex.match(Name)) {
  600. Type *ScalarTy =
  601. dyn_cast<VectorType>(F->getReturnType())->getElementType();
  602. ElementCount EC =
  603. dyn_cast<VectorType>(F->arg_begin()->getType())->getElementCount();
  604. Type *Ty = VectorType::get(ScalarTy, EC);
  605. Intrinsic::ID ID =
  606. StringSwitch<Intrinsic::ID>(Name)
  607. .StartsWith("aarch64.sve.ld2", Intrinsic::aarch64_sve_ld2_sret)
  608. .StartsWith("aarch64.sve.ld3", Intrinsic::aarch64_sve_ld3_sret)
  609. .StartsWith("aarch64.sve.ld4", Intrinsic::aarch64_sve_ld4_sret)
  610. .Default(Intrinsic::not_intrinsic);
  611. NewFn = Intrinsic::getDeclaration(F->getParent(), ID, Ty);
  612. return true;
  613. }
  614. if (Name.startswith("aarch64.sve.tuple.get")) {
  615. Type *Tys[] = {F->getReturnType(), F->arg_begin()->getType()};
  616. NewFn = Intrinsic::getDeclaration(F->getParent(),
  617. Intrinsic::vector_extract, Tys);
  618. return true;
  619. }
  620. if (Name.startswith("aarch64.sve.tuple.set")) {
  621. auto Args = F->getFunctionType()->params();
  622. Type *Tys[] = {Args[0], Args[2], Args[1]};
  623. NewFn = Intrinsic::getDeclaration(F->getParent(),
  624. Intrinsic::vector_insert, Tys);
  625. return true;
  626. }
  627. static const Regex CreateTupleRegex(
  628. "^aarch64\\.sve\\.tuple\\.create[234](.nxv[a-z0-9]+|$)");
  629. if (CreateTupleRegex.match(Name)) {
  630. auto Args = F->getFunctionType()->params();
  631. Type *Tys[] = {F->getReturnType(), Args[1]};
  632. NewFn = Intrinsic::getDeclaration(F->getParent(),
  633. Intrinsic::vector_insert, Tys);
  634. return true;
  635. }
  636. if (Name.startswith("arm.neon.vclz")) {
  637. Type* args[2] = {
  638. F->arg_begin()->getType(),
  639. Type::getInt1Ty(F->getContext())
  640. };
  641. // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
  642. // the end of the name. Change name from llvm.arm.neon.vclz.* to
  643. // llvm.ctlz.*
  644. FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
  645. NewFn = Function::Create(fType, F->getLinkage(), F->getAddressSpace(),
  646. "llvm.ctlz." + Name.substr(14), F->getParent());
  647. return true;
  648. }
  649. if (Name.startswith("arm.neon.vcnt")) {
  650. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
  651. F->arg_begin()->getType());
  652. return true;
  653. }
  654. static const Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$");
  655. if (vstRegex.match(Name)) {
  656. static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1,
  657. Intrinsic::arm_neon_vst2,
  658. Intrinsic::arm_neon_vst3,
  659. Intrinsic::arm_neon_vst4};
  660. static const Intrinsic::ID StoreLaneInts[] = {
  661. Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane,
  662. Intrinsic::arm_neon_vst4lane
  663. };
  664. auto fArgs = F->getFunctionType()->params();
  665. Type *Tys[] = {fArgs[0], fArgs[1]};
  666. if (!Name.contains("lane"))
  667. NewFn = Intrinsic::getDeclaration(F->getParent(),
  668. StoreInts[fArgs.size() - 3], Tys);
  669. else
  670. NewFn = Intrinsic::getDeclaration(F->getParent(),
  671. StoreLaneInts[fArgs.size() - 5], Tys);
  672. return true;
  673. }
  674. if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") {
  675. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer);
  676. return true;
  677. }
  678. if (Name.startswith("arm.neon.vqadds.")) {
  679. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::sadd_sat,
  680. F->arg_begin()->getType());
  681. return true;
  682. }
  683. if (Name.startswith("arm.neon.vqaddu.")) {
  684. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::uadd_sat,
  685. F->arg_begin()->getType());
  686. return true;
  687. }
  688. if (Name.startswith("arm.neon.vqsubs.")) {
  689. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ssub_sat,
  690. F->arg_begin()->getType());
  691. return true;
  692. }
  693. if (Name.startswith("arm.neon.vqsubu.")) {
  694. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::usub_sat,
  695. F->arg_begin()->getType());
  696. return true;
  697. }
  698. if (Name.startswith("aarch64.neon.addp")) {
  699. if (F->arg_size() != 2)
  700. break; // Invalid IR.
  701. VectorType *Ty = dyn_cast<VectorType>(F->getReturnType());
  702. if (Ty && Ty->getElementType()->isFloatingPointTy()) {
  703. NewFn = Intrinsic::getDeclaration(F->getParent(),
  704. Intrinsic::aarch64_neon_faddp, Ty);
  705. return true;
  706. }
  707. }
  708. // Changed in 12.0: bfdot accept v4bf16 and v8bf16 instead of v8i8 and v16i8
  709. // respectively
  710. if ((Name.startswith("arm.neon.bfdot.") ||
  711. Name.startswith("aarch64.neon.bfdot.")) &&
  712. Name.endswith("i8")) {
  713. Intrinsic::ID IID =
  714. StringSwitch<Intrinsic::ID>(Name)
  715. .Cases("arm.neon.bfdot.v2f32.v8i8",
  716. "arm.neon.bfdot.v4f32.v16i8",
  717. Intrinsic::arm_neon_bfdot)
  718. .Cases("aarch64.neon.bfdot.v2f32.v8i8",
  719. "aarch64.neon.bfdot.v4f32.v16i8",
  720. Intrinsic::aarch64_neon_bfdot)
  721. .Default(Intrinsic::not_intrinsic);
  722. if (IID == Intrinsic::not_intrinsic)
  723. break;
  724. size_t OperandWidth = F->getReturnType()->getPrimitiveSizeInBits();
  725. assert((OperandWidth == 64 || OperandWidth == 128) &&
  726. "Unexpected operand width");
  727. LLVMContext &Ctx = F->getParent()->getContext();
  728. std::array<Type *, 2> Tys {{
  729. F->getReturnType(),
  730. FixedVectorType::get(Type::getBFloatTy(Ctx), OperandWidth / 16)
  731. }};
  732. NewFn = Intrinsic::getDeclaration(F->getParent(), IID, Tys);
  733. return true;
  734. }
  735. // Changed in 12.0: bfmmla, bfmlalb and bfmlalt are not polymorphic anymore
  736. // and accept v8bf16 instead of v16i8
  737. if ((Name.startswith("arm.neon.bfm") ||
  738. Name.startswith("aarch64.neon.bfm")) &&
  739. Name.endswith(".v4f32.v16i8")) {
  740. Intrinsic::ID IID =
  741. StringSwitch<Intrinsic::ID>(Name)
  742. .Case("arm.neon.bfmmla.v4f32.v16i8",
  743. Intrinsic::arm_neon_bfmmla)
  744. .Case("arm.neon.bfmlalb.v4f32.v16i8",
  745. Intrinsic::arm_neon_bfmlalb)
  746. .Case("arm.neon.bfmlalt.v4f32.v16i8",
  747. Intrinsic::arm_neon_bfmlalt)
  748. .Case("aarch64.neon.bfmmla.v4f32.v16i8",
  749. Intrinsic::aarch64_neon_bfmmla)
  750. .Case("aarch64.neon.bfmlalb.v4f32.v16i8",
  751. Intrinsic::aarch64_neon_bfmlalb)
  752. .Case("aarch64.neon.bfmlalt.v4f32.v16i8",
  753. Intrinsic::aarch64_neon_bfmlalt)
  754. .Default(Intrinsic::not_intrinsic);
  755. if (IID == Intrinsic::not_intrinsic)
  756. break;
  757. std::array<Type *, 0> Tys;
  758. NewFn = Intrinsic::getDeclaration(F->getParent(), IID, Tys);
  759. return true;
  760. }
  761. if (Name == "arm.mve.vctp64" &&
  762. cast<FixedVectorType>(F->getReturnType())->getNumElements() == 4) {
  763. // A vctp64 returning a v4i1 is converted to return a v2i1. Rename the
  764. // function and deal with it below in UpgradeIntrinsicCall.
  765. rename(F);
  766. return true;
  767. }
  768. // These too are changed to accept a v2i1 insteead of the old v4i1.
  769. if (Name == "arm.mve.mull.int.predicated.v2i64.v4i32.v4i1" ||
  770. Name == "arm.mve.vqdmull.predicated.v2i64.v4i32.v4i1" ||
  771. Name == "arm.mve.vldr.gather.base.predicated.v2i64.v2i64.v4i1" ||
  772. Name == "arm.mve.vldr.gather.base.wb.predicated.v2i64.v2i64.v4i1" ||
  773. Name == "arm.mve.vldr.gather.offset.predicated.v2i64.p0i64.v2i64.v4i1" ||
  774. Name == "arm.mve.vstr.scatter.base.predicated.v2i64.v2i64.v4i1" ||
  775. Name == "arm.mve.vstr.scatter.base.wb.predicated.v2i64.v2i64.v4i1" ||
  776. Name == "arm.mve.vstr.scatter.offset.predicated.p0i64.v2i64.v2i64.v4i1" ||
  777. Name == "arm.cde.vcx1q.predicated.v2i64.v4i1" ||
  778. Name == "arm.cde.vcx1qa.predicated.v2i64.v4i1" ||
  779. Name == "arm.cde.vcx2q.predicated.v2i64.v4i1" ||
  780. Name == "arm.cde.vcx2qa.predicated.v2i64.v4i1" ||
  781. Name == "arm.cde.vcx3q.predicated.v2i64.v4i1" ||
  782. Name == "arm.cde.vcx3qa.predicated.v2i64.v4i1")
  783. return true;
  784. if (Name == "amdgcn.alignbit") {
  785. // Target specific intrinsic became redundant
  786. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::fshr,
  787. {F->getReturnType()});
  788. return true;
  789. }
  790. break;
  791. }
  792. case 'c': {
  793. if (Name.startswith("ctlz.") && F->arg_size() == 1) {
  794. rename(F);
  795. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
  796. F->arg_begin()->getType());
  797. return true;
  798. }
  799. if (Name.startswith("cttz.") && F->arg_size() == 1) {
  800. rename(F);
  801. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
  802. F->arg_begin()->getType());
  803. return true;
  804. }
  805. break;
  806. }
  807. case 'd': {
  808. if (Name == "dbg.value" && F->arg_size() == 4) {
  809. rename(F);
  810. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value);
  811. return true;
  812. }
  813. break;
  814. }
  815. case 'e': {
  816. if (Name.startswith("experimental.vector.extract.")) {
  817. rename(F);
  818. Type *Tys[] = {F->getReturnType(), F->arg_begin()->getType()};
  819. NewFn = Intrinsic::getDeclaration(F->getParent(),
  820. Intrinsic::vector_extract, Tys);
  821. return true;
  822. }
  823. if (Name.startswith("experimental.vector.insert.")) {
  824. rename(F);
  825. auto Args = F->getFunctionType()->params();
  826. Type *Tys[] = {Args[0], Args[1]};
  827. NewFn = Intrinsic::getDeclaration(F->getParent(),
  828. Intrinsic::vector_insert, Tys);
  829. return true;
  830. }
  831. SmallVector<StringRef, 2> Groups;
  832. static const Regex R("^experimental.vector.reduce.([a-z]+)\\.[a-z][0-9]+");
  833. if (R.match(Name, &Groups)) {
  834. Intrinsic::ID ID;
  835. ID = StringSwitch<Intrinsic::ID>(Groups[1])
  836. .Case("add", Intrinsic::vector_reduce_add)
  837. .Case("mul", Intrinsic::vector_reduce_mul)
  838. .Case("and", Intrinsic::vector_reduce_and)
  839. .Case("or", Intrinsic::vector_reduce_or)
  840. .Case("xor", Intrinsic::vector_reduce_xor)
  841. .Case("smax", Intrinsic::vector_reduce_smax)
  842. .Case("smin", Intrinsic::vector_reduce_smin)
  843. .Case("umax", Intrinsic::vector_reduce_umax)
  844. .Case("umin", Intrinsic::vector_reduce_umin)
  845. .Case("fmax", Intrinsic::vector_reduce_fmax)
  846. .Case("fmin", Intrinsic::vector_reduce_fmin)
  847. .Default(Intrinsic::not_intrinsic);
  848. if (ID != Intrinsic::not_intrinsic) {
  849. rename(F);
  850. auto Args = F->getFunctionType()->params();
  851. NewFn = Intrinsic::getDeclaration(F->getParent(), ID, {Args[0]});
  852. return true;
  853. }
  854. }
  855. static const Regex R2(
  856. "^experimental.vector.reduce.v2.([a-z]+)\\.[fi][0-9]+");
  857. Groups.clear();
  858. if (R2.match(Name, &Groups)) {
  859. Intrinsic::ID ID = Intrinsic::not_intrinsic;
  860. if (Groups[1] == "fadd")
  861. ID = Intrinsic::vector_reduce_fadd;
  862. if (Groups[1] == "fmul")
  863. ID = Intrinsic::vector_reduce_fmul;
  864. if (ID != Intrinsic::not_intrinsic) {
  865. rename(F);
  866. auto Args = F->getFunctionType()->params();
  867. Type *Tys[] = {Args[1]};
  868. NewFn = Intrinsic::getDeclaration(F->getParent(), ID, Tys);
  869. return true;
  870. }
  871. }
  872. break;
  873. }
  874. case 'f':
  875. if (Name.startswith("flt.rounds")) {
  876. rename(F);
  877. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::get_rounding);
  878. return true;
  879. }
  880. break;
  881. case 'i':
  882. case 'l': {
  883. bool IsLifetimeStart = Name.startswith("lifetime.start");
  884. if (IsLifetimeStart || Name.startswith("invariant.start")) {
  885. Intrinsic::ID ID = IsLifetimeStart ?
  886. Intrinsic::lifetime_start : Intrinsic::invariant_start;
  887. auto Args = F->getFunctionType()->params();
  888. Type* ObjectPtr[1] = {Args[1]};
  889. if (F->getName() != Intrinsic::getName(ID, ObjectPtr, F->getParent())) {
  890. rename(F);
  891. NewFn = Intrinsic::getDeclaration(F->getParent(), ID, ObjectPtr);
  892. return true;
  893. }
  894. }
  895. bool IsLifetimeEnd = Name.startswith("lifetime.end");
  896. if (IsLifetimeEnd || Name.startswith("invariant.end")) {
  897. Intrinsic::ID ID = IsLifetimeEnd ?
  898. Intrinsic::lifetime_end : Intrinsic::invariant_end;
  899. auto Args = F->getFunctionType()->params();
  900. Type* ObjectPtr[1] = {Args[IsLifetimeEnd ? 1 : 2]};
  901. if (F->getName() != Intrinsic::getName(ID, ObjectPtr, F->getParent())) {
  902. rename(F);
  903. NewFn = Intrinsic::getDeclaration(F->getParent(), ID, ObjectPtr);
  904. return true;
  905. }
  906. }
  907. if (Name.startswith("invariant.group.barrier")) {
  908. // Rename invariant.group.barrier to launder.invariant.group
  909. auto Args = F->getFunctionType()->params();
  910. Type* ObjectPtr[1] = {Args[0]};
  911. rename(F);
  912. NewFn = Intrinsic::getDeclaration(F->getParent(),
  913. Intrinsic::launder_invariant_group, ObjectPtr);
  914. return true;
  915. }
  916. break;
  917. }
  918. case 'm': {
  919. if (Name.startswith("masked.load.")) {
  920. Type *Tys[] = { F->getReturnType(), F->arg_begin()->getType() };
  921. if (F->getName() !=
  922. Intrinsic::getName(Intrinsic::masked_load, Tys, F->getParent())) {
  923. rename(F);
  924. NewFn = Intrinsic::getDeclaration(F->getParent(),
  925. Intrinsic::masked_load,
  926. Tys);
  927. return true;
  928. }
  929. }
  930. if (Name.startswith("masked.store.")) {
  931. auto Args = F->getFunctionType()->params();
  932. Type *Tys[] = { Args[0], Args[1] };
  933. if (F->getName() !=
  934. Intrinsic::getName(Intrinsic::masked_store, Tys, F->getParent())) {
  935. rename(F);
  936. NewFn = Intrinsic::getDeclaration(F->getParent(),
  937. Intrinsic::masked_store,
  938. Tys);
  939. return true;
  940. }
  941. }
  942. // Renaming gather/scatter intrinsics with no address space overloading
  943. // to the new overload which includes an address space
  944. if (Name.startswith("masked.gather.")) {
  945. Type *Tys[] = {F->getReturnType(), F->arg_begin()->getType()};
  946. if (F->getName() !=
  947. Intrinsic::getName(Intrinsic::masked_gather, Tys, F->getParent())) {
  948. rename(F);
  949. NewFn = Intrinsic::getDeclaration(F->getParent(),
  950. Intrinsic::masked_gather, Tys);
  951. return true;
  952. }
  953. }
  954. if (Name.startswith("masked.scatter.")) {
  955. auto Args = F->getFunctionType()->params();
  956. Type *Tys[] = {Args[0], Args[1]};
  957. if (F->getName() !=
  958. Intrinsic::getName(Intrinsic::masked_scatter, Tys, F->getParent())) {
  959. rename(F);
  960. NewFn = Intrinsic::getDeclaration(F->getParent(),
  961. Intrinsic::masked_scatter, Tys);
  962. return true;
  963. }
  964. }
  965. // Updating the memory intrinsics (memcpy/memmove/memset) that have an
  966. // alignment parameter to embedding the alignment as an attribute of
  967. // the pointer args.
  968. if (Name.startswith("memcpy.") && F->arg_size() == 5) {
  969. rename(F);
  970. // Get the types of dest, src, and len
  971. ArrayRef<Type *> ParamTypes = F->getFunctionType()->params().slice(0, 3);
  972. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::memcpy,
  973. ParamTypes);
  974. return true;
  975. }
  976. if (Name.startswith("memmove.") && F->arg_size() == 5) {
  977. rename(F);
  978. // Get the types of dest, src, and len
  979. ArrayRef<Type *> ParamTypes = F->getFunctionType()->params().slice(0, 3);
  980. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::memmove,
  981. ParamTypes);
  982. return true;
  983. }
  984. if (Name.startswith("memset.") && F->arg_size() == 5) {
  985. rename(F);
  986. // Get the types of dest, and len
  987. const auto *FT = F->getFunctionType();
  988. Type *ParamTypes[2] = {
  989. FT->getParamType(0), // Dest
  990. FT->getParamType(2) // len
  991. };
  992. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::memset,
  993. ParamTypes);
  994. return true;
  995. }
  996. break;
  997. }
  998. case 'n': {
  999. if (Name.startswith("nvvm.")) {
  1000. Name = Name.substr(5);
  1001. // The following nvvm intrinsics correspond exactly to an LLVM intrinsic.
  1002. Intrinsic::ID IID = StringSwitch<Intrinsic::ID>(Name)
  1003. .Cases("brev32", "brev64", Intrinsic::bitreverse)
  1004. .Case("clz.i", Intrinsic::ctlz)
  1005. .Case("popc.i", Intrinsic::ctpop)
  1006. .Default(Intrinsic::not_intrinsic);
  1007. if (IID != Intrinsic::not_intrinsic && F->arg_size() == 1) {
  1008. NewFn = Intrinsic::getDeclaration(F->getParent(), IID,
  1009. {F->getReturnType()});
  1010. return true;
  1011. }
  1012. // The following nvvm intrinsics correspond exactly to an LLVM idiom, but
  1013. // not to an intrinsic alone. We expand them in UpgradeIntrinsicCall.
  1014. //
  1015. // TODO: We could add lohi.i2d.
  1016. bool Expand = StringSwitch<bool>(Name)
  1017. .Cases("abs.i", "abs.ll", true)
  1018. .Cases("clz.ll", "popc.ll", "h2f", true)
  1019. .Cases("max.i", "max.ll", "max.ui", "max.ull", true)
  1020. .Cases("min.i", "min.ll", "min.ui", "min.ull", true)
  1021. .StartsWith("atomic.load.add.f32.p", true)
  1022. .StartsWith("atomic.load.add.f64.p", true)
  1023. .Default(false);
  1024. if (Expand) {
  1025. NewFn = nullptr;
  1026. return true;
  1027. }
  1028. }
  1029. break;
  1030. }
  1031. case 'o':
  1032. // We only need to change the name to match the mangling including the
  1033. // address space.
  1034. if (Name.startswith("objectsize.")) {
  1035. Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
  1036. if (F->arg_size() == 2 || F->arg_size() == 3 ||
  1037. F->getName() !=
  1038. Intrinsic::getName(Intrinsic::objectsize, Tys, F->getParent())) {
  1039. rename(F);
  1040. NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::objectsize,
  1041. Tys);
  1042. return true;
  1043. }
  1044. }
  1045. break;
  1046. case 'p':
  1047. if (Name == "prefetch") {
  1048. // Handle address space overloading.
  1049. Type *Tys[] = {F->arg_begin()->getType()};
  1050. if (F->getName() !=
  1051. Intrinsic::getName(Intrinsic::prefetch, Tys, F->getParent())) {
  1052. rename(F);
  1053. NewFn =
  1054. Intrinsic::getDeclaration(F->getParent(), Intrinsic::prefetch, Tys);
  1055. return true;
  1056. }
  1057. } else if (Name.startswith("ptr.annotation.") && F->arg_size() == 4) {
  1058. rename(F);
  1059. NewFn = Intrinsic::getDeclaration(
  1060. F->getParent(), Intrinsic::ptr_annotation,
  1061. {F->arg_begin()->getType(), F->getArg(1)->getType()});
  1062. return true;
  1063. }
  1064. break;
  1065. case 's':
  1066. if (Name == "stackprotectorcheck") {
  1067. NewFn = nullptr;
  1068. return true;
  1069. }
  1070. break;
  1071. case 'v': {
  1072. if (Name == "var.annotation" && F->arg_size() == 4) {
  1073. rename(F);
  1074. NewFn = Intrinsic::getDeclaration(
  1075. F->getParent(), Intrinsic::var_annotation,
  1076. {{F->arg_begin()->getType(), F->getArg(1)->getType()}});
  1077. return true;
  1078. }
  1079. break;
  1080. }
  1081. case 'x':
  1082. if (UpgradeX86IntrinsicFunction(F, Name, NewFn))
  1083. return true;
  1084. }
  1085. auto *ST = dyn_cast<StructType>(F->getReturnType());
  1086. if (ST && (!ST->isLiteral() || ST->isPacked())) {
  1087. // Replace return type with literal non-packed struct. Only do this for
  1088. // intrinsics declared to return a struct, not for intrinsics with
  1089. // overloaded return type, in which case the exact struct type will be
  1090. // mangled into the name.
  1091. SmallVector<Intrinsic::IITDescriptor> Desc;
  1092. Intrinsic::getIntrinsicInfoTableEntries(F->getIntrinsicID(), Desc);
  1093. if (Desc.front().Kind == Intrinsic::IITDescriptor::Struct) {
  1094. auto *FT = F->getFunctionType();
  1095. auto *NewST = StructType::get(ST->getContext(), ST->elements());
  1096. auto *NewFT = FunctionType::get(NewST, FT->params(), FT->isVarArg());
  1097. std::string Name = F->getName().str();
  1098. rename(F);
  1099. NewFn = Function::Create(NewFT, F->getLinkage(), F->getAddressSpace(),
  1100. Name, F->getParent());
  1101. // The new function may also need remangling.
  1102. if (auto Result = llvm::Intrinsic::remangleIntrinsicFunction(NewFn))
  1103. NewFn = *Result;
  1104. return true;
  1105. }
  1106. }
  1107. // Remangle our intrinsic since we upgrade the mangling
  1108. auto Result = llvm::Intrinsic::remangleIntrinsicFunction(F);
  1109. if (Result != std::nullopt) {
  1110. NewFn = *Result;
  1111. return true;
  1112. }
  1113. // This may not belong here. This function is effectively being overloaded
  1114. // to both detect an intrinsic which needs upgrading, and to provide the
  1115. // upgraded form of the intrinsic. We should perhaps have two separate
  1116. // functions for this.
  1117. return false;
  1118. }
  1119. bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
  1120. NewFn = nullptr;
  1121. bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
  1122. assert(F != NewFn && "Intrinsic function upgraded to the same function");
  1123. // Upgrade intrinsic attributes. This does not change the function.
  1124. if (NewFn)
  1125. F = NewFn;
  1126. if (Intrinsic::ID id = F->getIntrinsicID())
  1127. F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
  1128. return Upgraded;
  1129. }
  1130. GlobalVariable *llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
  1131. if (!(GV->hasName() && (GV->getName() == "llvm.global_ctors" ||
  1132. GV->getName() == "llvm.global_dtors")) ||
  1133. !GV->hasInitializer())
  1134. return nullptr;
  1135. ArrayType *ATy = dyn_cast<ArrayType>(GV->getValueType());
  1136. if (!ATy)
  1137. return nullptr;
  1138. StructType *STy = dyn_cast<StructType>(ATy->getElementType());
  1139. if (!STy || STy->getNumElements() != 2)
  1140. return nullptr;
  1141. LLVMContext &C = GV->getContext();
  1142. IRBuilder<> IRB(C);
  1143. auto EltTy = StructType::get(STy->getElementType(0), STy->getElementType(1),
  1144. IRB.getInt8PtrTy());
  1145. Constant *Init = GV->getInitializer();
  1146. unsigned N = Init->getNumOperands();
  1147. std::vector<Constant *> NewCtors(N);
  1148. for (unsigned i = 0; i != N; ++i) {
  1149. auto Ctor = cast<Constant>(Init->getOperand(i));
  1150. NewCtors[i] = ConstantStruct::get(
  1151. EltTy, Ctor->getAggregateElement(0u), Ctor->getAggregateElement(1),
  1152. Constant::getNullValue(IRB.getInt8PtrTy()));
  1153. }
  1154. Constant *NewInit = ConstantArray::get(ArrayType::get(EltTy, N), NewCtors);
  1155. return new GlobalVariable(NewInit->getType(), false, GV->getLinkage(),
  1156. NewInit, GV->getName());
  1157. }
  1158. // Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
  1159. // to byte shuffles.
  1160. static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder,
  1161. Value *Op, unsigned Shift) {
  1162. auto *ResultTy = cast<FixedVectorType>(Op->getType());
  1163. unsigned NumElts = ResultTy->getNumElements() * 8;
  1164. // Bitcast from a 64-bit element type to a byte element type.
  1165. Type *VecTy = FixedVectorType::get(Builder.getInt8Ty(), NumElts);
  1166. Op = Builder.CreateBitCast(Op, VecTy, "cast");
  1167. // We'll be shuffling in zeroes.
  1168. Value *Res = Constant::getNullValue(VecTy);
  1169. // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
  1170. // we'll just return the zero vector.
  1171. if (Shift < 16) {
  1172. int Idxs[64];
  1173. // 256/512-bit version is split into 2/4 16-byte lanes.
  1174. for (unsigned l = 0; l != NumElts; l += 16)
  1175. for (unsigned i = 0; i != 16; ++i) {
  1176. unsigned Idx = NumElts + i - Shift;
  1177. if (Idx < NumElts)
  1178. Idx -= NumElts - 16; // end of lane, switch operand.
  1179. Idxs[l + i] = Idx + l;
  1180. }
  1181. Res = Builder.CreateShuffleVector(Res, Op, ArrayRef(Idxs, NumElts));
  1182. }
  1183. // Bitcast back to a 64-bit element type.
  1184. return Builder.CreateBitCast(Res, ResultTy, "cast");
  1185. }
  1186. // Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
  1187. // to byte shuffles.
  1188. static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, Value *Op,
  1189. unsigned Shift) {
  1190. auto *ResultTy = cast<FixedVectorType>(Op->getType());
  1191. unsigned NumElts = ResultTy->getNumElements() * 8;
  1192. // Bitcast from a 64-bit element type to a byte element type.
  1193. Type *VecTy = FixedVectorType::get(Builder.getInt8Ty(), NumElts);
  1194. Op = Builder.CreateBitCast(Op, VecTy, "cast");
  1195. // We'll be shuffling in zeroes.
  1196. Value *Res = Constant::getNullValue(VecTy);
  1197. // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
  1198. // we'll just return the zero vector.
  1199. if (Shift < 16) {
  1200. int Idxs[64];
  1201. // 256/512-bit version is split into 2/4 16-byte lanes.
  1202. for (unsigned l = 0; l != NumElts; l += 16)
  1203. for (unsigned i = 0; i != 16; ++i) {
  1204. unsigned Idx = i + Shift;
  1205. if (Idx >= 16)
  1206. Idx += NumElts - 16; // end of lane, switch operand.
  1207. Idxs[l + i] = Idx + l;
  1208. }
  1209. Res = Builder.CreateShuffleVector(Op, Res, ArrayRef(Idxs, NumElts));
  1210. }
  1211. // Bitcast back to a 64-bit element type.
  1212. return Builder.CreateBitCast(Res, ResultTy, "cast");
  1213. }
  1214. static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
  1215. unsigned NumElts) {
  1216. assert(isPowerOf2_32(NumElts) && "Expected power-of-2 mask elements");
  1217. llvm::VectorType *MaskTy = FixedVectorType::get(
  1218. Builder.getInt1Ty(), cast<IntegerType>(Mask->getType())->getBitWidth());
  1219. Mask = Builder.CreateBitCast(Mask, MaskTy);
  1220. // If we have less than 8 elements (1, 2 or 4), then the starting mask was an
  1221. // i8 and we need to extract down to the right number of elements.
  1222. if (NumElts <= 4) {
  1223. int Indices[4];
  1224. for (unsigned i = 0; i != NumElts; ++i)
  1225. Indices[i] = i;
  1226. Mask = Builder.CreateShuffleVector(Mask, Mask, ArrayRef(Indices, NumElts),
  1227. "extract");
  1228. }
  1229. return Mask;
  1230. }
  1231. static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
  1232. Value *Op0, Value *Op1) {
  1233. // If the mask is all ones just emit the first operation.
  1234. if (const auto *C = dyn_cast<Constant>(Mask))
  1235. if (C->isAllOnesValue())
  1236. return Op0;
  1237. Mask = getX86MaskVec(Builder, Mask,
  1238. cast<FixedVectorType>(Op0->getType())->getNumElements());
  1239. return Builder.CreateSelect(Mask, Op0, Op1);
  1240. }
  1241. static Value *EmitX86ScalarSelect(IRBuilder<> &Builder, Value *Mask,
  1242. Value *Op0, Value *Op1) {
  1243. // If the mask is all ones just emit the first operation.
  1244. if (const auto *C = dyn_cast<Constant>(Mask))
  1245. if (C->isAllOnesValue())
  1246. return Op0;
  1247. auto *MaskTy = FixedVectorType::get(Builder.getInt1Ty(),
  1248. Mask->getType()->getIntegerBitWidth());
  1249. Mask = Builder.CreateBitCast(Mask, MaskTy);
  1250. Mask = Builder.CreateExtractElement(Mask, (uint64_t)0);
  1251. return Builder.CreateSelect(Mask, Op0, Op1);
  1252. }
  1253. // Handle autoupgrade for masked PALIGNR and VALIGND/Q intrinsics.
  1254. // PALIGNR handles large immediates by shifting while VALIGN masks the immediate
  1255. // so we need to handle both cases. VALIGN also doesn't have 128-bit lanes.
  1256. static Value *UpgradeX86ALIGNIntrinsics(IRBuilder<> &Builder, Value *Op0,
  1257. Value *Op1, Value *Shift,
  1258. Value *Passthru, Value *Mask,
  1259. bool IsVALIGN) {
  1260. unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
  1261. unsigned NumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
  1262. assert((IsVALIGN || NumElts % 16 == 0) && "Illegal NumElts for PALIGNR!");
  1263. assert((!IsVALIGN || NumElts <= 16) && "NumElts too large for VALIGN!");
  1264. assert(isPowerOf2_32(NumElts) && "NumElts not a power of 2!");
  1265. // Mask the immediate for VALIGN.
  1266. if (IsVALIGN)
  1267. ShiftVal &= (NumElts - 1);
  1268. // If palignr is shifting the pair of vectors more than the size of two
  1269. // lanes, emit zero.
  1270. if (ShiftVal >= 32)
  1271. return llvm::Constant::getNullValue(Op0->getType());
  1272. // If palignr is shifting the pair of input vectors more than one lane,
  1273. // but less than two lanes, convert to shifting in zeroes.
  1274. if (ShiftVal > 16) {
  1275. ShiftVal -= 16;
  1276. Op1 = Op0;
  1277. Op0 = llvm::Constant::getNullValue(Op0->getType());
  1278. }
  1279. int Indices[64];
  1280. // 256-bit palignr operates on 128-bit lanes so we need to handle that
  1281. for (unsigned l = 0; l < NumElts; l += 16) {
  1282. for (unsigned i = 0; i != 16; ++i) {
  1283. unsigned Idx = ShiftVal + i;
  1284. if (!IsVALIGN && Idx >= 16) // Disable wrap for VALIGN.
  1285. Idx += NumElts - 16; // End of lane, switch operand.
  1286. Indices[l + i] = Idx + l;
  1287. }
  1288. }
  1289. Value *Align = Builder.CreateShuffleVector(
  1290. Op1, Op0, ArrayRef(Indices, NumElts), "palignr");
  1291. return EmitX86Select(Builder, Mask, Align, Passthru);
  1292. }
  1293. static Value *UpgradeX86VPERMT2Intrinsics(IRBuilder<> &Builder, CallBase &CI,
  1294. bool ZeroMask, bool IndexForm) {
  1295. Type *Ty = CI.getType();
  1296. unsigned VecWidth = Ty->getPrimitiveSizeInBits();
  1297. unsigned EltWidth = Ty->getScalarSizeInBits();
  1298. bool IsFloat = Ty->isFPOrFPVectorTy();
  1299. Intrinsic::ID IID;
  1300. if (VecWidth == 128 && EltWidth == 32 && IsFloat)
  1301. IID = Intrinsic::x86_avx512_vpermi2var_ps_128;
  1302. else if (VecWidth == 128 && EltWidth == 32 && !IsFloat)
  1303. IID = Intrinsic::x86_avx512_vpermi2var_d_128;
  1304. else if (VecWidth == 128 && EltWidth == 64 && IsFloat)
  1305. IID = Intrinsic::x86_avx512_vpermi2var_pd_128;
  1306. else if (VecWidth == 128 && EltWidth == 64 && !IsFloat)
  1307. IID = Intrinsic::x86_avx512_vpermi2var_q_128;
  1308. else if (VecWidth == 256 && EltWidth == 32 && IsFloat)
  1309. IID = Intrinsic::x86_avx512_vpermi2var_ps_256;
  1310. else if (VecWidth == 256 && EltWidth == 32 && !IsFloat)
  1311. IID = Intrinsic::x86_avx512_vpermi2var_d_256;
  1312. else if (VecWidth == 256 && EltWidth == 64 && IsFloat)
  1313. IID = Intrinsic::x86_avx512_vpermi2var_pd_256;
  1314. else if (VecWidth == 256 && EltWidth == 64 && !IsFloat)
  1315. IID = Intrinsic::x86_avx512_vpermi2var_q_256;
  1316. else if (VecWidth == 512 && EltWidth == 32 && IsFloat)
  1317. IID = Intrinsic::x86_avx512_vpermi2var_ps_512;
  1318. else if (VecWidth == 512 && EltWidth == 32 && !IsFloat)
  1319. IID = Intrinsic::x86_avx512_vpermi2var_d_512;
  1320. else if (VecWidth == 512 && EltWidth == 64 && IsFloat)
  1321. IID = Intrinsic::x86_avx512_vpermi2var_pd_512;
  1322. else if (VecWidth == 512 && EltWidth == 64 && !IsFloat)
  1323. IID = Intrinsic::x86_avx512_vpermi2var_q_512;
  1324. else if (VecWidth == 128 && EltWidth == 16)
  1325. IID = Intrinsic::x86_avx512_vpermi2var_hi_128;
  1326. else if (VecWidth == 256 && EltWidth == 16)
  1327. IID = Intrinsic::x86_avx512_vpermi2var_hi_256;
  1328. else if (VecWidth == 512 && EltWidth == 16)
  1329. IID = Intrinsic::x86_avx512_vpermi2var_hi_512;
  1330. else if (VecWidth == 128 && EltWidth == 8)
  1331. IID = Intrinsic::x86_avx512_vpermi2var_qi_128;
  1332. else if (VecWidth == 256 && EltWidth == 8)
  1333. IID = Intrinsic::x86_avx512_vpermi2var_qi_256;
  1334. else if (VecWidth == 512 && EltWidth == 8)
  1335. IID = Intrinsic::x86_avx512_vpermi2var_qi_512;
  1336. else
  1337. llvm_unreachable("Unexpected intrinsic");
  1338. Value *Args[] = { CI.getArgOperand(0) , CI.getArgOperand(1),
  1339. CI.getArgOperand(2) };
  1340. // If this isn't index form we need to swap operand 0 and 1.
  1341. if (!IndexForm)
  1342. std::swap(Args[0], Args[1]);
  1343. Value *V = Builder.CreateCall(Intrinsic::getDeclaration(CI.getModule(), IID),
  1344. Args);
  1345. Value *PassThru = ZeroMask ? ConstantAggregateZero::get(Ty)
  1346. : Builder.CreateBitCast(CI.getArgOperand(1),
  1347. Ty);
  1348. return EmitX86Select(Builder, CI.getArgOperand(3), V, PassThru);
  1349. }
  1350. static Value *UpgradeX86BinaryIntrinsics(IRBuilder<> &Builder, CallBase &CI,
  1351. Intrinsic::ID IID) {
  1352. Type *Ty = CI.getType();
  1353. Value *Op0 = CI.getOperand(0);
  1354. Value *Op1 = CI.getOperand(1);
  1355. Function *Intrin = Intrinsic::getDeclaration(CI.getModule(), IID, Ty);
  1356. Value *Res = Builder.CreateCall(Intrin, {Op0, Op1});
  1357. if (CI.arg_size() == 4) { // For masked intrinsics.
  1358. Value *VecSrc = CI.getOperand(2);
  1359. Value *Mask = CI.getOperand(3);
  1360. Res = EmitX86Select(Builder, Mask, Res, VecSrc);
  1361. }
  1362. return Res;
  1363. }
  1364. static Value *upgradeX86Rotate(IRBuilder<> &Builder, CallBase &CI,
  1365. bool IsRotateRight) {
  1366. Type *Ty = CI.getType();
  1367. Value *Src = CI.getArgOperand(0);
  1368. Value *Amt = CI.getArgOperand(1);
  1369. // Amount may be scalar immediate, in which case create a splat vector.
  1370. // Funnel shifts amounts are treated as modulo and types are all power-of-2 so
  1371. // we only care about the lowest log2 bits anyway.
  1372. if (Amt->getType() != Ty) {
  1373. unsigned NumElts = cast<FixedVectorType>(Ty)->getNumElements();
  1374. Amt = Builder.CreateIntCast(Amt, Ty->getScalarType(), false);
  1375. Amt = Builder.CreateVectorSplat(NumElts, Amt);
  1376. }
  1377. Intrinsic::ID IID = IsRotateRight ? Intrinsic::fshr : Intrinsic::fshl;
  1378. Function *Intrin = Intrinsic::getDeclaration(CI.getModule(), IID, Ty);
  1379. Value *Res = Builder.CreateCall(Intrin, {Src, Src, Amt});
  1380. if (CI.arg_size() == 4) { // For masked intrinsics.
  1381. Value *VecSrc = CI.getOperand(2);
  1382. Value *Mask = CI.getOperand(3);
  1383. Res = EmitX86Select(Builder, Mask, Res, VecSrc);
  1384. }
  1385. return Res;
  1386. }
  1387. static Value *upgradeX86vpcom(IRBuilder<> &Builder, CallBase &CI, unsigned Imm,
  1388. bool IsSigned) {
  1389. Type *Ty = CI.getType();
  1390. Value *LHS = CI.getArgOperand(0);
  1391. Value *RHS = CI.getArgOperand(1);
  1392. CmpInst::Predicate Pred;
  1393. switch (Imm) {
  1394. case 0x0:
  1395. Pred = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1396. break;
  1397. case 0x1:
  1398. Pred = IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
  1399. break;
  1400. case 0x2:
  1401. Pred = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1402. break;
  1403. case 0x3:
  1404. Pred = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
  1405. break;
  1406. case 0x4:
  1407. Pred = ICmpInst::ICMP_EQ;
  1408. break;
  1409. case 0x5:
  1410. Pred = ICmpInst::ICMP_NE;
  1411. break;
  1412. case 0x6:
  1413. return Constant::getNullValue(Ty); // FALSE
  1414. case 0x7:
  1415. return Constant::getAllOnesValue(Ty); // TRUE
  1416. default:
  1417. llvm_unreachable("Unknown XOP vpcom/vpcomu predicate");
  1418. }
  1419. Value *Cmp = Builder.CreateICmp(Pred, LHS, RHS);
  1420. Value *Ext = Builder.CreateSExt(Cmp, Ty);
  1421. return Ext;
  1422. }
  1423. static Value *upgradeX86ConcatShift(IRBuilder<> &Builder, CallBase &CI,
  1424. bool IsShiftRight, bool ZeroMask) {
  1425. Type *Ty = CI.getType();
  1426. Value *Op0 = CI.getArgOperand(0);
  1427. Value *Op1 = CI.getArgOperand(1);
  1428. Value *Amt = CI.getArgOperand(2);
  1429. if (IsShiftRight)
  1430. std::swap(Op0, Op1);
  1431. // Amount may be scalar immediate, in which case create a splat vector.
  1432. // Funnel shifts amounts are treated as modulo and types are all power-of-2 so
  1433. // we only care about the lowest log2 bits anyway.
  1434. if (Amt->getType() != Ty) {
  1435. unsigned NumElts = cast<FixedVectorType>(Ty)->getNumElements();
  1436. Amt = Builder.CreateIntCast(Amt, Ty->getScalarType(), false);
  1437. Amt = Builder.CreateVectorSplat(NumElts, Amt);
  1438. }
  1439. Intrinsic::ID IID = IsShiftRight ? Intrinsic::fshr : Intrinsic::fshl;
  1440. Function *Intrin = Intrinsic::getDeclaration(CI.getModule(), IID, Ty);
  1441. Value *Res = Builder.CreateCall(Intrin, {Op0, Op1, Amt});
  1442. unsigned NumArgs = CI.arg_size();
  1443. if (NumArgs >= 4) { // For masked intrinsics.
  1444. Value *VecSrc = NumArgs == 5 ? CI.getArgOperand(3) :
  1445. ZeroMask ? ConstantAggregateZero::get(CI.getType()) :
  1446. CI.getArgOperand(0);
  1447. Value *Mask = CI.getOperand(NumArgs - 1);
  1448. Res = EmitX86Select(Builder, Mask, Res, VecSrc);
  1449. }
  1450. return Res;
  1451. }
  1452. static Value *UpgradeMaskedStore(IRBuilder<> &Builder,
  1453. Value *Ptr, Value *Data, Value *Mask,
  1454. bool Aligned) {
  1455. // Cast the pointer to the right type.
  1456. Ptr = Builder.CreateBitCast(Ptr,
  1457. llvm::PointerType::getUnqual(Data->getType()));
  1458. const Align Alignment =
  1459. Aligned
  1460. ? Align(Data->getType()->getPrimitiveSizeInBits().getFixedValue() / 8)
  1461. : Align(1);
  1462. // If the mask is all ones just emit a regular store.
  1463. if (const auto *C = dyn_cast<Constant>(Mask))
  1464. if (C->isAllOnesValue())
  1465. return Builder.CreateAlignedStore(Data, Ptr, Alignment);
  1466. // Convert the mask from an integer type to a vector of i1.
  1467. unsigned NumElts = cast<FixedVectorType>(Data->getType())->getNumElements();
  1468. Mask = getX86MaskVec(Builder, Mask, NumElts);
  1469. return Builder.CreateMaskedStore(Data, Ptr, Alignment, Mask);
  1470. }
  1471. static Value *UpgradeMaskedLoad(IRBuilder<> &Builder,
  1472. Value *Ptr, Value *Passthru, Value *Mask,
  1473. bool Aligned) {
  1474. Type *ValTy = Passthru->getType();
  1475. // Cast the pointer to the right type.
  1476. Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(ValTy));
  1477. const Align Alignment =
  1478. Aligned
  1479. ? Align(
  1480. Passthru->getType()->getPrimitiveSizeInBits().getFixedValue() /
  1481. 8)
  1482. : Align(1);
  1483. // If the mask is all ones just emit a regular store.
  1484. if (const auto *C = dyn_cast<Constant>(Mask))
  1485. if (C->isAllOnesValue())
  1486. return Builder.CreateAlignedLoad(ValTy, Ptr, Alignment);
  1487. // Convert the mask from an integer type to a vector of i1.
  1488. unsigned NumElts = cast<FixedVectorType>(ValTy)->getNumElements();
  1489. Mask = getX86MaskVec(Builder, Mask, NumElts);
  1490. return Builder.CreateMaskedLoad(ValTy, Ptr, Alignment, Mask, Passthru);
  1491. }
  1492. static Value *upgradeAbs(IRBuilder<> &Builder, CallBase &CI) {
  1493. Type *Ty = CI.getType();
  1494. Value *Op0 = CI.getArgOperand(0);
  1495. Function *F = Intrinsic::getDeclaration(CI.getModule(), Intrinsic::abs, Ty);
  1496. Value *Res = Builder.CreateCall(F, {Op0, Builder.getInt1(false)});
  1497. if (CI.arg_size() == 3)
  1498. Res = EmitX86Select(Builder, CI.getArgOperand(2), Res, CI.getArgOperand(1));
  1499. return Res;
  1500. }
  1501. static Value *upgradePMULDQ(IRBuilder<> &Builder, CallBase &CI, bool IsSigned) {
  1502. Type *Ty = CI.getType();
  1503. // Arguments have a vXi32 type so cast to vXi64.
  1504. Value *LHS = Builder.CreateBitCast(CI.getArgOperand(0), Ty);
  1505. Value *RHS = Builder.CreateBitCast(CI.getArgOperand(1), Ty);
  1506. if (IsSigned) {
  1507. // Shift left then arithmetic shift right.
  1508. Constant *ShiftAmt = ConstantInt::get(Ty, 32);
  1509. LHS = Builder.CreateShl(LHS, ShiftAmt);
  1510. LHS = Builder.CreateAShr(LHS, ShiftAmt);
  1511. RHS = Builder.CreateShl(RHS, ShiftAmt);
  1512. RHS = Builder.CreateAShr(RHS, ShiftAmt);
  1513. } else {
  1514. // Clear the upper bits.
  1515. Constant *Mask = ConstantInt::get(Ty, 0xffffffff);
  1516. LHS = Builder.CreateAnd(LHS, Mask);
  1517. RHS = Builder.CreateAnd(RHS, Mask);
  1518. }
  1519. Value *Res = Builder.CreateMul(LHS, RHS);
  1520. if (CI.arg_size() == 4)
  1521. Res = EmitX86Select(Builder, CI.getArgOperand(3), Res, CI.getArgOperand(2));
  1522. return Res;
  1523. }
  1524. // Applying mask on vector of i1's and make sure result is at least 8 bits wide.
  1525. static Value *ApplyX86MaskOn1BitsVec(IRBuilder<> &Builder, Value *Vec,
  1526. Value *Mask) {
  1527. unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
  1528. if (Mask) {
  1529. const auto *C = dyn_cast<Constant>(Mask);
  1530. if (!C || !C->isAllOnesValue())
  1531. Vec = Builder.CreateAnd(Vec, getX86MaskVec(Builder, Mask, NumElts));
  1532. }
  1533. if (NumElts < 8) {
  1534. int Indices[8];
  1535. for (unsigned i = 0; i != NumElts; ++i)
  1536. Indices[i] = i;
  1537. for (unsigned i = NumElts; i != 8; ++i)
  1538. Indices[i] = NumElts + i % NumElts;
  1539. Vec = Builder.CreateShuffleVector(Vec,
  1540. Constant::getNullValue(Vec->getType()),
  1541. Indices);
  1542. }
  1543. return Builder.CreateBitCast(Vec, Builder.getIntNTy(std::max(NumElts, 8U)));
  1544. }
  1545. static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallBase &CI,
  1546. unsigned CC, bool Signed) {
  1547. Value *Op0 = CI.getArgOperand(0);
  1548. unsigned NumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
  1549. Value *Cmp;
  1550. if (CC == 3) {
  1551. Cmp = Constant::getNullValue(
  1552. FixedVectorType::get(Builder.getInt1Ty(), NumElts));
  1553. } else if (CC == 7) {
  1554. Cmp = Constant::getAllOnesValue(
  1555. FixedVectorType::get(Builder.getInt1Ty(), NumElts));
  1556. } else {
  1557. ICmpInst::Predicate Pred;
  1558. switch (CC) {
  1559. default: llvm_unreachable("Unknown condition code");
  1560. case 0: Pred = ICmpInst::ICMP_EQ; break;
  1561. case 1: Pred = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
  1562. case 2: Pred = Signed ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
  1563. case 4: Pred = ICmpInst::ICMP_NE; break;
  1564. case 5: Pred = Signed ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
  1565. case 6: Pred = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
  1566. }
  1567. Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
  1568. }
  1569. Value *Mask = CI.getArgOperand(CI.arg_size() - 1);
  1570. return ApplyX86MaskOn1BitsVec(Builder, Cmp, Mask);
  1571. }
  1572. // Replace a masked intrinsic with an older unmasked intrinsic.
  1573. static Value *UpgradeX86MaskedShift(IRBuilder<> &Builder, CallBase &CI,
  1574. Intrinsic::ID IID) {
  1575. Function *Intrin = Intrinsic::getDeclaration(CI.getModule(), IID);
  1576. Value *Rep = Builder.CreateCall(Intrin,
  1577. { CI.getArgOperand(0), CI.getArgOperand(1) });
  1578. return EmitX86Select(Builder, CI.getArgOperand(3), Rep, CI.getArgOperand(2));
  1579. }
  1580. static Value* upgradeMaskedMove(IRBuilder<> &Builder, CallBase &CI) {
  1581. Value* A = CI.getArgOperand(0);
  1582. Value* B = CI.getArgOperand(1);
  1583. Value* Src = CI.getArgOperand(2);
  1584. Value* Mask = CI.getArgOperand(3);
  1585. Value* AndNode = Builder.CreateAnd(Mask, APInt(8, 1));
  1586. Value* Cmp = Builder.CreateIsNotNull(AndNode);
  1587. Value* Extract1 = Builder.CreateExtractElement(B, (uint64_t)0);
  1588. Value* Extract2 = Builder.CreateExtractElement(Src, (uint64_t)0);
  1589. Value* Select = Builder.CreateSelect(Cmp, Extract1, Extract2);
  1590. return Builder.CreateInsertElement(A, Select, (uint64_t)0);
  1591. }
  1592. static Value* UpgradeMaskToInt(IRBuilder<> &Builder, CallBase &CI) {
  1593. Value* Op = CI.getArgOperand(0);
  1594. Type* ReturnOp = CI.getType();
  1595. unsigned NumElts = cast<FixedVectorType>(CI.getType())->getNumElements();
  1596. Value *Mask = getX86MaskVec(Builder, Op, NumElts);
  1597. return Builder.CreateSExt(Mask, ReturnOp, "vpmovm2");
  1598. }
  1599. // Replace intrinsic with unmasked version and a select.
  1600. static bool upgradeAVX512MaskToSelect(StringRef Name, IRBuilder<> &Builder,
  1601. CallBase &CI, Value *&Rep) {
  1602. Name = Name.substr(12); // Remove avx512.mask.
  1603. unsigned VecWidth = CI.getType()->getPrimitiveSizeInBits();
  1604. unsigned EltWidth = CI.getType()->getScalarSizeInBits();
  1605. Intrinsic::ID IID;
  1606. if (Name.startswith("max.p")) {
  1607. if (VecWidth == 128 && EltWidth == 32)
  1608. IID = Intrinsic::x86_sse_max_ps;
  1609. else if (VecWidth == 128 && EltWidth == 64)
  1610. IID = Intrinsic::x86_sse2_max_pd;
  1611. else if (VecWidth == 256 && EltWidth == 32)
  1612. IID = Intrinsic::x86_avx_max_ps_256;
  1613. else if (VecWidth == 256 && EltWidth == 64)
  1614. IID = Intrinsic::x86_avx_max_pd_256;
  1615. else
  1616. llvm_unreachable("Unexpected intrinsic");
  1617. } else if (Name.startswith("min.p")) {
  1618. if (VecWidth == 128 && EltWidth == 32)
  1619. IID = Intrinsic::x86_sse_min_ps;
  1620. else if (VecWidth == 128 && EltWidth == 64)
  1621. IID = Intrinsic::x86_sse2_min_pd;
  1622. else if (VecWidth == 256 && EltWidth == 32)
  1623. IID = Intrinsic::x86_avx_min_ps_256;
  1624. else if (VecWidth == 256 && EltWidth == 64)
  1625. IID = Intrinsic::x86_avx_min_pd_256;
  1626. else
  1627. llvm_unreachable("Unexpected intrinsic");
  1628. } else if (Name.startswith("pshuf.b.")) {
  1629. if (VecWidth == 128)
  1630. IID = Intrinsic::x86_ssse3_pshuf_b_128;
  1631. else if (VecWidth == 256)
  1632. IID = Intrinsic::x86_avx2_pshuf_b;
  1633. else if (VecWidth == 512)
  1634. IID = Intrinsic::x86_avx512_pshuf_b_512;
  1635. else
  1636. llvm_unreachable("Unexpected intrinsic");
  1637. } else if (Name.startswith("pmul.hr.sw.")) {
  1638. if (VecWidth == 128)
  1639. IID = Intrinsic::x86_ssse3_pmul_hr_sw_128;
  1640. else if (VecWidth == 256)
  1641. IID = Intrinsic::x86_avx2_pmul_hr_sw;
  1642. else if (VecWidth == 512)
  1643. IID = Intrinsic::x86_avx512_pmul_hr_sw_512;
  1644. else
  1645. llvm_unreachable("Unexpected intrinsic");
  1646. } else if (Name.startswith("pmulh.w.")) {
  1647. if (VecWidth == 128)
  1648. IID = Intrinsic::x86_sse2_pmulh_w;
  1649. else if (VecWidth == 256)
  1650. IID = Intrinsic::x86_avx2_pmulh_w;
  1651. else if (VecWidth == 512)
  1652. IID = Intrinsic::x86_avx512_pmulh_w_512;
  1653. else
  1654. llvm_unreachable("Unexpected intrinsic");
  1655. } else if (Name.startswith("pmulhu.w.")) {
  1656. if (VecWidth == 128)
  1657. IID = Intrinsic::x86_sse2_pmulhu_w;
  1658. else if (VecWidth == 256)
  1659. IID = Intrinsic::x86_avx2_pmulhu_w;
  1660. else if (VecWidth == 512)
  1661. IID = Intrinsic::x86_avx512_pmulhu_w_512;
  1662. else
  1663. llvm_unreachable("Unexpected intrinsic");
  1664. } else if (Name.startswith("pmaddw.d.")) {
  1665. if (VecWidth == 128)
  1666. IID = Intrinsic::x86_sse2_pmadd_wd;
  1667. else if (VecWidth == 256)
  1668. IID = Intrinsic::x86_avx2_pmadd_wd;
  1669. else if (VecWidth == 512)
  1670. IID = Intrinsic::x86_avx512_pmaddw_d_512;
  1671. else
  1672. llvm_unreachable("Unexpected intrinsic");
  1673. } else if (Name.startswith("pmaddubs.w.")) {
  1674. if (VecWidth == 128)
  1675. IID = Intrinsic::x86_ssse3_pmadd_ub_sw_128;
  1676. else if (VecWidth == 256)
  1677. IID = Intrinsic::x86_avx2_pmadd_ub_sw;
  1678. else if (VecWidth == 512)
  1679. IID = Intrinsic::x86_avx512_pmaddubs_w_512;
  1680. else
  1681. llvm_unreachable("Unexpected intrinsic");
  1682. } else if (Name.startswith("packsswb.")) {
  1683. if (VecWidth == 128)
  1684. IID = Intrinsic::x86_sse2_packsswb_128;
  1685. else if (VecWidth == 256)
  1686. IID = Intrinsic::x86_avx2_packsswb;
  1687. else if (VecWidth == 512)
  1688. IID = Intrinsic::x86_avx512_packsswb_512;
  1689. else
  1690. llvm_unreachable("Unexpected intrinsic");
  1691. } else if (Name.startswith("packssdw.")) {
  1692. if (VecWidth == 128)
  1693. IID = Intrinsic::x86_sse2_packssdw_128;
  1694. else if (VecWidth == 256)
  1695. IID = Intrinsic::x86_avx2_packssdw;
  1696. else if (VecWidth == 512)
  1697. IID = Intrinsic::x86_avx512_packssdw_512;
  1698. else
  1699. llvm_unreachable("Unexpected intrinsic");
  1700. } else if (Name.startswith("packuswb.")) {
  1701. if (VecWidth == 128)
  1702. IID = Intrinsic::x86_sse2_packuswb_128;
  1703. else if (VecWidth == 256)
  1704. IID = Intrinsic::x86_avx2_packuswb;
  1705. else if (VecWidth == 512)
  1706. IID = Intrinsic::x86_avx512_packuswb_512;
  1707. else
  1708. llvm_unreachable("Unexpected intrinsic");
  1709. } else if (Name.startswith("packusdw.")) {
  1710. if (VecWidth == 128)
  1711. IID = Intrinsic::x86_sse41_packusdw;
  1712. else if (VecWidth == 256)
  1713. IID = Intrinsic::x86_avx2_packusdw;
  1714. else if (VecWidth == 512)
  1715. IID = Intrinsic::x86_avx512_packusdw_512;
  1716. else
  1717. llvm_unreachable("Unexpected intrinsic");
  1718. } else if (Name.startswith("vpermilvar.")) {
  1719. if (VecWidth == 128 && EltWidth == 32)
  1720. IID = Intrinsic::x86_avx_vpermilvar_ps;
  1721. else if (VecWidth == 128 && EltWidth == 64)
  1722. IID = Intrinsic::x86_avx_vpermilvar_pd;
  1723. else if (VecWidth == 256 && EltWidth == 32)
  1724. IID = Intrinsic::x86_avx_vpermilvar_ps_256;
  1725. else if (VecWidth == 256 && EltWidth == 64)
  1726. IID = Intrinsic::x86_avx_vpermilvar_pd_256;
  1727. else if (VecWidth == 512 && EltWidth == 32)
  1728. IID = Intrinsic::x86_avx512_vpermilvar_ps_512;
  1729. else if (VecWidth == 512 && EltWidth == 64)
  1730. IID = Intrinsic::x86_avx512_vpermilvar_pd_512;
  1731. else
  1732. llvm_unreachable("Unexpected intrinsic");
  1733. } else if (Name == "cvtpd2dq.256") {
  1734. IID = Intrinsic::x86_avx_cvt_pd2dq_256;
  1735. } else if (Name == "cvtpd2ps.256") {
  1736. IID = Intrinsic::x86_avx_cvt_pd2_ps_256;
  1737. } else if (Name == "cvttpd2dq.256") {
  1738. IID = Intrinsic::x86_avx_cvtt_pd2dq_256;
  1739. } else if (Name == "cvttps2dq.128") {
  1740. IID = Intrinsic::x86_sse2_cvttps2dq;
  1741. } else if (Name == "cvttps2dq.256") {
  1742. IID = Intrinsic::x86_avx_cvtt_ps2dq_256;
  1743. } else if (Name.startswith("permvar.")) {
  1744. bool IsFloat = CI.getType()->isFPOrFPVectorTy();
  1745. if (VecWidth == 256 && EltWidth == 32 && IsFloat)
  1746. IID = Intrinsic::x86_avx2_permps;
  1747. else if (VecWidth == 256 && EltWidth == 32 && !IsFloat)
  1748. IID = Intrinsic::x86_avx2_permd;
  1749. else if (VecWidth == 256 && EltWidth == 64 && IsFloat)
  1750. IID = Intrinsic::x86_avx512_permvar_df_256;
  1751. else if (VecWidth == 256 && EltWidth == 64 && !IsFloat)
  1752. IID = Intrinsic::x86_avx512_permvar_di_256;
  1753. else if (VecWidth == 512 && EltWidth == 32 && IsFloat)
  1754. IID = Intrinsic::x86_avx512_permvar_sf_512;
  1755. else if (VecWidth == 512 && EltWidth == 32 && !IsFloat)
  1756. IID = Intrinsic::x86_avx512_permvar_si_512;
  1757. else if (VecWidth == 512 && EltWidth == 64 && IsFloat)
  1758. IID = Intrinsic::x86_avx512_permvar_df_512;
  1759. else if (VecWidth == 512 && EltWidth == 64 && !IsFloat)
  1760. IID = Intrinsic::x86_avx512_permvar_di_512;
  1761. else if (VecWidth == 128 && EltWidth == 16)
  1762. IID = Intrinsic::x86_avx512_permvar_hi_128;
  1763. else if (VecWidth == 256 && EltWidth == 16)
  1764. IID = Intrinsic::x86_avx512_permvar_hi_256;
  1765. else if (VecWidth == 512 && EltWidth == 16)
  1766. IID = Intrinsic::x86_avx512_permvar_hi_512;
  1767. else if (VecWidth == 128 && EltWidth == 8)
  1768. IID = Intrinsic::x86_avx512_permvar_qi_128;
  1769. else if (VecWidth == 256 && EltWidth == 8)
  1770. IID = Intrinsic::x86_avx512_permvar_qi_256;
  1771. else if (VecWidth == 512 && EltWidth == 8)
  1772. IID = Intrinsic::x86_avx512_permvar_qi_512;
  1773. else
  1774. llvm_unreachable("Unexpected intrinsic");
  1775. } else if (Name.startswith("dbpsadbw.")) {
  1776. if (VecWidth == 128)
  1777. IID = Intrinsic::x86_avx512_dbpsadbw_128;
  1778. else if (VecWidth == 256)
  1779. IID = Intrinsic::x86_avx512_dbpsadbw_256;
  1780. else if (VecWidth == 512)
  1781. IID = Intrinsic::x86_avx512_dbpsadbw_512;
  1782. else
  1783. llvm_unreachable("Unexpected intrinsic");
  1784. } else if (Name.startswith("pmultishift.qb.")) {
  1785. if (VecWidth == 128)
  1786. IID = Intrinsic::x86_avx512_pmultishift_qb_128;
  1787. else if (VecWidth == 256)
  1788. IID = Intrinsic::x86_avx512_pmultishift_qb_256;
  1789. else if (VecWidth == 512)
  1790. IID = Intrinsic::x86_avx512_pmultishift_qb_512;
  1791. else
  1792. llvm_unreachable("Unexpected intrinsic");
  1793. } else if (Name.startswith("conflict.")) {
  1794. if (Name[9] == 'd' && VecWidth == 128)
  1795. IID = Intrinsic::x86_avx512_conflict_d_128;
  1796. else if (Name[9] == 'd' && VecWidth == 256)
  1797. IID = Intrinsic::x86_avx512_conflict_d_256;
  1798. else if (Name[9] == 'd' && VecWidth == 512)
  1799. IID = Intrinsic::x86_avx512_conflict_d_512;
  1800. else if (Name[9] == 'q' && VecWidth == 128)
  1801. IID = Intrinsic::x86_avx512_conflict_q_128;
  1802. else if (Name[9] == 'q' && VecWidth == 256)
  1803. IID = Intrinsic::x86_avx512_conflict_q_256;
  1804. else if (Name[9] == 'q' && VecWidth == 512)
  1805. IID = Intrinsic::x86_avx512_conflict_q_512;
  1806. else
  1807. llvm_unreachable("Unexpected intrinsic");
  1808. } else if (Name.startswith("pavg.")) {
  1809. if (Name[5] == 'b' && VecWidth == 128)
  1810. IID = Intrinsic::x86_sse2_pavg_b;
  1811. else if (Name[5] == 'b' && VecWidth == 256)
  1812. IID = Intrinsic::x86_avx2_pavg_b;
  1813. else if (Name[5] == 'b' && VecWidth == 512)
  1814. IID = Intrinsic::x86_avx512_pavg_b_512;
  1815. else if (Name[5] == 'w' && VecWidth == 128)
  1816. IID = Intrinsic::x86_sse2_pavg_w;
  1817. else if (Name[5] == 'w' && VecWidth == 256)
  1818. IID = Intrinsic::x86_avx2_pavg_w;
  1819. else if (Name[5] == 'w' && VecWidth == 512)
  1820. IID = Intrinsic::x86_avx512_pavg_w_512;
  1821. else
  1822. llvm_unreachable("Unexpected intrinsic");
  1823. } else
  1824. return false;
  1825. SmallVector<Value *, 4> Args(CI.args());
  1826. Args.pop_back();
  1827. Args.pop_back();
  1828. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI.getModule(), IID),
  1829. Args);
  1830. unsigned NumArgs = CI.arg_size();
  1831. Rep = EmitX86Select(Builder, CI.getArgOperand(NumArgs - 1), Rep,
  1832. CI.getArgOperand(NumArgs - 2));
  1833. return true;
  1834. }
  1835. /// Upgrade comment in call to inline asm that represents an objc retain release
  1836. /// marker.
  1837. void llvm::UpgradeInlineAsmString(std::string *AsmStr) {
  1838. size_t Pos;
  1839. if (AsmStr->find("mov\tfp") == 0 &&
  1840. AsmStr->find("objc_retainAutoreleaseReturnValue") != std::string::npos &&
  1841. (Pos = AsmStr->find("# marker")) != std::string::npos) {
  1842. AsmStr->replace(Pos, 1, ";");
  1843. }
  1844. }
  1845. static Value *UpgradeARMIntrinsicCall(StringRef Name, CallBase *CI, Function *F,
  1846. IRBuilder<> &Builder) {
  1847. if (Name == "mve.vctp64.old") {
  1848. // Replace the old v4i1 vctp64 with a v2i1 vctp and predicate-casts to the
  1849. // correct type.
  1850. Value *VCTP = Builder.CreateCall(
  1851. Intrinsic::getDeclaration(F->getParent(), Intrinsic::arm_mve_vctp64),
  1852. CI->getArgOperand(0), CI->getName());
  1853. Value *C1 = Builder.CreateCall(
  1854. Intrinsic::getDeclaration(
  1855. F->getParent(), Intrinsic::arm_mve_pred_v2i,
  1856. {VectorType::get(Builder.getInt1Ty(), 2, false)}),
  1857. VCTP);
  1858. return Builder.CreateCall(
  1859. Intrinsic::getDeclaration(
  1860. F->getParent(), Intrinsic::arm_mve_pred_i2v,
  1861. {VectorType::get(Builder.getInt1Ty(), 4, false)}),
  1862. C1);
  1863. } else if (Name == "mve.mull.int.predicated.v2i64.v4i32.v4i1" ||
  1864. Name == "mve.vqdmull.predicated.v2i64.v4i32.v4i1" ||
  1865. Name == "mve.vldr.gather.base.predicated.v2i64.v2i64.v4i1" ||
  1866. Name == "mve.vldr.gather.base.wb.predicated.v2i64.v2i64.v4i1" ||
  1867. Name == "mve.vldr.gather.offset.predicated.v2i64.p0i64.v2i64.v4i1" ||
  1868. Name == "mve.vstr.scatter.base.predicated.v2i64.v2i64.v4i1" ||
  1869. Name == "mve.vstr.scatter.base.wb.predicated.v2i64.v2i64.v4i1" ||
  1870. Name == "mve.vstr.scatter.offset.predicated.p0i64.v2i64.v2i64.v4i1" ||
  1871. Name == "cde.vcx1q.predicated.v2i64.v4i1" ||
  1872. Name == "cde.vcx1qa.predicated.v2i64.v4i1" ||
  1873. Name == "cde.vcx2q.predicated.v2i64.v4i1" ||
  1874. Name == "cde.vcx2qa.predicated.v2i64.v4i1" ||
  1875. Name == "cde.vcx3q.predicated.v2i64.v4i1" ||
  1876. Name == "cde.vcx3qa.predicated.v2i64.v4i1") {
  1877. std::vector<Type *> Tys;
  1878. unsigned ID = CI->getIntrinsicID();
  1879. Type *V2I1Ty = FixedVectorType::get(Builder.getInt1Ty(), 2);
  1880. switch (ID) {
  1881. case Intrinsic::arm_mve_mull_int_predicated:
  1882. case Intrinsic::arm_mve_vqdmull_predicated:
  1883. case Intrinsic::arm_mve_vldr_gather_base_predicated:
  1884. Tys = {CI->getType(), CI->getOperand(0)->getType(), V2I1Ty};
  1885. break;
  1886. case Intrinsic::arm_mve_vldr_gather_base_wb_predicated:
  1887. case Intrinsic::arm_mve_vstr_scatter_base_predicated:
  1888. case Intrinsic::arm_mve_vstr_scatter_base_wb_predicated:
  1889. Tys = {CI->getOperand(0)->getType(), CI->getOperand(0)->getType(),
  1890. V2I1Ty};
  1891. break;
  1892. case Intrinsic::arm_mve_vldr_gather_offset_predicated:
  1893. Tys = {CI->getType(), CI->getOperand(0)->getType(),
  1894. CI->getOperand(1)->getType(), V2I1Ty};
  1895. break;
  1896. case Intrinsic::arm_mve_vstr_scatter_offset_predicated:
  1897. Tys = {CI->getOperand(0)->getType(), CI->getOperand(1)->getType(),
  1898. CI->getOperand(2)->getType(), V2I1Ty};
  1899. break;
  1900. case Intrinsic::arm_cde_vcx1q_predicated:
  1901. case Intrinsic::arm_cde_vcx1qa_predicated:
  1902. case Intrinsic::arm_cde_vcx2q_predicated:
  1903. case Intrinsic::arm_cde_vcx2qa_predicated:
  1904. case Intrinsic::arm_cde_vcx3q_predicated:
  1905. case Intrinsic::arm_cde_vcx3qa_predicated:
  1906. Tys = {CI->getOperand(1)->getType(), V2I1Ty};
  1907. break;
  1908. default:
  1909. llvm_unreachable("Unhandled Intrinsic!");
  1910. }
  1911. std::vector<Value *> Ops;
  1912. for (Value *Op : CI->args()) {
  1913. Type *Ty = Op->getType();
  1914. if (Ty->getScalarSizeInBits() == 1) {
  1915. Value *C1 = Builder.CreateCall(
  1916. Intrinsic::getDeclaration(
  1917. F->getParent(), Intrinsic::arm_mve_pred_v2i,
  1918. {VectorType::get(Builder.getInt1Ty(), 4, false)}),
  1919. Op);
  1920. Op = Builder.CreateCall(
  1921. Intrinsic::getDeclaration(F->getParent(),
  1922. Intrinsic::arm_mve_pred_i2v, {V2I1Ty}),
  1923. C1);
  1924. }
  1925. Ops.push_back(Op);
  1926. }
  1927. Function *Fn = Intrinsic::getDeclaration(F->getParent(), ID, Tys);
  1928. return Builder.CreateCall(Fn, Ops, CI->getName());
  1929. }
  1930. llvm_unreachable("Unknown function for ARM CallBase upgrade.");
  1931. }
  1932. /// Upgrade a call to an old intrinsic. All argument and return casting must be
  1933. /// provided to seamlessly integrate with existing context.
  1934. void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
  1935. // Note dyn_cast to Function is not quite the same as getCalledFunction, which
  1936. // checks the callee's function type matches. It's likely we need to handle
  1937. // type changes here.
  1938. Function *F = dyn_cast<Function>(CI->getCalledOperand());
  1939. if (!F)
  1940. return;
  1941. LLVMContext &C = CI->getContext();
  1942. IRBuilder<> Builder(C);
  1943. Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
  1944. if (!NewFn) {
  1945. // Get the Function's name.
  1946. StringRef Name = F->getName();
  1947. assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
  1948. Name = Name.substr(5);
  1949. bool IsX86 = Name.startswith("x86.");
  1950. if (IsX86)
  1951. Name = Name.substr(4);
  1952. bool IsNVVM = Name.startswith("nvvm.");
  1953. if (IsNVVM)
  1954. Name = Name.substr(5);
  1955. bool IsARM = Name.startswith("arm.");
  1956. if (IsARM)
  1957. Name = Name.substr(4);
  1958. if (IsX86 && Name.startswith("sse4a.movnt.")) {
  1959. Module *M = F->getParent();
  1960. SmallVector<Metadata *, 1> Elts;
  1961. Elts.push_back(
  1962. ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
  1963. MDNode *Node = MDNode::get(C, Elts);
  1964. Value *Arg0 = CI->getArgOperand(0);
  1965. Value *Arg1 = CI->getArgOperand(1);
  1966. // Nontemporal (unaligned) store of the 0'th element of the float/double
  1967. // vector.
  1968. Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
  1969. PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
  1970. Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
  1971. Value *Extract =
  1972. Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
  1973. StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, Align(1));
  1974. SI->setMetadata(M->getMDKindID("nontemporal"), Node);
  1975. // Remove intrinsic.
  1976. CI->eraseFromParent();
  1977. return;
  1978. }
  1979. if (IsX86 && (Name.startswith("avx.movnt.") ||
  1980. Name.startswith("avx512.storent."))) {
  1981. Module *M = F->getParent();
  1982. SmallVector<Metadata *, 1> Elts;
  1983. Elts.push_back(
  1984. ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
  1985. MDNode *Node = MDNode::get(C, Elts);
  1986. Value *Arg0 = CI->getArgOperand(0);
  1987. Value *Arg1 = CI->getArgOperand(1);
  1988. // Convert the type of the pointer to a pointer to the stored type.
  1989. Value *BC = Builder.CreateBitCast(Arg0,
  1990. PointerType::getUnqual(Arg1->getType()),
  1991. "cast");
  1992. StoreInst *SI = Builder.CreateAlignedStore(
  1993. Arg1, BC,
  1994. Align(Arg1->getType()->getPrimitiveSizeInBits().getFixedValue() / 8));
  1995. SI->setMetadata(M->getMDKindID("nontemporal"), Node);
  1996. // Remove intrinsic.
  1997. CI->eraseFromParent();
  1998. return;
  1999. }
  2000. if (IsX86 && Name == "sse2.storel.dq") {
  2001. Value *Arg0 = CI->getArgOperand(0);
  2002. Value *Arg1 = CI->getArgOperand(1);
  2003. auto *NewVecTy = FixedVectorType::get(Type::getInt64Ty(C), 2);
  2004. Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
  2005. Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
  2006. Value *BC = Builder.CreateBitCast(Arg0,
  2007. PointerType::getUnqual(Elt->getType()),
  2008. "cast");
  2009. Builder.CreateAlignedStore(Elt, BC, Align(1));
  2010. // Remove intrinsic.
  2011. CI->eraseFromParent();
  2012. return;
  2013. }
  2014. if (IsX86 && (Name.startswith("sse.storeu.") ||
  2015. Name.startswith("sse2.storeu.") ||
  2016. Name.startswith("avx.storeu."))) {
  2017. Value *Arg0 = CI->getArgOperand(0);
  2018. Value *Arg1 = CI->getArgOperand(1);
  2019. Arg0 = Builder.CreateBitCast(Arg0,
  2020. PointerType::getUnqual(Arg1->getType()),
  2021. "cast");
  2022. Builder.CreateAlignedStore(Arg1, Arg0, Align(1));
  2023. // Remove intrinsic.
  2024. CI->eraseFromParent();
  2025. return;
  2026. }
  2027. if (IsX86 && Name == "avx512.mask.store.ss") {
  2028. Value *Mask = Builder.CreateAnd(CI->getArgOperand(2), Builder.getInt8(1));
  2029. UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
  2030. Mask, false);
  2031. // Remove intrinsic.
  2032. CI->eraseFromParent();
  2033. return;
  2034. }
  2035. if (IsX86 && (Name.startswith("avx512.mask.store"))) {
  2036. // "avx512.mask.storeu." or "avx512.mask.store."
  2037. bool Aligned = Name[17] != 'u'; // "avx512.mask.storeu".
  2038. UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
  2039. CI->getArgOperand(2), Aligned);
  2040. // Remove intrinsic.
  2041. CI->eraseFromParent();
  2042. return;
  2043. }
  2044. Value *Rep;
  2045. // Upgrade packed integer vector compare intrinsics to compare instructions.
  2046. if (IsX86 && (Name.startswith("sse2.pcmp") ||
  2047. Name.startswith("avx2.pcmp"))) {
  2048. // "sse2.pcpmpeq." "sse2.pcmpgt." "avx2.pcmpeq." or "avx2.pcmpgt."
  2049. bool CmpEq = Name[9] == 'e';
  2050. Rep = Builder.CreateICmp(CmpEq ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_SGT,
  2051. CI->getArgOperand(0), CI->getArgOperand(1));
  2052. Rep = Builder.CreateSExt(Rep, CI->getType(), "");
  2053. } else if (IsX86 && (Name.startswith("avx512.broadcastm"))) {
  2054. Type *ExtTy = Type::getInt32Ty(C);
  2055. if (CI->getOperand(0)->getType()->isIntegerTy(8))
  2056. ExtTy = Type::getInt64Ty(C);
  2057. unsigned NumElts = CI->getType()->getPrimitiveSizeInBits() /
  2058. ExtTy->getPrimitiveSizeInBits();
  2059. Rep = Builder.CreateZExt(CI->getArgOperand(0), ExtTy);
  2060. Rep = Builder.CreateVectorSplat(NumElts, Rep);
  2061. } else if (IsX86 && (Name == "sse.sqrt.ss" ||
  2062. Name == "sse2.sqrt.sd")) {
  2063. Value *Vec = CI->getArgOperand(0);
  2064. Value *Elt0 = Builder.CreateExtractElement(Vec, (uint64_t)0);
  2065. Function *Intr = Intrinsic::getDeclaration(F->getParent(),
  2066. Intrinsic::sqrt, Elt0->getType());
  2067. Elt0 = Builder.CreateCall(Intr, Elt0);
  2068. Rep = Builder.CreateInsertElement(Vec, Elt0, (uint64_t)0);
  2069. } else if (IsX86 && (Name.startswith("avx.sqrt.p") ||
  2070. Name.startswith("sse2.sqrt.p") ||
  2071. Name.startswith("sse.sqrt.p"))) {
  2072. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(),
  2073. Intrinsic::sqrt,
  2074. CI->getType()),
  2075. {CI->getArgOperand(0)});
  2076. } else if (IsX86 && (Name.startswith("avx512.mask.sqrt.p"))) {
  2077. if (CI->arg_size() == 4 &&
  2078. (!isa<ConstantInt>(CI->getArgOperand(3)) ||
  2079. cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue() != 4)) {
  2080. Intrinsic::ID IID = Name[18] == 's' ? Intrinsic::x86_avx512_sqrt_ps_512
  2081. : Intrinsic::x86_avx512_sqrt_pd_512;
  2082. Value *Args[] = { CI->getArgOperand(0), CI->getArgOperand(3) };
  2083. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(),
  2084. IID), Args);
  2085. } else {
  2086. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(),
  2087. Intrinsic::sqrt,
  2088. CI->getType()),
  2089. {CI->getArgOperand(0)});
  2090. }
  2091. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2092. CI->getArgOperand(1));
  2093. } else if (IsX86 && (Name.startswith("avx512.ptestm") ||
  2094. Name.startswith("avx512.ptestnm"))) {
  2095. Value *Op0 = CI->getArgOperand(0);
  2096. Value *Op1 = CI->getArgOperand(1);
  2097. Value *Mask = CI->getArgOperand(2);
  2098. Rep = Builder.CreateAnd(Op0, Op1);
  2099. llvm::Type *Ty = Op0->getType();
  2100. Value *Zero = llvm::Constant::getNullValue(Ty);
  2101. ICmpInst::Predicate Pred =
  2102. Name.startswith("avx512.ptestm") ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
  2103. Rep = Builder.CreateICmp(Pred, Rep, Zero);
  2104. Rep = ApplyX86MaskOn1BitsVec(Builder, Rep, Mask);
  2105. } else if (IsX86 && (Name.startswith("avx512.mask.pbroadcast"))){
  2106. unsigned NumElts = cast<FixedVectorType>(CI->getArgOperand(1)->getType())
  2107. ->getNumElements();
  2108. Rep = Builder.CreateVectorSplat(NumElts, CI->getArgOperand(0));
  2109. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2110. CI->getArgOperand(1));
  2111. } else if (IsX86 && (Name.startswith("avx512.kunpck"))) {
  2112. unsigned NumElts = CI->getType()->getScalarSizeInBits();
  2113. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), NumElts);
  2114. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), NumElts);
  2115. int Indices[64];
  2116. for (unsigned i = 0; i != NumElts; ++i)
  2117. Indices[i] = i;
  2118. // First extract half of each vector. This gives better codegen than
  2119. // doing it in a single shuffle.
  2120. LHS =
  2121. Builder.CreateShuffleVector(LHS, LHS, ArrayRef(Indices, NumElts / 2));
  2122. RHS =
  2123. Builder.CreateShuffleVector(RHS, RHS, ArrayRef(Indices, NumElts / 2));
  2124. // Concat the vectors.
  2125. // NOTE: Operands have to be swapped to match intrinsic definition.
  2126. Rep = Builder.CreateShuffleVector(RHS, LHS, ArrayRef(Indices, NumElts));
  2127. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2128. } else if (IsX86 && Name == "avx512.kand.w") {
  2129. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2130. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
  2131. Rep = Builder.CreateAnd(LHS, RHS);
  2132. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2133. } else if (IsX86 && Name == "avx512.kandn.w") {
  2134. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2135. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
  2136. LHS = Builder.CreateNot(LHS);
  2137. Rep = Builder.CreateAnd(LHS, RHS);
  2138. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2139. } else if (IsX86 && Name == "avx512.kor.w") {
  2140. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2141. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
  2142. Rep = Builder.CreateOr(LHS, RHS);
  2143. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2144. } else if (IsX86 && Name == "avx512.kxor.w") {
  2145. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2146. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
  2147. Rep = Builder.CreateXor(LHS, RHS);
  2148. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2149. } else if (IsX86 && Name == "avx512.kxnor.w") {
  2150. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2151. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
  2152. LHS = Builder.CreateNot(LHS);
  2153. Rep = Builder.CreateXor(LHS, RHS);
  2154. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2155. } else if (IsX86 && Name == "avx512.knot.w") {
  2156. Rep = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2157. Rep = Builder.CreateNot(Rep);
  2158. Rep = Builder.CreateBitCast(Rep, CI->getType());
  2159. } else if (IsX86 &&
  2160. (Name == "avx512.kortestz.w" || Name == "avx512.kortestc.w")) {
  2161. Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
  2162. Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
  2163. Rep = Builder.CreateOr(LHS, RHS);
  2164. Rep = Builder.CreateBitCast(Rep, Builder.getInt16Ty());
  2165. Value *C;
  2166. if (Name[14] == 'c')
  2167. C = ConstantInt::getAllOnesValue(Builder.getInt16Ty());
  2168. else
  2169. C = ConstantInt::getNullValue(Builder.getInt16Ty());
  2170. Rep = Builder.CreateICmpEQ(Rep, C);
  2171. Rep = Builder.CreateZExt(Rep, Builder.getInt32Ty());
  2172. } else if (IsX86 && (Name == "sse.add.ss" || Name == "sse2.add.sd" ||
  2173. Name == "sse.sub.ss" || Name == "sse2.sub.sd" ||
  2174. Name == "sse.mul.ss" || Name == "sse2.mul.sd" ||
  2175. Name == "sse.div.ss" || Name == "sse2.div.sd")) {
  2176. Type *I32Ty = Type::getInt32Ty(C);
  2177. Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
  2178. ConstantInt::get(I32Ty, 0));
  2179. Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
  2180. ConstantInt::get(I32Ty, 0));
  2181. Value *EltOp;
  2182. if (Name.contains(".add."))
  2183. EltOp = Builder.CreateFAdd(Elt0, Elt1);
  2184. else if (Name.contains(".sub."))
  2185. EltOp = Builder.CreateFSub(Elt0, Elt1);
  2186. else if (Name.contains(".mul."))
  2187. EltOp = Builder.CreateFMul(Elt0, Elt1);
  2188. else
  2189. EltOp = Builder.CreateFDiv(Elt0, Elt1);
  2190. Rep = Builder.CreateInsertElement(CI->getArgOperand(0), EltOp,
  2191. ConstantInt::get(I32Ty, 0));
  2192. } else if (IsX86 && Name.startswith("avx512.mask.pcmp")) {
  2193. // "avx512.mask.pcmpeq." or "avx512.mask.pcmpgt."
  2194. bool CmpEq = Name[16] == 'e';
  2195. Rep = upgradeMaskedCompare(Builder, *CI, CmpEq ? 0 : 6, true);
  2196. } else if (IsX86 && Name.startswith("avx512.mask.vpshufbitqmb.")) {
  2197. Type *OpTy = CI->getArgOperand(0)->getType();
  2198. unsigned VecWidth = OpTy->getPrimitiveSizeInBits();
  2199. Intrinsic::ID IID;
  2200. switch (VecWidth) {
  2201. default: llvm_unreachable("Unexpected intrinsic");
  2202. case 128: IID = Intrinsic::x86_avx512_vpshufbitqmb_128; break;
  2203. case 256: IID = Intrinsic::x86_avx512_vpshufbitqmb_256; break;
  2204. case 512: IID = Intrinsic::x86_avx512_vpshufbitqmb_512; break;
  2205. }
  2206. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2207. { CI->getOperand(0), CI->getArgOperand(1) });
  2208. Rep = ApplyX86MaskOn1BitsVec(Builder, Rep, CI->getArgOperand(2));
  2209. } else if (IsX86 && Name.startswith("avx512.mask.fpclass.p")) {
  2210. Type *OpTy = CI->getArgOperand(0)->getType();
  2211. unsigned VecWidth = OpTy->getPrimitiveSizeInBits();
  2212. unsigned EltWidth = OpTy->getScalarSizeInBits();
  2213. Intrinsic::ID IID;
  2214. if (VecWidth == 128 && EltWidth == 32)
  2215. IID = Intrinsic::x86_avx512_fpclass_ps_128;
  2216. else if (VecWidth == 256 && EltWidth == 32)
  2217. IID = Intrinsic::x86_avx512_fpclass_ps_256;
  2218. else if (VecWidth == 512 && EltWidth == 32)
  2219. IID = Intrinsic::x86_avx512_fpclass_ps_512;
  2220. else if (VecWidth == 128 && EltWidth == 64)
  2221. IID = Intrinsic::x86_avx512_fpclass_pd_128;
  2222. else if (VecWidth == 256 && EltWidth == 64)
  2223. IID = Intrinsic::x86_avx512_fpclass_pd_256;
  2224. else if (VecWidth == 512 && EltWidth == 64)
  2225. IID = Intrinsic::x86_avx512_fpclass_pd_512;
  2226. else
  2227. llvm_unreachable("Unexpected intrinsic");
  2228. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2229. { CI->getOperand(0), CI->getArgOperand(1) });
  2230. Rep = ApplyX86MaskOn1BitsVec(Builder, Rep, CI->getArgOperand(2));
  2231. } else if (IsX86 && Name.startswith("avx512.cmp.p")) {
  2232. SmallVector<Value *, 4> Args(CI->args());
  2233. Type *OpTy = Args[0]->getType();
  2234. unsigned VecWidth = OpTy->getPrimitiveSizeInBits();
  2235. unsigned EltWidth = OpTy->getScalarSizeInBits();
  2236. Intrinsic::ID IID;
  2237. if (VecWidth == 128 && EltWidth == 32)
  2238. IID = Intrinsic::x86_avx512_mask_cmp_ps_128;
  2239. else if (VecWidth == 256 && EltWidth == 32)
  2240. IID = Intrinsic::x86_avx512_mask_cmp_ps_256;
  2241. else if (VecWidth == 512 && EltWidth == 32)
  2242. IID = Intrinsic::x86_avx512_mask_cmp_ps_512;
  2243. else if (VecWidth == 128 && EltWidth == 64)
  2244. IID = Intrinsic::x86_avx512_mask_cmp_pd_128;
  2245. else if (VecWidth == 256 && EltWidth == 64)
  2246. IID = Intrinsic::x86_avx512_mask_cmp_pd_256;
  2247. else if (VecWidth == 512 && EltWidth == 64)
  2248. IID = Intrinsic::x86_avx512_mask_cmp_pd_512;
  2249. else
  2250. llvm_unreachable("Unexpected intrinsic");
  2251. Value *Mask = Constant::getAllOnesValue(CI->getType());
  2252. if (VecWidth == 512)
  2253. std::swap(Mask, Args.back());
  2254. Args.push_back(Mask);
  2255. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2256. Args);
  2257. } else if (IsX86 && Name.startswith("avx512.mask.cmp.")) {
  2258. // Integer compare intrinsics.
  2259. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2260. Rep = upgradeMaskedCompare(Builder, *CI, Imm, true);
  2261. } else if (IsX86 && Name.startswith("avx512.mask.ucmp.")) {
  2262. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2263. Rep = upgradeMaskedCompare(Builder, *CI, Imm, false);
  2264. } else if (IsX86 && (Name.startswith("avx512.cvtb2mask.") ||
  2265. Name.startswith("avx512.cvtw2mask.") ||
  2266. Name.startswith("avx512.cvtd2mask.") ||
  2267. Name.startswith("avx512.cvtq2mask."))) {
  2268. Value *Op = CI->getArgOperand(0);
  2269. Value *Zero = llvm::Constant::getNullValue(Op->getType());
  2270. Rep = Builder.CreateICmp(ICmpInst::ICMP_SLT, Op, Zero);
  2271. Rep = ApplyX86MaskOn1BitsVec(Builder, Rep, nullptr);
  2272. } else if(IsX86 && (Name == "ssse3.pabs.b.128" ||
  2273. Name == "ssse3.pabs.w.128" ||
  2274. Name == "ssse3.pabs.d.128" ||
  2275. Name.startswith("avx2.pabs") ||
  2276. Name.startswith("avx512.mask.pabs"))) {
  2277. Rep = upgradeAbs(Builder, *CI);
  2278. } else if (IsX86 && (Name == "sse41.pmaxsb" ||
  2279. Name == "sse2.pmaxs.w" ||
  2280. Name == "sse41.pmaxsd" ||
  2281. Name.startswith("avx2.pmaxs") ||
  2282. Name.startswith("avx512.mask.pmaxs"))) {
  2283. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::smax);
  2284. } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
  2285. Name == "sse41.pmaxuw" ||
  2286. Name == "sse41.pmaxud" ||
  2287. Name.startswith("avx2.pmaxu") ||
  2288. Name.startswith("avx512.mask.pmaxu"))) {
  2289. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::umax);
  2290. } else if (IsX86 && (Name == "sse41.pminsb" ||
  2291. Name == "sse2.pmins.w" ||
  2292. Name == "sse41.pminsd" ||
  2293. Name.startswith("avx2.pmins") ||
  2294. Name.startswith("avx512.mask.pmins"))) {
  2295. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::smin);
  2296. } else if (IsX86 && (Name == "sse2.pminu.b" ||
  2297. Name == "sse41.pminuw" ||
  2298. Name == "sse41.pminud" ||
  2299. Name.startswith("avx2.pminu") ||
  2300. Name.startswith("avx512.mask.pminu"))) {
  2301. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::umin);
  2302. } else if (IsX86 && (Name == "sse2.pmulu.dq" ||
  2303. Name == "avx2.pmulu.dq" ||
  2304. Name == "avx512.pmulu.dq.512" ||
  2305. Name.startswith("avx512.mask.pmulu.dq."))) {
  2306. Rep = upgradePMULDQ(Builder, *CI, /*Signed*/false);
  2307. } else if (IsX86 && (Name == "sse41.pmuldq" ||
  2308. Name == "avx2.pmul.dq" ||
  2309. Name == "avx512.pmul.dq.512" ||
  2310. Name.startswith("avx512.mask.pmul.dq."))) {
  2311. Rep = upgradePMULDQ(Builder, *CI, /*Signed*/true);
  2312. } else if (IsX86 && (Name == "sse.cvtsi2ss" ||
  2313. Name == "sse2.cvtsi2sd" ||
  2314. Name == "sse.cvtsi642ss" ||
  2315. Name == "sse2.cvtsi642sd")) {
  2316. Rep = Builder.CreateSIToFP(
  2317. CI->getArgOperand(1),
  2318. cast<VectorType>(CI->getType())->getElementType());
  2319. Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep, (uint64_t)0);
  2320. } else if (IsX86 && Name == "avx512.cvtusi2sd") {
  2321. Rep = Builder.CreateUIToFP(
  2322. CI->getArgOperand(1),
  2323. cast<VectorType>(CI->getType())->getElementType());
  2324. Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep, (uint64_t)0);
  2325. } else if (IsX86 && Name == "sse2.cvtss2sd") {
  2326. Rep = Builder.CreateExtractElement(CI->getArgOperand(1), (uint64_t)0);
  2327. Rep = Builder.CreateFPExt(
  2328. Rep, cast<VectorType>(CI->getType())->getElementType());
  2329. Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep, (uint64_t)0);
  2330. } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
  2331. Name == "sse2.cvtdq2ps" ||
  2332. Name == "avx.cvtdq2.pd.256" ||
  2333. Name == "avx.cvtdq2.ps.256" ||
  2334. Name.startswith("avx512.mask.cvtdq2pd.") ||
  2335. Name.startswith("avx512.mask.cvtudq2pd.") ||
  2336. Name.startswith("avx512.mask.cvtdq2ps.") ||
  2337. Name.startswith("avx512.mask.cvtudq2ps.") ||
  2338. Name.startswith("avx512.mask.cvtqq2pd.") ||
  2339. Name.startswith("avx512.mask.cvtuqq2pd.") ||
  2340. Name == "avx512.mask.cvtqq2ps.256" ||
  2341. Name == "avx512.mask.cvtqq2ps.512" ||
  2342. Name == "avx512.mask.cvtuqq2ps.256" ||
  2343. Name == "avx512.mask.cvtuqq2ps.512" ||
  2344. Name == "sse2.cvtps2pd" ||
  2345. Name == "avx.cvt.ps2.pd.256" ||
  2346. Name == "avx512.mask.cvtps2pd.128" ||
  2347. Name == "avx512.mask.cvtps2pd.256")) {
  2348. auto *DstTy = cast<FixedVectorType>(CI->getType());
  2349. Rep = CI->getArgOperand(0);
  2350. auto *SrcTy = cast<FixedVectorType>(Rep->getType());
  2351. unsigned NumDstElts = DstTy->getNumElements();
  2352. if (NumDstElts < SrcTy->getNumElements()) {
  2353. assert(NumDstElts == 2 && "Unexpected vector size");
  2354. Rep = Builder.CreateShuffleVector(Rep, Rep, ArrayRef<int>{0, 1});
  2355. }
  2356. bool IsPS2PD = SrcTy->getElementType()->isFloatTy();
  2357. bool IsUnsigned = (StringRef::npos != Name.find("cvtu"));
  2358. if (IsPS2PD)
  2359. Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
  2360. else if (CI->arg_size() == 4 &&
  2361. (!isa<ConstantInt>(CI->getArgOperand(3)) ||
  2362. cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue() != 4)) {
  2363. Intrinsic::ID IID = IsUnsigned ? Intrinsic::x86_avx512_uitofp_round
  2364. : Intrinsic::x86_avx512_sitofp_round;
  2365. Function *F = Intrinsic::getDeclaration(CI->getModule(), IID,
  2366. { DstTy, SrcTy });
  2367. Rep = Builder.CreateCall(F, { Rep, CI->getArgOperand(3) });
  2368. } else {
  2369. Rep = IsUnsigned ? Builder.CreateUIToFP(Rep, DstTy, "cvt")
  2370. : Builder.CreateSIToFP(Rep, DstTy, "cvt");
  2371. }
  2372. if (CI->arg_size() >= 3)
  2373. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2374. CI->getArgOperand(1));
  2375. } else if (IsX86 && (Name.startswith("avx512.mask.vcvtph2ps.") ||
  2376. Name.startswith("vcvtph2ps."))) {
  2377. auto *DstTy = cast<FixedVectorType>(CI->getType());
  2378. Rep = CI->getArgOperand(0);
  2379. auto *SrcTy = cast<FixedVectorType>(Rep->getType());
  2380. unsigned NumDstElts = DstTy->getNumElements();
  2381. if (NumDstElts != SrcTy->getNumElements()) {
  2382. assert(NumDstElts == 4 && "Unexpected vector size");
  2383. Rep = Builder.CreateShuffleVector(Rep, Rep, ArrayRef<int>{0, 1, 2, 3});
  2384. }
  2385. Rep = Builder.CreateBitCast(
  2386. Rep, FixedVectorType::get(Type::getHalfTy(C), NumDstElts));
  2387. Rep = Builder.CreateFPExt(Rep, DstTy, "cvtph2ps");
  2388. if (CI->arg_size() >= 3)
  2389. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2390. CI->getArgOperand(1));
  2391. } else if (IsX86 && Name.startswith("avx512.mask.load")) {
  2392. // "avx512.mask.loadu." or "avx512.mask.load."
  2393. bool Aligned = Name[16] != 'u'; // "avx512.mask.loadu".
  2394. Rep =
  2395. UpgradeMaskedLoad(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
  2396. CI->getArgOperand(2), Aligned);
  2397. } else if (IsX86 && Name.startswith("avx512.mask.expand.load.")) {
  2398. auto *ResultTy = cast<FixedVectorType>(CI->getType());
  2399. Type *PtrTy = ResultTy->getElementType();
  2400. // Cast the pointer to element type.
  2401. Value *Ptr = Builder.CreateBitCast(CI->getOperand(0),
  2402. llvm::PointerType::getUnqual(PtrTy));
  2403. Value *MaskVec = getX86MaskVec(Builder, CI->getArgOperand(2),
  2404. ResultTy->getNumElements());
  2405. Function *ELd = Intrinsic::getDeclaration(F->getParent(),
  2406. Intrinsic::masked_expandload,
  2407. ResultTy);
  2408. Rep = Builder.CreateCall(ELd, { Ptr, MaskVec, CI->getOperand(1) });
  2409. } else if (IsX86 && Name.startswith("avx512.mask.compress.store.")) {
  2410. auto *ResultTy = cast<VectorType>(CI->getArgOperand(1)->getType());
  2411. Type *PtrTy = ResultTy->getElementType();
  2412. // Cast the pointer to element type.
  2413. Value *Ptr = Builder.CreateBitCast(CI->getOperand(0),
  2414. llvm::PointerType::getUnqual(PtrTy));
  2415. Value *MaskVec =
  2416. getX86MaskVec(Builder, CI->getArgOperand(2),
  2417. cast<FixedVectorType>(ResultTy)->getNumElements());
  2418. Function *CSt = Intrinsic::getDeclaration(F->getParent(),
  2419. Intrinsic::masked_compressstore,
  2420. ResultTy);
  2421. Rep = Builder.CreateCall(CSt, { CI->getArgOperand(1), Ptr, MaskVec });
  2422. } else if (IsX86 && (Name.startswith("avx512.mask.compress.") ||
  2423. Name.startswith("avx512.mask.expand."))) {
  2424. auto *ResultTy = cast<FixedVectorType>(CI->getType());
  2425. Value *MaskVec = getX86MaskVec(Builder, CI->getArgOperand(2),
  2426. ResultTy->getNumElements());
  2427. bool IsCompress = Name[12] == 'c';
  2428. Intrinsic::ID IID = IsCompress ? Intrinsic::x86_avx512_mask_compress
  2429. : Intrinsic::x86_avx512_mask_expand;
  2430. Function *Intr = Intrinsic::getDeclaration(F->getParent(), IID, ResultTy);
  2431. Rep = Builder.CreateCall(Intr, { CI->getOperand(0), CI->getOperand(1),
  2432. MaskVec });
  2433. } else if (IsX86 && Name.startswith("xop.vpcom")) {
  2434. bool IsSigned;
  2435. if (Name.endswith("ub") || Name.endswith("uw") || Name.endswith("ud") ||
  2436. Name.endswith("uq"))
  2437. IsSigned = false;
  2438. else if (Name.endswith("b") || Name.endswith("w") || Name.endswith("d") ||
  2439. Name.endswith("q"))
  2440. IsSigned = true;
  2441. else
  2442. llvm_unreachable("Unknown suffix");
  2443. unsigned Imm;
  2444. if (CI->arg_size() == 3) {
  2445. Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2446. } else {
  2447. Name = Name.substr(9); // strip off "xop.vpcom"
  2448. if (Name.startswith("lt"))
  2449. Imm = 0;
  2450. else if (Name.startswith("le"))
  2451. Imm = 1;
  2452. else if (Name.startswith("gt"))
  2453. Imm = 2;
  2454. else if (Name.startswith("ge"))
  2455. Imm = 3;
  2456. else if (Name.startswith("eq"))
  2457. Imm = 4;
  2458. else if (Name.startswith("ne"))
  2459. Imm = 5;
  2460. else if (Name.startswith("false"))
  2461. Imm = 6;
  2462. else if (Name.startswith("true"))
  2463. Imm = 7;
  2464. else
  2465. llvm_unreachable("Unknown condition");
  2466. }
  2467. Rep = upgradeX86vpcom(Builder, *CI, Imm, IsSigned);
  2468. } else if (IsX86 && Name.startswith("xop.vpcmov")) {
  2469. Value *Sel = CI->getArgOperand(2);
  2470. Value *NotSel = Builder.CreateNot(Sel);
  2471. Value *Sel0 = Builder.CreateAnd(CI->getArgOperand(0), Sel);
  2472. Value *Sel1 = Builder.CreateAnd(CI->getArgOperand(1), NotSel);
  2473. Rep = Builder.CreateOr(Sel0, Sel1);
  2474. } else if (IsX86 && (Name.startswith("xop.vprot") ||
  2475. Name.startswith("avx512.prol") ||
  2476. Name.startswith("avx512.mask.prol"))) {
  2477. Rep = upgradeX86Rotate(Builder, *CI, false);
  2478. } else if (IsX86 && (Name.startswith("avx512.pror") ||
  2479. Name.startswith("avx512.mask.pror"))) {
  2480. Rep = upgradeX86Rotate(Builder, *CI, true);
  2481. } else if (IsX86 && (Name.startswith("avx512.vpshld.") ||
  2482. Name.startswith("avx512.mask.vpshld") ||
  2483. Name.startswith("avx512.maskz.vpshld"))) {
  2484. bool ZeroMask = Name[11] == 'z';
  2485. Rep = upgradeX86ConcatShift(Builder, *CI, false, ZeroMask);
  2486. } else if (IsX86 && (Name.startswith("avx512.vpshrd.") ||
  2487. Name.startswith("avx512.mask.vpshrd") ||
  2488. Name.startswith("avx512.maskz.vpshrd"))) {
  2489. bool ZeroMask = Name[11] == 'z';
  2490. Rep = upgradeX86ConcatShift(Builder, *CI, true, ZeroMask);
  2491. } else if (IsX86 && Name == "sse42.crc32.64.8") {
  2492. Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
  2493. Intrinsic::x86_sse42_crc32_32_8);
  2494. Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
  2495. Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
  2496. Rep = Builder.CreateZExt(Rep, CI->getType(), "");
  2497. } else if (IsX86 && (Name.startswith("avx.vbroadcast.s") ||
  2498. Name.startswith("avx512.vbroadcast.s"))) {
  2499. // Replace broadcasts with a series of insertelements.
  2500. auto *VecTy = cast<FixedVectorType>(CI->getType());
  2501. Type *EltTy = VecTy->getElementType();
  2502. unsigned EltNum = VecTy->getNumElements();
  2503. Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
  2504. EltTy->getPointerTo());
  2505. Value *Load = Builder.CreateLoad(EltTy, Cast);
  2506. Type *I32Ty = Type::getInt32Ty(C);
  2507. Rep = PoisonValue::get(VecTy);
  2508. for (unsigned I = 0; I < EltNum; ++I)
  2509. Rep = Builder.CreateInsertElement(Rep, Load,
  2510. ConstantInt::get(I32Ty, I));
  2511. } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
  2512. Name.startswith("sse41.pmovzx") ||
  2513. Name.startswith("avx2.pmovsx") ||
  2514. Name.startswith("avx2.pmovzx") ||
  2515. Name.startswith("avx512.mask.pmovsx") ||
  2516. Name.startswith("avx512.mask.pmovzx"))) {
  2517. auto *DstTy = cast<FixedVectorType>(CI->getType());
  2518. unsigned NumDstElts = DstTy->getNumElements();
  2519. // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
  2520. SmallVector<int, 8> ShuffleMask(NumDstElts);
  2521. for (unsigned i = 0; i != NumDstElts; ++i)
  2522. ShuffleMask[i] = i;
  2523. Value *SV =
  2524. Builder.CreateShuffleVector(CI->getArgOperand(0), ShuffleMask);
  2525. bool DoSext = (StringRef::npos != Name.find("pmovsx"));
  2526. Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
  2527. : Builder.CreateZExt(SV, DstTy);
  2528. // If there are 3 arguments, it's a masked intrinsic so we need a select.
  2529. if (CI->arg_size() == 3)
  2530. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2531. CI->getArgOperand(1));
  2532. } else if (Name == "avx512.mask.pmov.qd.256" ||
  2533. Name == "avx512.mask.pmov.qd.512" ||
  2534. Name == "avx512.mask.pmov.wb.256" ||
  2535. Name == "avx512.mask.pmov.wb.512") {
  2536. Type *Ty = CI->getArgOperand(1)->getType();
  2537. Rep = Builder.CreateTrunc(CI->getArgOperand(0), Ty);
  2538. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2539. CI->getArgOperand(1));
  2540. } else if (IsX86 && (Name.startswith("avx.vbroadcastf128") ||
  2541. Name == "avx2.vbroadcasti128")) {
  2542. // Replace vbroadcastf128/vbroadcasti128 with a vector load+shuffle.
  2543. Type *EltTy = cast<VectorType>(CI->getType())->getElementType();
  2544. unsigned NumSrcElts = 128 / EltTy->getPrimitiveSizeInBits();
  2545. auto *VT = FixedVectorType::get(EltTy, NumSrcElts);
  2546. Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
  2547. PointerType::getUnqual(VT));
  2548. Value *Load = Builder.CreateAlignedLoad(VT, Op, Align(1));
  2549. if (NumSrcElts == 2)
  2550. Rep = Builder.CreateShuffleVector(Load, ArrayRef<int>{0, 1, 0, 1});
  2551. else
  2552. Rep = Builder.CreateShuffleVector(
  2553. Load, ArrayRef<int>{0, 1, 2, 3, 0, 1, 2, 3});
  2554. } else if (IsX86 && (Name.startswith("avx512.mask.shuf.i") ||
  2555. Name.startswith("avx512.mask.shuf.f"))) {
  2556. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2557. Type *VT = CI->getType();
  2558. unsigned NumLanes = VT->getPrimitiveSizeInBits() / 128;
  2559. unsigned NumElementsInLane = 128 / VT->getScalarSizeInBits();
  2560. unsigned ControlBitsMask = NumLanes - 1;
  2561. unsigned NumControlBits = NumLanes / 2;
  2562. SmallVector<int, 8> ShuffleMask(0);
  2563. for (unsigned l = 0; l != NumLanes; ++l) {
  2564. unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask;
  2565. // We actually need the other source.
  2566. if (l >= NumLanes / 2)
  2567. LaneMask += NumLanes;
  2568. for (unsigned i = 0; i != NumElementsInLane; ++i)
  2569. ShuffleMask.push_back(LaneMask * NumElementsInLane + i);
  2570. }
  2571. Rep = Builder.CreateShuffleVector(CI->getArgOperand(0),
  2572. CI->getArgOperand(1), ShuffleMask);
  2573. Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
  2574. CI->getArgOperand(3));
  2575. }else if (IsX86 && (Name.startswith("avx512.mask.broadcastf") ||
  2576. Name.startswith("avx512.mask.broadcasti"))) {
  2577. unsigned NumSrcElts =
  2578. cast<FixedVectorType>(CI->getArgOperand(0)->getType())
  2579. ->getNumElements();
  2580. unsigned NumDstElts =
  2581. cast<FixedVectorType>(CI->getType())->getNumElements();
  2582. SmallVector<int, 8> ShuffleMask(NumDstElts);
  2583. for (unsigned i = 0; i != NumDstElts; ++i)
  2584. ShuffleMask[i] = i % NumSrcElts;
  2585. Rep = Builder.CreateShuffleVector(CI->getArgOperand(0),
  2586. CI->getArgOperand(0),
  2587. ShuffleMask);
  2588. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2589. CI->getArgOperand(1));
  2590. } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
  2591. Name.startswith("avx2.vbroadcast") ||
  2592. Name.startswith("avx512.pbroadcast") ||
  2593. Name.startswith("avx512.mask.broadcast.s"))) {
  2594. // Replace vp?broadcasts with a vector shuffle.
  2595. Value *Op = CI->getArgOperand(0);
  2596. ElementCount EC = cast<VectorType>(CI->getType())->getElementCount();
  2597. Type *MaskTy = VectorType::get(Type::getInt32Ty(C), EC);
  2598. SmallVector<int, 8> M;
  2599. ShuffleVectorInst::getShuffleMask(Constant::getNullValue(MaskTy), M);
  2600. Rep = Builder.CreateShuffleVector(Op, M);
  2601. if (CI->arg_size() == 3)
  2602. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2603. CI->getArgOperand(1));
  2604. } else if (IsX86 && (Name.startswith("sse2.padds.") ||
  2605. Name.startswith("avx2.padds.") ||
  2606. Name.startswith("avx512.padds.") ||
  2607. Name.startswith("avx512.mask.padds."))) {
  2608. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::sadd_sat);
  2609. } else if (IsX86 && (Name.startswith("sse2.psubs.") ||
  2610. Name.startswith("avx2.psubs.") ||
  2611. Name.startswith("avx512.psubs.") ||
  2612. Name.startswith("avx512.mask.psubs."))) {
  2613. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::ssub_sat);
  2614. } else if (IsX86 && (Name.startswith("sse2.paddus.") ||
  2615. Name.startswith("avx2.paddus.") ||
  2616. Name.startswith("avx512.mask.paddus."))) {
  2617. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::uadd_sat);
  2618. } else if (IsX86 && (Name.startswith("sse2.psubus.") ||
  2619. Name.startswith("avx2.psubus.") ||
  2620. Name.startswith("avx512.mask.psubus."))) {
  2621. Rep = UpgradeX86BinaryIntrinsics(Builder, *CI, Intrinsic::usub_sat);
  2622. } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
  2623. Rep = UpgradeX86ALIGNIntrinsics(Builder, CI->getArgOperand(0),
  2624. CI->getArgOperand(1),
  2625. CI->getArgOperand(2),
  2626. CI->getArgOperand(3),
  2627. CI->getArgOperand(4),
  2628. false);
  2629. } else if (IsX86 && Name.startswith("avx512.mask.valign.")) {
  2630. Rep = UpgradeX86ALIGNIntrinsics(Builder, CI->getArgOperand(0),
  2631. CI->getArgOperand(1),
  2632. CI->getArgOperand(2),
  2633. CI->getArgOperand(3),
  2634. CI->getArgOperand(4),
  2635. true);
  2636. } else if (IsX86 && (Name == "sse2.psll.dq" ||
  2637. Name == "avx2.psll.dq")) {
  2638. // 128/256-bit shift left specified in bits.
  2639. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2640. Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0),
  2641. Shift / 8); // Shift is in bits.
  2642. } else if (IsX86 && (Name == "sse2.psrl.dq" ||
  2643. Name == "avx2.psrl.dq")) {
  2644. // 128/256-bit shift right specified in bits.
  2645. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2646. Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0),
  2647. Shift / 8); // Shift is in bits.
  2648. } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
  2649. Name == "avx2.psll.dq.bs" ||
  2650. Name == "avx512.psll.dq.512")) {
  2651. // 128/256/512-bit shift left specified in bytes.
  2652. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2653. Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
  2654. } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
  2655. Name == "avx2.psrl.dq.bs" ||
  2656. Name == "avx512.psrl.dq.512")) {
  2657. // 128/256/512-bit shift right specified in bytes.
  2658. unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2659. Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
  2660. } else if (IsX86 && (Name == "sse41.pblendw" ||
  2661. Name.startswith("sse41.blendp") ||
  2662. Name.startswith("avx.blend.p") ||
  2663. Name == "avx2.pblendw" ||
  2664. Name.startswith("avx2.pblendd."))) {
  2665. Value *Op0 = CI->getArgOperand(0);
  2666. Value *Op1 = CI->getArgOperand(1);
  2667. unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2668. auto *VecTy = cast<FixedVectorType>(CI->getType());
  2669. unsigned NumElts = VecTy->getNumElements();
  2670. SmallVector<int, 16> Idxs(NumElts);
  2671. for (unsigned i = 0; i != NumElts; ++i)
  2672. Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
  2673. Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
  2674. } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
  2675. Name == "avx2.vinserti128" ||
  2676. Name.startswith("avx512.mask.insert"))) {
  2677. Value *Op0 = CI->getArgOperand(0);
  2678. Value *Op1 = CI->getArgOperand(1);
  2679. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2680. unsigned DstNumElts =
  2681. cast<FixedVectorType>(CI->getType())->getNumElements();
  2682. unsigned SrcNumElts =
  2683. cast<FixedVectorType>(Op1->getType())->getNumElements();
  2684. unsigned Scale = DstNumElts / SrcNumElts;
  2685. // Mask off the high bits of the immediate value; hardware ignores those.
  2686. Imm = Imm % Scale;
  2687. // Extend the second operand into a vector the size of the destination.
  2688. SmallVector<int, 8> Idxs(DstNumElts);
  2689. for (unsigned i = 0; i != SrcNumElts; ++i)
  2690. Idxs[i] = i;
  2691. for (unsigned i = SrcNumElts; i != DstNumElts; ++i)
  2692. Idxs[i] = SrcNumElts;
  2693. Rep = Builder.CreateShuffleVector(Op1, Idxs);
  2694. // Insert the second operand into the first operand.
  2695. // Note that there is no guarantee that instruction lowering will actually
  2696. // produce a vinsertf128 instruction for the created shuffles. In
  2697. // particular, the 0 immediate case involves no lane changes, so it can
  2698. // be handled as a blend.
  2699. // Example of shuffle mask for 32-bit elements:
  2700. // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
  2701. // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 >
  2702. // First fill with identify mask.
  2703. for (unsigned i = 0; i != DstNumElts; ++i)
  2704. Idxs[i] = i;
  2705. // Then replace the elements where we need to insert.
  2706. for (unsigned i = 0; i != SrcNumElts; ++i)
  2707. Idxs[i + Imm * SrcNumElts] = i + DstNumElts;
  2708. Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
  2709. // If the intrinsic has a mask operand, handle that.
  2710. if (CI->arg_size() == 5)
  2711. Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
  2712. CI->getArgOperand(3));
  2713. } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
  2714. Name == "avx2.vextracti128" ||
  2715. Name.startswith("avx512.mask.vextract"))) {
  2716. Value *Op0 = CI->getArgOperand(0);
  2717. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2718. unsigned DstNumElts =
  2719. cast<FixedVectorType>(CI->getType())->getNumElements();
  2720. unsigned SrcNumElts =
  2721. cast<FixedVectorType>(Op0->getType())->getNumElements();
  2722. unsigned Scale = SrcNumElts / DstNumElts;
  2723. // Mask off the high bits of the immediate value; hardware ignores those.
  2724. Imm = Imm % Scale;
  2725. // Get indexes for the subvector of the input vector.
  2726. SmallVector<int, 8> Idxs(DstNumElts);
  2727. for (unsigned i = 0; i != DstNumElts; ++i) {
  2728. Idxs[i] = i + (Imm * DstNumElts);
  2729. }
  2730. Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
  2731. // If the intrinsic has a mask operand, handle that.
  2732. if (CI->arg_size() == 4)
  2733. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2734. CI->getArgOperand(2));
  2735. } else if (!IsX86 && Name == "stackprotectorcheck") {
  2736. Rep = nullptr;
  2737. } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
  2738. Name.startswith("avx512.mask.perm.di."))) {
  2739. Value *Op0 = CI->getArgOperand(0);
  2740. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2741. auto *VecTy = cast<FixedVectorType>(CI->getType());
  2742. unsigned NumElts = VecTy->getNumElements();
  2743. SmallVector<int, 8> Idxs(NumElts);
  2744. for (unsigned i = 0; i != NumElts; ++i)
  2745. Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
  2746. Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
  2747. if (CI->arg_size() == 4)
  2748. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2749. CI->getArgOperand(2));
  2750. } else if (IsX86 && (Name.startswith("avx.vperm2f128.") ||
  2751. Name == "avx2.vperm2i128")) {
  2752. // The immediate permute control byte looks like this:
  2753. // [1:0] - select 128 bits from sources for low half of destination
  2754. // [2] - ignore
  2755. // [3] - zero low half of destination
  2756. // [5:4] - select 128 bits from sources for high half of destination
  2757. // [6] - ignore
  2758. // [7] - zero high half of destination
  2759. uint8_t Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2760. unsigned NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2761. unsigned HalfSize = NumElts / 2;
  2762. SmallVector<int, 8> ShuffleMask(NumElts);
  2763. // Determine which operand(s) are actually in use for this instruction.
  2764. Value *V0 = (Imm & 0x02) ? CI->getArgOperand(1) : CI->getArgOperand(0);
  2765. Value *V1 = (Imm & 0x20) ? CI->getArgOperand(1) : CI->getArgOperand(0);
  2766. // If needed, replace operands based on zero mask.
  2767. V0 = (Imm & 0x08) ? ConstantAggregateZero::get(CI->getType()) : V0;
  2768. V1 = (Imm & 0x80) ? ConstantAggregateZero::get(CI->getType()) : V1;
  2769. // Permute low half of result.
  2770. unsigned StartIndex = (Imm & 0x01) ? HalfSize : 0;
  2771. for (unsigned i = 0; i < HalfSize; ++i)
  2772. ShuffleMask[i] = StartIndex + i;
  2773. // Permute high half of result.
  2774. StartIndex = (Imm & 0x10) ? HalfSize : 0;
  2775. for (unsigned i = 0; i < HalfSize; ++i)
  2776. ShuffleMask[i + HalfSize] = NumElts + StartIndex + i;
  2777. Rep = Builder.CreateShuffleVector(V0, V1, ShuffleMask);
  2778. } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
  2779. Name == "sse2.pshuf.d" ||
  2780. Name.startswith("avx512.mask.vpermil.p") ||
  2781. Name.startswith("avx512.mask.pshuf.d."))) {
  2782. Value *Op0 = CI->getArgOperand(0);
  2783. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2784. auto *VecTy = cast<FixedVectorType>(CI->getType());
  2785. unsigned NumElts = VecTy->getNumElements();
  2786. // Calculate the size of each index in the immediate.
  2787. unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
  2788. unsigned IdxMask = ((1 << IdxSize) - 1);
  2789. SmallVector<int, 8> Idxs(NumElts);
  2790. // Lookup the bits for this element, wrapping around the immediate every
  2791. // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
  2792. // to offset by the first index of each group.
  2793. for (unsigned i = 0; i != NumElts; ++i)
  2794. Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
  2795. Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
  2796. if (CI->arg_size() == 4)
  2797. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2798. CI->getArgOperand(2));
  2799. } else if (IsX86 && (Name == "sse2.pshufl.w" ||
  2800. Name.startswith("avx512.mask.pshufl.w."))) {
  2801. Value *Op0 = CI->getArgOperand(0);
  2802. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2803. unsigned NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2804. SmallVector<int, 16> Idxs(NumElts);
  2805. for (unsigned l = 0; l != NumElts; l += 8) {
  2806. for (unsigned i = 0; i != 4; ++i)
  2807. Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
  2808. for (unsigned i = 4; i != 8; ++i)
  2809. Idxs[i + l] = i + l;
  2810. }
  2811. Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
  2812. if (CI->arg_size() == 4)
  2813. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2814. CI->getArgOperand(2));
  2815. } else if (IsX86 && (Name == "sse2.pshufh.w" ||
  2816. Name.startswith("avx512.mask.pshufh.w."))) {
  2817. Value *Op0 = CI->getArgOperand(0);
  2818. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  2819. unsigned NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2820. SmallVector<int, 16> Idxs(NumElts);
  2821. for (unsigned l = 0; l != NumElts; l += 8) {
  2822. for (unsigned i = 0; i != 4; ++i)
  2823. Idxs[i + l] = i + l;
  2824. for (unsigned i = 0; i != 4; ++i)
  2825. Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
  2826. }
  2827. Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
  2828. if (CI->arg_size() == 4)
  2829. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2830. CI->getArgOperand(2));
  2831. } else if (IsX86 && Name.startswith("avx512.mask.shuf.p")) {
  2832. Value *Op0 = CI->getArgOperand(0);
  2833. Value *Op1 = CI->getArgOperand(1);
  2834. unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
  2835. unsigned NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2836. unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
  2837. unsigned HalfLaneElts = NumLaneElts / 2;
  2838. SmallVector<int, 16> Idxs(NumElts);
  2839. for (unsigned i = 0; i != NumElts; ++i) {
  2840. // Base index is the starting element of the lane.
  2841. Idxs[i] = i - (i % NumLaneElts);
  2842. // If we are half way through the lane switch to the other source.
  2843. if ((i % NumLaneElts) >= HalfLaneElts)
  2844. Idxs[i] += NumElts;
  2845. // Now select the specific element. By adding HalfLaneElts bits from
  2846. // the immediate. Wrapping around the immediate every 8-bits.
  2847. Idxs[i] += (Imm >> ((i * HalfLaneElts) % 8)) & ((1 << HalfLaneElts) - 1);
  2848. }
  2849. Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
  2850. Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
  2851. CI->getArgOperand(3));
  2852. } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
  2853. Name.startswith("avx512.mask.movshdup") ||
  2854. Name.startswith("avx512.mask.movsldup"))) {
  2855. Value *Op0 = CI->getArgOperand(0);
  2856. unsigned NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2857. unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
  2858. unsigned Offset = 0;
  2859. if (Name.startswith("avx512.mask.movshdup."))
  2860. Offset = 1;
  2861. SmallVector<int, 16> Idxs(NumElts);
  2862. for (unsigned l = 0; l != NumElts; l += NumLaneElts)
  2863. for (unsigned i = 0; i != NumLaneElts; i += 2) {
  2864. Idxs[i + l + 0] = i + l + Offset;
  2865. Idxs[i + l + 1] = i + l + Offset;
  2866. }
  2867. Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
  2868. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  2869. CI->getArgOperand(1));
  2870. } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
  2871. Name.startswith("avx512.mask.unpckl."))) {
  2872. Value *Op0 = CI->getArgOperand(0);
  2873. Value *Op1 = CI->getArgOperand(1);
  2874. int NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2875. int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
  2876. SmallVector<int, 64> Idxs(NumElts);
  2877. for (int l = 0; l != NumElts; l += NumLaneElts)
  2878. for (int i = 0; i != NumLaneElts; ++i)
  2879. Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
  2880. Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
  2881. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2882. CI->getArgOperand(2));
  2883. } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
  2884. Name.startswith("avx512.mask.unpckh."))) {
  2885. Value *Op0 = CI->getArgOperand(0);
  2886. Value *Op1 = CI->getArgOperand(1);
  2887. int NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  2888. int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
  2889. SmallVector<int, 64> Idxs(NumElts);
  2890. for (int l = 0; l != NumElts; l += NumLaneElts)
  2891. for (int i = 0; i != NumLaneElts; ++i)
  2892. Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
  2893. Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
  2894. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2895. CI->getArgOperand(2));
  2896. } else if (IsX86 && (Name.startswith("avx512.mask.and.") ||
  2897. Name.startswith("avx512.mask.pand."))) {
  2898. VectorType *FTy = cast<VectorType>(CI->getType());
  2899. VectorType *ITy = VectorType::getInteger(FTy);
  2900. Rep = Builder.CreateAnd(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
  2901. Builder.CreateBitCast(CI->getArgOperand(1), ITy));
  2902. Rep = Builder.CreateBitCast(Rep, FTy);
  2903. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2904. CI->getArgOperand(2));
  2905. } else if (IsX86 && (Name.startswith("avx512.mask.andn.") ||
  2906. Name.startswith("avx512.mask.pandn."))) {
  2907. VectorType *FTy = cast<VectorType>(CI->getType());
  2908. VectorType *ITy = VectorType::getInteger(FTy);
  2909. Rep = Builder.CreateNot(Builder.CreateBitCast(CI->getArgOperand(0), ITy));
  2910. Rep = Builder.CreateAnd(Rep,
  2911. Builder.CreateBitCast(CI->getArgOperand(1), ITy));
  2912. Rep = Builder.CreateBitCast(Rep, FTy);
  2913. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2914. CI->getArgOperand(2));
  2915. } else if (IsX86 && (Name.startswith("avx512.mask.or.") ||
  2916. Name.startswith("avx512.mask.por."))) {
  2917. VectorType *FTy = cast<VectorType>(CI->getType());
  2918. VectorType *ITy = VectorType::getInteger(FTy);
  2919. Rep = Builder.CreateOr(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
  2920. Builder.CreateBitCast(CI->getArgOperand(1), ITy));
  2921. Rep = Builder.CreateBitCast(Rep, FTy);
  2922. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2923. CI->getArgOperand(2));
  2924. } else if (IsX86 && (Name.startswith("avx512.mask.xor.") ||
  2925. Name.startswith("avx512.mask.pxor."))) {
  2926. VectorType *FTy = cast<VectorType>(CI->getType());
  2927. VectorType *ITy = VectorType::getInteger(FTy);
  2928. Rep = Builder.CreateXor(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
  2929. Builder.CreateBitCast(CI->getArgOperand(1), ITy));
  2930. Rep = Builder.CreateBitCast(Rep, FTy);
  2931. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2932. CI->getArgOperand(2));
  2933. } else if (IsX86 && Name.startswith("avx512.mask.padd.")) {
  2934. Rep = Builder.CreateAdd(CI->getArgOperand(0), CI->getArgOperand(1));
  2935. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2936. CI->getArgOperand(2));
  2937. } else if (IsX86 && Name.startswith("avx512.mask.psub.")) {
  2938. Rep = Builder.CreateSub(CI->getArgOperand(0), CI->getArgOperand(1));
  2939. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2940. CI->getArgOperand(2));
  2941. } else if (IsX86 && Name.startswith("avx512.mask.pmull.")) {
  2942. Rep = Builder.CreateMul(CI->getArgOperand(0), CI->getArgOperand(1));
  2943. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2944. CI->getArgOperand(2));
  2945. } else if (IsX86 && Name.startswith("avx512.mask.add.p")) {
  2946. if (Name.endswith(".512")) {
  2947. Intrinsic::ID IID;
  2948. if (Name[17] == 's')
  2949. IID = Intrinsic::x86_avx512_add_ps_512;
  2950. else
  2951. IID = Intrinsic::x86_avx512_add_pd_512;
  2952. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2953. { CI->getArgOperand(0), CI->getArgOperand(1),
  2954. CI->getArgOperand(4) });
  2955. } else {
  2956. Rep = Builder.CreateFAdd(CI->getArgOperand(0), CI->getArgOperand(1));
  2957. }
  2958. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2959. CI->getArgOperand(2));
  2960. } else if (IsX86 && Name.startswith("avx512.mask.div.p")) {
  2961. if (Name.endswith(".512")) {
  2962. Intrinsic::ID IID;
  2963. if (Name[17] == 's')
  2964. IID = Intrinsic::x86_avx512_div_ps_512;
  2965. else
  2966. IID = Intrinsic::x86_avx512_div_pd_512;
  2967. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2968. { CI->getArgOperand(0), CI->getArgOperand(1),
  2969. CI->getArgOperand(4) });
  2970. } else {
  2971. Rep = Builder.CreateFDiv(CI->getArgOperand(0), CI->getArgOperand(1));
  2972. }
  2973. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2974. CI->getArgOperand(2));
  2975. } else if (IsX86 && Name.startswith("avx512.mask.mul.p")) {
  2976. if (Name.endswith(".512")) {
  2977. Intrinsic::ID IID;
  2978. if (Name[17] == 's')
  2979. IID = Intrinsic::x86_avx512_mul_ps_512;
  2980. else
  2981. IID = Intrinsic::x86_avx512_mul_pd_512;
  2982. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2983. { CI->getArgOperand(0), CI->getArgOperand(1),
  2984. CI->getArgOperand(4) });
  2985. } else {
  2986. Rep = Builder.CreateFMul(CI->getArgOperand(0), CI->getArgOperand(1));
  2987. }
  2988. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  2989. CI->getArgOperand(2));
  2990. } else if (IsX86 && Name.startswith("avx512.mask.sub.p")) {
  2991. if (Name.endswith(".512")) {
  2992. Intrinsic::ID IID;
  2993. if (Name[17] == 's')
  2994. IID = Intrinsic::x86_avx512_sub_ps_512;
  2995. else
  2996. IID = Intrinsic::x86_avx512_sub_pd_512;
  2997. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  2998. { CI->getArgOperand(0), CI->getArgOperand(1),
  2999. CI->getArgOperand(4) });
  3000. } else {
  3001. Rep = Builder.CreateFSub(CI->getArgOperand(0), CI->getArgOperand(1));
  3002. }
  3003. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  3004. CI->getArgOperand(2));
  3005. } else if (IsX86 && (Name.startswith("avx512.mask.max.p") ||
  3006. Name.startswith("avx512.mask.min.p")) &&
  3007. Name.drop_front(18) == ".512") {
  3008. bool IsDouble = Name[17] == 'd';
  3009. bool IsMin = Name[13] == 'i';
  3010. static const Intrinsic::ID MinMaxTbl[2][2] = {
  3011. { Intrinsic::x86_avx512_max_ps_512, Intrinsic::x86_avx512_max_pd_512 },
  3012. { Intrinsic::x86_avx512_min_ps_512, Intrinsic::x86_avx512_min_pd_512 }
  3013. };
  3014. Intrinsic::ID IID = MinMaxTbl[IsMin][IsDouble];
  3015. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  3016. { CI->getArgOperand(0), CI->getArgOperand(1),
  3017. CI->getArgOperand(4) });
  3018. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
  3019. CI->getArgOperand(2));
  3020. } else if (IsX86 && Name.startswith("avx512.mask.lzcnt.")) {
  3021. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(),
  3022. Intrinsic::ctlz,
  3023. CI->getType()),
  3024. { CI->getArgOperand(0), Builder.getInt1(false) });
  3025. Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
  3026. CI->getArgOperand(1));
  3027. } else if (IsX86 && Name.startswith("avx512.mask.psll")) {
  3028. bool IsImmediate = Name[16] == 'i' ||
  3029. (Name.size() > 18 && Name[18] == 'i');
  3030. bool IsVariable = Name[16] == 'v';
  3031. char Size = Name[16] == '.' ? Name[17] :
  3032. Name[17] == '.' ? Name[18] :
  3033. Name[18] == '.' ? Name[19] :
  3034. Name[20];
  3035. Intrinsic::ID IID;
  3036. if (IsVariable && Name[17] != '.') {
  3037. if (Size == 'd' && Name[17] == '2') // avx512.mask.psllv2.di
  3038. IID = Intrinsic::x86_avx2_psllv_q;
  3039. else if (Size == 'd' && Name[17] == '4') // avx512.mask.psllv4.di
  3040. IID = Intrinsic::x86_avx2_psllv_q_256;
  3041. else if (Size == 's' && Name[17] == '4') // avx512.mask.psllv4.si
  3042. IID = Intrinsic::x86_avx2_psllv_d;
  3043. else if (Size == 's' && Name[17] == '8') // avx512.mask.psllv8.si
  3044. IID = Intrinsic::x86_avx2_psllv_d_256;
  3045. else if (Size == 'h' && Name[17] == '8') // avx512.mask.psllv8.hi
  3046. IID = Intrinsic::x86_avx512_psllv_w_128;
  3047. else if (Size == 'h' && Name[17] == '1') // avx512.mask.psllv16.hi
  3048. IID = Intrinsic::x86_avx512_psllv_w_256;
  3049. else if (Name[17] == '3' && Name[18] == '2') // avx512.mask.psllv32hi
  3050. IID = Intrinsic::x86_avx512_psllv_w_512;
  3051. else
  3052. llvm_unreachable("Unexpected size");
  3053. } else if (Name.endswith(".128")) {
  3054. if (Size == 'd') // avx512.mask.psll.d.128, avx512.mask.psll.di.128
  3055. IID = IsImmediate ? Intrinsic::x86_sse2_pslli_d
  3056. : Intrinsic::x86_sse2_psll_d;
  3057. else if (Size == 'q') // avx512.mask.psll.q.128, avx512.mask.psll.qi.128
  3058. IID = IsImmediate ? Intrinsic::x86_sse2_pslli_q
  3059. : Intrinsic::x86_sse2_psll_q;
  3060. else if (Size == 'w') // avx512.mask.psll.w.128, avx512.mask.psll.wi.128
  3061. IID = IsImmediate ? Intrinsic::x86_sse2_pslli_w
  3062. : Intrinsic::x86_sse2_psll_w;
  3063. else
  3064. llvm_unreachable("Unexpected size");
  3065. } else if (Name.endswith(".256")) {
  3066. if (Size == 'd') // avx512.mask.psll.d.256, avx512.mask.psll.di.256
  3067. IID = IsImmediate ? Intrinsic::x86_avx2_pslli_d
  3068. : Intrinsic::x86_avx2_psll_d;
  3069. else if (Size == 'q') // avx512.mask.psll.q.256, avx512.mask.psll.qi.256
  3070. IID = IsImmediate ? Intrinsic::x86_avx2_pslli_q
  3071. : Intrinsic::x86_avx2_psll_q;
  3072. else if (Size == 'w') // avx512.mask.psll.w.256, avx512.mask.psll.wi.256
  3073. IID = IsImmediate ? Intrinsic::x86_avx2_pslli_w
  3074. : Intrinsic::x86_avx2_psll_w;
  3075. else
  3076. llvm_unreachable("Unexpected size");
  3077. } else {
  3078. if (Size == 'd') // psll.di.512, pslli.d, psll.d, psllv.d.512
  3079. IID = IsImmediate ? Intrinsic::x86_avx512_pslli_d_512 :
  3080. IsVariable ? Intrinsic::x86_avx512_psllv_d_512 :
  3081. Intrinsic::x86_avx512_psll_d_512;
  3082. else if (Size == 'q') // psll.qi.512, pslli.q, psll.q, psllv.q.512
  3083. IID = IsImmediate ? Intrinsic::x86_avx512_pslli_q_512 :
  3084. IsVariable ? Intrinsic::x86_avx512_psllv_q_512 :
  3085. Intrinsic::x86_avx512_psll_q_512;
  3086. else if (Size == 'w') // psll.wi.512, pslli.w, psll.w
  3087. IID = IsImmediate ? Intrinsic::x86_avx512_pslli_w_512
  3088. : Intrinsic::x86_avx512_psll_w_512;
  3089. else
  3090. llvm_unreachable("Unexpected size");
  3091. }
  3092. Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
  3093. } else if (IsX86 && Name.startswith("avx512.mask.psrl")) {
  3094. bool IsImmediate = Name[16] == 'i' ||
  3095. (Name.size() > 18 && Name[18] == 'i');
  3096. bool IsVariable = Name[16] == 'v';
  3097. char Size = Name[16] == '.' ? Name[17] :
  3098. Name[17] == '.' ? Name[18] :
  3099. Name[18] == '.' ? Name[19] :
  3100. Name[20];
  3101. Intrinsic::ID IID;
  3102. if (IsVariable && Name[17] != '.') {
  3103. if (Size == 'd' && Name[17] == '2') // avx512.mask.psrlv2.di
  3104. IID = Intrinsic::x86_avx2_psrlv_q;
  3105. else if (Size == 'd' && Name[17] == '4') // avx512.mask.psrlv4.di
  3106. IID = Intrinsic::x86_avx2_psrlv_q_256;
  3107. else if (Size == 's' && Name[17] == '4') // avx512.mask.psrlv4.si
  3108. IID = Intrinsic::x86_avx2_psrlv_d;
  3109. else if (Size == 's' && Name[17] == '8') // avx512.mask.psrlv8.si
  3110. IID = Intrinsic::x86_avx2_psrlv_d_256;
  3111. else if (Size == 'h' && Name[17] == '8') // avx512.mask.psrlv8.hi
  3112. IID = Intrinsic::x86_avx512_psrlv_w_128;
  3113. else if (Size == 'h' && Name[17] == '1') // avx512.mask.psrlv16.hi
  3114. IID = Intrinsic::x86_avx512_psrlv_w_256;
  3115. else if (Name[17] == '3' && Name[18] == '2') // avx512.mask.psrlv32hi
  3116. IID = Intrinsic::x86_avx512_psrlv_w_512;
  3117. else
  3118. llvm_unreachable("Unexpected size");
  3119. } else if (Name.endswith(".128")) {
  3120. if (Size == 'd') // avx512.mask.psrl.d.128, avx512.mask.psrl.di.128
  3121. IID = IsImmediate ? Intrinsic::x86_sse2_psrli_d
  3122. : Intrinsic::x86_sse2_psrl_d;
  3123. else if (Size == 'q') // avx512.mask.psrl.q.128, avx512.mask.psrl.qi.128
  3124. IID = IsImmediate ? Intrinsic::x86_sse2_psrli_q
  3125. : Intrinsic::x86_sse2_psrl_q;
  3126. else if (Size == 'w') // avx512.mask.psrl.w.128, avx512.mask.psrl.wi.128
  3127. IID = IsImmediate ? Intrinsic::x86_sse2_psrli_w
  3128. : Intrinsic::x86_sse2_psrl_w;
  3129. else
  3130. llvm_unreachable("Unexpected size");
  3131. } else if (Name.endswith(".256")) {
  3132. if (Size == 'd') // avx512.mask.psrl.d.256, avx512.mask.psrl.di.256
  3133. IID = IsImmediate ? Intrinsic::x86_avx2_psrli_d
  3134. : Intrinsic::x86_avx2_psrl_d;
  3135. else if (Size == 'q') // avx512.mask.psrl.q.256, avx512.mask.psrl.qi.256
  3136. IID = IsImmediate ? Intrinsic::x86_avx2_psrli_q
  3137. : Intrinsic::x86_avx2_psrl_q;
  3138. else if (Size == 'w') // avx512.mask.psrl.w.256, avx512.mask.psrl.wi.256
  3139. IID = IsImmediate ? Intrinsic::x86_avx2_psrli_w
  3140. : Intrinsic::x86_avx2_psrl_w;
  3141. else
  3142. llvm_unreachable("Unexpected size");
  3143. } else {
  3144. if (Size == 'd') // psrl.di.512, psrli.d, psrl.d, psrl.d.512
  3145. IID = IsImmediate ? Intrinsic::x86_avx512_psrli_d_512 :
  3146. IsVariable ? Intrinsic::x86_avx512_psrlv_d_512 :
  3147. Intrinsic::x86_avx512_psrl_d_512;
  3148. else if (Size == 'q') // psrl.qi.512, psrli.q, psrl.q, psrl.q.512
  3149. IID = IsImmediate ? Intrinsic::x86_avx512_psrli_q_512 :
  3150. IsVariable ? Intrinsic::x86_avx512_psrlv_q_512 :
  3151. Intrinsic::x86_avx512_psrl_q_512;
  3152. else if (Size == 'w') // psrl.wi.512, psrli.w, psrl.w)
  3153. IID = IsImmediate ? Intrinsic::x86_avx512_psrli_w_512
  3154. : Intrinsic::x86_avx512_psrl_w_512;
  3155. else
  3156. llvm_unreachable("Unexpected size");
  3157. }
  3158. Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
  3159. } else if (IsX86 && Name.startswith("avx512.mask.psra")) {
  3160. bool IsImmediate = Name[16] == 'i' ||
  3161. (Name.size() > 18 && Name[18] == 'i');
  3162. bool IsVariable = Name[16] == 'v';
  3163. char Size = Name[16] == '.' ? Name[17] :
  3164. Name[17] == '.' ? Name[18] :
  3165. Name[18] == '.' ? Name[19] :
  3166. Name[20];
  3167. Intrinsic::ID IID;
  3168. if (IsVariable && Name[17] != '.') {
  3169. if (Size == 's' && Name[17] == '4') // avx512.mask.psrav4.si
  3170. IID = Intrinsic::x86_avx2_psrav_d;
  3171. else if (Size == 's' && Name[17] == '8') // avx512.mask.psrav8.si
  3172. IID = Intrinsic::x86_avx2_psrav_d_256;
  3173. else if (Size == 'h' && Name[17] == '8') // avx512.mask.psrav8.hi
  3174. IID = Intrinsic::x86_avx512_psrav_w_128;
  3175. else if (Size == 'h' && Name[17] == '1') // avx512.mask.psrav16.hi
  3176. IID = Intrinsic::x86_avx512_psrav_w_256;
  3177. else if (Name[17] == '3' && Name[18] == '2') // avx512.mask.psrav32hi
  3178. IID = Intrinsic::x86_avx512_psrav_w_512;
  3179. else
  3180. llvm_unreachable("Unexpected size");
  3181. } else if (Name.endswith(".128")) {
  3182. if (Size == 'd') // avx512.mask.psra.d.128, avx512.mask.psra.di.128
  3183. IID = IsImmediate ? Intrinsic::x86_sse2_psrai_d
  3184. : Intrinsic::x86_sse2_psra_d;
  3185. else if (Size == 'q') // avx512.mask.psra.q.128, avx512.mask.psra.qi.128
  3186. IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_128 :
  3187. IsVariable ? Intrinsic::x86_avx512_psrav_q_128 :
  3188. Intrinsic::x86_avx512_psra_q_128;
  3189. else if (Size == 'w') // avx512.mask.psra.w.128, avx512.mask.psra.wi.128
  3190. IID = IsImmediate ? Intrinsic::x86_sse2_psrai_w
  3191. : Intrinsic::x86_sse2_psra_w;
  3192. else
  3193. llvm_unreachable("Unexpected size");
  3194. } else if (Name.endswith(".256")) {
  3195. if (Size == 'd') // avx512.mask.psra.d.256, avx512.mask.psra.di.256
  3196. IID = IsImmediate ? Intrinsic::x86_avx2_psrai_d
  3197. : Intrinsic::x86_avx2_psra_d;
  3198. else if (Size == 'q') // avx512.mask.psra.q.256, avx512.mask.psra.qi.256
  3199. IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_256 :
  3200. IsVariable ? Intrinsic::x86_avx512_psrav_q_256 :
  3201. Intrinsic::x86_avx512_psra_q_256;
  3202. else if (Size == 'w') // avx512.mask.psra.w.256, avx512.mask.psra.wi.256
  3203. IID = IsImmediate ? Intrinsic::x86_avx2_psrai_w
  3204. : Intrinsic::x86_avx2_psra_w;
  3205. else
  3206. llvm_unreachable("Unexpected size");
  3207. } else {
  3208. if (Size == 'd') // psra.di.512, psrai.d, psra.d, psrav.d.512
  3209. IID = IsImmediate ? Intrinsic::x86_avx512_psrai_d_512 :
  3210. IsVariable ? Intrinsic::x86_avx512_psrav_d_512 :
  3211. Intrinsic::x86_avx512_psra_d_512;
  3212. else if (Size == 'q') // psra.qi.512, psrai.q, psra.q
  3213. IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_512 :
  3214. IsVariable ? Intrinsic::x86_avx512_psrav_q_512 :
  3215. Intrinsic::x86_avx512_psra_q_512;
  3216. else if (Size == 'w') // psra.wi.512, psrai.w, psra.w
  3217. IID = IsImmediate ? Intrinsic::x86_avx512_psrai_w_512
  3218. : Intrinsic::x86_avx512_psra_w_512;
  3219. else
  3220. llvm_unreachable("Unexpected size");
  3221. }
  3222. Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
  3223. } else if (IsX86 && Name.startswith("avx512.mask.move.s")) {
  3224. Rep = upgradeMaskedMove(Builder, *CI);
  3225. } else if (IsX86 && Name.startswith("avx512.cvtmask2")) {
  3226. Rep = UpgradeMaskToInt(Builder, *CI);
  3227. } else if (IsX86 && Name.endswith(".movntdqa")) {
  3228. Module *M = F->getParent();
  3229. MDNode *Node = MDNode::get(
  3230. C, ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
  3231. Value *Ptr = CI->getArgOperand(0);
  3232. // Convert the type of the pointer to a pointer to the stored type.
  3233. Value *BC = Builder.CreateBitCast(
  3234. Ptr, PointerType::getUnqual(CI->getType()), "cast");
  3235. LoadInst *LI = Builder.CreateAlignedLoad(
  3236. CI->getType(), BC,
  3237. Align(CI->getType()->getPrimitiveSizeInBits().getFixedValue() / 8));
  3238. LI->setMetadata(M->getMDKindID("nontemporal"), Node);
  3239. Rep = LI;
  3240. } else if (IsX86 && (Name.startswith("fma.vfmadd.") ||
  3241. Name.startswith("fma.vfmsub.") ||
  3242. Name.startswith("fma.vfnmadd.") ||
  3243. Name.startswith("fma.vfnmsub."))) {
  3244. bool NegMul = Name[6] == 'n';
  3245. bool NegAcc = NegMul ? Name[8] == 's' : Name[7] == 's';
  3246. bool IsScalar = NegMul ? Name[12] == 's' : Name[11] == 's';
  3247. Value *Ops[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3248. CI->getArgOperand(2) };
  3249. if (IsScalar) {
  3250. Ops[0] = Builder.CreateExtractElement(Ops[0], (uint64_t)0);
  3251. Ops[1] = Builder.CreateExtractElement(Ops[1], (uint64_t)0);
  3252. Ops[2] = Builder.CreateExtractElement(Ops[2], (uint64_t)0);
  3253. }
  3254. if (NegMul && !IsScalar)
  3255. Ops[0] = Builder.CreateFNeg(Ops[0]);
  3256. if (NegMul && IsScalar)
  3257. Ops[1] = Builder.CreateFNeg(Ops[1]);
  3258. if (NegAcc)
  3259. Ops[2] = Builder.CreateFNeg(Ops[2]);
  3260. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(),
  3261. Intrinsic::fma,
  3262. Ops[0]->getType()),
  3263. Ops);
  3264. if (IsScalar)
  3265. Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep,
  3266. (uint64_t)0);
  3267. } else if (IsX86 && Name.startswith("fma4.vfmadd.s")) {
  3268. Value *Ops[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3269. CI->getArgOperand(2) };
  3270. Ops[0] = Builder.CreateExtractElement(Ops[0], (uint64_t)0);
  3271. Ops[1] = Builder.CreateExtractElement(Ops[1], (uint64_t)0);
  3272. Ops[2] = Builder.CreateExtractElement(Ops[2], (uint64_t)0);
  3273. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(),
  3274. Intrinsic::fma,
  3275. Ops[0]->getType()),
  3276. Ops);
  3277. Rep = Builder.CreateInsertElement(Constant::getNullValue(CI->getType()),
  3278. Rep, (uint64_t)0);
  3279. } else if (IsX86 && (Name.startswith("avx512.mask.vfmadd.s") ||
  3280. Name.startswith("avx512.maskz.vfmadd.s") ||
  3281. Name.startswith("avx512.mask3.vfmadd.s") ||
  3282. Name.startswith("avx512.mask3.vfmsub.s") ||
  3283. Name.startswith("avx512.mask3.vfnmsub.s"))) {
  3284. bool IsMask3 = Name[11] == '3';
  3285. bool IsMaskZ = Name[11] == 'z';
  3286. // Drop the "avx512.mask." to make it easier.
  3287. Name = Name.drop_front(IsMask3 || IsMaskZ ? 13 : 12);
  3288. bool NegMul = Name[2] == 'n';
  3289. bool NegAcc = NegMul ? Name[4] == 's' : Name[3] == 's';
  3290. Value *A = CI->getArgOperand(0);
  3291. Value *B = CI->getArgOperand(1);
  3292. Value *C = CI->getArgOperand(2);
  3293. if (NegMul && (IsMask3 || IsMaskZ))
  3294. A = Builder.CreateFNeg(A);
  3295. if (NegMul && !(IsMask3 || IsMaskZ))
  3296. B = Builder.CreateFNeg(B);
  3297. if (NegAcc)
  3298. C = Builder.CreateFNeg(C);
  3299. A = Builder.CreateExtractElement(A, (uint64_t)0);
  3300. B = Builder.CreateExtractElement(B, (uint64_t)0);
  3301. C = Builder.CreateExtractElement(C, (uint64_t)0);
  3302. if (!isa<ConstantInt>(CI->getArgOperand(4)) ||
  3303. cast<ConstantInt>(CI->getArgOperand(4))->getZExtValue() != 4) {
  3304. Value *Ops[] = { A, B, C, CI->getArgOperand(4) };
  3305. Intrinsic::ID IID;
  3306. if (Name.back() == 'd')
  3307. IID = Intrinsic::x86_avx512_vfmadd_f64;
  3308. else
  3309. IID = Intrinsic::x86_avx512_vfmadd_f32;
  3310. Function *FMA = Intrinsic::getDeclaration(CI->getModule(), IID);
  3311. Rep = Builder.CreateCall(FMA, Ops);
  3312. } else {
  3313. Function *FMA = Intrinsic::getDeclaration(CI->getModule(),
  3314. Intrinsic::fma,
  3315. A->getType());
  3316. Rep = Builder.CreateCall(FMA, { A, B, C });
  3317. }
  3318. Value *PassThru = IsMaskZ ? Constant::getNullValue(Rep->getType()) :
  3319. IsMask3 ? C : A;
  3320. // For Mask3 with NegAcc, we need to create a new extractelement that
  3321. // avoids the negation above.
  3322. if (NegAcc && IsMask3)
  3323. PassThru = Builder.CreateExtractElement(CI->getArgOperand(2),
  3324. (uint64_t)0);
  3325. Rep = EmitX86ScalarSelect(Builder, CI->getArgOperand(3),
  3326. Rep, PassThru);
  3327. Rep = Builder.CreateInsertElement(CI->getArgOperand(IsMask3 ? 2 : 0),
  3328. Rep, (uint64_t)0);
  3329. } else if (IsX86 && (Name.startswith("avx512.mask.vfmadd.p") ||
  3330. Name.startswith("avx512.mask.vfnmadd.p") ||
  3331. Name.startswith("avx512.mask.vfnmsub.p") ||
  3332. Name.startswith("avx512.mask3.vfmadd.p") ||
  3333. Name.startswith("avx512.mask3.vfmsub.p") ||
  3334. Name.startswith("avx512.mask3.vfnmsub.p") ||
  3335. Name.startswith("avx512.maskz.vfmadd.p"))) {
  3336. bool IsMask3 = Name[11] == '3';
  3337. bool IsMaskZ = Name[11] == 'z';
  3338. // Drop the "avx512.mask." to make it easier.
  3339. Name = Name.drop_front(IsMask3 || IsMaskZ ? 13 : 12);
  3340. bool NegMul = Name[2] == 'n';
  3341. bool NegAcc = NegMul ? Name[4] == 's' : Name[3] == 's';
  3342. Value *A = CI->getArgOperand(0);
  3343. Value *B = CI->getArgOperand(1);
  3344. Value *C = CI->getArgOperand(2);
  3345. if (NegMul && (IsMask3 || IsMaskZ))
  3346. A = Builder.CreateFNeg(A);
  3347. if (NegMul && !(IsMask3 || IsMaskZ))
  3348. B = Builder.CreateFNeg(B);
  3349. if (NegAcc)
  3350. C = Builder.CreateFNeg(C);
  3351. if (CI->arg_size() == 5 &&
  3352. (!isa<ConstantInt>(CI->getArgOperand(4)) ||
  3353. cast<ConstantInt>(CI->getArgOperand(4))->getZExtValue() != 4)) {
  3354. Intrinsic::ID IID;
  3355. // Check the character before ".512" in string.
  3356. if (Name[Name.size()-5] == 's')
  3357. IID = Intrinsic::x86_avx512_vfmadd_ps_512;
  3358. else
  3359. IID = Intrinsic::x86_avx512_vfmadd_pd_512;
  3360. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  3361. { A, B, C, CI->getArgOperand(4) });
  3362. } else {
  3363. Function *FMA = Intrinsic::getDeclaration(CI->getModule(),
  3364. Intrinsic::fma,
  3365. A->getType());
  3366. Rep = Builder.CreateCall(FMA, { A, B, C });
  3367. }
  3368. Value *PassThru = IsMaskZ ? llvm::Constant::getNullValue(CI->getType()) :
  3369. IsMask3 ? CI->getArgOperand(2) :
  3370. CI->getArgOperand(0);
  3371. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, PassThru);
  3372. } else if (IsX86 && Name.startswith("fma.vfmsubadd.p")) {
  3373. unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
  3374. unsigned EltWidth = CI->getType()->getScalarSizeInBits();
  3375. Intrinsic::ID IID;
  3376. if (VecWidth == 128 && EltWidth == 32)
  3377. IID = Intrinsic::x86_fma_vfmaddsub_ps;
  3378. else if (VecWidth == 256 && EltWidth == 32)
  3379. IID = Intrinsic::x86_fma_vfmaddsub_ps_256;
  3380. else if (VecWidth == 128 && EltWidth == 64)
  3381. IID = Intrinsic::x86_fma_vfmaddsub_pd;
  3382. else if (VecWidth == 256 && EltWidth == 64)
  3383. IID = Intrinsic::x86_fma_vfmaddsub_pd_256;
  3384. else
  3385. llvm_unreachable("Unexpected intrinsic");
  3386. Value *Ops[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3387. CI->getArgOperand(2) };
  3388. Ops[2] = Builder.CreateFNeg(Ops[2]);
  3389. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  3390. Ops);
  3391. } else if (IsX86 && (Name.startswith("avx512.mask.vfmaddsub.p") ||
  3392. Name.startswith("avx512.mask3.vfmaddsub.p") ||
  3393. Name.startswith("avx512.maskz.vfmaddsub.p") ||
  3394. Name.startswith("avx512.mask3.vfmsubadd.p"))) {
  3395. bool IsMask3 = Name[11] == '3';
  3396. bool IsMaskZ = Name[11] == 'z';
  3397. // Drop the "avx512.mask." to make it easier.
  3398. Name = Name.drop_front(IsMask3 || IsMaskZ ? 13 : 12);
  3399. bool IsSubAdd = Name[3] == 's';
  3400. if (CI->arg_size() == 5) {
  3401. Intrinsic::ID IID;
  3402. // Check the character before ".512" in string.
  3403. if (Name[Name.size()-5] == 's')
  3404. IID = Intrinsic::x86_avx512_vfmaddsub_ps_512;
  3405. else
  3406. IID = Intrinsic::x86_avx512_vfmaddsub_pd_512;
  3407. Value *Ops[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3408. CI->getArgOperand(2), CI->getArgOperand(4) };
  3409. if (IsSubAdd)
  3410. Ops[2] = Builder.CreateFNeg(Ops[2]);
  3411. Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(), IID),
  3412. Ops);
  3413. } else {
  3414. int NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  3415. Value *Ops[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3416. CI->getArgOperand(2) };
  3417. Function *FMA = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::fma,
  3418. Ops[0]->getType());
  3419. Value *Odd = Builder.CreateCall(FMA, Ops);
  3420. Ops[2] = Builder.CreateFNeg(Ops[2]);
  3421. Value *Even = Builder.CreateCall(FMA, Ops);
  3422. if (IsSubAdd)
  3423. std::swap(Even, Odd);
  3424. SmallVector<int, 32> Idxs(NumElts);
  3425. for (int i = 0; i != NumElts; ++i)
  3426. Idxs[i] = i + (i % 2) * NumElts;
  3427. Rep = Builder.CreateShuffleVector(Even, Odd, Idxs);
  3428. }
  3429. Value *PassThru = IsMaskZ ? llvm::Constant::getNullValue(CI->getType()) :
  3430. IsMask3 ? CI->getArgOperand(2) :
  3431. CI->getArgOperand(0);
  3432. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, PassThru);
  3433. } else if (IsX86 && (Name.startswith("avx512.mask.pternlog.") ||
  3434. Name.startswith("avx512.maskz.pternlog."))) {
  3435. bool ZeroMask = Name[11] == 'z';
  3436. unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
  3437. unsigned EltWidth = CI->getType()->getScalarSizeInBits();
  3438. Intrinsic::ID IID;
  3439. if (VecWidth == 128 && EltWidth == 32)
  3440. IID = Intrinsic::x86_avx512_pternlog_d_128;
  3441. else if (VecWidth == 256 && EltWidth == 32)
  3442. IID = Intrinsic::x86_avx512_pternlog_d_256;
  3443. else if (VecWidth == 512 && EltWidth == 32)
  3444. IID = Intrinsic::x86_avx512_pternlog_d_512;
  3445. else if (VecWidth == 128 && EltWidth == 64)
  3446. IID = Intrinsic::x86_avx512_pternlog_q_128;
  3447. else if (VecWidth == 256 && EltWidth == 64)
  3448. IID = Intrinsic::x86_avx512_pternlog_q_256;
  3449. else if (VecWidth == 512 && EltWidth == 64)
  3450. IID = Intrinsic::x86_avx512_pternlog_q_512;
  3451. else
  3452. llvm_unreachable("Unexpected intrinsic");
  3453. Value *Args[] = { CI->getArgOperand(0) , CI->getArgOperand(1),
  3454. CI->getArgOperand(2), CI->getArgOperand(3) };
  3455. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(), IID),
  3456. Args);
  3457. Value *PassThru = ZeroMask ? ConstantAggregateZero::get(CI->getType())
  3458. : CI->getArgOperand(0);
  3459. Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep, PassThru);
  3460. } else if (IsX86 && (Name.startswith("avx512.mask.vpmadd52") ||
  3461. Name.startswith("avx512.maskz.vpmadd52"))) {
  3462. bool ZeroMask = Name[11] == 'z';
  3463. bool High = Name[20] == 'h' || Name[21] == 'h';
  3464. unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
  3465. Intrinsic::ID IID;
  3466. if (VecWidth == 128 && !High)
  3467. IID = Intrinsic::x86_avx512_vpmadd52l_uq_128;
  3468. else if (VecWidth == 256 && !High)
  3469. IID = Intrinsic::x86_avx512_vpmadd52l_uq_256;
  3470. else if (VecWidth == 512 && !High)
  3471. IID = Intrinsic::x86_avx512_vpmadd52l_uq_512;
  3472. else if (VecWidth == 128 && High)
  3473. IID = Intrinsic::x86_avx512_vpmadd52h_uq_128;
  3474. else if (VecWidth == 256 && High)
  3475. IID = Intrinsic::x86_avx512_vpmadd52h_uq_256;
  3476. else if (VecWidth == 512 && High)
  3477. IID = Intrinsic::x86_avx512_vpmadd52h_uq_512;
  3478. else
  3479. llvm_unreachable("Unexpected intrinsic");
  3480. Value *Args[] = { CI->getArgOperand(0) , CI->getArgOperand(1),
  3481. CI->getArgOperand(2) };
  3482. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(), IID),
  3483. Args);
  3484. Value *PassThru = ZeroMask ? ConstantAggregateZero::get(CI->getType())
  3485. : CI->getArgOperand(0);
  3486. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, PassThru);
  3487. } else if (IsX86 && (Name.startswith("avx512.mask.vpermi2var.") ||
  3488. Name.startswith("avx512.mask.vpermt2var.") ||
  3489. Name.startswith("avx512.maskz.vpermt2var."))) {
  3490. bool ZeroMask = Name[11] == 'z';
  3491. bool IndexForm = Name[17] == 'i';
  3492. Rep = UpgradeX86VPERMT2Intrinsics(Builder, *CI, ZeroMask, IndexForm);
  3493. } else if (IsX86 && (Name.startswith("avx512.mask.vpdpbusd.") ||
  3494. Name.startswith("avx512.maskz.vpdpbusd.") ||
  3495. Name.startswith("avx512.mask.vpdpbusds.") ||
  3496. Name.startswith("avx512.maskz.vpdpbusds."))) {
  3497. bool ZeroMask = Name[11] == 'z';
  3498. bool IsSaturating = Name[ZeroMask ? 21 : 20] == 's';
  3499. unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
  3500. Intrinsic::ID IID;
  3501. if (VecWidth == 128 && !IsSaturating)
  3502. IID = Intrinsic::x86_avx512_vpdpbusd_128;
  3503. else if (VecWidth == 256 && !IsSaturating)
  3504. IID = Intrinsic::x86_avx512_vpdpbusd_256;
  3505. else if (VecWidth == 512 && !IsSaturating)
  3506. IID = Intrinsic::x86_avx512_vpdpbusd_512;
  3507. else if (VecWidth == 128 && IsSaturating)
  3508. IID = Intrinsic::x86_avx512_vpdpbusds_128;
  3509. else if (VecWidth == 256 && IsSaturating)
  3510. IID = Intrinsic::x86_avx512_vpdpbusds_256;
  3511. else if (VecWidth == 512 && IsSaturating)
  3512. IID = Intrinsic::x86_avx512_vpdpbusds_512;
  3513. else
  3514. llvm_unreachable("Unexpected intrinsic");
  3515. Value *Args[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3516. CI->getArgOperand(2) };
  3517. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(), IID),
  3518. Args);
  3519. Value *PassThru = ZeroMask ? ConstantAggregateZero::get(CI->getType())
  3520. : CI->getArgOperand(0);
  3521. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, PassThru);
  3522. } else if (IsX86 && (Name.startswith("avx512.mask.vpdpwssd.") ||
  3523. Name.startswith("avx512.maskz.vpdpwssd.") ||
  3524. Name.startswith("avx512.mask.vpdpwssds.") ||
  3525. Name.startswith("avx512.maskz.vpdpwssds."))) {
  3526. bool ZeroMask = Name[11] == 'z';
  3527. bool IsSaturating = Name[ZeroMask ? 21 : 20] == 's';
  3528. unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
  3529. Intrinsic::ID IID;
  3530. if (VecWidth == 128 && !IsSaturating)
  3531. IID = Intrinsic::x86_avx512_vpdpwssd_128;
  3532. else if (VecWidth == 256 && !IsSaturating)
  3533. IID = Intrinsic::x86_avx512_vpdpwssd_256;
  3534. else if (VecWidth == 512 && !IsSaturating)
  3535. IID = Intrinsic::x86_avx512_vpdpwssd_512;
  3536. else if (VecWidth == 128 && IsSaturating)
  3537. IID = Intrinsic::x86_avx512_vpdpwssds_128;
  3538. else if (VecWidth == 256 && IsSaturating)
  3539. IID = Intrinsic::x86_avx512_vpdpwssds_256;
  3540. else if (VecWidth == 512 && IsSaturating)
  3541. IID = Intrinsic::x86_avx512_vpdpwssds_512;
  3542. else
  3543. llvm_unreachable("Unexpected intrinsic");
  3544. Value *Args[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3545. CI->getArgOperand(2) };
  3546. Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(), IID),
  3547. Args);
  3548. Value *PassThru = ZeroMask ? ConstantAggregateZero::get(CI->getType())
  3549. : CI->getArgOperand(0);
  3550. Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, PassThru);
  3551. } else if (IsX86 && (Name == "addcarryx.u32" || Name == "addcarryx.u64" ||
  3552. Name == "addcarry.u32" || Name == "addcarry.u64" ||
  3553. Name == "subborrow.u32" || Name == "subborrow.u64")) {
  3554. Intrinsic::ID IID;
  3555. if (Name[0] == 'a' && Name.back() == '2')
  3556. IID = Intrinsic::x86_addcarry_32;
  3557. else if (Name[0] == 'a' && Name.back() == '4')
  3558. IID = Intrinsic::x86_addcarry_64;
  3559. else if (Name[0] == 's' && Name.back() == '2')
  3560. IID = Intrinsic::x86_subborrow_32;
  3561. else if (Name[0] == 's' && Name.back() == '4')
  3562. IID = Intrinsic::x86_subborrow_64;
  3563. else
  3564. llvm_unreachable("Unexpected intrinsic");
  3565. // Make a call with 3 operands.
  3566. Value *Args[] = { CI->getArgOperand(0), CI->getArgOperand(1),
  3567. CI->getArgOperand(2)};
  3568. Value *NewCall = Builder.CreateCall(
  3569. Intrinsic::getDeclaration(CI->getModule(), IID),
  3570. Args);
  3571. // Extract the second result and store it.
  3572. Value *Data = Builder.CreateExtractValue(NewCall, 1);
  3573. // Cast the pointer to the right type.
  3574. Value *Ptr = Builder.CreateBitCast(CI->getArgOperand(3),
  3575. llvm::PointerType::getUnqual(Data->getType()));
  3576. Builder.CreateAlignedStore(Data, Ptr, Align(1));
  3577. // Replace the original call result with the first result of the new call.
  3578. Value *CF = Builder.CreateExtractValue(NewCall, 0);
  3579. CI->replaceAllUsesWith(CF);
  3580. Rep = nullptr;
  3581. } else if (IsX86 && Name.startswith("avx512.mask.") &&
  3582. upgradeAVX512MaskToSelect(Name, Builder, *CI, Rep)) {
  3583. // Rep will be updated by the call in the condition.
  3584. } else if (IsNVVM && (Name == "abs.i" || Name == "abs.ll")) {
  3585. Value *Arg = CI->getArgOperand(0);
  3586. Value *Neg = Builder.CreateNeg(Arg, "neg");
  3587. Value *Cmp = Builder.CreateICmpSGE(
  3588. Arg, llvm::Constant::getNullValue(Arg->getType()), "abs.cond");
  3589. Rep = Builder.CreateSelect(Cmp, Arg, Neg, "abs");
  3590. } else if (IsNVVM && (Name.startswith("atomic.load.add.f32.p") ||
  3591. Name.startswith("atomic.load.add.f64.p"))) {
  3592. Value *Ptr = CI->getArgOperand(0);
  3593. Value *Val = CI->getArgOperand(1);
  3594. Rep = Builder.CreateAtomicRMW(AtomicRMWInst::FAdd, Ptr, Val, MaybeAlign(),
  3595. AtomicOrdering::SequentiallyConsistent);
  3596. } else if (IsNVVM && (Name == "max.i" || Name == "max.ll" ||
  3597. Name == "max.ui" || Name == "max.ull")) {
  3598. Value *Arg0 = CI->getArgOperand(0);
  3599. Value *Arg1 = CI->getArgOperand(1);
  3600. Value *Cmp = Name.endswith(".ui") || Name.endswith(".ull")
  3601. ? Builder.CreateICmpUGE(Arg0, Arg1, "max.cond")
  3602. : Builder.CreateICmpSGE(Arg0, Arg1, "max.cond");
  3603. Rep = Builder.CreateSelect(Cmp, Arg0, Arg1, "max");
  3604. } else if (IsNVVM && (Name == "min.i" || Name == "min.ll" ||
  3605. Name == "min.ui" || Name == "min.ull")) {
  3606. Value *Arg0 = CI->getArgOperand(0);
  3607. Value *Arg1 = CI->getArgOperand(1);
  3608. Value *Cmp = Name.endswith(".ui") || Name.endswith(".ull")
  3609. ? Builder.CreateICmpULE(Arg0, Arg1, "min.cond")
  3610. : Builder.CreateICmpSLE(Arg0, Arg1, "min.cond");
  3611. Rep = Builder.CreateSelect(Cmp, Arg0, Arg1, "min");
  3612. } else if (IsNVVM && Name == "clz.ll") {
  3613. // llvm.nvvm.clz.ll returns an i32, but llvm.ctlz.i64 and returns an i64.
  3614. Value *Arg = CI->getArgOperand(0);
  3615. Value *Ctlz = Builder.CreateCall(
  3616. Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
  3617. {Arg->getType()}),
  3618. {Arg, Builder.getFalse()}, "ctlz");
  3619. Rep = Builder.CreateTrunc(Ctlz, Builder.getInt32Ty(), "ctlz.trunc");
  3620. } else if (IsNVVM && Name == "popc.ll") {
  3621. // llvm.nvvm.popc.ll returns an i32, but llvm.ctpop.i64 and returns an
  3622. // i64.
  3623. Value *Arg = CI->getArgOperand(0);
  3624. Value *Popc = Builder.CreateCall(
  3625. Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
  3626. {Arg->getType()}),
  3627. Arg, "ctpop");
  3628. Rep = Builder.CreateTrunc(Popc, Builder.getInt32Ty(), "ctpop.trunc");
  3629. } else if (IsNVVM && Name == "h2f") {
  3630. Rep = Builder.CreateCall(Intrinsic::getDeclaration(
  3631. F->getParent(), Intrinsic::convert_from_fp16,
  3632. {Builder.getFloatTy()}),
  3633. CI->getArgOperand(0), "h2f");
  3634. } else if (IsARM) {
  3635. Rep = UpgradeARMIntrinsicCall(Name, CI, F, Builder);
  3636. } else {
  3637. llvm_unreachable("Unknown function for CallBase upgrade.");
  3638. }
  3639. if (Rep)
  3640. CI->replaceAllUsesWith(Rep);
  3641. CI->eraseFromParent();
  3642. return;
  3643. }
  3644. const auto &DefaultCase = [&]() -> void {
  3645. if (CI->getFunctionType() == NewFn->getFunctionType()) {
  3646. // Handle generic mangling change.
  3647. assert(
  3648. (CI->getCalledFunction()->getName() != NewFn->getName()) &&
  3649. "Unknown function for CallBase upgrade and isn't just a name change");
  3650. CI->setCalledFunction(NewFn);
  3651. return;
  3652. }
  3653. // This must be an upgrade from a named to a literal struct.
  3654. if (auto *OldST = dyn_cast<StructType>(CI->getType())) {
  3655. assert(OldST != NewFn->getReturnType() &&
  3656. "Return type must have changed");
  3657. assert(OldST->getNumElements() ==
  3658. cast<StructType>(NewFn->getReturnType())->getNumElements() &&
  3659. "Must have same number of elements");
  3660. SmallVector<Value *> Args(CI->args());
  3661. Value *NewCI = Builder.CreateCall(NewFn, Args);
  3662. Value *Res = PoisonValue::get(OldST);
  3663. for (unsigned Idx = 0; Idx < OldST->getNumElements(); ++Idx) {
  3664. Value *Elem = Builder.CreateExtractValue(NewCI, Idx);
  3665. Res = Builder.CreateInsertValue(Res, Elem, Idx);
  3666. }
  3667. CI->replaceAllUsesWith(Res);
  3668. CI->eraseFromParent();
  3669. return;
  3670. }
  3671. // We're probably about to produce something invalid. Let the verifier catch
  3672. // it instead of dying here.
  3673. CI->setCalledOperand(
  3674. ConstantExpr::getPointerCast(NewFn, CI->getCalledOperand()->getType()));
  3675. return;
  3676. };
  3677. CallInst *NewCall = nullptr;
  3678. switch (NewFn->getIntrinsicID()) {
  3679. default: {
  3680. DefaultCase();
  3681. return;
  3682. }
  3683. case Intrinsic::arm_neon_vst1:
  3684. case Intrinsic::arm_neon_vst2:
  3685. case Intrinsic::arm_neon_vst3:
  3686. case Intrinsic::arm_neon_vst4:
  3687. case Intrinsic::arm_neon_vst2lane:
  3688. case Intrinsic::arm_neon_vst3lane:
  3689. case Intrinsic::arm_neon_vst4lane: {
  3690. SmallVector<Value *, 4> Args(CI->args());
  3691. NewCall = Builder.CreateCall(NewFn, Args);
  3692. break;
  3693. }
  3694. case Intrinsic::aarch64_sve_bfmlalb_lane_v2:
  3695. case Intrinsic::aarch64_sve_bfmlalt_lane_v2:
  3696. case Intrinsic::aarch64_sve_bfdot_lane_v2: {
  3697. LLVMContext &Ctx = F->getParent()->getContext();
  3698. SmallVector<Value *, 4> Args(CI->args());
  3699. Args[3] = ConstantInt::get(Type::getInt32Ty(Ctx),
  3700. cast<ConstantInt>(Args[3])->getZExtValue());
  3701. NewCall = Builder.CreateCall(NewFn, Args);
  3702. break;
  3703. }
  3704. case Intrinsic::aarch64_sve_ld3_sret:
  3705. case Intrinsic::aarch64_sve_ld4_sret:
  3706. case Intrinsic::aarch64_sve_ld2_sret: {
  3707. StringRef Name = F->getName();
  3708. Name = Name.substr(5);
  3709. unsigned N = StringSwitch<unsigned>(Name)
  3710. .StartsWith("aarch64.sve.ld2", 2)
  3711. .StartsWith("aarch64.sve.ld3", 3)
  3712. .StartsWith("aarch64.sve.ld4", 4)
  3713. .Default(0);
  3714. ScalableVectorType *RetTy =
  3715. dyn_cast<ScalableVectorType>(F->getReturnType());
  3716. unsigned MinElts = RetTy->getMinNumElements() / N;
  3717. SmallVector<Value *, 2> Args(CI->args());
  3718. Value *NewLdCall = Builder.CreateCall(NewFn, Args);
  3719. Value *Ret = llvm::PoisonValue::get(RetTy);
  3720. for (unsigned I = 0; I < N; I++) {
  3721. Value *Idx = ConstantInt::get(Type::getInt64Ty(C), I * MinElts);
  3722. Value *SRet = Builder.CreateExtractValue(NewLdCall, I);
  3723. Ret = Builder.CreateInsertVector(RetTy, Ret, SRet, Idx);
  3724. }
  3725. NewCall = dyn_cast<CallInst>(Ret);
  3726. break;
  3727. }
  3728. case Intrinsic::vector_extract: {
  3729. StringRef Name = F->getName();
  3730. Name = Name.substr(5); // Strip llvm
  3731. if (!Name.startswith("aarch64.sve.tuple.get")) {
  3732. DefaultCase();
  3733. return;
  3734. }
  3735. ScalableVectorType *RetTy =
  3736. dyn_cast<ScalableVectorType>(F->getReturnType());
  3737. unsigned MinElts = RetTy->getMinNumElements();
  3738. unsigned I = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  3739. Value *NewIdx = ConstantInt::get(Type::getInt64Ty(C), I * MinElts);
  3740. NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0), NewIdx});
  3741. break;
  3742. }
  3743. case Intrinsic::vector_insert: {
  3744. StringRef Name = F->getName();
  3745. Name = Name.substr(5);
  3746. if (!Name.startswith("aarch64.sve.tuple")) {
  3747. DefaultCase();
  3748. return;
  3749. }
  3750. if (Name.startswith("aarch64.sve.tuple.set")) {
  3751. unsigned I = dyn_cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
  3752. ScalableVectorType *Ty =
  3753. dyn_cast<ScalableVectorType>(CI->getArgOperand(2)->getType());
  3754. Value *NewIdx =
  3755. ConstantInt::get(Type::getInt64Ty(C), I * Ty->getMinNumElements());
  3756. NewCall = Builder.CreateCall(
  3757. NewFn, {CI->getArgOperand(0), CI->getArgOperand(2), NewIdx});
  3758. break;
  3759. }
  3760. if (Name.startswith("aarch64.sve.tuple.create")) {
  3761. unsigned N = StringSwitch<unsigned>(Name)
  3762. .StartsWith("aarch64.sve.tuple.create2", 2)
  3763. .StartsWith("aarch64.sve.tuple.create3", 3)
  3764. .StartsWith("aarch64.sve.tuple.create4", 4)
  3765. .Default(0);
  3766. assert(N > 1 && "Create is expected to be between 2-4");
  3767. ScalableVectorType *RetTy =
  3768. dyn_cast<ScalableVectorType>(F->getReturnType());
  3769. Value *Ret = llvm::PoisonValue::get(RetTy);
  3770. unsigned MinElts = RetTy->getMinNumElements() / N;
  3771. for (unsigned I = 0; I < N; I++) {
  3772. Value *Idx = ConstantInt::get(Type::getInt64Ty(C), I * MinElts);
  3773. Value *V = CI->getArgOperand(I);
  3774. Ret = Builder.CreateInsertVector(RetTy, Ret, V, Idx);
  3775. }
  3776. NewCall = dyn_cast<CallInst>(Ret);
  3777. }
  3778. break;
  3779. }
  3780. case Intrinsic::arm_neon_bfdot:
  3781. case Intrinsic::arm_neon_bfmmla:
  3782. case Intrinsic::arm_neon_bfmlalb:
  3783. case Intrinsic::arm_neon_bfmlalt:
  3784. case Intrinsic::aarch64_neon_bfdot:
  3785. case Intrinsic::aarch64_neon_bfmmla:
  3786. case Intrinsic::aarch64_neon_bfmlalb:
  3787. case Intrinsic::aarch64_neon_bfmlalt: {
  3788. SmallVector<Value *, 3> Args;
  3789. assert(CI->arg_size() == 3 &&
  3790. "Mismatch between function args and call args");
  3791. size_t OperandWidth =
  3792. CI->getArgOperand(1)->getType()->getPrimitiveSizeInBits();
  3793. assert((OperandWidth == 64 || OperandWidth == 128) &&
  3794. "Unexpected operand width");
  3795. Type *NewTy = FixedVectorType::get(Type::getBFloatTy(C), OperandWidth / 16);
  3796. auto Iter = CI->args().begin();
  3797. Args.push_back(*Iter++);
  3798. Args.push_back(Builder.CreateBitCast(*Iter++, NewTy));
  3799. Args.push_back(Builder.CreateBitCast(*Iter++, NewTy));
  3800. NewCall = Builder.CreateCall(NewFn, Args);
  3801. break;
  3802. }
  3803. case Intrinsic::bitreverse:
  3804. NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0)});
  3805. break;
  3806. case Intrinsic::ctlz:
  3807. case Intrinsic::cttz:
  3808. assert(CI->arg_size() == 1 &&
  3809. "Mismatch between function args and call args");
  3810. NewCall =
  3811. Builder.CreateCall(NewFn, {CI->getArgOperand(0), Builder.getFalse()});
  3812. break;
  3813. case Intrinsic::objectsize: {
  3814. Value *NullIsUnknownSize =
  3815. CI->arg_size() == 2 ? Builder.getFalse() : CI->getArgOperand(2);
  3816. Value *Dynamic =
  3817. CI->arg_size() < 4 ? Builder.getFalse() : CI->getArgOperand(3);
  3818. NewCall = Builder.CreateCall(
  3819. NewFn, {CI->getArgOperand(0), CI->getArgOperand(1), NullIsUnknownSize, Dynamic});
  3820. break;
  3821. }
  3822. case Intrinsic::ctpop:
  3823. NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0)});
  3824. break;
  3825. case Intrinsic::convert_from_fp16:
  3826. NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0)});
  3827. break;
  3828. case Intrinsic::dbg_value:
  3829. // Upgrade from the old version that had an extra offset argument.
  3830. assert(CI->arg_size() == 4);
  3831. // Drop nonzero offsets instead of attempting to upgrade them.
  3832. if (auto *Offset = dyn_cast_or_null<Constant>(CI->getArgOperand(1)))
  3833. if (Offset->isZeroValue()) {
  3834. NewCall = Builder.CreateCall(
  3835. NewFn,
  3836. {CI->getArgOperand(0), CI->getArgOperand(2), CI->getArgOperand(3)});
  3837. break;
  3838. }
  3839. CI->eraseFromParent();
  3840. return;
  3841. case Intrinsic::ptr_annotation:
  3842. // Upgrade from versions that lacked the annotation attribute argument.
  3843. if (CI->arg_size() != 4) {
  3844. DefaultCase();
  3845. return;
  3846. }
  3847. // Create a new call with an added null annotation attribute argument.
  3848. NewCall = Builder.CreateCall(
  3849. NewFn,
  3850. {CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
  3851. CI->getArgOperand(3), Constant::getNullValue(Builder.getInt8PtrTy())});
  3852. NewCall->takeName(CI);
  3853. CI->replaceAllUsesWith(NewCall);
  3854. CI->eraseFromParent();
  3855. return;
  3856. case Intrinsic::var_annotation:
  3857. // Upgrade from versions that lacked the annotation attribute argument.
  3858. if (CI->arg_size() != 4) {
  3859. DefaultCase();
  3860. return;
  3861. }
  3862. // Create a new call with an added null annotation attribute argument.
  3863. NewCall = Builder.CreateCall(
  3864. NewFn,
  3865. {CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
  3866. CI->getArgOperand(3), Constant::getNullValue(Builder.getInt8PtrTy())});
  3867. NewCall->takeName(CI);
  3868. CI->replaceAllUsesWith(NewCall);
  3869. CI->eraseFromParent();
  3870. return;
  3871. case Intrinsic::x86_xop_vfrcz_ss:
  3872. case Intrinsic::x86_xop_vfrcz_sd:
  3873. NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(1)});
  3874. break;
  3875. case Intrinsic::x86_xop_vpermil2pd:
  3876. case Intrinsic::x86_xop_vpermil2ps:
  3877. case Intrinsic::x86_xop_vpermil2pd_256:
  3878. case Intrinsic::x86_xop_vpermil2ps_256: {
  3879. SmallVector<Value *, 4> Args(CI->args());
  3880. VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
  3881. VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
  3882. Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
  3883. NewCall = Builder.CreateCall(NewFn, Args);
  3884. break;
  3885. }
  3886. case Intrinsic::x86_sse41_ptestc:
  3887. case Intrinsic::x86_sse41_ptestz:
  3888. case Intrinsic::x86_sse41_ptestnzc: {
  3889. // The arguments for these intrinsics used to be v4f32, and changed
  3890. // to v2i64. This is purely a nop, since those are bitwise intrinsics.
  3891. // So, the only thing required is a bitcast for both arguments.
  3892. // First, check the arguments have the old type.
  3893. Value *Arg0 = CI->getArgOperand(0);
  3894. if (Arg0->getType() != FixedVectorType::get(Type::getFloatTy(C), 4))
  3895. return;
  3896. // Old intrinsic, add bitcasts
  3897. Value *Arg1 = CI->getArgOperand(1);
  3898. auto *NewVecTy = FixedVectorType::get(Type::getInt64Ty(C), 2);
  3899. Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
  3900. Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
  3901. NewCall = Builder.CreateCall(NewFn, {BC0, BC1});
  3902. break;
  3903. }
  3904. case Intrinsic::x86_rdtscp: {
  3905. // This used to take 1 arguments. If we have no arguments, it is already
  3906. // upgraded.
  3907. if (CI->getNumOperands() == 0)
  3908. return;
  3909. NewCall = Builder.CreateCall(NewFn);
  3910. // Extract the second result and store it.
  3911. Value *Data = Builder.CreateExtractValue(NewCall, 1);
  3912. // Cast the pointer to the right type.
  3913. Value *Ptr = Builder.CreateBitCast(CI->getArgOperand(0),
  3914. llvm::PointerType::getUnqual(Data->getType()));
  3915. Builder.CreateAlignedStore(Data, Ptr, Align(1));
  3916. // Replace the original call result with the first result of the new call.
  3917. Value *TSC = Builder.CreateExtractValue(NewCall, 0);
  3918. NewCall->takeName(CI);
  3919. CI->replaceAllUsesWith(TSC);
  3920. CI->eraseFromParent();
  3921. return;
  3922. }
  3923. case Intrinsic::x86_sse41_insertps:
  3924. case Intrinsic::x86_sse41_dppd:
  3925. case Intrinsic::x86_sse41_dpps:
  3926. case Intrinsic::x86_sse41_mpsadbw:
  3927. case Intrinsic::x86_avx_dp_ps_256:
  3928. case Intrinsic::x86_avx2_mpsadbw: {
  3929. // Need to truncate the last argument from i32 to i8 -- this argument models
  3930. // an inherently 8-bit immediate operand to these x86 instructions.
  3931. SmallVector<Value *, 4> Args(CI->args());
  3932. // Replace the last argument with a trunc.
  3933. Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
  3934. NewCall = Builder.CreateCall(NewFn, Args);
  3935. break;
  3936. }
  3937. case Intrinsic::x86_avx512_mask_cmp_pd_128:
  3938. case Intrinsic::x86_avx512_mask_cmp_pd_256:
  3939. case Intrinsic::x86_avx512_mask_cmp_pd_512:
  3940. case Intrinsic::x86_avx512_mask_cmp_ps_128:
  3941. case Intrinsic::x86_avx512_mask_cmp_ps_256:
  3942. case Intrinsic::x86_avx512_mask_cmp_ps_512: {
  3943. SmallVector<Value *, 4> Args(CI->args());
  3944. unsigned NumElts =
  3945. cast<FixedVectorType>(Args[0]->getType())->getNumElements();
  3946. Args[3] = getX86MaskVec(Builder, Args[3], NumElts);
  3947. NewCall = Builder.CreateCall(NewFn, Args);
  3948. Value *Res = ApplyX86MaskOn1BitsVec(Builder, NewCall, nullptr);
  3949. NewCall->takeName(CI);
  3950. CI->replaceAllUsesWith(Res);
  3951. CI->eraseFromParent();
  3952. return;
  3953. }
  3954. case Intrinsic::x86_avx512bf16_cvtne2ps2bf16_128:
  3955. case Intrinsic::x86_avx512bf16_cvtne2ps2bf16_256:
  3956. case Intrinsic::x86_avx512bf16_cvtne2ps2bf16_512:
  3957. case Intrinsic::x86_avx512bf16_mask_cvtneps2bf16_128:
  3958. case Intrinsic::x86_avx512bf16_cvtneps2bf16_256:
  3959. case Intrinsic::x86_avx512bf16_cvtneps2bf16_512: {
  3960. SmallVector<Value *, 4> Args(CI->args());
  3961. unsigned NumElts = cast<FixedVectorType>(CI->getType())->getNumElements();
  3962. if (NewFn->getIntrinsicID() ==
  3963. Intrinsic::x86_avx512bf16_mask_cvtneps2bf16_128)
  3964. Args[1] = Builder.CreateBitCast(
  3965. Args[1], FixedVectorType::get(Builder.getBFloatTy(), NumElts));
  3966. NewCall = Builder.CreateCall(NewFn, Args);
  3967. Value *Res = Builder.CreateBitCast(
  3968. NewCall, FixedVectorType::get(Builder.getInt16Ty(), NumElts));
  3969. NewCall->takeName(CI);
  3970. CI->replaceAllUsesWith(Res);
  3971. CI->eraseFromParent();
  3972. return;
  3973. }
  3974. case Intrinsic::x86_avx512bf16_dpbf16ps_128:
  3975. case Intrinsic::x86_avx512bf16_dpbf16ps_256:
  3976. case Intrinsic::x86_avx512bf16_dpbf16ps_512:{
  3977. SmallVector<Value *, 4> Args(CI->args());
  3978. unsigned NumElts =
  3979. cast<FixedVectorType>(CI->getType())->getNumElements() * 2;
  3980. Args[1] = Builder.CreateBitCast(
  3981. Args[1], FixedVectorType::get(Builder.getBFloatTy(), NumElts));
  3982. Args[2] = Builder.CreateBitCast(
  3983. Args[2], FixedVectorType::get(Builder.getBFloatTy(), NumElts));
  3984. NewCall = Builder.CreateCall(NewFn, Args);
  3985. break;
  3986. }
  3987. case Intrinsic::thread_pointer: {
  3988. NewCall = Builder.CreateCall(NewFn, {});
  3989. break;
  3990. }
  3991. case Intrinsic::invariant_start:
  3992. case Intrinsic::invariant_end: {
  3993. SmallVector<Value *, 4> Args(CI->args());
  3994. NewCall = Builder.CreateCall(NewFn, Args);
  3995. break;
  3996. }
  3997. case Intrinsic::masked_load:
  3998. case Intrinsic::masked_store:
  3999. case Intrinsic::masked_gather:
  4000. case Intrinsic::masked_scatter: {
  4001. SmallVector<Value *, 4> Args(CI->args());
  4002. NewCall = Builder.CreateCall(NewFn, Args);
  4003. NewCall->copyMetadata(*CI);
  4004. break;
  4005. }
  4006. case Intrinsic::memcpy:
  4007. case Intrinsic::memmove:
  4008. case Intrinsic::memset: {
  4009. // We have to make sure that the call signature is what we're expecting.
  4010. // We only want to change the old signatures by removing the alignment arg:
  4011. // @llvm.mem[cpy|move]...(i8*, i8*, i[32|i64], i32, i1)
  4012. // -> @llvm.mem[cpy|move]...(i8*, i8*, i[32|i64], i1)
  4013. // @llvm.memset...(i8*, i8, i[32|64], i32, i1)
  4014. // -> @llvm.memset...(i8*, i8, i[32|64], i1)
  4015. // Note: i8*'s in the above can be any pointer type
  4016. if (CI->arg_size() != 5) {
  4017. DefaultCase();
  4018. return;
  4019. }
  4020. // Remove alignment argument (3), and add alignment attributes to the
  4021. // dest/src pointers.
  4022. Value *Args[4] = {CI->getArgOperand(0), CI->getArgOperand(1),
  4023. CI->getArgOperand(2), CI->getArgOperand(4)};
  4024. NewCall = Builder.CreateCall(NewFn, Args);
  4025. AttributeList OldAttrs = CI->getAttributes();
  4026. AttributeList NewAttrs = AttributeList::get(
  4027. C, OldAttrs.getFnAttrs(), OldAttrs.getRetAttrs(),
  4028. {OldAttrs.getParamAttrs(0), OldAttrs.getParamAttrs(1),
  4029. OldAttrs.getParamAttrs(2), OldAttrs.getParamAttrs(4)});
  4030. NewCall->setAttributes(NewAttrs);
  4031. auto *MemCI = cast<MemIntrinsic>(NewCall);
  4032. // All mem intrinsics support dest alignment.
  4033. const ConstantInt *Align = cast<ConstantInt>(CI->getArgOperand(3));
  4034. MemCI->setDestAlignment(Align->getMaybeAlignValue());
  4035. // Memcpy/Memmove also support source alignment.
  4036. if (auto *MTI = dyn_cast<MemTransferInst>(MemCI))
  4037. MTI->setSourceAlignment(Align->getMaybeAlignValue());
  4038. break;
  4039. }
  4040. }
  4041. assert(NewCall && "Should have either set this variable or returned through "
  4042. "the default case");
  4043. NewCall->takeName(CI);
  4044. CI->replaceAllUsesWith(NewCall);
  4045. CI->eraseFromParent();
  4046. }
  4047. void llvm::UpgradeCallsToIntrinsic(Function *F) {
  4048. assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
  4049. // Check if this function should be upgraded and get the replacement function
  4050. // if there is one.
  4051. Function *NewFn;
  4052. if (UpgradeIntrinsicFunction(F, NewFn)) {
  4053. // Replace all users of the old function with the new function or new
  4054. // instructions. This is not a range loop because the call is deleted.
  4055. for (User *U : make_early_inc_range(F->users()))
  4056. if (CallBase *CB = dyn_cast<CallBase>(U))
  4057. UpgradeIntrinsicCall(CB, NewFn);
  4058. // Remove old function, no longer used, from the module.
  4059. F->eraseFromParent();
  4060. }
  4061. }
  4062. MDNode *llvm::UpgradeTBAANode(MDNode &MD) {
  4063. // Check if the tag uses struct-path aware TBAA format.
  4064. if (isa<MDNode>(MD.getOperand(0)) && MD.getNumOperands() >= 3)
  4065. return &MD;
  4066. auto &Context = MD.getContext();
  4067. if (MD.getNumOperands() == 3) {
  4068. Metadata *Elts[] = {MD.getOperand(0), MD.getOperand(1)};
  4069. MDNode *ScalarType = MDNode::get(Context, Elts);
  4070. // Create a MDNode <ScalarType, ScalarType, offset 0, const>
  4071. Metadata *Elts2[] = {ScalarType, ScalarType,
  4072. ConstantAsMetadata::get(
  4073. Constant::getNullValue(Type::getInt64Ty(Context))),
  4074. MD.getOperand(2)};
  4075. return MDNode::get(Context, Elts2);
  4076. }
  4077. // Create a MDNode <MD, MD, offset 0>
  4078. Metadata *Elts[] = {&MD, &MD, ConstantAsMetadata::get(Constant::getNullValue(
  4079. Type::getInt64Ty(Context)))};
  4080. return MDNode::get(Context, Elts);
  4081. }
  4082. Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
  4083. Instruction *&Temp) {
  4084. if (Opc != Instruction::BitCast)
  4085. return nullptr;
  4086. Temp = nullptr;
  4087. Type *SrcTy = V->getType();
  4088. if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
  4089. SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
  4090. LLVMContext &Context = V->getContext();
  4091. // We have no information about target data layout, so we assume that
  4092. // the maximum pointer size is 64bit.
  4093. Type *MidTy = Type::getInt64Ty(Context);
  4094. Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
  4095. return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
  4096. }
  4097. return nullptr;
  4098. }
  4099. Constant *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
  4100. if (Opc != Instruction::BitCast)
  4101. return nullptr;
  4102. Type *SrcTy = C->getType();
  4103. if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
  4104. SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
  4105. LLVMContext &Context = C->getContext();
  4106. // We have no information about target data layout, so we assume that
  4107. // the maximum pointer size is 64bit.
  4108. Type *MidTy = Type::getInt64Ty(Context);
  4109. return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
  4110. DestTy);
  4111. }
  4112. return nullptr;
  4113. }
  4114. /// Check the debug info version number, if it is out-dated, drop the debug
  4115. /// info. Return true if module is modified.
  4116. bool llvm::UpgradeDebugInfo(Module &M) {
  4117. unsigned Version = getDebugMetadataVersionFromModule(M);
  4118. if (Version == DEBUG_METADATA_VERSION) {
  4119. bool BrokenDebugInfo = false;
  4120. if (verifyModule(M, &llvm::errs(), &BrokenDebugInfo))
  4121. report_fatal_error("Broken module found, compilation aborted!");
  4122. if (!BrokenDebugInfo)
  4123. // Everything is ok.
  4124. return false;
  4125. else {
  4126. // Diagnose malformed debug info.
  4127. DiagnosticInfoIgnoringInvalidDebugMetadata Diag(M);
  4128. M.getContext().diagnose(Diag);
  4129. }
  4130. }
  4131. bool Modified = StripDebugInfo(M);
  4132. if (Modified && Version != DEBUG_METADATA_VERSION) {
  4133. // Diagnose a version mismatch.
  4134. DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
  4135. M.getContext().diagnose(DiagVersion);
  4136. }
  4137. return Modified;
  4138. }
  4139. /// This checks for objc retain release marker which should be upgraded. It
  4140. /// returns true if module is modified.
  4141. static bool UpgradeRetainReleaseMarker(Module &M) {
  4142. bool Changed = false;
  4143. const char *MarkerKey = "clang.arc.retainAutoreleasedReturnValueMarker";
  4144. NamedMDNode *ModRetainReleaseMarker = M.getNamedMetadata(MarkerKey);
  4145. if (ModRetainReleaseMarker) {
  4146. MDNode *Op = ModRetainReleaseMarker->getOperand(0);
  4147. if (Op) {
  4148. MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(0));
  4149. if (ID) {
  4150. SmallVector<StringRef, 4> ValueComp;
  4151. ID->getString().split(ValueComp, "#");
  4152. if (ValueComp.size() == 2) {
  4153. std::string NewValue = ValueComp[0].str() + ";" + ValueComp[1].str();
  4154. ID = MDString::get(M.getContext(), NewValue);
  4155. }
  4156. M.addModuleFlag(Module::Error, MarkerKey, ID);
  4157. M.eraseNamedMetadata(ModRetainReleaseMarker);
  4158. Changed = true;
  4159. }
  4160. }
  4161. }
  4162. return Changed;
  4163. }
  4164. void llvm::UpgradeARCRuntime(Module &M) {
  4165. // This lambda converts normal function calls to ARC runtime functions to
  4166. // intrinsic calls.
  4167. auto UpgradeToIntrinsic = [&](const char *OldFunc,
  4168. llvm::Intrinsic::ID IntrinsicFunc) {
  4169. Function *Fn = M.getFunction(OldFunc);
  4170. if (!Fn)
  4171. return;
  4172. Function *NewFn = llvm::Intrinsic::getDeclaration(&M, IntrinsicFunc);
  4173. for (User *U : make_early_inc_range(Fn->users())) {
  4174. CallInst *CI = dyn_cast<CallInst>(U);
  4175. if (!CI || CI->getCalledFunction() != Fn)
  4176. continue;
  4177. IRBuilder<> Builder(CI->getParent(), CI->getIterator());
  4178. FunctionType *NewFuncTy = NewFn->getFunctionType();
  4179. SmallVector<Value *, 2> Args;
  4180. // Don't upgrade the intrinsic if it's not valid to bitcast the return
  4181. // value to the return type of the old function.
  4182. if (NewFuncTy->getReturnType() != CI->getType() &&
  4183. !CastInst::castIsValid(Instruction::BitCast, CI,
  4184. NewFuncTy->getReturnType()))
  4185. continue;
  4186. bool InvalidCast = false;
  4187. for (unsigned I = 0, E = CI->arg_size(); I != E; ++I) {
  4188. Value *Arg = CI->getArgOperand(I);
  4189. // Bitcast argument to the parameter type of the new function if it's
  4190. // not a variadic argument.
  4191. if (I < NewFuncTy->getNumParams()) {
  4192. // Don't upgrade the intrinsic if it's not valid to bitcast the argument
  4193. // to the parameter type of the new function.
  4194. if (!CastInst::castIsValid(Instruction::BitCast, Arg,
  4195. NewFuncTy->getParamType(I))) {
  4196. InvalidCast = true;
  4197. break;
  4198. }
  4199. Arg = Builder.CreateBitCast(Arg, NewFuncTy->getParamType(I));
  4200. }
  4201. Args.push_back(Arg);
  4202. }
  4203. if (InvalidCast)
  4204. continue;
  4205. // Create a call instruction that calls the new function.
  4206. CallInst *NewCall = Builder.CreateCall(NewFuncTy, NewFn, Args);
  4207. NewCall->setTailCallKind(cast<CallInst>(CI)->getTailCallKind());
  4208. NewCall->takeName(CI);
  4209. // Bitcast the return value back to the type of the old call.
  4210. Value *NewRetVal = Builder.CreateBitCast(NewCall, CI->getType());
  4211. if (!CI->use_empty())
  4212. CI->replaceAllUsesWith(NewRetVal);
  4213. CI->eraseFromParent();
  4214. }
  4215. if (Fn->use_empty())
  4216. Fn->eraseFromParent();
  4217. };
  4218. // Unconditionally convert a call to "clang.arc.use" to a call to
  4219. // "llvm.objc.clang.arc.use".
  4220. UpgradeToIntrinsic("clang.arc.use", llvm::Intrinsic::objc_clang_arc_use);
  4221. // Upgrade the retain release marker. If there is no need to upgrade
  4222. // the marker, that means either the module is already new enough to contain
  4223. // new intrinsics or it is not ARC. There is no need to upgrade runtime call.
  4224. if (!UpgradeRetainReleaseMarker(M))
  4225. return;
  4226. std::pair<const char *, llvm::Intrinsic::ID> RuntimeFuncs[] = {
  4227. {"objc_autorelease", llvm::Intrinsic::objc_autorelease},
  4228. {"objc_autoreleasePoolPop", llvm::Intrinsic::objc_autoreleasePoolPop},
  4229. {"objc_autoreleasePoolPush", llvm::Intrinsic::objc_autoreleasePoolPush},
  4230. {"objc_autoreleaseReturnValue",
  4231. llvm::Intrinsic::objc_autoreleaseReturnValue},
  4232. {"objc_copyWeak", llvm::Intrinsic::objc_copyWeak},
  4233. {"objc_destroyWeak", llvm::Intrinsic::objc_destroyWeak},
  4234. {"objc_initWeak", llvm::Intrinsic::objc_initWeak},
  4235. {"objc_loadWeak", llvm::Intrinsic::objc_loadWeak},
  4236. {"objc_loadWeakRetained", llvm::Intrinsic::objc_loadWeakRetained},
  4237. {"objc_moveWeak", llvm::Intrinsic::objc_moveWeak},
  4238. {"objc_release", llvm::Intrinsic::objc_release},
  4239. {"objc_retain", llvm::Intrinsic::objc_retain},
  4240. {"objc_retainAutorelease", llvm::Intrinsic::objc_retainAutorelease},
  4241. {"objc_retainAutoreleaseReturnValue",
  4242. llvm::Intrinsic::objc_retainAutoreleaseReturnValue},
  4243. {"objc_retainAutoreleasedReturnValue",
  4244. llvm::Intrinsic::objc_retainAutoreleasedReturnValue},
  4245. {"objc_retainBlock", llvm::Intrinsic::objc_retainBlock},
  4246. {"objc_storeStrong", llvm::Intrinsic::objc_storeStrong},
  4247. {"objc_storeWeak", llvm::Intrinsic::objc_storeWeak},
  4248. {"objc_unsafeClaimAutoreleasedReturnValue",
  4249. llvm::Intrinsic::objc_unsafeClaimAutoreleasedReturnValue},
  4250. {"objc_retainedObject", llvm::Intrinsic::objc_retainedObject},
  4251. {"objc_unretainedObject", llvm::Intrinsic::objc_unretainedObject},
  4252. {"objc_unretainedPointer", llvm::Intrinsic::objc_unretainedPointer},
  4253. {"objc_retain_autorelease", llvm::Intrinsic::objc_retain_autorelease},
  4254. {"objc_sync_enter", llvm::Intrinsic::objc_sync_enter},
  4255. {"objc_sync_exit", llvm::Intrinsic::objc_sync_exit},
  4256. {"objc_arc_annotation_topdown_bbstart",
  4257. llvm::Intrinsic::objc_arc_annotation_topdown_bbstart},
  4258. {"objc_arc_annotation_topdown_bbend",
  4259. llvm::Intrinsic::objc_arc_annotation_topdown_bbend},
  4260. {"objc_arc_annotation_bottomup_bbstart",
  4261. llvm::Intrinsic::objc_arc_annotation_bottomup_bbstart},
  4262. {"objc_arc_annotation_bottomup_bbend",
  4263. llvm::Intrinsic::objc_arc_annotation_bottomup_bbend}};
  4264. for (auto &I : RuntimeFuncs)
  4265. UpgradeToIntrinsic(I.first, I.second);
  4266. }
  4267. bool llvm::UpgradeModuleFlags(Module &M) {
  4268. NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
  4269. if (!ModFlags)
  4270. return false;
  4271. bool HasObjCFlag = false, HasClassProperties = false, Changed = false;
  4272. bool HasSwiftVersionFlag = false;
  4273. uint8_t SwiftMajorVersion, SwiftMinorVersion;
  4274. uint32_t SwiftABIVersion;
  4275. auto Int8Ty = Type::getInt8Ty(M.getContext());
  4276. auto Int32Ty = Type::getInt32Ty(M.getContext());
  4277. for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
  4278. MDNode *Op = ModFlags->getOperand(I);
  4279. if (Op->getNumOperands() != 3)
  4280. continue;
  4281. MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
  4282. if (!ID)
  4283. continue;
  4284. auto SetBehavior = [&](Module::ModFlagBehavior B) {
  4285. Metadata *Ops[3] = {ConstantAsMetadata::get(ConstantInt::get(
  4286. Type::getInt32Ty(M.getContext()), B)),
  4287. MDString::get(M.getContext(), ID->getString()),
  4288. Op->getOperand(2)};
  4289. ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
  4290. Changed = true;
  4291. };
  4292. if (ID->getString() == "Objective-C Image Info Version")
  4293. HasObjCFlag = true;
  4294. if (ID->getString() == "Objective-C Class Properties")
  4295. HasClassProperties = true;
  4296. // Upgrade PIC from Error/Max to Min.
  4297. if (ID->getString() == "PIC Level") {
  4298. if (auto *Behavior =
  4299. mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0))) {
  4300. uint64_t V = Behavior->getLimitedValue();
  4301. if (V == Module::Error || V == Module::Max)
  4302. SetBehavior(Module::Min);
  4303. }
  4304. }
  4305. // Upgrade "PIE Level" from Error to Max.
  4306. if (ID->getString() == "PIE Level")
  4307. if (auto *Behavior =
  4308. mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)))
  4309. if (Behavior->getLimitedValue() == Module::Error)
  4310. SetBehavior(Module::Max);
  4311. // Upgrade branch protection and return address signing module flags. The
  4312. // module flag behavior for these fields were Error and now they are Min.
  4313. if (ID->getString() == "branch-target-enforcement" ||
  4314. ID->getString().startswith("sign-return-address")) {
  4315. if (auto *Behavior =
  4316. mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0))) {
  4317. if (Behavior->getLimitedValue() == Module::Error) {
  4318. Type *Int32Ty = Type::getInt32Ty(M.getContext());
  4319. Metadata *Ops[3] = {
  4320. ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Module::Min)),
  4321. Op->getOperand(1), Op->getOperand(2)};
  4322. ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
  4323. Changed = true;
  4324. }
  4325. }
  4326. }
  4327. // Upgrade Objective-C Image Info Section. Removed the whitespce in the
  4328. // section name so that llvm-lto will not complain about mismatching
  4329. // module flags that is functionally the same.
  4330. if (ID->getString() == "Objective-C Image Info Section") {
  4331. if (auto *Value = dyn_cast_or_null<MDString>(Op->getOperand(2))) {
  4332. SmallVector<StringRef, 4> ValueComp;
  4333. Value->getString().split(ValueComp, " ");
  4334. if (ValueComp.size() != 1) {
  4335. std::string NewValue;
  4336. for (auto &S : ValueComp)
  4337. NewValue += S.str();
  4338. Metadata *Ops[3] = {Op->getOperand(0), Op->getOperand(1),
  4339. MDString::get(M.getContext(), NewValue)};
  4340. ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
  4341. Changed = true;
  4342. }
  4343. }
  4344. }
  4345. // IRUpgrader turns a i32 type "Objective-C Garbage Collection" into i8 value.
  4346. // If the higher bits are set, it adds new module flag for swift info.
  4347. if (ID->getString() == "Objective-C Garbage Collection") {
  4348. auto Md = dyn_cast<ConstantAsMetadata>(Op->getOperand(2));
  4349. if (Md) {
  4350. assert(Md->getValue() && "Expected non-empty metadata");
  4351. auto Type = Md->getValue()->getType();
  4352. if (Type == Int8Ty)
  4353. continue;
  4354. unsigned Val = Md->getValue()->getUniqueInteger().getZExtValue();
  4355. if ((Val & 0xff) != Val) {
  4356. HasSwiftVersionFlag = true;
  4357. SwiftABIVersion = (Val & 0xff00) >> 8;
  4358. SwiftMajorVersion = (Val & 0xff000000) >> 24;
  4359. SwiftMinorVersion = (Val & 0xff0000) >> 16;
  4360. }
  4361. Metadata *Ops[3] = {
  4362. ConstantAsMetadata::get(ConstantInt::get(Int32Ty,Module::Error)),
  4363. Op->getOperand(1),
  4364. ConstantAsMetadata::get(ConstantInt::get(Int8Ty,Val & 0xff))};
  4365. ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
  4366. Changed = true;
  4367. }
  4368. }
  4369. }
  4370. // "Objective-C Class Properties" is recently added for Objective-C. We
  4371. // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
  4372. // flag of value 0, so we can correclty downgrade this flag when trying to
  4373. // link an ObjC bitcode without this module flag with an ObjC bitcode with
  4374. // this module flag.
  4375. if (HasObjCFlag && !HasClassProperties) {
  4376. M.addModuleFlag(llvm::Module::Override, "Objective-C Class Properties",
  4377. (uint32_t)0);
  4378. Changed = true;
  4379. }
  4380. if (HasSwiftVersionFlag) {
  4381. M.addModuleFlag(Module::Error, "Swift ABI Version",
  4382. SwiftABIVersion);
  4383. M.addModuleFlag(Module::Error, "Swift Major Version",
  4384. ConstantInt::get(Int8Ty, SwiftMajorVersion));
  4385. M.addModuleFlag(Module::Error, "Swift Minor Version",
  4386. ConstantInt::get(Int8Ty, SwiftMinorVersion));
  4387. Changed = true;
  4388. }
  4389. return Changed;
  4390. }
  4391. void llvm::UpgradeSectionAttributes(Module &M) {
  4392. auto TrimSpaces = [](StringRef Section) -> std::string {
  4393. SmallVector<StringRef, 5> Components;
  4394. Section.split(Components, ',');
  4395. SmallString<32> Buffer;
  4396. raw_svector_ostream OS(Buffer);
  4397. for (auto Component : Components)
  4398. OS << ',' << Component.trim();
  4399. return std::string(OS.str().substr(1));
  4400. };
  4401. for (auto &GV : M.globals()) {
  4402. if (!GV.hasSection())
  4403. continue;
  4404. StringRef Section = GV.getSection();
  4405. if (!Section.startswith("__DATA, __objc_catlist"))
  4406. continue;
  4407. // __DATA, __objc_catlist, regular, no_dead_strip
  4408. // __DATA,__objc_catlist,regular,no_dead_strip
  4409. GV.setSection(TrimSpaces(Section));
  4410. }
  4411. }
  4412. namespace {
  4413. // Prior to LLVM 10.0, the strictfp attribute could be used on individual
  4414. // callsites within a function that did not also have the strictfp attribute.
  4415. // Since 10.0, if strict FP semantics are needed within a function, the
  4416. // function must have the strictfp attribute and all calls within the function
  4417. // must also have the strictfp attribute. This latter restriction is
  4418. // necessary to prevent unwanted libcall simplification when a function is
  4419. // being cloned (such as for inlining).
  4420. //
  4421. // The "dangling" strictfp attribute usage was only used to prevent constant
  4422. // folding and other libcall simplification. The nobuiltin attribute on the
  4423. // callsite has the same effect.
  4424. struct StrictFPUpgradeVisitor : public InstVisitor<StrictFPUpgradeVisitor> {
  4425. StrictFPUpgradeVisitor() = default;
  4426. void visitCallBase(CallBase &Call) {
  4427. if (!Call.isStrictFP())
  4428. return;
  4429. if (isa<ConstrainedFPIntrinsic>(&Call))
  4430. return;
  4431. // If we get here, the caller doesn't have the strictfp attribute
  4432. // but this callsite does. Replace the strictfp attribute with nobuiltin.
  4433. Call.removeFnAttr(Attribute::StrictFP);
  4434. Call.addFnAttr(Attribute::NoBuiltin);
  4435. }
  4436. };
  4437. } // namespace
  4438. void llvm::UpgradeFunctionAttributes(Function &F) {
  4439. // If a function definition doesn't have the strictfp attribute,
  4440. // convert any callsite strictfp attributes to nobuiltin.
  4441. if (!F.isDeclaration() && !F.hasFnAttribute(Attribute::StrictFP)) {
  4442. StrictFPUpgradeVisitor SFPV;
  4443. SFPV.visit(F);
  4444. }
  4445. // Remove all incompatibile attributes from function.
  4446. F.removeRetAttrs(AttributeFuncs::typeIncompatible(F.getReturnType()));
  4447. for (auto &Arg : F.args())
  4448. Arg.removeAttrs(AttributeFuncs::typeIncompatible(Arg.getType()));
  4449. }
  4450. static bool isOldLoopArgument(Metadata *MD) {
  4451. auto *T = dyn_cast_or_null<MDTuple>(MD);
  4452. if (!T)
  4453. return false;
  4454. if (T->getNumOperands() < 1)
  4455. return false;
  4456. auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
  4457. if (!S)
  4458. return false;
  4459. return S->getString().startswith("llvm.vectorizer.");
  4460. }
  4461. static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
  4462. StringRef OldPrefix = "llvm.vectorizer.";
  4463. assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
  4464. if (OldTag == "llvm.vectorizer.unroll")
  4465. return MDString::get(C, "llvm.loop.interleave.count");
  4466. return MDString::get(
  4467. C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
  4468. .str());
  4469. }
  4470. static Metadata *upgradeLoopArgument(Metadata *MD) {
  4471. auto *T = dyn_cast_or_null<MDTuple>(MD);
  4472. if (!T)
  4473. return MD;
  4474. if (T->getNumOperands() < 1)
  4475. return MD;
  4476. auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
  4477. if (!OldTag)
  4478. return MD;
  4479. if (!OldTag->getString().startswith("llvm.vectorizer."))
  4480. return MD;
  4481. // This has an old tag. Upgrade it.
  4482. SmallVector<Metadata *, 8> Ops;
  4483. Ops.reserve(T->getNumOperands());
  4484. Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
  4485. for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
  4486. Ops.push_back(T->getOperand(I));
  4487. return MDTuple::get(T->getContext(), Ops);
  4488. }
  4489. MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
  4490. auto *T = dyn_cast<MDTuple>(&N);
  4491. if (!T)
  4492. return &N;
  4493. if (none_of(T->operands(), isOldLoopArgument))
  4494. return &N;
  4495. SmallVector<Metadata *, 8> Ops;
  4496. Ops.reserve(T->getNumOperands());
  4497. for (Metadata *MD : T->operands())
  4498. Ops.push_back(upgradeLoopArgument(MD));
  4499. return MDTuple::get(T->getContext(), Ops);
  4500. }
  4501. std::string llvm::UpgradeDataLayoutString(StringRef DL, StringRef TT) {
  4502. Triple T(TT);
  4503. // For AMDGPU we uprgrade older DataLayouts to include the default globals
  4504. // address space of 1.
  4505. if (T.isAMDGPU() && !DL.contains("-G") && !DL.startswith("G")) {
  4506. return DL.empty() ? std::string("G1") : (DL + "-G1").str();
  4507. }
  4508. if (T.isRISCV64()) {
  4509. // Make i32 a native type for 64-bit RISC-V.
  4510. auto I = DL.find("-n64-");
  4511. if (I != StringRef::npos)
  4512. return (DL.take_front(I) + "-n32:64-" + DL.drop_front(I + 5)).str();
  4513. return DL.str();
  4514. }
  4515. std::string Res = DL.str();
  4516. if (!T.isX86())
  4517. return Res;
  4518. // If the datalayout matches the expected format, add pointer size address
  4519. // spaces to the datalayout.
  4520. std::string AddrSpaces = "-p270:32:32-p271:32:32-p272:64:64";
  4521. if (!DL.contains(AddrSpaces)) {
  4522. SmallVector<StringRef, 4> Groups;
  4523. Regex R("(e-m:[a-z](-p:32:32)?)(-[if]64:.*$)");
  4524. if (R.match(DL, &Groups))
  4525. Res = (Groups[1] + AddrSpaces + Groups[3]).str();
  4526. }
  4527. // For 32-bit MSVC targets, raise the alignment of f80 values to 16 bytes.
  4528. // Raising the alignment is safe because Clang did not produce f80 values in
  4529. // the MSVC environment before this upgrade was added.
  4530. if (T.isWindowsMSVCEnvironment() && !T.isArch64Bit()) {
  4531. StringRef Ref = Res;
  4532. auto I = Ref.find("-f80:32-");
  4533. if (I != StringRef::npos)
  4534. Res = (Ref.take_front(I) + "-f80:128-" + Ref.drop_front(I + 8)).str();
  4535. }
  4536. return Res;
  4537. }
  4538. void llvm::UpgradeAttributes(AttrBuilder &B) {
  4539. StringRef FramePointer;
  4540. Attribute A = B.getAttribute("no-frame-pointer-elim");
  4541. if (A.isValid()) {
  4542. // The value can be "true" or "false".
  4543. FramePointer = A.getValueAsString() == "true" ? "all" : "none";
  4544. B.removeAttribute("no-frame-pointer-elim");
  4545. }
  4546. if (B.contains("no-frame-pointer-elim-non-leaf")) {
  4547. // The value is ignored. "no-frame-pointer-elim"="true" takes priority.
  4548. if (FramePointer != "all")
  4549. FramePointer = "non-leaf";
  4550. B.removeAttribute("no-frame-pointer-elim-non-leaf");
  4551. }
  4552. if (!FramePointer.empty())
  4553. B.addAttribute("frame-pointer", FramePointer);
  4554. A = B.getAttribute("null-pointer-is-valid");
  4555. if (A.isValid()) {
  4556. // The value can be "true" or "false".
  4557. bool NullPointerIsValid = A.getValueAsString() == "true";
  4558. B.removeAttribute("null-pointer-is-valid");
  4559. if (NullPointerIsValid)
  4560. B.addAttribute(Attribute::NullPointerIsValid);
  4561. }
  4562. }
  4563. void llvm::UpgradeOperandBundles(std::vector<OperandBundleDef> &Bundles) {
  4564. // clang.arc.attachedcall bundles are now required to have an operand.
  4565. // If they don't, it's okay to drop them entirely: when there is an operand,
  4566. // the "attachedcall" is meaningful and required, but without an operand,
  4567. // it's just a marker NOP. Dropping it merely prevents an optimization.
  4568. erase_if(Bundles, [&](OperandBundleDef &OBD) {
  4569. return OBD.getTag() == "clang.arc.attachedcall" &&
  4570. OBD.inputs().empty();
  4571. });
  4572. }