SelectionDAGISel.cpp 138 KB

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