blob: acbb5c9bd26ef1db9f5199e691ee1a126ba5b4f1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAGISel class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Dan Gohman8afcc1d2009-02-06 17:22:58 +000015#include "ScheduleDAGSDNodes.h"
Dan Gohman7ec20512009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohman39a0cdf2009-11-23 17:16:22 +000017#include "FunctionLoweringInfo.h"
Dan Gohman8afcc1d2009-02-06 17:22:58 +000018#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Analysis/AliasAnalysis.h"
Devang Patel3f7aedd2009-09-16 21:09:07 +000020#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Constants.h"
22#include "llvm/CallingConv.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Function.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/InlineAsm.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/IntrinsicInst.h"
Chris Lattner6d15db72009-10-27 17:02:08 +000030#include "llvm/LLVMContext.h"
Dan Gohman91ad3122008-08-19 22:33:34 +000031#include "llvm/CodeGen/FastISel.h"
Gordon Henriksenf194af22008-08-17 12:56:54 +000032#include "llvm/CodeGen/GCStrategy.h"
Gordon Henriksen1aed5992008-08-17 18:44:35 +000033#include "llvm/CodeGen/GCMetadata.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000035#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000038#include "llvm/CodeGen/MachineJumpTableInfo.h"
39#include "llvm/CodeGen/MachineModuleInfo.h"
40#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmandd6547d2009-01-15 22:18:12 +000041#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/CodeGen/SchedulerRegistry.h"
43#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel10e63332009-01-09 19:11:50 +000044#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000045#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046#include "llvm/Target/TargetData.h"
47#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanb65e3ac2009-10-29 22:30:23 +000048#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#include "llvm/Target/TargetInstrInfo.h"
50#include "llvm/Target/TargetLowering.h"
51#include "llvm/Target/TargetMachine.h"
52#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053#include "llvm/Support/Compiler.h"
Evan Cheng34fd4f32008-06-30 20:45:06 +000054#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000055#include "llvm/Support/ErrorHandling.h"
Evan Cheng34fd4f32008-06-30 20:45:06 +000056#include "llvm/Support/MathExtras.h"
57#include "llvm/Support/Timer.h"
Daniel Dunbar005975c2009-07-25 00:23:56 +000058#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059#include <algorithm>
60using namespace llvm;
61
Chris Lattner68068cc2008-06-17 06:09:18 +000062static cl::opt<bool>
Duncan Sandsb3ac3882008-10-27 08:42:46 +000063DisableLegalizeTypes("disable-legalize-types", cl::Hidden);
Dan Gohman91ad3122008-08-19 22:33:34 +000064static cl::opt<bool>
Dan Gohman69740ec2008-09-09 22:06:46 +000065EnableFastISelVerbose("fast-isel-verbose", cl::Hidden,
Dan Gohmaneec72ed2008-10-20 21:30:12 +000066 cl::desc("Enable verbose messages in the \"fast\" "
Dan Gohman69740ec2008-09-09 22:06:46 +000067 "instruction selector"));
68static cl::opt<bool>
Dan Gohmanee70e1d2008-09-09 23:05:00 +000069EnableFastISelAbort("fast-isel-abort", cl::Hidden,
70 cl::desc("Enable abort calls when \"fast\" instruction fails"));
Dan Gohman283e4992008-09-05 22:59:21 +000071static cl::opt<bool>
Evan Cheng48ac7b92009-11-09 06:49:37 +000072SchedLiveInCopies("schedule-livein-copies", cl::Hidden,
Dan Gohman283e4992008-09-05 22:59:21 +000073 cl::desc("Schedule copies of livein registers"),
74 cl::init(false));
Chris Lattner68068cc2008-06-17 06:09:18 +000075
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076#ifndef NDEBUG
77static cl::opt<bool>
Dan Gohmanb552df72008-07-21 20:00:07 +000078ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
79 cl::desc("Pop up a window to show dags before the first "
80 "dag combine pass"));
81static cl::opt<bool>
82ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
83 cl::desc("Pop up a window to show dags before legalize types"));
84static cl::opt<bool>
85ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
86 cl::desc("Pop up a window to show dags before legalize"));
87static cl::opt<bool>
88ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
89 cl::desc("Pop up a window to show dags before the second "
90 "dag combine pass"));
91static cl::opt<bool>
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000092ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
93 cl::desc("Pop up a window to show dags before the post legalize types"
94 " dag combine pass"));
95static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096ViewISelDAGs("view-isel-dags", cl::Hidden,
97 cl::desc("Pop up a window to show isel dags as they are selected"));
98static cl::opt<bool>
99ViewSchedDAGs("view-sched-dags", cl::Hidden,
100 cl::desc("Pop up a window to show sched dags as they are processed"));
Dan Gohman134c5b62007-08-28 20:32:58 +0000101static cl::opt<bool>
102ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
Chris Lattner2f69f132008-01-25 17:24:52 +0000103 cl::desc("Pop up a window to show SUnit dags after they are processed"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104#else
Dan Gohmanb552df72008-07-21 20:00:07 +0000105static const bool ViewDAGCombine1 = false,
106 ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false,
107 ViewDAGCombine2 = false,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000108 ViewDAGCombineLT = false,
Dan Gohmanb552df72008-07-21 20:00:07 +0000109 ViewISelDAGs = false, ViewSchedDAGs = false,
110 ViewSUnitDAGs = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111#endif
112
113//===---------------------------------------------------------------------===//
114///
115/// RegisterScheduler class - Track the registration of instruction schedulers.
116///
117//===---------------------------------------------------------------------===//
118MachinePassRegistry RegisterScheduler::Registry;
119
120//===---------------------------------------------------------------------===//
121///
122/// ISHeuristic command line option for instruction schedulers.
123///
124//===---------------------------------------------------------------------===//
Dan Gohman089efff2008-05-13 00:00:25 +0000125static cl::opt<RegisterScheduler::FunctionPassCtor, false,
126 RegisterPassParser<RegisterScheduler> >
127ISHeuristic("pre-RA-sched",
128 cl::init(&createDefaultScheduler),
129 cl::desc("Instruction schedulers available (before register"
130 " allocation):"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
Dan Gohman089efff2008-05-13 00:00:25 +0000132static RegisterScheduler
Dan Gohman669b9bf2008-10-14 20:25:08 +0000133defaultListDAGScheduler("default", "Best scheduler for the target",
Dan Gohman089efff2008-05-13 00:00:25 +0000134 createDefaultScheduler);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136namespace llvm {
137 //===--------------------------------------------------------------------===//
138 /// createDefaultScheduler - This creates an instruction scheduler appropriate
139 /// for the target.
Dan Gohman7c968a82009-02-11 04:27:20 +0000140 ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000141 CodeGenOpt::Level OptLevel) {
Dan Gohmana0c429e2009-01-15 16:58:17 +0000142 const TargetLowering &TLI = IS->getTargetLowering();
143
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000144 if (OptLevel == CodeGenOpt::None)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000145 return createFastDAGScheduler(IS, OptLevel);
Dan Gohman09ab1d72008-11-20 03:11:19 +0000146 if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000147 return createTDListDAGScheduler(IS, OptLevel);
Dan Gohman09ab1d72008-11-20 03:11:19 +0000148 assert(TLI.getSchedulingPreference() ==
149 TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
Bill Wendling58ed5d22009-04-29 00:15:41 +0000150 return createBURRListDAGScheduler(IS, OptLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152}
153
Evan Chenge637db12008-01-30 18:18:23 +0000154// EmitInstrWithCustomInserter - This method should be implemented by targets
Dan Gohman30afe012009-10-29 18:10:34 +0000155// that mark instructions with the 'usesCustomInserter' flag. These
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156// instructions are special in various ways, which require special support to
157// insert. The specified MachineInstr is created but not inserted into any
Dan Gohman30afe012009-10-29 18:10:34 +0000158// basic blocks, and this method is called to expand it into a sequence of
159// instructions, potentially also creating new basic blocks and control flow.
160// When new basic blocks are inserted and the edges from MBB to its successors
161// are modified, the method should insert pairs of <OldSucc, NewSucc> into the
162// DenseMap.
Evan Chenge637db12008-01-30 18:18:23 +0000163MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengd7dc9832009-09-18 21:02:19 +0000164 MachineBasicBlock *MBB,
165 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Edwin Török280d15b2009-07-12 20:07:01 +0000166#ifndef NDEBUG
Chris Lattner36eef822009-08-23 07:05:07 +0000167 errs() << "If a target marks an instruction with "
Dan Gohman30afe012009-10-29 18:10:34 +0000168 "'usesCustomInserter', it must implement "
Edwin Török280d15b2009-07-12 20:07:01 +0000169 "TargetLowering::EmitInstrWithCustomInserter!";
170#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000171 llvm_unreachable(0);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000172 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173}
174
Dan Gohman283e4992008-09-05 22:59:21 +0000175/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
176/// physical register has only a single copy use, then coalesced the copy
177/// if possible.
178static void EmitLiveInCopy(MachineBasicBlock *MBB,
179 MachineBasicBlock::iterator &InsertPos,
180 unsigned VirtReg, unsigned PhysReg,
181 const TargetRegisterClass *RC,
182 DenseMap<MachineInstr*, unsigned> &CopyRegMap,
183 const MachineRegisterInfo &MRI,
184 const TargetRegisterInfo &TRI,
185 const TargetInstrInfo &TII) {
186 unsigned NumUses = 0;
187 MachineInstr *UseMI = NULL;
188 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
189 UE = MRI.use_end(); UI != UE; ++UI) {
190 UseMI = &*UI;
191 if (++NumUses > 1)
192 break;
193 }
194
195 // If the number of uses is not one, or the use is not a move instruction,
196 // don't coalesce. Also, only coalesce away a virtual register to virtual
197 // register copy.
198 bool Coalesced = false;
Evan Chengf97496a2009-01-20 19:12:24 +0000199 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Dan Gohman283e4992008-09-05 22:59:21 +0000200 if (NumUses == 1 &&
Evan Chengf97496a2009-01-20 19:12:24 +0000201 TII.isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
Dan Gohman283e4992008-09-05 22:59:21 +0000202 TargetRegisterInfo::isVirtualRegister(DstReg)) {
203 VirtReg = DstReg;
204 Coalesced = true;
205 }
206
207 // Now find an ideal location to insert the copy.
208 MachineBasicBlock::iterator Pos = InsertPos;
209 while (Pos != MBB->begin()) {
210 MachineInstr *PrevMI = prior(Pos);
211 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
212 // copyRegToReg might emit multiple instructions to do a copy.
213 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
214 if (CopyDstReg && !TRI.regsOverlap(CopyDstReg, PhysReg))
215 // This is what the BB looks like right now:
216 // r1024 = mov r0
217 // ...
218 // r1 = mov r1024
219 //
220 // We want to insert "r1025 = mov r1". Inserting this copy below the
221 // move to r1024 makes it impossible for that move to be coalesced.
222 //
223 // r1025 = mov r1
224 // r1024 = mov r0
225 // ...
226 // r1 = mov 1024
227 // r2 = mov 1025
228 break; // Woot! Found a good location.
229 --Pos;
230 }
231
David Goodwin4a897932009-07-08 23:10:31 +0000232 bool Emitted = TII.copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
233 assert(Emitted && "Unable to issue a live-in copy instruction!\n");
234 (void) Emitted;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000235
Zhongxing Xu722d9662009-10-16 05:42:28 +0000236 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
Dan Gohman283e4992008-09-05 22:59:21 +0000237 if (Coalesced) {
238 if (&*InsertPos == UseMI) ++InsertPos;
239 MBB->erase(UseMI);
240 }
241}
242
243/// EmitLiveInCopies - If this is the first basic block in the function,
244/// and if it has live ins that need to be copied into vregs, emit the
245/// copies into the block.
246static void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
247 const MachineRegisterInfo &MRI,
248 const TargetRegisterInfo &TRI,
249 const TargetInstrInfo &TII) {
250 if (SchedLiveInCopies) {
251 // Emit the copies at a heuristically-determined location in the block.
252 DenseMap<MachineInstr*, unsigned> CopyRegMap;
253 MachineBasicBlock::iterator InsertPos = EntryMBB->begin();
254 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
255 E = MRI.livein_end(); LI != E; ++LI)
256 if (LI->second) {
257 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
258 EmitLiveInCopy(EntryMBB, InsertPos, LI->second, LI->first,
259 RC, CopyRegMap, MRI, TRI, TII);
260 }
261 } else {
262 // Emit the copies into the top of the block.
263 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
264 E = MRI.livein_end(); LI != E; ++LI)
265 if (LI->second) {
266 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
David Goodwin4a897932009-07-08 23:10:31 +0000267 bool Emitted = TII.copyRegToReg(*EntryMBB, EntryMBB->begin(),
268 LI->second, LI->first, RC, RC);
269 assert(Emitted && "Unable to issue a live-in copy instruction!\n");
270 (void) Emitted;
Dan Gohman283e4992008-09-05 22:59:21 +0000271 }
272 }
273}
274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275//===----------------------------------------------------------------------===//
276// SelectionDAGISel code
277//===----------------------------------------------------------------------===//
278
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000279SelectionDAGISel::SelectionDAGISel(TargetMachine &tm, CodeGenOpt::Level OL) :
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000280 MachineFunctionPass(&ID), TM(tm), TLI(*tm.getTargetLowering()),
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000281 FuncInfo(new FunctionLoweringInfo(TLI)),
282 CurDAG(new SelectionDAG(TLI, *FuncInfo)),
Dan Gohman7ec20512009-11-23 18:04:58 +0000283 SDB(new SelectionDAGBuilder(*CurDAG, TLI, *FuncInfo, OL)),
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000284 GFI(),
Bill Wendling58ed5d22009-04-29 00:15:41 +0000285 OptLevel(OL),
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000286 DAGSize(0)
287{}
288
289SelectionDAGISel::~SelectionDAGISel() {
Dan Gohman7ec20512009-11-23 18:04:58 +0000290 delete SDB;
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000291 delete CurDAG;
292 delete FuncInfo;
293}
294
Owen Andersonac9de032009-08-10 22:56:29 +0000295unsigned SelectionDAGISel::MakeReg(EVT VT) {
Chris Lattner1b989192007-12-31 04:13:23 +0000296 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297}
298
299void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
300 AU.addRequired<AliasAnalysis>();
Dan Gohman03354412009-07-31 23:36:22 +0000301 AU.addPreserved<AliasAnalysis>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000302 AU.addRequired<GCModuleInfo>();
Dan Gohman03354412009-07-31 23:36:22 +0000303 AU.addPreserved<GCModuleInfo>();
Devang Patel10e63332009-01-09 19:11:50 +0000304 AU.addRequired<DwarfWriter>();
Dan Gohman03354412009-07-31 23:36:22 +0000305 AU.addPreserved<DwarfWriter>();
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000306 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307}
308
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000309bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
310 Function &Fn = *mf.getFunction();
311
Dan Gohmanee70e1d2008-09-09 23:05:00 +0000312 // Do some sanity-checking on the command-line options.
313 assert((!EnableFastISelVerbose || EnableFastISel) &&
314 "-fast-isel-verbose requires -fast-isel");
315 assert((!EnableFastISelAbort || EnableFastISel) &&
316 "-fast-isel-abort requires -fast-isel");
317
Dan Gohmancc863aa2007-08-27 16:26:13 +0000318 // Get alias analysis for load/store combining.
319 AA = &getAnalysis<AliasAnalysis>();
320
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000321 MF = &mf;
Dan Gohman283e4992008-09-05 22:59:21 +0000322 const TargetInstrInfo &TII = *TM.getInstrInfo();
323 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
324
Dan Gohmanfbd88292009-08-01 03:51:09 +0000325 if (Fn.hasGC())
326 GFI = &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000327 else
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000328 GFI = 0;
Dan Gohman96eb47a2009-01-15 19:20:50 +0000329 RegInfo = &MF->getRegInfo();
Daniel Dunbar005975c2009-07-25 00:23:56 +0000330 DEBUG(errs() << "\n\n\n=== " << Fn.getName() << "\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000332 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
333 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Owen Andersonf3a4d6b2009-07-09 18:44:09 +0000334 CurDAG->init(*MF, MMI, DW);
Dan Gohman39a0cdf2009-11-23 17:16:22 +0000335 FuncInfo->set(Fn, *MF, EnableFastISel);
Dan Gohman7ec20512009-11-23 18:04:58 +0000336 SDB->init(GFI, *AA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337
Dale Johannesen85535762008-04-02 00:25:04 +0000338 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
339 if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator()))
340 // Mark landing pad.
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000341 FuncInfo->MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342
Dan Gohman96eb47a2009-01-15 19:20:50 +0000343 SelectAllBasicBlocks(Fn, *MF, MMI, DW, TII);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
Dan Gohman283e4992008-09-05 22:59:21 +0000345 // If the first basic block in the function has live ins that need to be
346 // copied into vregs, emit the copies into the top of the block before
347 // emitting the code for the block.
Dan Gohman96eb47a2009-01-15 19:20:50 +0000348 EmitLiveInCopies(MF->begin(), *RegInfo, TRI, TII);
Dan Gohman283e4992008-09-05 22:59:21 +0000349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 // Add function live-ins to entry block live-in set.
Dan Gohman283e4992008-09-05 22:59:21 +0000351 for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(),
352 E = RegInfo->livein_end(); I != E; ++I)
Dan Gohman96eb47a2009-01-15 19:20:50 +0000353 MF->begin()->addLiveIn(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354
355#ifndef NDEBUG
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000356 assert(FuncInfo->CatchInfoFound.size() == FuncInfo->CatchInfoLost.size() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 "Not all catch info was assigned to a landing pad!");
358#endif
359
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000360 FuncInfo->clear();
361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 return true;
363}
364
Dan Gohman14a66442008-08-23 02:25:05 +0000365void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB,
366 BasicBlock::iterator Begin,
Dan Gohman40b0a2e2009-11-20 02:51:26 +0000367 BasicBlock::iterator End,
368 bool &HadTailCall) {
Dan Gohman7ec20512009-11-23 18:04:58 +0000369 SDB->setCurrentBasicBlock(BB);
Devang Patel6de78e22009-09-28 21:41:20 +0000370 MetadataContext &TheMetadata = LLVMBB->getParent()->getContext().getMetadata();
Devang Patelb5896162009-09-28 21:14:55 +0000371 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Dan Gohman14a66442008-08-23 02:25:05 +0000372
Dan Gohman9178de12009-08-05 01:29:28 +0000373 // Lower all of the non-terminator instructions. If a call is emitted
374 // as a tail call, cease emitting nodes for this block.
Dan Gohman7ec20512009-11-23 18:04:58 +0000375 for (BasicBlock::iterator I = Begin; I != End && !SDB->HasTailCall; ++I) {
Devang Patela0fbb002009-09-16 20:39:11 +0000376 if (MDDbgKind) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000377 // Update DebugLoc if debug information is attached with this
Devang Patela0fbb002009-09-16 20:39:11 +0000378 // instruction.
Devang Patel90a0fe32009-11-10 23:06:00 +0000379 if (!isa<DbgInfoIntrinsic>(I))
380 if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, I)) {
381 DILocation DILoc(Dbg);
382 DebugLoc Loc = ExtractDebugLocation(DILoc, MF->getDebugLocInfo());
Dan Gohman7ec20512009-11-23 18:04:58 +0000383 SDB->setCurDebugLoc(Loc);
Devang Patel90a0fe32009-11-10 23:06:00 +0000384 if (MF->getDefaultDebugLoc().isUnknown())
385 MF->setDefaultDebugLoc(Loc);
386 }
Devang Patela0fbb002009-09-16 20:39:11 +0000387 }
Dan Gohman14a66442008-08-23 02:25:05 +0000388 if (!isa<TerminatorInst>(I))
Dan Gohman7ec20512009-11-23 18:04:58 +0000389 SDB->visit(*I);
Devang Patela0fbb002009-09-16 20:39:11 +0000390 }
Dan Gohman14a66442008-08-23 02:25:05 +0000391
Dan Gohman7ec20512009-11-23 18:04:58 +0000392 if (!SDB->HasTailCall) {
Dan Gohman9178de12009-08-05 01:29:28 +0000393 // Ensure that all instructions which are used outside of their defining
394 // blocks are available as virtual registers. Invoke is handled elsewhere.
395 for (BasicBlock::iterator I = Begin; I != End; ++I)
396 if (!isa<PHINode>(I) && !isa<InvokeInst>(I))
Dan Gohman7ec20512009-11-23 18:04:58 +0000397 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohman14a66442008-08-23 02:25:05 +0000398
Dan Gohman9178de12009-08-05 01:29:28 +0000399 // Handle PHI nodes in successor blocks.
400 if (End == LLVMBB->end()) {
401 HandlePHINodesInSuccessorBlocks(LLVMBB);
Dan Gohmanca4857a2008-09-03 23:12:08 +0000402
Dan Gohman9178de12009-08-05 01:29:28 +0000403 // Lower the terminator after the copies are emitted.
Dan Gohman7ec20512009-11-23 18:04:58 +0000404 SDB->visit(*LLVMBB->getTerminator());
Dan Gohman9178de12009-08-05 01:29:28 +0000405 }
Dan Gohmanca4857a2008-09-03 23:12:08 +0000406 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000407
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 // Make sure the root of the DAG is up-to-date.
Dan Gohman7ec20512009-11-23 18:04:58 +0000409 CurDAG->setRoot(SDB->getControlRoot());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000410
Dan Gohman14a66442008-08-23 02:25:05 +0000411 // Final step, emit the lowered DAG as machine code.
412 CodeGenAndEmitDAG();
Dan Gohman7ec20512009-11-23 18:04:58 +0000413 HadTailCall = SDB->HasTailCall;
414 SDB->clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415}
416
Dan Gohman14a66442008-08-23 02:25:05 +0000417void SelectionDAGISel::ComputeLiveOutVRegInfo() {
Chris Lattner68068cc2008-06-17 06:09:18 +0000418 SmallPtrSet<SDNode*, 128> VisitedNodes;
419 SmallVector<SDNode*, 128> Worklist;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000420
Gabor Greif1c80d112008-08-28 21:40:38 +0000421 Worklist.push_back(CurDAG->getRoot().getNode());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000422
Chris Lattner68068cc2008-06-17 06:09:18 +0000423 APInt Mask;
424 APInt KnownZero;
425 APInt KnownOne;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000426
Chris Lattner68068cc2008-06-17 06:09:18 +0000427 while (!Worklist.empty()) {
428 SDNode *N = Worklist.back();
429 Worklist.pop_back();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000430
Chris Lattner68068cc2008-06-17 06:09:18 +0000431 // If we've already seen this node, ignore it.
432 if (!VisitedNodes.insert(N))
433 continue;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000434
Chris Lattner68068cc2008-06-17 06:09:18 +0000435 // Otherwise, add all chain operands to the worklist.
436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000437 if (N->getOperand(i).getValueType() == MVT::Other)
Gabor Greif1c80d112008-08-28 21:40:38 +0000438 Worklist.push_back(N->getOperand(i).getNode());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000439
Chris Lattner68068cc2008-06-17 06:09:18 +0000440 // If this is a CopyToReg with a vreg dest, process it.
441 if (N->getOpcode() != ISD::CopyToReg)
442 continue;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000443
Chris Lattner68068cc2008-06-17 06:09:18 +0000444 unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
445 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
446 continue;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000447
Chris Lattner68068cc2008-06-17 06:09:18 +0000448 // Ignore non-scalar or non-integer values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000449 SDValue Src = N->getOperand(2);
Owen Andersonac9de032009-08-10 22:56:29 +0000450 EVT SrcVT = Src.getValueType();
Chris Lattner68068cc2008-06-17 06:09:18 +0000451 if (!SrcVT.isInteger() || SrcVT.isVector())
452 continue;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000453
Dan Gohman14a66442008-08-23 02:25:05 +0000454 unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
Chris Lattner68068cc2008-06-17 06:09:18 +0000455 Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits());
Dan Gohman14a66442008-08-23 02:25:05 +0000456 CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000457
Chris Lattner68068cc2008-06-17 06:09:18 +0000458 // Only install this information if it tells us something.
459 if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) {
460 DestReg -= TargetRegisterInfo::FirstVirtualRegister;
Dan Gohmanfbd88292009-08-01 03:51:09 +0000461 if (DestReg >= FuncInfo->LiveOutRegInfo.size())
462 FuncInfo->LiveOutRegInfo.resize(DestReg+1);
463 FunctionLoweringInfo::LiveOutInfo &LOI =
464 FuncInfo->LiveOutRegInfo[DestReg];
Chris Lattner68068cc2008-06-17 06:09:18 +0000465 LOI.NumSignBits = NumSignBits;
Dan Gohmand38e97f2009-03-27 23:55:04 +0000466 LOI.KnownOne = KnownOne;
467 LOI.KnownZero = KnownZero;
Chris Lattner68068cc2008-06-17 06:09:18 +0000468 }
469 }
470}
471
Dan Gohman14a66442008-08-23 02:25:05 +0000472void SelectionDAGISel::CodeGenAndEmitDAG() {
Dan Gohmanb552df72008-07-21 20:00:07 +0000473 std::string GroupName;
474 if (TimePassesIsEnabled)
475 GroupName = "Instruction Selection and Scheduling";
476 std::string BlockName;
477 if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs ||
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000478 ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs ||
479 ViewSUnitDAGs)
Dan Gohmanfbd88292009-08-01 03:51:09 +0000480 BlockName = MF->getFunction()->getNameStr() + ":" +
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000481 BB->getBasicBlock()->getNameStr();
Dan Gohmanb552df72008-07-21 20:00:07 +0000482
Chris Lattner2b40c562009-08-23 06:35:02 +0000483 DEBUG(errs() << "Initial selection DAG:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000484 DEBUG(CurDAG->dump());
Dan Gohmanb552df72008-07-21 20:00:07 +0000485
Dan Gohman14a66442008-08-23 02:25:05 +0000486 if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName);
Dan Gohmaneebf44e2007-10-08 15:12:17 +0000487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 // Run the DAG combiner in pre-legalize mode.
Evan Cheng19733c42008-07-01 17:59:20 +0000489 if (TimePassesIsEnabled) {
Dan Gohman368a08b2008-07-14 18:19:29 +0000490 NamedRegionTimer T("DAG Combining 1", GroupName);
Bill Wendling58ed5d22009-04-29 00:15:41 +0000491 CurDAG->Combine(Unrestricted, *AA, OptLevel);
Evan Cheng19733c42008-07-01 17:59:20 +0000492 } else {
Bill Wendling58ed5d22009-04-29 00:15:41 +0000493 CurDAG->Combine(Unrestricted, *AA, OptLevel);
Evan Cheng19733c42008-07-01 17:59:20 +0000494 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000495
Chris Lattner2b40c562009-08-23 06:35:02 +0000496 DEBUG(errs() << "Optimized lowered selection DAG:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000497 DEBUG(CurDAG->dump());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000498
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 // Second step, hack on the DAG until it only uses operations and types that
500 // the target supports.
Duncan Sandsb3ac3882008-10-27 08:42:46 +0000501 if (!DisableLegalizeTypes) {
Dan Gohman14a66442008-08-23 02:25:05 +0000502 if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " +
503 BlockName);
Dan Gohmanb552df72008-07-21 20:00:07 +0000504
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000505 bool Changed;
Dan Gohmanb552df72008-07-21 20:00:07 +0000506 if (TimePassesIsEnabled) {
507 NamedRegionTimer T("Type Legalization", GroupName);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000508 Changed = CurDAG->LegalizeTypes();
Dan Gohmanb552df72008-07-21 20:00:07 +0000509 } else {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000510 Changed = CurDAG->LegalizeTypes();
Dan Gohmanb552df72008-07-21 20:00:07 +0000511 }
512
Chris Lattner2b40c562009-08-23 06:35:02 +0000513 DEBUG(errs() << "Type-legalized selection DAG:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000514 DEBUG(CurDAG->dump());
Dan Gohmanb552df72008-07-21 20:00:07 +0000515
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000516 if (Changed) {
517 if (ViewDAGCombineLT)
518 CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
519
520 // Run the DAG combiner in post-type-legalize mode.
521 if (TimePassesIsEnabled) {
522 NamedRegionTimer T("DAG Combining after legalize types", GroupName);
Bill Wendling58ed5d22009-04-29 00:15:41 +0000523 CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000524 } else {
Bill Wendling58ed5d22009-04-29 00:15:41 +0000525 CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000526 }
527
Chris Lattner2b40c562009-08-23 06:35:02 +0000528 DEBUG(errs() << "Optimized type-legalized selection DAG:\n");
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000529 DEBUG(CurDAG->dump());
530 }
Eli Friedmane7cf6a82009-05-23 12:35:30 +0000531
532 if (TimePassesIsEnabled) {
533 NamedRegionTimer T("Vector Legalization", GroupName);
534 Changed = CurDAG->LegalizeVectors();
535 } else {
536 Changed = CurDAG->LegalizeVectors();
537 }
538
539 if (Changed) {
540 if (TimePassesIsEnabled) {
541 NamedRegionTimer T("Type Legalization 2", GroupName);
542 Changed = CurDAG->LegalizeTypes();
543 } else {
544 Changed = CurDAG->LegalizeTypes();
545 }
546
547 if (ViewDAGCombineLT)
548 CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
549
550 // Run the DAG combiner in post-type-legalize mode.
551 if (TimePassesIsEnabled) {
552 NamedRegionTimer T("DAG Combining after legalize vectors", GroupName);
553 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
554 } else {
555 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
556 }
557
Chris Lattner2b40c562009-08-23 06:35:02 +0000558 DEBUG(errs() << "Optimized vector-legalized selection DAG:\n");
Eli Friedmane7cf6a82009-05-23 12:35:30 +0000559 DEBUG(CurDAG->dump());
560 }
Chris Lattnerb29a6a42008-07-10 23:37:50 +0000561 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000562
Dan Gohman14a66442008-08-23 02:25:05 +0000563 if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName);
Dan Gohmanb552df72008-07-21 20:00:07 +0000564
Evan Cheng19733c42008-07-01 17:59:20 +0000565 if (TimePassesIsEnabled) {
Dan Gohman368a08b2008-07-14 18:19:29 +0000566 NamedRegionTimer T("DAG Legalization", GroupName);
Bill Wendling58ed5d22009-04-29 00:15:41 +0000567 CurDAG->Legalize(DisableLegalizeTypes, OptLevel);
Evan Cheng19733c42008-07-01 17:59:20 +0000568 } else {
Bill Wendling58ed5d22009-04-29 00:15:41 +0000569 CurDAG->Legalize(DisableLegalizeTypes, OptLevel);
Evan Cheng19733c42008-07-01 17:59:20 +0000570 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000571
Chris Lattner2b40c562009-08-23 06:35:02 +0000572 DEBUG(errs() << "Legalized selection DAG:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000573 DEBUG(CurDAG->dump());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000574
Dan Gohman14a66442008-08-23 02:25:05 +0000575 if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName);
Dan Gohmanb552df72008-07-21 20:00:07 +0000576
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 // Run the DAG combiner in post-legalize mode.
Evan Cheng19733c42008-07-01 17:59:20 +0000578 if (TimePassesIsEnabled) {
Dan Gohman368a08b2008-07-14 18:19:29 +0000579 NamedRegionTimer T("DAG Combining 2", GroupName);
Bill Wendling58ed5d22009-04-29 00:15:41 +0000580 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
Evan Cheng19733c42008-07-01 17:59:20 +0000581 } else {
Bill Wendling58ed5d22009-04-29 00:15:41 +0000582 CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
Evan Cheng19733c42008-07-01 17:59:20 +0000583 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000584
Chris Lattner2b40c562009-08-23 06:35:02 +0000585 DEBUG(errs() << "Optimized legalized selection DAG:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000586 DEBUG(CurDAG->dump());
Dan Gohmaneebf44e2007-10-08 15:12:17 +0000587
Dan Gohman14a66442008-08-23 02:25:05 +0000588 if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000589
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000590 if (OptLevel != CodeGenOpt::None)
Dan Gohman14a66442008-08-23 02:25:05 +0000591 ComputeLiveOutVRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592
593 // Third, instruction select all of the operations to machine code, adding the
594 // code to the MachineBasicBlock.
Evan Cheng19733c42008-07-01 17:59:20 +0000595 if (TimePassesIsEnabled) {
Dan Gohman368a08b2008-07-14 18:19:29 +0000596 NamedRegionTimer T("Instruction Selection", GroupName);
Dan Gohman14a66442008-08-23 02:25:05 +0000597 InstructionSelect();
Evan Cheng19733c42008-07-01 17:59:20 +0000598 } else {
Dan Gohman14a66442008-08-23 02:25:05 +0000599 InstructionSelect();
Evan Cheng19733c42008-07-01 17:59:20 +0000600 }
Evan Cheng34fd4f32008-06-30 20:45:06 +0000601
Chris Lattner2b40c562009-08-23 06:35:02 +0000602 DEBUG(errs() << "Selected selection DAG:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000603 DEBUG(CurDAG->dump());
Dan Gohmanb552df72008-07-21 20:00:07 +0000604
Dan Gohman14a66442008-08-23 02:25:05 +0000605 if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName);
Dan Gohmanb552df72008-07-21 20:00:07 +0000606
Dan Gohman368a08b2008-07-14 18:19:29 +0000607 // Schedule machine code.
Dan Gohman7c968a82009-02-11 04:27:20 +0000608 ScheduleDAGSDNodes *Scheduler = CreateScheduler();
Dan Gohman368a08b2008-07-14 18:19:29 +0000609 if (TimePassesIsEnabled) {
610 NamedRegionTimer T("Instruction Scheduling", GroupName);
Dan Gohman7c968a82009-02-11 04:27:20 +0000611 Scheduler->Run(CurDAG, BB, BB->end());
Dan Gohman368a08b2008-07-14 18:19:29 +0000612 } else {
Dan Gohman7c968a82009-02-11 04:27:20 +0000613 Scheduler->Run(CurDAG, BB, BB->end());
Dan Gohman368a08b2008-07-14 18:19:29 +0000614 }
615
Dan Gohmanb552df72008-07-21 20:00:07 +0000616 if (ViewSUnitDAGs) Scheduler->viewGraph();
617
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000618 // Emit machine code to BB. This can change 'BB' to the last block being
Evan Cheng34fd4f32008-06-30 20:45:06 +0000619 // inserted into.
Evan Cheng19733c42008-07-01 17:59:20 +0000620 if (TimePassesIsEnabled) {
Dan Gohman368a08b2008-07-14 18:19:29 +0000621 NamedRegionTimer T("Instruction Creation", GroupName);
Dan Gohman7ec20512009-11-23 18:04:58 +0000622 BB = Scheduler->EmitSchedule(&SDB->EdgeMapping);
Evan Cheng19733c42008-07-01 17:59:20 +0000623 } else {
Dan Gohman7ec20512009-11-23 18:04:58 +0000624 BB = Scheduler->EmitSchedule(&SDB->EdgeMapping);
Dan Gohman368a08b2008-07-14 18:19:29 +0000625 }
626
627 // Free the scheduler state.
628 if (TimePassesIsEnabled) {
629 NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName);
630 delete Scheduler;
631 } else {
632 delete Scheduler;
Evan Cheng19733c42008-07-01 17:59:20 +0000633 }
Evan Cheng34fd4f32008-06-30 20:45:06 +0000634
Chris Lattner2b40c562009-08-23 06:35:02 +0000635 DEBUG(errs() << "Selected machine code:\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 DEBUG(BB->dump());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000637}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638
Dan Gohman96eb47a2009-01-15 19:20:50 +0000639void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
640 MachineFunction &MF,
Dan Gohman9dd43582008-10-14 23:54:11 +0000641 MachineModuleInfo *MMI,
Devang Patelfcf1c752009-01-13 00:35:13 +0000642 DwarfWriter *DW,
Dan Gohman9dd43582008-10-14 23:54:11 +0000643 const TargetInstrInfo &TII) {
Dan Gohman9d7bc422008-09-29 21:55:50 +0000644 // Initialize the Fast-ISel state, if needed.
645 FastISel *FastIS = 0;
646 if (EnableFastISel)
Dan Gohman96eb47a2009-01-15 19:20:50 +0000647 FastIS = TLI.createFastISel(MF, MMI, DW,
Dan Gohman9d7bc422008-09-29 21:55:50 +0000648 FuncInfo->ValueMap,
649 FuncInfo->MBBMap,
Dan Gohman9dd43582008-10-14 23:54:11 +0000650 FuncInfo->StaticAllocaMap
651#ifndef NDEBUG
652 , FuncInfo->CatchInfoLost
653#endif
654 );
Dan Gohman9d7bc422008-09-29 21:55:50 +0000655
Devang Patel6de78e22009-09-28 21:41:20 +0000656 MetadataContext &TheMetadata = Fn.getContext().getMetadata();
Devang Patelb5896162009-09-28 21:14:55 +0000657 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
Devang Patela0fbb002009-09-16 20:39:11 +0000658
Dan Gohman9d7bc422008-09-29 21:55:50 +0000659 // Iterate over all basic blocks in the function.
Evan Cheng61828a82008-08-07 00:43:25 +0000660 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
661 BasicBlock *LLVMBB = &*I;
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000662 BB = FuncInfo->MBBMap[LLVMBB];
Dan Gohman14a66442008-08-23 02:25:05 +0000663
Dan Gohmanca4857a2008-09-03 23:12:08 +0000664 BasicBlock::iterator const Begin = LLVMBB->begin();
665 BasicBlock::iterator const End = LLVMBB->end();
Evan Chenge9d9a162008-09-08 16:01:27 +0000666 BasicBlock::iterator BI = Begin;
Dan Gohman2c44e9a2008-08-28 20:28:56 +0000667
668 // Lower any arguments needed in this block if this is the entry block.
Dan Gohman78ae76d2008-09-25 17:05:24 +0000669 bool SuppressFastISel = false;
670 if (LLVMBB == &Fn.getEntryBlock()) {
Dan Gohman2c44e9a2008-08-28 20:28:56 +0000671 LowerArguments(LLVMBB);
Dan Gohman14a66442008-08-23 02:25:05 +0000672
Dan Gohman78ae76d2008-09-25 17:05:24 +0000673 // If any of the arguments has the byval attribute, forgo
674 // fast-isel in the entry block.
Dan Gohman9d7bc422008-09-29 21:55:50 +0000675 if (FastIS) {
Dan Gohman78ae76d2008-09-25 17:05:24 +0000676 unsigned j = 1;
677 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
678 I != E; ++I, ++j)
Devang Pateld222f862008-09-25 21:00:45 +0000679 if (Fn.paramHasAttr(j, Attribute::ByVal)) {
Dan Gohman92902e82008-09-25 17:21:42 +0000680 if (EnableFastISelVerbose || EnableFastISelAbort)
Chris Lattner36eef822009-08-23 07:05:07 +0000681 errs() << "FastISel skips entry block due to byval argument\n";
Dan Gohman78ae76d2008-09-25 17:05:24 +0000682 SuppressFastISel = true;
683 break;
684 }
685 }
686 }
687
Dan Gohman9dd43582008-10-14 23:54:11 +0000688 if (MMI && BB->isLandingPad()) {
689 // Add a label to mark the beginning of the landing pad. Deletion of the
690 // landing pad can thus be detected via the MachineModuleInfo.
691 unsigned LabelID = MMI->addLandingPad(BB);
692
693 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EH_LABEL);
Dan Gohman7ec20512009-11-23 18:04:58 +0000694 BuildMI(BB, SDB->getCurDebugLoc(), II).addImm(LabelID);
Dan Gohman9dd43582008-10-14 23:54:11 +0000695
696 // Mark exception register as live in.
697 unsigned Reg = TLI.getExceptionAddressRegister();
698 if (Reg) BB->addLiveIn(Reg);
699
700 // Mark exception selector register as live in.
701 Reg = TLI.getExceptionSelectorRegister();
702 if (Reg) BB->addLiveIn(Reg);
703
704 // FIXME: Hack around an exception handling flaw (PR1508): the personality
705 // function and list of typeids logically belong to the invoke (or, if you
706 // like, the basic block containing the invoke), and need to be associated
707 // with it in the dwarf exception handling tables. Currently however the
708 // information is provided by an intrinsic (eh.selector) that can be moved
709 // to unexpected places by the optimizers: if the unwind edge is critical,
710 // then breaking it can result in the intrinsics being in the successor of
711 // the landing pad, not the landing pad itself. This results in exceptions
712 // not being caught because no typeids are associated with the invoke.
713 // This may not be the only way things can go wrong, but it is the only way
714 // we try to work around for the moment.
715 BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator());
716
717 if (Br && Br->isUnconditional()) { // Critical edge?
718 BasicBlock::iterator I, E;
719 for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I)
720 if (isa<EHSelectorInst>(I))
721 break;
722
723 if (I == E)
724 // No catch info found - try to extract some from the successor.
Dan Gohman7d3b3e42009-11-23 18:12:11 +0000725 CopyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, *FuncInfo);
Dan Gohman9dd43582008-10-14 23:54:11 +0000726 }
727 }
728
Dan Gohman14a66442008-08-23 02:25:05 +0000729 // Before doing SelectionDAG ISel, see if FastISel has been requested.
Dan Gohman9dd43582008-10-14 23:54:11 +0000730 if (FastIS && !SuppressFastISel) {
Dan Gohman9d7bc422008-09-29 21:55:50 +0000731 // Emit code for any incoming arguments. This must happen before
732 // beginning FastISel on the entry block.
733 if (LLVMBB == &Fn.getEntryBlock()) {
Dan Gohman7ec20512009-11-23 18:04:58 +0000734 CurDAG->setRoot(SDB->getControlRoot());
Dan Gohman9d7bc422008-09-29 21:55:50 +0000735 CodeGenAndEmitDAG();
Dan Gohman7ec20512009-11-23 18:04:58 +0000736 SDB->clear();
Dan Gohman9d7bc422008-09-29 21:55:50 +0000737 }
Dan Gohman2764e172008-10-04 00:56:36 +0000738 FastIS->startNewBlock(BB);
Dan Gohman9d7bc422008-09-29 21:55:50 +0000739 // Do FastISel on as many instructions as possible.
740 for (; BI != End; ++BI) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000741 if (MDDbgKind) {
742 // Update DebugLoc if debug information is attached with this
743 // instruction.
Devang Patel90a0fe32009-11-10 23:06:00 +0000744 if (!isa<DbgInfoIntrinsic>(BI))
745 if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, BI)) {
746 DILocation DILoc(Dbg);
747 DebugLoc Loc = ExtractDebugLocation(DILoc,
748 MF.getDebugLocInfo());
749 FastIS->setCurDebugLoc(Loc);
750 if (MF.getDefaultDebugLoc().isUnknown())
751 MF.setDefaultDebugLoc(Loc);
752 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000753 }
Devang Patela0fbb002009-09-16 20:39:11 +0000754
Dan Gohman9d7bc422008-09-29 21:55:50 +0000755 // Just before the terminator instruction, insert instructions to
756 // feed PHI nodes in successor blocks.
757 if (isa<TerminatorInst>(BI))
758 if (!HandlePHINodesInSuccessorBlocksFast(LLVMBB, FastIS)) {
Dan Gohmanee70e1d2008-09-09 23:05:00 +0000759 if (EnableFastISelVerbose || EnableFastISelAbort) {
Chris Lattner36eef822009-08-23 07:05:07 +0000760 errs() << "FastISel miss: ";
Dan Gohman69740ec2008-09-09 22:06:46 +0000761 BI->dump();
762 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000763 assert(!EnableFastISelAbort &&
Edwin Török280d15b2009-07-12 20:07:01 +0000764 "FastISel didn't handle a PHI in a successor");
Dan Gohman9d7bc422008-09-29 21:55:50 +0000765 break;
Dan Gohman14a66442008-08-23 02:25:05 +0000766 }
Dan Gohman9d7bc422008-09-29 21:55:50 +0000767
768 // First try normal tablegen-generated "fast" selection.
769 if (FastIS->SelectInstruction(BI))
770 continue;
771
772 // Next, try calling the target to attempt to handle the instruction.
773 if (FastIS->TargetSelectInstruction(BI))
774 continue;
775
776 // Then handle certain instructions as single-LLVM-Instruction blocks.
777 if (isa<CallInst>(BI)) {
778 if (EnableFastISelVerbose || EnableFastISelAbort) {
Chris Lattner36eef822009-08-23 07:05:07 +0000779 errs() << "FastISel missed call: ";
Dan Gohman9d7bc422008-09-29 21:55:50 +0000780 BI->dump();
781 }
782
Owen Anderson35b47072009-08-13 21:58:54 +0000783 if (BI->getType() != Type::getVoidTy(*CurDAG->getContext())) {
Dan Gohman9d7bc422008-09-29 21:55:50 +0000784 unsigned &R = FuncInfo->ValueMap[BI];
785 if (!R)
786 R = FuncInfo->CreateRegForValue(BI);
787 }
788
Dan Gohman7ec20512009-11-23 18:04:58 +0000789 SDB->setCurDebugLoc(FastIS->getCurDebugLoc());
Dan Gohman40b0a2e2009-11-20 02:51:26 +0000790
791 bool HadTailCall = false;
Chris Lattnerb44b4292009-12-03 00:50:42 +0000792 SelectBasicBlock(LLVMBB, BI, llvm::next(BI), HadTailCall);
Dan Gohman40b0a2e2009-11-20 02:51:26 +0000793
794 // If the call was emitted as a tail call, we're done with the block.
795 if (HadTailCall) {
796 BI = End;
797 break;
798 }
799
Dan Gohman2764e172008-10-04 00:56:36 +0000800 // If the instruction was codegen'd with multiple blocks,
801 // inform the FastISel object where to resume inserting.
802 FastIS->setCurrentBlock(BB);
Dan Gohman9d7bc422008-09-29 21:55:50 +0000803 continue;
Dan Gohman14a66442008-08-23 02:25:05 +0000804 }
Dan Gohman9d7bc422008-09-29 21:55:50 +0000805
806 // Otherwise, give up on FastISel for the rest of the block.
807 // For now, be a little lenient about non-branch terminators.
808 if (!isa<TerminatorInst>(BI) || isa<BranchInst>(BI)) {
809 if (EnableFastISelVerbose || EnableFastISelAbort) {
Chris Lattner36eef822009-08-23 07:05:07 +0000810 errs() << "FastISel miss: ";
Dan Gohman9d7bc422008-09-29 21:55:50 +0000811 BI->dump();
812 }
813 if (EnableFastISelAbort)
814 // The "fast" selector couldn't handle something and bailed.
815 // For the purpose of debugging, just abort.
Edwin Törökbd448e32009-07-14 16:55:14 +0000816 llvm_unreachable("FastISel didn't select the entire block");
Dan Gohman9d7bc422008-09-29 21:55:50 +0000817 }
818 break;
Dan Gohman14a66442008-08-23 02:25:05 +0000819 }
820 }
821
Dan Gohman61a1f1c2008-09-02 20:17:56 +0000822 // Run SelectionDAG instruction selection on the remainder of the block
823 // not handled by FastISel. If FastISel is not run, this is the entire
Dan Gohmanca4857a2008-09-03 23:12:08 +0000824 // block.
Devang Patel6bfb2052009-04-16 01:33:10 +0000825 if (BI != End) {
826 // If FastISel is run and it has known DebugLoc then use it.
827 if (FastIS && !FastIS->getCurDebugLoc().isUnknown())
Dan Gohman7ec20512009-11-23 18:04:58 +0000828 SDB->setCurDebugLoc(FastIS->getCurDebugLoc());
Dan Gohman40b0a2e2009-11-20 02:51:26 +0000829 bool HadTailCall;
830 SelectBasicBlock(LLVMBB, BI, End, HadTailCall);
Devang Patel6bfb2052009-04-16 01:33:10 +0000831 }
Dan Gohman14a66442008-08-23 02:25:05 +0000832
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000833 FinishBasicBlock();
Evan Cheng61828a82008-08-07 00:43:25 +0000834 }
Dan Gohman9d7bc422008-09-29 21:55:50 +0000835
836 delete FastIS;
Dan Gohmaned825d12008-07-07 23:02:41 +0000837}
838
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000839void
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000840SelectionDAGISel::FinishBasicBlock() {
Dan Gohman14a66442008-08-23 02:25:05 +0000841
Chris Lattner2b40c562009-08-23 06:35:02 +0000842 DEBUG(errs() << "Target-post-processed machine code:\n");
Dan Gohman14a66442008-08-23 02:25:05 +0000843 DEBUG(BB->dump());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844
Chris Lattner2b40c562009-08-23 06:35:02 +0000845 DEBUG(errs() << "Total amount of phi nodes to update: "
Dan Gohman7ec20512009-11-23 18:04:58 +0000846 << SDB->PHINodesToUpdate.size() << "\n");
847 DEBUG(for (unsigned i = 0, e = SDB->PHINodesToUpdate.size(); i != e; ++i)
Chris Lattner2b40c562009-08-23 06:35:02 +0000848 errs() << "Node " << i << " : ("
Dan Gohman7ec20512009-11-23 18:04:58 +0000849 << SDB->PHINodesToUpdate[i].first
850 << ", " << SDB->PHINodesToUpdate[i].second << ")\n");
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000851
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 // Next, now that we know what the last MBB the LLVM BB expanded is, update
853 // PHI nodes in successors.
Dan Gohman7ec20512009-11-23 18:04:58 +0000854 if (SDB->SwitchCases.empty() &&
855 SDB->JTCases.empty() &&
856 SDB->BitTestCases.empty()) {
857 for (unsigned i = 0, e = SDB->PHINodesToUpdate.size(); i != e; ++i) {
858 MachineInstr *PHI = SDB->PHINodesToUpdate[i].first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
860 "This is not a machine PHI node that we are updating!");
Dan Gohman7ec20512009-11-23 18:04:58 +0000861 PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[i].second,
Chris Lattnere44906f2007-12-30 00:57:42 +0000862 false));
863 PHI->addOperand(MachineOperand::CreateMBB(BB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 }
Dan Gohman7ec20512009-11-23 18:04:58 +0000865 SDB->PHINodesToUpdate.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 return;
867 }
868
Dan Gohman7ec20512009-11-23 18:04:58 +0000869 for (unsigned i = 0, e = SDB->BitTestCases.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 // Lower header first, if it wasn't already lowered
Dan Gohman7ec20512009-11-23 18:04:58 +0000871 if (!SDB->BitTestCases[i].Emitted) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman7ec20512009-11-23 18:04:58 +0000873 BB = SDB->BitTestCases[i].Parent;
874 SDB->setCurrentBasicBlock(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 // Emit the code
Dan Gohman7ec20512009-11-23 18:04:58 +0000876 SDB->visitBitTestHeader(SDB->BitTestCases[i]);
877 CurDAG->setRoot(SDB->getRoot());
Dan Gohman14a66442008-08-23 02:25:05 +0000878 CodeGenAndEmitDAG();
Dan Gohman7ec20512009-11-23 18:04:58 +0000879 SDB->clear();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000880 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881
Dan Gohman7ec20512009-11-23 18:04:58 +0000882 for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size(); j != ej; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman7ec20512009-11-23 18:04:58 +0000884 BB = SDB->BitTestCases[i].Cases[j].ThisBB;
885 SDB->setCurrentBasicBlock(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 // Emit the code
887 if (j+1 != ej)
Dan Gohman7ec20512009-11-23 18:04:58 +0000888 SDB->visitBitTestCase(SDB->BitTestCases[i].Cases[j+1].ThisBB,
889 SDB->BitTestCases[i].Reg,
890 SDB->BitTestCases[i].Cases[j]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 else
Dan Gohman7ec20512009-11-23 18:04:58 +0000892 SDB->visitBitTestCase(SDB->BitTestCases[i].Default,
893 SDB->BitTestCases[i].Reg,
894 SDB->BitTestCases[i].Cases[j]);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000895
896
Dan Gohman7ec20512009-11-23 18:04:58 +0000897 CurDAG->setRoot(SDB->getRoot());
Dan Gohman14a66442008-08-23 02:25:05 +0000898 CodeGenAndEmitDAG();
Dan Gohman7ec20512009-11-23 18:04:58 +0000899 SDB->clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 }
901
902 // Update PHI Nodes
Dan Gohman7ec20512009-11-23 18:04:58 +0000903 for (unsigned pi = 0, pe = SDB->PHINodesToUpdate.size(); pi != pe; ++pi) {
904 MachineInstr *PHI = SDB->PHINodesToUpdate[pi].first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 MachineBasicBlock *PHIBB = PHI->getParent();
906 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
907 "This is not a machine PHI node that we are updating!");
908 // This is "default" BB. We have two jumps to it. From "header" BB and
909 // from last "case" BB.
Dan Gohman7ec20512009-11-23 18:04:58 +0000910 if (PHIBB == SDB->BitTestCases[i].Default) {
911 PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second,
Chris Lattnere44906f2007-12-30 00:57:42 +0000912 false));
Dan Gohman7ec20512009-11-23 18:04:58 +0000913 PHI->addOperand(MachineOperand::CreateMBB(SDB->BitTestCases[i].Parent));
914 PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second,
Chris Lattnere44906f2007-12-30 00:57:42 +0000915 false));
Dan Gohman7ec20512009-11-23 18:04:58 +0000916 PHI->addOperand(MachineOperand::CreateMBB(SDB->BitTestCases[i].Cases.
Chris Lattnere44906f2007-12-30 00:57:42 +0000917 back().ThisBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 }
919 // One of "cases" BB.
Dan Gohman7ec20512009-11-23 18:04:58 +0000920 for (unsigned j = 0, ej = SDB->BitTestCases[i].Cases.size();
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000921 j != ej; ++j) {
Dan Gohman7ec20512009-11-23 18:04:58 +0000922 MachineBasicBlock* cBB = SDB->BitTestCases[i].Cases[j].ThisBB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 if (cBB->succ_end() !=
924 std::find(cBB->succ_begin(),cBB->succ_end(), PHIBB)) {
Dan Gohman7ec20512009-11-23 18:04:58 +0000925 PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second,
Chris Lattnere44906f2007-12-30 00:57:42 +0000926 false));
927 PHI->addOperand(MachineOperand::CreateMBB(cBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 }
929 }
930 }
931 }
Dan Gohman7ec20512009-11-23 18:04:58 +0000932 SDB->BitTestCases.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933
934 // If the JumpTable record is filled in, then we need to emit a jump table.
935 // Updating the PHI nodes is tricky in this case, since we need to determine
936 // whether the PHI is a successor of the range check MBB or the jump table MBB
Dan Gohman7ec20512009-11-23 18:04:58 +0000937 for (unsigned i = 0, e = SDB->JTCases.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 // Lower header first, if it wasn't already lowered
Dan Gohman7ec20512009-11-23 18:04:58 +0000939 if (!SDB->JTCases[i].first.Emitted) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman7ec20512009-11-23 18:04:58 +0000941 BB = SDB->JTCases[i].first.HeaderBB;
942 SDB->setCurrentBasicBlock(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 // Emit the code
Dan Gohman7ec20512009-11-23 18:04:58 +0000944 SDB->visitJumpTableHeader(SDB->JTCases[i].second, SDB->JTCases[i].first);
945 CurDAG->setRoot(SDB->getRoot());
Dan Gohman14a66442008-08-23 02:25:05 +0000946 CodeGenAndEmitDAG();
Dan Gohman7ec20512009-11-23 18:04:58 +0000947 SDB->clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000949
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman7ec20512009-11-23 18:04:58 +0000951 BB = SDB->JTCases[i].second.MBB;
952 SDB->setCurrentBasicBlock(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 // Emit the code
Dan Gohman7ec20512009-11-23 18:04:58 +0000954 SDB->visitJumpTable(SDB->JTCases[i].second);
955 CurDAG->setRoot(SDB->getRoot());
Dan Gohman14a66442008-08-23 02:25:05 +0000956 CodeGenAndEmitDAG();
Dan Gohman7ec20512009-11-23 18:04:58 +0000957 SDB->clear();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000958
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 // Update PHI Nodes
Dan Gohman7ec20512009-11-23 18:04:58 +0000960 for (unsigned pi = 0, pe = SDB->PHINodesToUpdate.size(); pi != pe; ++pi) {
961 MachineInstr *PHI = SDB->PHINodesToUpdate[pi].first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 MachineBasicBlock *PHIBB = PHI->getParent();
963 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
964 "This is not a machine PHI node that we are updating!");
965 // "default" BB. We can go there only from header BB.
Dan Gohman7ec20512009-11-23 18:04:58 +0000966 if (PHIBB == SDB->JTCases[i].second.Default) {
Evan Cheng5f3a5402009-09-19 09:51:03 +0000967 PHI->addOperand
Dan Gohman7ec20512009-11-23 18:04:58 +0000968 (MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second, false));
Evan Cheng5f3a5402009-09-19 09:51:03 +0000969 PHI->addOperand
Dan Gohman7ec20512009-11-23 18:04:58 +0000970 (MachineOperand::CreateMBB(SDB->JTCases[i].first.HeaderBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 }
972 // JT BB. Just iterate over successors here
973 if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) {
Evan Cheng5f3a5402009-09-19 09:51:03 +0000974 PHI->addOperand
Dan Gohman7ec20512009-11-23 18:04:58 +0000975 (MachineOperand::CreateReg(SDB->PHINodesToUpdate[pi].second, false));
Chris Lattnere44906f2007-12-30 00:57:42 +0000976 PHI->addOperand(MachineOperand::CreateMBB(BB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 }
978 }
979 }
Dan Gohman7ec20512009-11-23 18:04:58 +0000980 SDB->JTCases.clear();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000981
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 // If the switch block involved a branch to one of the actual successors, we
983 // need to update PHI nodes in that block.
Dan Gohman7ec20512009-11-23 18:04:58 +0000984 for (unsigned i = 0, e = SDB->PHINodesToUpdate.size(); i != e; ++i) {
985 MachineInstr *PHI = SDB->PHINodesToUpdate[i].first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
987 "This is not a machine PHI node that we are updating!");
988 if (BB->isSuccessor(PHI->getParent())) {
Dan Gohman7ec20512009-11-23 18:04:58 +0000989 PHI->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[i].second,
Chris Lattnere44906f2007-12-30 00:57:42 +0000990 false));
991 PHI->addOperand(MachineOperand::CreateMBB(BB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 }
993 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000994
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 // If we generated any switch lowering information, build and codegen any
996 // additional DAGs necessary.
Dan Gohman7ec20512009-11-23 18:04:58 +0000997 for (unsigned i = 0, e = SDB->SwitchCases.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 // Set the current basic block to the mbb we wish to insert the code into
Dan Gohman7ec20512009-11-23 18:04:58 +0000999 MachineBasicBlock *ThisBB = BB = SDB->SwitchCases[i].ThisBB;
1000 SDB->setCurrentBasicBlock(BB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 // Emit the code
Dan Gohman7ec20512009-11-23 18:04:58 +00001003 SDB->visitSwitchCase(SDB->SwitchCases[i]);
1004 CurDAG->setRoot(SDB->getRoot());
Dan Gohman14a66442008-08-23 02:25:05 +00001005 CodeGenAndEmitDAG();
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 // Handle any PHI nodes in successors of this chunk, as if we were coming
1008 // from the original BB before switch expansion. Note that PHI nodes can
1009 // occur multiple times in PHINodesToUpdate. We have to be very careful to
1010 // handle them the right number of times.
Dan Gohman7ec20512009-11-23 18:04:58 +00001011 while ((BB = SDB->SwitchCases[i].TrueBB)) { // Handle LHS and RHS.
Evan Chengd7dc9832009-09-18 21:02:19 +00001012 // If new BB's are created during scheduling, the edges may have been
Evan Cheng5f3a5402009-09-19 09:51:03 +00001013 // updated. That is, the edge from ThisBB to BB may have been split and
1014 // BB's predecessor is now another block.
Evan Chengd7dc9832009-09-18 21:02:19 +00001015 DenseMap<MachineBasicBlock*, MachineBasicBlock*>::iterator EI =
Dan Gohman7ec20512009-11-23 18:04:58 +00001016 SDB->EdgeMapping.find(BB);
1017 if (EI != SDB->EdgeMapping.end())
Evan Chengd7dc9832009-09-18 21:02:19 +00001018 ThisBB = EI->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 for (MachineBasicBlock::iterator Phi = BB->begin();
1020 Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){
1021 // This value for this PHI node is recorded in PHINodesToUpdate, get it.
1022 for (unsigned pn = 0; ; ++pn) {
Dan Gohman7ec20512009-11-23 18:04:58 +00001023 assert(pn != SDB->PHINodesToUpdate.size() &&
Dan Gohman0f2d71d2008-08-27 23:52:12 +00001024 "Didn't find PHI entry!");
Dan Gohman7ec20512009-11-23 18:04:58 +00001025 if (SDB->PHINodesToUpdate[pn].first == Phi) {
1026 Phi->addOperand(MachineOperand::CreateReg(SDB->PHINodesToUpdate[pn].
Evan Chengf3233f22009-09-18 08:26:06 +00001027 second, false));
Evan Chengd7dc9832009-09-18 21:02:19 +00001028 Phi->addOperand(MachineOperand::CreateMBB(ThisBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 break;
Evan Chengf3233f22009-09-18 08:26:06 +00001030 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 }
1032 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 // Don't process RHS if same block as LHS.
Dan Gohman7ec20512009-11-23 18:04:58 +00001035 if (BB == SDB->SwitchCases[i].FalseBB)
1036 SDB->SwitchCases[i].FalseBB = 0;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001037
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 // If we haven't handled the RHS, do so now. Otherwise, we're done.
Dan Gohman7ec20512009-11-23 18:04:58 +00001039 SDB->SwitchCases[i].TrueBB = SDB->SwitchCases[i].FalseBB;
1040 SDB->SwitchCases[i].FalseBB = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 }
Dan Gohman7ec20512009-11-23 18:04:58 +00001042 assert(SDB->SwitchCases[i].TrueBB == 0 && SDB->SwitchCases[i].FalseBB == 0);
1043 SDB->clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 }
Dan Gohman7ec20512009-11-23 18:04:58 +00001045 SDB->SwitchCases.clear();
Dan Gohman0f2d71d2008-08-27 23:52:12 +00001046
Dan Gohman7ec20512009-11-23 18:04:58 +00001047 SDB->PHINodesToUpdate.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048}
1049
1050
Dan Gohman29800e22009-02-06 18:26:51 +00001051/// Create the scheduler. If a specific scheduler was specified
1052/// via the SchedulerRegistry, use it, otherwise select the
1053/// one preferred by the target.
Dan Gohman368a08b2008-07-14 18:19:29 +00001054///
Dan Gohman7c968a82009-02-11 04:27:20 +00001055ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 if (!Ctor) {
1059 Ctor = ISHeuristic;
1060 RegisterScheduler::setDefault(Ctor);
1061 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001062
Bill Wendling58ed5d22009-04-29 00:15:41 +00001063 return Ctor(this, OptLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064}
1065
Dan Gohmandd6547d2009-01-15 22:18:12 +00001066ScheduleHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
1067 return new ScheduleHazardRecognizer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068}
1069
1070//===----------------------------------------------------------------------===//
1071// Helper functions used by the generated instruction selector.
1072//===----------------------------------------------------------------------===//
1073// Calls to these methods are generated by tblgen.
1074
1075/// CheckAndMask - The isel is trying to match something like (and X, 255). If
1076/// the dag combiner simplified the 255, we still want to match. RHS is the
1077/// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
1078/// specified in the .td file (e.g. 255).
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001079bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmand6098272007-07-24 23:00:27 +00001080 int64_t DesiredMaskS) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001081 const APInt &ActualMask = RHS->getAPIntValue();
1082 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 // If the actual mask exactly matches, success!
1085 if (ActualMask == DesiredMask)
1086 return true;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 // If the actual AND mask is allowing unallowed bits, this doesn't match.
Dan Gohman07961cd2008-02-25 21:11:39 +00001089 if (ActualMask.intersects(~DesiredMask))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 return false;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001091
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 // Otherwise, the DAG Combiner may have proven that the value coming in is
1093 // either already zero or is not demanded. Check for known zero input bits.
Dan Gohman07961cd2008-02-25 21:11:39 +00001094 APInt NeededMask = DesiredMask & ~ActualMask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095 if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
1096 return true;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 // TODO: check to see if missing bits are just not demanded.
1099
1100 // Otherwise, this pattern doesn't match.
1101 return false;
1102}
1103
1104/// CheckOrMask - The isel is trying to match something like (or X, 255). If
1105/// the dag combiner simplified the 255, we still want to match. RHS is the
1106/// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
1107/// specified in the .td file (e.g. 255).
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001108bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohman07961cd2008-02-25 21:11:39 +00001109 int64_t DesiredMaskS) const {
1110 const APInt &ActualMask = RHS->getAPIntValue();
1111 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001112
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 // If the actual mask exactly matches, success!
1114 if (ActualMask == DesiredMask)
1115 return true;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 // If the actual AND mask is allowing unallowed bits, this doesn't match.
Dan Gohman07961cd2008-02-25 21:11:39 +00001118 if (ActualMask.intersects(~DesiredMask))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 return false;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001120
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 // Otherwise, the DAG Combiner may have proven that the value coming in is
1122 // either already zero or is not demanded. Check for known zero input bits.
Dan Gohman07961cd2008-02-25 21:11:39 +00001123 APInt NeededMask = DesiredMask & ~ActualMask;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001124
Dan Gohman07961cd2008-02-25 21:11:39 +00001125 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001127
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 // If all the missing bits in the or are already known to be set, match!
1129 if ((NeededMask & KnownOne) == NeededMask)
1130 return true;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 // TODO: check to see if missing bits are just not demanded.
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001133
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 // Otherwise, this pattern doesn't match.
1135 return false;
1136}
1137
1138
1139/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
1140/// by tblgen. Others should not call it.
1141void SelectionDAGISel::
Dan Gohman14a66442008-08-23 02:25:05 +00001142SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001143 std::vector<SDValue> InOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 std::swap(InOps, Ops);
1145
1146 Ops.push_back(InOps[0]); // input chain.
1147 Ops.push_back(InOps[1]); // input asm string.
1148
1149 unsigned i = 2, e = InOps.size();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001150 if (InOps[e-1].getValueType() == MVT::Flag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001151 --e; // Don't process a flag operand if it is here.
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001152
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 while (i != e) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001154 unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
Dale Johannesen94464072008-09-24 01:07:17 +00001155 if ((Flags & 7) != 4 /*MEM*/) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 // Just skip over this operand, copying the operands verbatim.
Evan Cheng92167402009-03-20 18:03:34 +00001157 Ops.insert(Ops.end(), InOps.begin()+i,
1158 InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
1159 i += InlineAsm::getNumOperandRegisters(Flags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001160 } else {
Evan Cheng92167402009-03-20 18:03:34 +00001161 assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
1162 "Memory operand with multiple values?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 // Otherwise, this is a memory operand. Ask the target to select it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001164 std::vector<SDValue> SelOps;
Dan Gohman14a66442008-08-23 02:25:05 +00001165 if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001166 llvm_report_error("Could not match memory address. Inline asm"
1167 " failure!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 // Add this to the output node.
Owen Andersonac9de032009-08-10 22:56:29 +00001171 EVT IntPtrTy = TLI.getPointerTy();
Dale Johannesen94464072008-09-24 01:07:17 +00001172 Ops.push_back(CurDAG->getTargetConstant(4/*MEM*/ | (SelOps.size()<< 3),
Dan Gohman14a66442008-08-23 02:25:05 +00001173 IntPtrTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
1175 i += 2;
1176 }
1177 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +00001178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179 // Add the flag input back if present.
1180 if (e != InOps.size())
1181 Ops.push_back(InOps.back());
1182}
1183
Owen Andersonac9de032009-08-10 22:56:29 +00001184/// findFlagUse - Return use of EVT::Flag value produced by the specified
Anton Korobeynikovda76d322009-05-08 18:51:58 +00001185/// SDNode.
1186///
1187static SDNode *findFlagUse(SDNode *N) {
1188 unsigned FlagResNo = N->getNumValues()-1;
1189 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
1190 SDUse &Use = I.getUse();
1191 if (Use.getResNo() == FlagResNo)
1192 return Use.getUser();
1193 }
1194 return NULL;
1195}
1196
1197/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
1198/// This function recursively traverses up the operand chain, ignoring
1199/// certain nodes.
1200static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
1201 SDNode *Root,
1202 SmallPtrSet<SDNode*, 16> &Visited) {
1203 if (Use->getNodeId() < Def->getNodeId() ||
1204 !Visited.insert(Use))
1205 return false;
1206
1207 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
1208 SDNode *N = Use->getOperand(i).getNode();
1209 if (N == Def) {
1210 if (Use == ImmedUse || Use == Root)
1211 continue; // We are not looking for immediate use.
1212 assert(N != Root);
1213 return true;
1214 }
1215
1216 // Traverse up the operand chain.
1217 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
1218 return true;
1219 }
1220 return false;
1221}
1222
1223/// isNonImmUse - Start searching from Root up the DAG to check is Def can
1224/// be reached. Return true if that's the case. However, ignore direct uses
1225/// by ImmedUse (which would be U in the example illustrated in
1226/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
1227/// case).
1228/// FIXME: to be really generic, we should allow direct use by any node
1229/// that is being folded. But realisticly since we only fold loads which
1230/// have one non-chain use, we only need to watch out for load/op/store
1231/// and load/op/cmp case where the root (store / cmp) may reach the load via
1232/// its chain operand.
1233static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
1234 SmallPtrSet<SDNode*, 16> Visited;
1235 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
1236}
1237
1238/// IsLegalAndProfitableToFold - Returns true if the specific operand node N of
1239/// U can be folded during instruction selection that starts at Root and
1240/// folding N is profitable.
1241bool SelectionDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
1242 SDNode *Root) const {
1243 if (OptLevel == CodeGenOpt::None) return false;
1244
1245 // If Root use can somehow reach N through a path that that doesn't contain
1246 // U then folding N would create a cycle. e.g. In the following
1247 // diagram, Root can reach N through X. If N is folded into into Root, then
1248 // X is both a predecessor and a successor of U.
1249 //
1250 // [N*] //
1251 // ^ ^ //
1252 // / \ //
1253 // [U*] [X]? //
1254 // ^ ^ //
1255 // \ / //
1256 // \ / //
1257 // [Root*] //
1258 //
1259 // * indicates nodes to be folded together.
1260 //
1261 // If Root produces a flag, then it gets (even more) interesting. Since it
1262 // will be "glued" together with its flag use in the scheduler, we need to
1263 // check if it might reach N.
1264 //
1265 // [N*] //
1266 // ^ ^ //
1267 // / \ //
1268 // [U*] [X]? //
1269 // ^ ^ //
1270 // \ \ //
1271 // \ | //
1272 // [Root*] | //
1273 // ^ | //
1274 // f | //
1275 // | / //
1276 // [Y] / //
1277 // ^ / //
1278 // f / //
1279 // | / //
1280 // [FU] //
1281 //
1282 // If FU (flag use) indirectly reaches N (the load), and Root folds N
1283 // (call it Fold), then X is a predecessor of FU and a successor of
1284 // Fold. But since Fold and FU are flagged together, this will create
1285 // a cycle in the scheduling graph.
1286
Owen Andersonac9de032009-08-10 22:56:29 +00001287 EVT VT = Root->getValueType(Root->getNumValues()-1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001288 while (VT == MVT::Flag) {
Anton Korobeynikovda76d322009-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);
1294 }
1295
1296 return !isNonImmUse(Root, N, U);
1297}
1298
Dan Gohmanb65e3ac2009-10-29 22:30:23 +00001299SDNode *SelectionDAGISel::Select_INLINEASM(SDValue N) {
1300 std::vector<SDValue> Ops(N.getNode()->op_begin(), N.getNode()->op_end());
1301 SelectInlineAsmMemoryOperands(Ops);
1302
1303 std::vector<EVT> VTs;
1304 VTs.push_back(MVT::Other);
1305 VTs.push_back(MVT::Flag);
1306 SDValue New = CurDAG->getNode(ISD::INLINEASM, N.getDebugLoc(),
1307 VTs, &Ops[0], Ops.size());
1308 return New.getNode();
1309}
1310
1311SDNode *SelectionDAGISel::Select_UNDEF(const SDValue &N) {
1312 return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::IMPLICIT_DEF,
1313 N.getValueType());
1314}
1315
1316SDNode *SelectionDAGISel::Select_DBG_LABEL(const SDValue &N) {
1317 SDValue Chain = N.getOperand(0);
1318 unsigned C = cast<LabelSDNode>(N)->getLabelID();
1319 SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);
1320 return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DBG_LABEL,
1321 MVT::Other, Tmp, Chain);
1322}
1323
1324SDNode *SelectionDAGISel::Select_EH_LABEL(const SDValue &N) {
1325 SDValue Chain = N.getOperand(0);
1326 unsigned C = cast<LabelSDNode>(N)->getLabelID();
1327 SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);
1328 return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EH_LABEL,
1329 MVT::Other, Tmp, Chain);
1330}
1331
1332void SelectionDAGISel::CannotYetSelect(SDValue N) {
1333 std::string msg;
1334 raw_string_ostream Msg(msg);
1335 Msg << "Cannot yet select: ";
1336 N.getNode()->print(Msg, CurDAG);
1337 llvm_report_error(Msg.str());
1338}
1339
1340void SelectionDAGISel::CannotYetSelectIntrinsic(SDValue N) {
1341 errs() << "Cannot yet select: ";
1342 unsigned iid =
1343 cast<ConstantSDNode>(N.getOperand(N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();
1344 if (iid < Intrinsic::num_intrinsics)
1345 llvm_report_error("Cannot yet select: intrinsic %" + Intrinsic::getName((Intrinsic::ID)iid));
1346 else if (const TargetIntrinsicInfo *tii = TM.getIntrinsicInfo())
1347 llvm_report_error(Twine("Cannot yet select: target intrinsic %") +
1348 tii->getName(iid));
1349}
Anton Korobeynikovda76d322009-05-08 18:51:58 +00001350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351char SelectionDAGISel::ID = 0;