SelectionDAGISel.cpp 132 KB

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