SelectionDAGISel.cpp 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823
  1. //===- SelectionDAGISel.cpp - Implement the SelectionDAGISel class --------===//
  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 implements the SelectionDAGISel class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/SelectionDAGISel.h"
  13. #include "ScheduleDAGSDNodes.h"
  14. #include "SelectionDAGBuilder.h"
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/PostOrderIterator.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/Analysis/AliasAnalysis.h"
  24. #include "llvm/Analysis/AssumptionCache.h"
  25. #include "llvm/Analysis/BranchProbabilityInfo.h"
  26. #include "llvm/Analysis/CFG.h"
  27. #include "llvm/Analysis/EHPersonalities.h"
  28. #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
  29. #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
  30. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  31. #include "llvm/Analysis/ProfileSummaryInfo.h"
  32. #include "llvm/Analysis/TargetLibraryInfo.h"
  33. #include "llvm/Analysis/TargetTransformInfo.h"
  34. #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
  35. #include "llvm/CodeGen/CodeGenCommonISel.h"
  36. #include "llvm/CodeGen/FastISel.h"
  37. #include "llvm/CodeGen/FunctionLoweringInfo.h"
  38. #include "llvm/CodeGen/GCMetadata.h"
  39. #include "llvm/CodeGen/ISDOpcodes.h"
  40. #include "llvm/CodeGen/MachineBasicBlock.h"
  41. #include "llvm/CodeGen/MachineFrameInfo.h"
  42. #include "llvm/CodeGen/MachineFunction.h"
  43. #include "llvm/CodeGen/MachineFunctionPass.h"
  44. #include "llvm/CodeGen/MachineInstr.h"
  45. #include "llvm/CodeGen/MachineInstrBuilder.h"
  46. #include "llvm/CodeGen/MachineMemOperand.h"
  47. #include "llvm/CodeGen/MachineModuleInfo.h"
  48. #include "llvm/CodeGen/MachineOperand.h"
  49. #include "llvm/CodeGen/MachinePassRegistry.h"
  50. #include "llvm/CodeGen/MachineRegisterInfo.h"
  51. #include "llvm/CodeGen/SchedulerRegistry.h"
  52. #include "llvm/CodeGen/SelectionDAG.h"
  53. #include "llvm/CodeGen/SelectionDAGNodes.h"
  54. #include "llvm/CodeGen/StackMaps.h"
  55. #include "llvm/CodeGen/StackProtector.h"
  56. #include "llvm/CodeGen/SwiftErrorValueTracking.h"
  57. #include "llvm/CodeGen/TargetInstrInfo.h"
  58. #include "llvm/CodeGen/TargetLowering.h"
  59. #include "llvm/CodeGen/TargetRegisterInfo.h"
  60. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  61. #include "llvm/CodeGen/ValueTypes.h"
  62. #include "llvm/IR/BasicBlock.h"
  63. #include "llvm/IR/Constants.h"
  64. #include "llvm/IR/DataLayout.h"
  65. #include "llvm/IR/DebugInfo.h"
  66. #include "llvm/IR/DebugInfoMetadata.h"
  67. #include "llvm/IR/DebugLoc.h"
  68. #include "llvm/IR/DiagnosticInfo.h"
  69. #include "llvm/IR/Function.h"
  70. #include "llvm/IR/InlineAsm.h"
  71. #include "llvm/IR/InstIterator.h"
  72. #include "llvm/IR/Instruction.h"
  73. #include "llvm/IR/Instructions.h"
  74. #include "llvm/IR/IntrinsicInst.h"
  75. #include "llvm/IR/Intrinsics.h"
  76. #include "llvm/IR/IntrinsicsWebAssembly.h"
  77. #include "llvm/IR/Metadata.h"
  78. #include "llvm/IR/Statepoint.h"
  79. #include "llvm/IR/Type.h"
  80. #include "llvm/IR/User.h"
  81. #include "llvm/IR/Value.h"
  82. #include "llvm/InitializePasses.h"
  83. #include "llvm/MC/MCInstrDesc.h"
  84. #include "llvm/Pass.h"
  85. #include "llvm/Support/BranchProbability.h"
  86. #include "llvm/Support/Casting.h"
  87. #include "llvm/Support/CodeGen.h"
  88. #include "llvm/Support/CommandLine.h"
  89. #include "llvm/Support/Compiler.h"
  90. #include "llvm/Support/Debug.h"
  91. #include "llvm/Support/ErrorHandling.h"
  92. #include "llvm/Support/KnownBits.h"
  93. #include "llvm/Support/MachineValueType.h"
  94. #include "llvm/Support/Timer.h"
  95. #include "llvm/Support/raw_ostream.h"
  96. #include "llvm/Target/TargetIntrinsicInfo.h"
  97. #include "llvm/Target/TargetMachine.h"
  98. #include "llvm/Target/TargetOptions.h"
  99. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  100. #include <algorithm>
  101. #include <cassert>
  102. #include <cstdint>
  103. #include <iterator>
  104. #include <limits>
  105. #include <memory>
  106. #include <optional>
  107. #include <string>
  108. #include <utility>
  109. #include <vector>
  110. using namespace llvm;
  111. #define DEBUG_TYPE "isel"
  112. STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on");
  113. STATISTIC(NumFastIselSuccess, "Number of instructions fast isel selected");
  114. STATISTIC(NumFastIselBlocks, "Number of blocks selected entirely by fast isel");
  115. STATISTIC(NumDAGBlocks, "Number of blocks selected using DAG");
  116. STATISTIC(NumDAGIselRetries,"Number of times dag isel has to try another path");
  117. STATISTIC(NumEntryBlocks, "Number of entry blocks encountered");
  118. STATISTIC(NumFastIselFailLowerArguments,
  119. "Number of entry blocks where fast isel failed to lower arguments");
  120. static cl::opt<int> EnableFastISelAbort(
  121. "fast-isel-abort", cl::Hidden,
  122. cl::desc("Enable abort calls when \"fast\" instruction selection "
  123. "fails to lower an instruction: 0 disable the abort, 1 will "
  124. "abort but for args, calls and terminators, 2 will also "
  125. "abort for argument lowering, and 3 will never fallback "
  126. "to SelectionDAG."));
  127. static cl::opt<bool> EnableFastISelFallbackReport(
  128. "fast-isel-report-on-fallback", cl::Hidden,
  129. cl::desc("Emit a diagnostic when \"fast\" instruction selection "
  130. "falls back to SelectionDAG."));
  131. static cl::opt<bool>
  132. UseMBPI("use-mbpi",
  133. cl::desc("use Machine Branch Probability Info"),
  134. cl::init(true), cl::Hidden);
  135. #ifndef NDEBUG
  136. static cl::opt<std::string>
  137. FilterDAGBasicBlockName("filter-view-dags", cl::Hidden,
  138. cl::desc("Only display the basic block whose name "
  139. "matches this for all view-*-dags options"));
  140. static cl::opt<bool>
  141. ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
  142. cl::desc("Pop up a window to show dags before the first "
  143. "dag combine pass"));
  144. static cl::opt<bool>
  145. ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
  146. cl::desc("Pop up a window to show dags before legalize types"));
  147. static cl::opt<bool>
  148. ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
  149. cl::desc("Pop up a window to show dags before the post "
  150. "legalize types dag combine pass"));
  151. static cl::opt<bool>
  152. ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
  153. cl::desc("Pop up a window to show dags before legalize"));
  154. static cl::opt<bool>
  155. ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
  156. cl::desc("Pop up a window to show dags before the second "
  157. "dag combine pass"));
  158. static cl::opt<bool>
  159. ViewISelDAGs("view-isel-dags", cl::Hidden,
  160. cl::desc("Pop up a window to show isel dags as they are selected"));
  161. static cl::opt<bool>
  162. ViewSchedDAGs("view-sched-dags", cl::Hidden,
  163. cl::desc("Pop up a window to show sched dags as they are processed"));
  164. static cl::opt<bool>
  165. ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
  166. cl::desc("Pop up a window to show SUnit dags after they are processed"));
  167. #else
  168. static const bool ViewDAGCombine1 = false, ViewLegalizeTypesDAGs = false,
  169. ViewDAGCombineLT = false, ViewLegalizeDAGs = false,
  170. ViewDAGCombine2 = false, ViewISelDAGs = false,
  171. ViewSchedDAGs = false, ViewSUnitDAGs = false;
  172. #endif
  173. //===---------------------------------------------------------------------===//
  174. ///
  175. /// RegisterScheduler class - Track the registration of instruction schedulers.
  176. ///
  177. //===---------------------------------------------------------------------===//
  178. MachinePassRegistry<RegisterScheduler::FunctionPassCtor>
  179. RegisterScheduler::Registry;
  180. //===---------------------------------------------------------------------===//
  181. ///
  182. /// ISHeuristic command line option for instruction schedulers.
  183. ///
  184. //===---------------------------------------------------------------------===//
  185. static cl::opt<RegisterScheduler::FunctionPassCtor, false,
  186. RegisterPassParser<RegisterScheduler>>
  187. ISHeuristic("pre-RA-sched",
  188. cl::init(&createDefaultScheduler), cl::Hidden,
  189. cl::desc("Instruction schedulers available (before register"
  190. " allocation):"));
  191. static RegisterScheduler
  192. defaultListDAGScheduler("default", "Best scheduler for the target",
  193. createDefaultScheduler);
  194. namespace llvm {
  195. //===--------------------------------------------------------------------===//
  196. /// This class is used by SelectionDAGISel to temporarily override
  197. /// the optimization level on a per-function basis.
  198. class OptLevelChanger {
  199. SelectionDAGISel &IS;
  200. CodeGenOpt::Level SavedOptLevel;
  201. bool SavedFastISel;
  202. public:
  203. OptLevelChanger(SelectionDAGISel &ISel,
  204. CodeGenOpt::Level NewOptLevel) : IS(ISel) {
  205. SavedOptLevel = IS.OptLevel;
  206. SavedFastISel = IS.TM.Options.EnableFastISel;
  207. if (NewOptLevel == SavedOptLevel)
  208. return;
  209. IS.OptLevel = NewOptLevel;
  210. IS.TM.setOptLevel(NewOptLevel);
  211. LLVM_DEBUG(dbgs() << "\nChanging optimization level for Function "
  212. << IS.MF->getFunction().getName() << "\n");
  213. LLVM_DEBUG(dbgs() << "\tBefore: -O" << SavedOptLevel << " ; After: -O"
  214. << NewOptLevel << "\n");
  215. if (NewOptLevel == CodeGenOpt::None) {
  216. IS.TM.setFastISel(IS.TM.getO0WantsFastISel());
  217. LLVM_DEBUG(
  218. dbgs() << "\tFastISel is "
  219. << (IS.TM.Options.EnableFastISel ? "enabled" : "disabled")
  220. << "\n");
  221. }
  222. }
  223. ~OptLevelChanger() {
  224. if (IS.OptLevel == SavedOptLevel)
  225. return;
  226. LLVM_DEBUG(dbgs() << "\nRestoring optimization level for Function "
  227. << IS.MF->getFunction().getName() << "\n");
  228. LLVM_DEBUG(dbgs() << "\tBefore: -O" << IS.OptLevel << " ; After: -O"
  229. << SavedOptLevel << "\n");
  230. IS.OptLevel = SavedOptLevel;
  231. IS.TM.setOptLevel(SavedOptLevel);
  232. IS.TM.setFastISel(SavedFastISel);
  233. }
  234. };
  235. //===--------------------------------------------------------------------===//
  236. /// createDefaultScheduler - This creates an instruction scheduler appropriate
  237. /// for the target.
  238. ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
  239. CodeGenOpt::Level OptLevel) {
  240. const TargetLowering *TLI = IS->TLI;
  241. const TargetSubtargetInfo &ST = IS->MF->getSubtarget();
  242. // Try first to see if the Target has its own way of selecting a scheduler
  243. if (auto *SchedulerCtor = ST.getDAGScheduler(OptLevel)) {
  244. return SchedulerCtor(IS, OptLevel);
  245. }
  246. if (OptLevel == CodeGenOpt::None ||
  247. (ST.enableMachineScheduler() && ST.enableMachineSchedDefaultSched()) ||
  248. TLI->getSchedulingPreference() == Sched::Source)
  249. return createSourceListDAGScheduler(IS, OptLevel);
  250. if (TLI->getSchedulingPreference() == Sched::RegPressure)
  251. return createBURRListDAGScheduler(IS, OptLevel);
  252. if (TLI->getSchedulingPreference() == Sched::Hybrid)
  253. return createHybridListDAGScheduler(IS, OptLevel);
  254. if (TLI->getSchedulingPreference() == Sched::VLIW)
  255. return createVLIWDAGScheduler(IS, OptLevel);
  256. if (TLI->getSchedulingPreference() == Sched::Fast)
  257. return createFastDAGScheduler(IS, OptLevel);
  258. if (TLI->getSchedulingPreference() == Sched::Linearize)
  259. return createDAGLinearizer(IS, OptLevel);
  260. assert(TLI->getSchedulingPreference() == Sched::ILP &&
  261. "Unknown sched type!");
  262. return createILPListDAGScheduler(IS, OptLevel);
  263. }
  264. } // end namespace llvm
  265. // EmitInstrWithCustomInserter - This method should be implemented by targets
  266. // that mark instructions with the 'usesCustomInserter' flag. These
  267. // instructions are special in various ways, which require special support to
  268. // insert. The specified MachineInstr is created but not inserted into any
  269. // basic blocks, and this method is called to expand it into a sequence of
  270. // instructions, potentially also creating new basic blocks and control flow.
  271. // When new basic blocks are inserted and the edges from MBB to its successors
  272. // are modified, the method should insert pairs of <OldSucc, NewSucc> into the
  273. // DenseMap.
  274. MachineBasicBlock *
  275. TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
  276. MachineBasicBlock *MBB) const {
  277. #ifndef NDEBUG
  278. dbgs() << "If a target marks an instruction with "
  279. "'usesCustomInserter', it must implement "
  280. "TargetLowering::EmitInstrWithCustomInserter!\n";
  281. #endif
  282. llvm_unreachable(nullptr);
  283. }
  284. void TargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
  285. SDNode *Node) const {
  286. assert(!MI.hasPostISelHook() &&
  287. "If a target marks an instruction with 'hasPostISelHook', "
  288. "it must implement TargetLowering::AdjustInstrPostInstrSelection!");
  289. }
  290. //===----------------------------------------------------------------------===//
  291. // SelectionDAGISel code
  292. //===----------------------------------------------------------------------===//
  293. SelectionDAGISel::SelectionDAGISel(char &ID, TargetMachine &tm,
  294. CodeGenOpt::Level OL)
  295. : MachineFunctionPass(ID), TM(tm), FuncInfo(new FunctionLoweringInfo()),
  296. SwiftError(new SwiftErrorValueTracking()),
  297. CurDAG(new SelectionDAG(tm, OL)),
  298. SDB(std::make_unique<SelectionDAGBuilder>(*CurDAG, *FuncInfo, *SwiftError,
  299. OL)),
  300. OptLevel(OL) {
  301. initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
  302. initializeBranchProbabilityInfoWrapperPassPass(
  303. *PassRegistry::getPassRegistry());
  304. initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
  305. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  306. }
  307. SelectionDAGISel::~SelectionDAGISel() {
  308. delete CurDAG;
  309. delete SwiftError;
  310. }
  311. void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
  312. if (OptLevel != CodeGenOpt::None)
  313. AU.addRequired<AAResultsWrapperPass>();
  314. AU.addRequired<GCModuleInfo>();
  315. AU.addRequired<StackProtector>();
  316. AU.addPreserved<GCModuleInfo>();
  317. AU.addRequired<TargetLibraryInfoWrapperPass>();
  318. AU.addRequired<TargetTransformInfoWrapperPass>();
  319. AU.addRequired<AssumptionCacheTracker>();
  320. if (UseMBPI && OptLevel != CodeGenOpt::None)
  321. AU.addRequired<BranchProbabilityInfoWrapperPass>();
  322. AU.addRequired<ProfileSummaryInfoWrapperPass>();
  323. // AssignmentTrackingAnalysis only runs if assignment tracking is enabled for
  324. // the module.
  325. AU.addRequired<AssignmentTrackingAnalysis>();
  326. AU.addPreserved<AssignmentTrackingAnalysis>();
  327. if (OptLevel != CodeGenOpt::None)
  328. LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
  329. MachineFunctionPass::getAnalysisUsage(AU);
  330. }
  331. static void computeUsesMSVCFloatingPoint(const Triple &TT, const Function &F,
  332. MachineModuleInfo &MMI) {
  333. // Only needed for MSVC
  334. if (!TT.isWindowsMSVCEnvironment())
  335. return;
  336. // If it's already set, nothing to do.
  337. if (MMI.usesMSVCFloatingPoint())
  338. return;
  339. for (const Instruction &I : instructions(F)) {
  340. if (I.getType()->isFPOrFPVectorTy()) {
  341. MMI.setUsesMSVCFloatingPoint(true);
  342. return;
  343. }
  344. for (const auto &Op : I.operands()) {
  345. if (Op->getType()->isFPOrFPVectorTy()) {
  346. MMI.setUsesMSVCFloatingPoint(true);
  347. return;
  348. }
  349. }
  350. }
  351. }
  352. bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
  353. // If we already selected that function, we do not need to run SDISel.
  354. if (mf.getProperties().hasProperty(
  355. MachineFunctionProperties::Property::Selected))
  356. return false;
  357. // Do some sanity-checking on the command-line options.
  358. assert((!EnableFastISelAbort || TM.Options.EnableFastISel) &&
  359. "-fast-isel-abort > 0 requires -fast-isel");
  360. const Function &Fn = mf.getFunction();
  361. MF = &mf;
  362. // Decide what flavour of variable location debug-info will be used, before
  363. // we change the optimisation level.
  364. bool InstrRef = mf.shouldUseDebugInstrRef();
  365. mf.setUseDebugInstrRef(InstrRef);
  366. // Reset the target options before resetting the optimization
  367. // level below.
  368. // FIXME: This is a horrible hack and should be processed via
  369. // codegen looking at the optimization level explicitly when
  370. // it wants to look at it.
  371. TM.resetTargetOptions(Fn);
  372. // Reset OptLevel to None for optnone functions.
  373. CodeGenOpt::Level NewOptLevel = OptLevel;
  374. if (OptLevel != CodeGenOpt::None && skipFunction(Fn))
  375. NewOptLevel = CodeGenOpt::None;
  376. OptLevelChanger OLC(*this, NewOptLevel);
  377. TII = MF->getSubtarget().getInstrInfo();
  378. TLI = MF->getSubtarget().getTargetLowering();
  379. RegInfo = &MF->getRegInfo();
  380. LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(Fn);
  381. GFI = Fn.hasGC() ? &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn) : nullptr;
  382. ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn);
  383. AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(mf.getFunction());
  384. auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
  385. BlockFrequencyInfo *BFI = nullptr;
  386. if (PSI && PSI->hasProfileSummary() && OptLevel != CodeGenOpt::None)
  387. BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
  388. FunctionVarLocs const *FnVarLocs = nullptr;
  389. if (isAssignmentTrackingEnabled(*Fn.getParent()))
  390. FnVarLocs = getAnalysis<AssignmentTrackingAnalysis>().getResults();
  391. LLVM_DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n");
  392. CurDAG->init(*MF, *ORE, this, LibInfo,
  393. getAnalysisIfAvailable<LegacyDivergenceAnalysis>(), PSI, BFI,
  394. FnVarLocs);
  395. FuncInfo->set(Fn, *MF, CurDAG);
  396. SwiftError->setFunction(*MF);
  397. // Now get the optional analyzes if we want to.
  398. // This is based on the possibly changed OptLevel (after optnone is taken
  399. // into account). That's unfortunate but OK because it just means we won't
  400. // ask for passes that have been required anyway.
  401. if (UseMBPI && OptLevel != CodeGenOpt::None)
  402. FuncInfo->BPI = &getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
  403. else
  404. FuncInfo->BPI = nullptr;
  405. if (OptLevel != CodeGenOpt::None)
  406. AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
  407. else
  408. AA = nullptr;
  409. SDB->init(GFI, AA, AC, LibInfo);
  410. MF->setHasInlineAsm(false);
  411. FuncInfo->SplitCSR = false;
  412. // We split CSR if the target supports it for the given function
  413. // and the function has only return exits.
  414. if (OptLevel != CodeGenOpt::None && TLI->supportSplitCSR(MF)) {
  415. FuncInfo->SplitCSR = true;
  416. // Collect all the return blocks.
  417. for (const BasicBlock &BB : Fn) {
  418. if (!succ_empty(&BB))
  419. continue;
  420. const Instruction *Term = BB.getTerminator();
  421. if (isa<UnreachableInst>(Term) || isa<ReturnInst>(Term))
  422. continue;
  423. // Bail out if the exit block is not Return nor Unreachable.
  424. FuncInfo->SplitCSR = false;
  425. break;
  426. }
  427. }
  428. MachineBasicBlock *EntryMBB = &MF->front();
  429. if (FuncInfo->SplitCSR)
  430. // This performs initialization so lowering for SplitCSR will be correct.
  431. TLI->initializeSplitCSR(EntryMBB);
  432. SelectAllBasicBlocks(Fn);
  433. if (FastISelFailed && EnableFastISelFallbackReport) {
  434. DiagnosticInfoISelFallback DiagFallback(Fn);
  435. Fn.getContext().diagnose(DiagFallback);
  436. }
  437. // Replace forward-declared registers with the registers containing
  438. // the desired value.
  439. // Note: it is important that this happens **before** the call to
  440. // EmitLiveInCopies, since implementations can skip copies of unused
  441. // registers. If we don't apply the reg fixups before, some registers may
  442. // appear as unused and will be skipped, resulting in bad MI.
  443. MachineRegisterInfo &MRI = MF->getRegInfo();
  444. for (DenseMap<Register, Register>::iterator I = FuncInfo->RegFixups.begin(),
  445. E = FuncInfo->RegFixups.end();
  446. I != E; ++I) {
  447. Register From = I->first;
  448. Register To = I->second;
  449. // If To is also scheduled to be replaced, find what its ultimate
  450. // replacement is.
  451. while (true) {
  452. DenseMap<Register, Register>::iterator J = FuncInfo->RegFixups.find(To);
  453. if (J == E)
  454. break;
  455. To = J->second;
  456. }
  457. // Make sure the new register has a sufficiently constrained register class.
  458. if (From.isVirtual() && To.isVirtual())
  459. MRI.constrainRegClass(To, MRI.getRegClass(From));
  460. // Replace it.
  461. // Replacing one register with another won't touch the kill flags.
  462. // We need to conservatively clear the kill flags as a kill on the old
  463. // register might dominate existing uses of the new register.
  464. if (!MRI.use_empty(To))
  465. MRI.clearKillFlags(From);
  466. MRI.replaceRegWith(From, To);
  467. }
  468. // If the first basic block in the function has live ins that need to be
  469. // copied into vregs, emit the copies into the top of the block before
  470. // emitting the code for the block.
  471. const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
  472. RegInfo->EmitLiveInCopies(EntryMBB, TRI, *TII);
  473. // Insert copies in the entry block and the return blocks.
  474. if (FuncInfo->SplitCSR) {
  475. SmallVector<MachineBasicBlock*, 4> Returns;
  476. // Collect all the return blocks.
  477. for (MachineBasicBlock &MBB : mf) {
  478. if (!MBB.succ_empty())
  479. continue;
  480. MachineBasicBlock::iterator Term = MBB.getFirstTerminator();
  481. if (Term != MBB.end() && Term->isReturn()) {
  482. Returns.push_back(&MBB);
  483. continue;
  484. }
  485. }
  486. TLI->insertCopiesSplitCSR(EntryMBB, Returns);
  487. }
  488. DenseMap<unsigned, unsigned> LiveInMap;
  489. if (!FuncInfo->ArgDbgValues.empty())
  490. for (std::pair<unsigned, unsigned> LI : RegInfo->liveins())
  491. if (LI.second)
  492. LiveInMap.insert(LI);
  493. // Insert DBG_VALUE instructions for function arguments to the entry block.
  494. for (unsigned i = 0, e = FuncInfo->ArgDbgValues.size(); i != e; ++i) {
  495. MachineInstr *MI = FuncInfo->ArgDbgValues[e - i - 1];
  496. assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST &&
  497. "Function parameters should not be described by DBG_VALUE_LIST.");
  498. bool hasFI = MI->getDebugOperand(0).isFI();
  499. Register Reg =
  500. hasFI ? TRI.getFrameRegister(*MF) : MI->getDebugOperand(0).getReg();
  501. if (Reg.isPhysical())
  502. EntryMBB->insert(EntryMBB->begin(), MI);
  503. else {
  504. MachineInstr *Def = RegInfo->getVRegDef(Reg);
  505. if (Def) {
  506. MachineBasicBlock::iterator InsertPos = Def;
  507. // FIXME: VR def may not be in entry block.
  508. Def->getParent()->insert(std::next(InsertPos), MI);
  509. } else
  510. LLVM_DEBUG(dbgs() << "Dropping debug info for dead vreg"
  511. << Register::virtReg2Index(Reg) << "\n");
  512. }
  513. // Don't try and extend through copies in instruction referencing mode.
  514. if (InstrRef)
  515. continue;
  516. // If Reg is live-in then update debug info to track its copy in a vreg.
  517. DenseMap<unsigned, unsigned>::iterator LDI = LiveInMap.find(Reg);
  518. if (LDI != LiveInMap.end()) {
  519. assert(!hasFI && "There's no handling of frame pointer updating here yet "
  520. "- add if needed");
  521. MachineInstr *Def = RegInfo->getVRegDef(LDI->second);
  522. MachineBasicBlock::iterator InsertPos = Def;
  523. const MDNode *Variable = MI->getDebugVariable();
  524. const MDNode *Expr = MI->getDebugExpression();
  525. DebugLoc DL = MI->getDebugLoc();
  526. bool IsIndirect = MI->isIndirectDebugValue();
  527. if (IsIndirect)
  528. assert(MI->getDebugOffset().getImm() == 0 &&
  529. "DBG_VALUE with nonzero offset");
  530. assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
  531. "Expected inlined-at fields to agree");
  532. assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST &&
  533. "Didn't expect to see a DBG_VALUE_LIST here");
  534. // Def is never a terminator here, so it is ok to increment InsertPos.
  535. BuildMI(*EntryMBB, ++InsertPos, DL, TII->get(TargetOpcode::DBG_VALUE),
  536. IsIndirect, LDI->second, Variable, Expr);
  537. // If this vreg is directly copied into an exported register then
  538. // that COPY instructions also need DBG_VALUE, if it is the only
  539. // user of LDI->second.
  540. MachineInstr *CopyUseMI = nullptr;
  541. for (MachineRegisterInfo::use_instr_iterator
  542. UI = RegInfo->use_instr_begin(LDI->second),
  543. E = RegInfo->use_instr_end(); UI != E; ) {
  544. MachineInstr *UseMI = &*(UI++);
  545. if (UseMI->isDebugValue()) continue;
  546. if (UseMI->isCopy() && !CopyUseMI && UseMI->getParent() == EntryMBB) {
  547. CopyUseMI = UseMI; continue;
  548. }
  549. // Otherwise this is another use or second copy use.
  550. CopyUseMI = nullptr; break;
  551. }
  552. if (CopyUseMI &&
  553. TRI.getRegSizeInBits(LDI->second, MRI) ==
  554. TRI.getRegSizeInBits(CopyUseMI->getOperand(0).getReg(), MRI)) {
  555. // Use MI's debug location, which describes where Variable was
  556. // declared, rather than whatever is attached to CopyUseMI.
  557. MachineInstr *NewMI =
  558. BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE), IsIndirect,
  559. CopyUseMI->getOperand(0).getReg(), Variable, Expr);
  560. MachineBasicBlock::iterator Pos = CopyUseMI;
  561. EntryMBB->insertAfter(Pos, NewMI);
  562. }
  563. }
  564. }
  565. // For debug-info, in instruction referencing mode, we need to perform some
  566. // post-isel maintenence.
  567. if (MF->useDebugInstrRef())
  568. MF->finalizeDebugInstrRefs();
  569. // Determine if there are any calls in this machine function.
  570. MachineFrameInfo &MFI = MF->getFrameInfo();
  571. for (const auto &MBB : *MF) {
  572. if (MFI.hasCalls() && MF->hasInlineAsm())
  573. break;
  574. for (const auto &MI : MBB) {
  575. const MCInstrDesc &MCID = TII->get(MI.getOpcode());
  576. if ((MCID.isCall() && !MCID.isReturn()) ||
  577. MI.isStackAligningInlineAsm()) {
  578. MFI.setHasCalls(true);
  579. }
  580. if (MI.isInlineAsm()) {
  581. MF->setHasInlineAsm(true);
  582. }
  583. }
  584. }
  585. // Determine if there is a call to setjmp in the machine function.
  586. MF->setExposesReturnsTwice(Fn.callsFunctionThatReturnsTwice());
  587. // Determine if floating point is used for msvc
  588. computeUsesMSVCFloatingPoint(TM.getTargetTriple(), Fn, MF->getMMI());
  589. // Release function-specific state. SDB and CurDAG are already cleared
  590. // at this point.
  591. FuncInfo->clear();
  592. LLVM_DEBUG(dbgs() << "*** MachineFunction at end of ISel ***\n");
  593. LLVM_DEBUG(MF->print(dbgs()));
  594. return true;
  595. }
  596. static void reportFastISelFailure(MachineFunction &MF,
  597. OptimizationRemarkEmitter &ORE,
  598. OptimizationRemarkMissed &R,
  599. bool ShouldAbort) {
  600. // Print the function name explicitly if we don't have a debug location (which
  601. // makes the diagnostic less useful) or if we're going to emit a raw error.
  602. if (!R.getLocation().isValid() || ShouldAbort)
  603. R << (" (in function: " + MF.getName() + ")").str();
  604. if (ShouldAbort)
  605. report_fatal_error(Twine(R.getMsg()));
  606. ORE.emit(R);
  607. LLVM_DEBUG(dbgs() << R.getMsg() << "\n");
  608. }
  609. void SelectionDAGISel::SelectBasicBlock(BasicBlock::const_iterator Begin,
  610. BasicBlock::const_iterator End,
  611. bool &HadTailCall) {
  612. // Allow creating illegal types during DAG building for the basic block.
  613. CurDAG->NewNodesMustHaveLegalTypes = false;
  614. // Lower the instructions. If a call is emitted as a tail call, cease emitting
  615. // nodes for this block.
  616. for (BasicBlock::const_iterator I = Begin; I != End && !SDB->HasTailCall; ++I) {
  617. if (!ElidedArgCopyInstrs.count(&*I))
  618. SDB->visit(*I);
  619. }
  620. // Make sure the root of the DAG is up-to-date.
  621. CurDAG->setRoot(SDB->getControlRoot());
  622. HadTailCall = SDB->HasTailCall;
  623. SDB->resolveOrClearDbgInfo();
  624. SDB->clear();
  625. // Final step, emit the lowered DAG as machine code.
  626. CodeGenAndEmitDAG();
  627. }
  628. void SelectionDAGISel::ComputeLiveOutVRegInfo() {
  629. SmallPtrSet<SDNode *, 16> Added;
  630. SmallVector<SDNode*, 128> Worklist;
  631. Worklist.push_back(CurDAG->getRoot().getNode());
  632. Added.insert(CurDAG->getRoot().getNode());
  633. KnownBits Known;
  634. do {
  635. SDNode *N = Worklist.pop_back_val();
  636. // Otherwise, add all chain operands to the worklist.
  637. for (const SDValue &Op : N->op_values())
  638. if (Op.getValueType() == MVT::Other && Added.insert(Op.getNode()).second)
  639. Worklist.push_back(Op.getNode());
  640. // If this is a CopyToReg with a vreg dest, process it.
  641. if (N->getOpcode() != ISD::CopyToReg)
  642. continue;
  643. unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
  644. if (!Register::isVirtualRegister(DestReg))
  645. continue;
  646. // Ignore non-integer values.
  647. SDValue Src = N->getOperand(2);
  648. EVT SrcVT = Src.getValueType();
  649. if (!SrcVT.isInteger())
  650. continue;
  651. unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
  652. Known = CurDAG->computeKnownBits(Src);
  653. FuncInfo->AddLiveOutRegInfo(DestReg, NumSignBits, Known);
  654. } while (!Worklist.empty());
  655. }
  656. void SelectionDAGISel::CodeGenAndEmitDAG() {
  657. StringRef GroupName = "sdag";
  658. StringRef GroupDescription = "Instruction Selection and Scheduling";
  659. std::string BlockName;
  660. bool MatchFilterBB = false; (void)MatchFilterBB;
  661. #ifndef NDEBUG
  662. TargetTransformInfo &TTI =
  663. getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*FuncInfo->Fn);
  664. #endif
  665. // Pre-type legalization allow creation of any node types.
  666. CurDAG->NewNodesMustHaveLegalTypes = false;
  667. #ifndef NDEBUG
  668. MatchFilterBB = (FilterDAGBasicBlockName.empty() ||
  669. FilterDAGBasicBlockName ==
  670. FuncInfo->MBB->getBasicBlock()->getName());
  671. #endif
  672. #ifdef NDEBUG
  673. if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewDAGCombineLT ||
  674. ViewLegalizeDAGs || ViewDAGCombine2 || ViewISelDAGs || ViewSchedDAGs ||
  675. ViewSUnitDAGs)
  676. #endif
  677. {
  678. BlockName =
  679. (MF->getName() + ":" + FuncInfo->MBB->getBasicBlock()->getName()).str();
  680. }
  681. LLVM_DEBUG(dbgs() << "Initial selection DAG: "
  682. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  683. << "'\n";
  684. CurDAG->dump());
  685. #ifndef NDEBUG
  686. if (TTI.hasBranchDivergence())
  687. CurDAG->VerifyDAGDivergence();
  688. #endif
  689. if (ViewDAGCombine1 && MatchFilterBB)
  690. CurDAG->viewGraph("dag-combine1 input for " + BlockName);
  691. // Run the DAG combiner in pre-legalize mode.
  692. {
  693. NamedRegionTimer T("combine1", "DAG Combining 1", GroupName,
  694. GroupDescription, TimePassesIsEnabled);
  695. CurDAG->Combine(BeforeLegalizeTypes, AA, OptLevel);
  696. }
  697. LLVM_DEBUG(dbgs() << "Optimized lowered selection DAG: "
  698. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  699. << "'\n";
  700. CurDAG->dump());
  701. #ifndef NDEBUG
  702. if (TTI.hasBranchDivergence())
  703. CurDAG->VerifyDAGDivergence();
  704. #endif
  705. // Second step, hack on the DAG until it only uses operations and types that
  706. // the target supports.
  707. if (ViewLegalizeTypesDAGs && MatchFilterBB)
  708. CurDAG->viewGraph("legalize-types input for " + BlockName);
  709. bool Changed;
  710. {
  711. NamedRegionTimer T("legalize_types", "Type Legalization", GroupName,
  712. GroupDescription, TimePassesIsEnabled);
  713. Changed = CurDAG->LegalizeTypes();
  714. }
  715. LLVM_DEBUG(dbgs() << "Type-legalized selection DAG: "
  716. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  717. << "'\n";
  718. CurDAG->dump());
  719. #ifndef NDEBUG
  720. if (TTI.hasBranchDivergence())
  721. CurDAG->VerifyDAGDivergence();
  722. #endif
  723. // Only allow creation of legal node types.
  724. CurDAG->NewNodesMustHaveLegalTypes = true;
  725. if (Changed) {
  726. if (ViewDAGCombineLT && MatchFilterBB)
  727. CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
  728. // Run the DAG combiner in post-type-legalize mode.
  729. {
  730. NamedRegionTimer T("combine_lt", "DAG Combining after legalize types",
  731. GroupName, GroupDescription, TimePassesIsEnabled);
  732. CurDAG->Combine(AfterLegalizeTypes, AA, OptLevel);
  733. }
  734. LLVM_DEBUG(dbgs() << "Optimized type-legalized selection DAG: "
  735. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  736. << "'\n";
  737. CurDAG->dump());
  738. #ifndef NDEBUG
  739. if (TTI.hasBranchDivergence())
  740. CurDAG->VerifyDAGDivergence();
  741. #endif
  742. }
  743. {
  744. NamedRegionTimer T("legalize_vec", "Vector Legalization", GroupName,
  745. GroupDescription, TimePassesIsEnabled);
  746. Changed = CurDAG->LegalizeVectors();
  747. }
  748. if (Changed) {
  749. LLVM_DEBUG(dbgs() << "Vector-legalized selection DAG: "
  750. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  751. << "'\n";
  752. CurDAG->dump());
  753. #ifndef NDEBUG
  754. if (TTI.hasBranchDivergence())
  755. CurDAG->VerifyDAGDivergence();
  756. #endif
  757. {
  758. NamedRegionTimer T("legalize_types2", "Type Legalization 2", GroupName,
  759. GroupDescription, TimePassesIsEnabled);
  760. CurDAG->LegalizeTypes();
  761. }
  762. LLVM_DEBUG(dbgs() << "Vector/type-legalized selection DAG: "
  763. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  764. << "'\n";
  765. CurDAG->dump());
  766. #ifndef NDEBUG
  767. if (TTI.hasBranchDivergence())
  768. CurDAG->VerifyDAGDivergence();
  769. #endif
  770. if (ViewDAGCombineLT && MatchFilterBB)
  771. CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
  772. // Run the DAG combiner in post-type-legalize mode.
  773. {
  774. NamedRegionTimer T("combine_lv", "DAG Combining after legalize vectors",
  775. GroupName, GroupDescription, TimePassesIsEnabled);
  776. CurDAG->Combine(AfterLegalizeVectorOps, AA, OptLevel);
  777. }
  778. LLVM_DEBUG(dbgs() << "Optimized vector-legalized selection DAG: "
  779. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  780. << "'\n";
  781. CurDAG->dump());
  782. #ifndef NDEBUG
  783. if (TTI.hasBranchDivergence())
  784. CurDAG->VerifyDAGDivergence();
  785. #endif
  786. }
  787. if (ViewLegalizeDAGs && MatchFilterBB)
  788. CurDAG->viewGraph("legalize input for " + BlockName);
  789. {
  790. NamedRegionTimer T("legalize", "DAG Legalization", GroupName,
  791. GroupDescription, TimePassesIsEnabled);
  792. CurDAG->Legalize();
  793. }
  794. LLVM_DEBUG(dbgs() << "Legalized selection DAG: "
  795. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  796. << "'\n";
  797. CurDAG->dump());
  798. #ifndef NDEBUG
  799. if (TTI.hasBranchDivergence())
  800. CurDAG->VerifyDAGDivergence();
  801. #endif
  802. if (ViewDAGCombine2 && MatchFilterBB)
  803. CurDAG->viewGraph("dag-combine2 input for " + BlockName);
  804. // Run the DAG combiner in post-legalize mode.
  805. {
  806. NamedRegionTimer T("combine2", "DAG Combining 2", GroupName,
  807. GroupDescription, TimePassesIsEnabled);
  808. CurDAG->Combine(AfterLegalizeDAG, AA, OptLevel);
  809. }
  810. LLVM_DEBUG(dbgs() << "Optimized legalized selection DAG: "
  811. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  812. << "'\n";
  813. CurDAG->dump());
  814. #ifndef NDEBUG
  815. if (TTI.hasBranchDivergence())
  816. CurDAG->VerifyDAGDivergence();
  817. #endif
  818. if (OptLevel != CodeGenOpt::None)
  819. ComputeLiveOutVRegInfo();
  820. if (ViewISelDAGs && MatchFilterBB)
  821. CurDAG->viewGraph("isel input for " + BlockName);
  822. // Third, instruction select all of the operations to machine code, adding the
  823. // code to the MachineBasicBlock.
  824. {
  825. NamedRegionTimer T("isel", "Instruction Selection", GroupName,
  826. GroupDescription, TimePassesIsEnabled);
  827. DoInstructionSelection();
  828. }
  829. LLVM_DEBUG(dbgs() << "Selected selection DAG: "
  830. << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
  831. << "'\n";
  832. CurDAG->dump());
  833. if (ViewSchedDAGs && MatchFilterBB)
  834. CurDAG->viewGraph("scheduler input for " + BlockName);
  835. // Schedule machine code.
  836. ScheduleDAGSDNodes *Scheduler = CreateScheduler();
  837. {
  838. NamedRegionTimer T("sched", "Instruction Scheduling", GroupName,
  839. GroupDescription, TimePassesIsEnabled);
  840. Scheduler->Run(CurDAG, FuncInfo->MBB);
  841. }
  842. if (ViewSUnitDAGs && MatchFilterBB)
  843. Scheduler->viewGraph();
  844. // Emit machine code to BB. This can change 'BB' to the last block being
  845. // inserted into.
  846. MachineBasicBlock *FirstMBB = FuncInfo->MBB, *LastMBB;
  847. {
  848. NamedRegionTimer T("emit", "Instruction Creation", GroupName,
  849. GroupDescription, TimePassesIsEnabled);
  850. // FuncInfo->InsertPt is passed by reference and set to the end of the
  851. // scheduled instructions.
  852. LastMBB = FuncInfo->MBB = Scheduler->EmitSchedule(FuncInfo->InsertPt);
  853. }
  854. // If the block was split, make sure we update any references that are used to
  855. // update PHI nodes later on.
  856. if (FirstMBB != LastMBB)
  857. SDB->UpdateSplitBlock(FirstMBB, LastMBB);
  858. // Free the scheduler state.
  859. {
  860. NamedRegionTimer T("cleanup", "Instruction Scheduling Cleanup", GroupName,
  861. GroupDescription, TimePassesIsEnabled);
  862. delete Scheduler;
  863. }
  864. // Free the SelectionDAG state, now that we're finished with it.
  865. CurDAG->clear();
  866. }
  867. namespace {
  868. /// ISelUpdater - helper class to handle updates of the instruction selection
  869. /// graph.
  870. class ISelUpdater : public SelectionDAG::DAGUpdateListener {
  871. SelectionDAG::allnodes_iterator &ISelPosition;
  872. public:
  873. ISelUpdater(SelectionDAG &DAG, SelectionDAG::allnodes_iterator &isp)
  874. : SelectionDAG::DAGUpdateListener(DAG), ISelPosition(isp) {}
  875. /// NodeDeleted - Handle nodes deleted from the graph. If the node being
  876. /// deleted is the current ISelPosition node, update ISelPosition.
  877. ///
  878. void NodeDeleted(SDNode *N, SDNode *E) override {
  879. if (ISelPosition == SelectionDAG::allnodes_iterator(N))
  880. ++ISelPosition;
  881. }
  882. /// NodeInserted - Handle new nodes inserted into the graph: propagate
  883. /// metadata from root nodes that also applies to new nodes, in case the root
  884. /// is later deleted.
  885. void NodeInserted(SDNode *N) override {
  886. SDNode *CurNode = &*ISelPosition;
  887. if (MDNode *MD = DAG.getPCSections(CurNode))
  888. DAG.addPCSections(N, MD);
  889. }
  890. };
  891. } // end anonymous namespace
  892. // This function is used to enforce the topological node id property
  893. // leveraged during instruction selection. Before the selection process all
  894. // nodes are given a non-negative id such that all nodes have a greater id than
  895. // their operands. As this holds transitively we can prune checks that a node N
  896. // is a predecessor of M another by not recursively checking through M's
  897. // operands if N's ID is larger than M's ID. This significantly improves
  898. // performance of various legality checks (e.g. IsLegalToFold / UpdateChains).
  899. // However, when we fuse multiple nodes into a single node during the
  900. // selection we may induce a predecessor relationship between inputs and
  901. // outputs of distinct nodes being merged, violating the topological property.
  902. // Should a fused node have a successor which has yet to be selected,
  903. // our legality checks would be incorrect. To avoid this we mark all unselected
  904. // successor nodes, i.e. id != -1, as invalid for pruning by bit-negating (x =>
  905. // (-(x+1))) the ids and modify our pruning check to ignore negative Ids of M.
  906. // We use bit-negation to more clearly enforce that node id -1 can only be
  907. // achieved by selected nodes. As the conversion is reversable to the original
  908. // Id, topological pruning can still be leveraged when looking for unselected
  909. // nodes. This method is called internally in all ISel replacement related
  910. // functions.
  911. void SelectionDAGISel::EnforceNodeIdInvariant(SDNode *Node) {
  912. SmallVector<SDNode *, 4> Nodes;
  913. Nodes.push_back(Node);
  914. while (!Nodes.empty()) {
  915. SDNode *N = Nodes.pop_back_val();
  916. for (auto *U : N->uses()) {
  917. auto UId = U->getNodeId();
  918. if (UId > 0) {
  919. InvalidateNodeId(U);
  920. Nodes.push_back(U);
  921. }
  922. }
  923. }
  924. }
  925. // InvalidateNodeId - As explained in EnforceNodeIdInvariant, mark a
  926. // NodeId with the equivalent node id which is invalid for topological
  927. // pruning.
  928. void SelectionDAGISel::InvalidateNodeId(SDNode *N) {
  929. int InvalidId = -(N->getNodeId() + 1);
  930. N->setNodeId(InvalidId);
  931. }
  932. // getUninvalidatedNodeId - get original uninvalidated node id.
  933. int SelectionDAGISel::getUninvalidatedNodeId(SDNode *N) {
  934. int Id = N->getNodeId();
  935. if (Id < -1)
  936. return -(Id + 1);
  937. return Id;
  938. }
  939. void SelectionDAGISel::DoInstructionSelection() {
  940. LLVM_DEBUG(dbgs() << "===== Instruction selection begins: "
  941. << printMBBReference(*FuncInfo->MBB) << " '"
  942. << FuncInfo->MBB->getName() << "'\n");
  943. PreprocessISelDAG();
  944. // Select target instructions for the DAG.
  945. {
  946. // Number all nodes with a topological order and set DAGSize.
  947. DAGSize = CurDAG->AssignTopologicalOrder();
  948. // Create a dummy node (which is not added to allnodes), that adds
  949. // a reference to the root node, preventing it from being deleted,
  950. // and tracking any changes of the root.
  951. HandleSDNode Dummy(CurDAG->getRoot());
  952. SelectionDAG::allnodes_iterator ISelPosition (CurDAG->getRoot().getNode());
  953. ++ISelPosition;
  954. // Make sure that ISelPosition gets properly updated when nodes are deleted
  955. // in calls made from this function. New nodes inherit relevant metadata.
  956. ISelUpdater ISU(*CurDAG, ISelPosition);
  957. // The AllNodes list is now topological-sorted. Visit the
  958. // nodes by starting at the end of the list (the root of the
  959. // graph) and preceding back toward the beginning (the entry
  960. // node).
  961. while (ISelPosition != CurDAG->allnodes_begin()) {
  962. SDNode *Node = &*--ISelPosition;
  963. // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes,
  964. // but there are currently some corner cases that it misses. Also, this
  965. // makes it theoretically possible to disable the DAGCombiner.
  966. if (Node->use_empty())
  967. continue;
  968. #ifndef NDEBUG
  969. SmallVector<SDNode *, 4> Nodes;
  970. Nodes.push_back(Node);
  971. while (!Nodes.empty()) {
  972. auto N = Nodes.pop_back_val();
  973. if (N->getOpcode() == ISD::TokenFactor || N->getNodeId() < 0)
  974. continue;
  975. for (const SDValue &Op : N->op_values()) {
  976. if (Op->getOpcode() == ISD::TokenFactor)
  977. Nodes.push_back(Op.getNode());
  978. else {
  979. // We rely on topological ordering of node ids for checking for
  980. // cycles when fusing nodes during selection. All unselected nodes
  981. // successors of an already selected node should have a negative id.
  982. // This assertion will catch such cases. If this assertion triggers
  983. // it is likely you using DAG-level Value/Node replacement functions
  984. // (versus equivalent ISEL replacement) in backend-specific
  985. // selections. See comment in EnforceNodeIdInvariant for more
  986. // details.
  987. assert(Op->getNodeId() != -1 &&
  988. "Node has already selected predecessor node");
  989. }
  990. }
  991. }
  992. #endif
  993. // When we are using non-default rounding modes or FP exception behavior
  994. // FP operations are represented by StrictFP pseudo-operations. For
  995. // targets that do not (yet) understand strict FP operations directly,
  996. // we convert them to normal FP opcodes instead at this point. This
  997. // will allow them to be handled by existing target-specific instruction
  998. // selectors.
  999. if (!TLI->isStrictFPEnabled() && Node->isStrictFPOpcode()) {
  1000. // For some opcodes, we need to call TLI->getOperationAction using
  1001. // the first operand type instead of the result type. Note that this
  1002. // must match what SelectionDAGLegalize::LegalizeOp is doing.
  1003. EVT ActionVT;
  1004. switch (Node->getOpcode()) {
  1005. case ISD::STRICT_SINT_TO_FP:
  1006. case ISD::STRICT_UINT_TO_FP:
  1007. case ISD::STRICT_LRINT:
  1008. case ISD::STRICT_LLRINT:
  1009. case ISD::STRICT_LROUND:
  1010. case ISD::STRICT_LLROUND:
  1011. case ISD::STRICT_FSETCC:
  1012. case ISD::STRICT_FSETCCS:
  1013. ActionVT = Node->getOperand(1).getValueType();
  1014. break;
  1015. default:
  1016. ActionVT = Node->getValueType(0);
  1017. break;
  1018. }
  1019. if (TLI->getOperationAction(Node->getOpcode(), ActionVT)
  1020. == TargetLowering::Expand)
  1021. Node = CurDAG->mutateStrictFPToFP(Node);
  1022. }
  1023. LLVM_DEBUG(dbgs() << "\nISEL: Starting selection on root node: ";
  1024. Node->dump(CurDAG));
  1025. Select(Node);
  1026. }
  1027. CurDAG->setRoot(Dummy.getValue());
  1028. }
  1029. LLVM_DEBUG(dbgs() << "\n===== Instruction selection ends:\n");
  1030. PostprocessISelDAG();
  1031. }
  1032. static bool hasExceptionPointerOrCodeUser(const CatchPadInst *CPI) {
  1033. for (const User *U : CPI->users()) {
  1034. if (const IntrinsicInst *EHPtrCall = dyn_cast<IntrinsicInst>(U)) {
  1035. Intrinsic::ID IID = EHPtrCall->getIntrinsicID();
  1036. if (IID == Intrinsic::eh_exceptionpointer ||
  1037. IID == Intrinsic::eh_exceptioncode)
  1038. return true;
  1039. }
  1040. }
  1041. return false;
  1042. }
  1043. // wasm.landingpad.index intrinsic is for associating a landing pad index number
  1044. // with a catchpad instruction. Retrieve the landing pad index in the intrinsic
  1045. // and store the mapping in the function.
  1046. static void mapWasmLandingPadIndex(MachineBasicBlock *MBB,
  1047. const CatchPadInst *CPI) {
  1048. MachineFunction *MF = MBB->getParent();
  1049. // In case of single catch (...), we don't emit LSDA, so we don't need
  1050. // this information.
  1051. bool IsSingleCatchAllClause =
  1052. CPI->arg_size() == 1 &&
  1053. cast<Constant>(CPI->getArgOperand(0))->isNullValue();
  1054. // cathchpads for longjmp use an empty type list, e.g. catchpad within %0 []
  1055. // and they don't need LSDA info
  1056. bool IsCatchLongjmp = CPI->arg_size() == 0;
  1057. if (!IsSingleCatchAllClause && !IsCatchLongjmp) {
  1058. // Create a mapping from landing pad label to landing pad index.
  1059. bool IntrFound = false;
  1060. for (const User *U : CPI->users()) {
  1061. if (const auto *Call = dyn_cast<IntrinsicInst>(U)) {
  1062. Intrinsic::ID IID = Call->getIntrinsicID();
  1063. if (IID == Intrinsic::wasm_landingpad_index) {
  1064. Value *IndexArg = Call->getArgOperand(1);
  1065. int Index = cast<ConstantInt>(IndexArg)->getZExtValue();
  1066. MF->setWasmLandingPadIndex(MBB, Index);
  1067. IntrFound = true;
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. assert(IntrFound && "wasm.landingpad.index intrinsic not found!");
  1073. (void)IntrFound;
  1074. }
  1075. }
  1076. /// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and
  1077. /// do other setup for EH landing-pad blocks.
  1078. bool SelectionDAGISel::PrepareEHLandingPad() {
  1079. MachineBasicBlock *MBB = FuncInfo->MBB;
  1080. const Constant *PersonalityFn = FuncInfo->Fn->getPersonalityFn();
  1081. const BasicBlock *LLVMBB = MBB->getBasicBlock();
  1082. const TargetRegisterClass *PtrRC =
  1083. TLI->getRegClassFor(TLI->getPointerTy(CurDAG->getDataLayout()));
  1084. auto Pers = classifyEHPersonality(PersonalityFn);
  1085. // Catchpads have one live-in register, which typically holds the exception
  1086. // pointer or code.
  1087. if (isFuncletEHPersonality(Pers)) {
  1088. if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI())) {
  1089. if (hasExceptionPointerOrCodeUser(CPI)) {
  1090. // Get or create the virtual register to hold the pointer or code. Mark
  1091. // the live in physreg and copy into the vreg.
  1092. MCPhysReg EHPhysReg = TLI->getExceptionPointerRegister(PersonalityFn);
  1093. assert(EHPhysReg && "target lacks exception pointer register");
  1094. MBB->addLiveIn(EHPhysReg);
  1095. unsigned VReg = FuncInfo->getCatchPadExceptionPointerVReg(CPI, PtrRC);
  1096. BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(),
  1097. TII->get(TargetOpcode::COPY), VReg)
  1098. .addReg(EHPhysReg, RegState::Kill);
  1099. }
  1100. }
  1101. return true;
  1102. }
  1103. // Add a label to mark the beginning of the landing pad. Deletion of the
  1104. // landing pad can thus be detected via the MachineModuleInfo.
  1105. MCSymbol *Label = MF->addLandingPad(MBB);
  1106. const MCInstrDesc &II = TII->get(TargetOpcode::EH_LABEL);
  1107. BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), II)
  1108. .addSym(Label);
  1109. // If the unwinder does not preserve all registers, ensure that the
  1110. // function marks the clobbered registers as used.
  1111. const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
  1112. if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF))
  1113. MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
  1114. if (Pers == EHPersonality::Wasm_CXX) {
  1115. if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI()))
  1116. mapWasmLandingPadIndex(MBB, CPI);
  1117. } else {
  1118. // Assign the call site to the landing pad's begin label.
  1119. MF->setCallSiteLandingPad(Label, SDB->LPadToCallSiteMap[MBB]);
  1120. // Mark exception register as live in.
  1121. if (unsigned Reg = TLI->getExceptionPointerRegister(PersonalityFn))
  1122. FuncInfo->ExceptionPointerVirtReg = MBB->addLiveIn(Reg, PtrRC);
  1123. // Mark exception selector register as live in.
  1124. if (unsigned Reg = TLI->getExceptionSelectorRegister(PersonalityFn))
  1125. FuncInfo->ExceptionSelectorVirtReg = MBB->addLiveIn(Reg, PtrRC);
  1126. }
  1127. return true;
  1128. }
  1129. /// isFoldedOrDeadInstruction - Return true if the specified instruction is
  1130. /// side-effect free and is either dead or folded into a generated instruction.
  1131. /// Return false if it needs to be emitted.
  1132. static bool isFoldedOrDeadInstruction(const Instruction *I,
  1133. const FunctionLoweringInfo &FuncInfo) {
  1134. return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded.
  1135. !I->isTerminator() && // Terminators aren't folded.
  1136. !isa<DbgInfoIntrinsic>(I) && // Debug instructions aren't folded.
  1137. !I->isEHPad() && // EH pad instructions aren't folded.
  1138. !FuncInfo.isExportedInst(I); // Exported instrs must be computed.
  1139. }
  1140. static void processDbgDeclare(FunctionLoweringInfo &FuncInfo,
  1141. const Value *Address, DIExpression *Expr,
  1142. DILocalVariable *Var, DebugLoc DbgLoc) {
  1143. MachineFunction *MF = FuncInfo.MF;
  1144. const DataLayout &DL = MF->getDataLayout();
  1145. assert(Var && "Missing variable");
  1146. assert(DbgLoc && "Missing location");
  1147. // Look through casts and constant offset GEPs. These mostly come from
  1148. // inalloca.
  1149. APInt Offset(DL.getTypeSizeInBits(Address->getType()), 0);
  1150. Address = Address->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
  1151. // Check if the variable is a static alloca or a byval or inalloca
  1152. // argument passed in memory. If it is not, then we will ignore this
  1153. // intrinsic and handle this during isel like dbg.value.
  1154. int FI = std::numeric_limits<int>::max();
  1155. if (const auto *AI = dyn_cast<AllocaInst>(Address)) {
  1156. auto SI = FuncInfo.StaticAllocaMap.find(AI);
  1157. if (SI != FuncInfo.StaticAllocaMap.end())
  1158. FI = SI->second;
  1159. } else if (const auto *Arg = dyn_cast<Argument>(Address))
  1160. FI = FuncInfo.getArgumentFrameIndex(Arg);
  1161. if (FI == std::numeric_limits<int>::max())
  1162. return;
  1163. if (Offset.getBoolValue())
  1164. Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset,
  1165. Offset.getZExtValue());
  1166. LLVM_DEBUG(dbgs() << "processDbgDeclare: setVariableDbgInfo Var=" << *Var
  1167. << ", Expr=" << *Expr << ", FI=" << FI
  1168. << ", DbgLoc=" << DbgLoc << "\n");
  1169. MF->setVariableDbgInfo(Var, Expr, FI, DbgLoc);
  1170. }
  1171. /// Collect llvm.dbg.declare information. This is done after argument lowering
  1172. /// in case the declarations refer to arguments.
  1173. static void processDbgDeclares(FunctionLoweringInfo &FuncInfo) {
  1174. for (const BasicBlock &BB : *FuncInfo.Fn) {
  1175. for (const Instruction &I : BB) {
  1176. if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(&I)) {
  1177. Value *Address = DI->getAddress();
  1178. if (!Address) {
  1179. LLVM_DEBUG(dbgs() << "processDbgDeclares skipping " << *DI
  1180. << " (bad address)\n");
  1181. continue;
  1182. }
  1183. processDbgDeclare(FuncInfo, Address, DI->getExpression(),
  1184. DI->getVariable(), DI->getDebugLoc());
  1185. }
  1186. }
  1187. }
  1188. }
  1189. /// Collect single location variable information generated with assignment
  1190. /// tracking. This is done after argument lowering in case the declarations
  1191. /// refer to arguments.
  1192. static void processSingleLocVars(FunctionLoweringInfo &FuncInfo,
  1193. FunctionVarLocs const *FnVarLocs) {
  1194. for (auto It = FnVarLocs->single_locs_begin(),
  1195. End = FnVarLocs->single_locs_end();
  1196. It != End; ++It)
  1197. processDbgDeclare(FuncInfo, It->V, It->Expr,
  1198. FnVarLocs->getDILocalVariable(It->VariableID), It->DL);
  1199. }
  1200. void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
  1201. FastISelFailed = false;
  1202. // Initialize the Fast-ISel state, if needed.
  1203. FastISel *FastIS = nullptr;
  1204. if (TM.Options.EnableFastISel) {
  1205. LLVM_DEBUG(dbgs() << "Enabling fast-isel\n");
  1206. FastIS = TLI->createFastISel(*FuncInfo, LibInfo);
  1207. }
  1208. ReversePostOrderTraversal<const Function*> RPOT(&Fn);
  1209. // Lower arguments up front. An RPO iteration always visits the entry block
  1210. // first.
  1211. assert(*RPOT.begin() == &Fn.getEntryBlock());
  1212. ++NumEntryBlocks;
  1213. // Set up FuncInfo for ISel. Entry blocks never have PHIs.
  1214. FuncInfo->MBB = FuncInfo->MBBMap[&Fn.getEntryBlock()];
  1215. FuncInfo->InsertPt = FuncInfo->MBB->begin();
  1216. CurDAG->setFunctionLoweringInfo(FuncInfo.get());
  1217. if (!FastIS) {
  1218. LowerArguments(Fn);
  1219. } else {
  1220. // See if fast isel can lower the arguments.
  1221. FastIS->startNewBlock();
  1222. if (!FastIS->lowerArguments()) {
  1223. FastISelFailed = true;
  1224. // Fast isel failed to lower these arguments
  1225. ++NumFastIselFailLowerArguments;
  1226. OptimizationRemarkMissed R("sdagisel", "FastISelFailure",
  1227. Fn.getSubprogram(),
  1228. &Fn.getEntryBlock());
  1229. R << "FastISel didn't lower all arguments: "
  1230. << ore::NV("Prototype", Fn.getType());
  1231. reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 1);
  1232. // Use SelectionDAG argument lowering
  1233. LowerArguments(Fn);
  1234. CurDAG->setRoot(SDB->getControlRoot());
  1235. SDB->clear();
  1236. CodeGenAndEmitDAG();
  1237. }
  1238. // If we inserted any instructions at the beginning, make a note of
  1239. // where they are, so we can be sure to emit subsequent instructions
  1240. // after them.
  1241. if (FuncInfo->InsertPt != FuncInfo->MBB->begin())
  1242. FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt));
  1243. else
  1244. FastIS->setLastLocalValue(nullptr);
  1245. }
  1246. bool Inserted = SwiftError->createEntriesInEntryBlock(SDB->getCurDebugLoc());
  1247. if (FastIS && Inserted)
  1248. FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt));
  1249. if (isAssignmentTrackingEnabled(*Fn.getParent())) {
  1250. assert(CurDAG->getFunctionVarLocs() &&
  1251. "expected AssignmentTrackingAnalysis pass results");
  1252. processSingleLocVars(*FuncInfo, CurDAG->getFunctionVarLocs());
  1253. } else {
  1254. processDbgDeclares(*FuncInfo);
  1255. }
  1256. // Iterate over all basic blocks in the function.
  1257. StackProtector &SP = getAnalysis<StackProtector>();
  1258. for (const BasicBlock *LLVMBB : RPOT) {
  1259. if (OptLevel != CodeGenOpt::None) {
  1260. bool AllPredsVisited = true;
  1261. for (const BasicBlock *Pred : predecessors(LLVMBB)) {
  1262. if (!FuncInfo->VisitedBBs.count(Pred)) {
  1263. AllPredsVisited = false;
  1264. break;
  1265. }
  1266. }
  1267. if (AllPredsVisited) {
  1268. for (const PHINode &PN : LLVMBB->phis())
  1269. FuncInfo->ComputePHILiveOutRegInfo(&PN);
  1270. } else {
  1271. for (const PHINode &PN : LLVMBB->phis())
  1272. FuncInfo->InvalidatePHILiveOutRegInfo(&PN);
  1273. }
  1274. FuncInfo->VisitedBBs.insert(LLVMBB);
  1275. }
  1276. BasicBlock::const_iterator const Begin =
  1277. LLVMBB->getFirstNonPHI()->getIterator();
  1278. BasicBlock::const_iterator const End = LLVMBB->end();
  1279. BasicBlock::const_iterator BI = End;
  1280. FuncInfo->MBB = FuncInfo->MBBMap[LLVMBB];
  1281. if (!FuncInfo->MBB)
  1282. continue; // Some blocks like catchpads have no code or MBB.
  1283. // Insert new instructions after any phi or argument setup code.
  1284. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1285. // Setup an EH landing-pad block.
  1286. FuncInfo->ExceptionPointerVirtReg = 0;
  1287. FuncInfo->ExceptionSelectorVirtReg = 0;
  1288. if (LLVMBB->isEHPad())
  1289. if (!PrepareEHLandingPad())
  1290. continue;
  1291. // Before doing SelectionDAG ISel, see if FastISel has been requested.
  1292. if (FastIS) {
  1293. if (LLVMBB != &Fn.getEntryBlock())
  1294. FastIS->startNewBlock();
  1295. unsigned NumFastIselRemaining = std::distance(Begin, End);
  1296. // Pre-assign swifterror vregs.
  1297. SwiftError->preassignVRegs(FuncInfo->MBB, Begin, End);
  1298. // Do FastISel on as many instructions as possible.
  1299. for (; BI != Begin; --BI) {
  1300. const Instruction *Inst = &*std::prev(BI);
  1301. // If we no longer require this instruction, skip it.
  1302. if (isFoldedOrDeadInstruction(Inst, *FuncInfo) ||
  1303. ElidedArgCopyInstrs.count(Inst)) {
  1304. --NumFastIselRemaining;
  1305. continue;
  1306. }
  1307. // Bottom-up: reset the insert pos at the top, after any local-value
  1308. // instructions.
  1309. FastIS->recomputeInsertPt();
  1310. // Try to select the instruction with FastISel.
  1311. if (FastIS->selectInstruction(Inst)) {
  1312. --NumFastIselRemaining;
  1313. ++NumFastIselSuccess;
  1314. // If fast isel succeeded, skip over all the folded instructions, and
  1315. // then see if there is a load right before the selected instructions.
  1316. // Try to fold the load if so.
  1317. const Instruction *BeforeInst = Inst;
  1318. while (BeforeInst != &*Begin) {
  1319. BeforeInst = &*std::prev(BasicBlock::const_iterator(BeforeInst));
  1320. if (!isFoldedOrDeadInstruction(BeforeInst, *FuncInfo))
  1321. break;
  1322. }
  1323. if (BeforeInst != Inst && isa<LoadInst>(BeforeInst) &&
  1324. BeforeInst->hasOneUse() &&
  1325. FastIS->tryToFoldLoad(cast<LoadInst>(BeforeInst), Inst)) {
  1326. // If we succeeded, don't re-select the load.
  1327. LLVM_DEBUG(dbgs()
  1328. << "FastISel folded load: " << *BeforeInst << "\n");
  1329. BI = std::next(BasicBlock::const_iterator(BeforeInst));
  1330. --NumFastIselRemaining;
  1331. ++NumFastIselSuccess;
  1332. }
  1333. continue;
  1334. }
  1335. FastISelFailed = true;
  1336. // Then handle certain instructions as single-LLVM-Instruction blocks.
  1337. // We cannot separate out GCrelocates to their own blocks since we need
  1338. // to keep track of gc-relocates for a particular gc-statepoint. This is
  1339. // done by SelectionDAGBuilder::LowerAsSTATEPOINT, called before
  1340. // visitGCRelocate.
  1341. if (isa<CallInst>(Inst) && !isa<GCStatepointInst>(Inst) &&
  1342. !isa<GCRelocateInst>(Inst) && !isa<GCResultInst>(Inst)) {
  1343. OptimizationRemarkMissed R("sdagisel", "FastISelFailure",
  1344. Inst->getDebugLoc(), LLVMBB);
  1345. R << "FastISel missed call";
  1346. if (R.isEnabled() || EnableFastISelAbort) {
  1347. std::string InstStrStorage;
  1348. raw_string_ostream InstStr(InstStrStorage);
  1349. InstStr << *Inst;
  1350. R << ": " << InstStr.str();
  1351. }
  1352. reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 2);
  1353. if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy() &&
  1354. !Inst->use_empty()) {
  1355. Register &R = FuncInfo->ValueMap[Inst];
  1356. if (!R)
  1357. R = FuncInfo->CreateRegs(Inst);
  1358. }
  1359. bool HadTailCall = false;
  1360. MachineBasicBlock::iterator SavedInsertPt = FuncInfo->InsertPt;
  1361. SelectBasicBlock(Inst->getIterator(), BI, HadTailCall);
  1362. // If the call was emitted as a tail call, we're done with the block.
  1363. // We also need to delete any previously emitted instructions.
  1364. if (HadTailCall) {
  1365. FastIS->removeDeadCode(SavedInsertPt, FuncInfo->MBB->end());
  1366. --BI;
  1367. break;
  1368. }
  1369. // Recompute NumFastIselRemaining as Selection DAG instruction
  1370. // selection may have handled the call, input args, etc.
  1371. unsigned RemainingNow = std::distance(Begin, BI);
  1372. NumFastIselFailures += NumFastIselRemaining - RemainingNow;
  1373. NumFastIselRemaining = RemainingNow;
  1374. continue;
  1375. }
  1376. OptimizationRemarkMissed R("sdagisel", "FastISelFailure",
  1377. Inst->getDebugLoc(), LLVMBB);
  1378. bool ShouldAbort = EnableFastISelAbort;
  1379. if (Inst->isTerminator()) {
  1380. // Use a different message for terminator misses.
  1381. R << "FastISel missed terminator";
  1382. // Don't abort for terminator unless the level is really high
  1383. ShouldAbort = (EnableFastISelAbort > 2);
  1384. } else {
  1385. R << "FastISel missed";
  1386. }
  1387. if (R.isEnabled() || EnableFastISelAbort) {
  1388. std::string InstStrStorage;
  1389. raw_string_ostream InstStr(InstStrStorage);
  1390. InstStr << *Inst;
  1391. R << ": " << InstStr.str();
  1392. }
  1393. reportFastISelFailure(*MF, *ORE, R, ShouldAbort);
  1394. NumFastIselFailures += NumFastIselRemaining;
  1395. break;
  1396. }
  1397. FastIS->recomputeInsertPt();
  1398. }
  1399. if (SP.shouldEmitSDCheck(*LLVMBB)) {
  1400. bool FunctionBasedInstrumentation =
  1401. TLI->getSSPStackGuardCheck(*Fn.getParent());
  1402. SDB->SPDescriptor.initialize(LLVMBB, FuncInfo->MBBMap[LLVMBB],
  1403. FunctionBasedInstrumentation);
  1404. }
  1405. if (Begin != BI)
  1406. ++NumDAGBlocks;
  1407. else
  1408. ++NumFastIselBlocks;
  1409. if (Begin != BI) {
  1410. // Run SelectionDAG instruction selection on the remainder of the block
  1411. // not handled by FastISel. If FastISel is not run, this is the entire
  1412. // block.
  1413. bool HadTailCall;
  1414. SelectBasicBlock(Begin, BI, HadTailCall);
  1415. // But if FastISel was run, we already selected some of the block.
  1416. // If we emitted a tail-call, we need to delete any previously emitted
  1417. // instruction that follows it.
  1418. if (FastIS && HadTailCall && FuncInfo->InsertPt != FuncInfo->MBB->end())
  1419. FastIS->removeDeadCode(FuncInfo->InsertPt, FuncInfo->MBB->end());
  1420. }
  1421. if (FastIS)
  1422. FastIS->finishBasicBlock();
  1423. FinishBasicBlock();
  1424. FuncInfo->PHINodesToUpdate.clear();
  1425. ElidedArgCopyInstrs.clear();
  1426. }
  1427. SP.copyToMachineFrameInfo(MF->getFrameInfo());
  1428. SwiftError->propagateVRegs();
  1429. delete FastIS;
  1430. SDB->clearDanglingDebugInfo();
  1431. SDB->SPDescriptor.resetPerFunctionState();
  1432. }
  1433. void
  1434. SelectionDAGISel::FinishBasicBlock() {
  1435. LLVM_DEBUG(dbgs() << "Total amount of phi nodes to update: "
  1436. << FuncInfo->PHINodesToUpdate.size() << "\n";
  1437. for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e;
  1438. ++i) dbgs()
  1439. << "Node " << i << " : (" << FuncInfo->PHINodesToUpdate[i].first
  1440. << ", " << FuncInfo->PHINodesToUpdate[i].second << ")\n");
  1441. // Next, now that we know what the last MBB the LLVM BB expanded is, update
  1442. // PHI nodes in successors.
  1443. for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) {
  1444. MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[i].first);
  1445. assert(PHI->isPHI() &&
  1446. "This is not a machine PHI node that we are updating!");
  1447. if (!FuncInfo->MBB->isSuccessor(PHI->getParent()))
  1448. continue;
  1449. PHI.addReg(FuncInfo->PHINodesToUpdate[i].second).addMBB(FuncInfo->MBB);
  1450. }
  1451. // Handle stack protector.
  1452. if (SDB->SPDescriptor.shouldEmitFunctionBasedCheckStackProtector()) {
  1453. // The target provides a guard check function. There is no need to
  1454. // generate error handling code or to split current basic block.
  1455. MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB();
  1456. // Add load and check to the basicblock.
  1457. FuncInfo->MBB = ParentMBB;
  1458. FuncInfo->InsertPt =
  1459. findSplitPointForStackProtector(ParentMBB, *TII);
  1460. SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB);
  1461. CurDAG->setRoot(SDB->getRoot());
  1462. SDB->clear();
  1463. CodeGenAndEmitDAG();
  1464. // Clear the Per-BB State.
  1465. SDB->SPDescriptor.resetPerBBState();
  1466. } else if (SDB->SPDescriptor.shouldEmitStackProtector()) {
  1467. MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB();
  1468. MachineBasicBlock *SuccessMBB = SDB->SPDescriptor.getSuccessMBB();
  1469. // Find the split point to split the parent mbb. At the same time copy all
  1470. // physical registers used in the tail of parent mbb into virtual registers
  1471. // before the split point and back into physical registers after the split
  1472. // point. This prevents us needing to deal with Live-ins and many other
  1473. // register allocation issues caused by us splitting the parent mbb. The
  1474. // register allocator will clean up said virtual copies later on.
  1475. MachineBasicBlock::iterator SplitPoint =
  1476. findSplitPointForStackProtector(ParentMBB, *TII);
  1477. // Splice the terminator of ParentMBB into SuccessMBB.
  1478. SuccessMBB->splice(SuccessMBB->end(), ParentMBB,
  1479. SplitPoint,
  1480. ParentMBB->end());
  1481. // Add compare/jump on neq/jump to the parent BB.
  1482. FuncInfo->MBB = ParentMBB;
  1483. FuncInfo->InsertPt = ParentMBB->end();
  1484. SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB);
  1485. CurDAG->setRoot(SDB->getRoot());
  1486. SDB->clear();
  1487. CodeGenAndEmitDAG();
  1488. // CodeGen Failure MBB if we have not codegened it yet.
  1489. MachineBasicBlock *FailureMBB = SDB->SPDescriptor.getFailureMBB();
  1490. if (FailureMBB->empty()) {
  1491. FuncInfo->MBB = FailureMBB;
  1492. FuncInfo->InsertPt = FailureMBB->end();
  1493. SDB->visitSPDescriptorFailure(SDB->SPDescriptor);
  1494. CurDAG->setRoot(SDB->getRoot());
  1495. SDB->clear();
  1496. CodeGenAndEmitDAG();
  1497. }
  1498. // Clear the Per-BB State.
  1499. SDB->SPDescriptor.resetPerBBState();
  1500. }
  1501. // Lower each BitTestBlock.
  1502. for (auto &BTB : SDB->SL->BitTestCases) {
  1503. // Lower header first, if it wasn't already lowered
  1504. if (!BTB.Emitted) {
  1505. // Set the current basic block to the mbb we wish to insert the code into
  1506. FuncInfo->MBB = BTB.Parent;
  1507. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1508. // Emit the code
  1509. SDB->visitBitTestHeader(BTB, FuncInfo->MBB);
  1510. CurDAG->setRoot(SDB->getRoot());
  1511. SDB->clear();
  1512. CodeGenAndEmitDAG();
  1513. }
  1514. BranchProbability UnhandledProb = BTB.Prob;
  1515. for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) {
  1516. UnhandledProb -= BTB.Cases[j].ExtraProb;
  1517. // Set the current basic block to the mbb we wish to insert the code into
  1518. FuncInfo->MBB = BTB.Cases[j].ThisBB;
  1519. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1520. // Emit the code
  1521. // If all cases cover a contiguous range, it is not necessary to jump to
  1522. // the default block after the last bit test fails. This is because the
  1523. // range check during bit test header creation has guaranteed that every
  1524. // case here doesn't go outside the range. In this case, there is no need
  1525. // to perform the last bit test, as it will always be true. Instead, make
  1526. // the second-to-last bit-test fall through to the target of the last bit
  1527. // test, and delete the last bit test.
  1528. MachineBasicBlock *NextMBB;
  1529. if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
  1530. // Second-to-last bit-test with contiguous range or omitted range
  1531. // check: fall through to the target of the final bit test.
  1532. NextMBB = BTB.Cases[j + 1].TargetBB;
  1533. } else if (j + 1 == ej) {
  1534. // For the last bit test, fall through to Default.
  1535. NextMBB = BTB.Default;
  1536. } else {
  1537. // Otherwise, fall through to the next bit test.
  1538. NextMBB = BTB.Cases[j + 1].ThisBB;
  1539. }
  1540. SDB->visitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j],
  1541. FuncInfo->MBB);
  1542. CurDAG->setRoot(SDB->getRoot());
  1543. SDB->clear();
  1544. CodeGenAndEmitDAG();
  1545. if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
  1546. // Since we're not going to use the final bit test, remove it.
  1547. BTB.Cases.pop_back();
  1548. break;
  1549. }
  1550. }
  1551. // Update PHI Nodes
  1552. for (const std::pair<MachineInstr *, unsigned> &P :
  1553. FuncInfo->PHINodesToUpdate) {
  1554. MachineInstrBuilder PHI(*MF, P.first);
  1555. MachineBasicBlock *PHIBB = PHI->getParent();
  1556. assert(PHI->isPHI() &&
  1557. "This is not a machine PHI node that we are updating!");
  1558. // This is "default" BB. We have two jumps to it. From "header" BB and
  1559. // from last "case" BB, unless the latter was skipped.
  1560. if (PHIBB == BTB.Default) {
  1561. PHI.addReg(P.second).addMBB(BTB.Parent);
  1562. if (!BTB.ContiguousRange) {
  1563. PHI.addReg(P.second).addMBB(BTB.Cases.back().ThisBB);
  1564. }
  1565. }
  1566. // One of "cases" BB.
  1567. for (const SwitchCG::BitTestCase &BT : BTB.Cases) {
  1568. MachineBasicBlock* cBB = BT.ThisBB;
  1569. if (cBB->isSuccessor(PHIBB))
  1570. PHI.addReg(P.second).addMBB(cBB);
  1571. }
  1572. }
  1573. }
  1574. SDB->SL->BitTestCases.clear();
  1575. // If the JumpTable record is filled in, then we need to emit a jump table.
  1576. // Updating the PHI nodes is tricky in this case, since we need to determine
  1577. // whether the PHI is a successor of the range check MBB or the jump table MBB
  1578. for (unsigned i = 0, e = SDB->SL->JTCases.size(); i != e; ++i) {
  1579. // Lower header first, if it wasn't already lowered
  1580. if (!SDB->SL->JTCases[i].first.Emitted) {
  1581. // Set the current basic block to the mbb we wish to insert the code into
  1582. FuncInfo->MBB = SDB->SL->JTCases[i].first.HeaderBB;
  1583. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1584. // Emit the code
  1585. SDB->visitJumpTableHeader(SDB->SL->JTCases[i].second,
  1586. SDB->SL->JTCases[i].first, FuncInfo->MBB);
  1587. CurDAG->setRoot(SDB->getRoot());
  1588. SDB->clear();
  1589. CodeGenAndEmitDAG();
  1590. }
  1591. // Set the current basic block to the mbb we wish to insert the code into
  1592. FuncInfo->MBB = SDB->SL->JTCases[i].second.MBB;
  1593. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1594. // Emit the code
  1595. SDB->visitJumpTable(SDB->SL->JTCases[i].second);
  1596. CurDAG->setRoot(SDB->getRoot());
  1597. SDB->clear();
  1598. CodeGenAndEmitDAG();
  1599. // Update PHI Nodes
  1600. for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size();
  1601. pi != pe; ++pi) {
  1602. MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first);
  1603. MachineBasicBlock *PHIBB = PHI->getParent();
  1604. assert(PHI->isPHI() &&
  1605. "This is not a machine PHI node that we are updating!");
  1606. // "default" BB. We can go there only from header BB.
  1607. if (PHIBB == SDB->SL->JTCases[i].second.Default)
  1608. PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second)
  1609. .addMBB(SDB->SL->JTCases[i].first.HeaderBB);
  1610. // JT BB. Just iterate over successors here
  1611. if (FuncInfo->MBB->isSuccessor(PHIBB))
  1612. PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(FuncInfo->MBB);
  1613. }
  1614. }
  1615. SDB->SL->JTCases.clear();
  1616. // If we generated any switch lowering information, build and codegen any
  1617. // additional DAGs necessary.
  1618. for (unsigned i = 0, e = SDB->SL->SwitchCases.size(); i != e; ++i) {
  1619. // Set the current basic block to the mbb we wish to insert the code into
  1620. FuncInfo->MBB = SDB->SL->SwitchCases[i].ThisBB;
  1621. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1622. // Determine the unique successors.
  1623. SmallVector<MachineBasicBlock *, 2> Succs;
  1624. Succs.push_back(SDB->SL->SwitchCases[i].TrueBB);
  1625. if (SDB->SL->SwitchCases[i].TrueBB != SDB->SL->SwitchCases[i].FalseBB)
  1626. Succs.push_back(SDB->SL->SwitchCases[i].FalseBB);
  1627. // Emit the code. Note that this could result in FuncInfo->MBB being split.
  1628. SDB->visitSwitchCase(SDB->SL->SwitchCases[i], FuncInfo->MBB);
  1629. CurDAG->setRoot(SDB->getRoot());
  1630. SDB->clear();
  1631. CodeGenAndEmitDAG();
  1632. // Remember the last block, now that any splitting is done, for use in
  1633. // populating PHI nodes in successors.
  1634. MachineBasicBlock *ThisBB = FuncInfo->MBB;
  1635. // Handle any PHI nodes in successors of this chunk, as if we were coming
  1636. // from the original BB before switch expansion. Note that PHI nodes can
  1637. // occur multiple times in PHINodesToUpdate. We have to be very careful to
  1638. // handle them the right number of times.
  1639. for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
  1640. FuncInfo->MBB = Succs[i];
  1641. FuncInfo->InsertPt = FuncInfo->MBB->end();
  1642. // FuncInfo->MBB may have been removed from the CFG if a branch was
  1643. // constant folded.
  1644. if (ThisBB->isSuccessor(FuncInfo->MBB)) {
  1645. for (MachineBasicBlock::iterator
  1646. MBBI = FuncInfo->MBB->begin(), MBBE = FuncInfo->MBB->end();
  1647. MBBI != MBBE && MBBI->isPHI(); ++MBBI) {
  1648. MachineInstrBuilder PHI(*MF, MBBI);
  1649. // This value for this PHI node is recorded in PHINodesToUpdate.
  1650. for (unsigned pn = 0; ; ++pn) {
  1651. assert(pn != FuncInfo->PHINodesToUpdate.size() &&
  1652. "Didn't find PHI entry!");
  1653. if (FuncInfo->PHINodesToUpdate[pn].first == PHI) {
  1654. PHI.addReg(FuncInfo->PHINodesToUpdate[pn].second).addMBB(ThisBB);
  1655. break;
  1656. }
  1657. }
  1658. }
  1659. }
  1660. }
  1661. }
  1662. SDB->SL->SwitchCases.clear();
  1663. }
  1664. /// Create the scheduler. If a specific scheduler was specified
  1665. /// via the SchedulerRegistry, use it, otherwise select the
  1666. /// one preferred by the target.
  1667. ///
  1668. ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
  1669. return ISHeuristic(this, OptLevel);
  1670. }
  1671. //===----------------------------------------------------------------------===//
  1672. // Helper functions used by the generated instruction selector.
  1673. //===----------------------------------------------------------------------===//
  1674. // Calls to these methods are generated by tblgen.
  1675. /// CheckAndMask - The isel is trying to match something like (and X, 255). If
  1676. /// the dag combiner simplified the 255, we still want to match. RHS is the
  1677. /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
  1678. /// specified in the .td file (e.g. 255).
  1679. bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
  1680. int64_t DesiredMaskS) const {
  1681. const APInt &ActualMask = RHS->getAPIntValue();
  1682. const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
  1683. // If the actual mask exactly matches, success!
  1684. if (ActualMask == DesiredMask)
  1685. return true;
  1686. // If the actual AND mask is allowing unallowed bits, this doesn't match.
  1687. if (!ActualMask.isSubsetOf(DesiredMask))
  1688. return false;
  1689. // Otherwise, the DAG Combiner may have proven that the value coming in is
  1690. // either already zero or is not demanded. Check for known zero input bits.
  1691. APInt NeededMask = DesiredMask & ~ActualMask;
  1692. if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
  1693. return true;
  1694. // TODO: check to see if missing bits are just not demanded.
  1695. // Otherwise, this pattern doesn't match.
  1696. return false;
  1697. }
  1698. /// CheckOrMask - The isel is trying to match something like (or X, 255). If
  1699. /// the dag combiner simplified the 255, we still want to match. RHS is the
  1700. /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
  1701. /// specified in the .td file (e.g. 255).
  1702. bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
  1703. int64_t DesiredMaskS) const {
  1704. const APInt &ActualMask = RHS->getAPIntValue();
  1705. const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
  1706. // If the actual mask exactly matches, success!
  1707. if (ActualMask == DesiredMask)
  1708. return true;
  1709. // If the actual AND mask is allowing unallowed bits, this doesn't match.
  1710. if (!ActualMask.isSubsetOf(DesiredMask))
  1711. return false;
  1712. // Otherwise, the DAG Combiner may have proven that the value coming in is
  1713. // either already zero or is not demanded. Check for known zero input bits.
  1714. APInt NeededMask = DesiredMask & ~ActualMask;
  1715. KnownBits Known = CurDAG->computeKnownBits(LHS);
  1716. // If all the missing bits in the or are already known to be set, match!
  1717. if (NeededMask.isSubsetOf(Known.One))
  1718. return true;
  1719. // TODO: check to see if missing bits are just not demanded.
  1720. // Otherwise, this pattern doesn't match.
  1721. return false;
  1722. }
  1723. /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
  1724. /// by tblgen. Others should not call it.
  1725. void SelectionDAGISel::SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops,
  1726. const SDLoc &DL) {
  1727. std::vector<SDValue> InOps;
  1728. std::swap(InOps, Ops);
  1729. Ops.push_back(InOps[InlineAsm::Op_InputChain]); // 0
  1730. Ops.push_back(InOps[InlineAsm::Op_AsmString]); // 1
  1731. Ops.push_back(InOps[InlineAsm::Op_MDNode]); // 2, !srcloc
  1732. Ops.push_back(InOps[InlineAsm::Op_ExtraInfo]); // 3 (SideEffect, AlignStack)
  1733. unsigned i = InlineAsm::Op_FirstOperand, e = InOps.size();
  1734. if (InOps[e-1].getValueType() == MVT::Glue)
  1735. --e; // Don't process a glue operand if it is here.
  1736. while (i != e) {
  1737. unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
  1738. if (!InlineAsm::isMemKind(Flags) && !InlineAsm::isFuncKind(Flags)) {
  1739. // Just skip over this operand, copying the operands verbatim.
  1740. Ops.insert(Ops.end(), InOps.begin()+i,
  1741. InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
  1742. i += InlineAsm::getNumOperandRegisters(Flags) + 1;
  1743. } else {
  1744. assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
  1745. "Memory operand with multiple values?");
  1746. unsigned TiedToOperand;
  1747. if (InlineAsm::isUseOperandTiedToDef(Flags, TiedToOperand)) {
  1748. // We need the constraint ID from the operand this is tied to.
  1749. unsigned CurOp = InlineAsm::Op_FirstOperand;
  1750. Flags = cast<ConstantSDNode>(InOps[CurOp])->getZExtValue();
  1751. for (; TiedToOperand; --TiedToOperand) {
  1752. CurOp += InlineAsm::getNumOperandRegisters(Flags)+1;
  1753. Flags = cast<ConstantSDNode>(InOps[CurOp])->getZExtValue();
  1754. }
  1755. }
  1756. // Otherwise, this is a memory operand. Ask the target to select it.
  1757. std::vector<SDValue> SelOps;
  1758. unsigned ConstraintID = InlineAsm::getMemoryConstraintID(Flags);
  1759. if (SelectInlineAsmMemoryOperand(InOps[i+1], ConstraintID, SelOps))
  1760. report_fatal_error("Could not match memory address. Inline asm"
  1761. " failure!");
  1762. // Add this to the output node.
  1763. unsigned NewFlags =
  1764. InlineAsm::isMemKind(Flags)
  1765. ? InlineAsm::getFlagWord(InlineAsm::Kind_Mem, SelOps.size())
  1766. : InlineAsm::getFlagWord(InlineAsm::Kind_Func, SelOps.size());
  1767. NewFlags = InlineAsm::getFlagWordForMem(NewFlags, ConstraintID);
  1768. Ops.push_back(CurDAG->getTargetConstant(NewFlags, DL, MVT::i32));
  1769. llvm::append_range(Ops, SelOps);
  1770. i += 2;
  1771. }
  1772. }
  1773. // Add the glue input back if present.
  1774. if (e != InOps.size())
  1775. Ops.push_back(InOps.back());
  1776. }
  1777. /// findGlueUse - Return use of MVT::Glue value produced by the specified
  1778. /// SDNode.
  1779. ///
  1780. static SDNode *findGlueUse(SDNode *N) {
  1781. unsigned FlagResNo = N->getNumValues()-1;
  1782. for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
  1783. SDUse &Use = I.getUse();
  1784. if (Use.getResNo() == FlagResNo)
  1785. return Use.getUser();
  1786. }
  1787. return nullptr;
  1788. }
  1789. /// findNonImmUse - Return true if "Def" is a predecessor of "Root" via a path
  1790. /// beyond "ImmedUse". We may ignore chains as they are checked separately.
  1791. static bool findNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
  1792. bool IgnoreChains) {
  1793. SmallPtrSet<const SDNode *, 16> Visited;
  1794. SmallVector<const SDNode *, 16> WorkList;
  1795. // Only check if we have non-immediate uses of Def.
  1796. if (ImmedUse->isOnlyUserOf(Def))
  1797. return false;
  1798. // We don't care about paths to Def that go through ImmedUse so mark it
  1799. // visited and mark non-def operands as used.
  1800. Visited.insert(ImmedUse);
  1801. for (const SDValue &Op : ImmedUse->op_values()) {
  1802. SDNode *N = Op.getNode();
  1803. // Ignore chain deps (they are validated by
  1804. // HandleMergeInputChains) and immediate uses
  1805. if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def)
  1806. continue;
  1807. if (!Visited.insert(N).second)
  1808. continue;
  1809. WorkList.push_back(N);
  1810. }
  1811. // Initialize worklist to operands of Root.
  1812. if (Root != ImmedUse) {
  1813. for (const SDValue &Op : Root->op_values()) {
  1814. SDNode *N = Op.getNode();
  1815. // Ignore chains (they are validated by HandleMergeInputChains)
  1816. if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def)
  1817. continue;
  1818. if (!Visited.insert(N).second)
  1819. continue;
  1820. WorkList.push_back(N);
  1821. }
  1822. }
  1823. return SDNode::hasPredecessorHelper(Def, Visited, WorkList, 0, true);
  1824. }
  1825. /// IsProfitableToFold - Returns true if it's profitable to fold the specific
  1826. /// operand node N of U during instruction selection that starts at Root.
  1827. bool SelectionDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
  1828. SDNode *Root) const {
  1829. if (OptLevel == CodeGenOpt::None) return false;
  1830. return N.hasOneUse();
  1831. }
  1832. /// IsLegalToFold - Returns true if the specific operand node N of
  1833. /// U can be folded during instruction selection that starts at Root.
  1834. bool SelectionDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
  1835. CodeGenOpt::Level OptLevel,
  1836. bool IgnoreChains) {
  1837. if (OptLevel == CodeGenOpt::None) return false;
  1838. // If Root use can somehow reach N through a path that that doesn't contain
  1839. // U then folding N would create a cycle. e.g. In the following
  1840. // diagram, Root can reach N through X. If N is folded into Root, then
  1841. // X is both a predecessor and a successor of U.
  1842. //
  1843. // [N*] //
  1844. // ^ ^ //
  1845. // / \ //
  1846. // [U*] [X]? //
  1847. // ^ ^ //
  1848. // \ / //
  1849. // \ / //
  1850. // [Root*] //
  1851. //
  1852. // * indicates nodes to be folded together.
  1853. //
  1854. // If Root produces glue, then it gets (even more) interesting. Since it
  1855. // will be "glued" together with its glue use in the scheduler, we need to
  1856. // check if it might reach N.
  1857. //
  1858. // [N*] //
  1859. // ^ ^ //
  1860. // / \ //
  1861. // [U*] [X]? //
  1862. // ^ ^ //
  1863. // \ \ //
  1864. // \ | //
  1865. // [Root*] | //
  1866. // ^ | //
  1867. // f | //
  1868. // | / //
  1869. // [Y] / //
  1870. // ^ / //
  1871. // f / //
  1872. // | / //
  1873. // [GU] //
  1874. //
  1875. // If GU (glue use) indirectly reaches N (the load), and Root folds N
  1876. // (call it Fold), then X is a predecessor of GU and a successor of
  1877. // Fold. But since Fold and GU are glued together, this will create
  1878. // a cycle in the scheduling graph.
  1879. // If the node has glue, walk down the graph to the "lowest" node in the
  1880. // glueged set.
  1881. EVT VT = Root->getValueType(Root->getNumValues()-1);
  1882. while (VT == MVT::Glue) {
  1883. SDNode *GU = findGlueUse(Root);
  1884. if (!GU)
  1885. break;
  1886. Root = GU;
  1887. VT = Root->getValueType(Root->getNumValues()-1);
  1888. // If our query node has a glue result with a use, we've walked up it. If
  1889. // the user (which has already been selected) has a chain or indirectly uses
  1890. // the chain, HandleMergeInputChains will not consider it. Because of
  1891. // this, we cannot ignore chains in this predicate.
  1892. IgnoreChains = false;
  1893. }
  1894. return !findNonImmUse(Root, N.getNode(), U, IgnoreChains);
  1895. }
  1896. void SelectionDAGISel::Select_INLINEASM(SDNode *N) {
  1897. SDLoc DL(N);
  1898. std::vector<SDValue> Ops(N->op_begin(), N->op_end());
  1899. SelectInlineAsmMemoryOperands(Ops, DL);
  1900. const EVT VTs[] = {MVT::Other, MVT::Glue};
  1901. SDValue New = CurDAG->getNode(N->getOpcode(), DL, VTs, Ops);
  1902. New->setNodeId(-1);
  1903. ReplaceUses(N, New.getNode());
  1904. CurDAG->RemoveDeadNode(N);
  1905. }
  1906. void SelectionDAGISel::Select_READ_REGISTER(SDNode *Op) {
  1907. SDLoc dl(Op);
  1908. MDNodeSDNode *MD = cast<MDNodeSDNode>(Op->getOperand(1));
  1909. const MDString *RegStr = cast<MDString>(MD->getMD()->getOperand(0));
  1910. EVT VT = Op->getValueType(0);
  1911. LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT();
  1912. Register Reg =
  1913. TLI->getRegisterByName(RegStr->getString().data(), Ty,
  1914. CurDAG->getMachineFunction());
  1915. SDValue New = CurDAG->getCopyFromReg(
  1916. Op->getOperand(0), dl, Reg, Op->getValueType(0));
  1917. New->setNodeId(-1);
  1918. ReplaceUses(Op, New.getNode());
  1919. CurDAG->RemoveDeadNode(Op);
  1920. }
  1921. void SelectionDAGISel::Select_WRITE_REGISTER(SDNode *Op) {
  1922. SDLoc dl(Op);
  1923. MDNodeSDNode *MD = cast<MDNodeSDNode>(Op->getOperand(1));
  1924. const MDString *RegStr = cast<MDString>(MD->getMD()->getOperand(0));
  1925. EVT VT = Op->getOperand(2).getValueType();
  1926. LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT();
  1927. Register Reg = TLI->getRegisterByName(RegStr->getString().data(), Ty,
  1928. CurDAG->getMachineFunction());
  1929. SDValue New = CurDAG->getCopyToReg(
  1930. Op->getOperand(0), dl, Reg, Op->getOperand(2));
  1931. New->setNodeId(-1);
  1932. ReplaceUses(Op, New.getNode());
  1933. CurDAG->RemoveDeadNode(Op);
  1934. }
  1935. void SelectionDAGISel::Select_UNDEF(SDNode *N) {
  1936. CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
  1937. }
  1938. void SelectionDAGISel::Select_FREEZE(SDNode *N) {
  1939. // TODO: We don't have FREEZE pseudo-instruction in MachineInstr-level now.
  1940. // If FREEZE instruction is added later, the code below must be changed as
  1941. // well.
  1942. CurDAG->SelectNodeTo(N, TargetOpcode::COPY, N->getValueType(0),
  1943. N->getOperand(0));
  1944. }
  1945. void SelectionDAGISel::Select_ARITH_FENCE(SDNode *N) {
  1946. CurDAG->SelectNodeTo(N, TargetOpcode::ARITH_FENCE, N->getValueType(0),
  1947. N->getOperand(0));
  1948. }
  1949. void SelectionDAGISel::Select_MEMBARRIER(SDNode *N) {
  1950. CurDAG->SelectNodeTo(N, TargetOpcode::MEMBARRIER, N->getValueType(0),
  1951. N->getOperand(0));
  1952. }
  1953. void SelectionDAGISel::pushStackMapLiveVariable(SmallVectorImpl<SDValue> &Ops,
  1954. SDValue OpVal, SDLoc DL) {
  1955. SDNode *OpNode = OpVal.getNode();
  1956. // FrameIndex nodes should have been directly emitted to TargetFrameIndex
  1957. // nodes at DAG-construction time.
  1958. assert(OpNode->getOpcode() != ISD::FrameIndex);
  1959. if (OpNode->getOpcode() == ISD::Constant) {
  1960. Ops.push_back(
  1961. CurDAG->getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
  1962. Ops.push_back(
  1963. CurDAG->getTargetConstant(cast<ConstantSDNode>(OpNode)->getZExtValue(),
  1964. DL, OpVal.getValueType()));
  1965. } else {
  1966. Ops.push_back(OpVal);
  1967. }
  1968. }
  1969. void SelectionDAGISel::Select_STACKMAP(SDNode *N) {
  1970. SmallVector<SDValue, 32> Ops;
  1971. auto *It = N->op_begin();
  1972. SDLoc DL(N);
  1973. // Stash the chain and glue operands so we can move them to the end.
  1974. SDValue Chain = *It++;
  1975. SDValue InFlag = *It++;
  1976. // <id> operand.
  1977. SDValue ID = *It++;
  1978. assert(ID.getValueType() == MVT::i64);
  1979. Ops.push_back(ID);
  1980. // <numShadowBytes> operand.
  1981. SDValue Shad = *It++;
  1982. assert(Shad.getValueType() == MVT::i32);
  1983. Ops.push_back(Shad);
  1984. // Live variable operands.
  1985. for (; It != N->op_end(); It++)
  1986. pushStackMapLiveVariable(Ops, *It, DL);
  1987. Ops.push_back(Chain);
  1988. Ops.push_back(InFlag);
  1989. SDVTList NodeTys = CurDAG->getVTList(MVT::Other, MVT::Glue);
  1990. CurDAG->SelectNodeTo(N, TargetOpcode::STACKMAP, NodeTys, Ops);
  1991. }
  1992. void SelectionDAGISel::Select_PATCHPOINT(SDNode *N) {
  1993. SmallVector<SDValue, 32> Ops;
  1994. auto *It = N->op_begin();
  1995. SDLoc DL(N);
  1996. // Cache arguments that will be moved to the end in the target node.
  1997. SDValue Chain = *It++;
  1998. std::optional<SDValue> Glue;
  1999. if (It->getValueType() == MVT::Glue)
  2000. Glue = *It++;
  2001. SDValue RegMask = *It++;
  2002. // <id> operand.
  2003. SDValue ID = *It++;
  2004. assert(ID.getValueType() == MVT::i64);
  2005. Ops.push_back(ID);
  2006. // <numShadowBytes> operand.
  2007. SDValue Shad = *It++;
  2008. assert(Shad.getValueType() == MVT::i32);
  2009. Ops.push_back(Shad);
  2010. // Add the callee.
  2011. Ops.push_back(*It++);
  2012. // Add <numArgs>.
  2013. SDValue NumArgs = *It++;
  2014. assert(NumArgs.getValueType() == MVT::i32);
  2015. Ops.push_back(NumArgs);
  2016. // Calling convention.
  2017. Ops.push_back(*It++);
  2018. // Push the args for the call.
  2019. for (uint64_t I = cast<ConstantSDNode>(NumArgs)->getZExtValue(); I != 0; I--)
  2020. Ops.push_back(*It++);
  2021. // Now push the live variables.
  2022. for (; It != N->op_end(); It++)
  2023. pushStackMapLiveVariable(Ops, *It, DL);
  2024. // Finally, the regmask, chain and (if present) glue are moved to the end.
  2025. Ops.push_back(RegMask);
  2026. Ops.push_back(Chain);
  2027. if (Glue.has_value())
  2028. Ops.push_back(*Glue);
  2029. SDVTList NodeTys = N->getVTList();
  2030. CurDAG->SelectNodeTo(N, TargetOpcode::PATCHPOINT, NodeTys, Ops);
  2031. }
  2032. /// GetVBR - decode a vbr encoding whose top bit is set.
  2033. LLVM_ATTRIBUTE_ALWAYS_INLINE static uint64_t
  2034. GetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) {
  2035. assert(Val >= 128 && "Not a VBR");
  2036. Val &= 127; // Remove first vbr bit.
  2037. unsigned Shift = 7;
  2038. uint64_t NextBits;
  2039. do {
  2040. NextBits = MatcherTable[Idx++];
  2041. Val |= (NextBits&127) << Shift;
  2042. Shift += 7;
  2043. } while (NextBits & 128);
  2044. return Val;
  2045. }
  2046. /// When a match is complete, this method updates uses of interior chain results
  2047. /// to use the new results.
  2048. void SelectionDAGISel::UpdateChains(
  2049. SDNode *NodeToMatch, SDValue InputChain,
  2050. SmallVectorImpl<SDNode *> &ChainNodesMatched, bool isMorphNodeTo) {
  2051. SmallVector<SDNode*, 4> NowDeadNodes;
  2052. // Now that all the normal results are replaced, we replace the chain and
  2053. // glue results if present.
  2054. if (!ChainNodesMatched.empty()) {
  2055. assert(InputChain.getNode() &&
  2056. "Matched input chains but didn't produce a chain");
  2057. // Loop over all of the nodes we matched that produced a chain result.
  2058. // Replace all the chain results with the final chain we ended up with.
  2059. for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
  2060. SDNode *ChainNode = ChainNodesMatched[i];
  2061. // If ChainNode is null, it's because we replaced it on a previous
  2062. // iteration and we cleared it out of the map. Just skip it.
  2063. if (!ChainNode)
  2064. continue;
  2065. assert(ChainNode->getOpcode() != ISD::DELETED_NODE &&
  2066. "Deleted node left in chain");
  2067. // Don't replace the results of the root node if we're doing a
  2068. // MorphNodeTo.
  2069. if (ChainNode == NodeToMatch && isMorphNodeTo)
  2070. continue;
  2071. SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1);
  2072. if (ChainVal.getValueType() == MVT::Glue)
  2073. ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2);
  2074. assert(ChainVal.getValueType() == MVT::Other && "Not a chain?");
  2075. SelectionDAG::DAGNodeDeletedListener NDL(
  2076. *CurDAG, [&](SDNode *N, SDNode *E) {
  2077. std::replace(ChainNodesMatched.begin(), ChainNodesMatched.end(), N,
  2078. static_cast<SDNode *>(nullptr));
  2079. });
  2080. if (ChainNode->getOpcode() != ISD::TokenFactor)
  2081. ReplaceUses(ChainVal, InputChain);
  2082. // If the node became dead and we haven't already seen it, delete it.
  2083. if (ChainNode != NodeToMatch && ChainNode->use_empty() &&
  2084. !llvm::is_contained(NowDeadNodes, ChainNode))
  2085. NowDeadNodes.push_back(ChainNode);
  2086. }
  2087. }
  2088. if (!NowDeadNodes.empty())
  2089. CurDAG->RemoveDeadNodes(NowDeadNodes);
  2090. LLVM_DEBUG(dbgs() << "ISEL: Match complete!\n");
  2091. }
  2092. /// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains
  2093. /// operation for when the pattern matched at least one node with a chains. The
  2094. /// input vector contains a list of all of the chained nodes that we match. We
  2095. /// must determine if this is a valid thing to cover (i.e. matching it won't
  2096. /// induce cycles in the DAG) and if so, creating a TokenFactor node. that will
  2097. /// be used as the input node chain for the generated nodes.
  2098. static SDValue
  2099. HandleMergeInputChains(SmallVectorImpl<SDNode*> &ChainNodesMatched,
  2100. SelectionDAG *CurDAG) {
  2101. SmallPtrSet<const SDNode *, 16> Visited;
  2102. SmallVector<const SDNode *, 8> Worklist;
  2103. SmallVector<SDValue, 3> InputChains;
  2104. unsigned int Max = 8192;
  2105. // Quick exit on trivial merge.
  2106. if (ChainNodesMatched.size() == 1)
  2107. return ChainNodesMatched[0]->getOperand(0);
  2108. // Add chains that aren't already added (internal). Peek through
  2109. // token factors.
  2110. std::function<void(const SDValue)> AddChains = [&](const SDValue V) {
  2111. if (V.getValueType() != MVT::Other)
  2112. return;
  2113. if (V->getOpcode() == ISD::EntryToken)
  2114. return;
  2115. if (!Visited.insert(V.getNode()).second)
  2116. return;
  2117. if (V->getOpcode() == ISD::TokenFactor) {
  2118. for (const SDValue &Op : V->op_values())
  2119. AddChains(Op);
  2120. } else
  2121. InputChains.push_back(V);
  2122. };
  2123. for (auto *N : ChainNodesMatched) {
  2124. Worklist.push_back(N);
  2125. Visited.insert(N);
  2126. }
  2127. while (!Worklist.empty())
  2128. AddChains(Worklist.pop_back_val()->getOperand(0));
  2129. // Skip the search if there are no chain dependencies.
  2130. if (InputChains.size() == 0)
  2131. return CurDAG->getEntryNode();
  2132. // If one of these chains is a successor of input, we must have a
  2133. // node that is both the predecessor and successor of the
  2134. // to-be-merged nodes. Fail.
  2135. Visited.clear();
  2136. for (SDValue V : InputChains)
  2137. Worklist.push_back(V.getNode());
  2138. for (auto *N : ChainNodesMatched)
  2139. if (SDNode::hasPredecessorHelper(N, Visited, Worklist, Max, true))
  2140. return SDValue();
  2141. // Return merged chain.
  2142. if (InputChains.size() == 1)
  2143. return InputChains[0];
  2144. return CurDAG->getNode(ISD::TokenFactor, SDLoc(ChainNodesMatched[0]),
  2145. MVT::Other, InputChains);
  2146. }
  2147. /// MorphNode - Handle morphing a node in place for the selector.
  2148. SDNode *SelectionDAGISel::
  2149. MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
  2150. ArrayRef<SDValue> Ops, unsigned EmitNodeInfo) {
  2151. // It is possible we're using MorphNodeTo to replace a node with no
  2152. // normal results with one that has a normal result (or we could be
  2153. // adding a chain) and the input could have glue and chains as well.
  2154. // In this case we need to shift the operands down.
  2155. // FIXME: This is a horrible hack and broken in obscure cases, no worse
  2156. // than the old isel though.
  2157. int OldGlueResultNo = -1, OldChainResultNo = -1;
  2158. unsigned NTMNumResults = Node->getNumValues();
  2159. if (Node->getValueType(NTMNumResults-1) == MVT::Glue) {
  2160. OldGlueResultNo = NTMNumResults-1;
  2161. if (NTMNumResults != 1 &&
  2162. Node->getValueType(NTMNumResults-2) == MVT::Other)
  2163. OldChainResultNo = NTMNumResults-2;
  2164. } else if (Node->getValueType(NTMNumResults-1) == MVT::Other)
  2165. OldChainResultNo = NTMNumResults-1;
  2166. // Call the underlying SelectionDAG routine to do the transmogrification. Note
  2167. // that this deletes operands of the old node that become dead.
  2168. SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops);
  2169. // MorphNodeTo can operate in two ways: if an existing node with the
  2170. // specified operands exists, it can just return it. Otherwise, it
  2171. // updates the node in place to have the requested operands.
  2172. if (Res == Node) {
  2173. // If we updated the node in place, reset the node ID. To the isel,
  2174. // this should be just like a newly allocated machine node.
  2175. Res->setNodeId(-1);
  2176. }
  2177. unsigned ResNumResults = Res->getNumValues();
  2178. // Move the glue if needed.
  2179. if ((EmitNodeInfo & OPFL_GlueOutput) && OldGlueResultNo != -1 &&
  2180. (unsigned)OldGlueResultNo != ResNumResults-1)
  2181. ReplaceUses(SDValue(Node, OldGlueResultNo),
  2182. SDValue(Res, ResNumResults - 1));
  2183. if ((EmitNodeInfo & OPFL_GlueOutput) != 0)
  2184. --ResNumResults;
  2185. // Move the chain reference if needed.
  2186. if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 &&
  2187. (unsigned)OldChainResultNo != ResNumResults-1)
  2188. ReplaceUses(SDValue(Node, OldChainResultNo),
  2189. SDValue(Res, ResNumResults - 1));
  2190. // Otherwise, no replacement happened because the node already exists. Replace
  2191. // Uses of the old node with the new one.
  2192. if (Res != Node) {
  2193. ReplaceNode(Node, Res);
  2194. } else {
  2195. EnforceNodeIdInvariant(Res);
  2196. }
  2197. return Res;
  2198. }
  2199. /// CheckSame - Implements OP_CheckSame.
  2200. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2201. CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N,
  2202. const SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes) {
  2203. // Accept if it is exactly the same as a previously recorded node.
  2204. unsigned RecNo = MatcherTable[MatcherIndex++];
  2205. assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
  2206. return N == RecordedNodes[RecNo].first;
  2207. }
  2208. /// CheckChildSame - Implements OP_CheckChildXSame.
  2209. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool CheckChildSame(
  2210. const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N,
  2211. const SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes,
  2212. unsigned ChildNo) {
  2213. if (ChildNo >= N.getNumOperands())
  2214. return false; // Match fails if out of range child #.
  2215. return ::CheckSame(MatcherTable, MatcherIndex, N.getOperand(ChildNo),
  2216. RecordedNodes);
  2217. }
  2218. /// CheckPatternPredicate - Implements OP_CheckPatternPredicate.
  2219. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2220. CheckPatternPredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2221. const SelectionDAGISel &SDISel) {
  2222. return SDISel.CheckPatternPredicate(MatcherTable[MatcherIndex++]);
  2223. }
  2224. /// CheckNodePredicate - Implements OP_CheckNodePredicate.
  2225. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2226. CheckNodePredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2227. const SelectionDAGISel &SDISel, SDNode *N) {
  2228. return SDISel.CheckNodePredicate(N, MatcherTable[MatcherIndex++]);
  2229. }
  2230. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2231. CheckOpcode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2232. SDNode *N) {
  2233. uint16_t Opc = MatcherTable[MatcherIndex++];
  2234. Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
  2235. return N->getOpcode() == Opc;
  2236. }
  2237. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2238. CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N,
  2239. const TargetLowering *TLI, const DataLayout &DL) {
  2240. MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  2241. if (N.getValueType() == VT) return true;
  2242. // Handle the case when VT is iPTR.
  2243. return VT == MVT::iPTR && N.getValueType() == TLI->getPointerTy(DL);
  2244. }
  2245. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2246. CheckChildType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2247. SDValue N, const TargetLowering *TLI, const DataLayout &DL,
  2248. unsigned ChildNo) {
  2249. if (ChildNo >= N.getNumOperands())
  2250. return false; // Match fails if out of range child #.
  2251. return ::CheckType(MatcherTable, MatcherIndex, N.getOperand(ChildNo), TLI,
  2252. DL);
  2253. }
  2254. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2255. CheckCondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2256. SDValue N) {
  2257. return cast<CondCodeSDNode>(N)->get() ==
  2258. (ISD::CondCode)MatcherTable[MatcherIndex++];
  2259. }
  2260. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2261. CheckChild2CondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2262. SDValue N) {
  2263. if (2 >= N.getNumOperands())
  2264. return false;
  2265. return ::CheckCondCode(MatcherTable, MatcherIndex, N.getOperand(2));
  2266. }
  2267. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2268. CheckValueType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2269. SDValue N, const TargetLowering *TLI, const DataLayout &DL) {
  2270. MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  2271. if (cast<VTSDNode>(N)->getVT() == VT)
  2272. return true;
  2273. // Handle the case when VT is iPTR.
  2274. return VT == MVT::iPTR && cast<VTSDNode>(N)->getVT() == TLI->getPointerTy(DL);
  2275. }
  2276. // Bit 0 stores the sign of the immediate. The upper bits contain the magnitude
  2277. // shifted left by 1.
  2278. static uint64_t decodeSignRotatedValue(uint64_t V) {
  2279. if ((V & 1) == 0)
  2280. return V >> 1;
  2281. if (V != 1)
  2282. return -(V >> 1);
  2283. // There is no such thing as -0 with integers. "-0" really means MININT.
  2284. return 1ULL << 63;
  2285. }
  2286. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2287. CheckInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2288. SDValue N) {
  2289. int64_t Val = MatcherTable[MatcherIndex++];
  2290. if (Val & 128)
  2291. Val = GetVBR(Val, MatcherTable, MatcherIndex);
  2292. Val = decodeSignRotatedValue(Val);
  2293. ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
  2294. return C && C->getSExtValue() == Val;
  2295. }
  2296. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2297. CheckChildInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2298. SDValue N, unsigned ChildNo) {
  2299. if (ChildNo >= N.getNumOperands())
  2300. return false; // Match fails if out of range child #.
  2301. return ::CheckInteger(MatcherTable, MatcherIndex, N.getOperand(ChildNo));
  2302. }
  2303. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2304. CheckAndImm(const unsigned char *MatcherTable, unsigned &MatcherIndex,
  2305. SDValue N, const SelectionDAGISel &SDISel) {
  2306. int64_t Val = MatcherTable[MatcherIndex++];
  2307. if (Val & 128)
  2308. Val = GetVBR(Val, MatcherTable, MatcherIndex);
  2309. if (N->getOpcode() != ISD::AND) return false;
  2310. ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
  2311. return C && SDISel.CheckAndMask(N.getOperand(0), C, Val);
  2312. }
  2313. LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
  2314. CheckOrImm(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N,
  2315. const SelectionDAGISel &SDISel) {
  2316. int64_t Val = MatcherTable[MatcherIndex++];
  2317. if (Val & 128)
  2318. Val = GetVBR(Val, MatcherTable, MatcherIndex);
  2319. if (N->getOpcode() != ISD::OR) return false;
  2320. ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
  2321. return C && SDISel.CheckOrMask(N.getOperand(0), C, Val);
  2322. }
  2323. /// IsPredicateKnownToFail - If we know how and can do so without pushing a
  2324. /// scope, evaluate the current node. If the current predicate is known to
  2325. /// fail, set Result=true and return anything. If the current predicate is
  2326. /// known to pass, set Result=false and return the MatcherIndex to continue
  2327. /// with. If the current predicate is unknown, set Result=false and return the
  2328. /// MatcherIndex to continue with.
  2329. static unsigned IsPredicateKnownToFail(const unsigned char *Table,
  2330. unsigned Index, SDValue N,
  2331. bool &Result,
  2332. const SelectionDAGISel &SDISel,
  2333. SmallVectorImpl<std::pair<SDValue, SDNode*>> &RecordedNodes) {
  2334. switch (Table[Index++]) {
  2335. default:
  2336. Result = false;
  2337. return Index-1; // Could not evaluate this predicate.
  2338. case SelectionDAGISel::OPC_CheckSame:
  2339. Result = !::CheckSame(Table, Index, N, RecordedNodes);
  2340. return Index;
  2341. case SelectionDAGISel::OPC_CheckChild0Same:
  2342. case SelectionDAGISel::OPC_CheckChild1Same:
  2343. case SelectionDAGISel::OPC_CheckChild2Same:
  2344. case SelectionDAGISel::OPC_CheckChild3Same:
  2345. Result = !::CheckChildSame(Table, Index, N, RecordedNodes,
  2346. Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Same);
  2347. return Index;
  2348. case SelectionDAGISel::OPC_CheckPatternPredicate:
  2349. Result = !::CheckPatternPredicate(Table, Index, SDISel);
  2350. return Index;
  2351. case SelectionDAGISel::OPC_CheckPredicate:
  2352. Result = !::CheckNodePredicate(Table, Index, SDISel, N.getNode());
  2353. return Index;
  2354. case SelectionDAGISel::OPC_CheckOpcode:
  2355. Result = !::CheckOpcode(Table, Index, N.getNode());
  2356. return Index;
  2357. case SelectionDAGISel::OPC_CheckType:
  2358. Result = !::CheckType(Table, Index, N, SDISel.TLI,
  2359. SDISel.CurDAG->getDataLayout());
  2360. return Index;
  2361. case SelectionDAGISel::OPC_CheckTypeRes: {
  2362. unsigned Res = Table[Index++];
  2363. Result = !::CheckType(Table, Index, N.getValue(Res), SDISel.TLI,
  2364. SDISel.CurDAG->getDataLayout());
  2365. return Index;
  2366. }
  2367. case SelectionDAGISel::OPC_CheckChild0Type:
  2368. case SelectionDAGISel::OPC_CheckChild1Type:
  2369. case SelectionDAGISel::OPC_CheckChild2Type:
  2370. case SelectionDAGISel::OPC_CheckChild3Type:
  2371. case SelectionDAGISel::OPC_CheckChild4Type:
  2372. case SelectionDAGISel::OPC_CheckChild5Type:
  2373. case SelectionDAGISel::OPC_CheckChild6Type:
  2374. case SelectionDAGISel::OPC_CheckChild7Type:
  2375. Result = !::CheckChildType(
  2376. Table, Index, N, SDISel.TLI, SDISel.CurDAG->getDataLayout(),
  2377. Table[Index - 1] - SelectionDAGISel::OPC_CheckChild0Type);
  2378. return Index;
  2379. case SelectionDAGISel::OPC_CheckCondCode:
  2380. Result = !::CheckCondCode(Table, Index, N);
  2381. return Index;
  2382. case SelectionDAGISel::OPC_CheckChild2CondCode:
  2383. Result = !::CheckChild2CondCode(Table, Index, N);
  2384. return Index;
  2385. case SelectionDAGISel::OPC_CheckValueType:
  2386. Result = !::CheckValueType(Table, Index, N, SDISel.TLI,
  2387. SDISel.CurDAG->getDataLayout());
  2388. return Index;
  2389. case SelectionDAGISel::OPC_CheckInteger:
  2390. Result = !::CheckInteger(Table, Index, N);
  2391. return Index;
  2392. case SelectionDAGISel::OPC_CheckChild0Integer:
  2393. case SelectionDAGISel::OPC_CheckChild1Integer:
  2394. case SelectionDAGISel::OPC_CheckChild2Integer:
  2395. case SelectionDAGISel::OPC_CheckChild3Integer:
  2396. case SelectionDAGISel::OPC_CheckChild4Integer:
  2397. Result = !::CheckChildInteger(Table, Index, N,
  2398. Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Integer);
  2399. return Index;
  2400. case SelectionDAGISel::OPC_CheckAndImm:
  2401. Result = !::CheckAndImm(Table, Index, N, SDISel);
  2402. return Index;
  2403. case SelectionDAGISel::OPC_CheckOrImm:
  2404. Result = !::CheckOrImm(Table, Index, N, SDISel);
  2405. return Index;
  2406. }
  2407. }
  2408. namespace {
  2409. struct MatchScope {
  2410. /// FailIndex - If this match fails, this is the index to continue with.
  2411. unsigned FailIndex;
  2412. /// NodeStack - The node stack when the scope was formed.
  2413. SmallVector<SDValue, 4> NodeStack;
  2414. /// NumRecordedNodes - The number of recorded nodes when the scope was formed.
  2415. unsigned NumRecordedNodes;
  2416. /// NumMatchedMemRefs - The number of matched memref entries.
  2417. unsigned NumMatchedMemRefs;
  2418. /// InputChain/InputGlue - The current chain/glue
  2419. SDValue InputChain, InputGlue;
  2420. /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty.
  2421. bool HasChainNodesMatched;
  2422. };
  2423. /// \A DAG update listener to keep the matching state
  2424. /// (i.e. RecordedNodes and MatchScope) uptodate if the target is allowed to
  2425. /// change the DAG while matching. X86 addressing mode matcher is an example
  2426. /// for this.
  2427. class MatchStateUpdater : public SelectionDAG::DAGUpdateListener
  2428. {
  2429. SDNode **NodeToMatch;
  2430. SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes;
  2431. SmallVectorImpl<MatchScope> &MatchScopes;
  2432. public:
  2433. MatchStateUpdater(SelectionDAG &DAG, SDNode **NodeToMatch,
  2434. SmallVectorImpl<std::pair<SDValue, SDNode *>> &RN,
  2435. SmallVectorImpl<MatchScope> &MS)
  2436. : SelectionDAG::DAGUpdateListener(DAG), NodeToMatch(NodeToMatch),
  2437. RecordedNodes(RN), MatchScopes(MS) {}
  2438. void NodeDeleted(SDNode *N, SDNode *E) override {
  2439. // Some early-returns here to avoid the search if we deleted the node or
  2440. // if the update comes from MorphNodeTo (MorphNodeTo is the last thing we
  2441. // do, so it's unnecessary to update matching state at that point).
  2442. // Neither of these can occur currently because we only install this
  2443. // update listener during matching a complex patterns.
  2444. if (!E || E->isMachineOpcode())
  2445. return;
  2446. // Check if NodeToMatch was updated.
  2447. if (N == *NodeToMatch)
  2448. *NodeToMatch = E;
  2449. // Performing linear search here does not matter because we almost never
  2450. // run this code. You'd have to have a CSE during complex pattern
  2451. // matching.
  2452. for (auto &I : RecordedNodes)
  2453. if (I.first.getNode() == N)
  2454. I.first.setNode(E);
  2455. for (auto &I : MatchScopes)
  2456. for (auto &J : I.NodeStack)
  2457. if (J.getNode() == N)
  2458. J.setNode(E);
  2459. }
  2460. };
  2461. } // end anonymous namespace
  2462. void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
  2463. const unsigned char *MatcherTable,
  2464. unsigned TableSize) {
  2465. // FIXME: Should these even be selected? Handle these cases in the caller?
  2466. switch (NodeToMatch->getOpcode()) {
  2467. default:
  2468. break;
  2469. case ISD::EntryToken: // These nodes remain the same.
  2470. case ISD::BasicBlock:
  2471. case ISD::Register:
  2472. case ISD::RegisterMask:
  2473. case ISD::HANDLENODE:
  2474. case ISD::MDNODE_SDNODE:
  2475. case ISD::TargetConstant:
  2476. case ISD::TargetConstantFP:
  2477. case ISD::TargetConstantPool:
  2478. case ISD::TargetFrameIndex:
  2479. case ISD::TargetExternalSymbol:
  2480. case ISD::MCSymbol:
  2481. case ISD::TargetBlockAddress:
  2482. case ISD::TargetJumpTable:
  2483. case ISD::TargetGlobalTLSAddress:
  2484. case ISD::TargetGlobalAddress:
  2485. case ISD::TokenFactor:
  2486. case ISD::CopyFromReg:
  2487. case ISD::CopyToReg:
  2488. case ISD::EH_LABEL:
  2489. case ISD::ANNOTATION_LABEL:
  2490. case ISD::LIFETIME_START:
  2491. case ISD::LIFETIME_END:
  2492. case ISD::PSEUDO_PROBE:
  2493. NodeToMatch->setNodeId(-1); // Mark selected.
  2494. return;
  2495. case ISD::AssertSext:
  2496. case ISD::AssertZext:
  2497. case ISD::AssertAlign:
  2498. ReplaceUses(SDValue(NodeToMatch, 0), NodeToMatch->getOperand(0));
  2499. CurDAG->RemoveDeadNode(NodeToMatch);
  2500. return;
  2501. case ISD::INLINEASM:
  2502. case ISD::INLINEASM_BR:
  2503. Select_INLINEASM(NodeToMatch);
  2504. return;
  2505. case ISD::READ_REGISTER:
  2506. Select_READ_REGISTER(NodeToMatch);
  2507. return;
  2508. case ISD::WRITE_REGISTER:
  2509. Select_WRITE_REGISTER(NodeToMatch);
  2510. return;
  2511. case ISD::UNDEF:
  2512. Select_UNDEF(NodeToMatch);
  2513. return;
  2514. case ISD::FREEZE:
  2515. Select_FREEZE(NodeToMatch);
  2516. return;
  2517. case ISD::ARITH_FENCE:
  2518. Select_ARITH_FENCE(NodeToMatch);
  2519. return;
  2520. case ISD::MEMBARRIER:
  2521. Select_MEMBARRIER(NodeToMatch);
  2522. return;
  2523. case ISD::STACKMAP:
  2524. Select_STACKMAP(NodeToMatch);
  2525. return;
  2526. case ISD::PATCHPOINT:
  2527. Select_PATCHPOINT(NodeToMatch);
  2528. return;
  2529. }
  2530. assert(!NodeToMatch->isMachineOpcode() && "Node already selected!");
  2531. // Set up the node stack with NodeToMatch as the only node on the stack.
  2532. SmallVector<SDValue, 8> NodeStack;
  2533. SDValue N = SDValue(NodeToMatch, 0);
  2534. NodeStack.push_back(N);
  2535. // MatchScopes - Scopes used when matching, if a match failure happens, this
  2536. // indicates where to continue checking.
  2537. SmallVector<MatchScope, 8> MatchScopes;
  2538. // RecordedNodes - This is the set of nodes that have been recorded by the
  2539. // state machine. The second value is the parent of the node, or null if the
  2540. // root is recorded.
  2541. SmallVector<std::pair<SDValue, SDNode*>, 8> RecordedNodes;
  2542. // MatchedMemRefs - This is the set of MemRef's we've seen in the input
  2543. // pattern.
  2544. SmallVector<MachineMemOperand*, 2> MatchedMemRefs;
  2545. // These are the current input chain and glue for use when generating nodes.
  2546. // Various Emit operations change these. For example, emitting a copytoreg
  2547. // uses and updates these.
  2548. SDValue InputChain, InputGlue;
  2549. // ChainNodesMatched - If a pattern matches nodes that have input/output
  2550. // chains, the OPC_EmitMergeInputChains operation is emitted which indicates
  2551. // which ones they are. The result is captured into this list so that we can
  2552. // update the chain results when the pattern is complete.
  2553. SmallVector<SDNode*, 3> ChainNodesMatched;
  2554. LLVM_DEBUG(dbgs() << "ISEL: Starting pattern match\n");
  2555. // Determine where to start the interpreter. Normally we start at opcode #0,
  2556. // but if the state machine starts with an OPC_SwitchOpcode, then we
  2557. // accelerate the first lookup (which is guaranteed to be hot) with the
  2558. // OpcodeOffset table.
  2559. unsigned MatcherIndex = 0;
  2560. if (!OpcodeOffset.empty()) {
  2561. // Already computed the OpcodeOffset table, just index into it.
  2562. if (N.getOpcode() < OpcodeOffset.size())
  2563. MatcherIndex = OpcodeOffset[N.getOpcode()];
  2564. LLVM_DEBUG(dbgs() << " Initial Opcode index to " << MatcherIndex << "\n");
  2565. } else if (MatcherTable[0] == OPC_SwitchOpcode) {
  2566. // Otherwise, the table isn't computed, but the state machine does start
  2567. // with an OPC_SwitchOpcode instruction. Populate the table now, since this
  2568. // is the first time we're selecting an instruction.
  2569. unsigned Idx = 1;
  2570. while (true) {
  2571. // Get the size of this case.
  2572. unsigned CaseSize = MatcherTable[Idx++];
  2573. if (CaseSize & 128)
  2574. CaseSize = GetVBR(CaseSize, MatcherTable, Idx);
  2575. if (CaseSize == 0) break;
  2576. // Get the opcode, add the index to the table.
  2577. uint16_t Opc = MatcherTable[Idx++];
  2578. Opc |= (unsigned short)MatcherTable[Idx++] << 8;
  2579. if (Opc >= OpcodeOffset.size())
  2580. OpcodeOffset.resize((Opc+1)*2);
  2581. OpcodeOffset[Opc] = Idx;
  2582. Idx += CaseSize;
  2583. }
  2584. // Okay, do the lookup for the first opcode.
  2585. if (N.getOpcode() < OpcodeOffset.size())
  2586. MatcherIndex = OpcodeOffset[N.getOpcode()];
  2587. }
  2588. while (true) {
  2589. assert(MatcherIndex < TableSize && "Invalid index");
  2590. #ifndef NDEBUG
  2591. unsigned CurrentOpcodeIndex = MatcherIndex;
  2592. #endif
  2593. BuiltinOpcodes Opcode = (BuiltinOpcodes)MatcherTable[MatcherIndex++];
  2594. switch (Opcode) {
  2595. case OPC_Scope: {
  2596. // Okay, the semantics of this operation are that we should push a scope
  2597. // then evaluate the first child. However, pushing a scope only to have
  2598. // the first check fail (which then pops it) is inefficient. If we can
  2599. // determine immediately that the first check (or first several) will
  2600. // immediately fail, don't even bother pushing a scope for them.
  2601. unsigned FailIndex;
  2602. while (true) {
  2603. unsigned NumToSkip = MatcherTable[MatcherIndex++];
  2604. if (NumToSkip & 128)
  2605. NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
  2606. // Found the end of the scope with no match.
  2607. if (NumToSkip == 0) {
  2608. FailIndex = 0;
  2609. break;
  2610. }
  2611. FailIndex = MatcherIndex+NumToSkip;
  2612. unsigned MatcherIndexOfPredicate = MatcherIndex;
  2613. (void)MatcherIndexOfPredicate; // silence warning.
  2614. // If we can't evaluate this predicate without pushing a scope (e.g. if
  2615. // it is a 'MoveParent') or if the predicate succeeds on this node, we
  2616. // push the scope and evaluate the full predicate chain.
  2617. bool Result;
  2618. MatcherIndex = IsPredicateKnownToFail(MatcherTable, MatcherIndex, N,
  2619. Result, *this, RecordedNodes);
  2620. if (!Result)
  2621. break;
  2622. LLVM_DEBUG(
  2623. dbgs() << " Skipped scope entry (due to false predicate) at "
  2624. << "index " << MatcherIndexOfPredicate << ", continuing at "
  2625. << FailIndex << "\n");
  2626. ++NumDAGIselRetries;
  2627. // Otherwise, we know that this case of the Scope is guaranteed to fail,
  2628. // move to the next case.
  2629. MatcherIndex = FailIndex;
  2630. }
  2631. // If the whole scope failed to match, bail.
  2632. if (FailIndex == 0) break;
  2633. // Push a MatchScope which indicates where to go if the first child fails
  2634. // to match.
  2635. MatchScope NewEntry;
  2636. NewEntry.FailIndex = FailIndex;
  2637. NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end());
  2638. NewEntry.NumRecordedNodes = RecordedNodes.size();
  2639. NewEntry.NumMatchedMemRefs = MatchedMemRefs.size();
  2640. NewEntry.InputChain = InputChain;
  2641. NewEntry.InputGlue = InputGlue;
  2642. NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty();
  2643. MatchScopes.push_back(NewEntry);
  2644. continue;
  2645. }
  2646. case OPC_RecordNode: {
  2647. // Remember this node, it may end up being an operand in the pattern.
  2648. SDNode *Parent = nullptr;
  2649. if (NodeStack.size() > 1)
  2650. Parent = NodeStack[NodeStack.size()-2].getNode();
  2651. RecordedNodes.push_back(std::make_pair(N, Parent));
  2652. continue;
  2653. }
  2654. case OPC_RecordChild0: case OPC_RecordChild1:
  2655. case OPC_RecordChild2: case OPC_RecordChild3:
  2656. case OPC_RecordChild4: case OPC_RecordChild5:
  2657. case OPC_RecordChild6: case OPC_RecordChild7: {
  2658. unsigned ChildNo = Opcode-OPC_RecordChild0;
  2659. if (ChildNo >= N.getNumOperands())
  2660. break; // Match fails if out of range child #.
  2661. RecordedNodes.push_back(std::make_pair(N->getOperand(ChildNo),
  2662. N.getNode()));
  2663. continue;
  2664. }
  2665. case OPC_RecordMemRef:
  2666. if (auto *MN = dyn_cast<MemSDNode>(N))
  2667. MatchedMemRefs.push_back(MN->getMemOperand());
  2668. else {
  2669. LLVM_DEBUG(dbgs() << "Expected MemSDNode "; N->dump(CurDAG);
  2670. dbgs() << '\n');
  2671. }
  2672. continue;
  2673. case OPC_CaptureGlueInput:
  2674. // If the current node has an input glue, capture it in InputGlue.
  2675. if (N->getNumOperands() != 0 &&
  2676. N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue)
  2677. InputGlue = N->getOperand(N->getNumOperands()-1);
  2678. continue;
  2679. case OPC_MoveChild: {
  2680. unsigned ChildNo = MatcherTable[MatcherIndex++];
  2681. if (ChildNo >= N.getNumOperands())
  2682. break; // Match fails if out of range child #.
  2683. N = N.getOperand(ChildNo);
  2684. NodeStack.push_back(N);
  2685. continue;
  2686. }
  2687. case OPC_MoveChild0: case OPC_MoveChild1:
  2688. case OPC_MoveChild2: case OPC_MoveChild3:
  2689. case OPC_MoveChild4: case OPC_MoveChild5:
  2690. case OPC_MoveChild6: case OPC_MoveChild7: {
  2691. unsigned ChildNo = Opcode-OPC_MoveChild0;
  2692. if (ChildNo >= N.getNumOperands())
  2693. break; // Match fails if out of range child #.
  2694. N = N.getOperand(ChildNo);
  2695. NodeStack.push_back(N);
  2696. continue;
  2697. }
  2698. case OPC_MoveParent:
  2699. // Pop the current node off the NodeStack.
  2700. NodeStack.pop_back();
  2701. assert(!NodeStack.empty() && "Node stack imbalance!");
  2702. N = NodeStack.back();
  2703. continue;
  2704. case OPC_CheckSame:
  2705. if (!::CheckSame(MatcherTable, MatcherIndex, N, RecordedNodes)) break;
  2706. continue;
  2707. case OPC_CheckChild0Same: case OPC_CheckChild1Same:
  2708. case OPC_CheckChild2Same: case OPC_CheckChild3Same:
  2709. if (!::CheckChildSame(MatcherTable, MatcherIndex, N, RecordedNodes,
  2710. Opcode-OPC_CheckChild0Same))
  2711. break;
  2712. continue;
  2713. case OPC_CheckPatternPredicate:
  2714. if (!::CheckPatternPredicate(MatcherTable, MatcherIndex, *this)) break;
  2715. continue;
  2716. case OPC_CheckPredicate:
  2717. if (!::CheckNodePredicate(MatcherTable, MatcherIndex, *this,
  2718. N.getNode()))
  2719. break;
  2720. continue;
  2721. case OPC_CheckPredicateWithOperands: {
  2722. unsigned OpNum = MatcherTable[MatcherIndex++];
  2723. SmallVector<SDValue, 8> Operands;
  2724. for (unsigned i = 0; i < OpNum; ++i)
  2725. Operands.push_back(RecordedNodes[MatcherTable[MatcherIndex++]].first);
  2726. unsigned PredNo = MatcherTable[MatcherIndex++];
  2727. if (!CheckNodePredicateWithOperands(N.getNode(), PredNo, Operands))
  2728. break;
  2729. continue;
  2730. }
  2731. case OPC_CheckComplexPat: {
  2732. unsigned CPNum = MatcherTable[MatcherIndex++];
  2733. unsigned RecNo = MatcherTable[MatcherIndex++];
  2734. assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat");
  2735. // If target can modify DAG during matching, keep the matching state
  2736. // consistent.
  2737. std::unique_ptr<MatchStateUpdater> MSU;
  2738. if (ComplexPatternFuncMutatesDAG())
  2739. MSU.reset(new MatchStateUpdater(*CurDAG, &NodeToMatch, RecordedNodes,
  2740. MatchScopes));
  2741. if (!CheckComplexPattern(NodeToMatch, RecordedNodes[RecNo].second,
  2742. RecordedNodes[RecNo].first, CPNum,
  2743. RecordedNodes))
  2744. break;
  2745. continue;
  2746. }
  2747. case OPC_CheckOpcode:
  2748. if (!::CheckOpcode(MatcherTable, MatcherIndex, N.getNode())) break;
  2749. continue;
  2750. case OPC_CheckType:
  2751. if (!::CheckType(MatcherTable, MatcherIndex, N, TLI,
  2752. CurDAG->getDataLayout()))
  2753. break;
  2754. continue;
  2755. case OPC_CheckTypeRes: {
  2756. unsigned Res = MatcherTable[MatcherIndex++];
  2757. if (!::CheckType(MatcherTable, MatcherIndex, N.getValue(Res), TLI,
  2758. CurDAG->getDataLayout()))
  2759. break;
  2760. continue;
  2761. }
  2762. case OPC_SwitchOpcode: {
  2763. unsigned CurNodeOpcode = N.getOpcode();
  2764. unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
  2765. unsigned CaseSize;
  2766. while (true) {
  2767. // Get the size of this case.
  2768. CaseSize = MatcherTable[MatcherIndex++];
  2769. if (CaseSize & 128)
  2770. CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
  2771. if (CaseSize == 0) break;
  2772. uint16_t Opc = MatcherTable[MatcherIndex++];
  2773. Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
  2774. // If the opcode matches, then we will execute this case.
  2775. if (CurNodeOpcode == Opc)
  2776. break;
  2777. // Otherwise, skip over this case.
  2778. MatcherIndex += CaseSize;
  2779. }
  2780. // If no cases matched, bail out.
  2781. if (CaseSize == 0) break;
  2782. // Otherwise, execute the case we found.
  2783. LLVM_DEBUG(dbgs() << " OpcodeSwitch from " << SwitchStart << " to "
  2784. << MatcherIndex << "\n");
  2785. continue;
  2786. }
  2787. case OPC_SwitchType: {
  2788. MVT CurNodeVT = N.getSimpleValueType();
  2789. unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
  2790. unsigned CaseSize;
  2791. while (true) {
  2792. // Get the size of this case.
  2793. CaseSize = MatcherTable[MatcherIndex++];
  2794. if (CaseSize & 128)
  2795. CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
  2796. if (CaseSize == 0) break;
  2797. MVT CaseVT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  2798. if (CaseVT == MVT::iPTR)
  2799. CaseVT = TLI->getPointerTy(CurDAG->getDataLayout());
  2800. // If the VT matches, then we will execute this case.
  2801. if (CurNodeVT == CaseVT)
  2802. break;
  2803. // Otherwise, skip over this case.
  2804. MatcherIndex += CaseSize;
  2805. }
  2806. // If no cases matched, bail out.
  2807. if (CaseSize == 0) break;
  2808. // Otherwise, execute the case we found.
  2809. LLVM_DEBUG(dbgs() << " TypeSwitch[" << EVT(CurNodeVT).getEVTString()
  2810. << "] from " << SwitchStart << " to " << MatcherIndex
  2811. << '\n');
  2812. continue;
  2813. }
  2814. case OPC_CheckChild0Type: case OPC_CheckChild1Type:
  2815. case OPC_CheckChild2Type: case OPC_CheckChild3Type:
  2816. case OPC_CheckChild4Type: case OPC_CheckChild5Type:
  2817. case OPC_CheckChild6Type: case OPC_CheckChild7Type:
  2818. if (!::CheckChildType(MatcherTable, MatcherIndex, N, TLI,
  2819. CurDAG->getDataLayout(),
  2820. Opcode - OPC_CheckChild0Type))
  2821. break;
  2822. continue;
  2823. case OPC_CheckCondCode:
  2824. if (!::CheckCondCode(MatcherTable, MatcherIndex, N)) break;
  2825. continue;
  2826. case OPC_CheckChild2CondCode:
  2827. if (!::CheckChild2CondCode(MatcherTable, MatcherIndex, N)) break;
  2828. continue;
  2829. case OPC_CheckValueType:
  2830. if (!::CheckValueType(MatcherTable, MatcherIndex, N, TLI,
  2831. CurDAG->getDataLayout()))
  2832. break;
  2833. continue;
  2834. case OPC_CheckInteger:
  2835. if (!::CheckInteger(MatcherTable, MatcherIndex, N)) break;
  2836. continue;
  2837. case OPC_CheckChild0Integer: case OPC_CheckChild1Integer:
  2838. case OPC_CheckChild2Integer: case OPC_CheckChild3Integer:
  2839. case OPC_CheckChild4Integer:
  2840. if (!::CheckChildInteger(MatcherTable, MatcherIndex, N,
  2841. Opcode-OPC_CheckChild0Integer)) break;
  2842. continue;
  2843. case OPC_CheckAndImm:
  2844. if (!::CheckAndImm(MatcherTable, MatcherIndex, N, *this)) break;
  2845. continue;
  2846. case OPC_CheckOrImm:
  2847. if (!::CheckOrImm(MatcherTable, MatcherIndex, N, *this)) break;
  2848. continue;
  2849. case OPC_CheckImmAllOnesV:
  2850. if (!ISD::isConstantSplatVectorAllOnes(N.getNode()))
  2851. break;
  2852. continue;
  2853. case OPC_CheckImmAllZerosV:
  2854. if (!ISD::isConstantSplatVectorAllZeros(N.getNode()))
  2855. break;
  2856. continue;
  2857. case OPC_CheckFoldableChainNode: {
  2858. assert(NodeStack.size() != 1 && "No parent node");
  2859. // Verify that all intermediate nodes between the root and this one have
  2860. // a single use (ignoring chains, which are handled in UpdateChains).
  2861. bool HasMultipleUses = false;
  2862. for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i) {
  2863. unsigned NNonChainUses = 0;
  2864. SDNode *NS = NodeStack[i].getNode();
  2865. for (auto UI = NS->use_begin(), UE = NS->use_end(); UI != UE; ++UI)
  2866. if (UI.getUse().getValueType() != MVT::Other)
  2867. if (++NNonChainUses > 1) {
  2868. HasMultipleUses = true;
  2869. break;
  2870. }
  2871. if (HasMultipleUses) break;
  2872. }
  2873. if (HasMultipleUses) break;
  2874. // Check to see that the target thinks this is profitable to fold and that
  2875. // we can fold it without inducing cycles in the graph.
  2876. if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(),
  2877. NodeToMatch) ||
  2878. !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(),
  2879. NodeToMatch, OptLevel,
  2880. true/*We validate our own chains*/))
  2881. break;
  2882. continue;
  2883. }
  2884. case OPC_EmitInteger:
  2885. case OPC_EmitStringInteger: {
  2886. MVT::SimpleValueType VT =
  2887. (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  2888. int64_t Val = MatcherTable[MatcherIndex++];
  2889. if (Val & 128)
  2890. Val = GetVBR(Val, MatcherTable, MatcherIndex);
  2891. if (Opcode == OPC_EmitInteger)
  2892. Val = decodeSignRotatedValue(Val);
  2893. RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
  2894. CurDAG->getTargetConstant(Val, SDLoc(NodeToMatch),
  2895. VT), nullptr));
  2896. continue;
  2897. }
  2898. case OPC_EmitRegister: {
  2899. MVT::SimpleValueType VT =
  2900. (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  2901. unsigned RegNo = MatcherTable[MatcherIndex++];
  2902. RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
  2903. CurDAG->getRegister(RegNo, VT), nullptr));
  2904. continue;
  2905. }
  2906. case OPC_EmitRegister2: {
  2907. // For targets w/ more than 256 register names, the register enum
  2908. // values are stored in two bytes in the matcher table (just like
  2909. // opcodes).
  2910. MVT::SimpleValueType VT =
  2911. (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  2912. unsigned RegNo = MatcherTable[MatcherIndex++];
  2913. RegNo |= MatcherTable[MatcherIndex++] << 8;
  2914. RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
  2915. CurDAG->getRegister(RegNo, VT), nullptr));
  2916. continue;
  2917. }
  2918. case OPC_EmitConvertToTarget: {
  2919. // Convert from IMM/FPIMM to target version.
  2920. unsigned RecNo = MatcherTable[MatcherIndex++];
  2921. assert(RecNo < RecordedNodes.size() && "Invalid EmitConvertToTarget");
  2922. SDValue Imm = RecordedNodes[RecNo].first;
  2923. if (Imm->getOpcode() == ISD::Constant) {
  2924. const ConstantInt *Val=cast<ConstantSDNode>(Imm)->getConstantIntValue();
  2925. Imm = CurDAG->getTargetConstant(*Val, SDLoc(NodeToMatch),
  2926. Imm.getValueType());
  2927. } else if (Imm->getOpcode() == ISD::ConstantFP) {
  2928. const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue();
  2929. Imm = CurDAG->getTargetConstantFP(*Val, SDLoc(NodeToMatch),
  2930. Imm.getValueType());
  2931. }
  2932. RecordedNodes.push_back(std::make_pair(Imm, RecordedNodes[RecNo].second));
  2933. continue;
  2934. }
  2935. case OPC_EmitMergeInputChains1_0: // OPC_EmitMergeInputChains, 1, 0
  2936. case OPC_EmitMergeInputChains1_1: // OPC_EmitMergeInputChains, 1, 1
  2937. case OPC_EmitMergeInputChains1_2: { // OPC_EmitMergeInputChains, 1, 2
  2938. // These are space-optimized forms of OPC_EmitMergeInputChains.
  2939. assert(!InputChain.getNode() &&
  2940. "EmitMergeInputChains should be the first chain producing node");
  2941. assert(ChainNodesMatched.empty() &&
  2942. "Should only have one EmitMergeInputChains per match");
  2943. // Read all of the chained nodes.
  2944. unsigned RecNo = Opcode - OPC_EmitMergeInputChains1_0;
  2945. assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains");
  2946. ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
  2947. // If the chained node is not the root, we can't fold it if it has
  2948. // multiple uses.
  2949. // FIXME: What if other value results of the node have uses not matched
  2950. // by this pattern?
  2951. if (ChainNodesMatched.back() != NodeToMatch &&
  2952. !RecordedNodes[RecNo].first.hasOneUse()) {
  2953. ChainNodesMatched.clear();
  2954. break;
  2955. }
  2956. // Merge the input chains if they are not intra-pattern references.
  2957. InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
  2958. if (!InputChain.getNode())
  2959. break; // Failed to merge.
  2960. continue;
  2961. }
  2962. case OPC_EmitMergeInputChains: {
  2963. assert(!InputChain.getNode() &&
  2964. "EmitMergeInputChains should be the first chain producing node");
  2965. // This node gets a list of nodes we matched in the input that have
  2966. // chains. We want to token factor all of the input chains to these nodes
  2967. // together. However, if any of the input chains is actually one of the
  2968. // nodes matched in this pattern, then we have an intra-match reference.
  2969. // Ignore these because the newly token factored chain should not refer to
  2970. // the old nodes.
  2971. unsigned NumChains = MatcherTable[MatcherIndex++];
  2972. assert(NumChains != 0 && "Can't TF zero chains");
  2973. assert(ChainNodesMatched.empty() &&
  2974. "Should only have one EmitMergeInputChains per match");
  2975. // Read all of the chained nodes.
  2976. for (unsigned i = 0; i != NumChains; ++i) {
  2977. unsigned RecNo = MatcherTable[MatcherIndex++];
  2978. assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains");
  2979. ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
  2980. // If the chained node is not the root, we can't fold it if it has
  2981. // multiple uses.
  2982. // FIXME: What if other value results of the node have uses not matched
  2983. // by this pattern?
  2984. if (ChainNodesMatched.back() != NodeToMatch &&
  2985. !RecordedNodes[RecNo].first.hasOneUse()) {
  2986. ChainNodesMatched.clear();
  2987. break;
  2988. }
  2989. }
  2990. // If the inner loop broke out, the match fails.
  2991. if (ChainNodesMatched.empty())
  2992. break;
  2993. // Merge the input chains if they are not intra-pattern references.
  2994. InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
  2995. if (!InputChain.getNode())
  2996. break; // Failed to merge.
  2997. continue;
  2998. }
  2999. case OPC_EmitCopyToReg:
  3000. case OPC_EmitCopyToReg2: {
  3001. unsigned RecNo = MatcherTable[MatcherIndex++];
  3002. assert(RecNo < RecordedNodes.size() && "Invalid EmitCopyToReg");
  3003. unsigned DestPhysReg = MatcherTable[MatcherIndex++];
  3004. if (Opcode == OPC_EmitCopyToReg2)
  3005. DestPhysReg |= MatcherTable[MatcherIndex++] << 8;
  3006. if (!InputChain.getNode())
  3007. InputChain = CurDAG->getEntryNode();
  3008. InputChain = CurDAG->getCopyToReg(InputChain, SDLoc(NodeToMatch),
  3009. DestPhysReg, RecordedNodes[RecNo].first,
  3010. InputGlue);
  3011. InputGlue = InputChain.getValue(1);
  3012. continue;
  3013. }
  3014. case OPC_EmitNodeXForm: {
  3015. unsigned XFormNo = MatcherTable[MatcherIndex++];
  3016. unsigned RecNo = MatcherTable[MatcherIndex++];
  3017. assert(RecNo < RecordedNodes.size() && "Invalid EmitNodeXForm");
  3018. SDValue Res = RunSDNodeXForm(RecordedNodes[RecNo].first, XFormNo);
  3019. RecordedNodes.push_back(std::pair<SDValue,SDNode*>(Res, nullptr));
  3020. continue;
  3021. }
  3022. case OPC_Coverage: {
  3023. // This is emitted right before MorphNode/EmitNode.
  3024. // So it should be safe to assume that this node has been selected
  3025. unsigned index = MatcherTable[MatcherIndex++];
  3026. index |= (MatcherTable[MatcherIndex++] << 8);
  3027. dbgs() << "COVERED: " << getPatternForIndex(index) << "\n";
  3028. dbgs() << "INCLUDED: " << getIncludePathForIndex(index) << "\n";
  3029. continue;
  3030. }
  3031. case OPC_EmitNode: case OPC_MorphNodeTo:
  3032. case OPC_EmitNode0: case OPC_EmitNode1: case OPC_EmitNode2:
  3033. case OPC_MorphNodeTo0: case OPC_MorphNodeTo1: case OPC_MorphNodeTo2: {
  3034. uint16_t TargetOpc = MatcherTable[MatcherIndex++];
  3035. TargetOpc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
  3036. unsigned EmitNodeInfo = MatcherTable[MatcherIndex++];
  3037. // Get the result VT list.
  3038. unsigned NumVTs;
  3039. // If this is one of the compressed forms, get the number of VTs based
  3040. // on the Opcode. Otherwise read the next byte from the table.
  3041. if (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2)
  3042. NumVTs = Opcode - OPC_MorphNodeTo0;
  3043. else if (Opcode >= OPC_EmitNode0 && Opcode <= OPC_EmitNode2)
  3044. NumVTs = Opcode - OPC_EmitNode0;
  3045. else
  3046. NumVTs = MatcherTable[MatcherIndex++];
  3047. SmallVector<EVT, 4> VTs;
  3048. for (unsigned i = 0; i != NumVTs; ++i) {
  3049. MVT::SimpleValueType VT =
  3050. (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
  3051. if (VT == MVT::iPTR)
  3052. VT = TLI->getPointerTy(CurDAG->getDataLayout()).SimpleTy;
  3053. VTs.push_back(VT);
  3054. }
  3055. if (EmitNodeInfo & OPFL_Chain)
  3056. VTs.push_back(MVT::Other);
  3057. if (EmitNodeInfo & OPFL_GlueOutput)
  3058. VTs.push_back(MVT::Glue);
  3059. // This is hot code, so optimize the two most common cases of 1 and 2
  3060. // results.
  3061. SDVTList VTList;
  3062. if (VTs.size() == 1)
  3063. VTList = CurDAG->getVTList(VTs[0]);
  3064. else if (VTs.size() == 2)
  3065. VTList = CurDAG->getVTList(VTs[0], VTs[1]);
  3066. else
  3067. VTList = CurDAG->getVTList(VTs);
  3068. // Get the operand list.
  3069. unsigned NumOps = MatcherTable[MatcherIndex++];
  3070. SmallVector<SDValue, 8> Ops;
  3071. for (unsigned i = 0; i != NumOps; ++i) {
  3072. unsigned RecNo = MatcherTable[MatcherIndex++];
  3073. if (RecNo & 128)
  3074. RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
  3075. assert(RecNo < RecordedNodes.size() && "Invalid EmitNode");
  3076. Ops.push_back(RecordedNodes[RecNo].first);
  3077. }
  3078. // If there are variadic operands to add, handle them now.
  3079. if (EmitNodeInfo & OPFL_VariadicInfo) {
  3080. // Determine the start index to copy from.
  3081. unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo);
  3082. FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0;
  3083. assert(NodeToMatch->getNumOperands() >= FirstOpToCopy &&
  3084. "Invalid variadic node");
  3085. // Copy all of the variadic operands, not including a potential glue
  3086. // input.
  3087. for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands();
  3088. i != e; ++i) {
  3089. SDValue V = NodeToMatch->getOperand(i);
  3090. if (V.getValueType() == MVT::Glue) break;
  3091. Ops.push_back(V);
  3092. }
  3093. }
  3094. // If this has chain/glue inputs, add them.
  3095. if (EmitNodeInfo & OPFL_Chain)
  3096. Ops.push_back(InputChain);
  3097. if ((EmitNodeInfo & OPFL_GlueInput) && InputGlue.getNode() != nullptr)
  3098. Ops.push_back(InputGlue);
  3099. // Check whether any matched node could raise an FP exception. Since all
  3100. // such nodes must have a chain, it suffices to check ChainNodesMatched.
  3101. // We need to perform this check before potentially modifying one of the
  3102. // nodes via MorphNode.
  3103. bool MayRaiseFPException =
  3104. llvm::any_of(ChainNodesMatched, [this](SDNode *N) {
  3105. return mayRaiseFPException(N) && !N->getFlags().hasNoFPExcept();
  3106. });
  3107. // Create the node.
  3108. MachineSDNode *Res = nullptr;
  3109. bool IsMorphNodeTo = Opcode == OPC_MorphNodeTo ||
  3110. (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2);
  3111. if (!IsMorphNodeTo) {
  3112. // If this is a normal EmitNode command, just create the new node and
  3113. // add the results to the RecordedNodes list.
  3114. Res = CurDAG->getMachineNode(TargetOpc, SDLoc(NodeToMatch),
  3115. VTList, Ops);
  3116. // Add all the non-glue/non-chain results to the RecordedNodes list.
  3117. for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
  3118. if (VTs[i] == MVT::Other || VTs[i] == MVT::Glue) break;
  3119. RecordedNodes.push_back(std::pair<SDValue,SDNode*>(SDValue(Res, i),
  3120. nullptr));
  3121. }
  3122. } else {
  3123. assert(NodeToMatch->getOpcode() != ISD::DELETED_NODE &&
  3124. "NodeToMatch was removed partway through selection");
  3125. SelectionDAG::DAGNodeDeletedListener NDL(*CurDAG, [&](SDNode *N,
  3126. SDNode *E) {
  3127. CurDAG->salvageDebugInfo(*N);
  3128. auto &Chain = ChainNodesMatched;
  3129. assert((!E || !is_contained(Chain, N)) &&
  3130. "Chain node replaced during MorphNode");
  3131. llvm::erase_value(Chain, N);
  3132. });
  3133. Res = cast<MachineSDNode>(MorphNode(NodeToMatch, TargetOpc, VTList,
  3134. Ops, EmitNodeInfo));
  3135. }
  3136. // Set the NoFPExcept flag when no original matched node could
  3137. // raise an FP exception, but the new node potentially might.
  3138. if (!MayRaiseFPException && mayRaiseFPException(Res)) {
  3139. SDNodeFlags Flags = Res->getFlags();
  3140. Flags.setNoFPExcept(true);
  3141. Res->setFlags(Flags);
  3142. }
  3143. // If the node had chain/glue results, update our notion of the current
  3144. // chain and glue.
  3145. if (EmitNodeInfo & OPFL_GlueOutput) {
  3146. InputGlue = SDValue(Res, VTs.size()-1);
  3147. if (EmitNodeInfo & OPFL_Chain)
  3148. InputChain = SDValue(Res, VTs.size()-2);
  3149. } else if (EmitNodeInfo & OPFL_Chain)
  3150. InputChain = SDValue(Res, VTs.size()-1);
  3151. // If the OPFL_MemRefs glue is set on this node, slap all of the
  3152. // accumulated memrefs onto it.
  3153. //
  3154. // FIXME: This is vastly incorrect for patterns with multiple outputs
  3155. // instructions that access memory and for ComplexPatterns that match
  3156. // loads.
  3157. if (EmitNodeInfo & OPFL_MemRefs) {
  3158. // Only attach load or store memory operands if the generated
  3159. // instruction may load or store.
  3160. const MCInstrDesc &MCID = TII->get(TargetOpc);
  3161. bool mayLoad = MCID.mayLoad();
  3162. bool mayStore = MCID.mayStore();
  3163. // We expect to have relatively few of these so just filter them into a
  3164. // temporary buffer so that we can easily add them to the instruction.
  3165. SmallVector<MachineMemOperand *, 4> FilteredMemRefs;
  3166. for (MachineMemOperand *MMO : MatchedMemRefs) {
  3167. if (MMO->isLoad()) {
  3168. if (mayLoad)
  3169. FilteredMemRefs.push_back(MMO);
  3170. } else if (MMO->isStore()) {
  3171. if (mayStore)
  3172. FilteredMemRefs.push_back(MMO);
  3173. } else {
  3174. FilteredMemRefs.push_back(MMO);
  3175. }
  3176. }
  3177. CurDAG->setNodeMemRefs(Res, FilteredMemRefs);
  3178. }
  3179. LLVM_DEBUG(if (!MatchedMemRefs.empty() && Res->memoperands_empty()) dbgs()
  3180. << " Dropping mem operands\n";
  3181. dbgs() << " " << (IsMorphNodeTo ? "Morphed" : "Created")
  3182. << " node: ";
  3183. Res->dump(CurDAG););
  3184. // If this was a MorphNodeTo then we're completely done!
  3185. if (IsMorphNodeTo) {
  3186. // Update chain uses.
  3187. UpdateChains(Res, InputChain, ChainNodesMatched, true);
  3188. return;
  3189. }
  3190. continue;
  3191. }
  3192. case OPC_CompleteMatch: {
  3193. // The match has been completed, and any new nodes (if any) have been
  3194. // created. Patch up references to the matched dag to use the newly
  3195. // created nodes.
  3196. unsigned NumResults = MatcherTable[MatcherIndex++];
  3197. for (unsigned i = 0; i != NumResults; ++i) {
  3198. unsigned ResSlot = MatcherTable[MatcherIndex++];
  3199. if (ResSlot & 128)
  3200. ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex);
  3201. assert(ResSlot < RecordedNodes.size() && "Invalid CompleteMatch");
  3202. SDValue Res = RecordedNodes[ResSlot].first;
  3203. assert(i < NodeToMatch->getNumValues() &&
  3204. NodeToMatch->getValueType(i) != MVT::Other &&
  3205. NodeToMatch->getValueType(i) != MVT::Glue &&
  3206. "Invalid number of results to complete!");
  3207. assert((NodeToMatch->getValueType(i) == Res.getValueType() ||
  3208. NodeToMatch->getValueType(i) == MVT::iPTR ||
  3209. Res.getValueType() == MVT::iPTR ||
  3210. NodeToMatch->getValueType(i).getSizeInBits() ==
  3211. Res.getValueSizeInBits()) &&
  3212. "invalid replacement");
  3213. ReplaceUses(SDValue(NodeToMatch, i), Res);
  3214. }
  3215. // Update chain uses.
  3216. UpdateChains(NodeToMatch, InputChain, ChainNodesMatched, false);
  3217. // If the root node defines glue, we need to update it to the glue result.
  3218. // TODO: This never happens in our tests and I think it can be removed /
  3219. // replaced with an assert, but if we do it this the way the change is
  3220. // NFC.
  3221. if (NodeToMatch->getValueType(NodeToMatch->getNumValues() - 1) ==
  3222. MVT::Glue &&
  3223. InputGlue.getNode())
  3224. ReplaceUses(SDValue(NodeToMatch, NodeToMatch->getNumValues() - 1),
  3225. InputGlue);
  3226. assert(NodeToMatch->use_empty() &&
  3227. "Didn't replace all uses of the node?");
  3228. CurDAG->RemoveDeadNode(NodeToMatch);
  3229. return;
  3230. }
  3231. }
  3232. // If the code reached this point, then the match failed. See if there is
  3233. // another child to try in the current 'Scope', otherwise pop it until we
  3234. // find a case to check.
  3235. LLVM_DEBUG(dbgs() << " Match failed at index " << CurrentOpcodeIndex
  3236. << "\n");
  3237. ++NumDAGIselRetries;
  3238. while (true) {
  3239. if (MatchScopes.empty()) {
  3240. CannotYetSelect(NodeToMatch);
  3241. return;
  3242. }
  3243. // Restore the interpreter state back to the point where the scope was
  3244. // formed.
  3245. MatchScope &LastScope = MatchScopes.back();
  3246. RecordedNodes.resize(LastScope.NumRecordedNodes);
  3247. NodeStack.clear();
  3248. NodeStack.append(LastScope.NodeStack.begin(), LastScope.NodeStack.end());
  3249. N = NodeStack.back();
  3250. if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size())
  3251. MatchedMemRefs.resize(LastScope.NumMatchedMemRefs);
  3252. MatcherIndex = LastScope.FailIndex;
  3253. LLVM_DEBUG(dbgs() << " Continuing at " << MatcherIndex << "\n");
  3254. InputChain = LastScope.InputChain;
  3255. InputGlue = LastScope.InputGlue;
  3256. if (!LastScope.HasChainNodesMatched)
  3257. ChainNodesMatched.clear();
  3258. // Check to see what the offset is at the new MatcherIndex. If it is zero
  3259. // we have reached the end of this scope, otherwise we have another child
  3260. // in the current scope to try.
  3261. unsigned NumToSkip = MatcherTable[MatcherIndex++];
  3262. if (NumToSkip & 128)
  3263. NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
  3264. // If we have another child in this scope to match, update FailIndex and
  3265. // try it.
  3266. if (NumToSkip != 0) {
  3267. LastScope.FailIndex = MatcherIndex+NumToSkip;
  3268. break;
  3269. }
  3270. // End of this scope, pop it and try the next child in the containing
  3271. // scope.
  3272. MatchScopes.pop_back();
  3273. }
  3274. }
  3275. }
  3276. /// Return whether the node may raise an FP exception.
  3277. bool SelectionDAGISel::mayRaiseFPException(SDNode *N) const {
  3278. // For machine opcodes, consult the MCID flag.
  3279. if (N->isMachineOpcode()) {
  3280. const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
  3281. return MCID.mayRaiseFPException();
  3282. }
  3283. // For ISD opcodes, only StrictFP opcodes may raise an FP
  3284. // exception.
  3285. if (N->isTargetOpcode())
  3286. return N->isTargetStrictFPOpcode();
  3287. return N->isStrictFPOpcode();
  3288. }
  3289. bool SelectionDAGISel::isOrEquivalentToAdd(const SDNode *N) const {
  3290. assert(N->getOpcode() == ISD::OR && "Unexpected opcode");
  3291. auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
  3292. if (!C)
  3293. return false;
  3294. // Detect when "or" is used to add an offset to a stack object.
  3295. if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
  3296. MachineFrameInfo &MFI = MF->getFrameInfo();
  3297. Align A = MFI.getObjectAlign(FN->getIndex());
  3298. int32_t Off = C->getSExtValue();
  3299. // If the alleged offset fits in the zero bits guaranteed by
  3300. // the alignment, then this or is really an add.
  3301. return (Off >= 0) && (((A.value() - 1) & Off) == unsigned(Off));
  3302. }
  3303. return false;
  3304. }
  3305. void SelectionDAGISel::CannotYetSelect(SDNode *N) {
  3306. std::string msg;
  3307. raw_string_ostream Msg(msg);
  3308. Msg << "Cannot select: ";
  3309. if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN &&
  3310. N->getOpcode() != ISD::INTRINSIC_WO_CHAIN &&
  3311. N->getOpcode() != ISD::INTRINSIC_VOID) {
  3312. N->printrFull(Msg, CurDAG);
  3313. Msg << "\nIn function: " << MF->getName();
  3314. } else {
  3315. bool HasInputChain = N->getOperand(0).getValueType() == MVT::Other;
  3316. unsigned iid =
  3317. cast<ConstantSDNode>(N->getOperand(HasInputChain))->getZExtValue();
  3318. if (iid < Intrinsic::num_intrinsics)
  3319. Msg << "intrinsic %" << Intrinsic::getBaseName((Intrinsic::ID)iid);
  3320. else if (const TargetIntrinsicInfo *TII = TM.getIntrinsicInfo())
  3321. Msg << "target intrinsic %" << TII->getName(iid);
  3322. else
  3323. Msg << "unknown intrinsic #" << iid;
  3324. }
  3325. report_fatal_error(Twine(Msg.str()));
  3326. }