AutoUpgrade.cpp 181 KB

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