blob: a74116f1b2f06c3a41a6ff7c0f8cb46a5a07608c [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 Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-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"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/MathExtras.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000036#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000037#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000038#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000039#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43// Pattern Matcher Implementation
44//===----------------------------------------------------------------------===//
45
46namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000047 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
48 /// SDOperand's instead of register numbers for the leaves of the matched
49 /// tree.
50 struct X86ISelAddressMode {
51 enum {
52 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000053 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 } BaseType;
55
56 struct { // This is really a union, discriminated by BaseType!
57 SDOperand Reg;
58 int FrameIndex;
59 } Base;
60
Evan Cheng25ab6902006-09-08 06:48:29 +000061 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 unsigned Scale;
63 SDOperand IndexReg;
64 unsigned Disp;
65 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000066 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000067 const char *ES;
68 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000069 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000070
71 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000072 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
73 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 }
75 };
76}
77
78namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000079 Statistic<>
80 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
81
Evan Cheng82a35b32006-08-29 06:44:17 +000082 Statistic<>
83 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
84
Chris Lattnerc961eea2005-11-16 01:54:32 +000085 //===--------------------------------------------------------------------===//
86 /// ISel - X86 specific code to select X86 machine instructions for
87 /// SelectionDAG operations.
88 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000089 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000090 /// ContainsFPCode - Every instruction we select that uses or defines a FP
91 /// register should set this to true.
92 bool ContainsFPCode;
93
Evan Chenge50794a2006-08-29 18:28:33 +000094 /// FastISel - Enable fast(er) instruction selection.
95 ///
96 bool FastISel;
97
Evan Cheng25ab6902006-09-08 06:48:29 +000098 /// TM - Keep a reference to X86TargetMachine.
99 ///
100 X86TargetMachine &TM;
101
Chris Lattnerc961eea2005-11-16 01:54:32 +0000102 /// X86Lowering - This object fully describes how to lower LLVM code to an
103 /// X86-specific SelectionDAG.
104 X86TargetLowering X86Lowering;
105
106 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
107 /// make the right decision when generating code for different targets.
108 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000109
Evan Cheng25ab6902006-09-08 06:48:29 +0000110 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
111 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000112 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000113
Chris Lattnerc961eea2005-11-16 01:54:32 +0000114 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000116 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000117 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000118 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000119 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000120
Evan Cheng7ccced62006-02-18 00:15:05 +0000121 virtual bool runOnFunction(Function &Fn) {
122 // Make sure we re-emit a set of the global base reg if necessary
123 GlobalBaseReg = 0;
124 return SelectionDAGISel::runOnFunction(Fn);
125 }
126
Chris Lattnerc961eea2005-11-16 01:54:32 +0000127 virtual const char *getPassName() const {
128 return "X86 DAG->DAG Instruction Selection";
129 }
130
131 /// InstructionSelectBasicBlock - This callback is invoked by
132 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
133 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
134
Evan Cheng8700e142006-01-11 06:09:51 +0000135 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
136
Evan Chengf2dfafc2006-07-28 01:03:48 +0000137 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000138
Chris Lattnerc961eea2005-11-16 01:54:32 +0000139// Include the pieces autogenerated from the target description.
140#include "X86GenDAGISel.inc"
141
142 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000143 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000144
Evan Cheng2486af12006-02-11 02:05:36 +0000145 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengec693f72005-12-08 02:01:35 +0000146 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
147 SDOperand &Index, SDOperand &Disp);
148 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
149 SDOperand &Index, SDOperand &Disp);
Chris Lattner3a7cd952006-10-07 21:55:32 +0000150 bool SelectScalarSSELoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
151 SDOperand &Index, SDOperand &Disp);
Evan Cheng5e351682006-02-06 06:02:33 +0000152 bool TryFoldLoad(SDOperand P, SDOperand N,
153 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000154 SDOperand &Index, SDOperand &Disp);
Evan Cheng70e674e2006-08-28 20:10:17 +0000155 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000156
Chris Lattnerc0bad572006-06-08 18:03:49 +0000157 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
158 /// inline asm expressions.
159 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
160 char ConstraintCode,
161 std::vector<SDOperand> &OutOps,
162 SelectionDAG &DAG);
163
Evan Cheng3649b0e2006-06-02 22:38:37 +0000164 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
165
Evan Chenge5280532005-12-12 21:49:40 +0000166 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
167 SDOperand &Scale, SDOperand &Index,
168 SDOperand &Disp) {
169 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000170 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
171 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000172 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000173 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 // These are 32-bit even in 64-bit mode since RIP relative offset
175 // is 32-bit.
176 if (AM.GV)
177 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
178 else if (AM.CP)
179 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
180 else if (AM.ES)
181 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
182 else if (AM.JT != -1)
183 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
184 else
185 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000186 }
187
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000188 /// getI8Imm - Return a target constant with the specified value, of type
189 /// i8.
190 inline SDOperand getI8Imm(unsigned Imm) {
191 return CurDAG->getTargetConstant(Imm, MVT::i8);
192 }
193
Chris Lattnerc961eea2005-11-16 01:54:32 +0000194 /// getI16Imm - Return a target constant with the specified value, of type
195 /// i16.
196 inline SDOperand getI16Imm(unsigned Imm) {
197 return CurDAG->getTargetConstant(Imm, MVT::i16);
198 }
199
200 /// getI32Imm - Return a target constant with the specified value, of type
201 /// i32.
202 inline SDOperand getI32Imm(unsigned Imm) {
203 return CurDAG->getTargetConstant(Imm, MVT::i32);
204 }
Evan Chengf597dc72006-02-10 22:24:32 +0000205
Evan Cheng7ccced62006-02-18 00:15:05 +0000206 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
207 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000208 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000209
Evan Cheng23addc02006-02-10 22:46:26 +0000210#ifndef NDEBUG
211 unsigned Indent;
212#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000213 };
214}
215
Evan Chenga275ecb2006-10-10 01:46:56 +0000216static SDNode *findFlagUse(SDNode *N) {
217 unsigned FlagResNo = N->getNumValues()-1;
218 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
219 SDNode *User = *I;
220 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
221 SDOperand Op = User->getOperand(i);
222 if (Op.ResNo == FlagResNo)
223 return User;
224 }
225 }
226 return NULL;
227}
228
229static void findNonImmUse(SDNode* Use, SDNode* Def, SDNode *Ignore, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000230 std::set<SDNode *> &Visited) {
231 if (found ||
232 Use->getNodeId() > Def->getNodeId() ||
233 !Visited.insert(Use).second)
234 return;
235
236 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
237 SDNode *N = Use->getOperand(i).Val;
Evan Chenga275ecb2006-10-10 01:46:56 +0000238 if (N == Ignore)
239 continue;
Evan Chengf4b4c412006-08-08 00:31:00 +0000240 if (N != Def) {
Evan Chenga275ecb2006-10-10 01:46:56 +0000241 findNonImmUse(N, Def, Ignore, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000242 } else {
243 found = true;
244 break;
245 }
246 }
247}
248
Evan Chenga275ecb2006-10-10 01:46:56 +0000249static inline bool isNonImmUse(SDNode* Use, SDNode* Def, SDNode *Ignore=NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000250 std::set<SDNode *> Visited;
251 bool found = false;
252 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
253 SDNode *N = Use->getOperand(i).Val;
Evan Chenga275ecb2006-10-10 01:46:56 +0000254 if (N != Def && N != Ignore) {
255 findNonImmUse(N, Def, Ignore, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000256 if (found) break;
257 }
258 }
Evan Chenga275ecb2006-10-10 01:46:56 +0000259
260 if (!found && Ignore) {
261 // We must be checking for reachability between Def and a flag use. Go down
262 // recursively if Use also produces a flag.
263 MVT::ValueType VT = Use->getValueType(Use->getNumValues()-1);
264 if (VT == MVT::Flag && !Use->use_empty()) {
265 SDNode *FU = findFlagUse(Use);
266 if (FU)
267 return !isNonImmUse(FU, Def, Use);
268 }
269 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000270 return found;
271}
272
273
Evan Chengf2dfafc2006-07-28 01:03:48 +0000274bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Chenga8df1b42006-07-27 16:44:36 +0000275 // If U use can somehow reach N through another path then U can't fold N or
276 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000277 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000278 // a successor of U.
279 //
280 // [ N ]
281 // ^ ^
282 // | |
283 // / \---
284 // / [X]
285 // | ^
286 // [U]--------|
Evan Chenga275ecb2006-10-10 01:46:56 +0000287 if (!FastISel && !isNonImmUse(U, N)) {
288 // If U produces a flag, then it gets (even more) interesting. Since it
289 // would have been "glued" together with its flag use, we need to check if
290 // it might reach N:
291 //
292 // [ N ]
293 // ^ ^
294 // | |
295 // [U] \--
296 // ^ [TF]
297 // | |
298 // \ /
299 // [FU]
300 //
301 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
302 // NU), then TF is a predecessor of FU and a successor of NU. But since
303 // NU and FU are flagged together, this effectively creates a cycle.
304 MVT::ValueType VT = U->getValueType(U->getNumValues()-1);
305 if (VT == MVT::Flag && !U->use_empty()) {
306 SDNode *FU = findFlagUse(U);
307 if (FU)
308 return !isNonImmUse(FU, N, U);
309 }
310 return true;
311 }
312 return false;
Evan Chenga8df1b42006-07-27 16:44:36 +0000313}
314
Evan Cheng70e674e2006-08-28 20:10:17 +0000315/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
316/// and move load below the TokenFactor. Replace store's chain operand with
317/// load's chain result.
318static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
319 SDOperand Store, SDOperand TF) {
320 std::vector<SDOperand> Ops;
321 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
322 if (Load.Val == TF.Val->getOperand(i).Val)
323 Ops.push_back(Load.Val->getOperand(0));
324 else
325 Ops.push_back(TF.Val->getOperand(i));
326 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
327 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
328 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
329 Store.getOperand(2), Store.getOperand(3));
330}
331
332/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
333/// selector to pick more load-modify-store instructions. This is a common
334/// case:
335///
336/// [Load chain]
337/// ^
338/// |
339/// [Load]
340/// ^ ^
341/// | |
342/// / \-
343/// / |
344/// [TokenFactor] [Op]
345/// ^ ^
346/// | |
347/// \ /
348/// \ /
349/// [Store]
350///
351/// The fact the store's chain operand != load's chain will prevent the
352/// (store (op (load))) instruction from being selected. We can transform it to:
353///
354/// [Load chain]
355/// ^
356/// |
357/// [TokenFactor]
358/// ^
359/// |
360/// [Load]
361/// ^ ^
362/// | |
363/// | \-
364/// | |
365/// | [Op]
366/// | ^
367/// | |
368/// \ /
369/// \ /
370/// [Store]
371void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
372 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
373 E = DAG.allnodes_end(); I != E; ++I) {
374 if (I->getOpcode() != ISD::STORE)
375 continue;
376 SDOperand Chain = I->getOperand(0);
377 if (Chain.Val->getOpcode() != ISD::TokenFactor)
378 continue;
379
380 SDOperand N1 = I->getOperand(1);
381 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000382 if (MVT::isFloatingPoint(N1.getValueType()) ||
383 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000384 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000385 continue;
386
387 bool RModW = false;
388 SDOperand Load;
389 unsigned Opcode = N1.Val->getOpcode();
390 switch (Opcode) {
391 case ISD::ADD:
392 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000393 case ISD::AND:
394 case ISD::OR:
395 case ISD::XOR:
396 case ISD::ADDC:
397 case ISD::ADDE: {
398 SDOperand N10 = N1.getOperand(0);
399 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000400 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000401 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000402 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000403 RModW = true;
404 std::swap(N10, N11);
405 }
406 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000407 (N10.getOperand(1) == N2) &&
408 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000409 if (RModW)
410 Load = N10;
411 break;
412 }
413 case ISD::SUB:
414 case ISD::SHL:
415 case ISD::SRA:
416 case ISD::SRL:
417 case ISD::ROTL:
418 case ISD::ROTR:
419 case ISD::SUBC:
420 case ISD::SUBE:
421 case X86ISD::SHLD:
422 case X86ISD::SHRD: {
423 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000424 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000425 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000426 (N10.getOperand(1) == N2) &&
427 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000428 if (RModW)
429 Load = N10;
430 break;
431 }
432 }
433
Evan Cheng82a35b32006-08-29 06:44:17 +0000434 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000435 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000436 ++NumLoadMoved;
437 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000438 }
439}
440
Chris Lattnerc961eea2005-11-16 01:54:32 +0000441/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
442/// when it has created a SelectionDAG for us to codegen.
443void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
444 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000445 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000446
Evan Chenge50794a2006-08-29 18:28:33 +0000447 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000448 InstructionSelectPreprocess(DAG);
449
Chris Lattnerc961eea2005-11-16 01:54:32 +0000450 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000451#ifndef NDEBUG
452 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000453 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000454#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000455 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000456#ifndef NDEBUG
457 DEBUG(std::cerr << "===== Instruction selection ends:\n");
458#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000459
Chris Lattnerc961eea2005-11-16 01:54:32 +0000460 DAG.RemoveDeadNodes();
461
462 // Emit machine code to BB.
463 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000464
465 // If we are emitting FP stack code, scan the basic block to determine if this
466 // block defines any FP values. If so, put an FP_REG_KILL instruction before
467 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000468 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000469 // Note that FP stack instructions *are* used in SSE code when returning
470 // values, but these are not live out of the basic block, so we don't need
471 // an FP_REG_KILL in this case either.
472 bool ContainsFPCode = false;
473
474 // Scan all of the machine instructions in these MBBs, checking for FP
475 // stores.
476 MachineFunction::iterator MBBI = FirstMBB;
477 do {
478 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
479 !ContainsFPCode && I != E; ++I) {
480 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
481 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
482 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
483 RegMap->getRegClass(I->getOperand(0).getReg()) ==
484 X86::RFPRegisterClass) {
485 ContainsFPCode = true;
486 break;
487 }
488 }
489 }
490 } while (!ContainsFPCode && &*(MBBI++) != BB);
491
492 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
493 // a copy of the input value in this block.
494 if (!ContainsFPCode) {
495 // Final check, check LLVM BB's that are successors to the LLVM BB
496 // corresponding to BB for FP PHI nodes.
497 const BasicBlock *LLVMBB = BB->getBasicBlock();
498 const PHINode *PN;
499 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
500 !ContainsFPCode && SI != E; ++SI) {
501 for (BasicBlock::const_iterator II = SI->begin();
502 (PN = dyn_cast<PHINode>(II)); ++II) {
503 if (PN->getType()->isFloatingPoint()) {
504 ContainsFPCode = true;
505 break;
506 }
507 }
508 }
509 }
510
511 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
512 if (ContainsFPCode) {
513 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
514 ++NumFPKill;
515 }
516 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000517}
518
Evan Cheng8700e142006-01-11 06:09:51 +0000519/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
520/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000521void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
522 MachineFrameInfo *MFI) {
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000523 if (Subtarget->isTargetCygwin())
Evan Cheng3649b0e2006-06-02 22:38:37 +0000524 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
525
Evan Cheng8700e142006-01-11 06:09:51 +0000526 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
527 int CWFrameIdx = MFI->CreateStackObject(2, 2);
528 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
529
530 // Set the high part to be 64-bit precision.
531 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
532 CWFrameIdx, 1).addImm(2);
533
534 // Reload the modified control word now.
535 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
536}
537
538void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
539 // If this is main, emit special code for main.
540 MachineBasicBlock *BB = MF.begin();
541 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
542 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
543}
544
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000545/// MatchAddress - Add the specified node to the specified addressing mode,
546/// returning true if it cannot be done. This just pattern matches for the
547/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000548bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
549 bool isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000550 // RIP relative addressing: %rip + 32-bit displacement!
551 if (AM.isRIPRel) {
552 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000553 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000554 if (isInt32(AM.Disp + Val)) {
555 AM.Disp += Val;
556 return false;
557 }
558 }
559 return true;
560 }
561
Evan Cheng2ef88a02006-08-07 22:28:20 +0000562 int id = N.Val->getNodeId();
563 bool Available = isSelected(id);
Evan Cheng2486af12006-02-11 02:05:36 +0000564
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000565 switch (N.getOpcode()) {
566 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000567 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000568 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000569 if (isInt32(AM.Disp + Val)) {
570 AM.Disp += Val;
571 return false;
572 }
573 break;
574 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000575
576 case X86ISD::Wrapper:
Evan Cheng25ab6902006-09-08 06:48:29 +0000577 // If value is available in a register both base and index components have
578 // been picked, we can't fit the result available in the register in the
579 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
580
581 // Can't fit GV or CP in addressing mode for X86-64 medium or large code
582 // model since the displacement field is 32-bit. Ok for small code model.
583
584 // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP
585 // relative addressing mode.
586 if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) &&
587 (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) {
588 bool isRIP = Subtarget->is64Bit();
589 if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val ||
590 AM.BaseType == X86ISelAddressMode::FrameIndexBase))
591 break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000592 if (ConstantPoolSDNode *CP =
593 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
594 if (AM.CP == 0) {
Evan Chengc356a572006-09-12 21:04:05 +0000595 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000596 AM.Align = CP->getAlignment();
597 AM.Disp += CP->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000598 if (isRIP)
599 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000600 return false;
601 }
602 } else if (GlobalAddressSDNode *G =
603 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
604 if (AM.GV == 0) {
605 AM.GV = G->getGlobal();
606 AM.Disp += G->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000607 if (isRIP)
608 AM.isRIPRel = true;
609 return false;
610 }
611 } else if (isRoot && isRIP) {
612 if (ExternalSymbolSDNode *S =
613 dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) {
614 AM.ES = S->getSymbol();
615 AM.isRIPRel = true;
616 return false;
617 } else if (JumpTableSDNode *J =
618 dyn_cast<JumpTableSDNode>(N.getOperand(0))) {
619 AM.JT = J->getIndex();
620 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000621 return false;
622 }
623 }
624 }
625 break;
626
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000627 case ISD::FrameIndex:
628 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
629 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
630 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
631 return false;
632 }
633 break;
Evan Chengec693f72005-12-08 02:01:35 +0000634
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000635 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000636 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000637 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
638 unsigned Val = CN->getValue();
639 if (Val == 1 || Val == 2 || Val == 3) {
640 AM.Scale = 1 << Val;
641 SDOperand ShVal = N.Val->getOperand(0);
642
643 // Okay, we know that we have a scale by now. However, if the scaled
644 // value is an add of something and a constant, we can fold the
645 // constant into the disp field here.
646 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
647 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
648 AM.IndexReg = ShVal.Val->getOperand(0);
649 ConstantSDNode *AddVal =
650 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000651 uint64_t Disp = AM.Disp + AddVal->getValue() << Val;
652 if (isInt32(Disp))
653 AM.Disp = Disp;
654 else
655 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000656 } else {
657 AM.IndexReg = ShVal;
658 }
659 return false;
660 }
661 }
662 break;
Evan Chengec693f72005-12-08 02:01:35 +0000663
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000664 case ISD::MUL:
665 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000666 if (!Available &&
667 AM.BaseType == X86ISelAddressMode::RegBase &&
668 AM.Base.Reg.Val == 0 &&
669 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000670 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
671 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
672 AM.Scale = unsigned(CN->getValue())-1;
673
674 SDOperand MulVal = N.Val->getOperand(0);
675 SDOperand Reg;
676
677 // Okay, we know that we have a scale by now. However, if the scaled
678 // value is an add of something and a constant, we can fold the
679 // constant into the disp field here.
680 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
681 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
682 Reg = MulVal.Val->getOperand(0);
683 ConstantSDNode *AddVal =
684 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000685 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
686 if (isInt32(Disp))
687 AM.Disp = Disp;
688 else
689 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000690 } else {
691 Reg = N.Val->getOperand(0);
692 }
693
694 AM.IndexReg = AM.Base.Reg = Reg;
695 return false;
696 }
697 break;
698
699 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000700 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000701 X86ISelAddressMode Backup = AM;
702 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
703 !MatchAddress(N.Val->getOperand(1), AM, false))
704 return false;
705 AM = Backup;
706 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
707 !MatchAddress(N.Val->getOperand(0), AM, false))
708 return false;
709 AM = Backup;
710 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000711 break;
712 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000713
714 case ISD::OR: {
715 if (!Available) {
716 X86ISelAddressMode Backup = AM;
717 // Look for (x << c1) | c2 where (c2 < c1)
718 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
719 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
720 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
721 AM.Disp = CN->getValue();
722 return false;
723 }
724 }
725 AM = Backup;
726 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
727 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
728 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
729 AM.Disp = CN->getValue();
730 return false;
731 }
732 }
733 AM = Backup;
734 }
735 break;
736 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000737 }
738
739 // Is the base register already occupied?
740 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
741 // If so, check to see if the scale index register is set.
742 if (AM.IndexReg.Val == 0) {
743 AM.IndexReg = N;
744 AM.Scale = 1;
745 return false;
746 }
747
748 // Otherwise, we cannot select it.
749 return true;
750 }
751
752 // Default, generate it as a register.
753 AM.BaseType = X86ISelAddressMode::RegBase;
754 AM.Base.Reg = N;
755 return false;
756}
757
Evan Chengec693f72005-12-08 02:01:35 +0000758/// SelectAddr - returns true if it is able pattern match an addressing mode.
759/// It returns the operands which make up the maximal addressing mode it can
760/// match by reference.
761bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
762 SDOperand &Index, SDOperand &Disp) {
763 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000764 if (MatchAddress(N, AM))
765 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000766
Evan Cheng25ab6902006-09-08 06:48:29 +0000767 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000768 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000769 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000770 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000771 }
Evan Cheng8700e142006-01-11 06:09:51 +0000772
Evan Cheng7dd281b2006-02-05 05:25:07 +0000773 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000774 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000775
776 getAddressOperands(AM, Base, Scale, Index, Disp);
777 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000778}
779
Chris Lattner3a7cd952006-10-07 21:55:32 +0000780/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
781/// match a load whose top elements are either undef or zeros. The load flavor
782/// is derived from the type of N, which is either v4f32 or v2f64.
783bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand N, SDOperand &Base,
784 SDOperand &Scale,
785 SDOperand &Index, SDOperand &Disp) {
786#if 0
787 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
788 if (N.getOperand(0).getOpcode() == ISD::LOAD) {
789 SDOperand LoadAddr = N.getOperand(0).getOperand(0);
790 if (!SelectAddr(LoadAddr, Base, Scale, Index, Disp))
791 return false;
792 return true;
793 }
794 }
795 // TODO: Also handle the case where we explicitly require zeros in the top
796 // elements. This is a vector shuffle from the zero vector.
797#endif
798
799 return false;
800}
801
802
Evan Cheng51a9ed92006-02-25 10:09:08 +0000803/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
804/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng51a9ed92006-02-25 10:09:08 +0000805bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
806 SDOperand &Scale,
807 SDOperand &Index, SDOperand &Disp) {
808 X86ISelAddressMode AM;
809 if (MatchAddress(N, AM))
810 return false;
811
Evan Cheng25ab6902006-09-08 06:48:29 +0000812 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000813 unsigned Complexity = 0;
814 if (AM.BaseType == X86ISelAddressMode::RegBase)
815 if (AM.Base.Reg.Val)
816 Complexity = 1;
817 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000818 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000819 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
820 Complexity = 4;
821
822 if (AM.IndexReg.Val)
823 Complexity++;
824 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000825 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000826
Evan Cheng8c03fe42006-02-28 21:13:57 +0000827 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000828 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000829 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
830 else if (AM.Scale > 1)
831 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000832
833 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
834 // to a LEA. This is determined with some expermentation but is by no means
835 // optimal (especially for code size consideration). LEA is nice because of
836 // its three-address nature. Tweak the cost function again when we can run
837 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000838 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
839 // For X86-64, we should always use lea to materialize RIP relative
840 // addresses.
841 if (Subtarget->is64Bit())
842 Complexity = 4;
843 else
844 Complexity += 2;
845 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000846
847 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
848 Complexity++;
849
850 if (Complexity > 2) {
851 getAddressOperands(AM, Base, Scale, Index, Disp);
852 return true;
853 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000854 return false;
855}
856
Evan Cheng5e351682006-02-06 06:02:33 +0000857bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
858 SDOperand &Base, SDOperand &Scale,
859 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000860 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000861 N.hasOneUse() &&
Evan Chengeb8730d2006-08-16 23:59:00 +0000862 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng0114e942006-01-06 20:36:21 +0000863 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
864 return false;
865}
866
867static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000868 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
869 return (R->getReg() == 0);
870 return false;
871}
872
Evan Cheng7ccced62006-02-18 00:15:05 +0000873/// getGlobalBaseReg - Output the instructions required to put the
874/// base address to use for accessing globals into a register.
875///
Evan Cheng9ade2182006-08-26 05:34:46 +0000876SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000877 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000878 if (!GlobalBaseReg) {
879 // Insert the set of GlobalBaseReg into the first MBB of the function
880 MachineBasicBlock &FirstMBB = BB->getParent()->front();
881 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
882 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
883 // FIXME: when we get to LP64, we will need to create the appropriate
884 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000885 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000886 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
887 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
888 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000889 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000890}
891
Evan Chengb245d922006-05-20 01:36:52 +0000892static SDNode *FindCallStartFromCall(SDNode *Node) {
893 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
894 assert(Node->getOperand(0).getValueType() == MVT::Other &&
895 "Node doesn't have a token chain argument!");
896 return FindCallStartFromCall(Node->getOperand(0).Val);
897}
898
Evan Cheng9ade2182006-08-26 05:34:46 +0000899SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000900 SDNode *Node = N.Val;
901 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000902 unsigned Opc, MOpc;
903 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000904
Evan Chengf597dc72006-02-10 22:24:32 +0000905#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +0000906 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000907 DEBUG(std::cerr << "Selecting: ");
908 DEBUG(Node->dump(CurDAG));
909 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000910 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000911#endif
912
Evan Cheng34167212006-02-09 00:37:58 +0000913 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000914#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000915 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000916 DEBUG(std::cerr << "== ");
917 DEBUG(Node->dump(CurDAG));
918 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000919 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000920#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000921 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000922 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000923
Evan Cheng0114e942006-01-06 20:36:21 +0000924 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000925 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000926 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000927 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000928
Evan Cheng51a9ed92006-02-25 10:09:08 +0000929 case ISD::ADD: {
930 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
931 // code and is matched first so to prevent it from being turned into
932 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000933 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
934 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000935 SDOperand N0 = N.getOperand(0);
936 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +0000937 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000938 N0.getOpcode() == X86ISD::Wrapper &&
939 N1.getOpcode() == ISD::Constant) {
940 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
941 SDOperand C(0, 0);
942 // TODO: handle ExternalSymbolSDNode.
943 if (GlobalAddressSDNode *G =
944 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000945 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000946 G->getOffset() + Offset);
947 } else if (ConstantPoolSDNode *CP =
948 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +0000949 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000950 CP->getAlignment(),
951 CP->getOffset()+Offset);
952 }
953
Evan Cheng25ab6902006-09-08 06:48:29 +0000954 if (C.Val) {
955 if (Subtarget->is64Bit()) {
956 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
957 CurDAG->getRegister(0, PtrVT), C };
958 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
959 } else
960 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
961 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000962 }
963
964 // Other cases are handled by auto-generated code.
965 break;
Evan Chenga0ea0532006-02-23 02:43:52 +0000966 }
Evan Cheng020d2e82006-02-23 20:41:18 +0000967
Evan Cheng0114e942006-01-06 20:36:21 +0000968 case ISD::MULHU:
969 case ISD::MULHS: {
970 if (Opcode == ISD::MULHU)
971 switch (NVT) {
972 default: assert(0 && "Unsupported VT!");
973 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
974 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
975 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000976 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +0000977 }
978 else
979 switch (NVT) {
980 default: assert(0 && "Unsupported VT!");
981 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
982 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
983 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000984 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +0000985 }
986
987 unsigned LoReg, HiReg;
988 switch (NVT) {
989 default: assert(0 && "Unsupported VT!");
990 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
991 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
992 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000993 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +0000994 }
995
996 SDOperand N0 = Node->getOperand(0);
997 SDOperand N1 = Node->getOperand(1);
998
999 bool foldedLoad = false;
1000 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001001 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001002 // MULHU and MULHS are commmutative
1003 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001004 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001005 if (foldedLoad) {
1006 N0 = Node->getOperand(1);
1007 N1 = Node->getOperand(0);
1008 }
1009 }
1010
Evan Cheng34167212006-02-09 00:37:58 +00001011 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001012 if (foldedLoad) {
1013 Chain = N1.getOperand(0);
1014 AddToISelQueue(Chain);
1015 } else
Evan Cheng34167212006-02-09 00:37:58 +00001016 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001017
Evan Cheng34167212006-02-09 00:37:58 +00001018 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001019 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001020 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001021 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001022 InFlag = Chain.getValue(1);
1023
1024 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001025 AddToISelQueue(Tmp0);
1026 AddToISelQueue(Tmp1);
1027 AddToISelQueue(Tmp2);
1028 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001029 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001030 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001031 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001032 Chain = SDOperand(CNode, 0);
1033 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001034 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001035 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001036 InFlag =
1037 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001038 }
1039
Evan Cheng9ade2182006-08-26 05:34:46 +00001040 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001041 ReplaceUses(N.getValue(0), Result);
1042 if (foldedLoad)
1043 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001044
Evan Chengf597dc72006-02-10 22:24:32 +00001045#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001046 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001047 DEBUG(std::cerr << "=> ");
Evan Chengf597dc72006-02-10 22:24:32 +00001048 DEBUG(Result.Val->dump(CurDAG));
1049 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001050 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001051#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001052 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001053 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001054
Evan Cheng948f3432006-01-06 23:19:29 +00001055 case ISD::SDIV:
1056 case ISD::UDIV:
1057 case ISD::SREM:
1058 case ISD::UREM: {
1059 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1060 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1061 if (!isSigned)
1062 switch (NVT) {
1063 default: assert(0 && "Unsupported VT!");
1064 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1065 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1066 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001067 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001068 }
1069 else
1070 switch (NVT) {
1071 default: assert(0 && "Unsupported VT!");
1072 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1073 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1074 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001075 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001076 }
1077
1078 unsigned LoReg, HiReg;
1079 unsigned ClrOpcode, SExtOpcode;
1080 switch (NVT) {
1081 default: assert(0 && "Unsupported VT!");
1082 case MVT::i8:
1083 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengaede9b92006-06-02 21:20:34 +00001084 ClrOpcode = X86::MOV8r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001085 SExtOpcode = X86::CBW;
1086 break;
1087 case MVT::i16:
1088 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001089 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001090 SExtOpcode = X86::CWD;
1091 break;
1092 case MVT::i32:
1093 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001094 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001095 SExtOpcode = X86::CDQ;
1096 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001097 case MVT::i64:
1098 LoReg = X86::RAX; HiReg = X86::RDX;
1099 ClrOpcode = X86::MOV64r0;
1100 SExtOpcode = X86::CQO;
1101 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001102 }
1103
1104 SDOperand N0 = Node->getOperand(0);
1105 SDOperand N1 = Node->getOperand(1);
1106
1107 bool foldedLoad = false;
1108 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001109 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng34167212006-02-09 00:37:58 +00001110 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001111 if (foldedLoad) {
1112 Chain = N1.getOperand(0);
1113 AddToISelQueue(Chain);
1114 } else
Evan Cheng34167212006-02-09 00:37:58 +00001115 Chain = CurDAG->getEntryNode();
Evan Cheng948f3432006-01-06 23:19:29 +00001116
Evan Cheng34167212006-02-09 00:37:58 +00001117 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001118 AddToISelQueue(N0);
Evan Cheng948f3432006-01-06 23:19:29 +00001119 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001120 N0, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001121 InFlag = Chain.getValue(1);
1122
1123 if (isSigned) {
1124 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001125 InFlag =
1126 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001127 } else {
1128 // Zero out the high part, effectively zero extending the input.
Evan Chengaede9b92006-06-02 21:20:34 +00001129 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001130 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
1131 ClrNode, InFlag);
1132 InFlag = Chain.getValue(1);
1133 }
1134
1135 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001136 AddToISelQueue(Tmp0);
1137 AddToISelQueue(Tmp1);
1138 AddToISelQueue(Tmp2);
1139 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001140 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001141 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001142 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001143 Chain = SDOperand(CNode, 0);
1144 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001145 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001146 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001147 InFlag =
1148 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001149 }
1150
Evan Cheng9ade2182006-08-26 05:34:46 +00001151 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
1152 NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001153 ReplaceUses(N.getValue(0), Result);
1154 if (foldedLoad)
1155 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001156
1157#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001158 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001159 DEBUG(std::cerr << "=> ");
Evan Chengf597dc72006-02-10 22:24:32 +00001160 DEBUG(Result.Val->dump(CurDAG));
1161 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001162 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001163#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001164
1165 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001166 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001167
1168 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001169 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001170 unsigned Opc2;
1171 MVT::ValueType VT;
1172 switch (Node->getOperand(0).getValueType()) {
1173 default: assert(0 && "Unknown truncate!");
1174 case MVT::i16:
1175 Opc = X86::MOV16to16_;
1176 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001177 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001178 break;
1179 case MVT::i32:
1180 Opc = X86::MOV32to32_;
1181 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001182 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001183 break;
1184 }
1185
Evan Cheng04699902006-08-26 01:05:16 +00001186 AddToISelQueue(Node->getOperand(0));
1187 SDOperand Tmp =
1188 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001189 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001190
1191#ifndef NDEBUG
1192 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001193 DEBUG(std::cerr << "=> ");
Evan Cheng9ade2182006-08-26 05:34:46 +00001194 DEBUG(ResNode->dump(CurDAG));
Evan Cheng403be7e2006-05-08 08:01:26 +00001195 DEBUG(std::cerr << "\n");
1196 Indent -= 2;
1197#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001198 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001199 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001200
1201 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001202 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001203 }
1204
Evan Cheng9ade2182006-08-26 05:34:46 +00001205 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001206
Evan Chengf597dc72006-02-10 22:24:32 +00001207#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001208 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +00001209 DEBUG(std::cerr << "=> ");
Evan Cheng9ade2182006-08-26 05:34:46 +00001210 if (ResNode == NULL || ResNode == N.Val)
1211 DEBUG(N.Val->dump(CurDAG));
1212 else
1213 DEBUG(ResNode->dump(CurDAG));
Evan Chengf597dc72006-02-10 22:24:32 +00001214 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001215 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001216#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001217
1218 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001219}
1220
Chris Lattnerc0bad572006-06-08 18:03:49 +00001221bool X86DAGToDAGISel::
1222SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1223 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1224 SDOperand Op0, Op1, Op2, Op3;
1225 switch (ConstraintCode) {
1226 case 'o': // offsetable ??
1227 case 'v': // not offsetable ??
1228 default: return true;
1229 case 'm': // memory
1230 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1231 return true;
1232 break;
1233 }
1234
Evan Cheng04699902006-08-26 01:05:16 +00001235 OutOps.push_back(Op0);
1236 OutOps.push_back(Op1);
1237 OutOps.push_back(Op2);
1238 OutOps.push_back(Op3);
1239 AddToISelQueue(Op0);
1240 AddToISelQueue(Op1);
1241 AddToISelQueue(Op2);
1242 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001243 return false;
1244}
1245
Chris Lattnerc961eea2005-11-16 01:54:32 +00001246/// createX86ISelDag - This pass converts a legalized DAG into a
1247/// X86-specific DAG, ready for instruction scheduling.
1248///
Evan Chenge50794a2006-08-29 18:28:33 +00001249FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1250 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001251}