SelectionDAGISel.cpp 45 KB

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