blob: e8da9d79126858bc7398fda9adda194a863c8315 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengf597dc72006-02-10 22:24:32 +000015#define DEBUG_TYPE "isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000035#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000036#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000037using namespace llvm;
38
39//===----------------------------------------------------------------------===//
40// Pattern Matcher Implementation
41//===----------------------------------------------------------------------===//
42
43namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000044 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
45 /// SDOperand's instead of register numbers for the leaves of the matched
46 /// tree.
47 struct X86ISelAddressMode {
48 enum {
49 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000050 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 } BaseType;
52
53 struct { // This is really a union, discriminated by BaseType!
54 SDOperand Reg;
55 int FrameIndex;
56 } Base;
57
58 unsigned Scale;
59 SDOperand IndexReg;
60 unsigned Disp;
61 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000062 Constant *CP;
63 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000064
65 X86ISelAddressMode()
Evan Cheng51a9ed92006-02-25 10:09:08 +000066 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
67 CP(0), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000068 }
69 };
70}
71
72namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000073 Statistic<>
74 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
75
76 //===--------------------------------------------------------------------===//
77 /// ISel - X86 specific code to select X86 machine instructions for
78 /// SelectionDAG operations.
79 ///
80 class X86DAGToDAGISel : public SelectionDAGISel {
81 /// ContainsFPCode - Every instruction we select that uses or defines a FP
82 /// register should set this to true.
83 bool ContainsFPCode;
84
85 /// X86Lowering - This object fully describes how to lower LLVM code to an
86 /// X86-specific SelectionDAG.
87 X86TargetLowering X86Lowering;
88
89 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
90 /// make the right decision when generating code for different targets.
91 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +000092
93 unsigned GlobalBaseReg;
Chris Lattnerc961eea2005-11-16 01:54:32 +000094 public:
Evan Chengc4c62572006-03-13 23:20:37 +000095 X86DAGToDAGISel(X86TargetMachine &TM)
96 : SelectionDAGISel(X86Lowering),
97 X86Lowering(*TM.getTargetLowering()) {
Chris Lattnerc961eea2005-11-16 01:54:32 +000098 Subtarget = &TM.getSubtarget<X86Subtarget>();
99 }
100
Evan Cheng7ccced62006-02-18 00:15:05 +0000101 virtual bool runOnFunction(Function &Fn) {
102 // Make sure we re-emit a set of the global base reg if necessary
103 GlobalBaseReg = 0;
104 return SelectionDAGISel::runOnFunction(Fn);
105 }
106
Chris Lattnerc961eea2005-11-16 01:54:32 +0000107 virtual const char *getPassName() const {
108 return "X86 DAG->DAG Instruction Selection";
109 }
110
111 /// InstructionSelectBasicBlock - This callback is invoked by
112 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
113 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
114
Evan Cheng8700e142006-01-11 06:09:51 +0000115 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
116
Chris Lattnerc961eea2005-11-16 01:54:32 +0000117// Include the pieces autogenerated from the target description.
118#include "X86GenDAGISel.inc"
119
120 private:
Evan Cheng34167212006-02-09 00:37:58 +0000121 void Select(SDOperand &Result, SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000122
Evan Cheng2486af12006-02-11 02:05:36 +0000123 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengec693f72005-12-08 02:01:35 +0000124 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
125 SDOperand &Index, SDOperand &Disp);
126 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
127 SDOperand &Index, SDOperand &Disp);
Evan Cheng5e351682006-02-06 06:02:33 +0000128 bool TryFoldLoad(SDOperand P, SDOperand N,
129 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000130 SDOperand &Index, SDOperand &Disp);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000131
Evan Chenge5280532005-12-12 21:49:40 +0000132 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
133 SDOperand &Scale, SDOperand &Index,
134 SDOperand &Disp) {
135 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
136 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000137 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000138 Index = AM.IndexReg;
139 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000140 : (AM.CP ?
141 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
142 : getI32Imm(AM.Disp));
Evan Chenge5280532005-12-12 21:49:40 +0000143 }
144
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000145 /// getI8Imm - Return a target constant with the specified value, of type
146 /// i8.
147 inline SDOperand getI8Imm(unsigned Imm) {
148 return CurDAG->getTargetConstant(Imm, MVT::i8);
149 }
150
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151 /// getI16Imm - Return a target constant with the specified value, of type
152 /// i16.
153 inline SDOperand getI16Imm(unsigned Imm) {
154 return CurDAG->getTargetConstant(Imm, MVT::i16);
155 }
156
157 /// getI32Imm - Return a target constant with the specified value, of type
158 /// i32.
159 inline SDOperand getI32Imm(unsigned Imm) {
160 return CurDAG->getTargetConstant(Imm, MVT::i32);
161 }
Evan Chengf597dc72006-02-10 22:24:32 +0000162
Evan Cheng7ccced62006-02-18 00:15:05 +0000163 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
164 /// base register. Return the virtual register that holds this value.
165 SDOperand getGlobalBaseReg();
166
Evan Cheng23addc02006-02-10 22:46:26 +0000167#ifndef NDEBUG
168 unsigned Indent;
169#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000170 };
171}
172
173/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
174/// when it has created a SelectionDAG for us to codegen.
175void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
176 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000177 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000178
179 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000180#ifndef NDEBUG
181 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000182 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000183#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000184 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000185 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Evan Chengf597dc72006-02-10 22:24:32 +0000186#ifndef NDEBUG
187 DEBUG(std::cerr << "===== Instruction selection ends:\n");
188#endif
Evan Chengfcaa9952005-12-19 22:36:02 +0000189 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000190 HandleMap.clear();
191 ReplaceMap.clear();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000192 DAG.RemoveDeadNodes();
193
194 // Emit machine code to BB.
195 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000196
197 // If we are emitting FP stack code, scan the basic block to determine if this
198 // block defines any FP values. If so, put an FP_REG_KILL instruction before
199 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000200 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000201 // Note that FP stack instructions *are* used in SSE code when returning
202 // values, but these are not live out of the basic block, so we don't need
203 // an FP_REG_KILL in this case either.
204 bool ContainsFPCode = false;
205
206 // Scan all of the machine instructions in these MBBs, checking for FP
207 // stores.
208 MachineFunction::iterator MBBI = FirstMBB;
209 do {
210 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
211 !ContainsFPCode && I != E; ++I) {
212 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
213 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
214 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
215 RegMap->getRegClass(I->getOperand(0).getReg()) ==
216 X86::RFPRegisterClass) {
217 ContainsFPCode = true;
218 break;
219 }
220 }
221 }
222 } while (!ContainsFPCode && &*(MBBI++) != BB);
223
224 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
225 // a copy of the input value in this block.
226 if (!ContainsFPCode) {
227 // Final check, check LLVM BB's that are successors to the LLVM BB
228 // corresponding to BB for FP PHI nodes.
229 const BasicBlock *LLVMBB = BB->getBasicBlock();
230 const PHINode *PN;
231 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
232 !ContainsFPCode && SI != E; ++SI) {
233 for (BasicBlock::const_iterator II = SI->begin();
234 (PN = dyn_cast<PHINode>(II)); ++II) {
235 if (PN->getType()->isFloatingPoint()) {
236 ContainsFPCode = true;
237 break;
238 }
239 }
240 }
241 }
242
243 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
244 if (ContainsFPCode) {
245 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
246 ++NumFPKill;
247 }
248 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000249}
250
Evan Cheng8700e142006-01-11 06:09:51 +0000251/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
252/// the main function.
253static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
254 MachineFrameInfo *MFI) {
255 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
256 int CWFrameIdx = MFI->CreateStackObject(2, 2);
257 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
258
259 // Set the high part to be 64-bit precision.
260 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
261 CWFrameIdx, 1).addImm(2);
262
263 // Reload the modified control word now.
264 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
265}
266
267void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
268 // If this is main, emit special code for main.
269 MachineBasicBlock *BB = MF.begin();
270 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
271 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
272}
273
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000274/// MatchAddress - Add the specified node to the specified addressing mode,
275/// returning true if it cannot be done. This just pattern matches for the
276/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000277bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
278 bool isRoot) {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000279 bool Available = false;
280 // If N has already been selected, reuse the result unless in some very
281 // specific cases.
Evan Cheng2486af12006-02-11 02:05:36 +0000282 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
283 if (CGMI != CodeGenMap.end()) {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000284 Available = true;
Evan Cheng2486af12006-02-11 02:05:36 +0000285 }
286
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000287 switch (N.getOpcode()) {
288 default: break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000289 case ISD::Constant:
290 AM.Disp += cast<ConstantSDNode>(N)->getValue();
291 return false;
292
293 case X86ISD::Wrapper:
294 // If both base and index components have been picked, we can't fit
295 // the result available in the register in the addressing mode. Duplicate
296 // GlobalAddress or ConstantPool as displacement.
297 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
298 if (ConstantPoolSDNode *CP =
299 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
300 if (AM.CP == 0) {
301 AM.CP = CP->get();
302 AM.Align = CP->getAlignment();
303 AM.Disp += CP->getOffset();
304 return false;
305 }
306 } else if (GlobalAddressSDNode *G =
307 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
308 if (AM.GV == 0) {
309 AM.GV = G->getGlobal();
310 AM.Disp += G->getOffset();
311 return false;
312 }
313 }
314 }
315 break;
316
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000317 case ISD::FrameIndex:
318 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
319 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
320 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
321 return false;
322 }
323 break;
Evan Chengec693f72005-12-08 02:01:35 +0000324
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000325 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000326 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000327 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
328 unsigned Val = CN->getValue();
329 if (Val == 1 || Val == 2 || Val == 3) {
330 AM.Scale = 1 << Val;
331 SDOperand ShVal = N.Val->getOperand(0);
332
333 // Okay, we know that we have a scale by now. However, if the scaled
334 // value is an add of something and a constant, we can fold the
335 // constant into the disp field here.
336 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
337 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
338 AM.IndexReg = ShVal.Val->getOperand(0);
339 ConstantSDNode *AddVal =
340 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
341 AM.Disp += AddVal->getValue() << Val;
342 } else {
343 AM.IndexReg = ShVal;
344 }
345 return false;
346 }
347 }
348 break;
Evan Chengec693f72005-12-08 02:01:35 +0000349
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000350 case ISD::MUL:
351 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000352 if (!Available &&
353 AM.BaseType == X86ISelAddressMode::RegBase &&
354 AM.Base.Reg.Val == 0 &&
355 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000356 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
357 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
358 AM.Scale = unsigned(CN->getValue())-1;
359
360 SDOperand MulVal = N.Val->getOperand(0);
361 SDOperand Reg;
362
363 // Okay, we know that we have a scale by now. However, if the scaled
364 // value is an add of something and a constant, we can fold the
365 // constant into the disp field here.
366 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
367 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
368 Reg = MulVal.Val->getOperand(0);
369 ConstantSDNode *AddVal =
370 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
371 AM.Disp += AddVal->getValue() * CN->getValue();
372 } else {
373 Reg = N.Val->getOperand(0);
374 }
375
376 AM.IndexReg = AM.Base.Reg = Reg;
377 return false;
378 }
379 break;
380
381 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000382 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000383 X86ISelAddressMode Backup = AM;
384 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
385 !MatchAddress(N.Val->getOperand(1), AM, false))
386 return false;
387 AM = Backup;
388 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
389 !MatchAddress(N.Val->getOperand(0), AM, false))
390 return false;
391 AM = Backup;
392 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000393 break;
394 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000395
396 case ISD::OR: {
397 if (!Available) {
398 X86ISelAddressMode Backup = AM;
399 // Look for (x << c1) | c2 where (c2 < c1)
400 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
401 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
402 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
403 AM.Disp = CN->getValue();
404 return false;
405 }
406 }
407 AM = Backup;
408 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
409 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
410 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
411 AM.Disp = CN->getValue();
412 return false;
413 }
414 }
415 AM = Backup;
416 }
417 break;
418 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000419 }
420
421 // Is the base register already occupied?
422 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
423 // If so, check to see if the scale index register is set.
424 if (AM.IndexReg.Val == 0) {
425 AM.IndexReg = N;
426 AM.Scale = 1;
427 return false;
428 }
429
430 // Otherwise, we cannot select it.
431 return true;
432 }
433
434 // Default, generate it as a register.
435 AM.BaseType = X86ISelAddressMode::RegBase;
436 AM.Base.Reg = N;
437 return false;
438}
439
Evan Chengec693f72005-12-08 02:01:35 +0000440/// SelectAddr - returns true if it is able pattern match an addressing mode.
441/// It returns the operands which make up the maximal addressing mode it can
442/// match by reference.
443bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
444 SDOperand &Index, SDOperand &Disp) {
445 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000446 if (MatchAddress(N, AM))
447 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000448
Evan Cheng8700e142006-01-11 06:09:51 +0000449 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000450 if (!AM.Base.Reg.Val)
Evan Cheng8700e142006-01-11 06:09:51 +0000451 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000452 }
Evan Cheng8700e142006-01-11 06:09:51 +0000453
Evan Cheng7dd281b2006-02-05 05:25:07 +0000454 if (!AM.IndexReg.Val)
Evan Cheng8700e142006-01-11 06:09:51 +0000455 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
456
457 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000458
Evan Cheng8700e142006-01-11 06:09:51 +0000459 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000460}
461
Evan Cheng51a9ed92006-02-25 10:09:08 +0000462/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
463/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng51a9ed92006-02-25 10:09:08 +0000464bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
465 SDOperand &Scale,
466 SDOperand &Index, SDOperand &Disp) {
467 X86ISelAddressMode AM;
468 if (MatchAddress(N, AM))
469 return false;
470
471 unsigned Complexity = 0;
472 if (AM.BaseType == X86ISelAddressMode::RegBase)
473 if (AM.Base.Reg.Val)
474 Complexity = 1;
475 else
476 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
477 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
478 Complexity = 4;
479
480 if (AM.IndexReg.Val)
481 Complexity++;
482 else
483 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
484
Evan Cheng8c03fe42006-02-28 21:13:57 +0000485 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000486 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000487 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
488 else if (AM.Scale > 1)
489 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000490
491 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
492 // to a LEA. This is determined with some expermentation but is by no means
493 // optimal (especially for code size consideration). LEA is nice because of
494 // its three-address nature. Tweak the cost function again when we can run
495 // convertToThreeAddress() at register allocation time.
496 if (AM.GV || AM.CP)
497 Complexity += 2;
498
499 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
500 Complexity++;
501
502 if (Complexity > 2) {
503 getAddressOperands(AM, Base, Scale, Index, Disp);
504 return true;
505 }
506
507 return false;
508}
509
Evan Cheng5e351682006-02-06 06:02:33 +0000510bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
511 SDOperand &Base, SDOperand &Scale,
512 SDOperand &Index, SDOperand &Disp) {
513 if (N.getOpcode() == ISD::LOAD &&
514 N.hasOneUse() &&
515 !CodeGenMap.count(N.getValue(0)) &&
516 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
Evan Cheng0114e942006-01-06 20:36:21 +0000517 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
518 return false;
519}
520
521static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000522 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
523 return (R->getReg() == 0);
524 return false;
525}
526
Evan Cheng7ccced62006-02-18 00:15:05 +0000527/// getGlobalBaseReg - Output the instructions required to put the
528/// base address to use for accessing globals into a register.
529///
530SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
531 if (!GlobalBaseReg) {
532 // Insert the set of GlobalBaseReg into the first MBB of the function
533 MachineBasicBlock &FirstMBB = BB->getParent()->front();
534 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
535 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
536 // FIXME: when we get to LP64, we will need to create the appropriate
537 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000538 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000539 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
540 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
541 }
542 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
543}
544
Evan Chengb245d922006-05-20 01:36:52 +0000545static SDNode *FindCallStartFromCall(SDNode *Node) {
546 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
547 assert(Node->getOperand(0).getValueType() == MVT::Other &&
548 "Node doesn't have a token chain argument!");
549 return FindCallStartFromCall(Node->getOperand(0).Val);
550}
551
Evan Cheng34167212006-02-09 00:37:58 +0000552void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000553 SDNode *Node = N.Val;
554 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000555 unsigned Opc, MOpc;
556 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000557
Evan Chengf597dc72006-02-10 22:24:32 +0000558#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +0000559 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000560 DEBUG(std::cerr << "Selecting: ");
561 DEBUG(Node->dump(CurDAG));
562 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000563 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000564#endif
565
Evan Cheng34167212006-02-09 00:37:58 +0000566 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
567 Result = N;
Evan Chengf597dc72006-02-10 22:24:32 +0000568#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000569 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000570 DEBUG(std::cerr << "== ");
571 DEBUG(Node->dump(CurDAG));
572 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000573 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000574#endif
Evan Cheng34167212006-02-09 00:37:58 +0000575 return; // Already selected.
576 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000577
578 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
Evan Cheng34167212006-02-09 00:37:58 +0000579 if (CGMI != CodeGenMap.end()) {
580 Result = CGMI->second;
Evan Chengf597dc72006-02-10 22:24:32 +0000581#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000582 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000583 DEBUG(std::cerr << "== ");
584 DEBUG(Result.Val->dump(CurDAG));
585 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000586 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000587#endif
Evan Cheng34167212006-02-09 00:37:58 +0000588 return;
589 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000590
Evan Cheng0114e942006-01-06 20:36:21 +0000591 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000592 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000593 case X86ISD::GlobalBaseReg:
594 Result = getGlobalBaseReg();
595 return;
596
Evan Cheng51a9ed92006-02-25 10:09:08 +0000597 case ISD::ADD: {
598 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
599 // code and is matched first so to prevent it from being turned into
600 // LEA32r X+c.
601 SDOperand N0 = N.getOperand(0);
602 SDOperand N1 = N.getOperand(1);
603 if (N.Val->getValueType(0) == MVT::i32 &&
604 N0.getOpcode() == X86ISD::Wrapper &&
605 N1.getOpcode() == ISD::Constant) {
606 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
607 SDOperand C(0, 0);
608 // TODO: handle ExternalSymbolSDNode.
609 if (GlobalAddressSDNode *G =
610 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
611 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
612 G->getOffset() + Offset);
613 } else if (ConstantPoolSDNode *CP =
614 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
615 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
616 CP->getAlignment(),
617 CP->getOffset()+Offset);
618 }
619
620 if (C.Val) {
621 if (N.Val->hasOneUse()) {
622 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
623 } else {
624 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
625 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
626 }
627 return;
628 }
629 }
630
631 // Other cases are handled by auto-generated code.
632 break;
Evan Chenga0ea0532006-02-23 02:43:52 +0000633 }
Evan Cheng020d2e82006-02-23 20:41:18 +0000634
Evan Cheng0114e942006-01-06 20:36:21 +0000635 case ISD::MULHU:
636 case ISD::MULHS: {
637 if (Opcode == ISD::MULHU)
638 switch (NVT) {
639 default: assert(0 && "Unsupported VT!");
640 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
641 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
642 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
643 }
644 else
645 switch (NVT) {
646 default: assert(0 && "Unsupported VT!");
647 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
648 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
649 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
650 }
651
652 unsigned LoReg, HiReg;
653 switch (NVT) {
654 default: assert(0 && "Unsupported VT!");
655 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
656 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
657 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
658 }
659
660 SDOperand N0 = Node->getOperand(0);
661 SDOperand N1 = Node->getOperand(1);
662
663 bool foldedLoad = false;
664 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000665 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000666 // MULHU and MULHS are commmutative
667 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +0000668 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000669 if (foldedLoad) {
670 N0 = Node->getOperand(1);
671 N1 = Node->getOperand(0);
672 }
673 }
674
Evan Cheng34167212006-02-09 00:37:58 +0000675 SDOperand Chain;
676 if (foldedLoad)
677 Select(Chain, N1.getOperand(0));
678 else
679 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +0000680
Evan Cheng34167212006-02-09 00:37:58 +0000681 SDOperand InFlag(0, 0);
682 Select(N0, N0);
Evan Cheng0114e942006-01-06 20:36:21 +0000683 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000684 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000685 InFlag = Chain.getValue(1);
686
687 if (foldedLoad) {
Evan Cheng34167212006-02-09 00:37:58 +0000688 Select(Tmp0, Tmp0);
689 Select(Tmp1, Tmp1);
690 Select(Tmp2, Tmp2);
691 Select(Tmp3, Tmp3);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000692 SDNode *CNode =
693 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
694 Tmp2, Tmp3, Chain, InFlag);
695 Chain = SDOperand(CNode, 0);
696 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +0000697 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000698 Select(N1, N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000699 InFlag =
700 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +0000701 }
702
Evan Cheng34167212006-02-09 00:37:58 +0000703 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000704 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng5e351682006-02-06 06:02:33 +0000705 if (foldedLoad) {
Evan Cheng948f3432006-01-06 23:19:29 +0000706 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng7d82d602006-02-09 22:12:53 +0000707 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Cheng5e351682006-02-06 06:02:33 +0000708 }
Evan Cheng34167212006-02-09 00:37:58 +0000709
Evan Chengf597dc72006-02-10 22:24:32 +0000710#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000711 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000712 DEBUG(std::cerr << "== ");
713 DEBUG(Result.Val->dump(CurDAG));
714 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000715 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000716#endif
Evan Cheng34167212006-02-09 00:37:58 +0000717 return;
Evan Cheng948f3432006-01-06 23:19:29 +0000718 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000719
Evan Cheng948f3432006-01-06 23:19:29 +0000720 case ISD::SDIV:
721 case ISD::UDIV:
722 case ISD::SREM:
723 case ISD::UREM: {
724 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
725 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
726 if (!isSigned)
727 switch (NVT) {
728 default: assert(0 && "Unsupported VT!");
729 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
730 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
731 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
732 }
733 else
734 switch (NVT) {
735 default: assert(0 && "Unsupported VT!");
736 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
737 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
738 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
739 }
740
741 unsigned LoReg, HiReg;
742 unsigned ClrOpcode, SExtOpcode;
743 switch (NVT) {
744 default: assert(0 && "Unsupported VT!");
745 case MVT::i8:
746 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengaede9b92006-06-02 21:20:34 +0000747 ClrOpcode = X86::MOV8r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000748 SExtOpcode = X86::CBW;
749 break;
750 case MVT::i16:
751 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +0000752 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000753 SExtOpcode = X86::CWD;
754 break;
755 case MVT::i32:
756 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +0000757 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000758 SExtOpcode = X86::CDQ;
759 break;
760 }
761
762 SDOperand N0 = Node->getOperand(0);
763 SDOperand N1 = Node->getOperand(1);
764
765 bool foldedLoad = false;
766 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000767 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng34167212006-02-09 00:37:58 +0000768 SDOperand Chain;
769 if (foldedLoad)
770 Select(Chain, N1.getOperand(0));
771 else
772 Chain = CurDAG->getEntryNode();
Evan Cheng948f3432006-01-06 23:19:29 +0000773
Evan Cheng34167212006-02-09 00:37:58 +0000774 SDOperand InFlag(0, 0);
775 Select(N0, N0);
Evan Cheng948f3432006-01-06 23:19:29 +0000776 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000777 N0, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +0000778 InFlag = Chain.getValue(1);
779
780 if (isSigned) {
781 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000782 InFlag =
783 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000784 } else {
785 // Zero out the high part, effectively zero extending the input.
Evan Chengaede9b92006-06-02 21:20:34 +0000786 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000787 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
788 ClrNode, InFlag);
789 InFlag = Chain.getValue(1);
790 }
791
792 if (foldedLoad) {
Evan Cheng34167212006-02-09 00:37:58 +0000793 Select(Tmp0, Tmp0);
794 Select(Tmp1, Tmp1);
795 Select(Tmp2, Tmp2);
796 Select(Tmp3, Tmp3);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000797 SDNode *CNode =
798 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
799 Tmp2, Tmp3, Chain, InFlag);
800 Chain = SDOperand(CNode, 0);
801 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +0000802 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000803 Select(N1, N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000804 InFlag =
805 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000806 }
807
Evan Cheng34167212006-02-09 00:37:58 +0000808 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
809 NVT, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +0000810 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng5e351682006-02-06 06:02:33 +0000811 if (foldedLoad) {
Evan Cheng948f3432006-01-06 23:19:29 +0000812 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng7d82d602006-02-09 22:12:53 +0000813 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Cheng5e351682006-02-06 06:02:33 +0000814 }
Evan Chengf597dc72006-02-10 22:24:32 +0000815
816#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000817 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000818 DEBUG(std::cerr << "== ");
819 DEBUG(Result.Val->dump(CurDAG));
820 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000821 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000822#endif
Evan Cheng34167212006-02-09 00:37:58 +0000823 return;
Evan Cheng0114e942006-01-06 20:36:21 +0000824 }
Evan Cheng403be7e2006-05-08 08:01:26 +0000825
826 case ISD::TRUNCATE: {
827 if (NVT == MVT::i8) {
828 unsigned Opc2;
829 MVT::ValueType VT;
830 switch (Node->getOperand(0).getValueType()) {
831 default: assert(0 && "Unknown truncate!");
832 case MVT::i16:
833 Opc = X86::MOV16to16_;
834 VT = MVT::i16;
Evan Cheng069287d2006-05-16 07:21:53 +0000835 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng403be7e2006-05-08 08:01:26 +0000836 break;
837 case MVT::i32:
838 Opc = X86::MOV32to32_;
839 VT = MVT::i32;
Evan Cheng069287d2006-05-16 07:21:53 +0000840 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng403be7e2006-05-08 08:01:26 +0000841 break;
842 }
843
844 SDOperand Tmp0, Tmp1;
845 Select(Tmp0, Node->getOperand(0));
846 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0);
847 Result = CodeGenMap[N] =
848 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0);
849
850#ifndef NDEBUG
851 DEBUG(std::cerr << std::string(Indent-2, ' '));
852 DEBUG(std::cerr << "== ");
853 DEBUG(Result.Val->dump(CurDAG));
854 DEBUG(std::cerr << "\n");
855 Indent -= 2;
856#endif
857 return;
858 }
Evan Cheng6b2e2542006-05-20 07:44:28 +0000859
860 break;
Evan Cheng403be7e2006-05-08 08:01:26 +0000861 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000862 }
863
Evan Cheng34167212006-02-09 00:37:58 +0000864 SelectCode(Result, N);
Evan Chengf597dc72006-02-10 22:24:32 +0000865#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000866 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000867 DEBUG(std::cerr << "=> ");
868 DEBUG(Result.Val->dump(CurDAG));
869 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000870 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000871#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000872}
873
874/// createX86ISelDag - This pass converts a legalized DAG into a
875/// X86-specific DAG, ready for instruction scheduling.
876///
Evan Chengc4c62572006-03-13 23:20:37 +0000877FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000878 return new X86DAGToDAGISel(TM);
879}