SelectionDAGISel.cpp revision cfe2eab7446dedc471592fe702fefef783383171
11c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
2edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman//
31c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//                     The LLVM Compiler Infrastructure
41c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman//
81c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===----------------------------------------------------------------------===//
91c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//
101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner// This implements the SelectionDAGISel class.
111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//
121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===----------------------------------------------------------------------===//
131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#define DEBUG_TYPE "isel"
1584fbac580941548a6ab1121ed3b0ffdc4e2bc080Dan Gohman#include "ScheduleDAGSDNodes.h"
162048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman#include "SelectionDAGBuilder.h"
176277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "FunctionLoweringInfo.h"
1884fbac580941548a6ab1121ed3b0ffdc4e2bc080Dan Gohman#include "llvm/CodeGen/SelectionDAGISel.h"
19c7c3f110eda0ff8040e4bd99e38d3112b910810fJim Laskey#include "llvm/Analysis/AliasAnalysis.h"
20713f043c16220b746804f668a72f4e0344cbe4d1Devang Patel#include "llvm/Analysis/DebugInfo.h"
215502bf67cd49221583c15472150905ce13184d36Anton Korobeynikov#include "llvm/Constants.h"
22adf6a965a321372c640845407195594835921eb4Chris Lattner#include "llvm/CallingConv.h"
231c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/DerivedTypes.h"
241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Function.h"
2536ce69195ed488034d0bb11180cc2ebd923679c8Chris Lattner#include "llvm/GlobalVariable.h"
26ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner#include "llvm/InlineAsm.h"
271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Instructions.h"
281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Intrinsics.h"
2943970fec322d9e0153ca513de41d80af1c79bddeJim Laskey#include "llvm/IntrinsicInst.h"
3075c478a96a542dc386152e00518441e63d22058dChris Lattner#include "llvm/LLVMContext.h"
3178eca170e9ac5db7fd525f9bbf27090fefcbb646Dan Gohman#include "llvm/CodeGen/FastISel.h"
325a29c9eed157af51a8d338b5a225b146881819e8Gordon Henriksen#include "llvm/CodeGen/GCStrategy.h"
335eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen#include "llvm/CodeGen/GCMetadata.h"
341c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/MachineFunction.h"
35ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohman#include "llvm/CodeGen/MachineFunctionAnalysis.h"
361c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/MachineFrameInfo.h"
371c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/MachineInstrBuilder.h"
3884bc5427d6883f73cfeae3da640acd011d35c006Chris Lattner#include "llvm/CodeGen/MachineJumpTableInfo.h"
3984bc5427d6883f73cfeae3da640acd011d35c006Chris Lattner#include "llvm/CodeGen/MachineModuleInfo.h"
4084bc5427d6883f73cfeae3da640acd011d35c006Chris Lattner#include "llvm/CodeGen/MachineRegisterInfo.h"
41fc54c552963545a81e4ea38e60460590afb2d5aeDan Gohman#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
42eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey#include "llvm/CodeGen/SchedulerRegistry.h"
431c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/SelectionDAG.h"
446e7a1617ac4a34792d9097b8d3644b72f57a45f7Devang Patel#include "llvm/CodeGen/DwarfWriter.h"
456f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman#include "llvm/Target/TargetRegisterInfo.h"
461c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetData.h"
471c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetFrameInfo.h"
48e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman#include "llvm/Target/TargetIntrinsicInfo.h"
491c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetInstrInfo.h"
501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetLowering.h"
511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetMachine.h"
52124729155d860a0850eb4ef2a7cea4145e13a057Vladimir Prus#include "llvm/Target/TargetOptions.h"
53a4f0b3a084d120cfc5b5bb06f64b222f5cb72740Chris Lattner#include "llvm/Support/Compiler.h"
54db8d56b825efeb576d67b9dbe39d736d93306222Evan Cheng#include "llvm/Support/Debug.h"
557d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin#include "llvm/Support/ErrorHandling.h"
56db8d56b825efeb576d67b9dbe39d736d93306222Evan Cheng#include "llvm/Support/MathExtras.h"
57db8d56b825efeb576d67b9dbe39d736d93306222Evan Cheng#include "llvm/Support/Timer.h"
58ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h"
597e88103cdea8c36b2229dae8c60def14e3816512Jeff Cohen#include <algorithm>
601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerusing namespace llvm;
611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
62ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattnerstatic cl::opt<bool>
63293d5f8bc02a9148238b2c79484637f56de65b9cDan GohmanEnableFastISelVerbose("fast-isel-verbose", cl::Hidden,
64d659d50482e2f9edf8f436e4a1aa30c68c6d020bDan Gohman          cl::desc("Enable verbose messages in the \"fast\" "
65293d5f8bc02a9148238b2c79484637f56de65b9cDan Gohman                   "instruction selector"));
66293d5f8bc02a9148238b2c79484637f56de65b9cDan Gohmanstatic cl::opt<bool>
674344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan GohmanEnableFastISelAbort("fast-isel-abort", cl::Hidden,
684344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman          cl::desc("Enable abort calls when \"fast\" instruction fails"));
698a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohmanstatic cl::opt<bool>
70df8ed024735b651d6e0082262d4e7a00b2fa4d32Evan ChengSchedLiveInCopies("schedule-livein-copies", cl::Hidden,
718a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                  cl::desc("Schedule copies of livein registers"),
728a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                  cl::init(false));
73ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner
74da8abb02397d7cd62a1e16e7c534edd7096ac873Chris Lattner#ifndef NDEBUG
757944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattnerstatic cl::opt<bool>
76462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan GohmanViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
77462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman          cl::desc("Pop up a window to show dags before the first "
78462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman                   "dag combine pass"));
79462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohmanstatic cl::opt<bool>
80462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan GohmanViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
81462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman          cl::desc("Pop up a window to show dags before legalize types"));
82462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohmanstatic cl::opt<bool>
83462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan GohmanViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
84462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman          cl::desc("Pop up a window to show dags before legalize"));
85462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohmanstatic cl::opt<bool>
86462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan GohmanViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
87462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman          cl::desc("Pop up a window to show dags before the second "
88462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman                   "dag combine pass"));
89462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohmanstatic cl::opt<bool>
9025cf2275ff7de3de3bc0e508abaf457413d74725Duncan SandsViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
9125cf2275ff7de3de3bc0e508abaf457413d74725Duncan Sands          cl::desc("Pop up a window to show dags before the post legalize types"
9225cf2275ff7de3de3bc0e508abaf457413d74725Duncan Sands                   " dag combine pass"));
9325cf2275ff7de3de3bc0e508abaf457413d74725Duncan Sandsstatic cl::opt<bool>
94a9c2091cd38e401c846391c9951ff416e709b65eEvan ChengViewISelDAGs("view-isel-dags", cl::Hidden,
95a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng          cl::desc("Pop up a window to show isel dags as they are selected"));
96a9c2091cd38e401c846391c9951ff416e709b65eEvan Chengstatic cl::opt<bool>
97a9c2091cd38e401c846391c9951ff416e709b65eEvan ChengViewSchedDAGs("view-sched-dags", cl::Hidden,
98a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng          cl::desc("Pop up a window to show sched dags as they are processed"));
993e1a7aef17575d9c7058a035449d57e3c7295ed0Dan Gohmanstatic cl::opt<bool>
1003e1a7aef17575d9c7058a035449d57e3c7295ed0Dan GohmanViewSUnitDAGs("view-sunit-dags", cl::Hidden,
1015bab78527a946632cd9614daa0b9a82ee7b5e1ccChris Lattner      cl::desc("Pop up a window to show SUnit dags after they are processed"));
1027944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner#else
103462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohmanstatic const bool ViewDAGCombine1 = false,
104462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman                  ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false,
105462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman                  ViewDAGCombine2 = false,
10625cf2275ff7de3de3bc0e508abaf457413d74725Duncan Sands                  ViewDAGCombineLT = false,
107462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman                  ViewISelDAGs = false, ViewSchedDAGs = false,
108462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman                  ViewSUnitDAGs = false;
1097944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner#endif
1107944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner
111eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey//===---------------------------------------------------------------------===//
112eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey///
113eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey/// RegisterScheduler class - Track the registration of instruction schedulers.
114eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey///
115eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey//===---------------------------------------------------------------------===//
116eb577ba3b815a1fa4627b060dd2345d17abf672dJim LaskeyMachinePassRegistry RegisterScheduler::Registry;
117eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey
118eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey//===---------------------------------------------------------------------===//
119eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey///
120eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey/// ISHeuristic command line option for instruction schedulers.
121eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey///
122eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey//===---------------------------------------------------------------------===//
123844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic cl::opt<RegisterScheduler::FunctionPassCtor, false,
124844731a7f1909f55935e3514c9e713a62d67662eDan Gohman               RegisterPassParser<RegisterScheduler> >
125844731a7f1909f55935e3514c9e713a62d67662eDan GohmanISHeuristic("pre-RA-sched",
126844731a7f1909f55935e3514c9e713a62d67662eDan Gohman            cl::init(&createDefaultScheduler),
127844731a7f1909f55935e3514c9e713a62d67662eDan Gohman            cl::desc("Instruction schedulers available (before register"
128844731a7f1909f55935e3514c9e713a62d67662eDan Gohman                     " allocation):"));
12913ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey
130844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterScheduler
131b8cab9227a0f6ffbdaae33e3c64268e265008a6aDan GohmandefaultListDAGScheduler("default", "Best scheduler for the target",
132844731a7f1909f55935e3514c9e713a62d67662eDan Gohman                        createDefaultScheduler);
1334ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng
134f0cbcd48804961b05359ee41859bbd7774f41fe0Dan Gohmannamespace llvm {
135f0cbcd48804961b05359ee41859bbd7774f41fe0Dan Gohman  //===--------------------------------------------------------------------===//
136f0cbcd48804961b05359ee41859bbd7774f41fe0Dan Gohman  /// createDefaultScheduler - This creates an instruction scheduler appropriate
137f0cbcd48804961b05359ee41859bbd7774f41fe0Dan Gohman  /// for the target.
13847ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman  ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
13998a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                             CodeGenOpt::Level OptLevel) {
140e9530ecae4897fe8157bd4d7616043bd8c0484e2Dan Gohman    const TargetLowering &TLI = IS->getTargetLowering();
141e9530ecae4897fe8157bd4d7616043bd8c0484e2Dan Gohman
14298a366d547772010e94609e4584489b3e5ce0043Bill Wendling    if (OptLevel == CodeGenOpt::None)
143be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling      return createFastDAGScheduler(IS, OptLevel);
1449e76fea3abd4229749e6ead46a0016cabff4a056Dan Gohman    if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
145be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling      return createTDListDAGScheduler(IS, OptLevel);
1469e76fea3abd4229749e6ead46a0016cabff4a056Dan Gohman    assert(TLI.getSchedulingPreference() ==
147187361b056823df4ff292561fe47468dad956872Bill Wendling           TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
148be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling    return createBURRListDAGScheduler(IS, OptLevel);
1493a508c94a6c6f124e8b0549358ce76deb92ab662Chris Lattner  }
1501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
1511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
152ff9b373e8f5006c629af81e2619778b4c4f5249eEvan Cheng// EmitInstrWithCustomInserter - This method should be implemented by targets
153533297b58da8c74bec65551e1aface9801fc2259Dan Gohman// that mark instructions with the 'usesCustomInserter' flag.  These
154025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// instructions are special in various ways, which require special support to
155025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// insert.  The specified MachineInstr is created but not inserted into any
156533297b58da8c74bec65551e1aface9801fc2259Dan Gohman// basic blocks, and this method is called to expand it into a sequence of
157533297b58da8c74bec65551e1aface9801fc2259Dan Gohman// instructions, potentially also creating new basic blocks and control flow.
158533297b58da8c74bec65551e1aface9801fc2259Dan Gohman// When new basic blocks are inserted and the edges from MBB to its successors
159533297b58da8c74bec65551e1aface9801fc2259Dan Gohman// are modified, the method should insert pairs of <OldSucc, NewSucc> into the
160533297b58da8c74bec65551e1aface9801fc2259Dan Gohman// DenseMap.
161ff9b373e8f5006c629af81e2619778b4c4f5249eEvan ChengMachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
162fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng                                                         MachineBasicBlock *MBB,
163fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng                   DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
164f36892335b4919b9120e48a792e6b3630b9de978Torok Edwin#ifndef NDEBUG
1651a053236f165ac60ee88edca370a732d278aa2c1David Greene  dbgs() << "If a target marks an instruction with "
166533297b58da8c74bec65551e1aface9801fc2259Dan Gohman          "'usesCustomInserter', it must implement "
167f36892335b4919b9120e48a792e6b3630b9de978Torok Edwin          "TargetLowering::EmitInstrWithCustomInserter!";
168f36892335b4919b9120e48a792e6b3630b9de978Torok Edwin#endif
169c23197a26f34f559ea9797de51e187087c039c42Torok Edwin  llvm_unreachable(0);
170a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  return 0;
171025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner}
172025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner
1738a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
1748a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman/// physical register has only a single copy use, then coalesced the copy
1758a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman/// if possible.
1768a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohmanstatic void EmitLiveInCopy(MachineBasicBlock *MBB,
1778a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           MachineBasicBlock::iterator &InsertPos,
1788a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           unsigned VirtReg, unsigned PhysReg,
1798a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           const TargetRegisterClass *RC,
1808a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           DenseMap<MachineInstr*, unsigned> &CopyRegMap,
1818a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           const MachineRegisterInfo &MRI,
1828a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           const TargetRegisterInfo &TRI,
1838a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                           const TargetInstrInfo &TII) {
1848a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  unsigned NumUses = 0;
1858a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  MachineInstr *UseMI = NULL;
1868a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1878a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman         UE = MRI.use_end(); UI != UE; ++UI) {
1888a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    UseMI = &*UI;
1898a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    if (++NumUses > 1)
1908a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      break;
1918a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  }
1928a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
1938a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // If the number of uses is not one, or the use is not a move instruction,
1948a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // don't coalesce. Also, only coalesce away a virtual register to virtual
1958a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // register copy.
1968a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  bool Coalesced = false;
19704ee5a1d9267e5e6fab8f088095fcb83c3c5cbd1Evan Cheng  unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1988a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  if (NumUses == 1 &&
19904ee5a1d9267e5e6fab8f088095fcb83c3c5cbd1Evan Cheng      TII.isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
2008a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      TargetRegisterInfo::isVirtualRegister(DstReg)) {
2018a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    VirtReg = DstReg;
2028a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    Coalesced = true;
2038a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  }
2048a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
2058a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // Now find an ideal location to insert the copy.
2068a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  MachineBasicBlock::iterator Pos = InsertPos;
2078a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  while (Pos != MBB->begin()) {
2088a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    MachineInstr *PrevMI = prior(Pos);
2098a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
2108a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    // copyRegToReg might emit multiple instructions to do a copy.
2118a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
2128a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    if (CopyDstReg && !TRI.regsOverlap(CopyDstReg, PhysReg))
2138a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // This is what the BB looks like right now:
2148a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // r1024 = mov r0
2158a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // ...
2168a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // r1    = mov r1024
2178a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      //
2188a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // We want to insert "r1025 = mov r1". Inserting this copy below the
2198a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // move to r1024 makes it impossible for that move to be coalesced.
2208a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      //
2218a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // r1025 = mov r1
2228a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // r1024 = mov r0
2238a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // ...
2248a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // r1    = mov 1024
2258a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      // r2    = mov 1025
2268a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      break; // Woot! Found a good location.
2278a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    --Pos;
2288a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  }
2298a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
230f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin  bool Emitted = TII.copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
231f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin  assert(Emitted && "Unable to issue a live-in copy instruction!\n");
232f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin  (void) Emitted;
233a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
234931424a7d8b8b15ddda7308d6c5b08277d8bdc0eZhongxing Xu  CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
2358a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  if (Coalesced) {
2368a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    if (&*InsertPos == UseMI) ++InsertPos;
2378a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    MBB->erase(UseMI);
2388a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  }
2398a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman}
2408a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
2418a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman/// EmitLiveInCopies - If this is the first basic block in the function,
2428a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman/// and if it has live ins that need to be copied into vregs, emit the
2438a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman/// copies into the block.
2448a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohmanstatic void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
2458a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                             const MachineRegisterInfo &MRI,
2468a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                             const TargetRegisterInfo &TRI,
2478a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                             const TargetInstrInfo &TII) {
2488a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  if (SchedLiveInCopies) {
2498a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    // Emit the copies at a heuristically-determined location in the block.
2508a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    DenseMap<MachineInstr*, unsigned> CopyRegMap;
2518a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    MachineBasicBlock::iterator InsertPos = EntryMBB->begin();
2528a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
2538a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman           E = MRI.livein_end(); LI != E; ++LI)
2548a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      if (LI->second) {
2558a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman        const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
2568a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman        EmitLiveInCopy(EntryMBB, InsertPos, LI->second, LI->first,
2578a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman                       RC, CopyRegMap, MRI, TRI, TII);
2588a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      }
2598a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  } else {
2608a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    // Emit the copies into the top of the block.
2618a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman    for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
2628a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman           E = MRI.livein_end(); LI != E; ++LI)
2638a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      if (LI->second) {
2648a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman        const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
265f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin        bool Emitted = TII.copyRegToReg(*EntryMBB, EntryMBB->begin(),
266f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin                                        LI->second, LI->first, RC, RC);
267f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin        assert(Emitted && "Unable to issue a live-in copy instruction!\n");
268f1daf7d8abebd6e0104a6b41a774ccbb19a51c60David Goodwin        (void) Emitted;
2698a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman      }
2708a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  }
2718a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman}
2728a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
2737041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner//===----------------------------------------------------------------------===//
2747041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner// SelectionDAGISel code
2757041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner//===----------------------------------------------------------------------===//
2761c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
27798a366d547772010e94609e4584489b3e5ce0043Bill WendlingSelectionDAGISel::SelectionDAGISel(TargetMachine &tm, CodeGenOpt::Level OL) :
278ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohman  MachineFunctionPass(&ID), TM(tm), TLI(*tm.getTargetLowering()),
2797c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  FuncInfo(new FunctionLoweringInfo(TLI)),
2807c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  CurDAG(new SelectionDAG(TLI, *FuncInfo)),
2812048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB(new SelectionDAGBuilder(*CurDAG, TLI, *FuncInfo, OL)),
2827c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  GFI(),
283be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling  OptLevel(OL),
2847c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  DAGSize(0)
2857c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman{}
2867c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman
2877c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan GohmanSelectionDAGISel::~SelectionDAGISel() {
2882048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  delete SDB;
2897c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  delete CurDAG;
2907c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  delete FuncInfo;
2917c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman}
2927c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman
293e50ed30282bb5b4a9ed952580523f2dda16215acOwen Andersonunsigned SelectionDAGISel::MakeReg(EVT VT) {
29484bc5427d6883f73cfeae3da640acd011d35c006Chris Lattner  return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
2951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
2961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
297495a0b51915eb763576874f29192820b731edc22Chris Lattnervoid SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
298c7c3f110eda0ff8040e4bd99e38d3112b910810fJim Laskey  AU.addRequired<AliasAnalysis>();
299a3477fe06df794e0eae26ea243f25912c619a6c4Dan Gohman  AU.addPreserved<AliasAnalysis>();
3005eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  AU.addRequired<GCModuleInfo>();
301a3477fe06df794e0eae26ea243f25912c619a6c4Dan Gohman  AU.addPreserved<GCModuleInfo>();
3026e7a1617ac4a34792d9097b8d3644b72f57a45f7Devang Patel  AU.addRequired<DwarfWriter>();
303a3477fe06df794e0eae26ea243f25912c619a6c4Dan Gohman  AU.addPreserved<DwarfWriter>();
304ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohman  MachineFunctionPass::getAnalysisUsage(AU);
305495a0b51915eb763576874f29192820b731edc22Chris Lattner}
3061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
307ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohmanbool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
308ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohman  Function &Fn = *mf.getFunction();
309ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohman
3104344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman  // Do some sanity-checking on the command-line options.
3114344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman  assert((!EnableFastISelVerbose || EnableFastISel) &&
3124344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman         "-fast-isel-verbose requires -fast-isel");
3134344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman  assert((!EnableFastISelAbort || EnableFastISel) &&
3144344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman         "-fast-isel-abort requires -fast-isel");
3154344a5d0d7b280f0b891e0e6a4413b059d9080b3Dan Gohman
3165f43f92c69aae9837064cf08291db1b36a82789fDan Gohman  // Get alias analysis for load/store combining.
3175f43f92c69aae9837064cf08291db1b36a82789fDan Gohman  AA = &getAnalysis<AliasAnalysis>();
3185f43f92c69aae9837064cf08291db1b36a82789fDan Gohman
319ad2afc2a421a0e41603d5eee412d4d8c77e9bc1cDan Gohman  MF = &mf;
3208a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  const TargetInstrInfo &TII = *TM.getInstrInfo();
3218a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
3228a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
323f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman  if (Fn.hasGC())
324f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman    GFI = &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn);
325ce2247755e56f99a2377b64a1a9d393726582b85Gordon Henriksen  else
3265eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    GFI = 0;
32779ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman  RegInfo = &MF->getRegInfo();
3281a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n");
3291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3301465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
3311465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
3325dcaceb0a6a7fcf508d9898547e10d374b0e4cd1Owen Anderson  CurDAG->init(*MF, MMI, DW);
3336277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman  FuncInfo->set(Fn, *MF, EnableFastISel);
3342048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->init(GFI, *AA);
3351c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3361532f3ddd77c362dd5f613af06b4de636e3c5b0eDale Johannesen  for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
3371532f3ddd77c362dd5f613af06b4de636e3c5b0eDale Johannesen    if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator()))
3381532f3ddd77c362dd5f613af06b4de636e3c5b0eDale Johannesen      // Mark landing pad.
3397c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman      FuncInfo->MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
3409fac0b5ff24e733072bdde68c7f34590c1892922Duncan Sands
34179ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman  SelectAllBasicBlocks(Fn, *MF, MMI, DW, TII);
342edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
3438a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // If the first basic block in the function has live ins that need to be
3448a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // copied into vregs, emit the copies into the top of the block before
3458a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  // emitting the code for the block.
34679ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman  EmitLiveInCopies(MF->begin(), *RegInfo, TRI, TII);
3478a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman
348ad2070c667ebabf1650be63e6b3e0b411da86918Evan Cheng  // Add function live-ins to entry block live-in set.
3498a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman  for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(),
3508a11053f3ca7efb29312c4293c4fdb5d2ca3e20fDan Gohman         E = RegInfo->livein_end(); I != E; ++I)
35179ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman    MF->begin()->addLiveIn(I->first);
352ad2070c667ebabf1650be63e6b3e0b411da86918Evan Cheng
353f40708223e409a152834b1553c6883ff2035b957Duncan Sands#ifndef NDEBUG
3547c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  assert(FuncInfo->CatchInfoFound.size() == FuncInfo->CatchInfoLost.size() &&
355f40708223e409a152834b1553c6883ff2035b957Duncan Sands         "Not all catch info was assigned to a landing pad!");
356f40708223e409a152834b1553c6883ff2035b957Duncan Sands#endif
357f40708223e409a152834b1553c6883ff2035b957Duncan Sands
3587c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman  FuncInfo->clear();
3597c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman
3601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  return true;
3611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
3621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
36307f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman/// SetDebugLoc - Update MF's and SDB's DebugLocs if debug information is
36407f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman/// attached with this instruction.
3653990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattnerstatic void SetDebugLoc(unsigned MDDbgKind, Instruction *I,
3663990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner                        SelectionDAGBuilder *SDB,
3670eb419800ae51d6e0e00a656ede0627483755361Chris Lattner                        FastISel *FastIS, MachineFunction *MF) {
3680eb419800ae51d6e0e00a656ede0627483755361Chris Lattner  if (isa<DbgInfoIntrinsic>(I)) return;
3690eb419800ae51d6e0e00a656ede0627483755361Chris Lattner
3703990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner  if (MDNode *Dbg = I->getMetadata(MDDbgKind)) {
3710eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    DILocation DILoc(Dbg);
3720eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    DebugLoc Loc = ExtractDebugLocation(DILoc, MF->getDebugLocInfo());
3730eb419800ae51d6e0e00a656ede0627483755361Chris Lattner
3740eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    SDB->setCurDebugLoc(Loc);
3750eb419800ae51d6e0e00a656ede0627483755361Chris Lattner
3760eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    if (FastIS)
3770eb419800ae51d6e0e00a656ede0627483755361Chris Lattner      FastIS->setCurDebugLoc(Loc);
3780eb419800ae51d6e0e00a656ede0627483755361Chris Lattner
3790eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    // If the function doesn't have a default debug location yet, set
3800eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    // it. This is kind of a hack.
3810eb419800ae51d6e0e00a656ede0627483755361Chris Lattner    if (MF->getDefaultDebugLoc().isUnknown())
3820eb419800ae51d6e0e00a656ede0627483755361Chris Lattner      MF->setDefaultDebugLoc(Loc);
3830eb419800ae51d6e0e00a656ede0627483755361Chris Lattner  }
38407f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman}
38507f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman
38607f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman/// ResetDebugLoc - Set MF's and SDB's DebugLocs to Unknown.
3873990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattnerstatic void ResetDebugLoc(SelectionDAGBuilder *SDB, FastISel *FastIS) {
38807f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman  SDB->setCurDebugLoc(DebugLoc::getUnknownLoc());
38907f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman  if (FastIS)
390688fb80334e1ad7b259a051411bb72e18abea9e7Dan Gohman    FastIS->setCurDebugLoc(DebugLoc::getUnknownLoc());
39107f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman}
39207f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman
393f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohmanvoid SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB,
394f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman                                        BasicBlock::iterator Begin,
395b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman                                        BasicBlock::iterator End,
396b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman                                        bool &HadTailCall) {
3972048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->setCurrentBasicBlock(BB);
398081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  unsigned MDDbgKind = LLVMBB->getContext().getMDKindID("dbg");
399f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
40098ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman  // Lower all of the non-terminator instructions. If a call is emitted
40198ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman  // as a tail call, cease emitting nodes for this block.
4022048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  for (BasicBlock::iterator I = Begin; I != End && !SDB->HasTailCall; ++I) {
4033990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner    SetDebugLoc(MDDbgKind, I, SDB, 0, MF);
40407f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman
40507f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman    if (!isa<TerminatorInst>(I)) {
4062048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->visit(*I);
40707f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman
40807f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman      // Set the current debug location back to "unknown" so that it doesn't
40907f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman      // spuriously apply to subsequent instructions.
41007f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman      ResetDebugLoc(SDB, 0);
41107f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman    }
412123eaa71b50f5ac2ea2233685baedddbb72f387bDevang Patel  }
413f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
4142048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  if (!SDB->HasTailCall) {
41598ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman    // Ensure that all instructions which are used outside of their defining
41698ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman    // blocks are available as virtual registers.  Invoke is handled elsewhere.
41798ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman    for (BasicBlock::iterator I = Begin; I != End; ++I)
41898ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman      if (!isa<PHINode>(I) && !isa<InvokeInst>(I))
4192048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        SDB->CopyToExportRegsIfNeeded(I);
420f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
42198ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman    // Handle PHI nodes in successor blocks.
42298ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman    if (End == LLVMBB->end()) {
42398ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman      HandlePHINodesInSuccessorBlocks(LLVMBB);
4243df24e667f04a7003342b534310919abc9c87418Dan Gohman
42598ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman      // Lower the terminator after the copies are emitted.
4263990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner      SetDebugLoc(MDDbgKind, LLVMBB->getTerminator(), SDB, 0, MF);
4272048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->visit(*LLVMBB->getTerminator());
42807f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman      ResetDebugLoc(SDB, 0);
42998ca4f2a325f72374a477f9deba7d09e8999c29bDan Gohman    }
4303df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
431a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
432a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  // Make sure the root of the DAG is up-to-date.
4332048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  CurDAG->setRoot(SDB->getControlRoot());
434c85e1716f0e45e4c18a9ef2fbe431a51ac3a4252Arnold Schwaighofer
435f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  // Final step, emit the lowered DAG as machine code.
436f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  CodeGenAndEmitDAG();
4372048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  HadTailCall = SDB->HasTailCall;
4382048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->clear();
4391c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
4401c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
44154e146b935123d3d325e8eb30c2e1f2eca3377acEvan Chengnamespace {
44254e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
44354e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng/// nodes from the worklist.
44454e146b935123d3d325e8eb30c2e1f2eca3377acEvan Chengclass SDOPsWorkListRemover : public SelectionDAG::DAGUpdateListener {
44554e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng  SmallVector<SDNode*, 128> &Worklist;
44654e146b935123d3d325e8eb30c2e1f2eca3377acEvan Chengpublic:
44754e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng  SDOPsWorkListRemover(SmallVector<SDNode*, 128> &wl) : Worklist(wl) {}
44854e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng
44954e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng  virtual void NodeDeleted(SDNode *N, SDNode *E) {
45054e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng    Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), N),
45154e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng                   Worklist.end());
45254e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng  }
45354e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng
45454e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng  virtual void NodeUpdated(SDNode *N) {
45554e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng    // Ignore updates.
45654e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng  }
45754e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng};
45854e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng}
45954e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng
460046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng/// TrivialTruncElim - Eliminate some trivial nops that can result from
461046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng/// ShrinkDemandedOps: (trunc (ext n)) -> n.
462046632f272b22a653c204c0ef56eb4076368d4bbEvan Chengstatic bool TrivialTruncElim(SDValue Op,
463046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng                             TargetLowering::TargetLoweringOpt &TLO) {
464046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng  SDValue N0 = Op.getOperand(0);
465046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng  EVT VT = Op.getValueType();
466046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng  if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
467046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng       N0.getOpcode() == ISD::SIGN_EXTEND ||
468046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng       N0.getOpcode() == ISD::ANY_EXTEND) &&
469046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng      N0.getOperand(0).getValueType() == VT) {
470046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng    return TLO.CombineTo(Op, N0.getOperand(0));
471046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng  }
472046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng  return false;
473046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng}
474046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng
47554eb4c2991796c3014e831fc07e4a6c0a3df10f0Evan Cheng/// ShrinkDemandedOps - A late transformation pass that shrink expressions
47654eb4c2991796c3014e831fc07e4a6c0a3df10f0Evan Cheng/// using TargetLowering::TargetLoweringOpt::ShrinkDemandedOp. It converts
47754eb4c2991796c3014e831fc07e4a6c0a3df10f0Evan Cheng/// x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
478d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Chengvoid SelectionDAGISel::ShrinkDemandedOps() {
479d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  SmallVector<SDNode*, 128> Worklist;
480d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
481d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  // Add all the dag nodes to the worklist.
482d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  Worklist.reserve(CurDAG->allnodes_size());
483d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
484d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng       E = CurDAG->allnodes_end(); I != E; ++I)
485d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    Worklist.push_back(I);
486d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
487d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  APInt Mask;
488d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  APInt KnownZero;
489d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  APInt KnownOne;
490d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
491d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  TargetLowering::TargetLoweringOpt TLO(*CurDAG, true);
492d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  while (!Worklist.empty()) {
4937b1e2a50a51793428a792a15d2777a4c48e0db49Benjamin Kramer    SDNode *N = Worklist.pop_back_val();
494d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
495d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    if (N->use_empty() && N != CurDAG->getRoot().getNode()) {
49654e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng      CurDAG->DeleteNode(N);
497d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng      continue;
498d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    }
499d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
500d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    // Run ShrinkDemandedOp on scalar binary operations.
501d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    if (N->getNumValues() == 1 &&
502d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        N->getValueType(0).isSimple() && N->getValueType(0).isInteger()) {
503d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng      unsigned BitWidth = N->getValueType(0).getScalarType().getSizeInBits();
504d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng      APInt Demanded = APInt::getAllOnesValue(BitWidth);
505d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng      APInt KnownZero, KnownOne;
506d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng      if (TLI.SimplifyDemandedBits(SDValue(N, 0), Demanded,
507046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng                                   KnownZero, KnownOne, TLO) ||
508046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng          (N->getOpcode() == ISD::TRUNCATE &&
509046632f272b22a653c204c0ef56eb4076368d4bbEvan Cheng           TrivialTruncElim(SDValue(N, 0), TLO))) {
510d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        // Revisit the node.
511d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), N),
512d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                       Worklist.end());
513d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        Worklist.push_back(N);
514d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
515d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        // Replace the old value with the new one.
516d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        DEBUG(errs() << "\nReplacing ";
517d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng              TLO.Old.getNode()->dump(CurDAG);
518d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng              errs() << "\nWith: ";
519d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng              TLO.New.getNode()->dump(CurDAG);
520d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng              errs() << '\n');
521d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
522d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        Worklist.push_back(TLO.New.getNode());
52354e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng
52454e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng        SDOPsWorkListRemover DeadNodes(Worklist);
52554e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng        CurDAG->ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
526d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
527d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        if (TLO.Old.getNode()->use_empty()) {
528d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng          for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands();
529d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng               i != e; ++i) {
530d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            SDNode *OpNode = TLO.Old.getNode()->getOperand(i).getNode();
531d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            if (OpNode->hasOneUse()) {
532d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng              Worklist.erase(std::remove(Worklist.begin(), Worklist.end(),
53354e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng                                         OpNode), Worklist.end());
53454e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng              Worklist.push_back(OpNode);
535d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            }
536d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng          }
537d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
538d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng          Worklist.erase(std::remove(Worklist.begin(), Worklist.end(),
53954e146b935123d3d325e8eb30c2e1f2eca3377acEvan Cheng                                     TLO.Old.getNode()), Worklist.end());
540d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng          CurDAG->DeleteNode(TLO.Old.getNode());
541d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        }
542d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng      }
543d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    }
544d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  }
545d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng}
546d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng
547f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohmanvoid SelectionDAGISel::ComputeLiveOutVRegInfo() {
548ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner  SmallPtrSet<SDNode*, 128> VisitedNodes;
549ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner  SmallVector<SDNode*, 128> Worklist;
550a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
551ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  Worklist.push_back(CurDAG->getRoot().getNode());
552a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
553ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner  APInt Mask;
554ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner  APInt KnownZero;
555ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner  APInt KnownOne;
556a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
5577b1e2a50a51793428a792a15d2777a4c48e0db49Benjamin Kramer  do {
5587b1e2a50a51793428a792a15d2777a4c48e0db49Benjamin Kramer    SDNode *N = Worklist.pop_back_val();
559a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
560ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    // If we've already seen this node, ignore it.
561ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    if (!VisitedNodes.insert(N))
562ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner      continue;
563a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
564ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    // Otherwise, add all chain operands to the worklist.
565ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
566825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      if (N->getOperand(i).getValueType() == MVT::Other)
567ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        Worklist.push_back(N->getOperand(i).getNode());
568a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
569ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    // If this is a CopyToReg with a vreg dest, process it.
570ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    if (N->getOpcode() != ISD::CopyToReg)
571ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner      continue;
572a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
573ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
574ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    if (!TargetRegisterInfo::isVirtualRegister(DestReg))
575ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner      continue;
576a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
577ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    // Ignore non-scalar or non-integer values.
578475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Src = N->getOperand(2);
579e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT SrcVT = Src.getValueType();
580ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    if (!SrcVT.isInteger() || SrcVT.isVector())
581ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner      continue;
582a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
583f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
584ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits());
585f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne);
586a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
587ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    // Only install this information if it tells us something.
588ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) {
589ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner      DestReg -= TargetRegisterInfo::FirstVirtualRegister;
590f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman      if (DestReg >= FuncInfo->LiveOutRegInfo.size())
591f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman        FuncInfo->LiveOutRegInfo.resize(DestReg+1);
592f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman      FunctionLoweringInfo::LiveOutInfo &LOI =
593f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman        FuncInfo->LiveOutRegInfo[DestReg];
594ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner      LOI.NumSignBits = NumSignBits;
595a80efceb93211a22e13dfc68198da4a25c1a7762Dan Gohman      LOI.KnownOne = KnownOne;
596a80efceb93211a22e13dfc68198da4a25c1a7762Dan Gohman      LOI.KnownZero = KnownZero;
597ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner    }
5987b1e2a50a51793428a792a15d2777a4c48e0db49Benjamin Kramer  } while (!Worklist.empty());
599ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner}
600ead0d88ad7659dabd66cc3149af97d98256fca84Chris Lattner
601f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohmanvoid SelectionDAGISel::CodeGenAndEmitDAG() {
602462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman  std::string GroupName;
603462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman  if (TimePassesIsEnabled)
604462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman    GroupName = "Instruction Selection and Scheduling";
605462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman  std::string BlockName;
606462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman  if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs ||
60725cf2275ff7de3de3bc0e508abaf457413d74725Duncan Sands      ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs ||
60825cf2275ff7de3de3bc0e508abaf457413d74725Duncan Sands      ViewSUnitDAGs)
609f7d6cd46e920776bb33913b2498347cf99358dc4Dan Gohman    BlockName = MF->getFunction()->getNameStr() + ":" +
610f6ccee5a9d2b9573f679bca6266ade3eb8cd3f88Daniel Dunbar                BB->getBasicBlock()->getNameStr();
611462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
6121a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Initial selection DAG:\n");
613f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  DEBUG(CurDAG->dump());
614462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
615f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName);
616417e11b9bf625b3626f8765d44248927fb200906Dan Gohman
617af21d55aee8d6852fedfd630395a3c7c95df8f12Chris Lattner  // Run the DAG combiner in pre-legalize mode.
618ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  if (TimePassesIsEnabled) {
6195e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("DAG Combining 1", GroupName);
620be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling    CurDAG->Combine(Unrestricted, *AA, OptLevel);
621ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  } else {
622be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling    CurDAG->Combine(Unrestricted, *AA, OptLevel);
623ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  }
624a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
6251a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Optimized lowered selection DAG:\n");
626f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  DEBUG(CurDAG->dump());
627a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
6281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Second step, hack on the DAG until it only uses operations and types that
6291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // the target supports.
630714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " +
631714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman                                               BlockName);
632714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman
633714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  bool Changed;
634714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  if (TimePassesIsEnabled) {
635714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    NamedRegionTimer T("Type Legalization", GroupName);
636714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    Changed = CurDAG->LegalizeTypes();
637714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  } else {
638714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    Changed = CurDAG->LegalizeTypes();
639714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  }
640714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman
6411a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Type-legalized selection DAG:\n");
642714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  DEBUG(CurDAG->dump());
643462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
644714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  if (Changed) {
645714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    if (ViewDAGCombineLT)
646714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
647714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman
648714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    // Run the DAG combiner in post-type-legalize mode.
649462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman    if (TimePassesIsEnabled) {
650714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      NamedRegionTimer T("DAG Combining after legalize types", GroupName);
651714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
652462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman    } else {
653714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
654462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman    }
655462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
6561a053236f165ac60ee88edca370a732d278aa2c1David Greene    DEBUG(dbgs() << "Optimized type-legalized selection DAG:\n");
657f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    DEBUG(CurDAG->dump());
658714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  }
659462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
660714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  if (TimePassesIsEnabled) {
661714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    NamedRegionTimer T("Vector Legalization", GroupName);
662714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    Changed = CurDAG->LegalizeVectors();
663714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  } else {
664714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    Changed = CurDAG->LegalizeVectors();
665714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  }
6665c22c8074404797f1313b1334757254fb5c6487aEli Friedman
667714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman  if (Changed) {
6685c22c8074404797f1313b1334757254fb5c6487aEli Friedman    if (TimePassesIsEnabled) {
669714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      NamedRegionTimer T("Type Legalization 2", GroupName);
6709882007003b12466df717cdf66824120ede405beBill Wendling      CurDAG->LegalizeTypes();
6715c22c8074404797f1313b1334757254fb5c6487aEli Friedman    } else {
6729882007003b12466df717cdf66824120ede405beBill Wendling      CurDAG->LegalizeTypes();
6735c22c8074404797f1313b1334757254fb5c6487aEli Friedman    }
6745c22c8074404797f1313b1334757254fb5c6487aEli Friedman
675714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    if (ViewDAGCombineLT)
676714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
6775c22c8074404797f1313b1334757254fb5c6487aEli Friedman
678714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    // Run the DAG combiner in post-type-legalize mode.
679714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    if (TimePassesIsEnabled) {
680714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      NamedRegionTimer T("DAG Combining after legalize vectors", GroupName);
681714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
682714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    } else {
683714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman      CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
6845c22c8074404797f1313b1334757254fb5c6487aEli Friedman    }
685714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman
6861a053236f165ac60ee88edca370a732d278aa2c1David Greene    DEBUG(dbgs() << "Optimized vector-legalized selection DAG:\n");
687714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    DEBUG(CurDAG->dump());
68870587ea813986bb0dbef71610091d496e28c22a6Chris Lattner  }
689a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
690f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName);
691462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
692ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  if (TimePassesIsEnabled) {
6935e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("DAG Legalization", GroupName);
694714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    CurDAG->Legalize(OptLevel);
695ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  } else {
696714efc63d4319b771d89a95aa7a42d400f03d8c9Dan Gohman    CurDAG->Legalize(OptLevel);
697ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  }
698a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
6991a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Legalized selection DAG:\n");
700f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  DEBUG(CurDAG->dump());
701a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
702f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName);
703462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
704af21d55aee8d6852fedfd630395a3c7c95df8f12Chris Lattner  // Run the DAG combiner in post-legalize mode.
705ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  if (TimePassesIsEnabled) {
7065e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("DAG Combining 2", GroupName);
707be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling    CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
708ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  } else {
709be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling    CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
710ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  }
711a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
7121a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Optimized legalized selection DAG:\n");
713f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  DEBUG(CurDAG->dump());
714417e11b9bf625b3626f8765d44248927fb200906Dan Gohman
715f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName);
716a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
717d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  if (OptLevel != CodeGenOpt::None) {
718d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    ShrinkDemandedOps();
719f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    ComputeLiveOutVRegInfo();
720d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng  }
721552c4a849422ad91fa9988255ae35aebbf3acf7bEvan Cheng
722a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // Third, instruction select all of the operations to machine code, adding the
723a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // code to the MachineBasicBlock.
724ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  if (TimePassesIsEnabled) {
7255e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("Instruction Selection", GroupName);
7267c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    DoInstructionSelection();
727ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  } else {
7287c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    DoInstructionSelection();
729ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  }
730db8d56b825efeb576d67b9dbe39d736d93306222Evan Cheng
7311a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Selected selection DAG:\n");
732f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  DEBUG(CurDAG->dump());
733462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
734f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName);
735462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
7365e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  // Schedule machine code.
73747ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman  ScheduleDAGSDNodes *Scheduler = CreateScheduler();
7385e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  if (TimePassesIsEnabled) {
7395e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("Instruction Scheduling", GroupName);
74047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman    Scheduler->Run(CurDAG, BB, BB->end());
7415e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  } else {
74247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman    Scheduler->Run(CurDAG, BB, BB->end());
7435e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  }
7445e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman
745462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman  if (ViewSUnitDAGs) Scheduler->viewGraph();
746462dc7f4960e5074ddf4769ec8b2ef1ba7a4d2c8Dan Gohman
747a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  // Emit machine code to BB.  This can change 'BB' to the last block being
748db8d56b825efeb576d67b9dbe39d736d93306222Evan Cheng  // inserted into.
749ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  if (TimePassesIsEnabled) {
7505e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("Instruction Creation", GroupName);
7512048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    BB = Scheduler->EmitSchedule(&SDB->EdgeMapping);
752ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  } else {
7532048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    BB = Scheduler->EmitSchedule(&SDB->EdgeMapping);
7545e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  }
7555e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman
7565e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  // Free the scheduler state.
7575e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  if (TimePassesIsEnabled) {
7585e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName);
7595e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    delete Scheduler;
7605e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman  } else {
7615e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman    delete Scheduler;
762ebffb660a68384dd8b5e2ff36d68e94a3920611bEvan Cheng  }
763db8d56b825efeb576d67b9dbe39d736d93306222Evan Cheng
7641a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Selected machine code:\n");
7651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(BB->dump());
766a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar}
767f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman
7687c306da505e2d7f64e160890b274a47fa0740962Chris Lattnervoid SelectionDAGISel::DoInstructionSelection() {
7697c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  DEBUG(errs() << "===== Instruction selection begins:\n");
7707c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
7717c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  PreprocessISelDAG();
7727c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
7737c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  // Select target instructions for the DAG.
7747c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  {
7757c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // Number all nodes with a topological order and set DAGSize.
7767c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    DAGSize = CurDAG->AssignTopologicalOrder();
7777c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
7787c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // Create a dummy node (which is not added to allnodes), that adds
7797c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // a reference to the root node, preventing it from being deleted,
7807c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // and tracking any changes of the root.
7817c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    HandleSDNode Dummy(CurDAG->getRoot());
7827c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    ISelPosition = SelectionDAG::allnodes_iterator(CurDAG->getRoot().getNode());
7837c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    ++ISelPosition;
7847c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
7857c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // The AllNodes list is now topological-sorted. Visit the
7867c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // nodes by starting at the end of the list (the root of the
7877c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // graph) and preceding back toward the beginning (the entry
7887c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    // node).
7897c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    while (ISelPosition != CurDAG->allnodes_begin()) {
7907c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      SDNode *Node = --ISelPosition;
7917c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes,
7927c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // but there are currently some corner cases that it misses. Also, this
7937c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // makes it theoretically possible to disable the DAGCombiner.
7947c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      if (Node->use_empty())
7957c306da505e2d7f64e160890b274a47fa0740962Chris Lattner        continue;
7967c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
7977c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      SDNode *ResNode = Select(Node);
7987c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
79982dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      // FIXME: This is pretty gross.  'Select' should be changed to not return
80082dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      // anything at all and this code should be nuked with a tactical strike.
80182dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
8027c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // If node should not be replaced, continue with the next one.
80382dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      if (ResNode == Node || Node->getOpcode() == ISD::DELETED_NODE)
8047c306da505e2d7f64e160890b274a47fa0740962Chris Lattner        continue;
8057c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // Replace node.
8067c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      if (ResNode)
8077c306da505e2d7f64e160890b274a47fa0740962Chris Lattner        ReplaceUses(Node, ResNode);
8087c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
8097c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // If after the replacement this node is not used any more,
8107c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      // remove this dead node.
8117c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      if (Node->use_empty()) { // Don't delete EntryToken, etc.
8127c306da505e2d7f64e160890b274a47fa0740962Chris Lattner        ISelUpdater ISU(ISelPosition);
8137c306da505e2d7f64e160890b274a47fa0740962Chris Lattner        CurDAG->RemoveDeadNode(Node, &ISU);
8147c306da505e2d7f64e160890b274a47fa0740962Chris Lattner      }
8157c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    }
8167c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
8177c306da505e2d7f64e160890b274a47fa0740962Chris Lattner    CurDAG->setRoot(Dummy.getValue());
8187c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  }
8197c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  DEBUG(errs() << "===== Instruction selection ends:\n");
8207c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
8217c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  PostprocessISelDAG();
8227c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
8237c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  // FIXME: This shouldn't be needed, remove it.
8247c306da505e2d7f64e160890b274a47fa0740962Chris Lattner  CurDAG->RemoveDeadNodes();
8257c306da505e2d7f64e160890b274a47fa0740962Chris Lattner}
8267c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
8277c306da505e2d7f64e160890b274a47fa0740962Chris Lattner
82879ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohmanvoid SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
82979ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman                                            MachineFunction &MF,
830dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                            MachineModuleInfo *MMI,
83183489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel                                            DwarfWriter *DW,
832dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                            const TargetInstrInfo &TII) {
833a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman  // Initialize the Fast-ISel state, if needed.
834a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman  FastISel *FastIS = 0;
835a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman  if (EnableFastISel)
83679ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman    FastIS = TLI.createFastISel(MF, MMI, DW,
837a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman                                FuncInfo->ValueMap,
838a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman                                FuncInfo->MBBMap,
839dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                FuncInfo->StaticAllocaMap
840dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
841dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                , FuncInfo->CatchInfoLost
842dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
843dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                );
844a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman
845081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  unsigned MDDbgKind = Fn.getContext().getMDKindID("dbg");
846123eaa71b50f5ac2ea2233685baedddbb72f387bDevang Patel
847a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman  // Iterate over all basic blocks in the function.
84839fd6e81b1b0de8b820155dc6a0dae226c109de6Evan Cheng  for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
84939fd6e81b1b0de8b820155dc6a0dae226c109de6Evan Cheng    BasicBlock *LLVMBB = &*I;
8507c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman    BB = FuncInfo->MBBMap[LLVMBB];
851f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
8523df24e667f04a7003342b534310919abc9c87418Dan Gohman    BasicBlock::iterator const Begin = LLVMBB->begin();
8533df24e667f04a7003342b534310919abc9c87418Dan Gohman    BasicBlock::iterator const End = LLVMBB->end();
8549f1185076d60153fd35feb3cadb1b4cd8debc0b6Evan Cheng    BasicBlock::iterator BI = Begin;
8555edd361497f4c78a20fcbb75c6e7e94ff8046ed1Dan Gohman
8565edd361497f4c78a20fcbb75c6e7e94ff8046ed1Dan Gohman    // Lower any arguments needed in this block if this is the entry block.
85733134c4a75558288d663267c8991f6bd37a530afDan Gohman    bool SuppressFastISel = false;
85833134c4a75558288d663267c8991f6bd37a530afDan Gohman    if (LLVMBB == &Fn.getEntryBlock()) {
8595edd361497f4c78a20fcbb75c6e7e94ff8046ed1Dan Gohman      LowerArguments(LLVMBB);
860f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
86133134c4a75558288d663267c8991f6bd37a530afDan Gohman      // If any of the arguments has the byval attribute, forgo
86233134c4a75558288d663267c8991f6bd37a530afDan Gohman      // fast-isel in the entry block.
863a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      if (FastIS) {
86433134c4a75558288d663267c8991f6bd37a530afDan Gohman        unsigned j = 1;
86533134c4a75558288d663267c8991f6bd37a530afDan Gohman        for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
86633134c4a75558288d663267c8991f6bd37a530afDan Gohman             I != E; ++I, ++j)
8670598866c052147c31b808391f58434ce3dbfb838Devang Patel          if (Fn.paramHasAttr(j, Attribute::ByVal)) {
86877ca41e5c3f99ee8962b21a7d90bb251a594a65eDan Gohman            if (EnableFastISelVerbose || EnableFastISelAbort)
8691a053236f165ac60ee88edca370a732d278aa2c1David Greene              dbgs() << "FastISel skips entry block due to byval argument\n";
87033134c4a75558288d663267c8991f6bd37a530afDan Gohman            SuppressFastISel = true;
87133134c4a75558288d663267c8991f6bd37a530afDan Gohman            break;
87233134c4a75558288d663267c8991f6bd37a530afDan Gohman          }
87333134c4a75558288d663267c8991f6bd37a530afDan Gohman      }
87433134c4a75558288d663267c8991f6bd37a530afDan Gohman    }
87533134c4a75558288d663267c8991f6bd37a530afDan Gohman
876dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    if (MMI && BB->isLandingPad()) {
877dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // Add a label to mark the beginning of the landing pad.  Deletion of the
878dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // landing pad can thus be detected via the MachineModuleInfo.
879dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      unsigned LabelID = MMI->addLandingPad(BB);
880dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
881518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner      const TargetInstrDesc &II = TII.get(TargetOpcode::EH_LABEL);
8822048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      BuildMI(BB, SDB->getCurDebugLoc(), II).addImm(LabelID);
883dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
884dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // Mark exception register as live in.
885dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      unsigned Reg = TLI.getExceptionAddressRegister();
886dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      if (Reg) BB->addLiveIn(Reg);
887dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
888dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // Mark exception selector register as live in.
889dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      Reg = TLI.getExceptionSelectorRegister();
890dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      if (Reg) BB->addLiveIn(Reg);
891dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
892dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // FIXME: Hack around an exception handling flaw (PR1508): the personality
893dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // function and list of typeids logically belong to the invoke (or, if you
894dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // like, the basic block containing the invoke), and need to be associated
895dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // with it in the dwarf exception handling tables.  Currently however the
896dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // information is provided by an intrinsic (eh.selector) that can be moved
897dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // to unexpected places by the optimizers: if the unwind edge is critical,
898dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      // then breaking it can result in the intrinsics being in the successor of
899f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach      // the landing pad, not the landing pad itself.  This results
900f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach      // in exceptions not being caught because no typeids are associated with
901f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach      // the invoke.  This may not be the only way things can go wrong, but it
902f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach      // is the only way we try to work around for the moment.
903dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator());
904dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
905dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      if (Br && Br->isUnconditional()) { // Critical edge?
906dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        BasicBlock::iterator I, E;
907dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I)
908dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          if (isa<EHSelectorInst>(I))
909dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman            break;
910dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
911dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        if (I == E)
912dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          // No catch info found - try to extract some from the successor.
9135fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman          CopyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, *FuncInfo);
914dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      }
915dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
916dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
917f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    // Before doing SelectionDAG ISel, see if FastISel has been requested.
918dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    if (FastIS && !SuppressFastISel) {
919a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      // Emit code for any incoming arguments. This must happen before
920a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      // beginning FastISel on the entry block.
921a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      if (LLVMBB == &Fn.getEntryBlock()) {
9222048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        CurDAG->setRoot(SDB->getControlRoot());
923a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        CodeGenAndEmitDAG();
9242048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        SDB->clear();
925a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      }
926241f464d24a6c22721607841069bbeb17b3f71e6Dan Gohman      FastIS->startNewBlock(BB);
927a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      // Do FastISel on as many instructions as possible.
928a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman      for (; BI != End; ++BI) {
929a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        // Just before the terminator instruction, insert instructions to
930a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        // feed PHI nodes in successor blocks.
931a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        if (isa<TerminatorInst>(BI))
932a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          if (!HandlePHINodesInSuccessorBlocksFast(LLVMBB, FastIS)) {
93307f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman            ResetDebugLoc(SDB, FastIS);
93477ca41e5c3f99ee8962b21a7d90bb251a594a65eDan Gohman            if (EnableFastISelVerbose || EnableFastISelAbort) {
9351a053236f165ac60ee88edca370a732d278aa2c1David Greene              dbgs() << "FastISel miss: ";
93677ca41e5c3f99ee8962b21a7d90bb251a594a65eDan Gohman              BI->dump();
93777ca41e5c3f99ee8962b21a7d90bb251a594a65eDan Gohman            }
938a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar            assert(!EnableFastISelAbort &&
939f36892335b4919b9120e48a792e6b3630b9de978Torok Edwin                   "FastISel didn't handle a PHI in a successor");
940a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            break;
941a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          }
94233134c4a75558288d663267c8991f6bd37a530afDan Gohman
9433990b121cf4a0b280ed3e54cf13870cbf4259e78Chris Lattner        SetDebugLoc(MDDbgKind, BI, SDB, FastIS, &MF);
944381ca5572e3c37824e1c5be0fa02f7b5bdd4a1ceDan Gohman
94521c14e3649b3e2a70ac36ebb148f17648f10a2a9Dan Gohman        // Try to select the instruction with FastISel.
94607f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman        if (FastIS->SelectInstruction(BI)) {
94707f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman          ResetDebugLoc(SDB, FastIS);
948a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          continue;
94907f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman        }
950a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman
95107f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman        // Clear out the debug location so that it doesn't carry over to
95207f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman        // unrelated instructions.
95307f111e7b746747cade8371eeaf8c613058cb3a0Dan Gohman        ResetDebugLoc(SDB, FastIS);
954f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
955a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        // Then handle certain instructions as single-LLVM-Instruction blocks.
956a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        if (isa<CallInst>(BI)) {
957a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          if (EnableFastISelVerbose || EnableFastISelAbort) {
9581a053236f165ac60ee88edca370a732d278aa2c1David Greene            dbgs() << "FastISel missed call: ";
959a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            BI->dump();
960f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman          }
961f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
962f012705c7e4ca8cf90b6b734ce1d5355daca5ba5Benjamin Kramer          if (!BI->getType()->isVoidTy()) {
963a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            unsigned &R = FuncInfo->ValueMap[BI];
964a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            if (!R)
965a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman              R = FuncInfo->CreateRegForValue(BI);
966f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman          }
967a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman
968b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman          bool HadTailCall = false;
9697896c9f436a4eda5ec15e882a7505ba482a2fcd0Chris Lattner          SelectBasicBlock(LLVMBB, BI, llvm::next(BI), HadTailCall);
970b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman
971b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman          // If the call was emitted as a tail call, we're done with the block.
972b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman          if (HadTailCall) {
973b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman            BI = End;
974b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman            break;
975b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman          }
976b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman
977241f464d24a6c22721607841069bbeb17b3f71e6Dan Gohman          // If the instruction was codegen'd with multiple blocks,
978241f464d24a6c22721607841069bbeb17b3f71e6Dan Gohman          // inform the FastISel object where to resume inserting.
979241f464d24a6c22721607841069bbeb17b3f71e6Dan Gohman          FastIS->setCurrentBlock(BB);
980a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          continue;
981f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman        }
982a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman
983a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        // Otherwise, give up on FastISel for the rest of the block.
984a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        // For now, be a little lenient about non-branch terminators.
985a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        if (!isa<TerminatorInst>(BI) || isa<BranchInst>(BI)) {
986a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          if (EnableFastISelVerbose || EnableFastISelAbort) {
9871a053236f165ac60ee88edca370a732d278aa2c1David Greene            dbgs() << "FastISel miss: ";
988a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            BI->dump();
989a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          }
990a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman          if (EnableFastISelAbort)
991a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            // The "fast" selector couldn't handle something and bailed.
992a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman            // For the purpose of debugging, just abort.
993c23197a26f34f559ea9797de51e187087c039c42Torok Edwin            llvm_unreachable("FastISel didn't select the entire block");
994a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        }
995a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman        break;
996f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman      }
997f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    }
998f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman
999d2ff647964ed242e67ac12f7d21a58c864309d95Dan Gohman    // Run SelectionDAG instruction selection on the remainder of the block
1000d2ff647964ed242e67ac12f7d21a58c864309d95Dan Gohman    // not handled by FastISel. If FastISel is not run, this is the entire
10013df24e667f04a7003342b534310919abc9c87418Dan Gohman    // block.
1002390f3ace34855a3d4c9e0adf468976375f8c6dc1Devang Patel    if (BI != End) {
1003b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman      bool HadTailCall;
1004b4afb137efbc38462707bab9f5e8bc5c2d07cd2dDan Gohman      SelectBasicBlock(LLVMBB, BI, End, HadTailCall);
1005390f3ace34855a3d4c9e0adf468976375f8c6dc1Devang Patel    }
1006381cb07544a2bc119e39969d7d508a6247773e1cEvan Cheng
10077c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman    FinishBasicBlock();
100839fd6e81b1b0de8b820155dc6a0dae226c109de6Evan Cheng  }
1009a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman
1010a43abd102f123f6d3bd9ad92045f8119e3fb6153Dan Gohman  delete FastIS;
10110e5f1306b059b62d7725f324e087efbc8e7a782dDan Gohman}
10120e5f1306b059b62d7725f324e087efbc8e7a782dDan Gohman
1013fed90b6d097d50881afb45e4d79f430db66dd741Dan Gohmanvoid
10147c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan GohmanSelectionDAGISel::FinishBasicBlock() {
10151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
10161a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Target-post-processed machine code:\n");
1017f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman  DEBUG(BB->dump());
10184198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov
10191a053236f165ac60ee88edca370a732d278aa2c1David Greene  DEBUG(dbgs() << "Total amount of phi nodes to update: "
10202048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman               << SDB->PHINodesToUpdate.size() << "\n");
10212048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  DEBUG(for (unsigned i = 0, e = SDB->PHINodesToUpdate.size(); i != e; ++i)
10221a053236f165ac60ee88edca370a732d278aa2c1David Greene          dbgs() << "Node " << i << " : ("
10232048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman                 << SDB->PHINodesToUpdate[i].first
10242048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman                 << ", " << SDB->PHINodesToUpdate[i].second << ")\n");
1025a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1026a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // Next, now that we know what the last MBB the LLVM BB expanded is, update
10271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // PHI nodes in successors.
10282048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  if (SDB->SwitchCases.empty() &&
10292048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->JTCases.empty() &&
10302048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->BitTestCases.empty()) {
10312048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    for (unsigned i = 0, e = SDB->PHINodesToUpdate.size(); i != e; ++i) {
10322048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      MachineInstr *PHI = SDB->PHINodesToUpdate[i].first;
1033518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner      assert(PHI->isPHI() &&
1034f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman             "This is not a machine PHI node that we are updating!");
10352048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[i].second,
10369ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner                                                false));
10379ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner      PHI->addOperand(MachineOperand::CreateMBB(BB));
1038f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman    }
10392048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->PHINodesToUpdate.clear();
1040f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman    return;
10411c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
10424198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov
10432048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  for (unsigned i = 0, e = SDB->BitTestCases.size(); i != e; ++i) {
10444198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov    // Lower header first, if it wasn't already lowered
10452048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    if (!SDB->BitTestCases[i].Emitted) {
10464198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // Set the current basic block to the mbb we wish to insert the code into
10472048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      BB = SDB->BitTestCases[i].Parent;
10482048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->setCurrentBasicBlock(BB);
10494198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // Emit the code
10502048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->visitBitTestHeader(SDB->BitTestCases[i]);
10512048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      CurDAG->setRoot(SDB->getRoot());
1052f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman      CodeGenAndEmitDAG();
10532048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->clear();
1054a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    }
10554198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov
10562048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size(); j != ej; ++j) {
10574198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // Set the current basic block to the mbb we wish to insert the code into
10582048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      BB = SDB->BitTestCases[i].Cases[j].ThisBB;
10592048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->setCurrentBasicBlock(BB);
10604198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // Emit the code
10614198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      if (j+1 != ej)
10622048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        SDB->visitBitTestCase(SDB->BitTestCases[i].Cases[j+1].ThisBB,
10632048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman                              SDB->BitTestCases[i].Reg,
10642048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman                              SDB->BitTestCases[i].Cases[j]);
10654198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      else
10662048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        SDB->visitBitTestCase(SDB->BitTestCases[i].Default,
10672048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman                              SDB->BitTestCases[i].Reg,
10682048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman                              SDB->BitTestCases[i].Cases[j]);
1069a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1070a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
10712048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      CurDAG->setRoot(SDB->getRoot());
1072f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman      CodeGenAndEmitDAG();
10732048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->clear();
10744198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov    }
10754198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov
10764198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov    // Update PHI Nodes
10772048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    for (unsigned pi = 0, pe = SDB->PHINodesToUpdate.size(); pi != pe; ++pi) {
10782048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      MachineInstr *PHI = SDB->PHINodesToUpdate[pi].first;
10794198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      MachineBasicBlock *PHIBB = PHI->getParent();
1080518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner      assert(PHI->isPHI() &&
10814198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov             "This is not a machine PHI node that we are updating!");
10824198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // This is "default" BB. We have two jumps to it. From "header" BB and
10834198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // from last "case" BB.
10842048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      if (PHIBB == SDB->BitTestCases[i].Default) {
1085f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach        PHI->addOperand(MachineOperand::
1086f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach                        CreateReg(SDB->PHINodesToUpdate[pi].second, false));
10872048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        PHI->addOperand(MachineOperand::CreateMBB(SDB->BitTestCases[i].Parent));
1088f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach        PHI->addOperand(MachineOperand::
1089f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach                        CreateReg(SDB->PHINodesToUpdate[pi].second, false));
10902048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        PHI->addOperand(MachineOperand::CreateMBB(SDB->BitTestCases[i].Cases.
10919ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner                                                  back().ThisBB));
10924198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      }
10934198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // One of "cases" BB.
10942048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size();
10957c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman           j != ej; ++j) {
10962048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        MachineBasicBlock* cBB = SDB->BitTestCases[i].Cases[j].ThisBB;
1097dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen        if (cBB->isSuccessor(PHIBB)) {
1098f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach          PHI->addOperand(MachineOperand::
1099f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach                          CreateReg(SDB->PHINodesToUpdate[pi].second, false));
11009ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner          PHI->addOperand(MachineOperand::CreateMBB(cBB));
11014198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov        }
11024198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      }
11034198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov    }
11044198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov  }
11052048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->BitTestCases.clear();
11064198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov
11079453eea49bca1c3904047db495855c93e001d1cdNate Begeman  // If the JumpTable record is filled in, then we need to emit a jump table.
11089453eea49bca1c3904047db495855c93e001d1cdNate Begeman  // Updating the PHI nodes is tricky in this case, since we need to determine
11099453eea49bca1c3904047db495855c93e001d1cdNate Begeman  // whether the PHI is a successor of the range check MBB or the jump table MBB
11102048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  for (unsigned i = 0, e = SDB->JTCases.size(); i != e; ++i) {
11113a84b9baf67c938454840d10d9e53269c755cd3eAnton Korobeynikov    // Lower header first, if it wasn't already lowered
11122048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    if (!SDB->JTCases[i].first.Emitted) {
11133a84b9baf67c938454840d10d9e53269c755cd3eAnton Korobeynikov      // Set the current basic block to the mbb we wish to insert the code into
11142048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      BB = SDB->JTCases[i].first.HeaderBB;
11152048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->setCurrentBasicBlock(BB);
11163a84b9baf67c938454840d10d9e53269c755cd3eAnton Korobeynikov      // Emit the code
11172048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->visitJumpTableHeader(SDB->JTCases[i].second, SDB->JTCases[i].first);
11182048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      CurDAG->setRoot(SDB->getRoot());
1119f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman      CodeGenAndEmitDAG();
11202048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->clear();
11214198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov    }
1122a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
112337efe6764568a3829fee26aba532283131d1a104Nate Begeman    // Set the current basic block to the mbb we wish to insert the code into
11242048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    BB = SDB->JTCases[i].second.MBB;
11252048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->setCurrentBasicBlock(BB);
112637efe6764568a3829fee26aba532283131d1a104Nate Begeman    // Emit the code
11272048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->visitJumpTable(SDB->JTCases[i].second);
11282048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    CurDAG->setRoot(SDB->getRoot());
1129f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    CodeGenAndEmitDAG();
11302048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->clear();
1131a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
113237efe6764568a3829fee26aba532283131d1a104Nate Begeman    // Update PHI Nodes
11332048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    for (unsigned pi = 0, pe = SDB->PHINodesToUpdate.size(); pi != pe; ++pi) {
11342048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      MachineInstr *PHI = SDB->PHINodesToUpdate[pi].first;
113537efe6764568a3829fee26aba532283131d1a104Nate Begeman      MachineBasicBlock *PHIBB = PHI->getParent();
1136518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner      assert(PHI->isPHI() &&
113737efe6764568a3829fee26aba532283131d1a104Nate Begeman             "This is not a machine PHI node that we are updating!");
11384198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // "default" BB. We can go there only from header BB.
11392048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      if (PHIBB == SDB->JTCases[i].second.Default) {
1140ce31910eae5bd4896fa6c27798e7b26885691d3bEvan Cheng        PHI->addOperand
11412048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman          (MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second, false));
1142ce31910eae5bd4896fa6c27798e7b26885691d3bEvan Cheng        PHI->addOperand
11432048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman          (MachineOperand::CreateMBB(SDB->JTCases[i].first.HeaderBB));
1144f4360a478944af45d5f851a0903fbbfa44f520dcNate Begeman      }
11454198c58c716cbe4516ac3a1a407a3cd52548bc3bAnton Korobeynikov      // JT BB. Just iterate over successors here
1146dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen      if (BB->isSuccessor(PHIBB)) {
1147ce31910eae5bd4896fa6c27798e7b26885691d3bEvan Cheng        PHI->addOperand
11482048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman          (MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second, false));
11499ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner        PHI->addOperand(MachineOperand::CreateMBB(BB));
115037efe6764568a3829fee26aba532283131d1a104Nate Begeman      }
115137efe6764568a3829fee26aba532283131d1a104Nate Begeman    }
115237efe6764568a3829fee26aba532283131d1a104Nate Begeman  }
11532048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->JTCases.clear();
1154a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1155b2e806eecd85c98367f038817e742ef183dd9f87Chris Lattner  // If the switch block involved a branch to one of the actual successors, we
1156b2e806eecd85c98367f038817e742ef183dd9f87Chris Lattner  // need to update PHI nodes in that block.
11572048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  for (unsigned i = 0, e = SDB->PHINodesToUpdate.size(); i != e; ++i) {
11582048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    MachineInstr *PHI = SDB->PHINodesToUpdate[i].first;
1159518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner    assert(PHI->isPHI() &&
1160b2e806eecd85c98367f038817e742ef183dd9f87Chris Lattner           "This is not a machine PHI node that we are updating!");
1161b2e806eecd85c98367f038817e742ef183dd9f87Chris Lattner    if (BB->isSuccessor(PHI->getParent())) {
11622048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[i].second,
11639ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner                                                false));
11649ce2e9d5a07cb3e0176cb32838231243829d67c5Chris Lattner      PHI->addOperand(MachineOperand::CreateMBB(BB));
1165b2e806eecd85c98367f038817e742ef183dd9f87Chris Lattner    }
1166b2e806eecd85c98367f038817e742ef183dd9f87Chris Lattner  }
1167a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1168f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman  // If we generated any switch lowering information, build and codegen any
1169f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman  // additional DAGs necessary.
11702048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  for (unsigned i = 0, e = SDB->SwitchCases.size(); i != e; ++i) {
1171f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman    // Set the current basic block to the mbb we wish to insert the code into
11722048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    MachineBasicBlock *ThisBB = BB = SDB->SwitchCases[i].ThisBB;
11732048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->setCurrentBasicBlock(BB);
1174a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1175f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman    // Emit the code
11762048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->visitSwitchCase(SDB->SwitchCases[i]);
11772048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    CurDAG->setRoot(SDB->getRoot());
1178f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman    CodeGenAndEmitDAG();
1179a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1180d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner    // Handle any PHI nodes in successors of this chunk, as if we were coming
1181d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner    // from the original BB before switch expansion.  Note that PHI nodes can
1182d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner    // occur multiple times in PHINodesToUpdate.  We have to be very careful to
1183d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner    // handle them the right number of times.
11842048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    while ((BB = SDB->SwitchCases[i].TrueBB)) {  // Handle LHS and RHS.
1185fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng      // If new BB's are created during scheduling, the edges may have been
1186ce31910eae5bd4896fa6c27798e7b26885691d3bEvan Cheng      // updated. That is, the edge from ThisBB to BB may have been split and
1187ce31910eae5bd4896fa6c27798e7b26885691d3bEvan Cheng      // BB's predecessor is now another block.
1188fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng      DenseMap<MachineBasicBlock*, MachineBasicBlock*>::iterator EI =
11892048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        SDB->EdgeMapping.find(BB);
11902048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      if (EI != SDB->EdgeMapping.end())
1191fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng        ThisBB = EI->second;
1192dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen
1193dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen      // BB may have been removed from the CFG if a branch was constant folded.
1194dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen      if (ThisBB->isSuccessor(BB)) {
1195dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen        for (MachineBasicBlock::iterator Phi = BB->begin();
1196518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner             Phi != BB->end() && Phi->isPHI();
1197dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen             ++Phi) {
1198dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen          // This value for this PHI node is recorded in PHINodesToUpdate.
1199dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen          for (unsigned pn = 0; ; ++pn) {
1200dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen            assert(pn != SDB->PHINodesToUpdate.size() &&
1201dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen                   "Didn't find PHI entry!");
1202dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen            if (SDB->PHINodesToUpdate[pn].first == Phi) {
1203dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen              Phi->addOperand(MachineOperand::
1204dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen                              CreateReg(SDB->PHINodesToUpdate[pn].second,
1205dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen                                        false));
1206dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen              Phi->addOperand(MachineOperand::CreateMBB(ThisBB));
1207dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen              break;
1208dd437ba15e00d1a137e0c6e3ca1ff213ed6a2aa0Jakob Stoklund Olesen            }
12098be58a11868ae081c195c107cd39a07f1f523877Evan Cheng          }
1210d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner        }
1211f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman      }
1212a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1213d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner      // Don't process RHS if same block as LHS.
12142048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      if (BB == SDB->SwitchCases[i].FalseBB)
12152048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman        SDB->SwitchCases[i].FalseBB = 0;
1216a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1217d5e93c07957844e3086fb12d686b93dbbd2524c2Chris Lattner      // If we haven't handled the RHS, do so now.  Otherwise, we're done.
12182048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->SwitchCases[i].TrueBB = SDB->SwitchCases[i].FalseBB;
12192048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman      SDB->SwitchCases[i].FalseBB = 0;
1220f15485a8d0dff5f720b7ad27346129ac5c3ec503Nate Begeman    }
12212048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    assert(SDB->SwitchCases[i].TrueBB == 0 && SDB->SwitchCases[i].FalseBB == 0);
12222048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman    SDB->clear();
1223a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  }
12242048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->SwitchCases.clear();
12257c3234c6be0dc0bdf4b5d6f848cd728a77f349d7Dan Gohman
12262048b85c7c2c987874b9423e682ec8e60b60574bDan Gohman  SDB->PHINodesToUpdate.clear();
12271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
1228a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng
122913ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey
12300a3776dc6f702d4e3f4a189df7f43d8f99957773Dan Gohman/// Create the scheduler. If a specific scheduler was specified
12310a3776dc6f702d4e3f4a189df7f43d8f99957773Dan Gohman/// via the SchedulerRegistry, use it, otherwise select the
12320a3776dc6f702d4e3f4a189df7f43d8f99957773Dan Gohman/// one preferred by the target.
12335e84368b26415b3bb7f3f8d9cff3e05938dd82d0Dan Gohman///
123447ac0f0c7c39289f5970688154e385be22b7f293Dan GohmanScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
1235eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey  RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
1236a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
123713ec702c430b91ee49b9e6d9581cd95412f216c8Jim Laskey  if (!Ctor) {
1238eb577ba3b815a1fa4627b060dd2345d17abf672dJim Laskey    Ctor = ISHeuristic;
12399373beba6010dd34316a801c3a9b37ab9e048031Jim Laskey    RegisterScheduler::setDefault(Ctor);
12404ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  }
1241a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1242be8cc2a3dedeb7685f07e68cdc4b9502eb97eb2bBill Wendling  return Ctor(this, OptLevel);
1243a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng}
12440e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
1245fc54c552963545a81e4ea38e60460590afb2d5aeDan GohmanScheduleHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
1246fc54c552963545a81e4ea38e60460590afb2d5aeDan Gohman  return new ScheduleHazardRecognizer();
12479ff542f2cce5bf7bf3cf9f692cf3ec0690ad2b3bJim Laskey}
12489ff542f2cce5bf7bf3cf9f692cf3ec0690ad2b3bJim Laskey
1249755480681c795d585c73556c028b6143c46bda86Chris Lattner//===----------------------------------------------------------------------===//
1250755480681c795d585c73556c028b6143c46bda86Chris Lattner// Helper functions used by the generated instruction selector.
1251755480681c795d585c73556c028b6143c46bda86Chris Lattner//===----------------------------------------------------------------------===//
1252755480681c795d585c73556c028b6143c46bda86Chris Lattner// Calls to these methods are generated by tblgen.
1253755480681c795d585c73556c028b6143c46bda86Chris Lattner
1254755480681c795d585c73556c028b6143c46bda86Chris Lattner/// CheckAndMask - The isel is trying to match something like (and X, 255).  If
1255755480681c795d585c73556c028b6143c46bda86Chris Lattner/// the dag combiner simplified the 255, we still want to match.  RHS is the
1256755480681c795d585c73556c028b6143c46bda86Chris Lattner/// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
1257755480681c795d585c73556c028b6143c46bda86Chris Lattner/// specified in the .td file (e.g. 255).
1258a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbarbool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
1259dc9b3d0bb5ed3a3f59cad61e264f0ec43c946459Dan Gohman                                    int64_t DesiredMaskS) const {
12602e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  const APInt &ActualMask = RHS->getAPIntValue();
12612e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
1262a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1263755480681c795d585c73556c028b6143c46bda86Chris Lattner  // If the actual mask exactly matches, success!
1264755480681c795d585c73556c028b6143c46bda86Chris Lattner  if (ActualMask == DesiredMask)
1265755480681c795d585c73556c028b6143c46bda86Chris Lattner    return true;
1266a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1267755480681c795d585c73556c028b6143c46bda86Chris Lattner  // If the actual AND mask is allowing unallowed bits, this doesn't match.
12682e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  if (ActualMask.intersects(~DesiredMask))
1269755480681c795d585c73556c028b6143c46bda86Chris Lattner    return false;
1270a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1271755480681c795d585c73556c028b6143c46bda86Chris Lattner  // Otherwise, the DAG Combiner may have proven that the value coming in is
1272755480681c795d585c73556c028b6143c46bda86Chris Lattner  // either already zero or is not demanded.  Check for known zero input bits.
12732e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  APInt NeededMask = DesiredMask & ~ActualMask;
1274ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman  if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
1275755480681c795d585c73556c028b6143c46bda86Chris Lattner    return true;
1276a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1277755480681c795d585c73556c028b6143c46bda86Chris Lattner  // TODO: check to see if missing bits are just not demanded.
1278755480681c795d585c73556c028b6143c46bda86Chris Lattner
1279755480681c795d585c73556c028b6143c46bda86Chris Lattner  // Otherwise, this pattern doesn't match.
1280755480681c795d585c73556c028b6143c46bda86Chris Lattner  return false;
1281755480681c795d585c73556c028b6143c46bda86Chris Lattner}
1282755480681c795d585c73556c028b6143c46bda86Chris Lattner
1283755480681c795d585c73556c028b6143c46bda86Chris Lattner/// CheckOrMask - The isel is trying to match something like (or X, 255).  If
1284755480681c795d585c73556c028b6143c46bda86Chris Lattner/// the dag combiner simplified the 255, we still want to match.  RHS is the
1285755480681c795d585c73556c028b6143c46bda86Chris Lattner/// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
1286755480681c795d585c73556c028b6143c46bda86Chris Lattner/// specified in the .td file (e.g. 255).
1287a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbarbool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
12882e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                   int64_t DesiredMaskS) const {
12892e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  const APInt &ActualMask = RHS->getAPIntValue();
12902e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
1291a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1292755480681c795d585c73556c028b6143c46bda86Chris Lattner  // If the actual mask exactly matches, success!
1293755480681c795d585c73556c028b6143c46bda86Chris Lattner  if (ActualMask == DesiredMask)
1294755480681c795d585c73556c028b6143c46bda86Chris Lattner    return true;
1295a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1296755480681c795d585c73556c028b6143c46bda86Chris Lattner  // If the actual AND mask is allowing unallowed bits, this doesn't match.
12972e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  if (ActualMask.intersects(~DesiredMask))
1298755480681c795d585c73556c028b6143c46bda86Chris Lattner    return false;
1299a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1300755480681c795d585c73556c028b6143c46bda86Chris Lattner  // Otherwise, the DAG Combiner may have proven that the value coming in is
1301755480681c795d585c73556c028b6143c46bda86Chris Lattner  // either already zero or is not demanded.  Check for known zero input bits.
13022e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  APInt NeededMask = DesiredMask & ~ActualMask;
1303a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
13042e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman  APInt KnownZero, KnownOne;
1305ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman  CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
1306a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1307755480681c795d585c73556c028b6143c46bda86Chris Lattner  // If all the missing bits in the or are already known to be set, match!
1308755480681c795d585c73556c028b6143c46bda86Chris Lattner  if ((NeededMask & KnownOne) == NeededMask)
1309755480681c795d585c73556c028b6143c46bda86Chris Lattner    return true;
1310a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1311755480681c795d585c73556c028b6143c46bda86Chris Lattner  // TODO: check to see if missing bits are just not demanded.
1312a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1313755480681c795d585c73556c028b6143c46bda86Chris Lattner  // Otherwise, this pattern doesn't match.
1314755480681c795d585c73556c028b6143c46bda86Chris Lattner  return false;
1315755480681c795d585c73556c028b6143c46bda86Chris Lattner}
1316755480681c795d585c73556c028b6143c46bda86Chris Lattner
13179ff542f2cce5bf7bf3cf9f692cf3ec0690ad2b3bJim Laskey
13180e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
13190e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner/// by tblgen.  Others should not call it.
13200e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattnervoid SelectionDAGISel::
1321f350b277f32d7d47f86c0e54f4aec4d470500618Dan GohmanSelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) {
1322475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  std::vector<SDValue> InOps;
13230e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  std::swap(InOps, Ops);
13240e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
13250e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  Ops.push_back(InOps[0]);  // input chain.
13260e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  Ops.push_back(InOps[1]);  // input asm string.
13270e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
13280e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  unsigned i = 2, e = InOps.size();
1329825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (InOps[e-1].getValueType() == MVT::Flag)
13300e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    --e;  // Don't process a flag operand if it is here.
1331a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
13320e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  while (i != e) {
1333f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman    unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
133486b49f8e2de796cb46c7c8b6a4c4900533fd53f4Dale Johannesen    if ((Flags & 7) != 4 /*MEM*/) {
13350e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      // Just skip over this operand, copying the operands verbatim.
1336697cbbfb00c318f98d6eb51945f077e2bfe8781eEvan Cheng      Ops.insert(Ops.end(), InOps.begin()+i,
1337697cbbfb00c318f98d6eb51945f077e2bfe8781eEvan Cheng                 InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
1338697cbbfb00c318f98d6eb51945f077e2bfe8781eEvan Cheng      i += InlineAsm::getNumOperandRegisters(Flags) + 1;
13390e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    } else {
1340697cbbfb00c318f98d6eb51945f077e2bfe8781eEvan Cheng      assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
1341697cbbfb00c318f98d6eb51945f077e2bfe8781eEvan Cheng             "Memory operand with multiple values?");
13420e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      // Otherwise, this is a memory operand.  Ask the target to select it.
1343475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      std::vector<SDValue> SelOps;
1344f350b277f32d7d47f86c0e54f4aec4d470500618Dan Gohman      if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps)) {
13457d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin        llvm_report_error("Could not match memory address.  Inline asm"
13467d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin                          " failure!");
13470e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      }
1348a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
13490e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      // Add this to the output node.
135086b49f8e2de796cb46c7c8b6a4c4900533fd53f4Dale Johannesen      Ops.push_back(CurDAG->getTargetConstant(4/*MEM*/ | (SelOps.size()<< 3),
13519949933d6eb406bc74f7df7a0180a11c050a236fDale Johannesen                                              MVT::i32));
13520e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
13530e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      i += 2;
13540e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    }
13550e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  }
1356a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
13570e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  // Add the flag input back if present.
13580e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  if (e != InOps.size())
13590e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    Ops.push_back(InOps.back());
13600e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner}
1361794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
1362e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson/// findFlagUse - Return use of EVT::Flag value produced by the specified
1363c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// SDNode.
1364c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov///
1365c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikovstatic SDNode *findFlagUse(SDNode *N) {
1366c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  unsigned FlagResNo = N->getNumValues()-1;
1367c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
1368c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    SDUse &Use = I.getUse();
1369c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    if (Use.getResNo() == FlagResNo)
1370c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov      return Use.getUser();
1371c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  }
1372c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  return NULL;
1373c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov}
1374c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1375c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
1376c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// This function recursively traverses up the operand chain, ignoring
1377c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// certain nodes.
1378c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikovstatic bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
1379d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner                          SDNode *Root, SmallPtrSet<SDNode*, 16> &Visited,
1380d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner                          bool IgnoreChains) {
1381da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // The NodeID's are given uniques ID's where a node ID is guaranteed to be
1382da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // greater than all of its (recursive) operands.  If we scan to a point where
1383da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // 'use' is smaller than the node we're scanning for, then we know we will
1384da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // never find it.
1385da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  //
1386da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // The Use may be -1 (unassigned) if it is a newly allocated node.  This can
1387da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // happen because we scan down to newly selected nodes in the case of flag
1388da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // uses.
1389da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  if ((Use->getNodeId() < Def->getNodeId() && Use->getNodeId() != -1))
1390da244a091dcde022ea4938793d81ee957903ed70Chris Lattner    return false;
1391da244a091dcde022ea4938793d81ee957903ed70Chris Lattner
1392da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // Don't revisit nodes if we already scanned it and didn't fail, we know we
1393da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  // won't fail if we scan it again.
1394da244a091dcde022ea4938793d81ee957903ed70Chris Lattner  if (!Visited.insert(Use))
1395c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    return false;
1396c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1397c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
1398d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner    // Ignore chain uses, they are validated by HandleMergeInputChains.
1399d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner    if (Use->getOperand(i).getValueType() == MVT::Other && IgnoreChains)
1400d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner      continue;
1401d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner
1402c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    SDNode *N = Use->getOperand(i).getNode();
1403c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    if (N == Def) {
1404c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov      if (Use == ImmedUse || Use == Root)
1405c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov        continue;  // We are not looking for immediate use.
1406c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov      assert(N != Root);
1407c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov      return true;
1408c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    }
1409c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1410c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    // Traverse up the operand chain.
1411d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner    if (findNonImmUse(N, Def, ImmedUse, Root, Visited, IgnoreChains))
1412c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov      return true;
1413c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  }
1414c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  return false;
1415c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov}
1416c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1417c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// isNonImmUse - Start searching from Root up the DAG to check is Def can
1418c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// be reached. Return true if that's the case. However, ignore direct uses
1419c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// by ImmedUse (which would be U in the example illustrated in
1420014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng/// IsLegalToFold) and by Root (which can happen in the store case).
1421c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// FIXME: to be really generic, we should allow direct use by any node
1422c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// that is being folded. But realisticly since we only fold loads which
1423c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// have one non-chain use, we only need to watch out for load/op/store
1424c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// and load/op/cmp case where the root (store / cmp) may reach the load via
1425c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov/// its chain operand.
1426d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattnerstatic inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
1427d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner                               bool IgnoreChains) {
1428c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  SmallPtrSet<SDNode*, 16> Visited;
1429d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner  return findNonImmUse(Root, Def, ImmedUse, Root, Visited, IgnoreChains);
1430c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov}
1431c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1432014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng/// IsProfitableToFold - Returns true if it's profitable to fold the specific
1433014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng/// operand node N of U during instruction selection that starts at Root.
1434014bf215c3457bb34fee348265e8f63a70b4d503Evan Chengbool SelectionDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
1435014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng                                          SDNode *Root) const {
1436014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng  if (OptLevel == CodeGenOpt::None) return false;
1437014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng  return N.hasOneUse();
1438014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng}
1439014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng
1440014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng/// IsLegalToFold - Returns true if the specific operand node N of
1441014bf215c3457bb34fee348265e8f63a70b4d503Evan Cheng/// U can be folded during instruction selection that starts at Root.
1442d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattnerbool SelectionDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
1443d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner                                     bool IgnoreChains) const {
1444c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  if (OptLevel == CodeGenOpt::None) return false;
1445c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1446c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // If Root use can somehow reach N through a path that that doesn't contain
1447c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // U then folding N would create a cycle. e.g. In the following
1448c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // diagram, Root can reach N through X. If N is folded into into Root, then
1449c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // X is both a predecessor and a successor of U.
1450c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //
1451c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          [N*]           //
1452c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         ^   ^           //
1453c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //        /     \          //
1454c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //      [U*]    [X]?       //
1455c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //        ^     ^          //
1456c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         \   /           //
1457c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          \ /            //
1458c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         [Root*]         //
1459c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //
1460c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // * indicates nodes to be folded together.
1461c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //
1462c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // If Root produces a flag, then it gets (even more) interesting. Since it
1463c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // will be "glued" together with its flag use in the scheduler, we need to
1464c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // check if it might reach N.
1465c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //
1466c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          [N*]           //
1467c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         ^   ^           //
1468c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //        /     \          //
1469c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //      [U*]    [X]?       //
1470c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //        ^       ^        //
1471c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         \       \       //
1472c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          \      |       //
1473c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         [Root*] |       //
1474c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          ^      |       //
1475c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          f      |       //
1476c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          |      /       //
1477c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //         [Y]    /        //
1478c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //           ^   /         //
1479c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //           f  /          //
1480c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //           | /           //
1481c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //          [FU]           //
1482c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  //
1483c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // If FU (flag use) indirectly reaches N (the load), and Root folds N
1484c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // (call it Fold), then X is a predecessor of FU and a successor of
1485c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // Fold. But since Fold and FU are flagged together, this will create
1486c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  // a cycle in the scheduling graph.
1487c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1488e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = Root->getValueType(Root->getNumValues()-1);
1489825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  while (VT == MVT::Flag) {
1490c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    SDNode *FU = findFlagUse(Root);
1491c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    if (FU == NULL)
1492c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov      break;
1493c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    Root = FU;
1494c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov    VT = Root->getValueType(Root->getNumValues()-1);
1495c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov  }
1496c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1497d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner  return !isNonImmUse(Root, N.getNode(), U, IgnoreChains);
1498c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov}
1499c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
1500eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan GohmanSDNode *SelectionDAGISel::Select_INLINEASM(SDNode *N) {
1501eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan Gohman  std::vector<SDValue> Ops(N->op_begin(), N->op_end());
1502e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  SelectInlineAsmMemoryOperands(Ops);
1503e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman
1504e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  std::vector<EVT> VTs;
1505e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  VTs.push_back(MVT::Other);
1506e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  VTs.push_back(MVT::Flag);
1507eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan Gohman  SDValue New = CurDAG->getNode(ISD::INLINEASM, N->getDebugLoc(),
1508e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman                                VTs, &Ops[0], Ops.size());
1509d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner  New->setNodeId(-1);
1510e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  return New.getNode();
1511e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman}
1512e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman
1513eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan GohmanSDNode *SelectionDAGISel::Select_UNDEF(SDNode *N) {
1514518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner  return CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF,N->getValueType(0));
1515e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman}
1516e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman
1517eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan GohmanSDNode *SelectionDAGISel::Select_EH_LABEL(SDNode *N) {
1518eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan Gohman  SDValue Chain = N->getOperand(0);
1519e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  unsigned C = cast<LabelSDNode>(N)->getLabelID();
1520e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);
1521518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner  return CurDAG->SelectNodeTo(N, TargetOpcode::EH_LABEL,
1522e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman                              MVT::Other, Tmp, Chain);
1523e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman}
1524e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman
15252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner/// GetVBR - decode a vbr encoding whose top bit is set.
15262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris LattnerALWAYS_INLINE static uint64_t
15272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris LattnerGetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) {
15282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  assert(Val >= 128 && "Not a VBR");
15292a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  Val &= 127;  // Remove first vbr bit.
15302a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
15312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  unsigned Shift = 7;
15322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  uint64_t NextBits;
15332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  do {
153414df8dc680d40173f34d693f9dc451f8a30c907bChris Lattner    NextBits = MatcherTable[Idx++];
15352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    Val |= (NextBits&127) << Shift;
15362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    Shift += 7;
15372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  } while (NextBits & 128);
15382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
15392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  return Val;
15402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner}
15412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
15422a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
15432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner/// UpdateChainsAndFlags - When a match is complete, this method updates uses of
15442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner/// interior flag and chain results to use the new flag and chain results.
154582dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattnervoid SelectionDAGISel::
154682dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris LattnerUpdateChainsAndFlags(SDNode *NodeToMatch, SDValue InputChain,
154782dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                     const SmallVectorImpl<SDNode*> &ChainNodesMatched,
154882dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                     SDValue InputFlag,
154982dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                     const SmallVectorImpl<SDNode*> &FlagResultNodesMatched,
155082dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                     bool isMorphNodeTo) {
155182dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner  SmallVector<SDNode*, 4> NowDeadNodes;
155282dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
155382dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner  ISelUpdater ISU(ISelPosition);
155482dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
15552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // Now that all the normal results are replaced, we replace the chain and
15562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // flag results if present.
15572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  if (!ChainNodesMatched.empty()) {
15582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    assert(InputChain.getNode() != 0 &&
15592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner           "Matched input chains but didn't produce a chain");
15602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    // Loop over all of the nodes we matched that produced a chain result.
15612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    // Replace all the chain results with the final chain we ended up with.
15622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
15632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SDNode *ChainNode = ChainNodesMatched[i];
15642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
156582dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      // If this node was already deleted, don't look at it.
156682dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      if (ChainNode->getOpcode() == ISD::DELETED_NODE)
156782dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner        continue;
156882dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
15692a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Don't replace the results of the root node if we're doing a
15702a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // MorphNodeTo.
15712a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (ChainNode == NodeToMatch && isMorphNodeTo)
15722a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        continue;
15732a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
15742a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1);
15752a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (ChainVal.getValueType() == MVT::Flag)
15762a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2);
15772a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(ChainVal.getValueType() == MVT::Other && "Not a chain?");
157882dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      CurDAG->ReplaceAllUsesOfValueWith(ChainVal, InputChain, &ISU);
157982dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
158082dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      // If the node became dead, delete it.
158182dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      if (ChainNode->use_empty())
158282dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner        NowDeadNodes.push_back(ChainNode);
15832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
15842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  }
15852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
15862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // If the result produces a flag, update any flag results in the matched
15872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // pattern with the flag result.
15882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  if (InputFlag.getNode() != 0) {
15892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    // Handle any interior nodes explicitly marked.
15902a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    for (unsigned i = 0, e = FlagResultNodesMatched.size(); i != e; ++i) {
15912a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SDNode *FRN = FlagResultNodesMatched[i];
159282dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
159382dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      // If this node was already deleted, don't look at it.
159482dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      if (FRN->getOpcode() == ISD::DELETED_NODE)
159582dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner        continue;
159682dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
15972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(FRN->getValueType(FRN->getNumValues()-1) == MVT::Flag &&
15982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner             "Doesn't have a flag result");
15992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      CurDAG->ReplaceAllUsesOfValueWith(SDValue(FRN, FRN->getNumValues()-1),
160082dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                                        InputFlag, &ISU);
160182dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
160282dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      // If the node became dead, delete it.
160382dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      if (FRN->use_empty())
160482dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner        NowDeadNodes.push_back(FRN);
16052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
16062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  }
16072a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
160882dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner  if (!NowDeadNodes.empty())
160982dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner    CurDAG->RemoveDeadNodes(NowDeadNodes, &ISU);
161082dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner
16112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  DEBUG(errs() << "ISEL: Match complete!\n");
16122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner}
16132a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
1614c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattnerenum ChainResult {
1615c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  CR_Simple,
1616c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  CR_InducesCycle,
1617c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  CR_LeadsToInteriorNode
1618c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner};
1619c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1620c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// WalkChainUsers - Walk down the users of the specified chained node that is
1621c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// part of the pattern we're matching, looking at all of the users we find.
1622c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// This determines whether something is an interior node, whether we have a
1623c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// non-pattern node in between two pattern nodes (which prevent folding because
1624c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// it would induce a cycle) and whether we have a TokenFactor node sandwiched
1625c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// between pattern nodes (in which case the TF becomes part of the pattern).
1626c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner///
1627c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// The walk we do here is guaranteed to be small because we quickly get down to
1628c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// already selected nodes "below" us.
1629c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattnerstatic ChainResult
1630c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris LattnerWalkChainUsers(SDNode *ChainedNode,
1631c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner               SmallVectorImpl<SDNode*> &ChainedNodesInPattern,
1632c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner               SmallVectorImpl<SDNode*> &InteriorChainedNodes) {
1633c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  ChainResult Result = CR_Simple;
1634c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1635c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  for (SDNode::use_iterator UI = ChainedNode->use_begin(),
1636c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner         E = ChainedNode->use_end(); UI != E; ++UI) {
1637c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // Make sure the use is of the chain, not some other value we produce.
1638c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    if (UI.getUse().getValueType() != MVT::Other) continue;
1639c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1640c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    SDNode *User = *UI;
1641c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1642c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // If we see an already-selected machine node, then we've gone beyond the
1643c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // pattern that we're selecting down into the already selected chunk of the
1644c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // DAG.
1645c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    if (User->isMachineOpcode() ||
1646c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner        User->getOpcode() == ISD::HANDLENODE)  // Root of the graph.
1647c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      continue;
1648d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner
1649d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner    if (User->getOpcode() == ISD::CopyToReg ||
1650d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner        User->getOpcode() == ISD::CopyFromReg ||
1651d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner        User->getOpcode() == ISD::INLINEASM) {
1652d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner      // If their node ID got reset to -1 then they've already been selected.
1653d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner      // Treat them like a MachineOpcode.
1654d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner      if (User->getNodeId() == -1)
1655d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner        continue;
1656d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner    }
1657c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1658c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // If we have a TokenFactor, we handle it specially.
1659c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    if (User->getOpcode() != ISD::TokenFactor) {
1660c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // If the node isn't a token factor and isn't part of our pattern, then it
1661c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // must be a random chained node in between two nodes we're selecting.
1662c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // This happens when we have something like:
1663c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   x = load ptr
1664c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   call
1665c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   y = x+4
1666c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   store y -> ptr
1667c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // Because we structurally match the load/store as a read/modify/write,
1668c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // but the call is chained between them.  We cannot fold in this case
1669c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // because it would induce a cycle in the graph.
1670c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      if (!std::count(ChainedNodesInPattern.begin(),
1671c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner                      ChainedNodesInPattern.end(), User))
1672c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner        return CR_InducesCycle;
1673c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1674c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // Otherwise we found a node that is part of our pattern.  For example in:
1675c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   x = load ptr
1676c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   y = x+4
1677c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      //   store y -> ptr
1678c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // This would happen when we're scanning down from the load and see the
1679c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // store as a user.  Record that there is a use of ChainedNode that is
1680c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // part of the pattern and keep scanning uses.
1681c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      Result = CR_LeadsToInteriorNode;
1682c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      InteriorChainedNodes.push_back(User);
1683c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      continue;
1684c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    }
1685c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1686c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // If we found a TokenFactor, there are two cases to consider: first if the
1687c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // TokenFactor is just hanging "below" the pattern we're matching (i.e. no
1688c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // uses of the TF are in our pattern) we just want to ignore it.  Second,
1689c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // the TokenFactor can be sandwiched in between two chained nodes, like so:
1690c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //     [Load chain]
1691c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //         ^
1692c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //         |
1693c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //       [Load]
1694c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //       ^    ^
1695c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //       |    \                    DAG's like cheese
1696c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //      /       \                       do you?
1697c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //     /         |
1698c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // [TokenFactor] [Op]
1699c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //     ^          ^
1700c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //     |          |
1701c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //      \        /
1702c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //       \      /
1703c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //       [Store]
1704c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //
1705c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // In this case, the TokenFactor becomes part of our match and we rewrite it
1706c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // as a new TokenFactor.
1707c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    //
1708c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // To distinguish these two cases, do a recursive walk down the uses.
1709c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    switch (WalkChainUsers(User, ChainedNodesInPattern, InteriorChainedNodes)) {
1710c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    case CR_Simple:
1711c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // If the uses of the TokenFactor are just already-selected nodes, ignore
1712c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // it, it is "below" our pattern.
1713c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      continue;
1714c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    case CR_InducesCycle:
1715c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // If the uses of the TokenFactor lead to nodes that are not part of our
1716c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // pattern that are not selected, folding would turn this into a cycle,
1717c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // bail out now.
1718c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      return CR_InducesCycle;
1719c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    case CR_LeadsToInteriorNode:
1720c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      break;  // Otherwise, keep processing.
1721c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    }
1722c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1723c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // Okay, we know we're in the interesting interior case.  The TokenFactor
1724c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // is now going to be considered part of the pattern so that we rewrite its
1725c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // uses (it may have uses that are not part of the pattern) with the
1726c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // ultimate chain result of the generated code.  We will also add its chain
1727c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // inputs as inputs to the ultimate TokenFactor we create.
1728c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    Result = CR_LeadsToInteriorNode;
1729c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    ChainedNodesInPattern.push_back(User);
1730c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    InteriorChainedNodes.push_back(User);
1731c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    continue;
1732c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  }
1733c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1734c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  return Result;
1735c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner}
1736c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
17376b30792d2d7d142b8192241e6232aee004f745fbChris Lattner/// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains
173800592ec857b4c3f31b46412e2738924238c59983Chris Lattner/// operation for when the pattern matched at least one node with a chains.  The
1739c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// input vector contains a list of all of the chained nodes that we match.  We
1740c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// must determine if this is a valid thing to cover (i.e. matching it won't
1741c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// induce cycles in the DAG) and if so, creating a TokenFactor node. that will
1742c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner/// be used as the input node chain for the generated nodes.
17436b30792d2d7d142b8192241e6232aee004f745fbChris Lattnerstatic SDValue
1744c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris LattnerHandleMergeInputChains(SmallVectorImpl<SDNode*> &ChainNodesMatched,
17456b30792d2d7d142b8192241e6232aee004f745fbChris Lattner                       SelectionDAG *CurDAG) {
1746c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  // Walk all of the chained nodes we've matched, recursively scanning down the
1747c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  // users of the chain result. This adds any TokenFactor nodes that are caught
1748c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  // in between chained nodes to the chained and interior nodes list.
1749c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  SmallVector<SDNode*, 3> InteriorChainedNodes;
1750c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
1751c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    if (WalkChainUsers(ChainNodesMatched[i], ChainNodesMatched,
1752c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner                       InteriorChainedNodes) == CR_InducesCycle)
1753c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      return SDValue(); // Would induce a cycle.
1754c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  }
17556b30792d2d7d142b8192241e6232aee004f745fbChris Lattner
1756c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  // Okay, we have walked all the matched nodes and collected TokenFactor nodes
1757c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner  // that we are interested in.  Form our input TokenFactor node.
17586b30792d2d7d142b8192241e6232aee004f745fbChris Lattner  SmallVector<SDValue, 3> InputChains;
17596b30792d2d7d142b8192241e6232aee004f745fbChris Lattner  for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
1760c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // Add the input chain of this node to the InputChains list (which will be
1761c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // the operands of the generated TokenFactor) if it's not an interior node.
1762c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    SDNode *N = ChainNodesMatched[i];
1763c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    if (N->getOpcode() != ISD::TokenFactor) {
1764c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      if (std::count(InteriorChainedNodes.begin(),InteriorChainedNodes.end(),N))
1765c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner        continue;
1766c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1767c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      // Otherwise, add the input chain.
1768c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      SDValue InChain = ChainNodesMatched[i]->getOperand(0);
1769c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      assert(InChain.getValueType() == MVT::Other && "Not a chain");
17706b30792d2d7d142b8192241e6232aee004f745fbChris Lattner      InputChains.push_back(InChain);
1771c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      continue;
1772c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    }
1773c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner
1774c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // If we have a token factor, we want to add all inputs of the token factor
1775c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    // that are not part of the pattern we're matching.
1776c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    for (unsigned op = 0, e = N->getNumOperands(); op != e; ++op) {
1777c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner      if (!std::count(ChainNodesMatched.begin(), ChainNodesMatched.end(),
17786183fbd7267a7de857e32a2106cfb46a219728f1Chris Lattner                      N->getOperand(op).getNode()))
17796183fbd7267a7de857e32a2106cfb46a219728f1Chris Lattner        InputChains.push_back(N->getOperand(op));
1780c6d7ad3c7d83e9af29bf3ba3bf3280e72a952f98Chris Lattner    }
17816b30792d2d7d142b8192241e6232aee004f745fbChris Lattner  }
17826b30792d2d7d142b8192241e6232aee004f745fbChris Lattner
17836b30792d2d7d142b8192241e6232aee004f745fbChris Lattner  SDValue Res;
17846b30792d2d7d142b8192241e6232aee004f745fbChris Lattner  if (InputChains.size() == 1)
17856b30792d2d7d142b8192241e6232aee004f745fbChris Lattner    return InputChains[0];
17866b30792d2d7d142b8192241e6232aee004f745fbChris Lattner  return CurDAG->getNode(ISD::TokenFactor, ChainNodesMatched[0]->getDebugLoc(),
17876b30792d2d7d142b8192241e6232aee004f745fbChris Lattner                         MVT::Other, &InputChains[0], InputChains.size());
17886b30792d2d7d142b8192241e6232aee004f745fbChris Lattner}
17892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
17903ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner/// MorphNode - Handle morphing a node in place for the selector.
17913ff1e4db7896a8ccec74800d52a926118d025260Chris LattnerSDNode *SelectionDAGISel::
17923ff1e4db7896a8ccec74800d52a926118d025260Chris LattnerMorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
17933ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner          const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo) {
17943ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // It is possible we're using MorphNodeTo to replace a node with no
17953ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // normal results with one that has a normal result (or we could be
17963ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // adding a chain) and the input could have flags and chains as well.
17973ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // In this case we need to shifting the operands down.
17983ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // FIXME: This is a horrible hack and broken in obscure cases, no worse
17993ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // than the old isel though.  We should sink this into MorphNodeTo.
18003ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  int OldFlagResultNo = -1, OldChainResultNo = -1;
18013ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18023ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  unsigned NTMNumResults = Node->getNumValues();
18033ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  if (Node->getValueType(NTMNumResults-1) == MVT::Flag) {
18043ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    OldFlagResultNo = NTMNumResults-1;
18053ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    if (NTMNumResults != 1 &&
18063ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner        Node->getValueType(NTMNumResults-2) == MVT::Other)
18073ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner      OldChainResultNo = NTMNumResults-2;
18083ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  } else if (Node->getValueType(NTMNumResults-1) == MVT::Other)
18093ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    OldChainResultNo = NTMNumResults-1;
18103ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
181161c97f693216670cda554d287cd871fdea1b36f9Chris Lattner  // Call the underlying SelectionDAG routine to do the transmogrification. Note
181261c97f693216670cda554d287cd871fdea1b36f9Chris Lattner  // that this deletes operands of the old node that become dead.
18133ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops, NumOps);
18143ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18153ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // MorphNodeTo can operate in two ways: if an existing node with the
18163ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // specified operands exists, it can just return it.  Otherwise, it
18173ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // updates the node in place to have the requested operands.
18183ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  if (Res == Node) {
18193ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    // If we updated the node in place, reset the node ID.  To the isel,
18203ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    // this should be just like a newly allocated machine node.
18213ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    Res->setNodeId(-1);
18223ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  }
18233ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18243ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  unsigned ResNumResults = Res->getNumValues();
18253ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // Move the flag if needed.
18263ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  if ((EmitNodeInfo & OPFL_FlagOutput) && OldFlagResultNo != -1 &&
18273ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner      (unsigned)OldFlagResultNo != ResNumResults-1)
18283ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    CurDAG->ReplaceAllUsesOfValueWith(SDValue(Node, OldFlagResultNo),
18293ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner                                      SDValue(Res, ResNumResults-1));
18303ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18313ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  if ((EmitNodeInfo & OPFL_FlagOutput) != 0)
18323ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  --ResNumResults;
18333ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18343ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // Move the chain reference if needed.
18353ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 &&
18363ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner      (unsigned)OldChainResultNo != ResNumResults-1)
18373ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    CurDAG->ReplaceAllUsesOfValueWith(SDValue(Node, OldChainResultNo),
18383ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner                                      SDValue(Res, ResNumResults-1));
18393ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18403ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // Otherwise, no replacement happened because the node already exists. Replace
18413ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  // Uses of the old node with the new one.
18423ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  if (Res != Node)
18433ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner    CurDAG->ReplaceAllUsesWith(Node, Res);
18443ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18453ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner  return Res;
18463ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner}
18473ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18483ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner
18492a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattnerstruct MatchScope {
18502a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  /// FailIndex - If this match fails, this is the index to continue with.
18512a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  unsigned FailIndex;
18522a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
18532a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  /// NodeStack - The node stack when the scope was formed.
18542a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<SDValue, 4> NodeStack;
18552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
18562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  /// NumRecordedNodes - The number of recorded nodes when the scope was formed.
18572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  unsigned NumRecordedNodes;
18582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
18592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  /// NumMatchedMemRefs - The number of matched memref entries.
18602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  unsigned NumMatchedMemRefs;
18612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
18622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  /// InputChain/InputFlag - The current chain/flag
18632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SDValue InputChain, InputFlag;
18642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
18652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty.
18662a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  bool HasChainNodesMatched, HasFlagResultNodesMatched;
18672a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner};
18682a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
18692a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris LattnerSDNode *SelectionDAGISel::
18702a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris LattnerSelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable,
18712a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                 unsigned TableSize) {
18722a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // FIXME: Should these even be selected?  Handle these cases in the caller?
18732a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  switch (NodeToMatch->getOpcode()) {
18742a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  default:
18752a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    break;
18762a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::EntryToken:       // These nodes remain the same.
18772a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::BasicBlock:
18782a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::Register:
18792a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::HANDLENODE:
18802a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetConstant:
18812a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetConstantFP:
18822a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetConstantPool:
18832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetFrameIndex:
18842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetExternalSymbol:
18852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetBlockAddress:
18862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetJumpTable:
18872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetGlobalTLSAddress:
18882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TargetGlobalAddress:
18892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::TokenFactor:
18902a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::CopyFromReg:
18912a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::CopyToReg:
1892d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner    NodeToMatch->setNodeId(-1); // Mark selected.
18932a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    return 0;
18942a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::AssertSext:
18952a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::AssertZext:
18962a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    CurDAG->ReplaceAllUsesOfValueWith(SDValue(NodeToMatch, 0),
18972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                                      NodeToMatch->getOperand(0));
18982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    return 0;
18992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::INLINEASM: return Select_INLINEASM(NodeToMatch);
19002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::EH_LABEL:  return Select_EH_LABEL(NodeToMatch);
19012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  case ISD::UNDEF:     return Select_UNDEF(NodeToMatch);
19022a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  }
19032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19042a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  assert(!NodeToMatch->isMachineOpcode() && "Node already selected!");
19052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // Set up the node stack with NodeToMatch as the only node on the stack.
19072a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<SDValue, 8> NodeStack;
19082a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SDValue N = SDValue(NodeToMatch, 0);
19092a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  NodeStack.push_back(N);
19102a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // MatchScopes - Scopes used when matching, if a match failure happens, this
19122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // indicates where to continue checking.
19132a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<MatchScope, 8> MatchScopes;
19142a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19152a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // RecordedNodes - This is the set of nodes that have been recorded by the
19162a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // state machine.
19172a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<SDValue, 8> RecordedNodes;
19182a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19192a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // MatchedMemRefs - This is the set of MemRef's we've seen in the input
19202a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // pattern.
19212a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<MachineMemOperand*, 2> MatchedMemRefs;
19222a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19232a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // These are the current input chain and flag for use when generating nodes.
19242a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // Various Emit operations change these.  For example, emitting a copytoreg
19252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // uses and updates these.
19262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SDValue InputChain, InputFlag;
19272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // ChainNodesMatched - If a pattern matches nodes that have input/output
19292a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // chains, the OPC_EmitMergeInputChains operation is emitted which indicates
19302a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // which ones they are.  The result is captured into this list so that we can
19312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  // update the chain results when the pattern is complete.
19322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<SDNode*, 3> ChainNodesMatched;
19332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  SmallVector<SDNode*, 3> FlagResultNodesMatched;
19342a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  DEBUG(errs() << "ISEL: Starting pattern match on root node: ";
19362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        NodeToMatch->dump(CurDAG);
19372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        errs() << '\n');
19382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19397390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  // Determine where to start the interpreter.  Normally we start at opcode #0,
19407390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  // but if the state machine starts with an OPC_SwitchOpcode, then we
19417390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  // accelerate the first lookup (which is guaranteed to be hot) with the
19427390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  // OpcodeOffset table.
19432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  unsigned MatcherIndex = 0;
19447390eebd49b786f3293d24f76fedb02fe183468cChris Lattner
19457390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  if (!OpcodeOffset.empty()) {
19467390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    // Already computed the OpcodeOffset table, just index into it.
19477390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    if (N.getOpcode() < OpcodeOffset.size())
19487390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      MatcherIndex = OpcodeOffset[N.getOpcode()];
19497390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    DEBUG(errs() << "  Initial Opcode index to " << MatcherIndex << "\n");
19507390eebd49b786f3293d24f76fedb02fe183468cChris Lattner
19517390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  } else if (MatcherTable[0] == OPC_SwitchOpcode) {
19527390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    // Otherwise, the table isn't computed, but the state machine does start
19537390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    // with an OPC_SwitchOpcode instruction.  Populate the table now, since this
19547390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    // is the first time we're selecting an instruction.
19557390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    unsigned Idx = 1;
19567390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    while (1) {
19577390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      // Get the size of this case.
19587390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      unsigned CaseSize = MatcherTable[Idx++];
19597390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      if (CaseSize & 128)
19607390eebd49b786f3293d24f76fedb02fe183468cChris Lattner        CaseSize = GetVBR(CaseSize, MatcherTable, Idx);
19617390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      if (CaseSize == 0) break;
19627390eebd49b786f3293d24f76fedb02fe183468cChris Lattner
19637390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      // Get the opcode, add the index to the table.
19647390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      unsigned Opc = MatcherTable[Idx++];
19657390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      if (Opc >= OpcodeOffset.size())
19667390eebd49b786f3293d24f76fedb02fe183468cChris Lattner        OpcodeOffset.resize((Opc+1)*2);
19677390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      OpcodeOffset[Opc] = Idx;
19687390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      Idx += CaseSize;
19697390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    }
19707390eebd49b786f3293d24f76fedb02fe183468cChris Lattner
19717390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    // Okay, do the lookup for the first opcode.
19727390eebd49b786f3293d24f76fedb02fe183468cChris Lattner    if (N.getOpcode() < OpcodeOffset.size())
19737390eebd49b786f3293d24f76fedb02fe183468cChris Lattner      MatcherIndex = OpcodeOffset[N.getOpcode()];
19747390eebd49b786f3293d24f76fedb02fe183468cChris Lattner  }
19757390eebd49b786f3293d24f76fedb02fe183468cChris Lattner
19762a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  while (1) {
19772a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    assert(MatcherIndex < TableSize && "Invalid index");
19782a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    BuiltinOpcodes Opcode = (BuiltinOpcodes)MatcherTable[MatcherIndex++];
19792a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    switch (Opcode) {
19802a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_Scope: {
19812a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumToSkip = MatcherTable[MatcherIndex++];
19822a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (NumToSkip & 128)
19832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
19842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(NumToSkip != 0 &&
19852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner             "First entry of OPC_Scope shouldn't be 0, scope has no children?");
19862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
19872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Push a MatchScope which indicates where to go if the first child fails
19882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // to match.
19892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MatchScope NewEntry;
19902a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.FailIndex = MatcherIndex+NumToSkip;
19912a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end());
19922a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.NumRecordedNodes = RecordedNodes.size();
19932a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.NumMatchedMemRefs = MatchedMemRefs.size();
19942a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.InputChain = InputChain;
19952a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.InputFlag = InputFlag;
19962a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty();
19972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NewEntry.HasFlagResultNodesMatched = !FlagResultNodesMatched.empty();
19982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MatchScopes.push_back(NewEntry);
19992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
20012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_RecordNode:
20022a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Remember this node, it may end up being an operand in the pattern.
20032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.push_back(N);
20042a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
20062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_RecordChild0: case OPC_RecordChild1:
20072a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_RecordChild2: case OPC_RecordChild3:
20082a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_RecordChild4: case OPC_RecordChild5:
20092a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_RecordChild6: case OPC_RecordChild7: {
20102a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned ChildNo = Opcode-OPC_RecordChild0;
20112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (ChildNo >= N.getNumOperands())
20122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;  // Match fails if out of range child #.
20132a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
20142a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.push_back(N->getOperand(ChildNo));
20152a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20162a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
20172a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_RecordMemRef:
20182a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MatchedMemRefs.push_back(cast<MemSDNode>(N)->getMemOperand());
20192a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20202a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
20212a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CaptureFlagInput:
20222a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If the current node has an input flag, capture it in InputFlag.
20232a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (N->getNumOperands() != 0 &&
20242a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag)
20252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        InputFlag = N->getOperand(N->getNumOperands()-1);
20262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
20282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_MoveChild: {
20292a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned ChildNo = MatcherTable[MatcherIndex++];
20302a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (ChildNo >= N.getNumOperands())
20312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;  // Match fails if out of range child #.
20322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      N = N.getOperand(ChildNo);
20332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NodeStack.push_back(N);
20342a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
20362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
20372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_MoveParent:
20382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Pop the current node off the NodeStack.
20392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NodeStack.pop_back();
20402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(!NodeStack.empty() && "Node stack imbalance!");
20412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      N = NodeStack.back();
20422a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
20442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckSame: {
20452a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Accept if it is exactly the same as a previously recorded node.
20462a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned RecNo = MatcherTable[MatcherIndex++];
20472a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
20482a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (N != RecordedNodes[RecNo]) break;
20492a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20502a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
20512a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckPatternPredicate:
20522a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (!CheckPatternPredicate(MatcherTable[MatcherIndex++])) break;
20532a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20542a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckPredicate:
20552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (!CheckNodePredicate(N.getNode(), MatcherTable[MatcherIndex++])) break;
20562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckComplexPat:
20582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (!CheckComplexPattern(NodeToMatch, N,
20592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                               MatcherTable[MatcherIndex++], RecordedNodes))
20602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;
20612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckOpcode:
20632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (N->getOpcode() != MatcherTable[MatcherIndex++]) break;
20642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
20652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
2066eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner    case OPC_SwitchOpcode: {
2067eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      unsigned CurNodeOpcode = N.getOpcode();
20687d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
2069eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      unsigned CaseSize;
2070eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      while (1) {
2071eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        // Get the size of this case.
2072eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        CaseSize = MatcherTable[MatcherIndex++];
2073eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        if (CaseSize & 128)
2074eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner          CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
2075eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        if (CaseSize == 0) break;
2076eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner
2077eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        // If the opcode matches, then we will execute this case.
2078eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        if (CurNodeOpcode == MatcherTable[MatcherIndex++])
2079eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner          break;
2080eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner
2081eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        // Otherwise, skip over this case.
2082eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner        MatcherIndex += CaseSize;
2083eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      }
2084eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner
2085cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      // If no cases matched, bail out.
2086eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      if (CaseSize == 0) break;
2087eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner
2088eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      // Otherwise, execute the case we found.
2089eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      DEBUG(errs() << "  OpcodeSwitch from " << SwitchStart
2090eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner                   << " to " << MatcherIndex << "\n");
2091eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner      continue;
2092eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner    }
2093eb66921adb943ea841e72c8eee4777607c48b70eChris Lattner
20942a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckType: {
20952a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MVT::SimpleValueType VT =
20962a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
20972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (N.getValueType() != VT) {
20982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Handle the case when VT is iPTR.
20992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (VT != MVT::iPTR || N.getValueType() != TLI.getPointerTy())
21002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          break;
21012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
21022a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
21032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
2104cfe2eab7446dedc471592fe702fefef783383171Chris Lattner
2105cfe2eab7446dedc471592fe702fefef783383171Chris Lattner    case OPC_SwitchType: {
2106cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      MVT::SimpleValueType CurNodeVT = N.getValueType().getSimpleVT().SimpleTy;
2107cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
2108cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      unsigned CaseSize;
2109cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      while (1) {
2110cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        // Get the size of this case.
2111cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        CaseSize = MatcherTable[MatcherIndex++];
2112cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        if (CaseSize & 128)
2113cfe2eab7446dedc471592fe702fefef783383171Chris Lattner          CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
2114cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        if (CaseSize == 0) break;
2115cfe2eab7446dedc471592fe702fefef783383171Chris Lattner
2116cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        MVT::SimpleValueType CaseVT =
2117cfe2eab7446dedc471592fe702fefef783383171Chris Lattner          (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
2118cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        if (CaseVT == MVT::iPTR)
2119cfe2eab7446dedc471592fe702fefef783383171Chris Lattner          CaseVT = TLI.getPointerTy().SimpleTy;
2120cfe2eab7446dedc471592fe702fefef783383171Chris Lattner
2121cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        // If the VT matches, then we will execute this case.
2122cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        if (CurNodeVT == CaseVT)
2123cfe2eab7446dedc471592fe702fefef783383171Chris Lattner          break;
2124cfe2eab7446dedc471592fe702fefef783383171Chris Lattner
2125cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        // Otherwise, skip over this case.
2126cfe2eab7446dedc471592fe702fefef783383171Chris Lattner        MatcherIndex += CaseSize;
2127cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      }
2128cfe2eab7446dedc471592fe702fefef783383171Chris Lattner
2129cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      // If no cases matched, bail out.
2130cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      if (CaseSize == 0) break;
2131cfe2eab7446dedc471592fe702fefef783383171Chris Lattner
2132cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      // Otherwise, execute the case we found.
2133cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      DEBUG(errs() << "  TypeSwitch[" << EVT(CurNodeVT).getEVTString()
2134cfe2eab7446dedc471592fe702fefef783383171Chris Lattner                   << "] from " << SwitchStart << " to " << MatcherIndex<<'\n');
2135cfe2eab7446dedc471592fe702fefef783383171Chris Lattner      continue;
2136cfe2eab7446dedc471592fe702fefef783383171Chris Lattner    }
21372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckChild0Type: case OPC_CheckChild1Type:
21382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckChild2Type: case OPC_CheckChild3Type:
21392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckChild4Type: case OPC_CheckChild5Type:
21402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckChild6Type: case OPC_CheckChild7Type: {
21412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned ChildNo = Opcode-OPC_CheckChild0Type;
21422a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (ChildNo >= N.getNumOperands())
21432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;  // Match fails if out of range child #.
21442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
21452a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MVT::SimpleValueType VT =
21462a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
21472a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      EVT ChildVT = N.getOperand(ChildNo).getValueType();
21482a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (ChildVT != VT) {
21492a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Handle the case when VT is iPTR.
21502a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (VT != MVT::iPTR || ChildVT != TLI.getPointerTy())
21512a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          break;
21522a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
21532a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
21542a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
21552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckCondCode:
21562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (cast<CondCodeSDNode>(N)->get() !=
21572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          (ISD::CondCode)MatcherTable[MatcherIndex++]) break;
21582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
21592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckValueType: {
21602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MVT::SimpleValueType VT =
21612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
21622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (cast<VTSDNode>(N)->getVT() != VT) {
21632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Handle the case when VT is iPTR.
21642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (VT != MVT::iPTR || cast<VTSDNode>(N)->getVT() != TLI.getPointerTy())
21652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          break;
21662a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
21672a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
21682a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
21692a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckInteger: {
21702a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      int64_t Val = MatcherTable[MatcherIndex++];
21712a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Val & 128)
21722a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Val = GetVBR(Val, MatcherTable, MatcherIndex);
21732a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
21742a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
21752a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (C == 0 || C->getSExtValue() != Val)
21762a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;
21772a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
21782a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
21792a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckAndImm: {
21802a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      int64_t Val = MatcherTable[MatcherIndex++];
21812a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Val & 128)
21822a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Val = GetVBR(Val, MatcherTable, MatcherIndex);
21832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
21842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (N->getOpcode() != ISD::AND) break;
21852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
21862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (C == 0 || !CheckAndMask(N.getOperand(0), C, Val))
21872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;
21882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
21892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
21902a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckOrImm: {
21912a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      int64_t Val = MatcherTable[MatcherIndex++];
21922a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Val & 128)
21932a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Val = GetVBR(Val, MatcherTable, MatcherIndex);
21942a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
21952a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (N->getOpcode() != ISD::OR) break;
21962a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
21972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
21982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (C == 0 || !CheckOrMask(N.getOperand(0), C, Val))
21992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;
22002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
22012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
22022a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CheckFoldableChainNode: {
22042a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(NodeStack.size() != 1 && "No parent node");
22052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Verify that all intermediate nodes between the root and this one have
22062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // a single use.
22072a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      bool HasMultipleUses = false;
22082a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i)
22092a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (!NodeStack[i].hasOneUse()) {
22102a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          HasMultipleUses = true;
22112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          break;
22122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        }
22132a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (HasMultipleUses) break;
22142a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22152a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Check to see that the target thinks this is profitable to fold and that
22162a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // we can fold it without inducing cycles in the graph.
22172a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(),
22182a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                              NodeToMatch) ||
22192a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(),
2220d1b738298359846b9cccaa0931e6ec1fc59a6d87Chris Lattner                         NodeToMatch, true/*We validate our own chains*/))
22212a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;
22222a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22232a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
22242a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
22252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitInteger: {
22262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MVT::SimpleValueType VT =
22272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
22282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      int64_t Val = MatcherTable[MatcherIndex++];
22292a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Val & 128)
22302a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Val = GetVBR(Val, MatcherTable, MatcherIndex);
22312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.push_back(CurDAG->getTargetConstant(Val, VT));
22322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
22332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
22342a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitRegister: {
22352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MVT::SimpleValueType VT =
22362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
22372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned RegNo = MatcherTable[MatcherIndex++];
22382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.push_back(CurDAG->getRegister(RegNo, VT));
22392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
22402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
22412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22422a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitConvertToTarget:  {
22432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Convert from IMM/FPIMM to target version.
22442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned RecNo = MatcherTable[MatcherIndex++];
22452a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
22462a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SDValue Imm = RecordedNodes[RecNo];
22472a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22482a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Imm->getOpcode() == ISD::Constant) {
22492a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        int64_t Val = cast<ConstantSDNode>(Imm)->getZExtValue();
22502a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Imm = CurDAG->getTargetConstant(Val, Imm.getValueType());
22512a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      } else if (Imm->getOpcode() == ISD::ConstantFP) {
22522a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue();
22532a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Imm = CurDAG->getTargetConstantFP(*Val, Imm.getValueType());
22542a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
22552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.push_back(Imm);
22572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
22582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
22592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitMergeInputChains: {
22612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(InputChain.getNode() == 0 &&
22622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner             "EmitMergeInputChains should be the first chain producing node");
22632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // This node gets a list of nodes we matched in the input that have
22642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // chains.  We want to token factor all of the input chains to these nodes
22652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // together.  However, if any of the input chains is actually one of the
22662a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // nodes matched in this pattern, then we have an intra-match reference.
22672a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Ignore these because the newly token factored chain should not refer to
22682a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // the old nodes.
22692a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumChains = MatcherTable[MatcherIndex++];
22702a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(NumChains != 0 && "Can't TF zero chains");
22712a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22722a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(ChainNodesMatched.empty() &&
22732a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner             "Should only have one EmitMergeInputChains per match");
22742a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22752a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Read all of the chained nodes.
227600592ec857b4c3f31b46412e2738924238c59983Chris Lattner      for (unsigned i = 0; i != NumChains; ++i) {
227700592ec857b4c3f31b46412e2738924238c59983Chris Lattner        unsigned RecNo = MatcherTable[MatcherIndex++];
22782a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
22792a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        ChainNodesMatched.push_back(RecordedNodes[RecNo].getNode());
22802a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22812a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // FIXME: What if other value results of the node have uses not matched
22822a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // by this pattern?
22832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (ChainNodesMatched.back() != NodeToMatch &&
22842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner            !RecordedNodes[RecNo].hasOneUse()) {
22852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          ChainNodesMatched.clear();
22862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          break;
22872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        }
22882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
22896b30792d2d7d142b8192241e6232aee004f745fbChris Lattner
22906b30792d2d7d142b8192241e6232aee004f745fbChris Lattner      // If the inner loop broke out, the match fails.
22916b30792d2d7d142b8192241e6232aee004f745fbChris Lattner      if (ChainNodesMatched.empty())
22926b30792d2d7d142b8192241e6232aee004f745fbChris Lattner        break;
22932a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
22946b30792d2d7d142b8192241e6232aee004f745fbChris Lattner      // Merge the input chains if they are not intra-pattern references.
22956b30792d2d7d142b8192241e6232aee004f745fbChris Lattner      InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
22966b30792d2d7d142b8192241e6232aee004f745fbChris Lattner
22976b30792d2d7d142b8192241e6232aee004f745fbChris Lattner      if (InputChain.getNode() == 0)
22986b30792d2d7d142b8192241e6232aee004f745fbChris Lattner        break;  // Failed to merge.
22992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
23012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
23022a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitCopyToReg: {
23042a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned RecNo = MatcherTable[MatcherIndex++];
23052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
23062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned DestPhysReg = MatcherTable[MatcherIndex++];
23072a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23082a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (InputChain.getNode() == 0)
23092a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        InputChain = CurDAG->getEntryNode();
23102a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      InputChain = CurDAG->getCopyToReg(InputChain, NodeToMatch->getDebugLoc(),
23122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                                        DestPhysReg, RecordedNodes[RecNo],
23132a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                                        InputFlag);
23142a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23152a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      InputFlag = InputChain.getValue(1);
23162a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
23172a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
23182a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23192a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitNodeXForm: {
23202a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned XFormNo = MatcherTable[MatcherIndex++];
23212a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned RecNo = MatcherTable[MatcherIndex++];
23222a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
23232a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.push_back(RunSDNodeXForm(RecordedNodes[RecNo], XFormNo));
23242a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
23252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
23262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_EmitNode:
23282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_MorphNodeTo: {
232914df8dc680d40173f34d693f9dc451f8a30c907bChris Lattner      uint16_t TargetOpc = MatcherTable[MatcherIndex++];
233014df8dc680d40173f34d693f9dc451f8a30c907bChris Lattner      TargetOpc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
23312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned EmitNodeInfo = MatcherTable[MatcherIndex++];
23322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Get the result VT list.
23332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumVTs = MatcherTable[MatcherIndex++];
23342a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SmallVector<EVT, 4> VTs;
23352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      for (unsigned i = 0; i != NumVTs; ++i) {
23362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        MVT::SimpleValueType VT =
23372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
23382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (VT == MVT::iPTR) VT = TLI.getPointerTy().SimpleTy;
23392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        VTs.push_back(VT);
23402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
23412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23422a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (EmitNodeInfo & OPFL_Chain)
23432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        VTs.push_back(MVT::Other);
23442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (EmitNodeInfo & OPFL_FlagOutput)
23452a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        VTs.push_back(MVT::Flag);
23462a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23477d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      // This is hot code, so optimize the two most common cases of 1 and 2
23487d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      // results.
23497d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      SDVTList VTList;
23507d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      if (VTs.size() == 1)
23517d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner        VTList = CurDAG->getVTList(VTs[0]);
23527d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      else if (VTs.size() == 2)
23537d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner        VTList = CurDAG->getVTList(VTs[0], VTs[1]);
23547d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner      else
23557d892d6e6d451e9a0d0f9db839a943a256126c05Chris Lattner        VTList = CurDAG->getVTList(VTs.data(), VTs.size());
23562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Get the operand list.
23582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumOps = MatcherTable[MatcherIndex++];
23592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SmallVector<SDValue, 8> Ops;
23602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      for (unsigned i = 0; i != NumOps; ++i) {
23612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        unsigned RecNo = MatcherTable[MatcherIndex++];
23622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (RecNo & 128)
23632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
23642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        assert(RecNo < RecordedNodes.size() && "Invalid EmitNode");
23662a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Ops.push_back(RecordedNodes[RecNo]);
23672a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
23682a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23692a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If there are variadic operands to add, handle them now.
23702a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (EmitNodeInfo & OPFL_VariadicInfo) {
23712a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Determine the start index to copy from.
23722a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo);
23732a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0;
23742a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        assert(NodeToMatch->getNumOperands() >= FirstOpToCopy &&
23752a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner               "Invalid variadic node");
23762a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Copy all of the variadic operands, not including a potential flag
23772a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // input.
23782a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands();
23792a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner             i != e; ++i) {
23802a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          SDValue V = NodeToMatch->getOperand(i);
23812a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          if (V.getValueType() == MVT::Flag) break;
23822a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          Ops.push_back(V);
23832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        }
23842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
23852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If this has chain/flag inputs, add them.
23872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (EmitNodeInfo & OPFL_Chain)
23882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Ops.push_back(InputChain);
23892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if ((EmitNodeInfo & OPFL_FlagInput) && InputFlag.getNode() != 0)
23902a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Ops.push_back(InputFlag);
23912a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
23922a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Create the node.
23932a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      SDNode *Res = 0;
23942a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Opcode != OPC_MorphNodeTo) {
23952a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // If this is a normal EmitNode command, just create the new node and
23962a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // add the results to the RecordedNodes list.
23972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        Res = CurDAG->getMachineNode(TargetOpc, NodeToMatch->getDebugLoc(),
23982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                                     VTList, Ops.data(), Ops.size());
23992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Add all the non-flag/non-chain results to the RecordedNodes list.
24012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
24022a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          if (VTs[i] == MVT::Other || VTs[i] == MVT::Flag) break;
24032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          RecordedNodes.push_back(SDValue(Res, i));
24042a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        }
24052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      } else {
24073ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner        Res = MorphNode(NodeToMatch, TargetOpc, VTList, Ops.data(), Ops.size(),
24083ff1e4db7896a8ccec74800d52a926118d025260Chris Lattner                        EmitNodeInfo);
24092a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
24102a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If the node had chain/flag results, update our notion of the current
24122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // chain and flag.
241382dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner      if (EmitNodeInfo & OPFL_FlagOutput) {
24142a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        InputFlag = SDValue(Res, VTs.size()-1);
24152a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (EmitNodeInfo & OPFL_Chain)
24162a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          InputChain = SDValue(Res, VTs.size()-2);
24172a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      } else if (EmitNodeInfo & OPFL_Chain)
24182a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        InputChain = SDValue(Res, VTs.size()-1);
24192a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24202a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If the OPFL_MemRefs flag is set on this node, slap all of the
24212a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // accumulated memrefs onto it.
24222a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      //
24232a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // FIXME: This is vastly incorrect for patterns with multiple outputs
24242a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // instructions that access memory and for ComplexPatterns that match
24252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // loads.
24262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (EmitNodeInfo & OPFL_MemRefs) {
24272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        MachineSDNode::mmo_iterator MemRefs =
24282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          MF->allocateMemRefsArray(MatchedMemRefs.size());
24292a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        std::copy(MatchedMemRefs.begin(), MatchedMemRefs.end(), MemRefs);
24302a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        cast<MachineSDNode>(Res)
24312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          ->setMemRefs(MemRefs, MemRefs + MatchedMemRefs.size());
24322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
24332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24342a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      DEBUG(errs() << "  "
24352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                   << (Opcode == OPC_MorphNodeTo ? "Morphed" : "Created")
24362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                   << " node: "; Res->dump(CurDAG); errs() << "\n");
24372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If this was a MorphNodeTo then we're completely done!
24392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (Opcode == OPC_MorphNodeTo) {
24402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // Update chain and flag uses.
24412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        UpdateChainsAndFlags(NodeToMatch, InputChain, ChainNodesMatched,
244282dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                             InputFlag, FlagResultNodesMatched, true);
244382dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner        return Res;
24442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
24452a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24462a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
24472a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
24482a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24492a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_MarkFlagResults: {
24502a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumNodes = MatcherTable[MatcherIndex++];
24512a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24522a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Read and remember all the flag-result nodes.
24532a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      for (unsigned i = 0; i != NumNodes; ++i) {
24542a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        unsigned RecNo = MatcherTable[MatcherIndex++];
24552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (RecNo & 128)
24562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
24572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
24592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        FlagResultNodesMatched.push_back(RecordedNodes[RecNo].getNode());
24602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
24612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      continue;
24622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
24632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    case OPC_CompleteMatch: {
24652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // The match has been completed, and any new nodes (if any) have been
24662a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // created.  Patch up references to the matched dag to use the newly
24672a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // created nodes.
24682a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumResults = MatcherTable[MatcherIndex++];
24692a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24702a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      for (unsigned i = 0; i != NumResults; ++i) {
24712a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        unsigned ResSlot = MatcherTable[MatcherIndex++];
24722a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (ResSlot & 128)
24732a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex);
24742a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24752a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        assert(ResSlot < RecordedNodes.size() && "Invalid CheckSame");
24762a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        SDValue Res = RecordedNodes[ResSlot];
24772a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24782a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // FIXME2: Eliminate this horrible hack by fixing the 'Gen' program
24792a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // after (parallel) on input patterns are removed.  This would also
24802a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // allow us to stop encoding #results in OPC_CompleteMatch's table
24812a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        // entry.
24822a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        if (NodeToMatch->getNumValues() <= i ||
24832a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner            NodeToMatch->getValueType(i) == MVT::Other ||
24842a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner            NodeToMatch->getValueType(i) == MVT::Flag)
24852a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner          break;
24862a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        assert((NodeToMatch->getValueType(i) == Res.getValueType() ||
24872a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                NodeToMatch->getValueType(i) == MVT::iPTR ||
24882a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                Res.getValueType() == MVT::iPTR ||
24892a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                NodeToMatch->getValueType(i).getSizeInBits() ==
24902a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                    Res.getValueType().getSizeInBits()) &&
24912a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner               "invalid replacement");
24922a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        CurDAG->ReplaceAllUsesOfValueWith(SDValue(NodeToMatch, i), Res);
24932a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
24942a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
24952a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If the root node defines a flag, add it to the flag nodes to update
24962a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // list.
24972a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (NodeToMatch->getValueType(NodeToMatch->getNumValues()-1) == MVT::Flag)
24982a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        FlagResultNodesMatched.push_back(NodeToMatch);
24992a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25002a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Update chain and flag uses.
25012a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      UpdateChainsAndFlags(NodeToMatch, InputChain, ChainNodesMatched,
250282dd3d38d9b3bd49f19b8230f39d33e8ee6d4055Chris Lattner                           InputFlag, FlagResultNodesMatched, false);
25032a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25042a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      assert(NodeToMatch->use_empty() &&
25052a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner             "Didn't replace all uses of the node?");
25062a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25072a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // FIXME: We just return here, which interacts correctly with SelectRoot
25082a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // above.  We should fix this to not return an SDNode* anymore.
25092a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      return 0;
25102a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
25112a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
25122a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25132a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    // If the code reached this point, then the match failed.  See if there is
25142a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    // another child to try in the current 'Scope', otherwise pop it until we
25152a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    // find a case to check.
25162a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    while (1) {
25172a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (MatchScopes.empty()) {
25182a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        CannotYetSelect(NodeToMatch);
25192a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        return 0;
25202a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
25212a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25222a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Restore the interpreter state back to the point where the scope was
25232a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // formed.
25242a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MatchScope &LastScope = MatchScopes.back();
25252a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      RecordedNodes.resize(LastScope.NumRecordedNodes);
25262a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NodeStack.clear();
25272a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      NodeStack.append(LastScope.NodeStack.begin(), LastScope.NodeStack.end());
25282a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      N = NodeStack.back();
25292a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25302a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      DEBUG(errs() << "  Match failed at index " << MatcherIndex
25312a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner                   << " continuing at " << LastScope.FailIndex << "\n");
25322a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25332a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size())
25342a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        MatchedMemRefs.resize(LastScope.NumMatchedMemRefs);
25352a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MatcherIndex = LastScope.FailIndex;
25362a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25372a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      InputChain = LastScope.InputChain;
25382a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      InputFlag = LastScope.InputFlag;
25392a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (!LastScope.HasChainNodesMatched)
25402a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        ChainNodesMatched.clear();
25412a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (!LastScope.HasFlagResultNodesMatched)
25422a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        FlagResultNodesMatched.clear();
25432a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25442a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // Check to see what the offset is at the new MatcherIndex.  If it is zero
25452a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // we have reached the end of this scope, otherwise we have another child
25462a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // in the current scope to try.
25472a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      unsigned NumToSkip = MatcherTable[MatcherIndex++];
25482a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (NumToSkip & 128)
25492a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
25502a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25512a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // If we have another child in this scope to match, update FailIndex and
25522a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // try it.
25532a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      if (NumToSkip != 0) {
25542a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        LastScope.FailIndex = MatcherIndex+NumToSkip;
25552a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner        break;
25562a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      }
25572a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25582a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // End of this scope, pop it and try the next child in the containing
25592a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      // scope.
25602a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner      MatchScopes.pop_back();
25612a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner    }
25622a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner  }
25632a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner}
25642a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25652a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
25662a49d57d2d9dd19125a9ba302e883f6ba6c38b83Chris Lattner
2567eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan Gohmanvoid SelectionDAGISel::CannotYetSelect(SDNode *N) {
2568409ac586e83525a760692cf0affb01f1c9efa594Chris Lattner  if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2569409ac586e83525a760692cf0affb01f1c9efa594Chris Lattner      N->getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2570409ac586e83525a760692cf0affb01f1c9efa594Chris Lattner      N->getOpcode() == ISD::INTRINSIC_VOID)
2571409ac586e83525a760692cf0affb01f1c9efa594Chris Lattner    return CannotYetSelectIntrinsic(N);
2572409ac586e83525a760692cf0affb01f1c9efa594Chris Lattner
2573e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  std::string msg;
2574e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  raw_string_ostream Msg(msg);
2575e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  Msg << "Cannot yet select: ";
2576ce6715faa37b2e44edcf400084aa808ae30d7085David Greene  N->printrFull(Msg, CurDAG);
2577e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  llvm_report_error(Msg.str());
2578e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman}
2579e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman
2580eeb3a00b84b7767d236ec8cf0619b9217fc247b9Dan Gohmanvoid SelectionDAGISel::CannotYetSelectIntrinsic(SDNode *N) {
25811a053236f165ac60ee88edca370a732d278aa2c1David Greene  dbgs() << "Cannot yet select: ";
2582e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  unsigned iid =
2583f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach    cast<ConstantSDNode>(N->getOperand(N->getOperand(0).getValueType() ==
2584f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach                                       MVT::Other))->getZExtValue();
2585e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  if (iid < Intrinsic::num_intrinsics)
2586f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach    llvm_report_error("Cannot yet select: intrinsic %" +
2587f4549b02a7d9907d06e7fb8afefd129452d1f7c5Jim Grosbach                      Intrinsic::getName((Intrinsic::ID)iid));
2588e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman  else if (const TargetIntrinsicInfo *tii = TM.getIntrinsicInfo())
2589e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman    llvm_report_error(Twine("Cannot yet select: target intrinsic %") +
2590e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman                      tii->getName(iid));
2591e1f188fe27050ec812e5b64cc996e6bb93300392Dan Gohman}
2592c1c6ef8f74fc550f29cfec1f2fb699dd8c2fb94eAnton Korobeynikov
25931997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patelchar SelectionDAGISel::ID = 0;
2594