SelectionDAGISel.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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 "ScheduleDAGSDNodes.h"
  15. #include "SelectionDAGBuild.h"
  16. #include "llvm/CodeGen/SelectionDAGISel.h"
  17. #include "llvm/Analysis/AliasAnalysis.h"
  18. #include "llvm/Constants.h"
  19. #include "llvm/CallingConv.h"
  20. #include "llvm/DerivedTypes.h"
  21. #include "llvm/Function.h"
  22. #include "llvm/GlobalVariable.h"
  23. #include "llvm/InlineAsm.h"
  24. #include "llvm/Instructions.h"
  25. #include "llvm/Intrinsics.h"
  26. #include "llvm/IntrinsicInst.h"
  27. #include "llvm/CodeGen/FastISel.h"
  28. #include "llvm/CodeGen/GCStrategy.h"
  29. #include "llvm/CodeGen/GCMetadata.h"
  30. #include "llvm/CodeGen/MachineFunction.h"
  31. #include "llvm/CodeGen/MachineFunctionAnalysis.h"
  32. #include "llvm/CodeGen/MachineFrameInfo.h"
  33. #include "llvm/CodeGen/MachineInstrBuilder.h"
  34. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  35. #include "llvm/CodeGen/MachineModuleInfo.h"
  36. #include "llvm/CodeGen/MachineRegisterInfo.h"
  37. #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
  38. #include "llvm/CodeGen/SchedulerRegistry.h"
  39. #include "llvm/CodeGen/SelectionDAG.h"
  40. #include "llvm/CodeGen/DwarfWriter.h"
  41. #include "llvm/Target/TargetRegisterInfo.h"
  42. #include "llvm/Target/TargetData.h"
  43. #include "llvm/Target/TargetFrameInfo.h"
  44. #include "llvm/Target/TargetInstrInfo.h"
  45. #include "llvm/Target/TargetLowering.h"
  46. #include "llvm/Target/TargetMachine.h"
  47. #include "llvm/Target/TargetOptions.h"
  48. #include "llvm/Support/Compiler.h"
  49. #include "llvm/Support/Debug.h"
  50. #include "llvm/Support/ErrorHandling.h"
  51. #include "llvm/Support/MathExtras.h"
  52. #include "llvm/Support/Timer.h"
  53. #include "llvm/Support/raw_ostream.h"
  54. #include <algorithm>
  55. using namespace llvm;
  56. static cl::opt<bool>
  57. DisableLegalizeTypes("disable-legalize-types", cl::Hidden);
  58. static cl::opt<bool>
  59. EnableFastISelVerbose("fast-isel-verbose", cl::Hidden,
  60. cl::desc("Enable verbose messages in the \"fast\" "
  61. "instruction selector"));
  62. static cl::opt<bool>
  63. EnableFastISelAbort("fast-isel-abort", cl::Hidden,
  64. cl::desc("Enable abort calls when \"fast\" instruction fails"));
  65. static cl::opt<bool>
  66. SchedLiveInCopies("schedule-livein-copies",
  67. cl::desc("Schedule copies of livein registers"),
  68. cl::init(false));
  69. #ifndef NDEBUG
  70. static cl::opt<bool>
  71. ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
  72. cl::desc("Pop up a window to show dags before the first "
  73. "dag combine pass"));
  74. static cl::opt<bool>
  75. ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
  76. cl::desc("Pop up a window to show dags before legalize types"));
  77. static cl::opt<bool>
  78. ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
  79. cl::desc("Pop up a window to show dags before legalize"));
  80. static cl::opt<bool>
  81. ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
  82. cl::desc("Pop up a window to show dags before the second "
  83. "dag combine pass"));
  84. static cl::opt<bool>
  85. ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
  86. cl::desc("Pop up a window to show dags before the post legalize types"
  87. " dag combine pass"));
  88. static cl::opt<bool>
  89. ViewISelDAGs("view-isel-dags", cl::Hidden,
  90. cl::desc("Pop up a window to show isel dags as they are selected"));
  91. static cl::opt<bool>
  92. ViewSchedDAGs("view-sched-dags", cl::Hidden,
  93. cl::desc("Pop up a window to show sched dags as they are processed"));
  94. static cl::opt<bool>
  95. ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
  96. cl::desc("Pop up a window to show SUnit dags after they are processed"));
  97. #else
  98. static const bool ViewDAGCombine1 = false,
  99. ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false,
  100. ViewDAGCombine2 = false,
  101. ViewDAGCombineLT = false,
  102. ViewISelDAGs = false, ViewSchedDAGs = false,
  103. ViewSUnitDAGs = false;
  104. #endif
  105. //===---------------------------------------------------------------------===//
  106. ///
  107. /// RegisterScheduler class - Track the registration of instruction schedulers.
  108. ///
  109. //===---------------------------------------------------------------------===//
  110. MachinePassRegistry RegisterScheduler::Registry;
  111. //===---------------------------------------------------------------------===//
  112. ///
  113. /// ISHeuristic command line option for instruction schedulers.
  114. ///
  115. //===---------------------------------------------------------------------===//
  116. static cl::opt<RegisterScheduler::FunctionPassCtor, false,
  117. RegisterPassParser<RegisterScheduler> >
  118. ISHeuristic("pre-RA-sched",
  119. cl::init(&createDefaultScheduler),
  120. cl::desc("Instruction schedulers available (before register"
  121. " allocation):"));
  122. static RegisterScheduler
  123. defaultListDAGScheduler("default", "Best scheduler for the target",
  124. createDefaultScheduler);
  125. namespace llvm {
  126. //===--------------------------------------------------------------------===//
  127. /// createDefaultScheduler - This creates an instruction scheduler appropriate
  128. /// for the target.
  129. ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
  130. CodeGenOpt::Level OptLevel) {
  131. const TargetLowering &TLI = IS->getTargetLowering();
  132. if (OptLevel == CodeGenOpt::None)
  133. return createFastDAGScheduler(IS, OptLevel);
  134. if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
  135. return createTDListDAGScheduler(IS, OptLevel);
  136. assert(TLI.getSchedulingPreference() ==
  137. TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
  138. return createBURRListDAGScheduler(IS, OptLevel);
  139. }
  140. }
  141. // EmitInstrWithCustomInserter - This method should be implemented by targets
  142. // that mark instructions with the 'usesCustomDAGSchedInserter' flag. These
  143. // instructions are special in various ways, which require special support to
  144. // insert. The specified MachineInstr is created but not inserted into any
  145. // basic blocks, and the scheduler passes ownership of it to this method.
  146. MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
  147. MachineBasicBlock *MBB) const {
  148. #ifndef NDEBUG
  149. cerr << "If a target marks an instruction with "
  150. "'usesCustomDAGSchedInserter', it must implement "
  151. "TargetLowering::EmitInstrWithCustomInserter!";
  152. #endif
  153. llvm_unreachable(0);
  154. return 0;
  155. }
  156. /// EmitLiveInCopy - Emit a copy for a live in physical register. If the
  157. /// physical register has only a single copy use, then coalesced the copy
  158. /// if possible.
  159. static void EmitLiveInCopy(MachineBasicBlock *MBB,
  160. MachineBasicBlock::iterator &InsertPos,
  161. unsigned VirtReg, unsigned PhysReg,
  162. const TargetRegisterClass *RC,
  163. DenseMap<MachineInstr*, unsigned> &CopyRegMap,
  164. const MachineRegisterInfo &MRI,
  165. const TargetRegisterInfo &TRI,
  166. const TargetInstrInfo &TII) {
  167. unsigned NumUses = 0;
  168. MachineInstr *UseMI = NULL;
  169. for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
  170. UE = MRI.use_end(); UI != UE; ++UI) {
  171. UseMI = &*UI;
  172. if (++NumUses > 1)
  173. break;
  174. }
  175. // If the number of uses is not one, or the use is not a move instruction,
  176. // don't coalesce. Also, only coalesce away a virtual register to virtual
  177. // register copy.
  178. bool Coalesced = false;
  179. unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
  180. if (NumUses == 1 &&
  181. TII.isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
  182. TargetRegisterInfo::isVirtualRegister(DstReg)) {
  183. VirtReg = DstReg;
  184. Coalesced = true;
  185. }
  186. // Now find an ideal location to insert the copy.
  187. MachineBasicBlock::iterator Pos = InsertPos;
  188. while (Pos != MBB->begin()) {
  189. MachineInstr *PrevMI = prior(Pos);
  190. DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
  191. // copyRegToReg might emit multiple instructions to do a copy.
  192. unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
  193. if (CopyDstReg && !TRI.regsOverlap(CopyDstReg, PhysReg))
  194. // This is what the BB looks like right now:
  195. // r1024 = mov r0
  196. // ...
  197. // r1 = mov r1024
  198. //
  199. // We want to insert "r1025 = mov r1". Inserting this copy below the
  200. // move to r1024 makes it impossible for that move to be coalesced.
  201. //
  202. // r1025 = mov r1
  203. // r1024 = mov r0
  204. // ...
  205. // r1 = mov 1024
  206. // r2 = mov 1025
  207. break; // Woot! Found a good location.
  208. --Pos;
  209. }
  210. bool Emitted = TII.copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
  211. assert(Emitted && "Unable to issue a live-in copy instruction!\n");
  212. (void) Emitted;
  213. CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
  214. if (Coalesced) {
  215. if (&*InsertPos == UseMI) ++InsertPos;
  216. MBB->erase(UseMI);
  217. }
  218. }
  219. /// EmitLiveInCopies - If this is the first basic block in the function,
  220. /// and if it has live ins that need to be copied into vregs, emit the
  221. /// copies into the block.
  222. static void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
  223. const MachineRegisterInfo &MRI,
  224. const TargetRegisterInfo &TRI,
  225. const TargetInstrInfo &TII) {
  226. if (SchedLiveInCopies) {
  227. // Emit the copies at a heuristically-determined location in the block.
  228. DenseMap<MachineInstr*, unsigned> CopyRegMap;
  229. MachineBasicBlock::iterator InsertPos = EntryMBB->begin();
  230. for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
  231. E = MRI.livein_end(); LI != E; ++LI)
  232. if (LI->second) {
  233. const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
  234. EmitLiveInCopy(EntryMBB, InsertPos, LI->second, LI->first,
  235. RC, CopyRegMap, MRI, TRI, TII);
  236. }
  237. } else {
  238. // Emit the copies into the top of the block.
  239. for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
  240. E = MRI.livein_end(); LI != E; ++LI)
  241. if (LI->second) {
  242. const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
  243. bool Emitted = TII.copyRegToReg(*EntryMBB, EntryMBB->begin(),
  244. LI->second, LI->first, RC, RC);
  245. assert(Emitted && "Unable to issue a live-in copy instruction!\n");
  246. (void) Emitted;
  247. }
  248. }
  249. }
  250. //===----------------------------------------------------------------------===//
  251. // SelectionDAGISel code
  252. //===----------------------------------------------------------------------===//
  253. SelectionDAGISel::SelectionDAGISel(TargetMachine &tm, CodeGenOpt::Level OL) :
  254. MachineFunctionPass(&ID), TM(tm), TLI(*tm.getTargetLowering()),
  255. FuncInfo(new FunctionLoweringInfo(TLI)),
  256. CurDAG(new SelectionDAG(TLI, *FuncInfo)),
  257. SDL(new SelectionDAGLowering(*CurDAG, TLI, *FuncInfo, OL)),
  258. GFI(),
  259. OptLevel(OL),
  260. DAGSize(0)
  261. {}
  262. SelectionDAGISel::~SelectionDAGISel() {
  263. delete SDL;
  264. delete CurDAG;
  265. delete FuncInfo;
  266. }
  267. unsigned SelectionDAGISel::MakeReg(EVT VT) {
  268. return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
  269. }
  270. void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
  271. AU.addRequired<AliasAnalysis>();
  272. AU.addPreserved<AliasAnalysis>();
  273. AU.addRequired<GCModuleInfo>();
  274. AU.addPreserved<GCModuleInfo>();
  275. AU.addRequired<DwarfWriter>();
  276. AU.addPreserved<DwarfWriter>();
  277. MachineFunctionPass::getAnalysisUsage(AU);
  278. }
  279. bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
  280. Function &Fn = *mf.getFunction();
  281. // Do some sanity-checking on the command-line options.
  282. assert((!EnableFastISelVerbose || EnableFastISel) &&
  283. "-fast-isel-verbose requires -fast-isel");
  284. assert((!EnableFastISelAbort || EnableFastISel) &&
  285. "-fast-isel-abort requires -fast-isel");
  286. // Get alias analysis for load/store combining.
  287. AA = &getAnalysis<AliasAnalysis>();
  288. MF = &mf;
  289. const TargetInstrInfo &TII = *TM.getInstrInfo();
  290. const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
  291. if (Fn.hasGC())
  292. GFI = &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn);
  293. else
  294. GFI = 0;
  295. RegInfo = &MF->getRegInfo();
  296. DEBUG(errs() << "\n\n\n=== " << Fn.getName() << "\n");
  297. MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
  298. DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
  299. CurDAG->init(*MF, MMI, DW);
  300. FuncInfo->set(Fn, *MF, *CurDAG, EnableFastISel);
  301. SDL->init(GFI, *AA);
  302. for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
  303. if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator()))
  304. // Mark landing pad.
  305. FuncInfo->MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
  306. SelectAllBasicBlocks(Fn, *MF, MMI, DW, TII);
  307. // If the first basic block in the function has live ins that need to be
  308. // copied into vregs, emit the copies into the top of the block before
  309. // emitting the code for the block.
  310. EmitLiveInCopies(MF->begin(), *RegInfo, TRI, TII);
  311. // Add function live-ins to entry block live-in set.
  312. for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(),
  313. E = RegInfo->livein_end(); I != E; ++I)
  314. MF->begin()->addLiveIn(I->first);
  315. #ifndef NDEBUG
  316. assert(FuncInfo->CatchInfoFound.size() == FuncInfo->CatchInfoLost.size() &&
  317. "Not all catch info was assigned to a landing pad!");
  318. #endif
  319. FuncInfo->clear();
  320. return true;
  321. }
  322. static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB,
  323. MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
  324. for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I)
  325. if (EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
  326. // Apply the catch info to DestBB.
  327. AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
  328. #ifndef NDEBUG
  329. if (!FLI.MBBMap[SrcBB]->isLandingPad())
  330. FLI.CatchInfoFound.insert(EHSel);
  331. #endif
  332. }
  333. }
  334. void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB,
  335. BasicBlock::iterator Begin,
  336. BasicBlock::iterator End) {
  337. SDL->setCurrentBasicBlock(BB);
  338. // Lower all of the non-terminator instructions. If a call is emitted
  339. // as a tail call, cease emitting nodes for this block.
  340. for (BasicBlock::iterator I = Begin; I != End && !SDL->HasTailCall; ++I)
  341. if (!isa<TerminatorInst>(I))
  342. SDL->visit(*I);
  343. if (!SDL->HasTailCall) {
  344. // Ensure that all instructions which are used outside of their defining
  345. // blocks are available as virtual registers. Invoke is handled elsewhere.
  346. for (BasicBlock::iterator I = Begin; I != End; ++I)
  347. if (!isa<PHINode>(I) && !isa<InvokeInst>(I))
  348. SDL->CopyToExportRegsIfNeeded(I);
  349. // Handle PHI nodes in successor blocks.
  350. if (End == LLVMBB->end()) {
  351. HandlePHINodesInSuccessorBlocks(LLVMBB);
  352. // Lower the terminator after the copies are emitted.
  353. SDL->visit(*LLVMBB->getTerminator());
  354. }
  355. }
  356. // Make sure the root of the DAG is up-to-date.
  357. CurDAG->setRoot(SDL->getControlRoot());
  358. // Final step, emit the lowered DAG as machine code.
  359. CodeGenAndEmitDAG();
  360. SDL->clear();
  361. }
  362. void SelectionDAGISel::ComputeLiveOutVRegInfo() {
  363. SmallPtrSet<SDNode*, 128> VisitedNodes;
  364. SmallVector<SDNode*, 128> Worklist;
  365. Worklist.push_back(CurDAG->getRoot().getNode());
  366. APInt Mask;
  367. APInt KnownZero;
  368. APInt KnownOne;
  369. while (!Worklist.empty()) {
  370. SDNode *N = Worklist.back();
  371. Worklist.pop_back();
  372. // If we've already seen this node, ignore it.
  373. if (!VisitedNodes.insert(N))
  374. continue;
  375. // Otherwise, add all chain operands to the worklist.
  376. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  377. if (N->getOperand(i).getValueType() == MVT::Other)
  378. Worklist.push_back(N->getOperand(i).getNode());
  379. // If this is a CopyToReg with a vreg dest, process it.
  380. if (N->getOpcode() != ISD::CopyToReg)
  381. continue;
  382. unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
  383. if (!TargetRegisterInfo::isVirtualRegister(DestReg))
  384. continue;
  385. // Ignore non-scalar or non-integer values.
  386. SDValue Src = N->getOperand(2);
  387. EVT SrcVT = Src.getValueType();
  388. if (!SrcVT.isInteger() || SrcVT.isVector())
  389. continue;
  390. unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
  391. Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits());
  392. CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne);
  393. // Only install this information if it tells us something.
  394. if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) {
  395. DestReg -= TargetRegisterInfo::FirstVirtualRegister;
  396. if (DestReg >= FuncInfo->LiveOutRegInfo.size())
  397. FuncInfo->LiveOutRegInfo.resize(DestReg+1);
  398. FunctionLoweringInfo::LiveOutInfo &LOI =
  399. FuncInfo->LiveOutRegInfo[DestReg];
  400. LOI.NumSignBits = NumSignBits;
  401. LOI.KnownOne = KnownOne;
  402. LOI.KnownZero = KnownZero;
  403. }
  404. }
  405. }
  406. void SelectionDAGISel::CodeGenAndEmitDAG() {
  407. std::string GroupName;
  408. if (TimePassesIsEnabled)
  409. GroupName = "Instruction Selection and Scheduling";
  410. std::string BlockName;
  411. if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs ||
  412. ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs ||
  413. ViewSUnitDAGs)
  414. BlockName = MF->getFunction()->getNameStr() + ":" +
  415. BB->getBasicBlock()->getNameStr();
  416. DOUT << "Initial selection DAG:\n";
  417. DEBUG(CurDAG->dump());
  418. if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName);
  419. // Run the DAG combiner in pre-legalize mode.
  420. if (TimePassesIsEnabled) {
  421. NamedRegionTimer T("DAG Combining 1", GroupName);
  422. CurDAG->Combine(Unrestricted, *AA, OptLevel);
  423. } else {
  424. CurDAG->Combine(Unrestricted, *AA, OptLevel);
  425. }
  426. DOUT << "Optimized lowered selection DAG:\n";
  427. DEBUG(CurDAG->dump());
  428. // Second step, hack on the DAG until it only uses operations and types that
  429. // the target supports.
  430. if (!DisableLegalizeTypes) {
  431. if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " +
  432. BlockName);
  433. bool Changed;
  434. if (TimePassesIsEnabled) {
  435. NamedRegionTimer T("Type Legalization", GroupName);
  436. Changed = CurDAG->LegalizeTypes();
  437. } else {
  438. Changed = CurDAG->LegalizeTypes();
  439. }
  440. DOUT << "Type-legalized selection DAG:\n";
  441. DEBUG(CurDAG->dump());
  442. if (Changed) {
  443. if (ViewDAGCombineLT)
  444. CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
  445. // Run the DAG combiner in post-type-legalize mode.
  446. if (TimePassesIsEnabled) {
  447. NamedRegionTimer T("DAG Combining after legalize types", GroupName);
  448. CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
  449. } else {
  450. CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
  451. }
  452. DOUT << "Optimized type-legalized selection DAG:\n";
  453. DEBUG(CurDAG->dump());
  454. }
  455. if (TimePassesIsEnabled) {
  456. NamedRegionTimer T("Vector Legalization", GroupName);
  457. Changed = CurDAG->LegalizeVectors();
  458. } else {
  459. Changed = CurDAG->LegalizeVectors();
  460. }
  461. if (Changed) {
  462. if (TimePassesIsEnabled) {
  463. NamedRegionTimer T("Type Legalization 2", GroupName);
  464. Changed = CurDAG->LegalizeTypes();
  465. } else {
  466. Changed = CurDAG->LegalizeTypes();
  467. }
  468. if (ViewDAGCombineLT)
  469. CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
  470. // Run the DAG combiner in post-type-legalize mode.
  471. if (TimePassesIsEnabled) {
  472. NamedRegionTimer T("DAG Combining after legalize vectors", GroupName);
  473. CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
  474. } else {
  475. CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
  476. }
  477. DOUT << "Optimized vector-legalized selection DAG:\n";
  478. DEBUG(CurDAG->dump());
  479. }
  480. }
  481. if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName);
  482. if (TimePassesIsEnabled) {
  483. NamedRegionTimer T("DAG Legalization", GroupName);
  484. CurDAG->Legalize(DisableLegalizeTypes, OptLevel);
  485. } else {
  486. CurDAG->Legalize(DisableLegalizeTypes, OptLevel);
  487. }
  488. DOUT << "Legalized selection DAG:\n";
  489. DEBUG(CurDAG->dump());
  490. if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName);
  491. // Run the DAG combiner in post-legalize mode.
  492. if (TimePassesIsEnabled) {
  493. NamedRegionTimer T("DAG Combining 2", GroupName);
  494. CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
  495. } else {
  496. CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
  497. }
  498. DOUT << "Optimized legalized selection DAG:\n";
  499. DEBUG(CurDAG->dump());
  500. if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName);
  501. if (OptLevel != CodeGenOpt::None)
  502. ComputeLiveOutVRegInfo();
  503. // Third, instruction select all of the operations to machine code, adding the
  504. // code to the MachineBasicBlock.
  505. if (TimePassesIsEnabled) {
  506. NamedRegionTimer T("Instruction Selection", GroupName);
  507. InstructionSelect();
  508. } else {
  509. InstructionSelect();
  510. }
  511. DOUT << "Selected selection DAG:\n";
  512. DEBUG(CurDAG->dump());
  513. if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName);
  514. // Schedule machine code.
  515. ScheduleDAGSDNodes *Scheduler = CreateScheduler();
  516. if (TimePassesIsEnabled) {
  517. NamedRegionTimer T("Instruction Scheduling", GroupName);
  518. Scheduler->Run(CurDAG, BB, BB->end());
  519. } else {
  520. Scheduler->Run(CurDAG, BB, BB->end());
  521. }
  522. if (ViewSUnitDAGs) Scheduler->viewGraph();
  523. // Emit machine code to BB. This can change 'BB' to the last block being
  524. // inserted into.
  525. if (TimePassesIsEnabled) {
  526. NamedRegionTimer T("Instruction Creation", GroupName);
  527. BB = Scheduler->EmitSchedule();
  528. } else {
  529. BB = Scheduler->EmitSchedule();
  530. }
  531. // Free the scheduler state.
  532. if (TimePassesIsEnabled) {
  533. NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName);
  534. delete Scheduler;
  535. } else {
  536. delete Scheduler;
  537. }
  538. DOUT << "Selected machine code:\n";
  539. DEBUG(BB->dump());
  540. }
  541. void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
  542. MachineFunction &MF,
  543. MachineModuleInfo *MMI,
  544. DwarfWriter *DW,
  545. const TargetInstrInfo &TII) {
  546. // Initialize the Fast-ISel state, if needed.
  547. FastISel *FastIS = 0;
  548. if (EnableFastISel)
  549. FastIS = TLI.createFastISel(MF, MMI, DW,
  550. FuncInfo->ValueMap,
  551. FuncInfo->MBBMap,
  552. FuncInfo->StaticAllocaMap
  553. #ifndef NDEBUG
  554. , FuncInfo->CatchInfoLost
  555. #endif
  556. );
  557. // Iterate over all basic blocks in the function.
  558. for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
  559. BasicBlock *LLVMBB = &*I;
  560. BB = FuncInfo->MBBMap[LLVMBB];
  561. BasicBlock::iterator const Begin = LLVMBB->begin();
  562. BasicBlock::iterator const End = LLVMBB->end();
  563. BasicBlock::iterator BI = Begin;
  564. // Lower any arguments needed in this block if this is the entry block.
  565. bool SuppressFastISel = false;
  566. if (LLVMBB == &Fn.getEntryBlock()) {
  567. LowerArguments(LLVMBB);
  568. // If any of the arguments has the byval attribute, forgo
  569. // fast-isel in the entry block.
  570. if (FastIS) {
  571. unsigned j = 1;
  572. for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
  573. I != E; ++I, ++j)
  574. if (Fn.paramHasAttr(j, Attribute::ByVal)) {
  575. if (EnableFastISelVerbose || EnableFastISelAbort)
  576. cerr << "FastISel skips entry block due to byval argument\n";
  577. SuppressFastISel = true;
  578. break;
  579. }
  580. }
  581. }
  582. if (MMI && BB->isLandingPad()) {
  583. // Add a label to mark the beginning of the landing pad. Deletion of the
  584. // landing pad can thus be detected via the MachineModuleInfo.
  585. unsigned LabelID = MMI->addLandingPad(BB);
  586. const TargetInstrDesc &II = TII.get(TargetInstrInfo::EH_LABEL);
  587. BuildMI(BB, SDL->getCurDebugLoc(), II).addImm(LabelID);
  588. // Mark exception register as live in.
  589. unsigned Reg = TLI.getExceptionAddressRegister();
  590. if (Reg) BB->addLiveIn(Reg);
  591. // Mark exception selector register as live in.
  592. Reg = TLI.getExceptionSelectorRegister();
  593. if (Reg) BB->addLiveIn(Reg);
  594. // FIXME: Hack around an exception handling flaw (PR1508): the personality
  595. // function and list of typeids logically belong to the invoke (or, if you
  596. // like, the basic block containing the invoke), and need to be associated
  597. // with it in the dwarf exception handling tables. Currently however the
  598. // information is provided by an intrinsic (eh.selector) that can be moved
  599. // to unexpected places by the optimizers: if the unwind edge is critical,
  600. // then breaking it can result in the intrinsics being in the successor of
  601. // the landing pad, not the landing pad itself. This results in exceptions
  602. // not being caught because no typeids are associated with the invoke.
  603. // This may not be the only way things can go wrong, but it is the only way
  604. // we try to work around for the moment.
  605. BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator());
  606. if (Br && Br->isUnconditional()) { // Critical edge?
  607. BasicBlock::iterator I, E;
  608. for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I)
  609. if (isa<EHSelectorInst>(I))
  610. break;
  611. if (I == E)
  612. // No catch info found - try to extract some from the successor.
  613. copyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, *FuncInfo);
  614. }
  615. }
  616. // Before doing SelectionDAG ISel, see if FastISel has been requested.
  617. if (FastIS && !SuppressFastISel) {
  618. // Emit code for any incoming arguments. This must happen before
  619. // beginning FastISel on the entry block.
  620. if (LLVMBB == &Fn.getEntryBlock()) {
  621. CurDAG->setRoot(SDL->getControlRoot());
  622. CodeGenAndEmitDAG();
  623. SDL->clear();
  624. }
  625. FastIS->startNewBlock(BB);
  626. // Do FastISel on as many instructions as possible.
  627. for (; BI != End; ++BI) {
  628. // Just before the terminator instruction, insert instructions to
  629. // feed PHI nodes in successor blocks.
  630. if (isa<TerminatorInst>(BI))
  631. if (!HandlePHINodesInSuccessorBlocksFast(LLVMBB, FastIS)) {
  632. if (EnableFastISelVerbose || EnableFastISelAbort) {
  633. cerr << "FastISel miss: ";
  634. BI->dump();
  635. }
  636. assert(!EnableFastISelAbort &&
  637. "FastISel didn't handle a PHI in a successor");
  638. break;
  639. }
  640. // First try normal tablegen-generated "fast" selection.
  641. if (FastIS->SelectInstruction(BI))
  642. continue;
  643. // Next, try calling the target to attempt to handle the instruction.
  644. if (FastIS->TargetSelectInstruction(BI))
  645. continue;
  646. // Then handle certain instructions as single-LLVM-Instruction blocks.
  647. if (isa<CallInst>(BI)) {
  648. if (EnableFastISelVerbose || EnableFastISelAbort) {
  649. cerr << "FastISel missed call: ";
  650. BI->dump();
  651. }
  652. if (BI->getType() != Type::getVoidTy(*CurDAG->getContext())) {
  653. unsigned &R = FuncInfo->ValueMap[BI];
  654. if (!R)
  655. R = FuncInfo->CreateRegForValue(BI);
  656. }
  657. SDL->setCurDebugLoc(FastIS->getCurDebugLoc());
  658. SelectBasicBlock(LLVMBB, BI, next(BI));
  659. // If the instruction was codegen'd with multiple blocks,
  660. // inform the FastISel object where to resume inserting.
  661. FastIS->setCurrentBlock(BB);
  662. continue;
  663. }
  664. // Otherwise, give up on FastISel for the rest of the block.
  665. // For now, be a little lenient about non-branch terminators.
  666. if (!isa<TerminatorInst>(BI) || isa<BranchInst>(BI)) {
  667. if (EnableFastISelVerbose || EnableFastISelAbort) {
  668. cerr << "FastISel miss: ";
  669. BI->dump();
  670. }
  671. if (EnableFastISelAbort)
  672. // The "fast" selector couldn't handle something and bailed.
  673. // For the purpose of debugging, just abort.
  674. llvm_unreachable("FastISel didn't select the entire block");
  675. }
  676. break;
  677. }
  678. }
  679. // Run SelectionDAG instruction selection on the remainder of the block
  680. // not handled by FastISel. If FastISel is not run, this is the entire
  681. // block.
  682. if (BI != End) {
  683. // If FastISel is run and it has known DebugLoc then use it.
  684. if (FastIS && !FastIS->getCurDebugLoc().isUnknown())
  685. SDL->setCurDebugLoc(FastIS->getCurDebugLoc());
  686. SelectBasicBlock(LLVMBB, BI, End);
  687. }
  688. FinishBasicBlock();
  689. }
  690. delete FastIS;
  691. }
  692. void
  693. SelectionDAGISel::FinishBasicBlock() {
  694. DOUT << "Target-post-processed machine code:\n";
  695. DEBUG(BB->dump());
  696. DOUT << "Total amount of phi nodes to update: "
  697. << SDL->PHINodesToUpdate.size() << "\n";
  698. DEBUG(for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i)
  699. DOUT << "Node " << i << " : (" << SDL->PHINodesToUpdate[i].first
  700. << ", " << SDL->PHINodesToUpdate[i].second << ")\n";);
  701. // Next, now that we know what the last MBB the LLVM BB expanded is, update
  702. // PHI nodes in successors.
  703. if (SDL->SwitchCases.empty() &&
  704. SDL->JTCases.empty() &&
  705. SDL->BitTestCases.empty()) {
  706. for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) {
  707. MachineInstr *PHI = SDL->PHINodesToUpdate[i].first;
  708. assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
  709. "This is not a machine PHI node that we are updating!");
  710. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[i].second,
  711. false));
  712. PHI->addOperand(MachineOperand::CreateMBB(BB));
  713. }
  714. SDL->PHINodesToUpdate.clear();
  715. return;
  716. }
  717. for (unsigned i = 0, e = SDL->BitTestCases.size(); i != e; ++i) {
  718. // Lower header first, if it wasn't already lowered
  719. if (!SDL->BitTestCases[i].Emitted) {
  720. // Set the current basic block to the mbb we wish to insert the code into
  721. BB = SDL->BitTestCases[i].Parent;
  722. SDL->setCurrentBasicBlock(BB);
  723. // Emit the code
  724. SDL->visitBitTestHeader(SDL->BitTestCases[i]);
  725. CurDAG->setRoot(SDL->getRoot());
  726. CodeGenAndEmitDAG();
  727. SDL->clear();
  728. }
  729. for (unsigned j = 0, ej = SDL->BitTestCases[i].Cases.size(); j != ej; ++j) {
  730. // Set the current basic block to the mbb we wish to insert the code into
  731. BB = SDL->BitTestCases[i].Cases[j].ThisBB;
  732. SDL->setCurrentBasicBlock(BB);
  733. // Emit the code
  734. if (j+1 != ej)
  735. SDL->visitBitTestCase(SDL->BitTestCases[i].Cases[j+1].ThisBB,
  736. SDL->BitTestCases[i].Reg,
  737. SDL->BitTestCases[i].Cases[j]);
  738. else
  739. SDL->visitBitTestCase(SDL->BitTestCases[i].Default,
  740. SDL->BitTestCases[i].Reg,
  741. SDL->BitTestCases[i].Cases[j]);
  742. CurDAG->setRoot(SDL->getRoot());
  743. CodeGenAndEmitDAG();
  744. SDL->clear();
  745. }
  746. // Update PHI Nodes
  747. for (unsigned pi = 0, pe = SDL->PHINodesToUpdate.size(); pi != pe; ++pi) {
  748. MachineInstr *PHI = SDL->PHINodesToUpdate[pi].first;
  749. MachineBasicBlock *PHIBB = PHI->getParent();
  750. assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
  751. "This is not a machine PHI node that we are updating!");
  752. // This is "default" BB. We have two jumps to it. From "header" BB and
  753. // from last "case" BB.
  754. if (PHIBB == SDL->BitTestCases[i].Default) {
  755. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
  756. false));
  757. PHI->addOperand(MachineOperand::CreateMBB(SDL->BitTestCases[i].Parent));
  758. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
  759. false));
  760. PHI->addOperand(MachineOperand::CreateMBB(SDL->BitTestCases[i].Cases.
  761. back().ThisBB));
  762. }
  763. // One of "cases" BB.
  764. for (unsigned j = 0, ej = SDL->BitTestCases[i].Cases.size();
  765. j != ej; ++j) {
  766. MachineBasicBlock* cBB = SDL->BitTestCases[i].Cases[j].ThisBB;
  767. if (cBB->succ_end() !=
  768. std::find(cBB->succ_begin(),cBB->succ_end(), PHIBB)) {
  769. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
  770. false));
  771. PHI->addOperand(MachineOperand::CreateMBB(cBB));
  772. }
  773. }
  774. }
  775. }
  776. SDL->BitTestCases.clear();
  777. // If the JumpTable record is filled in, then we need to emit a jump table.
  778. // Updating the PHI nodes is tricky in this case, since we need to determine
  779. // whether the PHI is a successor of the range check MBB or the jump table MBB
  780. for (unsigned i = 0, e = SDL->JTCases.size(); i != e; ++i) {
  781. // Lower header first, if it wasn't already lowered
  782. if (!SDL->JTCases[i].first.Emitted) {
  783. // Set the current basic block to the mbb we wish to insert the code into
  784. BB = SDL->JTCases[i].first.HeaderBB;
  785. SDL->setCurrentBasicBlock(BB);
  786. // Emit the code
  787. SDL->visitJumpTableHeader(SDL->JTCases[i].second, SDL->JTCases[i].first);
  788. CurDAG->setRoot(SDL->getRoot());
  789. CodeGenAndEmitDAG();
  790. SDL->clear();
  791. }
  792. // Set the current basic block to the mbb we wish to insert the code into
  793. BB = SDL->JTCases[i].second.MBB;
  794. SDL->setCurrentBasicBlock(BB);
  795. // Emit the code
  796. SDL->visitJumpTable(SDL->JTCases[i].second);
  797. CurDAG->setRoot(SDL->getRoot());
  798. CodeGenAndEmitDAG();
  799. SDL->clear();
  800. // Update PHI Nodes
  801. for (unsigned pi = 0, pe = SDL->PHINodesToUpdate.size(); pi != pe; ++pi) {
  802. MachineInstr *PHI = SDL->PHINodesToUpdate[pi].first;
  803. MachineBasicBlock *PHIBB = PHI->getParent();
  804. assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
  805. "This is not a machine PHI node that we are updating!");
  806. // "default" BB. We can go there only from header BB.
  807. if (PHIBB == SDL->JTCases[i].second.Default) {
  808. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
  809. false));
  810. PHI->addOperand(MachineOperand::CreateMBB(SDL->JTCases[i].first.HeaderBB));
  811. }
  812. // JT BB. Just iterate over successors here
  813. if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) {
  814. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
  815. false));
  816. PHI->addOperand(MachineOperand::CreateMBB(BB));
  817. }
  818. }
  819. }
  820. SDL->JTCases.clear();
  821. // If the switch block involved a branch to one of the actual successors, we
  822. // need to update PHI nodes in that block.
  823. for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) {
  824. MachineInstr *PHI = SDL->PHINodesToUpdate[i].first;
  825. assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
  826. "This is not a machine PHI node that we are updating!");
  827. if (BB->isSuccessor(PHI->getParent())) {
  828. PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[i].second,
  829. false));
  830. PHI->addOperand(MachineOperand::CreateMBB(BB));
  831. }
  832. }
  833. // If we generated any switch lowering information, build and codegen any
  834. // additional DAGs necessary.
  835. for (unsigned i = 0, e = SDL->SwitchCases.size(); i != e; ++i) {
  836. // Set the current basic block to the mbb we wish to insert the code into
  837. BB = SDL->SwitchCases[i].ThisBB;
  838. SDL->setCurrentBasicBlock(BB);
  839. // Emit the code
  840. SDL->visitSwitchCase(SDL->SwitchCases[i]);
  841. CurDAG->setRoot(SDL->getRoot());
  842. CodeGenAndEmitDAG();
  843. SDL->clear();
  844. // Handle any PHI nodes in successors of this chunk, as if we were coming
  845. // from the original BB before switch expansion. Note that PHI nodes can
  846. // occur multiple times in PHINodesToUpdate. We have to be very careful to
  847. // handle them the right number of times.
  848. while ((BB = SDL->SwitchCases[i].TrueBB)) { // Handle LHS and RHS.
  849. for (MachineBasicBlock::iterator Phi = BB->begin();
  850. Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){
  851. // This value for this PHI node is recorded in PHINodesToUpdate, get it.
  852. for (unsigned pn = 0; ; ++pn) {
  853. assert(pn != SDL->PHINodesToUpdate.size() &&
  854. "Didn't find PHI entry!");
  855. if (SDL->PHINodesToUpdate[pn].first == Phi) {
  856. Phi->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pn].
  857. second, false));
  858. Phi->addOperand(MachineOperand::CreateMBB(SDL->SwitchCases[i].ThisBB));
  859. break;
  860. }
  861. }
  862. }
  863. // Don't process RHS if same block as LHS.
  864. if (BB == SDL->SwitchCases[i].FalseBB)
  865. SDL->SwitchCases[i].FalseBB = 0;
  866. // If we haven't handled the RHS, do so now. Otherwise, we're done.
  867. SDL->SwitchCases[i].TrueBB = SDL->SwitchCases[i].FalseBB;
  868. SDL->SwitchCases[i].FalseBB = 0;
  869. }
  870. assert(SDL->SwitchCases[i].TrueBB == 0 && SDL->SwitchCases[i].FalseBB == 0);
  871. }
  872. SDL->SwitchCases.clear();
  873. SDL->PHINodesToUpdate.clear();
  874. }
  875. /// Create the scheduler. If a specific scheduler was specified
  876. /// via the SchedulerRegistry, use it, otherwise select the
  877. /// one preferred by the target.
  878. ///
  879. ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
  880. RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
  881. if (!Ctor) {
  882. Ctor = ISHeuristic;
  883. RegisterScheduler::setDefault(Ctor);
  884. }
  885. return Ctor(this, OptLevel);
  886. }
  887. ScheduleHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
  888. return new ScheduleHazardRecognizer();
  889. }
  890. //===----------------------------------------------------------------------===//
  891. // Helper functions used by the generated instruction selector.
  892. //===----------------------------------------------------------------------===//
  893. // Calls to these methods are generated by tblgen.
  894. /// CheckAndMask - The isel is trying to match something like (and X, 255). If
  895. /// the dag combiner simplified the 255, we still want to match. RHS is the
  896. /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
  897. /// specified in the .td file (e.g. 255).
  898. bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
  899. int64_t DesiredMaskS) const {
  900. const APInt &ActualMask = RHS->getAPIntValue();
  901. const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
  902. // If the actual mask exactly matches, success!
  903. if (ActualMask == DesiredMask)
  904. return true;
  905. // If the actual AND mask is allowing unallowed bits, this doesn't match.
  906. if (ActualMask.intersects(~DesiredMask))
  907. return false;
  908. // Otherwise, the DAG Combiner may have proven that the value coming in is
  909. // either already zero or is not demanded. Check for known zero input bits.
  910. APInt NeededMask = DesiredMask & ~ActualMask;
  911. if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
  912. return true;
  913. // TODO: check to see if missing bits are just not demanded.
  914. // Otherwise, this pattern doesn't match.
  915. return false;
  916. }
  917. /// CheckOrMask - The isel is trying to match something like (or X, 255). If
  918. /// the dag combiner simplified the 255, we still want to match. RHS is the
  919. /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
  920. /// specified in the .td file (e.g. 255).
  921. bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
  922. int64_t DesiredMaskS) const {
  923. const APInt &ActualMask = RHS->getAPIntValue();
  924. const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
  925. // If the actual mask exactly matches, success!
  926. if (ActualMask == DesiredMask)
  927. return true;
  928. // If the actual AND mask is allowing unallowed bits, this doesn't match.
  929. if (ActualMask.intersects(~DesiredMask))
  930. return false;
  931. // Otherwise, the DAG Combiner may have proven that the value coming in is
  932. // either already zero or is not demanded. Check for known zero input bits.
  933. APInt NeededMask = DesiredMask & ~ActualMask;
  934. APInt KnownZero, KnownOne;
  935. CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
  936. // If all the missing bits in the or are already known to be set, match!
  937. if ((NeededMask & KnownOne) == NeededMask)
  938. return true;
  939. // TODO: check to see if missing bits are just not demanded.
  940. // Otherwise, this pattern doesn't match.
  941. return false;
  942. }
  943. /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
  944. /// by tblgen. Others should not call it.
  945. void SelectionDAGISel::
  946. SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) {
  947. std::vector<SDValue> InOps;
  948. std::swap(InOps, Ops);
  949. Ops.push_back(InOps[0]); // input chain.
  950. Ops.push_back(InOps[1]); // input asm string.
  951. unsigned i = 2, e = InOps.size();
  952. if (InOps[e-1].getValueType() == MVT::Flag)
  953. --e; // Don't process a flag operand if it is here.
  954. while (i != e) {
  955. unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
  956. if ((Flags & 7) != 4 /*MEM*/) {
  957. // Just skip over this operand, copying the operands verbatim.
  958. Ops.insert(Ops.end(), InOps.begin()+i,
  959. InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
  960. i += InlineAsm::getNumOperandRegisters(Flags) + 1;
  961. } else {
  962. assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
  963. "Memory operand with multiple values?");
  964. // Otherwise, this is a memory operand. Ask the target to select it.
  965. std::vector<SDValue> SelOps;
  966. if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps)) {
  967. llvm_report_error("Could not match memory address. Inline asm"
  968. " failure!");
  969. }
  970. // Add this to the output node.
  971. EVT IntPtrTy = TLI.getPointerTy();
  972. Ops.push_back(CurDAG->getTargetConstant(4/*MEM*/ | (SelOps.size()<< 3),
  973. IntPtrTy));
  974. Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
  975. i += 2;
  976. }
  977. }
  978. // Add the flag input back if present.
  979. if (e != InOps.size())
  980. Ops.push_back(InOps.back());
  981. }
  982. /// findFlagUse - Return use of EVT::Flag value produced by the specified
  983. /// SDNode.
  984. ///
  985. static SDNode *findFlagUse(SDNode *N) {
  986. unsigned FlagResNo = N->getNumValues()-1;
  987. for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
  988. SDUse &Use = I.getUse();
  989. if (Use.getResNo() == FlagResNo)
  990. return Use.getUser();
  991. }
  992. return NULL;
  993. }
  994. /// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
  995. /// This function recursively traverses up the operand chain, ignoring
  996. /// certain nodes.
  997. static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
  998. SDNode *Root,
  999. SmallPtrSet<SDNode*, 16> &Visited) {
  1000. if (Use->getNodeId() < Def->getNodeId() ||
  1001. !Visited.insert(Use))
  1002. return false;
  1003. for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
  1004. SDNode *N = Use->getOperand(i).getNode();
  1005. if (N == Def) {
  1006. if (Use == ImmedUse || Use == Root)
  1007. continue; // We are not looking for immediate use.
  1008. assert(N != Root);
  1009. return true;
  1010. }
  1011. // Traverse up the operand chain.
  1012. if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
  1013. return true;
  1014. }
  1015. return false;
  1016. }
  1017. /// isNonImmUse - Start searching from Root up the DAG to check is Def can
  1018. /// be reached. Return true if that's the case. However, ignore direct uses
  1019. /// by ImmedUse (which would be U in the example illustrated in
  1020. /// IsLegalAndProfitableToFold) and by Root (which can happen in the store
  1021. /// case).
  1022. /// FIXME: to be really generic, we should allow direct use by any node
  1023. /// that is being folded. But realisticly since we only fold loads which
  1024. /// have one non-chain use, we only need to watch out for load/op/store
  1025. /// and load/op/cmp case where the root (store / cmp) may reach the load via
  1026. /// its chain operand.
  1027. static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
  1028. SmallPtrSet<SDNode*, 16> Visited;
  1029. return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
  1030. }
  1031. /// IsLegalAndProfitableToFold - Returns true if the specific operand node N of
  1032. /// U can be folded during instruction selection that starts at Root and
  1033. /// folding N is profitable.
  1034. bool SelectionDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
  1035. SDNode *Root) const {
  1036. if (OptLevel == CodeGenOpt::None) return false;
  1037. // If Root use can somehow reach N through a path that that doesn't contain
  1038. // U then folding N would create a cycle. e.g. In the following
  1039. // diagram, Root can reach N through X. If N is folded into into Root, then
  1040. // X is both a predecessor and a successor of U.
  1041. //
  1042. // [N*] //
  1043. // ^ ^ //
  1044. // / \ //
  1045. // [U*] [X]? //
  1046. // ^ ^ //
  1047. // \ / //
  1048. // \ / //
  1049. // [Root*] //
  1050. //
  1051. // * indicates nodes to be folded together.
  1052. //
  1053. // If Root produces a flag, then it gets (even more) interesting. Since it
  1054. // will be "glued" together with its flag use in the scheduler, we need to
  1055. // check if it might reach N.
  1056. //
  1057. // [N*] //
  1058. // ^ ^ //
  1059. // / \ //
  1060. // [U*] [X]? //
  1061. // ^ ^ //
  1062. // \ \ //
  1063. // \ | //
  1064. // [Root*] | //
  1065. // ^ | //
  1066. // f | //
  1067. // | / //
  1068. // [Y] / //
  1069. // ^ / //
  1070. // f / //
  1071. // | / //
  1072. // [FU] //
  1073. //
  1074. // If FU (flag use) indirectly reaches N (the load), and Root folds N
  1075. // (call it Fold), then X is a predecessor of FU and a successor of
  1076. // Fold. But since Fold and FU are flagged together, this will create
  1077. // a cycle in the scheduling graph.
  1078. EVT VT = Root->getValueType(Root->getNumValues()-1);
  1079. while (VT == MVT::Flag) {
  1080. SDNode *FU = findFlagUse(Root);
  1081. if (FU == NULL)
  1082. break;
  1083. Root = FU;
  1084. VT = Root->getValueType(Root->getNumValues()-1);
  1085. }
  1086. return !isNonImmUse(Root, N, U);
  1087. }
  1088. char SelectionDAGISel::ID = 0;