SelectionDAGISel.cpp 124 KB

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