SelectionDAGISel.cpp 142 KB

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