blob: 76aa0da2b3fe55ac336ddb1d987d11f2f27466d6 [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 Chengb9d34bd2006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-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"
Chris Lattner3d27be12006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/MathExtras.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000036#include "llvm/ADT/Statistic.h"
Chris Lattnerde02d772006-01-22 23:41:00 +000037#include <iostream>
Evan Chengb9d34bd2006-08-07 22:28:20 +000038#include <queue>
Evan Cheng54cb1832006-02-05 06:46:41 +000039#include <set>
Chris Lattner655e7df2005-11-16 01:54:32 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43// Pattern Matcher Implementation
44//===----------------------------------------------------------------------===//
45
46namespace {
Chris Lattner3f0f71b2005-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 Lattneraa2372562006-05-24 17:04:05 +000053 FrameIndexBase
Chris Lattner3f0f71b2005-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 Cheng11b0a5d2006-09-08 06:48:29 +000061 bool isRIPRel; // RIP relative?
Chris Lattner3f0f71b2005-11-19 02:11:08 +000062 unsigned Scale;
63 SDOperand IndexReg;
64 unsigned Disp;
65 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000066 Constant *CP;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000067 const char *ES;
68 int JT;
Evan Cheng77d86ff2006-02-25 10:09:08 +000069 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000070
71 X86ISelAddressMode()
Evan Cheng11b0a5d2006-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 Lattner3f0f71b2005-11-19 02:11:08 +000074 }
75 };
76}
77
78namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000079 Statistic<>
80 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
81
Evan Chengc07feb142006-08-29 06:44:17 +000082 Statistic<>
83 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
84
Chris Lattner655e7df2005-11-16 01:54:32 +000085 //===--------------------------------------------------------------------===//
86 /// ISel - X86 specific code to select X86 machine instructions for
87 /// SelectionDAG operations.
88 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000089 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-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 Cheng358b9ed2006-08-29 18:28:33 +000094 /// FastISel - Enable fast(er) instruction selection.
95 ///
96 bool FastISel;
97
Evan Cheng11b0a5d2006-09-08 06:48:29 +000098 /// TM - Keep a reference to X86TargetMachine.
99 ///
100 X86TargetMachine &TM;
101
Chris Lattner655e7df2005-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 Cheng5588de92006-02-18 00:15:05 +0000109
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000110 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
111 /// base register.
Evan Cheng5588de92006-02-18 00:15:05 +0000112 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +0000113
Chris Lattner655e7df2005-11-16 01:54:32 +0000114 public:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000115 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng2dd2c652006-03-13 23:20:37 +0000116 : SelectionDAGISel(X86Lowering),
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000117 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Cheng691a63d2006-07-27 16:44:36 +0000118 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000119 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000120
Evan Cheng5588de92006-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 Lattner655e7df2005-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 Chengbc7a0f442006-01-11 06:09:51 +0000135 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
136
Evan Chenge2a3f702006-07-28 01:03:48 +0000137 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Cheng691a63d2006-07-27 16:44:36 +0000138
Chris Lattner655e7df2005-11-16 01:54:32 +0000139// Include the pieces autogenerated from the target description.
140#include "X86GenDAGISel.inc"
141
142 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000143 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000144
Evan Chenga86ba852006-02-11 02:05:36 +0000145 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-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 Lattner398195e2006-10-07 21:55:32 +0000150 bool SelectScalarSSELoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng4090dc42006-10-11 21:06:01 +0000151 SDOperand &Index, SDOperand &Disp,
152 SDOperand &InChain, SDOperand &OutChain);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000153 bool TryFoldLoad(SDOperand P, SDOperand N,
154 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000155 SDOperand &Index, SDOperand &Disp);
Evan Cheng64a9e282006-08-28 20:10:17 +0000156 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000157
Chris Lattnerba1ed582006-06-08 18:03:49 +0000158 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
159 /// inline asm expressions.
160 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
161 char ConstraintCode,
162 std::vector<SDOperand> &OutOps,
163 SelectionDAG &DAG);
164
Evan Chenge8a42362006-06-02 22:38:37 +0000165 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
166
Evan Cheng67ed58e2005-12-12 21:49:40 +0000167 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
168 SDOperand &Scale, SDOperand &Index,
169 SDOperand &Disp) {
170 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000171 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
172 AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000173 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000174 Index = AM.IndexReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000175 // These are 32-bit even in 64-bit mode since RIP relative offset
176 // is 32-bit.
177 if (AM.GV)
178 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
179 else if (AM.CP)
180 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
181 else if (AM.ES)
182 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
183 else if (AM.JT != -1)
184 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
185 else
186 Disp = getI32Imm(AM.Disp);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000187 }
188
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000189 /// getI8Imm - Return a target constant with the specified value, of type
190 /// i8.
191 inline SDOperand getI8Imm(unsigned Imm) {
192 return CurDAG->getTargetConstant(Imm, MVT::i8);
193 }
194
Chris Lattner655e7df2005-11-16 01:54:32 +0000195 /// getI16Imm - Return a target constant with the specified value, of type
196 /// i16.
197 inline SDOperand getI16Imm(unsigned Imm) {
198 return CurDAG->getTargetConstant(Imm, MVT::i16);
199 }
200
201 /// getI32Imm - Return a target constant with the specified value, of type
202 /// i32.
203 inline SDOperand getI32Imm(unsigned Imm) {
204 return CurDAG->getTargetConstant(Imm, MVT::i32);
205 }
Evan Chengd49cc362006-02-10 22:24:32 +0000206
Evan Cheng5588de92006-02-18 00:15:05 +0000207 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
208 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000209 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000210
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000211#ifndef NDEBUG
212 unsigned Indent;
213#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000214 };
215}
216
Evan Cheng61b8b432006-10-10 01:46:56 +0000217static SDNode *findFlagUse(SDNode *N) {
218 unsigned FlagResNo = N->getNumValues()-1;
219 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
220 SDNode *User = *I;
221 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
222 SDOperand Op = User->getOperand(i);
223 if (Op.ResNo == FlagResNo)
224 return User;
225 }
226 }
227 return NULL;
228}
229
230static void findNonImmUse(SDNode* Use, SDNode* Def, SDNode *Ignore, bool &found,
Evan Cheng72bb66a2006-08-08 00:31:00 +0000231 std::set<SDNode *> &Visited) {
232 if (found ||
233 Use->getNodeId() > Def->getNodeId() ||
234 !Visited.insert(Use).second)
235 return;
236
237 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
238 SDNode *N = Use->getOperand(i).Val;
Evan Cheng61b8b432006-10-10 01:46:56 +0000239 if (N == Ignore)
240 continue;
Evan Cheng72bb66a2006-08-08 00:31:00 +0000241 if (N != Def) {
Evan Cheng61b8b432006-10-10 01:46:56 +0000242 findNonImmUse(N, Def, Ignore, found, Visited);
Evan Cheng72bb66a2006-08-08 00:31:00 +0000243 } else {
244 found = true;
245 break;
246 }
247 }
248}
249
Evan Cheng61b8b432006-10-10 01:46:56 +0000250static inline bool isNonImmUse(SDNode* Use, SDNode* Def, SDNode *Ignore=NULL) {
Evan Cheng72bb66a2006-08-08 00:31:00 +0000251 std::set<SDNode *> Visited;
252 bool found = false;
253 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
254 SDNode *N = Use->getOperand(i).Val;
Evan Cheng61b8b432006-10-10 01:46:56 +0000255 if (N != Def && N != Ignore) {
256 findNonImmUse(N, Def, Ignore, found, Visited);
Evan Cheng72bb66a2006-08-08 00:31:00 +0000257 if (found) break;
258 }
259 }
Evan Cheng61b8b432006-10-10 01:46:56 +0000260
261 if (!found && Ignore) {
262 // We must be checking for reachability between Def and a flag use. Go down
263 // recursively if Use also produces a flag.
264 MVT::ValueType VT = Use->getValueType(Use->getNumValues()-1);
265 if (VT == MVT::Flag && !Use->use_empty()) {
266 SDNode *FU = findFlagUse(Use);
267 if (FU)
268 return !isNonImmUse(FU, Def, Use);
269 }
270 }
Evan Cheng72bb66a2006-08-08 00:31:00 +0000271 return found;
272}
273
274
Evan Chenge2a3f702006-07-28 01:03:48 +0000275bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng691a63d2006-07-27 16:44:36 +0000276 // If U use can somehow reach N through another path then U can't fold N or
277 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000278 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000279 // a successor of U.
280 //
281 // [ N ]
282 // ^ ^
283 // | |
284 // / \---
285 // / [X]
286 // | ^
287 // [U]--------|
Evan Cheng61b8b432006-10-10 01:46:56 +0000288 if (!FastISel && !isNonImmUse(U, N)) {
289 // If U produces a flag, then it gets (even more) interesting. Since it
290 // would have been "glued" together with its flag use, we need to check if
291 // it might reach N:
292 //
293 // [ N ]
294 // ^ ^
295 // | |
296 // [U] \--
297 // ^ [TF]
298 // | |
299 // \ /
300 // [FU]
301 //
302 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
303 // NU), then TF is a predecessor of FU and a successor of NU. But since
304 // NU and FU are flagged together, this effectively creates a cycle.
305 MVT::ValueType VT = U->getValueType(U->getNumValues()-1);
306 if (VT == MVT::Flag && !U->use_empty()) {
307 SDNode *FU = findFlagUse(U);
308 if (FU)
309 return !isNonImmUse(FU, N, U);
310 }
311 return true;
312 }
313 return false;
Evan Cheng691a63d2006-07-27 16:44:36 +0000314}
315
Evan Cheng64a9e282006-08-28 20:10:17 +0000316/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
317/// and move load below the TokenFactor. Replace store's chain operand with
318/// load's chain result.
319static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
320 SDOperand Store, SDOperand TF) {
321 std::vector<SDOperand> Ops;
322 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
323 if (Load.Val == TF.Val->getOperand(i).Val)
324 Ops.push_back(Load.Val->getOperand(0));
325 else
326 Ops.push_back(TF.Val->getOperand(i));
327 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
328 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
329 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
330 Store.getOperand(2), Store.getOperand(3));
331}
332
333/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
334/// selector to pick more load-modify-store instructions. This is a common
335/// case:
336///
337/// [Load chain]
338/// ^
339/// |
340/// [Load]
341/// ^ ^
342/// | |
343/// / \-
344/// / |
345/// [TokenFactor] [Op]
346/// ^ ^
347/// | |
348/// \ /
349/// \ /
350/// [Store]
351///
352/// The fact the store's chain operand != load's chain will prevent the
353/// (store (op (load))) instruction from being selected. We can transform it to:
354///
355/// [Load chain]
356/// ^
357/// |
358/// [TokenFactor]
359/// ^
360/// |
361/// [Load]
362/// ^ ^
363/// | |
364/// | \-
365/// | |
366/// | [Op]
367/// | ^
368/// | |
369/// \ /
370/// \ /
371/// [Store]
372void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
373 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
374 E = DAG.allnodes_end(); I != E; ++I) {
375 if (I->getOpcode() != ISD::STORE)
376 continue;
377 SDOperand Chain = I->getOperand(0);
378 if (Chain.Val->getOpcode() != ISD::TokenFactor)
379 continue;
380
381 SDOperand N1 = I->getOperand(1);
382 SDOperand N2 = I->getOperand(2);
Evan Cheng2c4e0f12006-09-01 22:52:28 +0000383 if (MVT::isFloatingPoint(N1.getValueType()) ||
384 MVT::isVector(N1.getValueType()) ||
Evan Chengdfb85152006-08-29 18:37:37 +0000385 !N1.hasOneUse())
Evan Cheng64a9e282006-08-28 20:10:17 +0000386 continue;
387
388 bool RModW = false;
389 SDOperand Load;
390 unsigned Opcode = N1.Val->getOpcode();
391 switch (Opcode) {
392 case ISD::ADD:
393 case ISD::MUL:
Evan Cheng64a9e282006-08-28 20:10:17 +0000394 case ISD::AND:
395 case ISD::OR:
396 case ISD::XOR:
397 case ISD::ADDC:
398 case ISD::ADDE: {
399 SDOperand N10 = N1.getOperand(0);
400 SDOperand N11 = N1.getOperand(1);
Evan Chenge71fe34d2006-10-09 20:57:25 +0000401 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng64a9e282006-08-28 20:10:17 +0000402 RModW = true;
Evan Chenge71fe34d2006-10-09 20:57:25 +0000403 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000404 RModW = true;
405 std::swap(N10, N11);
406 }
407 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000408 (N10.getOperand(1) == N2) &&
409 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000410 if (RModW)
411 Load = N10;
412 break;
413 }
414 case ISD::SUB:
415 case ISD::SHL:
416 case ISD::SRA:
417 case ISD::SRL:
418 case ISD::ROTL:
419 case ISD::ROTR:
420 case ISD::SUBC:
421 case ISD::SUBE:
422 case X86ISD::SHLD:
423 case X86ISD::SHRD: {
424 SDOperand N10 = N1.getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +0000425 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng64a9e282006-08-28 20:10:17 +0000426 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000427 (N10.getOperand(1) == N2) &&
428 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000429 if (RModW)
430 Load = N10;
431 break;
432 }
433 }
434
Evan Chengc07feb142006-08-29 06:44:17 +0000435 if (RModW) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000436 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Chengc07feb142006-08-29 06:44:17 +0000437 ++NumLoadMoved;
438 }
Evan Cheng64a9e282006-08-28 20:10:17 +0000439 }
440}
441
Chris Lattner655e7df2005-11-16 01:54:32 +0000442/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
443/// when it has created a SelectionDAG for us to codegen.
444void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
445 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000446 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000447
Evan Cheng358b9ed2006-08-29 18:28:33 +0000448 if (!FastISel)
Evan Cheng64a9e282006-08-28 20:10:17 +0000449 InstructionSelectPreprocess(DAG);
450
Chris Lattner655e7df2005-11-16 01:54:32 +0000451 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000452#ifndef NDEBUG
453 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000454 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000455#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000456 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000457#ifndef NDEBUG
458 DEBUG(std::cerr << "===== Instruction selection ends:\n");
459#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000460
Chris Lattner655e7df2005-11-16 01:54:32 +0000461 DAG.RemoveDeadNodes();
462
463 // Emit machine code to BB.
464 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000465
466 // If we are emitting FP stack code, scan the basic block to determine if this
467 // block defines any FP values. If so, put an FP_REG_KILL instruction before
468 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000469 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000470 // Note that FP stack instructions *are* used in SSE code when returning
471 // values, but these are not live out of the basic block, so we don't need
472 // an FP_REG_KILL in this case either.
473 bool ContainsFPCode = false;
474
475 // Scan all of the machine instructions in these MBBs, checking for FP
476 // stores.
477 MachineFunction::iterator MBBI = FirstMBB;
478 do {
479 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
480 !ContainsFPCode && I != E; ++I) {
481 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
482 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
483 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
484 RegMap->getRegClass(I->getOperand(0).getReg()) ==
485 X86::RFPRegisterClass) {
486 ContainsFPCode = true;
487 break;
488 }
489 }
490 }
491 } while (!ContainsFPCode && &*(MBBI++) != BB);
492
493 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
494 // a copy of the input value in this block.
495 if (!ContainsFPCode) {
496 // Final check, check LLVM BB's that are successors to the LLVM BB
497 // corresponding to BB for FP PHI nodes.
498 const BasicBlock *LLVMBB = BB->getBasicBlock();
499 const PHINode *PN;
500 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
501 !ContainsFPCode && SI != E; ++SI) {
502 for (BasicBlock::const_iterator II = SI->begin();
503 (PN = dyn_cast<PHINode>(II)); ++II) {
504 if (PN->getType()->isFloatingPoint()) {
505 ContainsFPCode = true;
506 break;
507 }
508 }
509 }
510 }
511
512 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
513 if (ContainsFPCode) {
514 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
515 ++NumFPKill;
516 }
517 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000518}
519
Evan Chengbc7a0f442006-01-11 06:09:51 +0000520/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
521/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000522void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
523 MachineFrameInfo *MFI) {
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +0000524 if (Subtarget->isTargetCygwin())
Evan Chenge8a42362006-06-02 22:38:37 +0000525 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
526
Evan Chengbc7a0f442006-01-11 06:09:51 +0000527 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
528 int CWFrameIdx = MFI->CreateStackObject(2, 2);
529 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
530
531 // Set the high part to be 64-bit precision.
532 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
533 CWFrameIdx, 1).addImm(2);
534
535 // Reload the modified control word now.
536 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
537}
538
539void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
540 // If this is main, emit special code for main.
541 MachineBasicBlock *BB = MF.begin();
542 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
543 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
544}
545
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000546/// MatchAddress - Add the specified node to the specified addressing mode,
547/// returning true if it cannot be done. This just pattern matches for the
548/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000549bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
550 bool isRoot) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000551 // RIP relative addressing: %rip + 32-bit displacement!
552 if (AM.isRIPRel) {
553 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner706dd3e2006-09-13 04:45:25 +0000554 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000555 if (isInt32(AM.Disp + Val)) {
556 AM.Disp += Val;
557 return false;
558 }
559 }
560 return true;
561 }
562
Evan Chengb9d34bd2006-08-07 22:28:20 +0000563 int id = N.Val->getNodeId();
564 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000565
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000566 switch (N.getOpcode()) {
567 default: break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000568 case ISD::Constant: {
Chris Lattner706dd3e2006-09-13 04:45:25 +0000569 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000570 if (isInt32(AM.Disp + Val)) {
571 AM.Disp += Val;
572 return false;
573 }
574 break;
575 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000576
577 case X86ISD::Wrapper:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000578 // If value is available in a register both base and index components have
579 // been picked, we can't fit the result available in the register in the
580 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
581
582 // Can't fit GV or CP in addressing mode for X86-64 medium or large code
583 // model since the displacement field is 32-bit. Ok for small code model.
584
585 // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP
586 // relative addressing mode.
587 if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) &&
588 (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) {
589 bool isRIP = Subtarget->is64Bit();
590 if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val ||
591 AM.BaseType == X86ISelAddressMode::FrameIndexBase))
592 break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000593 if (ConstantPoolSDNode *CP =
594 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
595 if (AM.CP == 0) {
Evan Cheng9a083a42006-09-12 21:04:05 +0000596 AM.CP = CP->getConstVal();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000597 AM.Align = CP->getAlignment();
598 AM.Disp += CP->getOffset();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000599 if (isRIP)
600 AM.isRIPRel = true;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000601 return false;
602 }
603 } else if (GlobalAddressSDNode *G =
604 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
605 if (AM.GV == 0) {
606 AM.GV = G->getGlobal();
607 AM.Disp += G->getOffset();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000608 if (isRIP)
609 AM.isRIPRel = true;
610 return false;
611 }
612 } else if (isRoot && isRIP) {
613 if (ExternalSymbolSDNode *S =
614 dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) {
615 AM.ES = S->getSymbol();
616 AM.isRIPRel = true;
617 return false;
618 } else if (JumpTableSDNode *J =
619 dyn_cast<JumpTableSDNode>(N.getOperand(0))) {
620 AM.JT = J->getIndex();
621 AM.isRIPRel = true;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000622 return false;
623 }
624 }
625 }
626 break;
627
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000628 case ISD::FrameIndex:
629 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
630 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
631 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
632 return false;
633 }
634 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000635
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000636 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000637 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000638 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
639 unsigned Val = CN->getValue();
640 if (Val == 1 || Val == 2 || Val == 3) {
641 AM.Scale = 1 << Val;
642 SDOperand ShVal = N.Val->getOperand(0);
643
644 // Okay, we know that we have a scale by now. However, if the scaled
645 // value is an add of something and a constant, we can fold the
646 // constant into the disp field here.
647 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
648 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
649 AM.IndexReg = ShVal.Val->getOperand(0);
650 ConstantSDNode *AddVal =
651 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000652 uint64_t Disp = AM.Disp + AddVal->getValue() << Val;
653 if (isInt32(Disp))
654 AM.Disp = Disp;
655 else
656 AM.IndexReg = ShVal;
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000657 } else {
658 AM.IndexReg = ShVal;
659 }
660 return false;
661 }
662 }
663 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000664
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000665 case ISD::MUL:
666 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000667 if (!Available &&
668 AM.BaseType == X86ISelAddressMode::RegBase &&
669 AM.Base.Reg.Val == 0 &&
670 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000671 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
672 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
673 AM.Scale = unsigned(CN->getValue())-1;
674
675 SDOperand MulVal = N.Val->getOperand(0);
676 SDOperand Reg;
677
678 // Okay, we know that we have a scale by now. However, if the scaled
679 // value is an add of something and a constant, we can fold the
680 // constant into the disp field here.
681 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
682 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
683 Reg = MulVal.Val->getOperand(0);
684 ConstantSDNode *AddVal =
685 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000686 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
687 if (isInt32(Disp))
688 AM.Disp = Disp;
689 else
690 Reg = N.Val->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000691 } else {
692 Reg = N.Val->getOperand(0);
693 }
694
695 AM.IndexReg = AM.Base.Reg = Reg;
696 return false;
697 }
698 break;
699
700 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000701 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000702 X86ISelAddressMode Backup = AM;
703 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
704 !MatchAddress(N.Val->getOperand(1), AM, false))
705 return false;
706 AM = Backup;
707 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
708 !MatchAddress(N.Val->getOperand(0), AM, false))
709 return false;
710 AM = Backup;
711 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000712 break;
713 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000714
715 case ISD::OR: {
716 if (!Available) {
717 X86ISelAddressMode Backup = AM;
718 // Look for (x << c1) | c2 where (c2 < c1)
719 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
720 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
721 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
722 AM.Disp = CN->getValue();
723 return false;
724 }
725 }
726 AM = Backup;
727 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
728 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
729 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
730 AM.Disp = CN->getValue();
731 return false;
732 }
733 }
734 AM = Backup;
735 }
736 break;
737 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000738 }
739
740 // Is the base register already occupied?
741 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
742 // If so, check to see if the scale index register is set.
743 if (AM.IndexReg.Val == 0) {
744 AM.IndexReg = N;
745 AM.Scale = 1;
746 return false;
747 }
748
749 // Otherwise, we cannot select it.
750 return true;
751 }
752
753 // Default, generate it as a register.
754 AM.BaseType = X86ISelAddressMode::RegBase;
755 AM.Base.Reg = N;
756 return false;
757}
758
Evan Chengc9fab312005-12-08 02:01:35 +0000759/// SelectAddr - returns true if it is able pattern match an addressing mode.
760/// It returns the operands which make up the maximal addressing mode it can
761/// match by reference.
762bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
763 SDOperand &Index, SDOperand &Disp) {
764 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000765 if (MatchAddress(N, AM))
766 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000767
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000768 MVT::ValueType VT = N.getValueType();
Evan Chengbc7a0f442006-01-11 06:09:51 +0000769 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000770 if (!AM.Base.Reg.Val)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000771 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengc9fab312005-12-08 02:01:35 +0000772 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000773
Evan Chengd19d51f2006-02-05 05:25:07 +0000774 if (!AM.IndexReg.Val)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000775 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Chengbc7a0f442006-01-11 06:09:51 +0000776
777 getAddressOperands(AM, Base, Scale, Index, Disp);
778 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000779}
780
Chris Lattner398195e2006-10-07 21:55:32 +0000781/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
782/// match a load whose top elements are either undef or zeros. The load flavor
783/// is derived from the type of N, which is either v4f32 or v2f64.
784bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand N, SDOperand &Base,
Evan Cheng4090dc42006-10-11 21:06:01 +0000785 SDOperand &Scale, SDOperand &Index,
786 SDOperand &Disp, SDOperand &InChain,
787 SDOperand &OutChain) {
Chris Lattner398195e2006-10-07 21:55:32 +0000788 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Evan Cheng4090dc42006-10-11 21:06:01 +0000789 InChain = N.getOperand(0);
790 if (ISD::isNON_EXTLoad(InChain.Val)) {
791 LoadSDNode *LD = cast<LoadSDNode>(InChain);
792 SDOperand LoadAddr = LD->getBasePtr();
Chris Lattner398195e2006-10-07 21:55:32 +0000793 if (!SelectAddr(LoadAddr, Base, Scale, Index, Disp))
794 return false;
Evan Cheng4090dc42006-10-11 21:06:01 +0000795 OutChain = LD->getChain();
Chris Lattner398195e2006-10-07 21:55:32 +0000796 return true;
797 }
798 }
799 // TODO: Also handle the case where we explicitly require zeros in the top
800 // elements. This is a vector shuffle from the zero vector.
Chris Lattner398195e2006-10-07 21:55:32 +0000801
802 return false;
803}
804
805
Evan Cheng77d86ff2006-02-25 10:09:08 +0000806/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
807/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000808bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
809 SDOperand &Scale,
810 SDOperand &Index, SDOperand &Disp) {
811 X86ISelAddressMode AM;
812 if (MatchAddress(N, AM))
813 return false;
814
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000815 MVT::ValueType VT = N.getValueType();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000816 unsigned Complexity = 0;
817 if (AM.BaseType == X86ISelAddressMode::RegBase)
818 if (AM.Base.Reg.Val)
819 Complexity = 1;
820 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000821 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000822 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
823 Complexity = 4;
824
825 if (AM.IndexReg.Val)
826 Complexity++;
827 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000828 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000829
Evan Cheng990c3602006-02-28 21:13:57 +0000830 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000831 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000832 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
833 else if (AM.Scale > 1)
834 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000835
836 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
837 // to a LEA. This is determined with some expermentation but is by no means
838 // optimal (especially for code size consideration). LEA is nice because of
839 // its three-address nature. Tweak the cost function again when we can run
840 // convertToThreeAddress() at register allocation time.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000841 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
842 // For X86-64, we should always use lea to materialize RIP relative
843 // addresses.
844 if (Subtarget->is64Bit())
845 Complexity = 4;
846 else
847 Complexity += 2;
848 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000849
850 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
851 Complexity++;
852
853 if (Complexity > 2) {
854 getAddressOperands(AM, Base, Scale, Index, Disp);
855 return true;
856 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000857 return false;
858}
859
Evan Chengd5f2ba02006-02-06 06:02:33 +0000860bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
861 SDOperand &Base, SDOperand &Scale,
862 SDOperand &Index, SDOperand &Disp) {
Evan Chenge71fe34d2006-10-09 20:57:25 +0000863 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Chengd5f2ba02006-02-06 06:02:33 +0000864 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000865 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000866 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
867 return false;
868}
869
870static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000871 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
872 return (R->getReg() == 0);
873 return false;
874}
875
Evan Cheng5588de92006-02-18 00:15:05 +0000876/// getGlobalBaseReg - Output the instructions required to put the
877/// base address to use for accessing globals into a register.
878///
Evan Cheng61413a32006-08-26 05:34:46 +0000879SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000880 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng5588de92006-02-18 00:15:05 +0000881 if (!GlobalBaseReg) {
882 // Insert the set of GlobalBaseReg into the first MBB of the function
883 MachineBasicBlock &FirstMBB = BB->getParent()->front();
884 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
885 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
886 // FIXME: when we get to LP64, we will need to create the appropriate
887 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000888 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000889 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
890 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
891 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000892 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000893}
894
Evan Chengf838cfc2006-05-20 01:36:52 +0000895static SDNode *FindCallStartFromCall(SDNode *Node) {
896 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
897 assert(Node->getOperand(0).getValueType() == MVT::Other &&
898 "Node doesn't have a token chain argument!");
899 return FindCallStartFromCall(Node->getOperand(0).Val);
900}
901
Evan Cheng61413a32006-08-26 05:34:46 +0000902SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000903 SDNode *Node = N.Val;
904 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000905 unsigned Opc, MOpc;
906 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000907
Evan Chengd49cc362006-02-10 22:24:32 +0000908#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000909 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000910 DEBUG(std::cerr << "Selecting: ");
911 DEBUG(Node->dump(CurDAG));
912 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000913 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000914#endif
915
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000916 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000917#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000918 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000919 DEBUG(std::cerr << "== ");
920 DEBUG(Node->dump(CurDAG));
921 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000922 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000923#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000924 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000925 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000926
Evan Cheng10d27902006-01-06 20:36:21 +0000927 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000928 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000929 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000930 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000931
Evan Cheng77d86ff2006-02-25 10:09:08 +0000932 case ISD::ADD: {
933 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
934 // code and is matched first so to prevent it from being turned into
935 // LEA32r X+c.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000936 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
937 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng77d86ff2006-02-25 10:09:08 +0000938 SDOperand N0 = N.getOperand(0);
939 SDOperand N1 = N.getOperand(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000940 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng77d86ff2006-02-25 10:09:08 +0000941 N0.getOpcode() == X86ISD::Wrapper &&
942 N1.getOpcode() == ISD::Constant) {
943 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
944 SDOperand C(0, 0);
945 // TODO: handle ExternalSymbolSDNode.
946 if (GlobalAddressSDNode *G =
947 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000948 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng77d86ff2006-02-25 10:09:08 +0000949 G->getOffset() + Offset);
950 } else if (ConstantPoolSDNode *CP =
951 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Cheng9a083a42006-09-12 21:04:05 +0000952 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng77d86ff2006-02-25 10:09:08 +0000953 CP->getAlignment(),
954 CP->getOffset()+Offset);
955 }
956
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000957 if (C.Val) {
958 if (Subtarget->is64Bit()) {
959 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
960 CurDAG->getRegister(0, PtrVT), C };
961 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
962 } else
963 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
964 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000965 }
966
967 // Other cases are handled by auto-generated code.
968 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000969 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000970
Evan Cheng10d27902006-01-06 20:36:21 +0000971 case ISD::MULHU:
972 case ISD::MULHS: {
973 if (Opcode == ISD::MULHU)
974 switch (NVT) {
975 default: assert(0 && "Unsupported VT!");
976 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
977 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
978 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000979 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng10d27902006-01-06 20:36:21 +0000980 }
981 else
982 switch (NVT) {
983 default: assert(0 && "Unsupported VT!");
984 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
985 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
986 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000987 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng10d27902006-01-06 20:36:21 +0000988 }
989
990 unsigned LoReg, HiReg;
991 switch (NVT) {
992 default: assert(0 && "Unsupported VT!");
993 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
994 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
995 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000996 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng10d27902006-01-06 20:36:21 +0000997 }
998
999 SDOperand N0 = Node->getOperand(0);
1000 SDOperand N1 = Node->getOperand(1);
1001
1002 bool foldedLoad = false;
1003 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +00001004 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +00001005 // MULHU and MULHS are commmutative
1006 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +00001007 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +00001008 if (foldedLoad) {
1009 N0 = Node->getOperand(1);
1010 N1 = Node->getOperand(0);
1011 }
1012 }
1013
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001014 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +00001015 if (foldedLoad) {
1016 Chain = N1.getOperand(0);
1017 AddToISelQueue(Chain);
1018 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001019 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +00001020
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001021 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +00001022 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +00001023 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001024 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +00001025 InFlag = Chain.getValue(1);
1026
1027 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +00001028 AddToISelQueue(Tmp0);
1029 AddToISelQueue(Tmp1);
1030 AddToISelQueue(Tmp2);
1031 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +00001032 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +00001033 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +00001034 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +00001035 Chain = SDOperand(CNode, 0);
1036 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +00001037 } else {
Evan Cheng2d487222006-08-26 01:05:16 +00001038 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +00001039 InFlag =
1040 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +00001041 }
1042
Evan Cheng61413a32006-08-26 05:34:46 +00001043 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +00001044 ReplaceUses(N.getValue(0), Result);
1045 if (foldedLoad)
1046 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001047
Evan Chengd49cc362006-02-10 22:24:32 +00001048#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001049 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001050 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +00001051 DEBUG(Result.Val->dump(CurDAG));
1052 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001053 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001054#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001055 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +00001056 }
Evan Cheng5588de92006-02-18 00:15:05 +00001057
Evan Cheng92e27972006-01-06 23:19:29 +00001058 case ISD::SDIV:
1059 case ISD::UDIV:
1060 case ISD::SREM:
1061 case ISD::UREM: {
1062 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1063 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1064 if (!isSigned)
1065 switch (NVT) {
1066 default: assert(0 && "Unsupported VT!");
1067 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1068 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1069 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001070 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng92e27972006-01-06 23:19:29 +00001071 }
1072 else
1073 switch (NVT) {
1074 default: assert(0 && "Unsupported VT!");
1075 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1076 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1077 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001078 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng92e27972006-01-06 23:19:29 +00001079 }
1080
1081 unsigned LoReg, HiReg;
1082 unsigned ClrOpcode, SExtOpcode;
1083 switch (NVT) {
1084 default: assert(0 && "Unsupported VT!");
1085 case MVT::i8:
1086 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001087 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001088 SExtOpcode = X86::CBW;
1089 break;
1090 case MVT::i16:
1091 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001092 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001093 SExtOpcode = X86::CWD;
1094 break;
1095 case MVT::i32:
1096 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +00001097 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +00001098 SExtOpcode = X86::CDQ;
1099 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001100 case MVT::i64:
1101 LoReg = X86::RAX; HiReg = X86::RDX;
1102 ClrOpcode = X86::MOV64r0;
1103 SExtOpcode = X86::CQO;
1104 break;
Evan Cheng92e27972006-01-06 23:19:29 +00001105 }
1106
1107 SDOperand N0 = Node->getOperand(0);
1108 SDOperand N1 = Node->getOperand(1);
1109
1110 bool foldedLoad = false;
1111 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +00001112 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001113 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +00001114 if (foldedLoad) {
1115 Chain = N1.getOperand(0);
1116 AddToISelQueue(Chain);
1117 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001118 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +00001119
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001120 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +00001121 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +00001122 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001123 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +00001124 InFlag = Chain.getValue(1);
1125
1126 if (isSigned) {
1127 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +00001128 InFlag =
1129 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001130 } else {
1131 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +00001132 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001133 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
1134 ClrNode, InFlag);
1135 InFlag = Chain.getValue(1);
1136 }
1137
1138 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +00001139 AddToISelQueue(Tmp0);
1140 AddToISelQueue(Tmp1);
1141 AddToISelQueue(Tmp2);
1142 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +00001143 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +00001144 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +00001145 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +00001146 Chain = SDOperand(CNode, 0);
1147 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +00001148 } else {
Evan Cheng2d487222006-08-26 01:05:16 +00001149 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +00001150 InFlag =
1151 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +00001152 }
1153
Evan Cheng61413a32006-08-26 05:34:46 +00001154 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
1155 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +00001156 ReplaceUses(N.getValue(0), Result);
1157 if (foldedLoad)
1158 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +00001159
1160#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001161 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001162 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +00001163 DEBUG(Result.Val->dump(CurDAG));
1164 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001165 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001166#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001167
1168 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +00001169 }
Evan Cheng9733bde2006-05-08 08:01:26 +00001170
1171 case ISD::TRUNCATE: {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001172 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng9733bde2006-05-08 08:01:26 +00001173 unsigned Opc2;
1174 MVT::ValueType VT;
1175 switch (Node->getOperand(0).getValueType()) {
1176 default: assert(0 && "Unknown truncate!");
1177 case MVT::i16:
1178 Opc = X86::MOV16to16_;
1179 VT = MVT::i16;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001180 Opc2 = X86::TRUNC_16_to8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001181 break;
1182 case MVT::i32:
1183 Opc = X86::MOV32to32_;
1184 VT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001185 Opc2 = X86::TRUNC_32_to8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001186 break;
1187 }
1188
Evan Cheng2d487222006-08-26 01:05:16 +00001189 AddToISelQueue(Node->getOperand(0));
1190 SDOperand Tmp =
1191 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +00001192 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +00001193
1194#ifndef NDEBUG
1195 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001196 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001197 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +00001198 DEBUG(std::cerr << "\n");
1199 Indent -= 2;
1200#endif
Evan Cheng61413a32006-08-26 05:34:46 +00001201 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +00001202 }
Evan Chenga26c4512006-05-20 07:44:28 +00001203
1204 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001205 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001206 }
1207
Evan Cheng61413a32006-08-26 05:34:46 +00001208 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001209
Evan Chengd49cc362006-02-10 22:24:32 +00001210#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001211 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +00001212 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001213 if (ResNode == NULL || ResNode == N.Val)
1214 DEBUG(N.Val->dump(CurDAG));
1215 else
1216 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +00001217 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001218 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001219#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001220
1221 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001222}
1223
Chris Lattnerba1ed582006-06-08 18:03:49 +00001224bool X86DAGToDAGISel::
1225SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1226 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1227 SDOperand Op0, Op1, Op2, Op3;
1228 switch (ConstraintCode) {
1229 case 'o': // offsetable ??
1230 case 'v': // not offsetable ??
1231 default: return true;
1232 case 'm': // memory
1233 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1234 return true;
1235 break;
1236 }
1237
Evan Cheng2d487222006-08-26 01:05:16 +00001238 OutOps.push_back(Op0);
1239 OutOps.push_back(Op1);
1240 OutOps.push_back(Op2);
1241 OutOps.push_back(Op3);
1242 AddToISelQueue(Op0);
1243 AddToISelQueue(Op1);
1244 AddToISelQueue(Op2);
1245 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001246 return false;
1247}
1248
Chris Lattner655e7df2005-11-16 01:54:32 +00001249/// createX86ISelDag - This pass converts a legalized DAG into a
1250/// X86-specific DAG, ready for instruction scheduling.
1251///
Evan Cheng358b9ed2006-08-29 18:28:33 +00001252FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1253 return new X86DAGToDAGISel(TM, Fast);
Chris Lattner655e7df2005-11-16 01:54:32 +00001254}