blob: 2cdd1cc9783b90464e5d65a730c7a220b8bf07f3 [file] [log] [blame]
Chris Lattner1c08c712005-01-07 07:47:53 +00001//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner1c08c712005-01-07 07:47:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner1c08c712005-01-07 07:47:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAGISel class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Dan Gohman84fbac52009-02-06 17:22:58 +000015#include "ScheduleDAGSDNodes.h"
Dan Gohman2048b852009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000017#include "FunctionLoweringInfo.h"
Dan Gohman84fbac52009-02-06 17:22:58 +000018#include "llvm/CodeGen/SelectionDAGISel.h"
Jim Laskeyc7c3f112006-10-16 20:52:31 +000019#include "llvm/Analysis/AliasAnalysis.h"
Devang Patel713f0432009-09-16 21:09:07 +000020#include "llvm/Analysis/DebugInfo.h"
Anton Korobeynikov5502bf62007-04-04 21:14:49 +000021#include "llvm/Constants.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000022#include "llvm/Function.h"
Chris Lattnerce7518c2006-01-26 22:24:51 +000023#include "llvm/InlineAsm.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000024#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
Jim Laskey43970fe2006-03-23 18:06:46 +000026#include "llvm/IntrinsicInst.h"
Chris Lattner75c478a2009-10-27 17:02:08 +000027#include "llvm/LLVMContext.h"
Dan Gohman78eca172008-08-19 22:33:34 +000028#include "llvm/CodeGen/FastISel.h"
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000029#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000030#include "llvm/CodeGen/GCMetadata.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000031#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanfc54c552009-01-15 22:18:12 +000035#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000036#include "llvm/CodeGen/SchedulerRegistry.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000037#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000038#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmane1f188f2009-10-29 22:30:23 +000039#include "llvm/Target/TargetIntrinsicInfo.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000040#include "llvm/Target/TargetInstrInfo.h"
41#include "llvm/Target/TargetLowering.h"
42#include "llvm/Target/TargetMachine.h"
Vladimir Prus12472912006-05-23 13:43:15 +000043#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000044#include "llvm/Support/Compiler.h"
Evan Chengdb8d56b2008-06-30 20:45:06 +000045#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000046#include "llvm/Support/ErrorHandling.h"
Evan Chengdb8d56b2008-06-30 20:45:06 +000047#include "llvm/Support/Timer.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000048#include "llvm/Support/raw_ostream.h"
Chris Lattnerc19ae9d2010-03-04 19:46:56 +000049#include "llvm/ADT/Statistic.h"
Jeff Cohen7e881032006-02-24 02:52:40 +000050#include <algorithm>
Chris Lattner1c08c712005-01-07 07:47:53 +000051using namespace llvm;
52
Chris Lattnerc19ae9d2010-03-04 19:46:56 +000053STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on");
Chris Lattnerbed993d2010-03-28 19:46:56 +000054STATISTIC(NumDAGIselRetries,"Number of times dag isel has to try another path");
Chris Lattnerc19ae9d2010-03-04 19:46:56 +000055
Chris Lattneread0d882008-06-17 06:09:18 +000056static cl::opt<bool>
Dan Gohman293d5f82008-09-09 22:06:46 +000057EnableFastISelVerbose("fast-isel-verbose", cl::Hidden,
Dan Gohmand659d502008-10-20 21:30:12 +000058 cl::desc("Enable verbose messages in the \"fast\" "
Dan Gohman293d5f82008-09-09 22:06:46 +000059 "instruction selector"));
60static cl::opt<bool>
Dan Gohman4344a5d2008-09-09 23:05:00 +000061EnableFastISelAbort("fast-isel-abort", cl::Hidden,
62 cl::desc("Enable abort calls when \"fast\" instruction fails"));
Chris Lattneread0d882008-06-17 06:09:18 +000063
Chris Lattnerda8abb02005-09-01 18:44:10 +000064#ifndef NDEBUG
Chris Lattner7944d9d2005-01-12 03:41:21 +000065static cl::opt<bool>
Dan Gohman462dc7f2008-07-21 20:00:07 +000066ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
67 cl::desc("Pop up a window to show dags before the first "
68 "dag combine pass"));
69static cl::opt<bool>
70ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
71 cl::desc("Pop up a window to show dags before legalize types"));
72static cl::opt<bool>
73ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
74 cl::desc("Pop up a window to show dags before legalize"));
75static cl::opt<bool>
76ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
77 cl::desc("Pop up a window to show dags before the second "
78 "dag combine pass"));
79static cl::opt<bool>
Duncan Sands25cf2272008-11-24 14:53:14 +000080ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
81 cl::desc("Pop up a window to show dags before the post legalize types"
82 " dag combine pass"));
83static cl::opt<bool>
Evan Chenga9c20912006-01-21 02:32:06 +000084ViewISelDAGs("view-isel-dags", cl::Hidden,
85 cl::desc("Pop up a window to show isel dags as they are selected"));
86static cl::opt<bool>
87ViewSchedDAGs("view-sched-dags", cl::Hidden,
88 cl::desc("Pop up a window to show sched dags as they are processed"));
Dan Gohman3e1a7ae2007-08-28 20:32:58 +000089static cl::opt<bool>
90ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
Chris Lattner5bab7852008-01-25 17:24:52 +000091 cl::desc("Pop up a window to show SUnit dags after they are processed"));
Chris Lattner7944d9d2005-01-12 03:41:21 +000092#else
Dan Gohman462dc7f2008-07-21 20:00:07 +000093static const bool ViewDAGCombine1 = false,
94 ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false,
95 ViewDAGCombine2 = false,
Duncan Sands25cf2272008-11-24 14:53:14 +000096 ViewDAGCombineLT = false,
Dan Gohman462dc7f2008-07-21 20:00:07 +000097 ViewISelDAGs = false, ViewSchedDAGs = false,
98 ViewSUnitDAGs = false;
Chris Lattner7944d9d2005-01-12 03:41:21 +000099#endif
100
Jim Laskeyeb577ba2006-08-02 12:30:23 +0000101//===---------------------------------------------------------------------===//
102///
103/// RegisterScheduler class - Track the registration of instruction schedulers.
104///
105//===---------------------------------------------------------------------===//
106MachinePassRegistry RegisterScheduler::Registry;
107
108//===---------------------------------------------------------------------===//
109///
110/// ISHeuristic command line option for instruction schedulers.
111///
112//===---------------------------------------------------------------------===//
Dan Gohman844731a2008-05-13 00:00:25 +0000113static cl::opt<RegisterScheduler::FunctionPassCtor, false,
114 RegisterPassParser<RegisterScheduler> >
115ISHeuristic("pre-RA-sched",
116 cl::init(&createDefaultScheduler),
117 cl::desc("Instruction schedulers available (before register"
118 " allocation):"));
Jim Laskey13ec7022006-08-01 14:21:23 +0000119
Dan Gohman844731a2008-05-13 00:00:25 +0000120static RegisterScheduler
Dan Gohmanb8cab922008-10-14 20:25:08 +0000121defaultListDAGScheduler("default", "Best scheduler for the target",
Dan Gohman844731a2008-05-13 00:00:25 +0000122 createDefaultScheduler);
Evan Cheng4ef10862006-01-23 07:01:07 +0000123
Chris Lattner1c08c712005-01-07 07:47:53 +0000124namespace llvm {
125 //===--------------------------------------------------------------------===//
Jim Laskey9373beb2006-08-01 19:14:14 +0000126 /// createDefaultScheduler - This creates an instruction scheduler appropriate
127 /// for the target.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000128 ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
Bill Wendling98a366d2009-04-29 23:29:43 +0000129 CodeGenOpt::Level OptLevel) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000130 const TargetLowering &TLI = IS->getTargetLowering();
131
Bill Wendling98a366d2009-04-29 23:29:43 +0000132 if (OptLevel == CodeGenOpt::None)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000133 return createFastDAGScheduler(IS, OptLevel);
Dan Gohman9e76fea2008-11-20 03:11:19 +0000134 if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000135 return createTDListDAGScheduler(IS, OptLevel);
Dan Gohman9e76fea2008-11-20 03:11:19 +0000136 assert(TLI.getSchedulingPreference() ==
Bill Wendling187361b2010-01-23 10:26:57 +0000137 TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000138 return createBURRListDAGScheduler(IS, OptLevel);
Jim Laskey9373beb2006-08-01 19:14:14 +0000139 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000140}
141
Evan Chengff9b3732008-01-30 18:18:23 +0000142// EmitInstrWithCustomInserter - This method should be implemented by targets
Dan Gohman533297b2009-10-29 18:10:34 +0000143// that mark instructions with the 'usesCustomInserter' flag. These
Chris Lattner025c39b2005-08-26 20:54:47 +0000144// instructions are special in various ways, which require special support to
145// insert. The specified MachineInstr is created but not inserted into any
Dan Gohman533297b2009-10-29 18:10:34 +0000146// basic blocks, and this method is called to expand it into a sequence of
147// instructions, potentially also creating new basic blocks and control flow.
148// When new basic blocks are inserted and the edges from MBB to its successors
149// are modified, the method should insert pairs of <OldSucc, NewSucc> into the
150// DenseMap.
Evan Chengff9b3732008-01-30 18:18:23 +0000151MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000152 MachineBasicBlock *MBB,
153 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Torok Edwinf3689232009-07-12 20:07:01 +0000154#ifndef NDEBUG
David Greene1a053232010-01-05 01:26:11 +0000155 dbgs() << "If a target marks an instruction with "
Dan Gohman533297b2009-10-29 18:10:34 +0000156 "'usesCustomInserter', it must implement "
Torok Edwinf3689232009-07-12 20:07:01 +0000157 "TargetLowering::EmitInstrWithCustomInserter!";
158#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000159 llvm_unreachable(0);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000160 return 0;
Chris Lattner025c39b2005-08-26 20:54:47 +0000161}
162
Chris Lattner7041ee32005-01-11 05:56:49 +0000163//===----------------------------------------------------------------------===//
164// SelectionDAGISel code
165//===----------------------------------------------------------------------===//
Chris Lattner1c08c712005-01-07 07:47:53 +0000166
Dan Gohmanf0757b02010-04-21 01:34:56 +0000167SelectionDAGISel::SelectionDAGISel(const TargetMachine &tm, CodeGenOpt::Level OL) :
Dan Gohmanad2afc22009-07-31 18:16:33 +0000168 MachineFunctionPass(&ID), TM(tm), TLI(*tm.getTargetLowering()),
Dan Gohman7c3234c2008-08-27 23:52:12 +0000169 FuncInfo(new FunctionLoweringInfo(TLI)),
Dan Gohman50d2b1a2010-04-19 19:22:07 +0000170 CurDAG(new SelectionDAG(tm, *FuncInfo)),
Dan Gohman55e59c12010-04-19 19:05:59 +0000171 SDB(new SelectionDAGBuilder(*CurDAG, *FuncInfo, OL)),
Dan Gohman7c3234c2008-08-27 23:52:12 +0000172 GFI(),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000173 OptLevel(OL),
Dan Gohman7c3234c2008-08-27 23:52:12 +0000174 DAGSize(0)
175{}
176
177SelectionDAGISel::~SelectionDAGISel() {
Dan Gohman2048b852009-11-23 18:04:58 +0000178 delete SDB;
Dan Gohman7c3234c2008-08-27 23:52:12 +0000179 delete CurDAG;
180 delete FuncInfo;
181}
182
Chris Lattner495a0b52005-08-17 06:37:43 +0000183void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000184 AU.addRequired<AliasAnalysis>();
Dan Gohmana3477fe2009-07-31 23:36:22 +0000185 AU.addPreserved<AliasAnalysis>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000186 AU.addRequired<GCModuleInfo>();
Dan Gohmana3477fe2009-07-31 23:36:22 +0000187 AU.addPreserved<GCModuleInfo>();
Dan Gohmanad2afc22009-07-31 18:16:33 +0000188 MachineFunctionPass::getAnalysisUsage(AU);
Chris Lattner495a0b52005-08-17 06:37:43 +0000189}
Chris Lattner1c08c712005-01-07 07:47:53 +0000190
Dan Gohmanad2afc22009-07-31 18:16:33 +0000191bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
Dan Gohman4344a5d2008-09-09 23:05:00 +0000192 // Do some sanity-checking on the command-line options.
193 assert((!EnableFastISelVerbose || EnableFastISel) &&
194 "-fast-isel-verbose requires -fast-isel");
195 assert((!EnableFastISelAbort || EnableFastISel) &&
196 "-fast-isel-abort requires -fast-isel");
197
Dan Gohmanae541aa2010-04-15 04:33:49 +0000198 const Function &Fn = *mf.getFunction();
Dan Gohman8a110532008-09-05 22:59:21 +0000199 const TargetInstrInfo &TII = *TM.getInstrInfo();
200 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
201
Dan Gohmanc0fb65d2010-04-14 17:02:07 +0000202 MF = &mf;
Dan Gohman79ce2762009-01-15 19:20:50 +0000203 RegInfo = &MF->getRegInfo();
Dan Gohmanc0fb65d2010-04-14 17:02:07 +0000204 AA = &getAnalysis<AliasAnalysis>();
205 GFI = Fn.hasGC() ? &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn) : 0;
206
David Greene1a053232010-01-05 01:26:11 +0000207 DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n");
Chris Lattner1c08c712005-01-07 07:47:53 +0000208
Chris Lattnerde6e7832010-04-05 06:10:13 +0000209 CurDAG->init(*MF);
Dan Gohman6277eb22009-11-23 17:16:22 +0000210 FuncInfo->set(Fn, *MF, EnableFastISel);
Dan Gohman2048b852009-11-23 18:04:58 +0000211 SDB->init(GFI, *AA);
Chris Lattner1c08c712005-01-07 07:47:53 +0000212
Dan Gohman6a732b52010-04-14 20:05:00 +0000213 SelectAllBasicBlocks(Fn);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000214
Dan Gohman5dc44332010-04-14 17:13:16 +0000215 // Release function-specific state. SDB and CurDAG are already cleared
216 // at this point.
217 FuncInfo->clear();
218
Dan Gohman8a110532008-09-05 22:59:21 +0000219 // If the first basic block in the function has live ins that need to be
220 // copied into vregs, emit the copies into the top of the block before
221 // emitting the code for the block.
Dan Gohman98708262010-04-14 16:51:49 +0000222 RegInfo->EmitLiveInCopies(MF->begin(), TRI, TII);
Dan Gohman8a110532008-09-05 22:59:21 +0000223
Chris Lattner1c08c712005-01-07 07:47:53 +0000224 return true;
225}
226
Dan Gohmana9a33212010-04-20 00:29:35 +0000227MachineBasicBlock *
228SelectionDAGISel::SelectBasicBlock(MachineBasicBlock *BB,
229 const BasicBlock *LLVMBB,
230 BasicBlock::const_iterator Begin,
231 BasicBlock::const_iterator End,
232 bool &HadTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000233 // Lower all of the non-terminator instructions. If a call is emitted
Dan Gohmanf89d1dc2010-04-16 05:06:56 +0000234 // as a tail call, cease emitting nodes for this block. Terminators
235 // are handled below.
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000236 for (BasicBlock::const_iterator I = Begin; I != End && !SDB->HasTailCall; ++I)
Dan Gohmanf89d1dc2010-04-16 05:06:56 +0000237 SDB->visit(*I);
Dan Gohmanf350b272008-08-23 02:25:05 +0000238
Chris Lattnera651cf62005-01-17 19:43:36 +0000239 // Make sure the root of the DAG is up-to-date.
Dan Gohman2048b852009-11-23 18:04:58 +0000240 CurDAG->setRoot(SDB->getControlRoot());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000241
Dan Gohmanf350b272008-08-23 02:25:05 +0000242 // Final step, emit the lowered DAG as machine code.
Dan Gohmana9a33212010-04-20 00:29:35 +0000243 BB = CodeGenAndEmitDAG(BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000244 HadTailCall = SDB->HasTailCall;
245 SDB->clear();
Dan Gohmana9a33212010-04-20 00:29:35 +0000246 return BB;
Chris Lattner1c08c712005-01-07 07:47:53 +0000247}
248
Evan Cheng54e146b2010-01-09 00:21:08 +0000249namespace {
250/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
251/// nodes from the worklist.
252class SDOPsWorkListRemover : public SelectionDAG::DAGUpdateListener {
253 SmallVector<SDNode*, 128> &Worklist;
Chris Lattner25e0ab92010-03-14 19:43:04 +0000254 SmallPtrSet<SDNode*, 128> &InWorklist;
Evan Cheng54e146b2010-01-09 00:21:08 +0000255public:
Chris Lattner25e0ab92010-03-14 19:43:04 +0000256 SDOPsWorkListRemover(SmallVector<SDNode*, 128> &wl,
257 SmallPtrSet<SDNode*, 128> &inwl)
258 : Worklist(wl), InWorklist(inwl) {}
Evan Cheng54e146b2010-01-09 00:21:08 +0000259
Chris Lattner25e0ab92010-03-14 19:43:04 +0000260 void RemoveFromWorklist(SDNode *N) {
261 if (!InWorklist.erase(N)) return;
262
263 SmallVector<SDNode*, 128>::iterator I =
264 std::find(Worklist.begin(), Worklist.end(), N);
265 assert(I != Worklist.end() && "Not in worklist");
266
267 *I = Worklist.back();
268 Worklist.pop_back();
269 }
270
Evan Cheng54e146b2010-01-09 00:21:08 +0000271 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner25e0ab92010-03-14 19:43:04 +0000272 RemoveFromWorklist(N);
Evan Cheng54e146b2010-01-09 00:21:08 +0000273 }
274
275 virtual void NodeUpdated(SDNode *N) {
276 // Ignore updates.
277 }
278};
279}
280
Evan Cheng046632f2010-02-10 02:17:34 +0000281/// TrivialTruncElim - Eliminate some trivial nops that can result from
282/// ShrinkDemandedOps: (trunc (ext n)) -> n.
283static bool TrivialTruncElim(SDValue Op,
284 TargetLowering::TargetLoweringOpt &TLO) {
285 SDValue N0 = Op.getOperand(0);
286 EVT VT = Op.getValueType();
287 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
288 N0.getOpcode() == ISD::SIGN_EXTEND ||
289 N0.getOpcode() == ISD::ANY_EXTEND) &&
290 N0.getOperand(0).getValueType() == VT) {
291 return TLO.CombineTo(Op, N0.getOperand(0));
292 }
293 return false;
294}
295
Evan Cheng54eb4c22010-01-06 19:43:21 +0000296/// ShrinkDemandedOps - A late transformation pass that shrink expressions
297/// using TargetLowering::TargetLoweringOpt::ShrinkDemandedOp. It converts
298/// x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
Evan Chengd40d03e2010-01-06 19:38:29 +0000299void SelectionDAGISel::ShrinkDemandedOps() {
300 SmallVector<SDNode*, 128> Worklist;
Chris Lattner25e0ab92010-03-14 19:43:04 +0000301 SmallPtrSet<SDNode*, 128> InWorklist;
Evan Chengd40d03e2010-01-06 19:38:29 +0000302
303 // Add all the dag nodes to the worklist.
304 Worklist.reserve(CurDAG->allnodes_size());
305 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
Chris Lattner25e0ab92010-03-14 19:43:04 +0000306 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengd40d03e2010-01-06 19:38:29 +0000307 Worklist.push_back(I);
Chris Lattner25e0ab92010-03-14 19:43:04 +0000308 InWorklist.insert(I);
309 }
Evan Chengd40d03e2010-01-06 19:38:29 +0000310
Evan Chenge5b51ac2010-04-17 06:13:15 +0000311 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true, true);
Evan Chengd40d03e2010-01-06 19:38:29 +0000312 while (!Worklist.empty()) {
Benjamin Kramer7b1e2a52010-01-07 17:27:56 +0000313 SDNode *N = Worklist.pop_back_val();
Chris Lattner25e0ab92010-03-14 19:43:04 +0000314 InWorklist.erase(N);
Evan Chengd40d03e2010-01-06 19:38:29 +0000315
316 if (N->use_empty() && N != CurDAG->getRoot().getNode()) {
Chris Lattnerc4a3f232010-03-14 19:46:02 +0000317 // Deleting this node may make its operands dead, add them to the worklist
318 // if they aren't already there.
319 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
320 if (InWorklist.insert(N->getOperand(i).getNode()))
321 Worklist.push_back(N->getOperand(i).getNode());
322
Evan Cheng54e146b2010-01-09 00:21:08 +0000323 CurDAG->DeleteNode(N);
Evan Chengd40d03e2010-01-06 19:38:29 +0000324 continue;
325 }
326
327 // Run ShrinkDemandedOp on scalar binary operations.
Chris Lattner25e0ab92010-03-14 19:43:04 +0000328 if (N->getNumValues() != 1 ||
329 !N->getValueType(0).isSimple() || !N->getValueType(0).isInteger())
330 continue;
331
332 unsigned BitWidth = N->getValueType(0).getScalarType().getSizeInBits();
333 APInt Demanded = APInt::getAllOnesValue(BitWidth);
334 APInt KnownZero, KnownOne;
335 if (!TLI.SimplifyDemandedBits(SDValue(N, 0), Demanded,
336 KnownZero, KnownOne, TLO) &&
337 (N->getOpcode() != ISD::TRUNCATE ||
338 !TrivialTruncElim(SDValue(N, 0), TLO)))
339 continue;
340
341 // Revisit the node.
342 assert(!InWorklist.count(N) && "Already in worklist");
343 Worklist.push_back(N);
344 InWorklist.insert(N);
Evan Chengd40d03e2010-01-06 19:38:29 +0000345
Chris Lattner25e0ab92010-03-14 19:43:04 +0000346 // Replace the old value with the new one.
347 DEBUG(errs() << "\nShrinkDemandedOps replacing ";
348 TLO.Old.getNode()->dump(CurDAG);
349 errs() << "\nWith: ";
350 TLO.New.getNode()->dump(CurDAG);
351 errs() << '\n');
Evan Chengd40d03e2010-01-06 19:38:29 +0000352
Chris Lattner25e0ab92010-03-14 19:43:04 +0000353 if (InWorklist.insert(TLO.New.getNode()))
354 Worklist.push_back(TLO.New.getNode());
Evan Cheng54e146b2010-01-09 00:21:08 +0000355
Chris Lattner25e0ab92010-03-14 19:43:04 +0000356 SDOPsWorkListRemover DeadNodes(Worklist, InWorklist);
357 CurDAG->ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Evan Chengd40d03e2010-01-06 19:38:29 +0000358
Chris Lattner25e0ab92010-03-14 19:43:04 +0000359 if (!TLO.Old.getNode()->use_empty()) continue;
360
361 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands();
362 i != e; ++i) {
363 SDNode *OpNode = TLO.Old.getNode()->getOperand(i).getNode();
364 if (OpNode->hasOneUse()) {
365 // Add OpNode to the end of the list to revisit.
366 DeadNodes.RemoveFromWorklist(OpNode);
367 Worklist.push_back(OpNode);
368 InWorklist.insert(OpNode);
Evan Chengd40d03e2010-01-06 19:38:29 +0000369 }
370 }
Chris Lattner25e0ab92010-03-14 19:43:04 +0000371
372 DeadNodes.RemoveFromWorklist(TLO.Old.getNode());
373 CurDAG->DeleteNode(TLO.Old.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +0000374 }
375}
376
Dan Gohmanf350b272008-08-23 02:25:05 +0000377void SelectionDAGISel::ComputeLiveOutVRegInfo() {
Chris Lattneread0d882008-06-17 06:09:18 +0000378 SmallPtrSet<SDNode*, 128> VisitedNodes;
379 SmallVector<SDNode*, 128> Worklist;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000380
Gabor Greifba36cb52008-08-28 21:40:38 +0000381 Worklist.push_back(CurDAG->getRoot().getNode());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000382
Chris Lattneread0d882008-06-17 06:09:18 +0000383 APInt Mask;
384 APInt KnownZero;
385 APInt KnownOne;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000386
Benjamin Kramer7b1e2a52010-01-07 17:27:56 +0000387 do {
388 SDNode *N = Worklist.pop_back_val();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000389
Chris Lattneread0d882008-06-17 06:09:18 +0000390 // If we've already seen this node, ignore it.
391 if (!VisitedNodes.insert(N))
392 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000393
Chris Lattneread0d882008-06-17 06:09:18 +0000394 // Otherwise, add all chain operands to the worklist.
395 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 if (N->getOperand(i).getValueType() == MVT::Other)
Gabor Greifba36cb52008-08-28 21:40:38 +0000397 Worklist.push_back(N->getOperand(i).getNode());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000398
Chris Lattneread0d882008-06-17 06:09:18 +0000399 // If this is a CopyToReg with a vreg dest, process it.
400 if (N->getOpcode() != ISD::CopyToReg)
401 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000402
Chris Lattneread0d882008-06-17 06:09:18 +0000403 unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
404 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
405 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000406
Chris Lattneread0d882008-06-17 06:09:18 +0000407 // Ignore non-scalar or non-integer values.
Dan Gohman475871a2008-07-27 21:46:04 +0000408 SDValue Src = N->getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +0000409 EVT SrcVT = Src.getValueType();
Chris Lattneread0d882008-06-17 06:09:18 +0000410 if (!SrcVT.isInteger() || SrcVT.isVector())
411 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000412
Dan Gohmanf350b272008-08-23 02:25:05 +0000413 unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
Chris Lattneread0d882008-06-17 06:09:18 +0000414 Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits());
Dan Gohmanf350b272008-08-23 02:25:05 +0000415 CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000416
Chris Lattneread0d882008-06-17 06:09:18 +0000417 // Only install this information if it tells us something.
418 if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) {
419 DestReg -= TargetRegisterInfo::FirstVirtualRegister;
Dan Gohmanf7d6cd42009-08-01 03:51:09 +0000420 if (DestReg >= FuncInfo->LiveOutRegInfo.size())
421 FuncInfo->LiveOutRegInfo.resize(DestReg+1);
422 FunctionLoweringInfo::LiveOutInfo &LOI =
423 FuncInfo->LiveOutRegInfo[DestReg];
Chris Lattneread0d882008-06-17 06:09:18 +0000424 LOI.NumSignBits = NumSignBits;
Dan Gohmana80efce2009-03-27 23:55:04 +0000425 LOI.KnownOne = KnownOne;
426 LOI.KnownZero = KnownZero;
Chris Lattneread0d882008-06-17 06:09:18 +0000427 }
Benjamin Kramer7b1e2a52010-01-07 17:27:56 +0000428 } while (!Worklist.empty());
Chris Lattneread0d882008-06-17 06:09:18 +0000429}
430
Dan Gohmana9a33212010-04-20 00:29:35 +0000431MachineBasicBlock *SelectionDAGISel::CodeGenAndEmitDAG(MachineBasicBlock *BB) {
Dan Gohman462dc7f2008-07-21 20:00:07 +0000432 std::string GroupName;
433 if (TimePassesIsEnabled)
434 GroupName = "Instruction Selection and Scheduling";
435 std::string BlockName;
436 if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs ||
Duncan Sands25cf2272008-11-24 14:53:14 +0000437 ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs ||
438 ViewSUnitDAGs)
Dan Gohmanf7d6cd42009-08-01 03:51:09 +0000439 BlockName = MF->getFunction()->getNameStr() + ":" +
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000440 BB->getBasicBlock()->getNameStr();
Dan Gohman462dc7f2008-07-21 20:00:07 +0000441
David Greene1a053232010-01-05 01:26:11 +0000442 DEBUG(dbgs() << "Initial selection DAG:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000443 DEBUG(CurDAG->dump());
Dan Gohman462dc7f2008-07-21 20:00:07 +0000444
Dan Gohmanf350b272008-08-23 02:25:05 +0000445 if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName);
Dan Gohman417e11b2007-10-08 15:12:17 +0000446
Chris Lattneraf21d552005-10-10 16:47:10 +0000447 // Run the DAG combiner in pre-legalize mode.
Evan Chengebffb662008-07-01 17:59:20 +0000448 if (TimePassesIsEnabled) {
Dan Gohman5e843682008-07-14 18:19:29 +0000449 NamedRegionTimer T("DAG Combining 1", GroupName);
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000450 CurDAG->Combine(Unrestricted, *AA, OptLevel);
Evan Chengebffb662008-07-01 17:59:20 +0000451 } else {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000452 CurDAG->Combine(Unrestricted, *AA, OptLevel);
Evan Chengebffb662008-07-01 17:59:20 +0000453 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000454
David Greene1a053232010-01-05 01:26:11 +0000455 DEBUG(dbgs() << "Optimized lowered selection DAG:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000456 DEBUG(CurDAG->dump());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000457
Chris Lattner1c08c712005-01-07 07:47:53 +0000458 // Second step, hack on the DAG until it only uses operations and types that
459 // the target supports.
Dan Gohman714efc62009-12-05 17:51:33 +0000460 if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " +
461 BlockName);
Dan Gohman462dc7f2008-07-21 20:00:07 +0000462
Dan Gohman714efc62009-12-05 17:51:33 +0000463 bool Changed;
464 if (TimePassesIsEnabled) {
465 NamedRegionTimer T("Type Legalization", GroupName);
466 Changed = CurDAG->LegalizeTypes();
467 } else {
468 Changed = CurDAG->LegalizeTypes();
469 }
470
David Greene1a053232010-01-05 01:26:11 +0000471 DEBUG(dbgs() << "Type-legalized selection DAG:\n");
Dan Gohman714efc62009-12-05 17:51:33 +0000472 DEBUG(CurDAG->dump());
473
474 if (Changed) {
475 if (ViewDAGCombineLT)
476 CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
477
478 // Run the DAG combiner in post-type-legalize mode.
Dan Gohman462dc7f2008-07-21 20:00:07 +0000479 if (TimePassesIsEnabled) {
Dan Gohman714efc62009-12-05 17:51:33 +0000480 NamedRegionTimer T("DAG Combining after legalize types", GroupName);
481 CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
Dan Gohman462dc7f2008-07-21 20:00:07 +0000482 } else {
Dan Gohman714efc62009-12-05 17:51:33 +0000483 CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
Dan Gohman462dc7f2008-07-21 20:00:07 +0000484 }
485
David Greene1a053232010-01-05 01:26:11 +0000486 DEBUG(dbgs() << "Optimized type-legalized selection DAG:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000487 DEBUG(CurDAG->dump());
Dan Gohman714efc62009-12-05 17:51:33 +0000488 }
Dan Gohman462dc7f2008-07-21 20:00:07 +0000489
Dan Gohman714efc62009-12-05 17:51:33 +0000490 if (TimePassesIsEnabled) {
491 NamedRegionTimer T("Vector Legalization", GroupName);
492 Changed = CurDAG->LegalizeVectors();
493 } else {
494 Changed = CurDAG->LegalizeVectors();
495 }
Duncan Sands25cf2272008-11-24 14:53:14 +0000496
Dan Gohman714efc62009-12-05 17:51:33 +0000497 if (Changed) {
Eli Friedman5c22c802009-05-23 12:35:30 +0000498 if (TimePassesIsEnabled) {
Dan Gohman714efc62009-12-05 17:51:33 +0000499 NamedRegionTimer T("Type Legalization 2", GroupName);
Bill Wendling98820072009-12-28 01:51:30 +0000500 CurDAG->LegalizeTypes();
Eli Friedman5c22c802009-05-23 12:35:30 +0000501 } else {
Bill Wendling98820072009-12-28 01:51:30 +0000502 CurDAG->LegalizeTypes();
Eli Friedman5c22c802009-05-23 12:35:30 +0000503 }
504
Dan Gohman714efc62009-12-05 17:51:33 +0000505 if (ViewDAGCombineLT)
506 CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
Eli Friedman5c22c802009-05-23 12:35:30 +0000507
Dan Gohman714efc62009-12-05 17:51:33 +0000508 // Run the DAG combiner in post-type-legalize mode.
509 if (TimePassesIsEnabled) {
510 NamedRegionTimer T("DAG Combining after legalize vectors", GroupName);
511 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
512 } else {
513 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
Eli Friedman5c22c802009-05-23 12:35:30 +0000514 }
Dan Gohman714efc62009-12-05 17:51:33 +0000515
David Greene1a053232010-01-05 01:26:11 +0000516 DEBUG(dbgs() << "Optimized vector-legalized selection DAG:\n");
Dan Gohman714efc62009-12-05 17:51:33 +0000517 DEBUG(CurDAG->dump());
Chris Lattner70587ea2008-07-10 23:37:50 +0000518 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000519
Dan Gohmanf350b272008-08-23 02:25:05 +0000520 if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName);
Dan Gohman462dc7f2008-07-21 20:00:07 +0000521
Evan Chengebffb662008-07-01 17:59:20 +0000522 if (TimePassesIsEnabled) {
Dan Gohman5e843682008-07-14 18:19:29 +0000523 NamedRegionTimer T("DAG Legalization", GroupName);
Dan Gohman714efc62009-12-05 17:51:33 +0000524 CurDAG->Legalize(OptLevel);
Evan Chengebffb662008-07-01 17:59:20 +0000525 } else {
Dan Gohman714efc62009-12-05 17:51:33 +0000526 CurDAG->Legalize(OptLevel);
Evan Chengebffb662008-07-01 17:59:20 +0000527 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000528
David Greene1a053232010-01-05 01:26:11 +0000529 DEBUG(dbgs() << "Legalized selection DAG:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000530 DEBUG(CurDAG->dump());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000531
Dan Gohmanf350b272008-08-23 02:25:05 +0000532 if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName);
Dan Gohman462dc7f2008-07-21 20:00:07 +0000533
Chris Lattneraf21d552005-10-10 16:47:10 +0000534 // Run the DAG combiner in post-legalize mode.
Evan Chengebffb662008-07-01 17:59:20 +0000535 if (TimePassesIsEnabled) {
Dan Gohman5e843682008-07-14 18:19:29 +0000536 NamedRegionTimer T("DAG Combining 2", GroupName);
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000537 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
Evan Chengebffb662008-07-01 17:59:20 +0000538 } else {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000539 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
Evan Chengebffb662008-07-01 17:59:20 +0000540 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000541
David Greene1a053232010-01-05 01:26:11 +0000542 DEBUG(dbgs() << "Optimized legalized selection DAG:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000543 DEBUG(CurDAG->dump());
Dan Gohman417e11b2007-10-08 15:12:17 +0000544
Evan Chengd40d03e2010-01-06 19:38:29 +0000545 if (OptLevel != CodeGenOpt::None) {
546 ShrinkDemandedOps();
Dan Gohmanf350b272008-08-23 02:25:05 +0000547 ComputeLiveOutVRegInfo();
Evan Chengd40d03e2010-01-06 19:38:29 +0000548 }
Evan Cheng552c4a82006-04-28 02:09:19 +0000549
Chris Lattner552186d2010-03-14 19:27:55 +0000550 if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName);
551
Chris Lattnera33ef482005-03-30 01:10:47 +0000552 // Third, instruction select all of the operations to machine code, adding the
553 // code to the MachineBasicBlock.
Evan Chengebffb662008-07-01 17:59:20 +0000554 if (TimePassesIsEnabled) {
Dan Gohman5e843682008-07-14 18:19:29 +0000555 NamedRegionTimer T("Instruction Selection", GroupName);
Chris Lattner7c306da2010-03-02 06:34:30 +0000556 DoInstructionSelection();
Evan Chengebffb662008-07-01 17:59:20 +0000557 } else {
Chris Lattner7c306da2010-03-02 06:34:30 +0000558 DoInstructionSelection();
Evan Chengebffb662008-07-01 17:59:20 +0000559 }
Evan Chengdb8d56b2008-06-30 20:45:06 +0000560
David Greene1a053232010-01-05 01:26:11 +0000561 DEBUG(dbgs() << "Selected selection DAG:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000562 DEBUG(CurDAG->dump());
Dan Gohman462dc7f2008-07-21 20:00:07 +0000563
Dan Gohmanf350b272008-08-23 02:25:05 +0000564 if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName);
Dan Gohman462dc7f2008-07-21 20:00:07 +0000565
Dan Gohman5e843682008-07-14 18:19:29 +0000566 // Schedule machine code.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000567 ScheduleDAGSDNodes *Scheduler = CreateScheduler();
Dan Gohman5e843682008-07-14 18:19:29 +0000568 if (TimePassesIsEnabled) {
569 NamedRegionTimer T("Instruction Scheduling", GroupName);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000570 Scheduler->Run(CurDAG, BB, BB->end());
Dan Gohman5e843682008-07-14 18:19:29 +0000571 } else {
Dan Gohman47ac0f02009-02-11 04:27:20 +0000572 Scheduler->Run(CurDAG, BB, BB->end());
Dan Gohman5e843682008-07-14 18:19:29 +0000573 }
574
Dan Gohman462dc7f2008-07-21 20:00:07 +0000575 if (ViewSUnitDAGs) Scheduler->viewGraph();
576
Daniel Dunbara279bc32009-09-20 02:20:51 +0000577 // Emit machine code to BB. This can change 'BB' to the last block being
Evan Chengdb8d56b2008-06-30 20:45:06 +0000578 // inserted into.
Evan Chengebffb662008-07-01 17:59:20 +0000579 if (TimePassesIsEnabled) {
Dan Gohman5e843682008-07-14 18:19:29 +0000580 NamedRegionTimer T("Instruction Creation", GroupName);
Dan Gohman2048b852009-11-23 18:04:58 +0000581 BB = Scheduler->EmitSchedule(&SDB->EdgeMapping);
Evan Chengebffb662008-07-01 17:59:20 +0000582 } else {
Dan Gohman2048b852009-11-23 18:04:58 +0000583 BB = Scheduler->EmitSchedule(&SDB->EdgeMapping);
Dan Gohman5e843682008-07-14 18:19:29 +0000584 }
585
586 // Free the scheduler state.
587 if (TimePassesIsEnabled) {
588 NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName);
589 delete Scheduler;
590 } else {
591 delete Scheduler;
Evan Chengebffb662008-07-01 17:59:20 +0000592 }
Evan Chengdb8d56b2008-06-30 20:45:06 +0000593
Dan Gohmana9a33212010-04-20 00:29:35 +0000594 return BB;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000595}
Chris Lattner1c08c712005-01-07 07:47:53 +0000596
Chris Lattner7c306da2010-03-02 06:34:30 +0000597void SelectionDAGISel::DoInstructionSelection() {
598 DEBUG(errs() << "===== Instruction selection begins:\n");
599
600 PreprocessISelDAG();
601
602 // Select target instructions for the DAG.
603 {
604 // Number all nodes with a topological order and set DAGSize.
605 DAGSize = CurDAG->AssignTopologicalOrder();
606
607 // Create a dummy node (which is not added to allnodes), that adds
608 // a reference to the root node, preventing it from being deleted,
609 // and tracking any changes of the root.
610 HandleSDNode Dummy(CurDAG->getRoot());
611 ISelPosition = SelectionDAG::allnodes_iterator(CurDAG->getRoot().getNode());
612 ++ISelPosition;
613
614 // The AllNodes list is now topological-sorted. Visit the
615 // nodes by starting at the end of the list (the root of the
616 // graph) and preceding back toward the beginning (the entry
617 // node).
618 while (ISelPosition != CurDAG->allnodes_begin()) {
619 SDNode *Node = --ISelPosition;
620 // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes,
621 // but there are currently some corner cases that it misses. Also, this
622 // makes it theoretically possible to disable the DAGCombiner.
623 if (Node->use_empty())
624 continue;
625
626 SDNode *ResNode = Select(Node);
627
Chris Lattner82dd3d32010-03-02 07:50:03 +0000628 // FIXME: This is pretty gross. 'Select' should be changed to not return
629 // anything at all and this code should be nuked with a tactical strike.
630
Chris Lattner7c306da2010-03-02 06:34:30 +0000631 // If node should not be replaced, continue with the next one.
Chris Lattner82dd3d32010-03-02 07:50:03 +0000632 if (ResNode == Node || Node->getOpcode() == ISD::DELETED_NODE)
Chris Lattner7c306da2010-03-02 06:34:30 +0000633 continue;
634 // Replace node.
635 if (ResNode)
636 ReplaceUses(Node, ResNode);
637
638 // If after the replacement this node is not used any more,
639 // remove this dead node.
640 if (Node->use_empty()) { // Don't delete EntryToken, etc.
641 ISelUpdater ISU(ISelPosition);
642 CurDAG->RemoveDeadNode(Node, &ISU);
643 }
644 }
645
646 CurDAG->setRoot(Dummy.getValue());
647 }
648 DEBUG(errs() << "===== Instruction selection ends:\n");
649
650 PostprocessISelDAG();
Chris Lattner7c306da2010-03-02 06:34:30 +0000651}
652
Dan Gohman25208642010-04-14 19:53:31 +0000653/// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and
654/// do other setup for EH landing-pad blocks.
655void SelectionDAGISel::PrepareEHLandingPad(MachineBasicBlock *BB) {
656 // Add a label to mark the beginning of the landing pad. Deletion of the
657 // landing pad can thus be detected via the MachineModuleInfo.
658 MCSymbol *Label = MF->getMMI().addLandingPad(BB);
659
Dan Gohman55e59c12010-04-19 19:05:59 +0000660 const TargetInstrDesc &II = TM.getInstrInfo()->get(TargetOpcode::EH_LABEL);
Dan Gohman25208642010-04-14 19:53:31 +0000661 BuildMI(BB, SDB->getCurDebugLoc(), II).addSym(Label);
662
663 // Mark exception register as live in.
664 unsigned Reg = TLI.getExceptionAddressRegister();
665 if (Reg) BB->addLiveIn(Reg);
666
667 // Mark exception selector register as live in.
668 Reg = TLI.getExceptionSelectorRegister();
669 if (Reg) BB->addLiveIn(Reg);
670
671 // FIXME: Hack around an exception handling flaw (PR1508): the personality
672 // function and list of typeids logically belong to the invoke (or, if you
673 // like, the basic block containing the invoke), and need to be associated
674 // with it in the dwarf exception handling tables. Currently however the
675 // information is provided by an intrinsic (eh.selector) that can be moved
676 // to unexpected places by the optimizers: if the unwind edge is critical,
677 // then breaking it can result in the intrinsics being in the successor of
678 // the landing pad, not the landing pad itself. This results
679 // in exceptions not being caught because no typeids are associated with
680 // the invoke. This may not be the only way things can go wrong, but it
681 // is the only way we try to work around for the moment.
682 const BasicBlock *LLVMBB = BB->getBasicBlock();
683 const BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator());
684
685 if (Br && Br->isUnconditional()) { // Critical edge?
686 BasicBlock::const_iterator I, E;
687 for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I)
688 if (isa<EHSelectorInst>(I))
689 break;
690
691 if (I == E)
692 // No catch info found - try to extract some from the successor.
693 CopyCatchInfo(Br->getSuccessor(0), LLVMBB, &MF->getMMI(), *FuncInfo);
694 }
695}
Chris Lattner7c306da2010-03-02 06:34:30 +0000696
Dan Gohman46510a72010-04-15 01:51:59 +0000697void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
Dan Gohmana43abd12008-09-29 21:55:50 +0000698 // Initialize the Fast-ISel state, if needed.
699 FastISel *FastIS = 0;
700 if (EnableFastISel)
Dan Gohman6a732b52010-04-14 20:05:00 +0000701 FastIS = TLI.createFastISel(*MF, FuncInfo->ValueMap, FuncInfo->MBBMap,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000702 FuncInfo->StaticAllocaMap,
703 FuncInfo->PHINodesToUpdate
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000704#ifndef NDEBUG
705 , FuncInfo->CatchInfoLost
706#endif
707 );
Dan Gohmana43abd12008-09-29 21:55:50 +0000708
709 // Iterate over all basic blocks in the function.
Dan Gohman46510a72010-04-15 01:51:59 +0000710 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
711 const BasicBlock *LLVMBB = &*I;
Dan Gohmana9a33212010-04-20 00:29:35 +0000712 MachineBasicBlock *BB = FuncInfo->MBBMap[LLVMBB];
Dan Gohmanf350b272008-08-23 02:25:05 +0000713
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000714 BasicBlock::const_iterator const Begin = LLVMBB->getFirstNonPHI();
Dan Gohman46510a72010-04-15 01:51:59 +0000715 BasicBlock::const_iterator const End = LLVMBB->end();
716 BasicBlock::const_iterator BI = Begin;
Dan Gohman5edd3612008-08-28 20:28:56 +0000717
718 // Lower any arguments needed in this block if this is the entry block.
Dan Gohman33134c42008-09-25 17:05:24 +0000719 bool SuppressFastISel = false;
720 if (LLVMBB == &Fn.getEntryBlock()) {
Dan Gohman5edd3612008-08-28 20:28:56 +0000721 LowerArguments(LLVMBB);
Dan Gohmanf350b272008-08-23 02:25:05 +0000722
Dan Gohman33134c42008-09-25 17:05:24 +0000723 // If any of the arguments has the byval attribute, forgo
724 // fast-isel in the entry block.
Dan Gohmana43abd12008-09-29 21:55:50 +0000725 if (FastIS) {
Dan Gohman33134c42008-09-25 17:05:24 +0000726 unsigned j = 1;
Dan Gohman46510a72010-04-15 01:51:59 +0000727 for (Function::const_arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
Dan Gohman33134c42008-09-25 17:05:24 +0000728 I != E; ++I, ++j)
Devang Patel05988662008-09-25 21:00:45 +0000729 if (Fn.paramHasAttr(j, Attribute::ByVal)) {
Dan Gohman77ca41e2008-09-25 17:21:42 +0000730 if (EnableFastISelVerbose || EnableFastISelAbort)
David Greene1a053232010-01-05 01:26:11 +0000731 dbgs() << "FastISel skips entry block due to byval argument\n";
Dan Gohman33134c42008-09-25 17:05:24 +0000732 SuppressFastISel = true;
733 break;
734 }
735 }
736 }
737
Dan Gohman25208642010-04-14 19:53:31 +0000738 // Setup an EH landing-pad block.
739 if (BB->isLandingPad())
740 PrepareEHLandingPad(BB);
741
Dan Gohmanf350b272008-08-23 02:25:05 +0000742 // Before doing SelectionDAG ISel, see if FastISel has been requested.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000743 if (FastIS && !SuppressFastISel) {
Dan Gohmana43abd12008-09-29 21:55:50 +0000744 // Emit code for any incoming arguments. This must happen before
745 // beginning FastISel on the entry block.
746 if (LLVMBB == &Fn.getEntryBlock()) {
Dan Gohman2048b852009-11-23 18:04:58 +0000747 CurDAG->setRoot(SDB->getControlRoot());
Dan Gohmana9a33212010-04-20 00:29:35 +0000748 BB = CodeGenAndEmitDAG(BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000749 SDB->clear();
Dan Gohmana43abd12008-09-29 21:55:50 +0000750 }
Dan Gohman241f4642008-10-04 00:56:36 +0000751 FastIS->startNewBlock(BB);
Dan Gohmana43abd12008-09-29 21:55:50 +0000752 // Do FastISel on as many instructions as possible.
753 for (; BI != End; ++BI) {
Dan Gohman21c14e32010-01-12 04:32:35 +0000754 // Try to select the instruction with FastISel.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000755 if (FastIS->SelectInstruction(BI))
Dan Gohmana43abd12008-09-29 21:55:50 +0000756 continue;
Dan Gohmana43abd12008-09-29 21:55:50 +0000757
758 // Then handle certain instructions as single-LLVM-Instruction blocks.
759 if (isa<CallInst>(BI)) {
Chris Lattnerc19ae9d2010-03-04 19:46:56 +0000760 ++NumFastIselFailures;
Dan Gohmana43abd12008-09-29 21:55:50 +0000761 if (EnableFastISelVerbose || EnableFastISelAbort) {
David Greene1a053232010-01-05 01:26:11 +0000762 dbgs() << "FastISel missed call: ";
Dan Gohmana43abd12008-09-29 21:55:50 +0000763 BI->dump();
764 }
765
Dan Gohman33b7a292010-04-16 17:15:02 +0000766 if (!BI->getType()->isVoidTy() && !BI->use_empty()) {
Dan Gohmana43abd12008-09-29 21:55:50 +0000767 unsigned &R = FuncInfo->ValueMap[BI];
768 if (!R)
769 R = FuncInfo->CreateRegForValue(BI);
770 }
771
Dan Gohmanb4afb132009-11-20 02:51:26 +0000772 bool HadTailCall = false;
Dan Gohmana9a33212010-04-20 00:29:35 +0000773 BB = SelectBasicBlock(BB, LLVMBB, BI, llvm::next(BI), HadTailCall);
Dan Gohmanb4afb132009-11-20 02:51:26 +0000774
775 // If the call was emitted as a tail call, we're done with the block.
776 if (HadTailCall) {
777 BI = End;
778 break;
779 }
780
Dan Gohman241f4642008-10-04 00:56:36 +0000781 // If the instruction was codegen'd with multiple blocks,
782 // inform the FastISel object where to resume inserting.
783 FastIS->setCurrentBlock(BB);
Dan Gohmana43abd12008-09-29 21:55:50 +0000784 continue;
Dan Gohmanf350b272008-08-23 02:25:05 +0000785 }
Dan Gohmana43abd12008-09-29 21:55:50 +0000786
787 // Otherwise, give up on FastISel for the rest of the block.
788 // For now, be a little lenient about non-branch terminators.
789 if (!isa<TerminatorInst>(BI) || isa<BranchInst>(BI)) {
Chris Lattnerc19ae9d2010-03-04 19:46:56 +0000790 ++NumFastIselFailures;
Dan Gohmana43abd12008-09-29 21:55:50 +0000791 if (EnableFastISelVerbose || EnableFastISelAbort) {
David Greene1a053232010-01-05 01:26:11 +0000792 dbgs() << "FastISel miss: ";
Dan Gohmana43abd12008-09-29 21:55:50 +0000793 BI->dump();
794 }
795 if (EnableFastISelAbort)
796 // The "fast" selector couldn't handle something and bailed.
797 // For the purpose of debugging, just abort.
Torok Edwinc23197a2009-07-14 16:55:14 +0000798 llvm_unreachable("FastISel didn't select the entire block");
Dan Gohmana43abd12008-09-29 21:55:50 +0000799 }
800 break;
Dan Gohmanf350b272008-08-23 02:25:05 +0000801 }
802 }
803
Dan Gohmand2ff6472008-09-02 20:17:56 +0000804 // Run SelectionDAG instruction selection on the remainder of the block
805 // not handled by FastISel. If FastISel is not run, this is the entire
Dan Gohman3df24e62008-09-03 23:12:08 +0000806 // block.
Devang Patel390f3ac2009-04-16 01:33:10 +0000807 if (BI != End) {
Dan Gohmanb4afb132009-11-20 02:51:26 +0000808 bool HadTailCall;
Dan Gohmana9a33212010-04-20 00:29:35 +0000809 BB = SelectBasicBlock(BB, LLVMBB, BI, End, HadTailCall);
Devang Patel390f3ac2009-04-16 01:33:10 +0000810 }
Dan Gohmanf350b272008-08-23 02:25:05 +0000811
Dan Gohmana9a33212010-04-20 00:29:35 +0000812 FinishBasicBlock(BB);
Dan Gohman620427d2010-04-22 19:55:20 +0000813 FuncInfo->PHINodesToUpdate.clear();
Evan Cheng39fd6e82008-08-07 00:43:25 +0000814 }
Dan Gohmana43abd12008-09-29 21:55:50 +0000815
816 delete FastIS;
Dan Gohman0e5f1302008-07-07 23:02:41 +0000817}
818
Dan Gohmanfed90b62008-07-28 21:51:04 +0000819void
Dan Gohmana9a33212010-04-20 00:29:35 +0000820SelectionDAGISel::FinishBasicBlock(MachineBasicBlock *BB) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000821
David Greene1a053232010-01-05 01:26:11 +0000822 DEBUG(dbgs() << "Target-post-processed machine code:\n");
Dan Gohmanf350b272008-08-23 02:25:05 +0000823 DEBUG(BB->dump());
Nate Begemanf15485a2006-03-27 01:32:24 +0000824
David Greene1a053232010-01-05 01:26:11 +0000825 DEBUG(dbgs() << "Total amount of phi nodes to update: "
Dan Gohman620427d2010-04-22 19:55:20 +0000826 << FuncInfo->PHINodesToUpdate.size() << "\n");
827 DEBUG(for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i)
David Greene1a053232010-01-05 01:26:11 +0000828 dbgs() << "Node " << i << " : ("
Dan Gohman620427d2010-04-22 19:55:20 +0000829 << FuncInfo->PHINodesToUpdate[i].first
830 << ", " << FuncInfo->PHINodesToUpdate[i].second << ")\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000831
Chris Lattnera33ef482005-03-30 01:10:47 +0000832 // Next, now that we know what the last MBB the LLVM BB expanded is, update
Chris Lattner1c08c712005-01-07 07:47:53 +0000833 // PHI nodes in successors.
Dan Gohman2048b852009-11-23 18:04:58 +0000834 if (SDB->SwitchCases.empty() &&
835 SDB->JTCases.empty() &&
836 SDB->BitTestCases.empty()) {
Dan Gohman620427d2010-04-22 19:55:20 +0000837 for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) {
838 MachineInstr *PHI = FuncInfo->PHINodesToUpdate[i].first;
Chris Lattner518bb532010-02-09 19:54:29 +0000839 assert(PHI->isPHI() &&
Nate Begemanf15485a2006-03-27 01:32:24 +0000840 "This is not a machine PHI node that we are updating!");
Jakob Stoklund Olesen580bba22010-03-05 21:49:10 +0000841 if (!BB->isSuccessor(PHI->getParent()))
842 continue;
Dan Gohman620427d2010-04-22 19:55:20 +0000843 PHI->addOperand(
844 MachineOperand::CreateReg(FuncInfo->PHINodesToUpdate[i].second, false));
Chris Lattner9ce2e9d2007-12-30 00:57:42 +0000845 PHI->addOperand(MachineOperand::CreateMBB(BB));
Nate Begemanf15485a2006-03-27 01:32:24 +0000846 }
847 return;
Chris Lattner1c08c712005-01-07 07:47:53 +0000848 }
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000849
Dan Gohman2048b852009-11-23 18:04:58 +0000850 for (unsigned i = 0, e = SDB->BitTestCases.size(); i != e; ++i) {
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000851 // Lower header first, if it wasn't already lowered
Dan Gohman2048b852009-11-23 18:04:58 +0000852 if (!SDB->BitTestCases[i].Emitted) {
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000853 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman2048b852009-11-23 18:04:58 +0000854 BB = SDB->BitTestCases[i].Parent;
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000855 // Emit the code
Dan Gohman99be8ae2010-04-19 22:41:47 +0000856 SDB->visitBitTestHeader(SDB->BitTestCases[i], BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000857 CurDAG->setRoot(SDB->getRoot());
Dan Gohmana9a33212010-04-20 00:29:35 +0000858 BB = CodeGenAndEmitDAG(BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000859 SDB->clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000860 }
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000861
Dan Gohman2048b852009-11-23 18:04:58 +0000862 for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size(); j != ej; ++j) {
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000863 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman2048b852009-11-23 18:04:58 +0000864 BB = SDB->BitTestCases[i].Cases[j].ThisBB;
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000865 // Emit the code
866 if (j+1 != ej)
Dan Gohman2048b852009-11-23 18:04:58 +0000867 SDB->visitBitTestCase(SDB->BitTestCases[i].Cases[j+1].ThisBB,
868 SDB->BitTestCases[i].Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000869 SDB->BitTestCases[i].Cases[j],
870 BB);
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000871 else
Dan Gohman2048b852009-11-23 18:04:58 +0000872 SDB->visitBitTestCase(SDB->BitTestCases[i].Default,
873 SDB->BitTestCases[i].Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000874 SDB->BitTestCases[i].Cases[j],
875 BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000876
877
Dan Gohman2048b852009-11-23 18:04:58 +0000878 CurDAG->setRoot(SDB->getRoot());
Dan Gohmana9a33212010-04-20 00:29:35 +0000879 BB = CodeGenAndEmitDAG(BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000880 SDB->clear();
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000881 }
882
883 // Update PHI Nodes
Dan Gohman620427d2010-04-22 19:55:20 +0000884 for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size();
885 pi != pe; ++pi) {
886 MachineInstr *PHI = FuncInfo->PHINodesToUpdate[pi].first;
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000887 MachineBasicBlock *PHIBB = PHI->getParent();
Chris Lattner518bb532010-02-09 19:54:29 +0000888 assert(PHI->isPHI() &&
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000889 "This is not a machine PHI node that we are updating!");
890 // This is "default" BB. We have two jumps to it. From "header" BB and
891 // from last "case" BB.
Dan Gohman2048b852009-11-23 18:04:58 +0000892 if (PHIBB == SDB->BitTestCases[i].Default) {
Jim Grosbachf4549b02010-01-15 00:36:15 +0000893 PHI->addOperand(MachineOperand::
Dan Gohman620427d2010-04-22 19:55:20 +0000894 CreateReg(FuncInfo->PHINodesToUpdate[pi].second,
895 false));
Dan Gohman2048b852009-11-23 18:04:58 +0000896 PHI->addOperand(MachineOperand::CreateMBB(SDB->BitTestCases[i].Parent));
Jim Grosbachf4549b02010-01-15 00:36:15 +0000897 PHI->addOperand(MachineOperand::
Dan Gohman620427d2010-04-22 19:55:20 +0000898 CreateReg(FuncInfo->PHINodesToUpdate[pi].second,
899 false));
Dan Gohman2048b852009-11-23 18:04:58 +0000900 PHI->addOperand(MachineOperand::CreateMBB(SDB->BitTestCases[i].Cases.
Chris Lattner9ce2e9d2007-12-30 00:57:42 +0000901 back().ThisBB));
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000902 }
903 // One of "cases" BB.
Dan Gohman2048b852009-11-23 18:04:58 +0000904 for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000905 j != ej; ++j) {
Dan Gohman2048b852009-11-23 18:04:58 +0000906 MachineBasicBlock* cBB = SDB->BitTestCases[i].Cases[j].ThisBB;
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +0000907 if (cBB->isSuccessor(PHIBB)) {
Jim Grosbachf4549b02010-01-15 00:36:15 +0000908 PHI->addOperand(MachineOperand::
Dan Gohman620427d2010-04-22 19:55:20 +0000909 CreateReg(FuncInfo->PHINodesToUpdate[pi].second,
910 false));
Chris Lattner9ce2e9d2007-12-30 00:57:42 +0000911 PHI->addOperand(MachineOperand::CreateMBB(cBB));
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000912 }
913 }
914 }
915 }
Dan Gohman2048b852009-11-23 18:04:58 +0000916 SDB->BitTestCases.clear();
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000917
Nate Begeman9453eea2006-04-23 06:26:20 +0000918 // If the JumpTable record is filled in, then we need to emit a jump table.
919 // Updating the PHI nodes is tricky in this case, since we need to determine
920 // whether the PHI is a successor of the range check MBB or the jump table MBB
Dan Gohman2048b852009-11-23 18:04:58 +0000921 for (unsigned i = 0, e = SDB->JTCases.size(); i != e; ++i) {
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +0000922 // Lower header first, if it wasn't already lowered
Dan Gohman2048b852009-11-23 18:04:58 +0000923 if (!SDB->JTCases[i].first.Emitted) {
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +0000924 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman2048b852009-11-23 18:04:58 +0000925 BB = SDB->JTCases[i].first.HeaderBB;
Anton Korobeynikov3a84b9b2007-03-25 15:07:15 +0000926 // Emit the code
Dan Gohman99be8ae2010-04-19 22:41:47 +0000927 SDB->visitJumpTableHeader(SDB->JTCases[i].second, SDB->JTCases[i].first,
928 BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000929 CurDAG->setRoot(SDB->getRoot());
Dan Gohmana9a33212010-04-20 00:29:35 +0000930 BB = CodeGenAndEmitDAG(BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000931 SDB->clear();
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000932 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000933
Nate Begeman37efe672006-04-22 18:53:45 +0000934 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman2048b852009-11-23 18:04:58 +0000935 BB = SDB->JTCases[i].second.MBB;
Nate Begeman37efe672006-04-22 18:53:45 +0000936 // Emit the code
Dan Gohman2048b852009-11-23 18:04:58 +0000937 SDB->visitJumpTable(SDB->JTCases[i].second);
938 CurDAG->setRoot(SDB->getRoot());
Dan Gohmana9a33212010-04-20 00:29:35 +0000939 BB = CodeGenAndEmitDAG(BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000940 SDB->clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000941
Nate Begeman37efe672006-04-22 18:53:45 +0000942 // Update PHI Nodes
Dan Gohman620427d2010-04-22 19:55:20 +0000943 for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size();
944 pi != pe; ++pi) {
945 MachineInstr *PHI = FuncInfo->PHINodesToUpdate[pi].first;
Nate Begeman37efe672006-04-22 18:53:45 +0000946 MachineBasicBlock *PHIBB = PHI->getParent();
Chris Lattner518bb532010-02-09 19:54:29 +0000947 assert(PHI->isPHI() &&
Nate Begeman37efe672006-04-22 18:53:45 +0000948 "This is not a machine PHI node that we are updating!");
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000949 // "default" BB. We can go there only from header BB.
Dan Gohman2048b852009-11-23 18:04:58 +0000950 if (PHIBB == SDB->JTCases[i].second.Default) {
Evan Chengce319102009-09-19 09:51:03 +0000951 PHI->addOperand
Dan Gohman620427d2010-04-22 19:55:20 +0000952 (MachineOperand::CreateReg(FuncInfo->PHINodesToUpdate[pi].second,
953 false));
Evan Chengce319102009-09-19 09:51:03 +0000954 PHI->addOperand
Dan Gohman2048b852009-11-23 18:04:58 +0000955 (MachineOperand::CreateMBB(SDB->JTCases[i].first.HeaderBB));
Nate Begemanf4360a42006-05-03 03:48:02 +0000956 }
Anton Korobeynikov4198c582007-04-09 12:31:58 +0000957 // JT BB. Just iterate over successors here
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +0000958 if (BB->isSuccessor(PHIBB)) {
Evan Chengce319102009-09-19 09:51:03 +0000959 PHI->addOperand
Dan Gohman620427d2010-04-22 19:55:20 +0000960 (MachineOperand::CreateReg(FuncInfo->PHINodesToUpdate[pi].second,
961 false));
Chris Lattner9ce2e9d2007-12-30 00:57:42 +0000962 PHI->addOperand(MachineOperand::CreateMBB(BB));
Nate Begeman37efe672006-04-22 18:53:45 +0000963 }
964 }
Nate Begeman37efe672006-04-22 18:53:45 +0000965 }
Dan Gohman2048b852009-11-23 18:04:58 +0000966 SDB->JTCases.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000967
Chris Lattnerb2e806e2006-10-22 23:00:53 +0000968 // If the switch block involved a branch to one of the actual successors, we
969 // need to update PHI nodes in that block.
Dan Gohman620427d2010-04-22 19:55:20 +0000970 for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) {
971 MachineInstr *PHI = FuncInfo->PHINodesToUpdate[i].first;
Chris Lattner518bb532010-02-09 19:54:29 +0000972 assert(PHI->isPHI() &&
Chris Lattnerb2e806e2006-10-22 23:00:53 +0000973 "This is not a machine PHI node that we are updating!");
974 if (BB->isSuccessor(PHI->getParent())) {
Dan Gohman620427d2010-04-22 19:55:20 +0000975 PHI->addOperand(
976 MachineOperand::CreateReg(FuncInfo->PHINodesToUpdate[i].second, false));
Chris Lattner9ce2e9d2007-12-30 00:57:42 +0000977 PHI->addOperand(MachineOperand::CreateMBB(BB));
Chris Lattnerb2e806e2006-10-22 23:00:53 +0000978 }
979 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000980
Nate Begemanf15485a2006-03-27 01:32:24 +0000981 // If we generated any switch lowering information, build and codegen any
982 // additional DAGs necessary.
Dan Gohman2048b852009-11-23 18:04:58 +0000983 for (unsigned i = 0, e = SDB->SwitchCases.size(); i != e; ++i) {
Nate Begemanf15485a2006-03-27 01:32:24 +0000984 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman2048b852009-11-23 18:04:58 +0000985 MachineBasicBlock *ThisBB = BB = SDB->SwitchCases[i].ThisBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000986
Nate Begemanf15485a2006-03-27 01:32:24 +0000987 // Emit the code
Dan Gohman99be8ae2010-04-19 22:41:47 +0000988 SDB->visitSwitchCase(SDB->SwitchCases[i], BB);
Dan Gohman2048b852009-11-23 18:04:58 +0000989 CurDAG->setRoot(SDB->getRoot());
Dan Gohmana9a33212010-04-20 00:29:35 +0000990 BB = CodeGenAndEmitDAG(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000991
Chris Lattnerd5e93c02006-09-07 01:59:34 +0000992 // Handle any PHI nodes in successors of this chunk, as if we were coming
993 // from the original BB before switch expansion. Note that PHI nodes can
994 // occur multiple times in PHINodesToUpdate. We have to be very careful to
995 // handle them the right number of times.
Dan Gohman2048b852009-11-23 18:04:58 +0000996 while ((BB = SDB->SwitchCases[i].TrueBB)) { // Handle LHS and RHS.
Evan Chengfb2e7522009-09-18 21:02:19 +0000997 // If new BB's are created during scheduling, the edges may have been
Evan Chengce319102009-09-19 09:51:03 +0000998 // updated. That is, the edge from ThisBB to BB may have been split and
999 // BB's predecessor is now another block.
Evan Chengfb2e7522009-09-18 21:02:19 +00001000 DenseMap<MachineBasicBlock*, MachineBasicBlock*>::iterator EI =
Dan Gohman2048b852009-11-23 18:04:58 +00001001 SDB->EdgeMapping.find(BB);
1002 if (EI != SDB->EdgeMapping.end())
Evan Chengfb2e7522009-09-18 21:02:19 +00001003 ThisBB = EI->second;
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +00001004
1005 // BB may have been removed from the CFG if a branch was constant folded.
1006 if (ThisBB->isSuccessor(BB)) {
1007 for (MachineBasicBlock::iterator Phi = BB->begin();
Chris Lattner518bb532010-02-09 19:54:29 +00001008 Phi != BB->end() && Phi->isPHI();
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +00001009 ++Phi) {
1010 // This value for this PHI node is recorded in PHINodesToUpdate.
1011 for (unsigned pn = 0; ; ++pn) {
Dan Gohman620427d2010-04-22 19:55:20 +00001012 assert(pn != FuncInfo->PHINodesToUpdate.size() &&
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +00001013 "Didn't find PHI entry!");
Dan Gohman620427d2010-04-22 19:55:20 +00001014 if (FuncInfo->PHINodesToUpdate[pn].first == Phi) {
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +00001015 Phi->addOperand(MachineOperand::
Dan Gohman620427d2010-04-22 19:55:20 +00001016 CreateReg(FuncInfo->PHINodesToUpdate[pn].second,
Jakob Stoklund Olesendd437ba2010-01-11 21:02:33 +00001017 false));
1018 Phi->addOperand(MachineOperand::CreateMBB(ThisBB));
1019 break;
1020 }
Evan Cheng8be58a12009-09-18 08:26:06 +00001021 }
Chris Lattnerd5e93c02006-09-07 01:59:34 +00001022 }
Nate Begemanf15485a2006-03-27 01:32:24 +00001023 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001024
Chris Lattnerd5e93c02006-09-07 01:59:34 +00001025 // Don't process RHS if same block as LHS.
Dan Gohman2048b852009-11-23 18:04:58 +00001026 if (BB == SDB->SwitchCases[i].FalseBB)
1027 SDB->SwitchCases[i].FalseBB = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001028
Chris Lattnerd5e93c02006-09-07 01:59:34 +00001029 // If we haven't handled the RHS, do so now. Otherwise, we're done.
Dan Gohman2048b852009-11-23 18:04:58 +00001030 SDB->SwitchCases[i].TrueBB = SDB->SwitchCases[i].FalseBB;
1031 SDB->SwitchCases[i].FalseBB = 0;
Nate Begemanf15485a2006-03-27 01:32:24 +00001032 }
Dan Gohman2048b852009-11-23 18:04:58 +00001033 assert(SDB->SwitchCases[i].TrueBB == 0 && SDB->SwitchCases[i].FalseBB == 0);
1034 SDB->clear();
Chris Lattnera33ef482005-03-30 01:10:47 +00001035 }
Dan Gohman2048b852009-11-23 18:04:58 +00001036 SDB->SwitchCases.clear();
Chris Lattner1c08c712005-01-07 07:47:53 +00001037}
Evan Chenga9c20912006-01-21 02:32:06 +00001038
Jim Laskey13ec7022006-08-01 14:21:23 +00001039
Dan Gohman0a3776d2009-02-06 18:26:51 +00001040/// Create the scheduler. If a specific scheduler was specified
1041/// via the SchedulerRegistry, use it, otherwise select the
1042/// one preferred by the target.
Dan Gohman5e843682008-07-14 18:19:29 +00001043///
Dan Gohman47ac0f02009-02-11 04:27:20 +00001044ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
Jim Laskeyeb577ba2006-08-02 12:30:23 +00001045 RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001046
Jim Laskey13ec7022006-08-01 14:21:23 +00001047 if (!Ctor) {
Jim Laskeyeb577ba2006-08-02 12:30:23 +00001048 Ctor = ISHeuristic;
Jim Laskey9373beb2006-08-01 19:14:14 +00001049 RegisterScheduler::setDefault(Ctor);
Evan Cheng4ef10862006-01-23 07:01:07 +00001050 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001051
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001052 return Ctor(this, OptLevel);
Evan Chenga9c20912006-01-21 02:32:06 +00001053}
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001054
Dan Gohmanfc54c552009-01-15 22:18:12 +00001055ScheduleHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
1056 return new ScheduleHazardRecognizer();
Jim Laskey9ff542f2006-08-01 18:29:48 +00001057}
1058
Chris Lattner75548062006-10-11 03:58:02 +00001059//===----------------------------------------------------------------------===//
1060// Helper functions used by the generated instruction selector.
1061//===----------------------------------------------------------------------===//
1062// Calls to these methods are generated by tblgen.
1063
1064/// CheckAndMask - The isel is trying to match something like (and X, 255). If
1065/// the dag combiner simplified the 255, we still want to match. RHS is the
1066/// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
1067/// specified in the .td file (e.g. 255).
Daniel Dunbara279bc32009-09-20 02:20:51 +00001068bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmandc9b3d02007-07-24 23:00:27 +00001069 int64_t DesiredMaskS) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001070 const APInt &ActualMask = RHS->getAPIntValue();
1071 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001072
Chris Lattner75548062006-10-11 03:58:02 +00001073 // If the actual mask exactly matches, success!
1074 if (ActualMask == DesiredMask)
1075 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001076
Chris Lattner75548062006-10-11 03:58:02 +00001077 // If the actual AND mask is allowing unallowed bits, this doesn't match.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001078 if (ActualMask.intersects(~DesiredMask))
Chris Lattner75548062006-10-11 03:58:02 +00001079 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001080
Chris Lattner75548062006-10-11 03:58:02 +00001081 // Otherwise, the DAG Combiner may have proven that the value coming in is
1082 // either already zero or is not demanded. Check for known zero input bits.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001083 APInt NeededMask = DesiredMask & ~ActualMask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001084 if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
Chris Lattner75548062006-10-11 03:58:02 +00001085 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001086
Chris Lattner75548062006-10-11 03:58:02 +00001087 // TODO: check to see if missing bits are just not demanded.
1088
1089 // Otherwise, this pattern doesn't match.
1090 return false;
1091}
1092
1093/// CheckOrMask - The isel is trying to match something like (or X, 255). If
1094/// the dag combiner simplified the 255, we still want to match. RHS is the
1095/// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
1096/// specified in the .td file (e.g. 255).
Daniel Dunbara279bc32009-09-20 02:20:51 +00001097bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001098 int64_t DesiredMaskS) const {
1099 const APInt &ActualMask = RHS->getAPIntValue();
1100 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001101
Chris Lattner75548062006-10-11 03:58:02 +00001102 // If the actual mask exactly matches, success!
1103 if (ActualMask == DesiredMask)
1104 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001105
Chris Lattner75548062006-10-11 03:58:02 +00001106 // If the actual AND mask is allowing unallowed bits, this doesn't match.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001107 if (ActualMask.intersects(~DesiredMask))
Chris Lattner75548062006-10-11 03:58:02 +00001108 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001109
Chris Lattner75548062006-10-11 03:58:02 +00001110 // Otherwise, the DAG Combiner may have proven that the value coming in is
1111 // either already zero or is not demanded. Check for known zero input bits.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001112 APInt NeededMask = DesiredMask & ~ActualMask;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001113
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001114 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001115 CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001116
Chris Lattner75548062006-10-11 03:58:02 +00001117 // If all the missing bits in the or are already known to be set, match!
1118 if ((NeededMask & KnownOne) == NeededMask)
1119 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001120
Chris Lattner75548062006-10-11 03:58:02 +00001121 // TODO: check to see if missing bits are just not demanded.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001122
Chris Lattner75548062006-10-11 03:58:02 +00001123 // Otherwise, this pattern doesn't match.
1124 return false;
1125}
1126
Jim Laskey9ff542f2006-08-01 18:29:48 +00001127
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001128/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
1129/// by tblgen. Others should not call it.
1130void SelectionDAGISel::
Dan Gohmanf350b272008-08-23 02:25:05 +00001131SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +00001132 std::vector<SDValue> InOps;
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001133 std::swap(InOps, Ops);
1134
Chris Lattnerdecc2672010-04-07 05:20:54 +00001135 Ops.push_back(InOps[InlineAsm::Op_InputChain]); // 0
1136 Ops.push_back(InOps[InlineAsm::Op_AsmString]); // 1
1137 Ops.push_back(InOps[InlineAsm::Op_MDNode]); // 2, !srcloc
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001138
Chris Lattnerdecc2672010-04-07 05:20:54 +00001139 unsigned i = InlineAsm::Op_FirstOperand, e = InOps.size();
Owen Anderson825b72b2009-08-11 20:47:22 +00001140 if (InOps[e-1].getValueType() == MVT::Flag)
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001141 --e; // Don't process a flag operand if it is here.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001142
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001143 while (i != e) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001144 unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00001145 if (!InlineAsm::isMemKind(Flags)) {
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001146 // Just skip over this operand, copying the operands verbatim.
Evan Cheng697cbbf2009-03-20 18:03:34 +00001147 Ops.insert(Ops.end(), InOps.begin()+i,
1148 InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
1149 i += InlineAsm::getNumOperandRegisters(Flags) + 1;
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001150 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00001151 assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
1152 "Memory operand with multiple values?");
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001153 // Otherwise, this is a memory operand. Ask the target to select it.
Dan Gohman475871a2008-07-27 21:46:04 +00001154 std::vector<SDValue> SelOps;
Chris Lattnerdecc2672010-04-07 05:20:54 +00001155 if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps))
Chris Lattner75361b62010-04-07 22:58:41 +00001156 report_fatal_error("Could not match memory address. Inline asm"
Chris Lattner87d677c2010-04-07 23:50:38 +00001157 " failure!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001158
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001159 // Add this to the output node.
Chris Lattnerdecc2672010-04-07 05:20:54 +00001160 unsigned NewFlags =
1161 InlineAsm::getFlagWord(InlineAsm::Kind_Mem, SelOps.size());
1162 Ops.push_back(CurDAG->getTargetConstant(NewFlags, MVT::i32));
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001163 Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
1164 i += 2;
1165 }
1166 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001167
Chris Lattner0e43f2b2006-02-24 02:13:54 +00001168 // Add the flag input back if present.
1169 if (e != InOps.size())
1170 Ops.push_back(InOps.back());
1171}
Devang Patel794fd752007-05-01 21:15:47 +00001172
Owen Andersone50ed302009-08-10 22:56:29 +00001173/// findFlagUse - Return use of EVT::Flag value produced by the specified
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001174/// SDNode.
1175///
1176static SDNode *findFlagUse(SDNode *N) {
1177 unsigned FlagResNo = N->getNumValues()-1;
1178 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
1179 SDUse &Use = I.getUse();
1180 if (Use.getResNo() == FlagResNo)
1181 return Use.getUser();
1182 }
1183 return NULL;
1184}
1185
1186/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
1187/// This function recursively traverses up the operand chain, ignoring
1188/// certain nodes.
1189static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Chris Lattnerd1b73822010-03-02 22:20:06 +00001190 SDNode *Root, SmallPtrSet<SDNode*, 16> &Visited,
1191 bool IgnoreChains) {
Chris Lattnerda244a02010-02-23 19:32:27 +00001192 // The NodeID's are given uniques ID's where a node ID is guaranteed to be
1193 // greater than all of its (recursive) operands. If we scan to a point where
1194 // 'use' is smaller than the node we're scanning for, then we know we will
1195 // never find it.
1196 //
1197 // The Use may be -1 (unassigned) if it is a newly allocated node. This can
1198 // happen because we scan down to newly selected nodes in the case of flag
1199 // uses.
1200 if ((Use->getNodeId() < Def->getNodeId() && Use->getNodeId() != -1))
1201 return false;
1202
1203 // Don't revisit nodes if we already scanned it and didn't fail, we know we
1204 // won't fail if we scan it again.
1205 if (!Visited.insert(Use))
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001206 return false;
1207
1208 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001209 // Ignore chain uses, they are validated by HandleMergeInputChains.
1210 if (Use->getOperand(i).getValueType() == MVT::Other && IgnoreChains)
1211 continue;
1212
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001213 SDNode *N = Use->getOperand(i).getNode();
1214 if (N == Def) {
1215 if (Use == ImmedUse || Use == Root)
1216 continue; // We are not looking for immediate use.
1217 assert(N != Root);
1218 return true;
1219 }
1220
1221 // Traverse up the operand chain.
Chris Lattnerd1b73822010-03-02 22:20:06 +00001222 if (findNonImmUse(N, Def, ImmedUse, Root, Visited, IgnoreChains))
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001223 return true;
1224 }
1225 return false;
1226}
1227
Evan Cheng014bf212010-02-15 19:41:07 +00001228/// IsProfitableToFold - Returns true if it's profitable to fold the specific
1229/// operand node N of U during instruction selection that starts at Root.
1230bool SelectionDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
1231 SDNode *Root) const {
1232 if (OptLevel == CodeGenOpt::None) return false;
1233 return N.hasOneUse();
1234}
1235
1236/// IsLegalToFold - Returns true if the specific operand node N of
1237/// U can be folded during instruction selection that starts at Root.
Chris Lattnerd1b73822010-03-02 22:20:06 +00001238bool SelectionDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
Dan Gohmand858e902010-04-17 15:26:15 +00001239 CodeGenOpt::Level OptLevel,
1240 bool IgnoreChains) {
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001241 if (OptLevel == CodeGenOpt::None) return false;
1242
1243 // If Root use can somehow reach N through a path that that doesn't contain
1244 // U then folding N would create a cycle. e.g. In the following
1245 // diagram, Root can reach N through X. If N is folded into into Root, then
1246 // X is both a predecessor and a successor of U.
1247 //
1248 // [N*] //
1249 // ^ ^ //
1250 // / \ //
1251 // [U*] [X]? //
1252 // ^ ^ //
1253 // \ / //
1254 // \ / //
1255 // [Root*] //
1256 //
1257 // * indicates nodes to be folded together.
1258 //
1259 // If Root produces a flag, then it gets (even more) interesting. Since it
1260 // will be "glued" together with its flag use in the scheduler, we need to
1261 // check if it might reach N.
1262 //
1263 // [N*] //
1264 // ^ ^ //
1265 // / \ //
1266 // [U*] [X]? //
1267 // ^ ^ //
1268 // \ \ //
1269 // \ | //
1270 // [Root*] | //
1271 // ^ | //
1272 // f | //
1273 // | / //
1274 // [Y] / //
1275 // ^ / //
1276 // f / //
1277 // | / //
1278 // [FU] //
1279 //
1280 // If FU (flag use) indirectly reaches N (the load), and Root folds N
1281 // (call it Fold), then X is a predecessor of FU and a successor of
1282 // Fold. But since Fold and FU are flagged together, this will create
1283 // a cycle in the scheduling graph.
1284
Chris Lattner9bbcd5e2010-03-05 05:49:45 +00001285 // If the node has flags, walk down the graph to the "lowest" node in the
1286 // flagged set.
Owen Andersone50ed302009-08-10 22:56:29 +00001287 EVT VT = Root->getValueType(Root->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +00001288 while (VT == MVT::Flag) {
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001289 SDNode *FU = findFlagUse(Root);
1290 if (FU == NULL)
1291 break;
1292 Root = FU;
1293 VT = Root->getValueType(Root->getNumValues()-1);
Chris Lattner18fdaba2010-03-05 06:19:13 +00001294
1295 // If our query node has a flag result with a use, we've walked up it. If
1296 // the user (which has already been selected) has a chain or indirectly uses
1297 // the chain, our WalkChainUsers predicate will not consider it. Because of
1298 // this, we cannot ignore chains in this predicate.
1299 IgnoreChains = false;
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001300 }
Chris Lattner18fdaba2010-03-05 06:19:13 +00001301
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001302
Chris Lattner9bbcd5e2010-03-05 05:49:45 +00001303 SmallPtrSet<SDNode*, 16> Visited;
1304 return !findNonImmUse(Root, N.getNode(), U, Root, Visited, IgnoreChains);
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +00001305}
1306
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001307SDNode *SelectionDAGISel::Select_INLINEASM(SDNode *N) {
1308 std::vector<SDValue> Ops(N->op_begin(), N->op_end());
Dan Gohmane1f188f2009-10-29 22:30:23 +00001309 SelectInlineAsmMemoryOperands(Ops);
1310
1311 std::vector<EVT> VTs;
1312 VTs.push_back(MVT::Other);
1313 VTs.push_back(MVT::Flag);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001314 SDValue New = CurDAG->getNode(ISD::INLINEASM, N->getDebugLoc(),
Dan Gohmane1f188f2009-10-29 22:30:23 +00001315 VTs, &Ops[0], Ops.size());
Chris Lattnerd1b73822010-03-02 22:20:06 +00001316 New->setNodeId(-1);
Dan Gohmane1f188f2009-10-29 22:30:23 +00001317 return New.getNode();
1318}
1319
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001320SDNode *SelectionDAGISel::Select_UNDEF(SDNode *N) {
Chris Lattner518bb532010-02-09 19:54:29 +00001321 return CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF,N->getValueType(0));
Dan Gohmane1f188f2009-10-29 22:30:23 +00001322}
1323
Chris Lattner2a49d572010-02-28 22:37:22 +00001324/// GetVBR - decode a vbr encoding whose top bit is set.
1325ALWAYS_INLINE static uint64_t
1326GetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) {
1327 assert(Val >= 128 && "Not a VBR");
1328 Val &= 127; // Remove first vbr bit.
1329
1330 unsigned Shift = 7;
1331 uint64_t NextBits;
1332 do {
Chris Lattner14df8dc2010-02-28 22:38:43 +00001333 NextBits = MatcherTable[Idx++];
Chris Lattner2a49d572010-02-28 22:37:22 +00001334 Val |= (NextBits&127) << Shift;
1335 Shift += 7;
1336 } while (NextBits & 128);
1337
1338 return Val;
1339}
1340
Chris Lattner2a49d572010-02-28 22:37:22 +00001341
1342/// UpdateChainsAndFlags - When a match is complete, this method updates uses of
1343/// interior flag and chain results to use the new flag and chain results.
Chris Lattner82dd3d32010-03-02 07:50:03 +00001344void SelectionDAGISel::
1345UpdateChainsAndFlags(SDNode *NodeToMatch, SDValue InputChain,
1346 const SmallVectorImpl<SDNode*> &ChainNodesMatched,
1347 SDValue InputFlag,
1348 const SmallVectorImpl<SDNode*> &FlagResultNodesMatched,
1349 bool isMorphNodeTo) {
1350 SmallVector<SDNode*, 4> NowDeadNodes;
1351
1352 ISelUpdater ISU(ISelPosition);
1353
Chris Lattner2a49d572010-02-28 22:37:22 +00001354 // Now that all the normal results are replaced, we replace the chain and
1355 // flag results if present.
1356 if (!ChainNodesMatched.empty()) {
1357 assert(InputChain.getNode() != 0 &&
1358 "Matched input chains but didn't produce a chain");
1359 // Loop over all of the nodes we matched that produced a chain result.
1360 // Replace all the chain results with the final chain we ended up with.
1361 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
1362 SDNode *ChainNode = ChainNodesMatched[i];
1363
Chris Lattner82dd3d32010-03-02 07:50:03 +00001364 // If this node was already deleted, don't look at it.
1365 if (ChainNode->getOpcode() == ISD::DELETED_NODE)
1366 continue;
1367
Chris Lattner2a49d572010-02-28 22:37:22 +00001368 // Don't replace the results of the root node if we're doing a
1369 // MorphNodeTo.
1370 if (ChainNode == NodeToMatch && isMorphNodeTo)
1371 continue;
1372
1373 SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1);
1374 if (ChainVal.getValueType() == MVT::Flag)
1375 ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2);
1376 assert(ChainVal.getValueType() == MVT::Other && "Not a chain?");
Chris Lattner82dd3d32010-03-02 07:50:03 +00001377 CurDAG->ReplaceAllUsesOfValueWith(ChainVal, InputChain, &ISU);
1378
Chris Lattner856fb392010-03-28 05:28:31 +00001379 // If the node became dead and we haven't already seen it, delete it.
1380 if (ChainNode->use_empty() &&
1381 !std::count(NowDeadNodes.begin(), NowDeadNodes.end(), ChainNode))
Chris Lattner82dd3d32010-03-02 07:50:03 +00001382 NowDeadNodes.push_back(ChainNode);
Chris Lattner2a49d572010-02-28 22:37:22 +00001383 }
1384 }
1385
1386 // If the result produces a flag, update any flag results in the matched
1387 // pattern with the flag result.
1388 if (InputFlag.getNode() != 0) {
1389 // Handle any interior nodes explicitly marked.
1390 for (unsigned i = 0, e = FlagResultNodesMatched.size(); i != e; ++i) {
1391 SDNode *FRN = FlagResultNodesMatched[i];
Chris Lattner82dd3d32010-03-02 07:50:03 +00001392
1393 // If this node was already deleted, don't look at it.
1394 if (FRN->getOpcode() == ISD::DELETED_NODE)
1395 continue;
1396
Chris Lattner2a49d572010-02-28 22:37:22 +00001397 assert(FRN->getValueType(FRN->getNumValues()-1) == MVT::Flag &&
1398 "Doesn't have a flag result");
1399 CurDAG->ReplaceAllUsesOfValueWith(SDValue(FRN, FRN->getNumValues()-1),
Chris Lattner82dd3d32010-03-02 07:50:03 +00001400 InputFlag, &ISU);
1401
Chris Lattner19e37cb2010-03-28 04:54:33 +00001402 // If the node became dead and we haven't already seen it, delete it.
1403 if (FRN->use_empty() &&
1404 !std::count(NowDeadNodes.begin(), NowDeadNodes.end(), FRN))
Chris Lattner82dd3d32010-03-02 07:50:03 +00001405 NowDeadNodes.push_back(FRN);
Chris Lattner2a49d572010-02-28 22:37:22 +00001406 }
1407 }
1408
Chris Lattner82dd3d32010-03-02 07:50:03 +00001409 if (!NowDeadNodes.empty())
1410 CurDAG->RemoveDeadNodes(NowDeadNodes, &ISU);
1411
Chris Lattner2a49d572010-02-28 22:37:22 +00001412 DEBUG(errs() << "ISEL: Match complete!\n");
1413}
1414
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001415enum ChainResult {
1416 CR_Simple,
1417 CR_InducesCycle,
1418 CR_LeadsToInteriorNode
1419};
1420
1421/// WalkChainUsers - Walk down the users of the specified chained node that is
1422/// part of the pattern we're matching, looking at all of the users we find.
1423/// This determines whether something is an interior node, whether we have a
1424/// non-pattern node in between two pattern nodes (which prevent folding because
1425/// it would induce a cycle) and whether we have a TokenFactor node sandwiched
1426/// between pattern nodes (in which case the TF becomes part of the pattern).
1427///
1428/// The walk we do here is guaranteed to be small because we quickly get down to
1429/// already selected nodes "below" us.
1430static ChainResult
1431WalkChainUsers(SDNode *ChainedNode,
1432 SmallVectorImpl<SDNode*> &ChainedNodesInPattern,
1433 SmallVectorImpl<SDNode*> &InteriorChainedNodes) {
1434 ChainResult Result = CR_Simple;
1435
1436 for (SDNode::use_iterator UI = ChainedNode->use_begin(),
1437 E = ChainedNode->use_end(); UI != E; ++UI) {
1438 // Make sure the use is of the chain, not some other value we produce.
1439 if (UI.getUse().getValueType() != MVT::Other) continue;
1440
1441 SDNode *User = *UI;
1442
1443 // If we see an already-selected machine node, then we've gone beyond the
1444 // pattern that we're selecting down into the already selected chunk of the
1445 // DAG.
1446 if (User->isMachineOpcode() ||
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001447 User->getOpcode() == ISD::HANDLENODE) // Root of the graph.
1448 continue;
Chris Lattnerd1b73822010-03-02 22:20:06 +00001449
1450 if (User->getOpcode() == ISD::CopyToReg ||
1451 User->getOpcode() == ISD::CopyFromReg ||
Chris Lattner7561d482010-03-14 02:33:54 +00001452 User->getOpcode() == ISD::INLINEASM ||
1453 User->getOpcode() == ISD::EH_LABEL) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001454 // If their node ID got reset to -1 then they've already been selected.
1455 // Treat them like a MachineOpcode.
1456 if (User->getNodeId() == -1)
1457 continue;
1458 }
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001459
1460 // If we have a TokenFactor, we handle it specially.
1461 if (User->getOpcode() != ISD::TokenFactor) {
1462 // If the node isn't a token factor and isn't part of our pattern, then it
1463 // must be a random chained node in between two nodes we're selecting.
1464 // This happens when we have something like:
1465 // x = load ptr
1466 // call
1467 // y = x+4
1468 // store y -> ptr
1469 // Because we structurally match the load/store as a read/modify/write,
1470 // but the call is chained between them. We cannot fold in this case
1471 // because it would induce a cycle in the graph.
1472 if (!std::count(ChainedNodesInPattern.begin(),
1473 ChainedNodesInPattern.end(), User))
1474 return CR_InducesCycle;
1475
1476 // Otherwise we found a node that is part of our pattern. For example in:
1477 // x = load ptr
1478 // y = x+4
1479 // store y -> ptr
1480 // This would happen when we're scanning down from the load and see the
1481 // store as a user. Record that there is a use of ChainedNode that is
1482 // part of the pattern and keep scanning uses.
1483 Result = CR_LeadsToInteriorNode;
1484 InteriorChainedNodes.push_back(User);
1485 continue;
1486 }
1487
1488 // If we found a TokenFactor, there are two cases to consider: first if the
1489 // TokenFactor is just hanging "below" the pattern we're matching (i.e. no
1490 // uses of the TF are in our pattern) we just want to ignore it. Second,
1491 // the TokenFactor can be sandwiched in between two chained nodes, like so:
1492 // [Load chain]
1493 // ^
1494 // |
1495 // [Load]
1496 // ^ ^
1497 // | \ DAG's like cheese
1498 // / \ do you?
1499 // / |
1500 // [TokenFactor] [Op]
1501 // ^ ^
1502 // | |
1503 // \ /
1504 // \ /
1505 // [Store]
1506 //
1507 // In this case, the TokenFactor becomes part of our match and we rewrite it
1508 // as a new TokenFactor.
1509 //
1510 // To distinguish these two cases, do a recursive walk down the uses.
1511 switch (WalkChainUsers(User, ChainedNodesInPattern, InteriorChainedNodes)) {
1512 case CR_Simple:
1513 // If the uses of the TokenFactor are just already-selected nodes, ignore
1514 // it, it is "below" our pattern.
1515 continue;
1516 case CR_InducesCycle:
1517 // If the uses of the TokenFactor lead to nodes that are not part of our
1518 // pattern that are not selected, folding would turn this into a cycle,
1519 // bail out now.
1520 return CR_InducesCycle;
1521 case CR_LeadsToInteriorNode:
1522 break; // Otherwise, keep processing.
1523 }
1524
1525 // Okay, we know we're in the interesting interior case. The TokenFactor
1526 // is now going to be considered part of the pattern so that we rewrite its
1527 // uses (it may have uses that are not part of the pattern) with the
1528 // ultimate chain result of the generated code. We will also add its chain
1529 // inputs as inputs to the ultimate TokenFactor we create.
1530 Result = CR_LeadsToInteriorNode;
1531 ChainedNodesInPattern.push_back(User);
1532 InteriorChainedNodes.push_back(User);
1533 continue;
1534 }
1535
1536 return Result;
1537}
1538
Chris Lattner6b307922010-03-02 00:00:03 +00001539/// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains
Chris Lattner00592ec2010-03-02 19:34:59 +00001540/// operation for when the pattern matched at least one node with a chains. The
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001541/// input vector contains a list of all of the chained nodes that we match. We
1542/// must determine if this is a valid thing to cover (i.e. matching it won't
1543/// induce cycles in the DAG) and if so, creating a TokenFactor node. that will
1544/// be used as the input node chain for the generated nodes.
Chris Lattner6b307922010-03-02 00:00:03 +00001545static SDValue
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001546HandleMergeInputChains(SmallVectorImpl<SDNode*> &ChainNodesMatched,
Chris Lattner6b307922010-03-02 00:00:03 +00001547 SelectionDAG *CurDAG) {
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001548 // Walk all of the chained nodes we've matched, recursively scanning down the
1549 // users of the chain result. This adds any TokenFactor nodes that are caught
1550 // in between chained nodes to the chained and interior nodes list.
1551 SmallVector<SDNode*, 3> InteriorChainedNodes;
1552 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
1553 if (WalkChainUsers(ChainNodesMatched[i], ChainNodesMatched,
1554 InteriorChainedNodes) == CR_InducesCycle)
1555 return SDValue(); // Would induce a cycle.
1556 }
Chris Lattner6b307922010-03-02 00:00:03 +00001557
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001558 // Okay, we have walked all the matched nodes and collected TokenFactor nodes
1559 // that we are interested in. Form our input TokenFactor node.
Chris Lattner6b307922010-03-02 00:00:03 +00001560 SmallVector<SDValue, 3> InputChains;
1561 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001562 // Add the input chain of this node to the InputChains list (which will be
1563 // the operands of the generated TokenFactor) if it's not an interior node.
1564 SDNode *N = ChainNodesMatched[i];
1565 if (N->getOpcode() != ISD::TokenFactor) {
1566 if (std::count(InteriorChainedNodes.begin(),InteriorChainedNodes.end(),N))
1567 continue;
1568
1569 // Otherwise, add the input chain.
1570 SDValue InChain = ChainNodesMatched[i]->getOperand(0);
1571 assert(InChain.getValueType() == MVT::Other && "Not a chain");
Chris Lattner6b307922010-03-02 00:00:03 +00001572 InputChains.push_back(InChain);
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001573 continue;
1574 }
1575
1576 // If we have a token factor, we want to add all inputs of the token factor
1577 // that are not part of the pattern we're matching.
1578 for (unsigned op = 0, e = N->getNumOperands(); op != e; ++op) {
1579 if (!std::count(ChainNodesMatched.begin(), ChainNodesMatched.end(),
Chris Lattner6183fbd2010-03-02 02:37:23 +00001580 N->getOperand(op).getNode()))
1581 InputChains.push_back(N->getOperand(op));
Chris Lattnerc6d7ad32010-03-02 02:22:10 +00001582 }
Chris Lattner6b307922010-03-02 00:00:03 +00001583 }
1584
1585 SDValue Res;
1586 if (InputChains.size() == 1)
1587 return InputChains[0];
1588 return CurDAG->getNode(ISD::TokenFactor, ChainNodesMatched[0]->getDebugLoc(),
1589 MVT::Other, &InputChains[0], InputChains.size());
1590}
Chris Lattner2a49d572010-02-28 22:37:22 +00001591
Chris Lattner3ff1e4d2010-03-02 06:55:04 +00001592/// MorphNode - Handle morphing a node in place for the selector.
1593SDNode *SelectionDAGISel::
1594MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
1595 const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo) {
1596 // It is possible we're using MorphNodeTo to replace a node with no
1597 // normal results with one that has a normal result (or we could be
1598 // adding a chain) and the input could have flags and chains as well.
Chris Lattnercaa88702010-03-28 08:43:23 +00001599 // In this case we need to shift the operands down.
Chris Lattner3ff1e4d2010-03-02 06:55:04 +00001600 // FIXME: This is a horrible hack and broken in obscure cases, no worse
Chris Lattnera6f86932010-03-27 18:54:50 +00001601 // than the old isel though.
Chris Lattner3ff1e4d2010-03-02 06:55:04 +00001602 int OldFlagResultNo = -1, OldChainResultNo = -1;
1603
1604 unsigned NTMNumResults = Node->getNumValues();
1605 if (Node->getValueType(NTMNumResults-1) == MVT::Flag) {
1606 OldFlagResultNo = NTMNumResults-1;
1607 if (NTMNumResults != 1 &&
1608 Node->getValueType(NTMNumResults-2) == MVT::Other)
1609 OldChainResultNo = NTMNumResults-2;
1610 } else if (Node->getValueType(NTMNumResults-1) == MVT::Other)
1611 OldChainResultNo = NTMNumResults-1;
1612
Chris Lattner61c97f62010-03-02 07:14:49 +00001613 // Call the underlying SelectionDAG routine to do the transmogrification. Note
1614 // that this deletes operands of the old node that become dead.
Chris Lattner3ff1e4d2010-03-02 06:55:04 +00001615 SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops, NumOps);
1616
1617 // MorphNodeTo can operate in two ways: if an existing node with the
1618 // specified operands exists, it can just return it. Otherwise, it
1619 // updates the node in place to have the requested operands.
1620 if (Res == Node) {
1621 // If we updated the node in place, reset the node ID. To the isel,
1622 // this should be just like a newly allocated machine node.
1623 Res->setNodeId(-1);
1624 }
1625
1626 unsigned ResNumResults = Res->getNumValues();
1627 // Move the flag if needed.
1628 if ((EmitNodeInfo & OPFL_FlagOutput) && OldFlagResultNo != -1 &&
1629 (unsigned)OldFlagResultNo != ResNumResults-1)
1630 CurDAG->ReplaceAllUsesOfValueWith(SDValue(Node, OldFlagResultNo),
1631 SDValue(Res, ResNumResults-1));
1632
1633 if ((EmitNodeInfo & OPFL_FlagOutput) != 0)
1634 --ResNumResults;
1635
1636 // Move the chain reference if needed.
1637 if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 &&
1638 (unsigned)OldChainResultNo != ResNumResults-1)
1639 CurDAG->ReplaceAllUsesOfValueWith(SDValue(Node, OldChainResultNo),
1640 SDValue(Res, ResNumResults-1));
1641
1642 // Otherwise, no replacement happened because the node already exists. Replace
1643 // Uses of the old node with the new one.
1644 if (Res != Node)
1645 CurDAG->ReplaceAllUsesWith(Node, Res);
1646
1647 return Res;
1648}
1649
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001650/// CheckPatternPredicate - Implements OP_CheckPatternPredicate.
1651ALWAYS_INLINE static bool
Chris Lattnerda828e32010-03-03 07:46:25 +00001652CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1653 SDValue N, const SmallVectorImpl<SDValue> &RecordedNodes) {
1654 // Accept if it is exactly the same as a previously recorded node.
1655 unsigned RecNo = MatcherTable[MatcherIndex++];
1656 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
1657 return N == RecordedNodes[RecNo];
1658}
1659
1660/// CheckPatternPredicate - Implements OP_CheckPatternPredicate.
1661ALWAYS_INLINE static bool
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001662CheckPatternPredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1663 SelectionDAGISel &SDISel) {
1664 return SDISel.CheckPatternPredicate(MatcherTable[MatcherIndex++]);
1665}
1666
1667/// CheckNodePredicate - Implements OP_CheckNodePredicate.
1668ALWAYS_INLINE static bool
1669CheckNodePredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1670 SelectionDAGISel &SDISel, SDNode *N) {
1671 return SDISel.CheckNodePredicate(N, MatcherTable[MatcherIndex++]);
1672}
1673
1674ALWAYS_INLINE static bool
1675CheckOpcode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1676 SDNode *N) {
Chris Lattnerd5d5a3d2010-03-25 06:33:05 +00001677 uint16_t Opc = MatcherTable[MatcherIndex++];
1678 Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
1679 return N->getOpcode() == Opc;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001680}
1681
1682ALWAYS_INLINE static bool
1683CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1684 SDValue N, const TargetLowering &TLI) {
1685 MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
1686 if (N.getValueType() == VT) return true;
1687
1688 // Handle the case when VT is iPTR.
1689 return VT == MVT::iPTR && N.getValueType() == TLI.getPointerTy();
1690}
1691
1692ALWAYS_INLINE static bool
Chris Lattnerda828e32010-03-03 07:46:25 +00001693CheckChildType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1694 SDValue N, const TargetLowering &TLI,
1695 unsigned ChildNo) {
1696 if (ChildNo >= N.getNumOperands())
1697 return false; // Match fails if out of range child #.
1698 return ::CheckType(MatcherTable, MatcherIndex, N.getOperand(ChildNo), TLI);
1699}
1700
1701
1702ALWAYS_INLINE static bool
1703CheckCondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1704 SDValue N) {
1705 return cast<CondCodeSDNode>(N)->get() ==
1706 (ISD::CondCode)MatcherTable[MatcherIndex++];
1707}
1708
1709ALWAYS_INLINE static bool
1710CheckValueType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1711 SDValue N, const TargetLowering &TLI) {
1712 MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
1713 if (cast<VTSDNode>(N)->getVT() == VT)
1714 return true;
1715
1716 // Handle the case when VT is iPTR.
1717 return VT == MVT::iPTR && cast<VTSDNode>(N)->getVT() == TLI.getPointerTy();
1718}
1719
1720ALWAYS_INLINE static bool
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001721CheckInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1722 SDValue N) {
1723 int64_t Val = MatcherTable[MatcherIndex++];
1724 if (Val & 128)
1725 Val = GetVBR(Val, MatcherTable, MatcherIndex);
1726
1727 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
1728 return C != 0 && C->getSExtValue() == Val;
1729}
1730
Chris Lattnerda828e32010-03-03 07:46:25 +00001731ALWAYS_INLINE static bool
1732CheckAndImm(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1733 SDValue N, SelectionDAGISel &SDISel) {
1734 int64_t Val = MatcherTable[MatcherIndex++];
1735 if (Val & 128)
1736 Val = GetVBR(Val, MatcherTable, MatcherIndex);
1737
1738 if (N->getOpcode() != ISD::AND) return false;
1739
1740 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1741 return C != 0 && SDISel.CheckAndMask(N.getOperand(0), C, Val);
1742}
1743
1744ALWAYS_INLINE static bool
1745CheckOrImm(const unsigned char *MatcherTable, unsigned &MatcherIndex,
1746 SDValue N, SelectionDAGISel &SDISel) {
1747 int64_t Val = MatcherTable[MatcherIndex++];
1748 if (Val & 128)
1749 Val = GetVBR(Val, MatcherTable, MatcherIndex);
1750
1751 if (N->getOpcode() != ISD::OR) return false;
1752
1753 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1754 return C != 0 && SDISel.CheckOrMask(N.getOperand(0), C, Val);
1755}
1756
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001757/// IsPredicateKnownToFail - If we know how and can do so without pushing a
1758/// scope, evaluate the current node. If the current predicate is known to
1759/// fail, set Result=true and return anything. If the current predicate is
1760/// known to pass, set Result=false and return the MatcherIndex to continue
1761/// with. If the current predicate is unknown, set Result=false and return the
1762/// MatcherIndex to continue with.
1763static unsigned IsPredicateKnownToFail(const unsigned char *Table,
1764 unsigned Index, SDValue N,
Chris Lattnerda828e32010-03-03 07:46:25 +00001765 bool &Result, SelectionDAGISel &SDISel,
1766 SmallVectorImpl<SDValue> &RecordedNodes){
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001767 switch (Table[Index++]) {
1768 default:
1769 Result = false;
1770 return Index-1; // Could not evaluate this predicate.
Chris Lattnerda828e32010-03-03 07:46:25 +00001771 case SelectionDAGISel::OPC_CheckSame:
1772 Result = !::CheckSame(Table, Index, N, RecordedNodes);
1773 return Index;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001774 case SelectionDAGISel::OPC_CheckPatternPredicate:
Chris Lattnerda828e32010-03-03 07:46:25 +00001775 Result = !::CheckPatternPredicate(Table, Index, SDISel);
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001776 return Index;
1777 case SelectionDAGISel::OPC_CheckPredicate:
Chris Lattnerda828e32010-03-03 07:46:25 +00001778 Result = !::CheckNodePredicate(Table, Index, SDISel, N.getNode());
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001779 return Index;
1780 case SelectionDAGISel::OPC_CheckOpcode:
1781 Result = !::CheckOpcode(Table, Index, N.getNode());
1782 return Index;
1783 case SelectionDAGISel::OPC_CheckType:
1784 Result = !::CheckType(Table, Index, N, SDISel.TLI);
1785 return Index;
1786 case SelectionDAGISel::OPC_CheckChild0Type:
1787 case SelectionDAGISel::OPC_CheckChild1Type:
1788 case SelectionDAGISel::OPC_CheckChild2Type:
1789 case SelectionDAGISel::OPC_CheckChild3Type:
1790 case SelectionDAGISel::OPC_CheckChild4Type:
1791 case SelectionDAGISel::OPC_CheckChild5Type:
1792 case SelectionDAGISel::OPC_CheckChild6Type:
Chris Lattnerda828e32010-03-03 07:46:25 +00001793 case SelectionDAGISel::OPC_CheckChild7Type:
1794 Result = !::CheckChildType(Table, Index, N, SDISel.TLI,
1795 Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Type);
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001796 return Index;
Chris Lattnerda828e32010-03-03 07:46:25 +00001797 case SelectionDAGISel::OPC_CheckCondCode:
1798 Result = !::CheckCondCode(Table, Index, N);
1799 return Index;
1800 case SelectionDAGISel::OPC_CheckValueType:
1801 Result = !::CheckValueType(Table, Index, N, SDISel.TLI);
1802 return Index;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001803 case SelectionDAGISel::OPC_CheckInteger:
1804 Result = !::CheckInteger(Table, Index, N);
1805 return Index;
Chris Lattnerda828e32010-03-03 07:46:25 +00001806 case SelectionDAGISel::OPC_CheckAndImm:
1807 Result = !::CheckAndImm(Table, Index, N, SDISel);
1808 return Index;
1809 case SelectionDAGISel::OPC_CheckOrImm:
1810 Result = !::CheckOrImm(Table, Index, N, SDISel);
1811 return Index;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001812 }
1813}
1814
Dan Gohmanb3579832010-04-15 17:08:50 +00001815namespace {
Chris Lattner3ff1e4d2010-03-02 06:55:04 +00001816
Chris Lattner2a49d572010-02-28 22:37:22 +00001817struct MatchScope {
1818 /// FailIndex - If this match fails, this is the index to continue with.
1819 unsigned FailIndex;
1820
1821 /// NodeStack - The node stack when the scope was formed.
1822 SmallVector<SDValue, 4> NodeStack;
1823
1824 /// NumRecordedNodes - The number of recorded nodes when the scope was formed.
1825 unsigned NumRecordedNodes;
1826
1827 /// NumMatchedMemRefs - The number of matched memref entries.
1828 unsigned NumMatchedMemRefs;
1829
1830 /// InputChain/InputFlag - The current chain/flag
1831 SDValue InputChain, InputFlag;
1832
1833 /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty.
1834 bool HasChainNodesMatched, HasFlagResultNodesMatched;
1835};
1836
Dan Gohmanb3579832010-04-15 17:08:50 +00001837}
1838
Chris Lattner2a49d572010-02-28 22:37:22 +00001839SDNode *SelectionDAGISel::
1840SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable,
1841 unsigned TableSize) {
1842 // FIXME: Should these even be selected? Handle these cases in the caller?
1843 switch (NodeToMatch->getOpcode()) {
1844 default:
1845 break;
1846 case ISD::EntryToken: // These nodes remain the same.
1847 case ISD::BasicBlock:
1848 case ISD::Register:
Chris Lattner3b9d6212010-03-14 17:53:23 +00001849 //case ISD::VALUETYPE:
1850 //case ISD::CONDCODE:
Chris Lattner2a49d572010-02-28 22:37:22 +00001851 case ISD::HANDLENODE:
Chris Lattnerdecc2672010-04-07 05:20:54 +00001852 case ISD::MDNODE_SDNODE:
Chris Lattner2a49d572010-02-28 22:37:22 +00001853 case ISD::TargetConstant:
1854 case ISD::TargetConstantFP:
1855 case ISD::TargetConstantPool:
1856 case ISD::TargetFrameIndex:
1857 case ISD::TargetExternalSymbol:
1858 case ISD::TargetBlockAddress:
1859 case ISD::TargetJumpTable:
1860 case ISD::TargetGlobalTLSAddress:
1861 case ISD::TargetGlobalAddress:
1862 case ISD::TokenFactor:
1863 case ISD::CopyFromReg:
1864 case ISD::CopyToReg:
Chris Lattner7561d482010-03-14 02:33:54 +00001865 case ISD::EH_LABEL:
Chris Lattnerd1b73822010-03-02 22:20:06 +00001866 NodeToMatch->setNodeId(-1); // Mark selected.
Chris Lattner2a49d572010-02-28 22:37:22 +00001867 return 0;
1868 case ISD::AssertSext:
1869 case ISD::AssertZext:
1870 CurDAG->ReplaceAllUsesOfValueWith(SDValue(NodeToMatch, 0),
1871 NodeToMatch->getOperand(0));
1872 return 0;
1873 case ISD::INLINEASM: return Select_INLINEASM(NodeToMatch);
Chris Lattner2a49d572010-02-28 22:37:22 +00001874 case ISD::UNDEF: return Select_UNDEF(NodeToMatch);
1875 }
1876
1877 assert(!NodeToMatch->isMachineOpcode() && "Node already selected!");
1878
1879 // Set up the node stack with NodeToMatch as the only node on the stack.
1880 SmallVector<SDValue, 8> NodeStack;
1881 SDValue N = SDValue(NodeToMatch, 0);
1882 NodeStack.push_back(N);
1883
1884 // MatchScopes - Scopes used when matching, if a match failure happens, this
1885 // indicates where to continue checking.
1886 SmallVector<MatchScope, 8> MatchScopes;
1887
1888 // RecordedNodes - This is the set of nodes that have been recorded by the
1889 // state machine.
1890 SmallVector<SDValue, 8> RecordedNodes;
1891
1892 // MatchedMemRefs - This is the set of MemRef's we've seen in the input
1893 // pattern.
1894 SmallVector<MachineMemOperand*, 2> MatchedMemRefs;
1895
1896 // These are the current input chain and flag for use when generating nodes.
1897 // Various Emit operations change these. For example, emitting a copytoreg
1898 // uses and updates these.
1899 SDValue InputChain, InputFlag;
1900
1901 // ChainNodesMatched - If a pattern matches nodes that have input/output
1902 // chains, the OPC_EmitMergeInputChains operation is emitted which indicates
1903 // which ones they are. The result is captured into this list so that we can
1904 // update the chain results when the pattern is complete.
1905 SmallVector<SDNode*, 3> ChainNodesMatched;
1906 SmallVector<SDNode*, 3> FlagResultNodesMatched;
1907
1908 DEBUG(errs() << "ISEL: Starting pattern match on root node: ";
1909 NodeToMatch->dump(CurDAG);
1910 errs() << '\n');
1911
Chris Lattner7390eeb2010-03-01 18:47:11 +00001912 // Determine where to start the interpreter. Normally we start at opcode #0,
1913 // but if the state machine starts with an OPC_SwitchOpcode, then we
1914 // accelerate the first lookup (which is guaranteed to be hot) with the
1915 // OpcodeOffset table.
Chris Lattner2a49d572010-02-28 22:37:22 +00001916 unsigned MatcherIndex = 0;
Chris Lattner7390eeb2010-03-01 18:47:11 +00001917
1918 if (!OpcodeOffset.empty()) {
1919 // Already computed the OpcodeOffset table, just index into it.
1920 if (N.getOpcode() < OpcodeOffset.size())
1921 MatcherIndex = OpcodeOffset[N.getOpcode()];
1922 DEBUG(errs() << " Initial Opcode index to " << MatcherIndex << "\n");
1923
1924 } else if (MatcherTable[0] == OPC_SwitchOpcode) {
1925 // Otherwise, the table isn't computed, but the state machine does start
1926 // with an OPC_SwitchOpcode instruction. Populate the table now, since this
1927 // is the first time we're selecting an instruction.
1928 unsigned Idx = 1;
1929 while (1) {
1930 // Get the size of this case.
1931 unsigned CaseSize = MatcherTable[Idx++];
1932 if (CaseSize & 128)
1933 CaseSize = GetVBR(CaseSize, MatcherTable, Idx);
1934 if (CaseSize == 0) break;
1935
1936 // Get the opcode, add the index to the table.
Chris Lattnerd5d5a3d2010-03-25 06:33:05 +00001937 uint16_t Opc = MatcherTable[Idx++];
1938 Opc |= (unsigned short)MatcherTable[Idx++] << 8;
Chris Lattner7390eeb2010-03-01 18:47:11 +00001939 if (Opc >= OpcodeOffset.size())
1940 OpcodeOffset.resize((Opc+1)*2);
1941 OpcodeOffset[Opc] = Idx;
1942 Idx += CaseSize;
1943 }
1944
1945 // Okay, do the lookup for the first opcode.
1946 if (N.getOpcode() < OpcodeOffset.size())
1947 MatcherIndex = OpcodeOffset[N.getOpcode()];
1948 }
1949
Chris Lattner2a49d572010-02-28 22:37:22 +00001950 while (1) {
1951 assert(MatcherIndex < TableSize && "Invalid index");
Dan Gohman19b38262010-03-09 02:15:05 +00001952#ifndef NDEBUG
1953 unsigned CurrentOpcodeIndex = MatcherIndex;
1954#endif
Chris Lattner2a49d572010-02-28 22:37:22 +00001955 BuiltinOpcodes Opcode = (BuiltinOpcodes)MatcherTable[MatcherIndex++];
1956 switch (Opcode) {
1957 case OPC_Scope: {
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001958 // Okay, the semantics of this operation are that we should push a scope
1959 // then evaluate the first child. However, pushing a scope only to have
1960 // the first check fail (which then pops it) is inefficient. If we can
1961 // determine immediately that the first check (or first several) will
1962 // immediately fail, don't even bother pushing a scope for them.
1963 unsigned FailIndex;
1964
1965 while (1) {
1966 unsigned NumToSkip = MatcherTable[MatcherIndex++];
1967 if (NumToSkip & 128)
1968 NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
1969 // Found the end of the scope with no match.
1970 if (NumToSkip == 0) {
1971 FailIndex = 0;
1972 break;
1973 }
1974
1975 FailIndex = MatcherIndex+NumToSkip;
1976
Chris Lattnera6f86932010-03-27 18:54:50 +00001977 unsigned MatcherIndexOfPredicate = MatcherIndex;
1978 (void)MatcherIndexOfPredicate; // silence warning.
1979
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001980 // If we can't evaluate this predicate without pushing a scope (e.g. if
1981 // it is a 'MoveParent') or if the predicate succeeds on this node, we
1982 // push the scope and evaluate the full predicate chain.
1983 bool Result;
1984 MatcherIndex = IsPredicateKnownToFail(MatcherTable, MatcherIndex, N,
Chris Lattnerda828e32010-03-03 07:46:25 +00001985 Result, *this, RecordedNodes);
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001986 if (!Result)
1987 break;
1988
Chris Lattnera6f86932010-03-27 18:54:50 +00001989 DEBUG(errs() << " Skipped scope entry (due to false predicate) at "
1990 << "index " << MatcherIndexOfPredicate
1991 << ", continuing at " << FailIndex << "\n");
Chris Lattnerbed993d2010-03-28 19:46:56 +00001992 ++NumDAGIselRetries;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00001993
1994 // Otherwise, we know that this case of the Scope is guaranteed to fail,
1995 // move to the next case.
1996 MatcherIndex = FailIndex;
1997 }
1998
1999 // If the whole scope failed to match, bail.
2000 if (FailIndex == 0) break;
2001
Chris Lattner2a49d572010-02-28 22:37:22 +00002002 // Push a MatchScope which indicates where to go if the first child fails
2003 // to match.
2004 MatchScope NewEntry;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00002005 NewEntry.FailIndex = FailIndex;
Chris Lattner2a49d572010-02-28 22:37:22 +00002006 NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end());
2007 NewEntry.NumRecordedNodes = RecordedNodes.size();
2008 NewEntry.NumMatchedMemRefs = MatchedMemRefs.size();
2009 NewEntry.InputChain = InputChain;
2010 NewEntry.InputFlag = InputFlag;
2011 NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty();
2012 NewEntry.HasFlagResultNodesMatched = !FlagResultNodesMatched.empty();
2013 MatchScopes.push_back(NewEntry);
2014 continue;
2015 }
2016 case OPC_RecordNode:
2017 // Remember this node, it may end up being an operand in the pattern.
2018 RecordedNodes.push_back(N);
2019 continue;
2020
2021 case OPC_RecordChild0: case OPC_RecordChild1:
2022 case OPC_RecordChild2: case OPC_RecordChild3:
2023 case OPC_RecordChild4: case OPC_RecordChild5:
2024 case OPC_RecordChild6: case OPC_RecordChild7: {
2025 unsigned ChildNo = Opcode-OPC_RecordChild0;
2026 if (ChildNo >= N.getNumOperands())
2027 break; // Match fails if out of range child #.
2028
2029 RecordedNodes.push_back(N->getOperand(ChildNo));
2030 continue;
2031 }
2032 case OPC_RecordMemRef:
2033 MatchedMemRefs.push_back(cast<MemSDNode>(N)->getMemOperand());
2034 continue;
2035
2036 case OPC_CaptureFlagInput:
2037 // If the current node has an input flag, capture it in InputFlag.
2038 if (N->getNumOperands() != 0 &&
2039 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag)
2040 InputFlag = N->getOperand(N->getNumOperands()-1);
2041 continue;
2042
2043 case OPC_MoveChild: {
2044 unsigned ChildNo = MatcherTable[MatcherIndex++];
2045 if (ChildNo >= N.getNumOperands())
2046 break; // Match fails if out of range child #.
2047 N = N.getOperand(ChildNo);
2048 NodeStack.push_back(N);
2049 continue;
2050 }
2051
2052 case OPC_MoveParent:
2053 // Pop the current node off the NodeStack.
2054 NodeStack.pop_back();
2055 assert(!NodeStack.empty() && "Node stack imbalance!");
2056 N = NodeStack.back();
2057 continue;
2058
Chris Lattnerda828e32010-03-03 07:46:25 +00002059 case OPC_CheckSame:
2060 if (!::CheckSame(MatcherTable, MatcherIndex, N, RecordedNodes)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002061 continue;
Chris Lattner2a49d572010-02-28 22:37:22 +00002062 case OPC_CheckPatternPredicate:
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00002063 if (!::CheckPatternPredicate(MatcherTable, MatcherIndex, *this)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002064 continue;
2065 case OPC_CheckPredicate:
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00002066 if (!::CheckNodePredicate(MatcherTable, MatcherIndex, *this,
2067 N.getNode()))
2068 break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002069 continue;
Chris Lattner57bf8a42010-03-04 01:23:08 +00002070 case OPC_CheckComplexPat: {
2071 unsigned CPNum = MatcherTable[MatcherIndex++];
2072 unsigned RecNo = MatcherTable[MatcherIndex++];
2073 assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat");
2074 if (!CheckComplexPattern(NodeToMatch, RecordedNodes[RecNo], CPNum,
2075 RecordedNodes))
Chris Lattner2a49d572010-02-28 22:37:22 +00002076 break;
2077 continue;
Chris Lattner57bf8a42010-03-04 01:23:08 +00002078 }
Chris Lattner2a49d572010-02-28 22:37:22 +00002079 case OPC_CheckOpcode:
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00002080 if (!::CheckOpcode(MatcherTable, MatcherIndex, N.getNode())) break;
2081 continue;
2082
2083 case OPC_CheckType:
2084 if (!::CheckType(MatcherTable, MatcherIndex, N, TLI)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002085 continue;
2086
Chris Lattnereb669212010-03-01 06:59:22 +00002087 case OPC_SwitchOpcode: {
2088 unsigned CurNodeOpcode = N.getOpcode();
Chris Lattner7d892d62010-03-01 07:43:08 +00002089 unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
Chris Lattnereb669212010-03-01 06:59:22 +00002090 unsigned CaseSize;
2091 while (1) {
2092 // Get the size of this case.
2093 CaseSize = MatcherTable[MatcherIndex++];
2094 if (CaseSize & 128)
2095 CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
2096 if (CaseSize == 0) break;
2097
Chris Lattnerd5d5a3d2010-03-25 06:33:05 +00002098 uint16_t Opc = MatcherTable[MatcherIndex++];
2099 Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
2100
Chris Lattnereb669212010-03-01 06:59:22 +00002101 // If the opcode matches, then we will execute this case.
Chris Lattnerd5d5a3d2010-03-25 06:33:05 +00002102 if (CurNodeOpcode == Opc)
Chris Lattnereb669212010-03-01 06:59:22 +00002103 break;
2104
2105 // Otherwise, skip over this case.
2106 MatcherIndex += CaseSize;
2107 }
2108
Chris Lattnercfe2eab2010-03-03 06:28:15 +00002109 // If no cases matched, bail out.
Chris Lattnereb669212010-03-01 06:59:22 +00002110 if (CaseSize == 0) break;
2111
2112 // Otherwise, execute the case we found.
2113 DEBUG(errs() << " OpcodeSwitch from " << SwitchStart
2114 << " to " << MatcherIndex << "\n");
2115 continue;
2116 }
2117
Chris Lattnercfe2eab2010-03-03 06:28:15 +00002118 case OPC_SwitchType: {
2119 MVT::SimpleValueType CurNodeVT = N.getValueType().getSimpleVT().SimpleTy;
2120 unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
2121 unsigned CaseSize;
2122 while (1) {
2123 // Get the size of this case.
2124 CaseSize = MatcherTable[MatcherIndex++];
2125 if (CaseSize & 128)
2126 CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
2127 if (CaseSize == 0) break;
2128
2129 MVT::SimpleValueType CaseVT =
2130 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
2131 if (CaseVT == MVT::iPTR)
2132 CaseVT = TLI.getPointerTy().SimpleTy;
2133
2134 // If the VT matches, then we will execute this case.
2135 if (CurNodeVT == CaseVT)
2136 break;
2137
2138 // Otherwise, skip over this case.
2139 MatcherIndex += CaseSize;
2140 }
2141
2142 // If no cases matched, bail out.
2143 if (CaseSize == 0) break;
2144
2145 // Otherwise, execute the case we found.
2146 DEBUG(errs() << " TypeSwitch[" << EVT(CurNodeVT).getEVTString()
2147 << "] from " << SwitchStart << " to " << MatcherIndex<<'\n');
2148 continue;
2149 }
Chris Lattner2a49d572010-02-28 22:37:22 +00002150 case OPC_CheckChild0Type: case OPC_CheckChild1Type:
2151 case OPC_CheckChild2Type: case OPC_CheckChild3Type:
2152 case OPC_CheckChild4Type: case OPC_CheckChild5Type:
Chris Lattnerda828e32010-03-03 07:46:25 +00002153 case OPC_CheckChild6Type: case OPC_CheckChild7Type:
2154 if (!::CheckChildType(MatcherTable, MatcherIndex, N, TLI,
2155 Opcode-OPC_CheckChild0Type))
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00002156 break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002157 continue;
Chris Lattner2a49d572010-02-28 22:37:22 +00002158 case OPC_CheckCondCode:
Chris Lattnerda828e32010-03-03 07:46:25 +00002159 if (!::CheckCondCode(MatcherTable, MatcherIndex, N)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002160 continue;
Chris Lattnerda828e32010-03-03 07:46:25 +00002161 case OPC_CheckValueType:
2162 if (!::CheckValueType(MatcherTable, MatcherIndex, N, TLI)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002163 continue;
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +00002164 case OPC_CheckInteger:
2165 if (!::CheckInteger(MatcherTable, MatcherIndex, N)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002166 continue;
Chris Lattnerda828e32010-03-03 07:46:25 +00002167 case OPC_CheckAndImm:
2168 if (!::CheckAndImm(MatcherTable, MatcherIndex, N, *this)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002169 continue;
Chris Lattnerda828e32010-03-03 07:46:25 +00002170 case OPC_CheckOrImm:
2171 if (!::CheckOrImm(MatcherTable, MatcherIndex, N, *this)) break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002172 continue;
Chris Lattner2a49d572010-02-28 22:37:22 +00002173
2174 case OPC_CheckFoldableChainNode: {
2175 assert(NodeStack.size() != 1 && "No parent node");
2176 // Verify that all intermediate nodes between the root and this one have
2177 // a single use.
2178 bool HasMultipleUses = false;
2179 for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i)
2180 if (!NodeStack[i].hasOneUse()) {
2181 HasMultipleUses = true;
2182 break;
2183 }
2184 if (HasMultipleUses) break;
2185
2186 // Check to see that the target thinks this is profitable to fold and that
2187 // we can fold it without inducing cycles in the graph.
2188 if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(),
2189 NodeToMatch) ||
2190 !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(),
Dan Gohmand858e902010-04-17 15:26:15 +00002191 NodeToMatch, OptLevel,
2192 true/*We validate our own chains*/))
Chris Lattner2a49d572010-02-28 22:37:22 +00002193 break;
2194
2195 continue;
2196 }
Chris Lattner2a49d572010-02-28 22:37:22 +00002197 case OPC_EmitInteger: {
2198 MVT::SimpleValueType VT =
2199 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
2200 int64_t Val = MatcherTable[MatcherIndex++];
2201 if (Val & 128)
2202 Val = GetVBR(Val, MatcherTable, MatcherIndex);
2203 RecordedNodes.push_back(CurDAG->getTargetConstant(Val, VT));
2204 continue;
2205 }
2206 case OPC_EmitRegister: {
2207 MVT::SimpleValueType VT =
2208 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
2209 unsigned RegNo = MatcherTable[MatcherIndex++];
2210 RecordedNodes.push_back(CurDAG->getRegister(RegNo, VT));
2211 continue;
2212 }
2213
2214 case OPC_EmitConvertToTarget: {
2215 // Convert from IMM/FPIMM to target version.
2216 unsigned RecNo = MatcherTable[MatcherIndex++];
2217 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2218 SDValue Imm = RecordedNodes[RecNo];
2219
2220 if (Imm->getOpcode() == ISD::Constant) {
2221 int64_t Val = cast<ConstantSDNode>(Imm)->getZExtValue();
2222 Imm = CurDAG->getTargetConstant(Val, Imm.getValueType());
2223 } else if (Imm->getOpcode() == ISD::ConstantFP) {
2224 const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue();
2225 Imm = CurDAG->getTargetConstantFP(*Val, Imm.getValueType());
2226 }
2227
2228 RecordedNodes.push_back(Imm);
2229 continue;
2230 }
2231
Chris Lattneraa4e3392010-03-28 05:50:16 +00002232 case OPC_EmitMergeInputChains1_0: // OPC_EmitMergeInputChains, 1, 0
2233 case OPC_EmitMergeInputChains1_1: { // OPC_EmitMergeInputChains, 1, 1
2234 // These are space-optimized forms of OPC_EmitMergeInputChains.
2235 assert(InputChain.getNode() == 0 &&
2236 "EmitMergeInputChains should be the first chain producing node");
2237 assert(ChainNodesMatched.empty() &&
2238 "Should only have one EmitMergeInputChains per match");
2239
2240 // Read all of the chained nodes.
2241 unsigned RecNo = Opcode == OPC_EmitMergeInputChains1_1;
2242 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2243 ChainNodesMatched.push_back(RecordedNodes[RecNo].getNode());
2244
2245 // FIXME: What if other value results of the node have uses not matched
2246 // by this pattern?
2247 if (ChainNodesMatched.back() != NodeToMatch &&
2248 !RecordedNodes[RecNo].hasOneUse()) {
2249 ChainNodesMatched.clear();
2250 break;
2251 }
2252
2253 // Merge the input chains if they are not intra-pattern references.
2254 InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
2255
2256 if (InputChain.getNode() == 0)
2257 break; // Failed to merge.
2258 continue;
2259 }
2260
Chris Lattner2a49d572010-02-28 22:37:22 +00002261 case OPC_EmitMergeInputChains: {
2262 assert(InputChain.getNode() == 0 &&
2263 "EmitMergeInputChains should be the first chain producing node");
2264 // This node gets a list of nodes we matched in the input that have
2265 // chains. We want to token factor all of the input chains to these nodes
2266 // together. However, if any of the input chains is actually one of the
2267 // nodes matched in this pattern, then we have an intra-match reference.
2268 // Ignore these because the newly token factored chain should not refer to
2269 // the old nodes.
2270 unsigned NumChains = MatcherTable[MatcherIndex++];
2271 assert(NumChains != 0 && "Can't TF zero chains");
2272
2273 assert(ChainNodesMatched.empty() &&
2274 "Should only have one EmitMergeInputChains per match");
2275
Chris Lattner2a49d572010-02-28 22:37:22 +00002276 // Read all of the chained nodes.
Chris Lattner00592ec2010-03-02 19:34:59 +00002277 for (unsigned i = 0; i != NumChains; ++i) {
2278 unsigned RecNo = MatcherTable[MatcherIndex++];
Chris Lattner2a49d572010-02-28 22:37:22 +00002279 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2280 ChainNodesMatched.push_back(RecordedNodes[RecNo].getNode());
2281
2282 // FIXME: What if other value results of the node have uses not matched
2283 // by this pattern?
2284 if (ChainNodesMatched.back() != NodeToMatch &&
2285 !RecordedNodes[RecNo].hasOneUse()) {
2286 ChainNodesMatched.clear();
2287 break;
2288 }
2289 }
Chris Lattner6b307922010-03-02 00:00:03 +00002290
2291 // If the inner loop broke out, the match fails.
2292 if (ChainNodesMatched.empty())
2293 break;
Chris Lattner2a49d572010-02-28 22:37:22 +00002294
Chris Lattner6b307922010-03-02 00:00:03 +00002295 // Merge the input chains if they are not intra-pattern references.
2296 InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG);
2297
2298 if (InputChain.getNode() == 0)
2299 break; // Failed to merge.
Chris Lattner2a49d572010-02-28 22:37:22 +00002300
Chris Lattner2a49d572010-02-28 22:37:22 +00002301 continue;
2302 }
2303
2304 case OPC_EmitCopyToReg: {
2305 unsigned RecNo = MatcherTable[MatcherIndex++];
2306 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2307 unsigned DestPhysReg = MatcherTable[MatcherIndex++];
2308
2309 if (InputChain.getNode() == 0)
2310 InputChain = CurDAG->getEntryNode();
2311
2312 InputChain = CurDAG->getCopyToReg(InputChain, NodeToMatch->getDebugLoc(),
2313 DestPhysReg, RecordedNodes[RecNo],
2314 InputFlag);
2315
2316 InputFlag = InputChain.getValue(1);
2317 continue;
2318 }
2319
2320 case OPC_EmitNodeXForm: {
2321 unsigned XFormNo = MatcherTable[MatcherIndex++];
2322 unsigned RecNo = MatcherTable[MatcherIndex++];
2323 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2324 RecordedNodes.push_back(RunSDNodeXForm(RecordedNodes[RecNo], XFormNo));
2325 continue;
2326 }
2327
2328 case OPC_EmitNode:
2329 case OPC_MorphNodeTo: {
Chris Lattner14df8dc2010-02-28 22:38:43 +00002330 uint16_t TargetOpc = MatcherTable[MatcherIndex++];
2331 TargetOpc |= (unsigned short)MatcherTable[MatcherIndex++] << 8;
Chris Lattner2a49d572010-02-28 22:37:22 +00002332 unsigned EmitNodeInfo = MatcherTable[MatcherIndex++];
2333 // Get the result VT list.
2334 unsigned NumVTs = MatcherTable[MatcherIndex++];
2335 SmallVector<EVT, 4> VTs;
2336 for (unsigned i = 0; i != NumVTs; ++i) {
2337 MVT::SimpleValueType VT =
2338 (MVT::SimpleValueType)MatcherTable[MatcherIndex++];
2339 if (VT == MVT::iPTR) VT = TLI.getPointerTy().SimpleTy;
2340 VTs.push_back(VT);
2341 }
2342
2343 if (EmitNodeInfo & OPFL_Chain)
2344 VTs.push_back(MVT::Other);
2345 if (EmitNodeInfo & OPFL_FlagOutput)
2346 VTs.push_back(MVT::Flag);
2347
Chris Lattner7d892d62010-03-01 07:43:08 +00002348 // This is hot code, so optimize the two most common cases of 1 and 2
2349 // results.
2350 SDVTList VTList;
2351 if (VTs.size() == 1)
2352 VTList = CurDAG->getVTList(VTs[0]);
2353 else if (VTs.size() == 2)
2354 VTList = CurDAG->getVTList(VTs[0], VTs[1]);
2355 else
2356 VTList = CurDAG->getVTList(VTs.data(), VTs.size());
Chris Lattner2a49d572010-02-28 22:37:22 +00002357
2358 // Get the operand list.
2359 unsigned NumOps = MatcherTable[MatcherIndex++];
2360 SmallVector<SDValue, 8> Ops;
2361 for (unsigned i = 0; i != NumOps; ++i) {
2362 unsigned RecNo = MatcherTable[MatcherIndex++];
2363 if (RecNo & 128)
2364 RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
2365
2366 assert(RecNo < RecordedNodes.size() && "Invalid EmitNode");
2367 Ops.push_back(RecordedNodes[RecNo]);
2368 }
2369
2370 // If there are variadic operands to add, handle them now.
2371 if (EmitNodeInfo & OPFL_VariadicInfo) {
2372 // Determine the start index to copy from.
2373 unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo);
2374 FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0;
2375 assert(NodeToMatch->getNumOperands() >= FirstOpToCopy &&
2376 "Invalid variadic node");
2377 // Copy all of the variadic operands, not including a potential flag
2378 // input.
2379 for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands();
2380 i != e; ++i) {
2381 SDValue V = NodeToMatch->getOperand(i);
2382 if (V.getValueType() == MVT::Flag) break;
2383 Ops.push_back(V);
2384 }
2385 }
2386
2387 // If this has chain/flag inputs, add them.
2388 if (EmitNodeInfo & OPFL_Chain)
2389 Ops.push_back(InputChain);
2390 if ((EmitNodeInfo & OPFL_FlagInput) && InputFlag.getNode() != 0)
2391 Ops.push_back(InputFlag);
2392
2393 // Create the node.
2394 SDNode *Res = 0;
2395 if (Opcode != OPC_MorphNodeTo) {
2396 // If this is a normal EmitNode command, just create the new node and
2397 // add the results to the RecordedNodes list.
2398 Res = CurDAG->getMachineNode(TargetOpc, NodeToMatch->getDebugLoc(),
2399 VTList, Ops.data(), Ops.size());
2400
2401 // Add all the non-flag/non-chain results to the RecordedNodes list.
2402 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
2403 if (VTs[i] == MVT::Other || VTs[i] == MVT::Flag) break;
2404 RecordedNodes.push_back(SDValue(Res, i));
2405 }
2406
2407 } else {
Chris Lattner3ff1e4d2010-03-02 06:55:04 +00002408 Res = MorphNode(NodeToMatch, TargetOpc, VTList, Ops.data(), Ops.size(),
2409 EmitNodeInfo);
Chris Lattner2a49d572010-02-28 22:37:22 +00002410 }
2411
2412 // If the node had chain/flag results, update our notion of the current
2413 // chain and flag.
Chris Lattner82dd3d32010-03-02 07:50:03 +00002414 if (EmitNodeInfo & OPFL_FlagOutput) {
Chris Lattner2a49d572010-02-28 22:37:22 +00002415 InputFlag = SDValue(Res, VTs.size()-1);
2416 if (EmitNodeInfo & OPFL_Chain)
2417 InputChain = SDValue(Res, VTs.size()-2);
2418 } else if (EmitNodeInfo & OPFL_Chain)
2419 InputChain = SDValue(Res, VTs.size()-1);
2420
2421 // If the OPFL_MemRefs flag is set on this node, slap all of the
2422 // accumulated memrefs onto it.
2423 //
2424 // FIXME: This is vastly incorrect for patterns with multiple outputs
2425 // instructions that access memory and for ComplexPatterns that match
2426 // loads.
2427 if (EmitNodeInfo & OPFL_MemRefs) {
2428 MachineSDNode::mmo_iterator MemRefs =
2429 MF->allocateMemRefsArray(MatchedMemRefs.size());
2430 std::copy(MatchedMemRefs.begin(), MatchedMemRefs.end(), MemRefs);
2431 cast<MachineSDNode>(Res)
2432 ->setMemRefs(MemRefs, MemRefs + MatchedMemRefs.size());
2433 }
2434
2435 DEBUG(errs() << " "
2436 << (Opcode == OPC_MorphNodeTo ? "Morphed" : "Created")
2437 << " node: "; Res->dump(CurDAG); errs() << "\n");
2438
2439 // If this was a MorphNodeTo then we're completely done!
2440 if (Opcode == OPC_MorphNodeTo) {
2441 // Update chain and flag uses.
2442 UpdateChainsAndFlags(NodeToMatch, InputChain, ChainNodesMatched,
Chris Lattner82dd3d32010-03-02 07:50:03 +00002443 InputFlag, FlagResultNodesMatched, true);
2444 return Res;
Chris Lattner2a49d572010-02-28 22:37:22 +00002445 }
2446
2447 continue;
2448 }
2449
2450 case OPC_MarkFlagResults: {
2451 unsigned NumNodes = MatcherTable[MatcherIndex++];
2452
2453 // Read and remember all the flag-result nodes.
2454 for (unsigned i = 0; i != NumNodes; ++i) {
2455 unsigned RecNo = MatcherTable[MatcherIndex++];
2456 if (RecNo & 128)
2457 RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
2458
2459 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2460 FlagResultNodesMatched.push_back(RecordedNodes[RecNo].getNode());
2461 }
2462 continue;
2463 }
2464
2465 case OPC_CompleteMatch: {
2466 // The match has been completed, and any new nodes (if any) have been
2467 // created. Patch up references to the matched dag to use the newly
2468 // created nodes.
2469 unsigned NumResults = MatcherTable[MatcherIndex++];
2470
2471 for (unsigned i = 0; i != NumResults; ++i) {
2472 unsigned ResSlot = MatcherTable[MatcherIndex++];
2473 if (ResSlot & 128)
2474 ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex);
2475
2476 assert(ResSlot < RecordedNodes.size() && "Invalid CheckSame");
2477 SDValue Res = RecordedNodes[ResSlot];
2478
Chris Lattnerb0e483e2010-03-28 05:54:03 +00002479 assert(i < NodeToMatch->getNumValues() &&
2480 NodeToMatch->getValueType(i) != MVT::Other &&
2481 NodeToMatch->getValueType(i) != MVT::Flag &&
2482 "Invalid number of results to complete!");
Chris Lattner2a49d572010-02-28 22:37:22 +00002483 assert((NodeToMatch->getValueType(i) == Res.getValueType() ||
2484 NodeToMatch->getValueType(i) == MVT::iPTR ||
2485 Res.getValueType() == MVT::iPTR ||
2486 NodeToMatch->getValueType(i).getSizeInBits() ==
2487 Res.getValueType().getSizeInBits()) &&
2488 "invalid replacement");
2489 CurDAG->ReplaceAllUsesOfValueWith(SDValue(NodeToMatch, i), Res);
2490 }
2491
2492 // If the root node defines a flag, add it to the flag nodes to update
2493 // list.
2494 if (NodeToMatch->getValueType(NodeToMatch->getNumValues()-1) == MVT::Flag)
2495 FlagResultNodesMatched.push_back(NodeToMatch);
2496
2497 // Update chain and flag uses.
2498 UpdateChainsAndFlags(NodeToMatch, InputChain, ChainNodesMatched,
Chris Lattner82dd3d32010-03-02 07:50:03 +00002499 InputFlag, FlagResultNodesMatched, false);
Chris Lattner2a49d572010-02-28 22:37:22 +00002500
2501 assert(NodeToMatch->use_empty() &&
2502 "Didn't replace all uses of the node?");
2503
2504 // FIXME: We just return here, which interacts correctly with SelectRoot
2505 // above. We should fix this to not return an SDNode* anymore.
2506 return 0;
2507 }
2508 }
2509
2510 // If the code reached this point, then the match failed. See if there is
2511 // another child to try in the current 'Scope', otherwise pop it until we
2512 // find a case to check.
Dan Gohman19b38262010-03-09 02:15:05 +00002513 DEBUG(errs() << " Match failed at index " << CurrentOpcodeIndex << "\n");
Chris Lattnerbed993d2010-03-28 19:46:56 +00002514 ++NumDAGIselRetries;
Chris Lattner2a49d572010-02-28 22:37:22 +00002515 while (1) {
2516 if (MatchScopes.empty()) {
2517 CannotYetSelect(NodeToMatch);
2518 return 0;
2519 }
2520
2521 // Restore the interpreter state back to the point where the scope was
2522 // formed.
2523 MatchScope &LastScope = MatchScopes.back();
2524 RecordedNodes.resize(LastScope.NumRecordedNodes);
2525 NodeStack.clear();
2526 NodeStack.append(LastScope.NodeStack.begin(), LastScope.NodeStack.end());
2527 N = NodeStack.back();
2528
Chris Lattner2a49d572010-02-28 22:37:22 +00002529 if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size())
2530 MatchedMemRefs.resize(LastScope.NumMatchedMemRefs);
2531 MatcherIndex = LastScope.FailIndex;
2532
Dan Gohman19b38262010-03-09 02:15:05 +00002533 DEBUG(errs() << " Continuing at " << MatcherIndex << "\n");
2534
Chris Lattner2a49d572010-02-28 22:37:22 +00002535 InputChain = LastScope.InputChain;
2536 InputFlag = LastScope.InputFlag;
2537 if (!LastScope.HasChainNodesMatched)
2538 ChainNodesMatched.clear();
2539 if (!LastScope.HasFlagResultNodesMatched)
2540 FlagResultNodesMatched.clear();
2541
2542 // Check to see what the offset is at the new MatcherIndex. If it is zero
2543 // we have reached the end of this scope, otherwise we have another child
2544 // in the current scope to try.
2545 unsigned NumToSkip = MatcherTable[MatcherIndex++];
2546 if (NumToSkip & 128)
2547 NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
2548
2549 // If we have another child in this scope to match, update FailIndex and
2550 // try it.
2551 if (NumToSkip != 0) {
2552 LastScope.FailIndex = MatcherIndex+NumToSkip;
2553 break;
2554 }
2555
2556 // End of this scope, pop it and try the next child in the containing
2557 // scope.
2558 MatchScopes.pop_back();
2559 }
2560 }
2561}
2562
2563
2564
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002565void SelectionDAGISel::CannotYetSelect(SDNode *N) {
Dan Gohmane1f188f2009-10-29 22:30:23 +00002566 std::string msg;
2567 raw_string_ostream Msg(msg);
2568 Msg << "Cannot yet select: ";
Chris Lattner2c4afd12010-03-04 00:21:16 +00002569
2570 if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN &&
2571 N->getOpcode() != ISD::INTRINSIC_WO_CHAIN &&
2572 N->getOpcode() != ISD::INTRINSIC_VOID) {
2573 N->printrFull(Msg, CurDAG);
2574 } else {
2575 bool HasInputChain = N->getOperand(0).getValueType() == MVT::Other;
2576 unsigned iid =
2577 cast<ConstantSDNode>(N->getOperand(HasInputChain))->getZExtValue();
2578 if (iid < Intrinsic::num_intrinsics)
2579 Msg << "intrinsic %" << Intrinsic::getName((Intrinsic::ID)iid);
2580 else if (const TargetIntrinsicInfo *TII = TM.getIntrinsicInfo())
2581 Msg << "target intrinsic %" << TII->getName(iid);
2582 else
2583 Msg << "unknown intrinsic #" << iid;
2584 }
Chris Lattner75361b62010-04-07 22:58:41 +00002585 report_fatal_error(Msg.str());
Dan Gohmane1f188f2009-10-29 22:30:23 +00002586}
2587
Devang Patel19974732007-05-03 01:11:54 +00002588char SelectionDAGISel::ID = 0;