blob: 719d1e7d7130507ecf235aac01c49fdbf24227c3 [file] [log] [blame]
Chris Lattner5930d3d2005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattner655e7df2005-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 Chengd49cc362006-02-10 22:24:32 +000015#define DEBUG_TYPE "isel"
Chris Lattner655e7df2005-11-16 01:54:32 +000016#include "X86.h"
Evan Chengbc7a0f442006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner7c551262006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner5d70a7c2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner7c551262006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-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 Lattnerde02d772006-01-22 23:41:00 +000035#include <iostream>
Evan Cheng54cb1832006-02-05 06:46:41 +000036#include <set>
Chris Lattner655e7df2005-11-16 01:54:32 +000037using namespace llvm;
38
39//===----------------------------------------------------------------------===//
40// Pattern Matcher Implementation
41//===----------------------------------------------------------------------===//
42
43namespace {
Chris Lattner3f0f71b2005-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,
50 FrameIndexBase,
51 } 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 Cheng77d86ff2006-02-25 10:09:08 +000062 Constant *CP;
63 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000064
65 X86ISelAddressMode()
Evan Cheng77d86ff2006-02-25 10:09:08 +000066 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
67 CP(0), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000068 }
69 };
70}
71
72namespace {
Chris Lattner655e7df2005-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 Cheng5588de92006-02-18 00:15:05 +000092
93 unsigned GlobalBaseReg;
Chris Lattner655e7df2005-11-16 01:54:32 +000094 public:
Evan Cheng2dd2c652006-03-13 23:20:37 +000095 X86DAGToDAGISel(X86TargetMachine &TM)
96 : SelectionDAGISel(X86Lowering),
97 X86Lowering(*TM.getTargetLowering()) {
Chris Lattner655e7df2005-11-16 01:54:32 +000098 Subtarget = &TM.getSubtarget<X86Subtarget>();
99 }
100
Evan Cheng5588de92006-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 Lattner655e7df2005-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 Chengbc7a0f442006-01-11 06:09:51 +0000115 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
116
Chris Lattner655e7df2005-11-16 01:54:32 +0000117// Include the pieces autogenerated from the target description.
118#include "X86GenDAGISel.inc"
119
120 private:
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000121 void Select(SDOperand &Result, SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000122
Evan Chenga86ba852006-02-11 02:05:36 +0000123 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-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 Chengd5f2ba02006-02-06 06:02:33 +0000128 bool TryFoldLoad(SDOperand P, SDOperand N,
129 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000130 SDOperand &Index, SDOperand &Disp);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000131
Evan Cheng67ed58e2005-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 Cheng1d712482005-12-17 09:13:43 +0000137 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000138 Index = AM.IndexReg;
139 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000140 : (AM.CP ?
141 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
142 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000143 }
144
Chris Lattner3f0f71b2005-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 Lattner655e7df2005-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 Chengd49cc362006-02-10 22:24:32 +0000162
Evan Cheng5588de92006-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 Cheng2b6f78b2006-02-10 22:46:26 +0000167#ifndef NDEBUG
168 unsigned Indent;
169#endif
Chris Lattner655e7df2005-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 Lattner7c551262006-01-11 01:15:34 +0000177 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000178
179 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000180#ifndef NDEBUG
181 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000182 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000183#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000184 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000185#ifndef NDEBUG
186 DEBUG(std::cerr << "===== Instruction selection ends:\n");
187#endif
Evan Cheng1d9b6712005-12-19 22:36:02 +0000188 CodeGenMap.clear();
Chris Lattner655e7df2005-11-16 01:54:32 +0000189 DAG.RemoveDeadNodes();
190
191 // Emit machine code to BB.
192 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000193
194 // If we are emitting FP stack code, scan the basic block to determine if this
195 // block defines any FP values. If so, put an FP_REG_KILL instruction before
196 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000197 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000198 // Note that FP stack instructions *are* used in SSE code when returning
199 // values, but these are not live out of the basic block, so we don't need
200 // an FP_REG_KILL in this case either.
201 bool ContainsFPCode = false;
202
203 // Scan all of the machine instructions in these MBBs, checking for FP
204 // stores.
205 MachineFunction::iterator MBBI = FirstMBB;
206 do {
207 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
208 !ContainsFPCode && I != E; ++I) {
209 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
210 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
211 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
212 RegMap->getRegClass(I->getOperand(0).getReg()) ==
213 X86::RFPRegisterClass) {
214 ContainsFPCode = true;
215 break;
216 }
217 }
218 }
219 } while (!ContainsFPCode && &*(MBBI++) != BB);
220
221 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
222 // a copy of the input value in this block.
223 if (!ContainsFPCode) {
224 // Final check, check LLVM BB's that are successors to the LLVM BB
225 // corresponding to BB for FP PHI nodes.
226 const BasicBlock *LLVMBB = BB->getBasicBlock();
227 const PHINode *PN;
228 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
229 !ContainsFPCode && SI != E; ++SI) {
230 for (BasicBlock::const_iterator II = SI->begin();
231 (PN = dyn_cast<PHINode>(II)); ++II) {
232 if (PN->getType()->isFloatingPoint()) {
233 ContainsFPCode = true;
234 break;
235 }
236 }
237 }
238 }
239
240 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
241 if (ContainsFPCode) {
242 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
243 ++NumFPKill;
244 }
245 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000246}
247
Evan Chengbc7a0f442006-01-11 06:09:51 +0000248/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
249/// the main function.
250static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
251 MachineFrameInfo *MFI) {
252 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
253 int CWFrameIdx = MFI->CreateStackObject(2, 2);
254 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
255
256 // Set the high part to be 64-bit precision.
257 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
258 CWFrameIdx, 1).addImm(2);
259
260 // Reload the modified control word now.
261 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
262}
263
264void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
265 // If this is main, emit special code for main.
266 MachineBasicBlock *BB = MF.begin();
267 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
268 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
269}
270
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000271/// MatchAddress - Add the specified node to the specified addressing mode,
272/// returning true if it cannot be done. This just pattern matches for the
273/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000274bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
275 bool isRoot) {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000276 bool Available = false;
277 // If N has already been selected, reuse the result unless in some very
278 // specific cases.
Evan Chenga86ba852006-02-11 02:05:36 +0000279 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
280 if (CGMI != CodeGenMap.end()) {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000281 Available = true;
Evan Chenga86ba852006-02-11 02:05:36 +0000282 }
283
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000284 switch (N.getOpcode()) {
285 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000286 case ISD::Constant:
287 AM.Disp += cast<ConstantSDNode>(N)->getValue();
288 return false;
289
290 case X86ISD::Wrapper:
291 // If both base and index components have been picked, we can't fit
292 // the result available in the register in the addressing mode. Duplicate
293 // GlobalAddress or ConstantPool as displacement.
294 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
295 if (ConstantPoolSDNode *CP =
296 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
297 if (AM.CP == 0) {
298 AM.CP = CP->get();
299 AM.Align = CP->getAlignment();
300 AM.Disp += CP->getOffset();
301 return false;
302 }
303 } else if (GlobalAddressSDNode *G =
304 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
305 if (AM.GV == 0) {
306 AM.GV = G->getGlobal();
307 AM.Disp += G->getOffset();
308 return false;
309 }
310 }
311 }
312 break;
313
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000314 case ISD::FrameIndex:
315 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
316 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
317 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
318 return false;
319 }
320 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000321
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000322 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000323 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000324 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
325 unsigned Val = CN->getValue();
326 if (Val == 1 || Val == 2 || Val == 3) {
327 AM.Scale = 1 << Val;
328 SDOperand ShVal = N.Val->getOperand(0);
329
330 // Okay, we know that we have a scale by now. However, if the scaled
331 // value is an add of something and a constant, we can fold the
332 // constant into the disp field here.
333 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
334 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
335 AM.IndexReg = ShVal.Val->getOperand(0);
336 ConstantSDNode *AddVal =
337 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
338 AM.Disp += AddVal->getValue() << Val;
339 } else {
340 AM.IndexReg = ShVal;
341 }
342 return false;
343 }
344 }
345 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000346
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000347 case ISD::MUL:
348 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000349 if (!Available &&
350 AM.BaseType == X86ISelAddressMode::RegBase &&
351 AM.Base.Reg.Val == 0 &&
352 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000353 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
354 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
355 AM.Scale = unsigned(CN->getValue())-1;
356
357 SDOperand MulVal = N.Val->getOperand(0);
358 SDOperand Reg;
359
360 // Okay, we know that we have a scale by now. However, if the scaled
361 // value is an add of something and a constant, we can fold the
362 // constant into the disp field here.
363 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
364 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
365 Reg = MulVal.Val->getOperand(0);
366 ConstantSDNode *AddVal =
367 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
368 AM.Disp += AddVal->getValue() * CN->getValue();
369 } else {
370 Reg = N.Val->getOperand(0);
371 }
372
373 AM.IndexReg = AM.Base.Reg = Reg;
374 return false;
375 }
376 break;
377
378 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000379 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000380 X86ISelAddressMode Backup = AM;
381 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
382 !MatchAddress(N.Val->getOperand(1), AM, false))
383 return false;
384 AM = Backup;
385 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
386 !MatchAddress(N.Val->getOperand(0), AM, false))
387 return false;
388 AM = Backup;
389 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000390 break;
391 }
392 }
393
394 // Is the base register already occupied?
395 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
396 // If so, check to see if the scale index register is set.
397 if (AM.IndexReg.Val == 0) {
398 AM.IndexReg = N;
399 AM.Scale = 1;
400 return false;
401 }
402
403 // Otherwise, we cannot select it.
404 return true;
405 }
406
407 // Default, generate it as a register.
408 AM.BaseType = X86ISelAddressMode::RegBase;
409 AM.Base.Reg = N;
410 return false;
411}
412
Evan Chengc9fab312005-12-08 02:01:35 +0000413/// SelectAddr - returns true if it is able pattern match an addressing mode.
414/// It returns the operands which make up the maximal addressing mode it can
415/// match by reference.
416bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
417 SDOperand &Index, SDOperand &Disp) {
418 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000419 if (MatchAddress(N, AM))
420 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000421
Evan Chengbc7a0f442006-01-11 06:09:51 +0000422 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000423 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000424 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000425 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000426
Evan Chengd19d51f2006-02-05 05:25:07 +0000427 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000428 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
429
430 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000431
Evan Chengbc7a0f442006-01-11 06:09:51 +0000432 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000433}
434
Evan Cheng77d86ff2006-02-25 10:09:08 +0000435/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
436/// mode it matches can be cost effectively emitted as an LEA instruction.
437/// For X86, it always is unless it's just a (Reg + const).
438bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
439 SDOperand &Scale,
440 SDOperand &Index, SDOperand &Disp) {
441 X86ISelAddressMode AM;
442 if (MatchAddress(N, AM))
443 return false;
444
445 unsigned Complexity = 0;
446 if (AM.BaseType == X86ISelAddressMode::RegBase)
447 if (AM.Base.Reg.Val)
448 Complexity = 1;
449 else
450 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
451 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
452 Complexity = 4;
453
454 if (AM.IndexReg.Val)
455 Complexity++;
456 else
457 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
458
Evan Cheng990c3602006-02-28 21:13:57 +0000459 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000460 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000461 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
462 else if (AM.Scale > 1)
463 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000464
465 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
466 // to a LEA. This is determined with some expermentation but is by no means
467 // optimal (especially for code size consideration). LEA is nice because of
468 // its three-address nature. Tweak the cost function again when we can run
469 // convertToThreeAddress() at register allocation time.
470 if (AM.GV || AM.CP)
471 Complexity += 2;
472
473 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
474 Complexity++;
475
476 if (Complexity > 2) {
477 getAddressOperands(AM, Base, Scale, Index, Disp);
478 return true;
479 }
480
481 return false;
482}
483
Evan Chengd5f2ba02006-02-06 06:02:33 +0000484bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
485 SDOperand &Base, SDOperand &Scale,
486 SDOperand &Index, SDOperand &Disp) {
487 if (N.getOpcode() == ISD::LOAD &&
488 N.hasOneUse() &&
489 !CodeGenMap.count(N.getValue(0)) &&
490 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
Evan Cheng10d27902006-01-06 20:36:21 +0000491 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
492 return false;
493}
494
495static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000496 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
497 return (R->getReg() == 0);
498 return false;
499}
500
Evan Cheng5588de92006-02-18 00:15:05 +0000501/// getGlobalBaseReg - Output the instructions required to put the
502/// base address to use for accessing globals into a register.
503///
504SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
505 if (!GlobalBaseReg) {
506 // Insert the set of GlobalBaseReg into the first MBB of the function
507 MachineBasicBlock &FirstMBB = BB->getParent()->front();
508 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
509 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
510 // FIXME: when we get to LP64, we will need to create the appropriate
511 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000512 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000513 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
514 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
515 }
516 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
517}
518
Evan Chengf838cfc2006-05-20 01:36:52 +0000519static SDNode *FindCallStartFromCall(SDNode *Node) {
520 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
521 assert(Node->getOperand(0).getValueType() == MVT::Other &&
522 "Node doesn't have a token chain argument!");
523 return FindCallStartFromCall(Node->getOperand(0).Val);
524}
525
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000526void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000527 SDNode *Node = N.Val;
528 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000529 unsigned Opc, MOpc;
530 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000531
Evan Chengd49cc362006-02-10 22:24:32 +0000532#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000533 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000534 DEBUG(std::cerr << "Selecting: ");
535 DEBUG(Node->dump(CurDAG));
536 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000537 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000538#endif
539
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000540 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
541 Result = N;
Evan Chengd49cc362006-02-10 22:24:32 +0000542#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000543 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000544 DEBUG(std::cerr << "== ");
545 DEBUG(Node->dump(CurDAG));
546 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000547 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000548#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000549 return; // Already selected.
550 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000551
552 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000553 if (CGMI != CodeGenMap.end()) {
554 Result = CGMI->second;
Evan Chengd49cc362006-02-10 22:24:32 +0000555#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000556 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000557 DEBUG(std::cerr << "== ");
558 DEBUG(Result.Val->dump(CurDAG));
559 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000560 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000561#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000562 return;
563 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000564
Evan Cheng10d27902006-01-06 20:36:21 +0000565 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000566 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000567 case X86ISD::GlobalBaseReg:
568 Result = getGlobalBaseReg();
569 return;
570
Evan Cheng77d86ff2006-02-25 10:09:08 +0000571 case ISD::ADD: {
572 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
573 // code and is matched first so to prevent it from being turned into
574 // LEA32r X+c.
575 SDOperand N0 = N.getOperand(0);
576 SDOperand N1 = N.getOperand(1);
577 if (N.Val->getValueType(0) == MVT::i32 &&
578 N0.getOpcode() == X86ISD::Wrapper &&
579 N1.getOpcode() == ISD::Constant) {
580 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
581 SDOperand C(0, 0);
582 // TODO: handle ExternalSymbolSDNode.
583 if (GlobalAddressSDNode *G =
584 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
585 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
586 G->getOffset() + Offset);
587 } else if (ConstantPoolSDNode *CP =
588 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
589 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
590 CP->getAlignment(),
591 CP->getOffset()+Offset);
592 }
593
594 if (C.Val) {
595 if (N.Val->hasOneUse()) {
596 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
597 } else {
598 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
599 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
600 }
601 return;
602 }
603 }
604
605 // Other cases are handled by auto-generated code.
606 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000607 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000608
Evan Cheng10d27902006-01-06 20:36:21 +0000609 case ISD::MULHU:
610 case ISD::MULHS: {
611 if (Opcode == ISD::MULHU)
612 switch (NVT) {
613 default: assert(0 && "Unsupported VT!");
614 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
615 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
616 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
617 }
618 else
619 switch (NVT) {
620 default: assert(0 && "Unsupported VT!");
621 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
622 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
623 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
624 }
625
626 unsigned LoReg, HiReg;
627 switch (NVT) {
628 default: assert(0 && "Unsupported VT!");
629 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
630 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
631 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
632 }
633
634 SDOperand N0 = Node->getOperand(0);
635 SDOperand N1 = Node->getOperand(1);
636
637 bool foldedLoad = false;
638 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000639 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000640 // MULHU and MULHS are commmutative
641 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000642 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000643 if (foldedLoad) {
644 N0 = Node->getOperand(1);
645 N1 = Node->getOperand(0);
646 }
647 }
648
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000649 SDOperand Chain;
650 if (foldedLoad)
651 Select(Chain, N1.getOperand(0));
652 else
653 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000654
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000655 SDOperand InFlag(0, 0);
656 Select(N0, N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000657 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000658 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000659 InFlag = Chain.getValue(1);
660
661 if (foldedLoad) {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000662 Select(Tmp0, Tmp0);
663 Select(Tmp1, Tmp1);
664 Select(Tmp2, Tmp2);
665 Select(Tmp3, Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000666 SDNode *CNode =
667 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
668 Tmp2, Tmp3, Chain, InFlag);
669 Chain = SDOperand(CNode, 0);
670 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000671 } else {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000672 Select(N1, N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000673 InFlag =
674 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000675 }
676
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000677 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000678 CodeGenMap[N.getValue(0)] = Result;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000679 if (foldedLoad) {
Evan Cheng92e27972006-01-06 23:19:29 +0000680 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng101e4b92006-02-09 22:12:53 +0000681 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000682 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000683
Evan Chengd49cc362006-02-10 22:24:32 +0000684#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000685 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000686 DEBUG(std::cerr << "== ");
687 DEBUG(Result.Val->dump(CurDAG));
688 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000689 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000690#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000691 return;
Evan Cheng92e27972006-01-06 23:19:29 +0000692 }
Evan Cheng5588de92006-02-18 00:15:05 +0000693
Evan Cheng92e27972006-01-06 23:19:29 +0000694 case ISD::SDIV:
695 case ISD::UDIV:
696 case ISD::SREM:
697 case ISD::UREM: {
698 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
699 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
700 if (!isSigned)
701 switch (NVT) {
702 default: assert(0 && "Unsupported VT!");
703 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
704 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
705 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
706 }
707 else
708 switch (NVT) {
709 default: assert(0 && "Unsupported VT!");
710 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
711 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
712 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
713 }
714
715 unsigned LoReg, HiReg;
716 unsigned ClrOpcode, SExtOpcode;
717 switch (NVT) {
718 default: assert(0 && "Unsupported VT!");
719 case MVT::i8:
720 LoReg = X86::AL; HiReg = X86::AH;
721 ClrOpcode = X86::MOV8ri;
722 SExtOpcode = X86::CBW;
723 break;
724 case MVT::i16:
725 LoReg = X86::AX; HiReg = X86::DX;
726 ClrOpcode = X86::MOV16ri;
727 SExtOpcode = X86::CWD;
728 break;
729 case MVT::i32:
730 LoReg = X86::EAX; HiReg = X86::EDX;
731 ClrOpcode = X86::MOV32ri;
732 SExtOpcode = X86::CDQ;
733 break;
734 }
735
736 SDOperand N0 = Node->getOperand(0);
737 SDOperand N1 = Node->getOperand(1);
738
739 bool foldedLoad = false;
740 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000741 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000742 SDOperand Chain;
743 if (foldedLoad)
744 Select(Chain, N1.getOperand(0));
745 else
746 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000747
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000748 SDOperand InFlag(0, 0);
749 Select(N0, N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000750 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000751 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000752 InFlag = Chain.getValue(1);
753
754 if (isSigned) {
755 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000756 InFlag =
757 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000758 } else {
759 // Zero out the high part, effectively zero extending the input.
760 SDOperand ClrNode =
Evan Chengd1b82d82006-02-09 07:17:49 +0000761 SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT,
762 CurDAG->getTargetConstant(0, NVT)), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000763 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
764 ClrNode, InFlag);
765 InFlag = Chain.getValue(1);
766 }
767
768 if (foldedLoad) {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000769 Select(Tmp0, Tmp0);
770 Select(Tmp1, Tmp1);
771 Select(Tmp2, Tmp2);
772 Select(Tmp3, Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000773 SDNode *CNode =
774 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
775 Tmp2, Tmp3, Chain, InFlag);
776 Chain = SDOperand(CNode, 0);
777 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000778 } else {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000779 Select(N1, N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000780 InFlag =
781 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000782 }
783
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000784 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
785 NVT, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000786 CodeGenMap[N.getValue(0)] = Result;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000787 if (foldedLoad) {
Evan Cheng92e27972006-01-06 23:19:29 +0000788 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng101e4b92006-02-09 22:12:53 +0000789 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000790 }
Evan Chengd49cc362006-02-10 22:24:32 +0000791
792#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000793 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000794 DEBUG(std::cerr << "== ");
795 DEBUG(Result.Val->dump(CurDAG));
796 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000797 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000798#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000799 return;
Evan Cheng10d27902006-01-06 20:36:21 +0000800 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000801
802 case ISD::TRUNCATE: {
803 if (NVT == MVT::i8) {
804 unsigned Opc2;
805 MVT::ValueType VT;
806 switch (Node->getOperand(0).getValueType()) {
807 default: assert(0 && "Unknown truncate!");
808 case MVT::i16:
809 Opc = X86::MOV16to16_;
810 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +0000811 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000812 break;
813 case MVT::i32:
814 Opc = X86::MOV32to32_;
815 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +0000816 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000817 break;
818 }
819
820 SDOperand Tmp0, Tmp1;
821 Select(Tmp0, Node->getOperand(0));
822 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0);
823 Result = CodeGenMap[N] =
824 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0);
825
826#ifndef NDEBUG
827 DEBUG(std::cerr << std::string(Indent-2, ' '));
828 DEBUG(std::cerr << "== ");
829 DEBUG(Result.Val->dump(CurDAG));
830 DEBUG(std::cerr << "\n");
831 Indent -= 2;
832#endif
833 return;
834 }
835 }
Evan Chengf838cfc2006-05-20 01:36:52 +0000836
Evan Chengb9ac06b2006-05-20 01:40:16 +0000837 case X86ISD::CALL:
838 case X86ISD::TAILCALL: {
Evan Chengf838cfc2006-05-20 01:36:52 +0000839 // Handle indirect call which folds a load here. This never matches by
840 // the TableGen generated code since the load's chain result is read by
841 // the callseq_start node.
842 SDOperand N1 = Node->getOperand(1);
843 if (N1.getOpcode() == ISD::LOAD && N1.hasOneUse() &&
844 !CodeGenMap.count(N1.getValue(0))) {
845 SDOperand Chain = Node->getOperand(0);
846 SDNode *CallStart = FindCallStartFromCall(Chain.Val);
847 if (!CallStart || CallStart->getOperand(0).Val != N1.Val)
848 break;
849 SDOperand Base, Scale, Index, Disp;
850 if (SelectAddr(N1.getOperand(1), Base, Scale, Index, Disp)) {
851 Select(Base, Base);
852 Select(Scale, Scale);
853 Select(Index, Index);
854 Select(Disp, Disp);
855 Select(Chain, Chain);
856 bool HasOptInFlag = false;
857 SDOperand InFlag;
858 if (N.getNumOperands() == 3) {
859 Select(InFlag, N.getOperand(2));
860 HasOptInFlag = true;
861 }
862 SDNode *ResNode;
863 if (HasOptInFlag)
864 ResNode = CurDAG->getTargetNode(X86::CALL32m, MVT::Other, MVT::Flag,
865 Base, Scale, Index, Disp, Chain,
866 InFlag);
867 else
868 ResNode = CurDAG->getTargetNode(X86::CALL32m, MVT::Other, MVT::Flag,
869 Base, Scale, Index, Disp, Chain);
870
871 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
872 Chain.ResNo);
873 SelectionDAG::InsertISelMapEntry(CodeGenMap, N1.Val, 1, ResNode, 0);
874 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, ResNode, 1);
875 Result = SDOperand(ResNode, 0);
876
877#ifndef NDEBUG
878 DEBUG(std::cerr << std::string(Indent-2, ' '));
879 DEBUG(std::cerr << "== ");
880 DEBUG(Result.Val->dump(CurDAG));
881 DEBUG(std::cerr << "\n");
882 Indent -= 2;
883#endif
884 return;
885 }
886 }
887 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000888 }
889
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000890 SelectCode(Result, N);
Evan Chengd49cc362006-02-10 22:24:32 +0000891#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000892 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000893 DEBUG(std::cerr << "=> ");
894 DEBUG(Result.Val->dump(CurDAG));
895 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000896 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000897#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000898}
899
900/// createX86ISelDag - This pass converts a legalized DAG into a
901/// X86-specific DAG, ready for instruction scheduling.
902///
Evan Cheng2dd2c652006-03-13 23:20:37 +0000903FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000904 return new X86DAGToDAGISel(TM);
905}