blob: bc2870ed24ce3ec5e15be8383485ecde60d53bc5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "x86-isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/GlobalValue.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Type.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/MathExtras.h"
38#include "llvm/ADT/Statistic.h"
39#include <queue>
40#include <set>
41using namespace llvm;
42
43STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
44STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
46
47//===----------------------------------------------------------------------===//
48// Pattern Matcher Implementation
49//===----------------------------------------------------------------------===//
50
51namespace {
52 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
53 /// SDOperand's instead of register numbers for the leaves of the matched
54 /// tree.
55 struct X86ISelAddressMode {
56 enum {
57 RegBase,
58 FrameIndexBase
59 } BaseType;
60
61 struct { // This is really a union, discriminated by BaseType!
62 SDOperand Reg;
63 int FrameIndex;
64 } Base;
65
66 bool isRIPRel; // RIP relative?
67 unsigned Scale;
68 SDOperand IndexReg;
69 unsigned Disp;
70 GlobalValue *GV;
71 Constant *CP;
72 const char *ES;
73 int JT;
74 unsigned Align; // CP alignment.
75
76 X86ISelAddressMode()
77 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
78 GV(0), CP(0), ES(0), JT(-1), Align(0) {
79 }
80 };
81}
82
83namespace {
84 //===--------------------------------------------------------------------===//
85 /// ISel - X86 specific code to select X86 machine instructions for
86 /// SelectionDAG operations.
87 ///
88 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
89 /// ContainsFPCode - Every instruction we select that uses or defines a FP
90 /// register should set this to true.
91 bool ContainsFPCode;
92
93 /// FastISel - Enable fast(er) instruction selection.
94 ///
95 bool FastISel;
96
97 /// TM - Keep a reference to X86TargetMachine.
98 ///
99 X86TargetMachine &TM;
100
101 /// X86Lowering - This object fully describes how to lower LLVM code to an
102 /// X86-specific SelectionDAG.
103 X86TargetLowering X86Lowering;
104
105 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const X86Subtarget *Subtarget;
108
109 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
110 /// base register.
111 unsigned GlobalBaseReg;
112
113 public:
114 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
115 : SelectionDAGISel(X86Lowering),
116 ContainsFPCode(false), FastISel(fast), TM(tm),
117 X86Lowering(*TM.getTargetLowering()),
118 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
119
120 virtual bool runOnFunction(Function &Fn) {
121 // Make sure we re-emit a set of the global base reg if necessary
122 GlobalBaseReg = 0;
123 return SelectionDAGISel::runOnFunction(Fn);
124 }
125
126 virtual const char *getPassName() const {
127 return "X86 DAG->DAG Instruction Selection";
128 }
129
130 /// InstructionSelectBasicBlock - This callback is invoked by
131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
132 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
133
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000134 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
135
Dan Gohmand6098272007-07-24 23:00:27 +0000136 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138// Include the pieces autogenerated from the target description.
139#include "X86GenDAGISel.inc"
140
141 private:
142 SDNode *Select(SDOperand N);
143
144 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
145 bool isRoot = true, unsigned Depth = 0);
Dan Gohmana60c1b32007-08-13 20:03:06 +0000146 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
147 bool isRoot, unsigned Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
149 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
150 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
151 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
152 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
153 SDOperand N, SDOperand &Base, SDOperand &Scale,
154 SDOperand &Index, SDOperand &Disp,
155 SDOperand &InChain, SDOperand &OutChain);
156 bool TryFoldLoad(SDOperand P, SDOperand N,
157 SDOperand &Base, SDOperand &Scale,
158 SDOperand &Index, SDOperand &Disp);
159 void InstructionSelectPreprocess(SelectionDAG &DAG);
160
161 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
162 /// inline asm expressions.
163 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
164 char ConstraintCode,
165 std::vector<SDOperand> &OutOps,
166 SelectionDAG &DAG);
167
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000168 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
171 SDOperand &Scale, SDOperand &Index,
172 SDOperand &Disp) {
173 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
174 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
175 AM.Base.Reg;
176 Scale = getI8Imm(AM.Scale);
177 Index = AM.IndexReg;
178 // These are 32-bit even in 64-bit mode since RIP relative offset
179 // is 32-bit.
180 if (AM.GV)
181 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
182 else if (AM.CP)
183 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
184 else if (AM.ES)
185 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
186 else if (AM.JT != -1)
187 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
188 else
189 Disp = getI32Imm(AM.Disp);
190 }
191
192 /// getI8Imm - Return a target constant with the specified value, of type
193 /// i8.
194 inline SDOperand getI8Imm(unsigned Imm) {
195 return CurDAG->getTargetConstant(Imm, MVT::i8);
196 }
197
198 /// getI16Imm - Return a target constant with the specified value, of type
199 /// i16.
200 inline SDOperand getI16Imm(unsigned Imm) {
201 return CurDAG->getTargetConstant(Imm, MVT::i16);
202 }
203
204 /// getI32Imm - Return a target constant with the specified value, of type
205 /// i32.
206 inline SDOperand getI32Imm(unsigned Imm) {
207 return CurDAG->getTargetConstant(Imm, MVT::i32);
208 }
209
210 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
211 /// base register. Return the virtual register that holds this value.
212 SDNode *getGlobalBaseReg();
213
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000214 /// getTruncate - return an SDNode that implements a subreg based truncate
215 /// of the specified operand to the the specified value type.
216 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218#ifndef NDEBUG
219 unsigned Indent;
220#endif
221 };
222}
223
224static SDNode *findFlagUse(SDNode *N) {
225 unsigned FlagResNo = N->getNumValues()-1;
226 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
227 SDNode *User = *I;
228 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
229 SDOperand Op = User->getOperand(i);
230 if (Op.Val == N && Op.ResNo == FlagResNo)
231 return User;
232 }
233 }
234 return NULL;
235}
236
237static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
238 SDNode *Root, SDNode *Skip, bool &found,
239 std::set<SDNode *> &Visited) {
240 if (found ||
241 Use->getNodeId() > Def->getNodeId() ||
242 !Visited.insert(Use).second)
243 return;
244
245 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
246 SDNode *N = Use->getOperand(i).Val;
247 if (N == Skip)
248 continue;
249 if (N == Def) {
250 if (Use == ImmedUse)
251 continue; // Immediate use is ok.
252 if (Use == Root) {
253 assert(Use->getOpcode() == ISD::STORE ||
254 Use->getOpcode() == X86ISD::CMP);
255 continue;
256 }
257 found = true;
258 break;
259 }
260 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
261 }
262}
263
264/// isNonImmUse - Start searching from Root up the DAG to check is Def can
265/// be reached. Return true if that's the case. However, ignore direct uses
266/// by ImmedUse (which would be U in the example illustrated in
267/// CanBeFoldedBy) and by Root (which can happen in the store case).
268/// FIXME: to be really generic, we should allow direct use by any node
269/// that is being folded. But realisticly since we only fold loads which
270/// have one non-chain use, we only need to watch out for load/op/store
271/// and load/op/cmp case where the root (store / cmp) may reach the load via
272/// its chain operand.
273static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
274 SDNode *Skip = NULL) {
275 std::set<SDNode *> Visited;
276 bool found = false;
277 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
278 return found;
279}
280
281
Dan Gohmand6098272007-07-24 23:00:27 +0000282bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 if (FastISel) return false;
284
285 // If U use can somehow reach N through another path then U can't fold N or
286 // it will create a cycle. e.g. In the following diagram, U can reach N
287 // through X. If N is folded into into U, then X is both a predecessor and
288 // a successor of U.
289 //
290 // [ N ]
291 // ^ ^
292 // | |
293 // / \---
294 // / [X]
295 // | ^
296 // [U]--------|
297
298 if (isNonImmUse(Root, N, U))
299 return false;
300
301 // If U produces a flag, then it gets (even more) interesting. Since it
302 // would have been "glued" together with its flag use, we need to check if
303 // it might reach N:
304 //
305 // [ N ]
306 // ^ ^
307 // | |
308 // [U] \--
309 // ^ [TF]
310 // | ^
311 // | |
312 // \ /
313 // [FU]
314 //
315 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
316 // NU), then TF is a predecessor of FU and a successor of NU. But since
317 // NU and FU are flagged together, this effectively creates a cycle.
318 bool HasFlagUse = false;
319 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
320 while ((VT == MVT::Flag && !Root->use_empty())) {
321 SDNode *FU = findFlagUse(Root);
322 if (FU == NULL)
323 break;
324 else {
325 Root = FU;
326 HasFlagUse = true;
327 }
328 VT = Root->getValueType(Root->getNumValues()-1);
329 }
330
331 if (HasFlagUse)
332 return !isNonImmUse(Root, N, Root, U);
333 return true;
334}
335
336/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
337/// and move load below the TokenFactor. Replace store's chain operand with
338/// load's chain result.
339static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
340 SDOperand Store, SDOperand TF) {
341 std::vector<SDOperand> Ops;
342 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
343 if (Load.Val == TF.Val->getOperand(i).Val)
344 Ops.push_back(Load.Val->getOperand(0));
345 else
346 Ops.push_back(TF.Val->getOperand(i));
347 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
348 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
349 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
350 Store.getOperand(2), Store.getOperand(3));
351}
352
353/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
354/// selector to pick more load-modify-store instructions. This is a common
355/// case:
356///
357/// [Load chain]
358/// ^
359/// |
360/// [Load]
361/// ^ ^
362/// | |
363/// / \-
364/// / |
365/// [TokenFactor] [Op]
366/// ^ ^
367/// | |
368/// \ /
369/// \ /
370/// [Store]
371///
372/// The fact the store's chain operand != load's chain will prevent the
373/// (store (op (load))) instruction from being selected. We can transform it to:
374///
375/// [Load chain]
376/// ^
377/// |
378/// [TokenFactor]
379/// ^
380/// |
381/// [Load]
382/// ^ ^
383/// | |
384/// | \-
385/// | |
386/// | [Op]
387/// | ^
388/// | |
389/// \ /
390/// \ /
391/// [Store]
392void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
393 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
394 E = DAG.allnodes_end(); I != E; ++I) {
395 if (!ISD::isNON_TRUNCStore(I))
396 continue;
397 SDOperand Chain = I->getOperand(0);
398 if (Chain.Val->getOpcode() != ISD::TokenFactor)
399 continue;
400
401 SDOperand N1 = I->getOperand(1);
402 SDOperand N2 = I->getOperand(2);
403 if (MVT::isFloatingPoint(N1.getValueType()) ||
404 MVT::isVector(N1.getValueType()) ||
405 !N1.hasOneUse())
406 continue;
407
408 bool RModW = false;
409 SDOperand Load;
410 unsigned Opcode = N1.Val->getOpcode();
411 switch (Opcode) {
412 case ISD::ADD:
413 case ISD::MUL:
414 case ISD::AND:
415 case ISD::OR:
416 case ISD::XOR:
417 case ISD::ADDC:
418 case ISD::ADDE: {
419 SDOperand N10 = N1.getOperand(0);
420 SDOperand N11 = N1.getOperand(1);
421 if (ISD::isNON_EXTLoad(N10.Val))
422 RModW = true;
423 else if (ISD::isNON_EXTLoad(N11.Val)) {
424 RModW = true;
425 std::swap(N10, N11);
426 }
427 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
428 (N10.getOperand(1) == N2) &&
429 (N10.Val->getValueType(0) == N1.getValueType());
430 if (RModW)
431 Load = N10;
432 break;
433 }
434 case ISD::SUB:
435 case ISD::SHL:
436 case ISD::SRA:
437 case ISD::SRL:
438 case ISD::ROTL:
439 case ISD::ROTR:
440 case ISD::SUBC:
441 case ISD::SUBE:
442 case X86ISD::SHLD:
443 case X86ISD::SHRD: {
444 SDOperand N10 = N1.getOperand(0);
445 if (ISD::isNON_EXTLoad(N10.Val))
446 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
447 (N10.getOperand(1) == N2) &&
448 (N10.Val->getValueType(0) == N1.getValueType());
449 if (RModW)
450 Load = N10;
451 break;
452 }
453 }
454
455 if (RModW) {
456 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
457 ++NumLoadMoved;
458 }
459 }
460}
461
462/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
463/// when it has created a SelectionDAG for us to codegen.
464void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
465 DEBUG(BB->dump());
466 MachineFunction::iterator FirstMBB = BB;
467
468 if (!FastISel)
469 InstructionSelectPreprocess(DAG);
470
471 // Codegen the basic block.
472#ifndef NDEBUG
473 DOUT << "===== Instruction selection begins:\n";
474 Indent = 0;
475#endif
476 DAG.setRoot(SelectRoot(DAG.getRoot()));
477#ifndef NDEBUG
478 DOUT << "===== Instruction selection ends:\n";
479#endif
480
481 DAG.RemoveDeadNodes();
482
483 // Emit machine code to BB.
484 ScheduleAndEmitDAG(DAG);
485
486 // If we are emitting FP stack code, scan the basic block to determine if this
487 // block defines any FP values. If so, put an FP_REG_KILL instruction before
488 // the terminator of the block.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000489
Dale Johannesen684887e2007-09-24 22:52:39 +0000490 // Note that FP stack instructions are used in all modes for long double,
491 // so we always need to do this check.
492 // Also note that it's possible for an FP stack register to be live across
493 // an instruction that produces multiple basic blocks (SSE CMOV) so we
494 // must check all the generated basic blocks.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000495
496 // Scan all of the machine instructions in these MBBs, checking for FP
497 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
498 MachineFunction::iterator MBBI = FirstMBB;
499 do {
Dale Johannesen684887e2007-09-24 22:52:39 +0000500 bool ContainsFPCode = false;
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000501 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
502 !ContainsFPCode && I != E; ++I) {
503 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
504 const TargetRegisterClass *clas;
505 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
506 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
507 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner1b989192007-12-31 04:13:23 +0000508 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000509 X86::RFP32RegisterClass ||
510 clas == X86::RFP64RegisterClass ||
511 clas == X86::RFP80RegisterClass)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 ContainsFPCode = true;
513 break;
514 }
515 }
516 }
517 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000518 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
519 // a copy of the input value in this block. In SSE mode, we only care about
520 // 80-bit values.
521 if (!ContainsFPCode) {
522 // Final check, check LLVM BB's that are successors to the LLVM BB
523 // corresponding to BB for FP PHI nodes.
524 const BasicBlock *LLVMBB = BB->getBasicBlock();
525 const PHINode *PN;
526 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
527 !ContainsFPCode && SI != E; ++SI) {
528 for (BasicBlock::const_iterator II = SI->begin();
529 (PN = dyn_cast<PHINode>(II)); ++II) {
530 if (PN->getType()==Type::X86_FP80Ty ||
531 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
532 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
533 ContainsFPCode = true;
534 break;
535 }
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000536 }
537 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000539 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
540 if (ContainsFPCode) {
541 BuildMI(*MBBI, MBBI->getFirstTerminator(),
542 TM.getInstrInfo()->get(X86::FP_REG_KILL));
543 ++NumFPKill;
544 }
545 } while (&*(MBBI++) != BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546}
547
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000548/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
549/// the main function.
550void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
551 MachineFrameInfo *MFI) {
552 const TargetInstrInfo *TII = TM.getInstrInfo();
553 if (Subtarget->isTargetCygMing())
554 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
555}
556
557void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
558 // If this is main, emit special code for main.
559 MachineBasicBlock *BB = MF.begin();
560 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
561 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
562}
563
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564/// MatchAddress - Add the specified node to the specified addressing mode,
565/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000566/// addressing mode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
568 bool isRoot, unsigned Depth) {
Dan Gohmana60c1b32007-08-13 20:03:06 +0000569 // Limit recursion.
570 if (Depth > 5)
571 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572
573 // RIP relative addressing: %rip + 32-bit displacement!
574 if (AM.isRIPRel) {
575 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
576 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
577 if (isInt32(AM.Disp + Val)) {
578 AM.Disp += Val;
579 return false;
580 }
581 }
582 return true;
583 }
584
585 int id = N.Val->getNodeId();
Evan Chengf2abee72007-12-13 00:43:27 +0000586 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587
588 switch (N.getOpcode()) {
589 default: break;
590 case ISD::Constant: {
591 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
592 if (isInt32(AM.Disp + Val)) {
593 AM.Disp += Val;
594 return false;
595 }
596 break;
597 }
598
599 case X86ISD::Wrapper: {
600 bool is64Bit = Subtarget->is64Bit();
601 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
602 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
603 break;
604 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
605 break;
606 // If value is available in a register both base and index components have
607 // been picked, we can't fit the result available in the register in the
608 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Chengf2abee72007-12-13 00:43:27 +0000609 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 bool isStatic = TM.getRelocationModel() == Reloc::Static;
611 SDOperand N0 = N.getOperand(0);
Evan Chenga2f7d4e2007-07-26 07:35:15 +0000612 // Mac OS X X86-64 lower 4G address is not available.
Evan Cheng09e13792007-08-01 23:45:51 +0000613 bool isAbs32 = !is64Bit ||
614 (isStatic && Subtarget->hasLow4GUserSpaceAddress());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
616 GlobalValue *GV = G->getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 if (isAbs32 || isRoot) {
618 AM.GV = GV;
619 AM.Disp += G->getOffset();
620 AM.isRIPRel = !isAbs32;
621 return false;
622 }
623 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chenga2f7d4e2007-07-26 07:35:15 +0000624 if (isAbs32 || isRoot) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 AM.CP = CP->getConstVal();
626 AM.Align = CP->getAlignment();
627 AM.Disp += CP->getOffset();
Evan Chengeda2f2b2007-07-26 17:02:45 +0000628 AM.isRIPRel = !isAbs32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 return false;
630 }
631 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chenga2f7d4e2007-07-26 07:35:15 +0000632 if (isAbs32 || isRoot) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 AM.ES = S->getSymbol();
Evan Chengeda2f2b2007-07-26 17:02:45 +0000634 AM.isRIPRel = !isAbs32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 return false;
636 }
637 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chenga2f7d4e2007-07-26 07:35:15 +0000638 if (isAbs32 || isRoot) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 AM.JT = J->getIndex();
Evan Chengeda2f2b2007-07-26 17:02:45 +0000640 AM.isRIPRel = !isAbs32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 return false;
642 }
643 }
644 }
645 break;
646 }
647
648 case ISD::FrameIndex:
649 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
650 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
651 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
652 return false;
653 }
654 break;
655
656 case ISD::SHL:
Evan Chengf2abee72007-12-13 00:43:27 +0000657 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000658 break;
659
660 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
661 unsigned Val = CN->getValue();
662 if (Val == 1 || Val == 2 || Val == 3) {
663 AM.Scale = 1 << Val;
664 SDOperand ShVal = N.Val->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665
Chris Lattner7f06edd2007-12-08 07:22:58 +0000666 // Okay, we know that we have a scale by now. However, if the scaled
667 // value is an add of something and a constant, we can fold the
668 // constant into the disp field here.
669 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
670 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
671 AM.IndexReg = ShVal.Val->getOperand(0);
672 ConstantSDNode *AddVal =
673 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
674 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
675 if (isInt32(Disp))
676 AM.Disp = Disp;
677 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000679 } else {
680 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000682 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 }
684 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
Dan Gohman35b99222007-10-22 20:22:24 +0000687 case ISD::SMUL_LOHI:
688 case ISD::UMUL_LOHI:
689 // A mul_lohi where we need the low part can be folded as a plain multiply.
690 if (N.ResNo != 0) break;
691 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 case ISD::MUL:
693 // X*[3,5,9] -> X+X*[2,4,8]
Evan Chengf2abee72007-12-13 00:43:27 +0000694 if (!AlreadySelected &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 AM.BaseType == X86ISelAddressMode::RegBase &&
696 AM.Base.Reg.Val == 0 &&
697 AM.IndexReg.Val == 0) {
698 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
699 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
700 AM.Scale = unsigned(CN->getValue())-1;
701
702 SDOperand MulVal = N.Val->getOperand(0);
703 SDOperand Reg;
704
705 // Okay, we know that we have a scale by now. However, if the scaled
706 // value is an add of something and a constant, we can fold the
707 // constant into the disp field here.
708 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
709 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
710 Reg = MulVal.Val->getOperand(0);
711 ConstantSDNode *AddVal =
712 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
713 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
714 if (isInt32(Disp))
715 AM.Disp = Disp;
716 else
717 Reg = N.Val->getOperand(0);
718 } else {
719 Reg = N.Val->getOperand(0);
720 }
721
722 AM.IndexReg = AM.Base.Reg = Reg;
723 return false;
724 }
725 }
726 break;
727
728 case ISD::ADD:
Evan Chengf2abee72007-12-13 00:43:27 +0000729 if (!AlreadySelected) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 X86ISelAddressMode Backup = AM;
731 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
732 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
733 return false;
734 AM = Backup;
735 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
736 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
737 return false;
738 AM = Backup;
739 }
740 break;
741
742 case ISD::OR:
743 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Chengf2abee72007-12-13 00:43:27 +0000744 if (AlreadySelected) break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000745
746 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
747 X86ISelAddressMode Backup = AM;
748 // Start with the LHS as an addr mode.
749 if (!MatchAddress(N.getOperand(0), AM, false) &&
750 // Address could not have picked a GV address for the displacement.
751 AM.GV == NULL &&
752 // On x86-64, the resultant disp must fit in 32-bits.
753 isInt32(AM.Disp + CN->getSignExtended()) &&
754 // Check to see if the LHS & C is zero.
755 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
756 AM.Disp += CN->getValue();
757 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000759 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 }
761 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000762
763 case ISD::AND: {
764 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
765 // allows us to fold the shift into this addressing mode.
766 if (AlreadySelected) break;
767 SDOperand Shift = N.getOperand(0);
768 if (Shift.getOpcode() != ISD::SHL) break;
769
770 // Scale must not be used already.
771 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
772
773 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
774 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
775 if (!C1 || !C2) break;
776
777 // Not likely to be profitable if either the AND or SHIFT node has more
778 // than one use (unless all uses are for address computation). Besides,
779 // isel mechanism requires their node ids to be reused.
780 if (!N.hasOneUse() || !Shift.hasOneUse())
781 break;
782
783 // Verify that the shift amount is something we can fold.
784 unsigned ShiftCst = C1->getValue();
785 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
786 break;
787
788 // Get the new AND mask, this folds to a constant.
789 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
790 SDOperand(C2, 0), SDOperand(C1, 0));
791 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
792 Shift.getOperand(0), NewANDMask);
793 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
794 NewAND.Val->setNodeId(N.Val->getNodeId());
795
796 AM.Scale = 1 << ShiftCst;
797 AM.IndexReg = NewAND;
798 return false;
799 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 }
801
Dan Gohmana60c1b32007-08-13 20:03:06 +0000802 return MatchAddressBase(N, AM, isRoot, Depth);
803}
804
805/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
806/// specified addressing mode without any further recursion.
807bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
808 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 // Is the base register already occupied?
810 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
811 // If so, check to see if the scale index register is set.
812 if (AM.IndexReg.Val == 0) {
813 AM.IndexReg = N;
814 AM.Scale = 1;
815 return false;
816 }
817
818 // Otherwise, we cannot select it.
819 return true;
820 }
821
822 // Default, generate it as a register.
823 AM.BaseType = X86ISelAddressMode::RegBase;
824 AM.Base.Reg = N;
825 return false;
826}
827
828/// SelectAddr - returns true if it is able pattern match an addressing mode.
829/// It returns the operands which make up the maximal addressing mode it can
830/// match by reference.
831bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
832 SDOperand &Scale, SDOperand &Index,
833 SDOperand &Disp) {
834 X86ISelAddressMode AM;
835 if (MatchAddress(N, AM))
836 return false;
837
838 MVT::ValueType VT = N.getValueType();
839 if (AM.BaseType == X86ISelAddressMode::RegBase) {
840 if (!AM.Base.Reg.Val)
841 AM.Base.Reg = CurDAG->getRegister(0, VT);
842 }
843
844 if (!AM.IndexReg.Val)
845 AM.IndexReg = CurDAG->getRegister(0, VT);
846
847 getAddressOperands(AM, Base, Scale, Index, Disp);
848 return true;
849}
850
851/// isZeroNode - Returns true if Elt is a constant zero or a floating point
852/// constant +0.0.
853static inline bool isZeroNode(SDOperand Elt) {
854 return ((isa<ConstantSDNode>(Elt) &&
855 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
856 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +0000857 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858}
859
860
861/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
862/// match a load whose top elements are either undef or zeros. The load flavor
863/// is derived from the type of N, which is either v4f32 or v2f64.
864bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
865 SDOperand N, SDOperand &Base,
866 SDOperand &Scale, SDOperand &Index,
867 SDOperand &Disp, SDOperand &InChain,
868 SDOperand &OutChain) {
869 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
870 InChain = N.getOperand(0).getValue(1);
871 if (ISD::isNON_EXTLoad(InChain.Val) &&
872 InChain.getValue(0).hasOneUse() &&
873 N.hasOneUse() &&
874 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
875 LoadSDNode *LD = cast<LoadSDNode>(InChain);
876 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
877 return false;
878 OutChain = LD->getChain();
879 return true;
880 }
881 }
882
883 // Also handle the case where we explicitly require zeros in the top
884 // elements. This is a vector shuffle from the zero vector.
885 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +0000886 // Check to see if the top elements are all zeros (or bitcast of zeros).
887 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
889 N.getOperand(1).Val->hasOneUse() &&
890 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
891 N.getOperand(1).getOperand(0).hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
893 // from the LHS.
Chris Lattnere6aa3862007-11-25 00:24:49 +0000894 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 SDOperand ShufMask = N.getOperand(2);
896 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
897 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
898 if (C->getValue() == VecWidth) {
899 for (unsigned i = 1; i != VecWidth; ++i) {
900 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
901 // ok.
902 } else {
903 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
904 if (C->getValue() >= VecWidth) return false;
905 }
906 }
907 }
908
909 // Okay, this is a zero extending load. Fold it.
910 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
911 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
912 return false;
913 OutChain = LD->getChain();
914 InChain = SDOperand(LD, 1);
915 return true;
916 }
917 }
918 return false;
919}
920
921
922/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
923/// mode it matches can be cost effectively emitted as an LEA instruction.
924bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
925 SDOperand &Base, SDOperand &Scale,
926 SDOperand &Index, SDOperand &Disp) {
927 X86ISelAddressMode AM;
928 if (MatchAddress(N, AM))
929 return false;
930
931 MVT::ValueType VT = N.getValueType();
932 unsigned Complexity = 0;
933 if (AM.BaseType == X86ISelAddressMode::RegBase)
934 if (AM.Base.Reg.Val)
935 Complexity = 1;
936 else
937 AM.Base.Reg = CurDAG->getRegister(0, VT);
938 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
939 Complexity = 4;
940
941 if (AM.IndexReg.Val)
942 Complexity++;
943 else
944 AM.IndexReg = CurDAG->getRegister(0, VT);
945
946 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
947 // a simple shift.
948 if (AM.Scale > 1)
949 Complexity++;
950
951 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
952 // to a LEA. This is determined with some expermentation but is by no means
953 // optimal (especially for code size consideration). LEA is nice because of
954 // its three-address nature. Tweak the cost function again when we can run
955 // convertToThreeAddress() at register allocation time.
956 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
957 // For X86-64, we should always use lea to materialize RIP relative
958 // addresses.
959 if (Subtarget->is64Bit())
960 Complexity = 4;
961 else
962 Complexity += 2;
963 }
964
965 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
966 Complexity++;
967
968 if (Complexity > 2) {
969 getAddressOperands(AM, Base, Scale, Index, Disp);
970 return true;
971 }
972 return false;
973}
974
975bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
976 SDOperand &Base, SDOperand &Scale,
977 SDOperand &Index, SDOperand &Disp) {
978 if (ISD::isNON_EXTLoad(N.Val) &&
979 N.hasOneUse() &&
980 CanBeFoldedBy(N.Val, P.Val, P.Val))
981 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
982 return false;
983}
984
985/// getGlobalBaseReg - Output the instructions required to put the
986/// base address to use for accessing globals into a register.
987///
988SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
989 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
990 if (!GlobalBaseReg) {
991 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0729ccf2008-01-05 00:41:47 +0000992 MachineFunction *MF = BB->getParent();
993 MachineBasicBlock &FirstMBB = MF->front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0729ccf2008-01-05 00:41:47 +0000995 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner1b989192007-12-31 04:13:23 +0000996 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997
998 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Cheng34f93712007-12-22 02:26:46 +0000999 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1000 // only used in JIT code emission as displacement to pc.
Evan Cheng0729ccf2008-01-05 00:41:47 +00001001 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002
1003 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1004 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
1005 if (TM.getRelocationModel() == Reloc::PIC_ &&
1006 Subtarget->isPICStyleGOT()) {
Chris Lattner1b989192007-12-31 04:13:23 +00001007 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0729ccf2008-01-05 00:41:47 +00001008 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1009 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 } else {
1011 GlobalBaseReg = PC;
1012 }
1013
1014 }
1015 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
1016}
1017
1018static SDNode *FindCallStartFromCall(SDNode *Node) {
1019 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1020 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1021 "Node doesn't have a token chain argument!");
1022 return FindCallStartFromCall(Node->getOperand(0).Val);
1023}
1024
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001025SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1026 SDOperand SRIdx;
1027 switch (VT) {
1028 case MVT::i8:
1029 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1030 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1031 if (!Subtarget->is64Bit()) {
1032 unsigned Opc;
1033 MVT::ValueType VT;
1034 switch (N0.getValueType()) {
1035 default: assert(0 && "Unknown truncate!");
1036 case MVT::i16:
1037 Opc = X86::MOV16to16_;
1038 VT = MVT::i16;
1039 break;
1040 case MVT::i32:
1041 Opc = X86::MOV32to32_;
1042 VT = MVT::i32;
1043 break;
1044 }
Evan Chenge1f39552007-10-12 07:55:53 +00001045 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1046 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1047 VT, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001048 }
1049 break;
1050 case MVT::i16:
1051 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1052 break;
1053 case MVT::i32:
1054 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1055 break;
Evan Chenge1f39552007-10-12 07:55:53 +00001056 default: assert(0 && "Unknown truncate!"); break;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001057 }
Evan Chenge1f39552007-10-12 07:55:53 +00001058 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001059}
1060
1061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062SDNode *X86DAGToDAGISel::Select(SDOperand N) {
1063 SDNode *Node = N.Val;
1064 MVT::ValueType NVT = Node->getValueType(0);
1065 unsigned Opc, MOpc;
1066 unsigned Opcode = Node->getOpcode();
1067
1068#ifndef NDEBUG
1069 DOUT << std::string(Indent, ' ') << "Selecting: ";
1070 DEBUG(Node->dump(CurDAG));
1071 DOUT << "\n";
1072 Indent += 2;
1073#endif
1074
1075 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
1076#ifndef NDEBUG
1077 DOUT << std::string(Indent-2, ' ') << "== ";
1078 DEBUG(Node->dump(CurDAG));
1079 DOUT << "\n";
1080 Indent -= 2;
1081#endif
1082 return NULL; // Already selected.
1083 }
1084
1085 switch (Opcode) {
1086 default: break;
1087 case X86ISD::GlobalBaseReg:
1088 return getGlobalBaseReg();
1089
1090 case ISD::ADD: {
1091 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1092 // code and is matched first so to prevent it from being turned into
1093 // LEA32r X+c.
1094 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1095 MVT::ValueType PtrVT = TLI.getPointerTy();
1096 SDOperand N0 = N.getOperand(0);
1097 SDOperand N1 = N.getOperand(1);
1098 if (N.Val->getValueType(0) == PtrVT &&
1099 N0.getOpcode() == X86ISD::Wrapper &&
1100 N1.getOpcode() == ISD::Constant) {
1101 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1102 SDOperand C(0, 0);
1103 // TODO: handle ExternalSymbolSDNode.
1104 if (GlobalAddressSDNode *G =
1105 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1106 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1107 G->getOffset() + Offset);
1108 } else if (ConstantPoolSDNode *CP =
1109 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1110 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1111 CP->getAlignment(),
1112 CP->getOffset()+Offset);
1113 }
1114
1115 if (C.Val) {
1116 if (Subtarget->is64Bit()) {
1117 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1118 CurDAG->getRegister(0, PtrVT), C };
1119 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1120 } else
1121 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1122 }
1123 }
1124
1125 // Other cases are handled by auto-generated code.
1126 break;
1127 }
1128
Dan Gohman5a199552007-10-08 18:33:35 +00001129 case ISD::SMUL_LOHI:
1130 case ISD::UMUL_LOHI: {
1131 SDOperand N0 = Node->getOperand(0);
1132 SDOperand N1 = Node->getOperand(1);
1133
Dan Gohmana5685ba2007-10-09 15:44:37 +00001134 // There are several forms of IMUL that just return the low part and
1135 // don't have fixed-register operands. If we don't need the high part,
1136 // use these instead. They can be selected with the generated ISel code.
Dan Gohman5a199552007-10-08 18:33:35 +00001137 if (NVT != MVT::i8 &&
1138 N.getValue(1).use_empty()) {
1139 N = CurDAG->getNode(ISD::MUL, NVT, N0, N1);
1140 break;
1141 }
1142
1143 bool isSigned = Opcode == ISD::SMUL_LOHI;
1144 if (!isSigned)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 switch (NVT) {
1146 default: assert(0 && "Unsupported VT!");
1147 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1148 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1149 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1150 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1151 }
1152 else
1153 switch (NVT) {
1154 default: assert(0 && "Unsupported VT!");
1155 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1156 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1157 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1158 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1159 }
1160
1161 unsigned LoReg, HiReg;
1162 switch (NVT) {
1163 default: assert(0 && "Unsupported VT!");
1164 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1165 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1166 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1167 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1168 }
1169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001171 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001172 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 if (!foldedLoad) {
1174 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001175 if (foldedLoad)
1176 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 }
1178
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179 AddToISelQueue(N0);
Dan Gohman5a199552007-10-08 18:33:35 +00001180 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1181 N0, SDOperand()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182
1183 if (foldedLoad) {
Dan Gohman5a199552007-10-08 18:33:35 +00001184 AddToISelQueue(N1.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001185 AddToISelQueue(Tmp0);
1186 AddToISelQueue(Tmp1);
1187 AddToISelQueue(Tmp2);
1188 AddToISelQueue(Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001189 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001190 SDNode *CNode =
1191 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 InFlag = SDOperand(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001193 // Update the chain.
1194 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 } else {
1196 AddToISelQueue(N1);
1197 InFlag =
1198 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1199 }
1200
Dan Gohman5a199552007-10-08 18:33:35 +00001201 // Copy the low half of the result, if it is needed.
1202 if (!N.getValue(0).use_empty()) {
1203 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1204 LoReg, NVT, InFlag);
1205 InFlag = Result.getValue(2);
1206 ReplaceUses(N.getValue(0), Result);
1207#ifndef NDEBUG
1208 DOUT << std::string(Indent-2, ' ') << "=> ";
1209 DEBUG(Result.Val->dump(CurDAG));
1210 DOUT << "\n";
1211#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001212 }
Dan Gohman5a199552007-10-08 18:33:35 +00001213 // Copy the high half of the result, if it is needed.
1214 if (!N.getValue(1).use_empty()) {
1215 SDOperand Result;
1216 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1217 // Prevent use of AH in a REX instruction by referencing AX instead.
1218 // Shift it down 8 bits.
1219 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1220 X86::AX, MVT::i16, InFlag);
1221 InFlag = Result.getValue(2);
1222 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1223 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1224 // Then truncate it down to i8.
1225 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1226 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1227 MVT::i8, Result, SRIdx), 0);
1228 } else {
1229 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1230 HiReg, NVT, InFlag);
1231 InFlag = Result.getValue(2);
1232 }
1233 ReplaceUses(N.getValue(1), Result);
1234#ifndef NDEBUG
1235 DOUT << std::string(Indent-2, ' ') << "=> ";
1236 DEBUG(Result.Val->dump(CurDAG));
1237 DOUT << "\n";
1238#endif
1239 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240
1241#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 Indent -= 2;
1243#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245 return NULL;
1246 }
1247
Dan Gohman5a199552007-10-08 18:33:35 +00001248 case ISD::SDIVREM:
1249 case ISD::UDIVREM: {
1250 SDOperand N0 = Node->getOperand(0);
1251 SDOperand N1 = Node->getOperand(1);
1252
1253 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001254 if (!isSigned)
1255 switch (NVT) {
1256 default: assert(0 && "Unsupported VT!");
1257 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1258 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1259 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1260 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1261 }
1262 else
1263 switch (NVT) {
1264 default: assert(0 && "Unsupported VT!");
1265 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1266 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1267 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1268 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1269 }
1270
1271 unsigned LoReg, HiReg;
1272 unsigned ClrOpcode, SExtOpcode;
1273 switch (NVT) {
1274 default: assert(0 && "Unsupported VT!");
1275 case MVT::i8:
1276 LoReg = X86::AL; HiReg = X86::AH;
1277 ClrOpcode = 0;
1278 SExtOpcode = X86::CBW;
1279 break;
1280 case MVT::i16:
1281 LoReg = X86::AX; HiReg = X86::DX;
1282 ClrOpcode = X86::MOV16r0;
1283 SExtOpcode = X86::CWD;
1284 break;
1285 case MVT::i32:
1286 LoReg = X86::EAX; HiReg = X86::EDX;
1287 ClrOpcode = X86::MOV32r0;
1288 SExtOpcode = X86::CDQ;
1289 break;
1290 case MVT::i64:
1291 LoReg = X86::RAX; HiReg = X86::RDX;
1292 ClrOpcode = X86::MOV64r0;
1293 SExtOpcode = X86::CQO;
1294 break;
1295 }
1296
Dan Gohman5a199552007-10-08 18:33:35 +00001297 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1298 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1299
1300 SDOperand InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 if (NVT == MVT::i8 && !isSigned) {
1302 // Special case for div8, just use a move with zero extension to AX to
1303 // clear the upper 8 bits (AH).
1304 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1305 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1306 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1307 AddToISelQueue(N0.getOperand(0));
1308 AddToISelQueue(Tmp0);
1309 AddToISelQueue(Tmp1);
1310 AddToISelQueue(Tmp2);
1311 AddToISelQueue(Tmp3);
1312 Move =
1313 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1314 Ops, 5), 0);
1315 Chain = Move.getValue(1);
1316 ReplaceUses(N0.getValue(1), Chain);
1317 } else {
1318 AddToISelQueue(N0);
1319 Move =
1320 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1321 Chain = CurDAG->getEntryNode();
1322 }
Dan Gohman5a199552007-10-08 18:33:35 +00001323 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 InFlag = Chain.getValue(1);
1325 } else {
1326 AddToISelQueue(N0);
1327 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001328 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1329 LoReg, N0, SDOperand()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 if (isSigned) {
1331 // Sign extend the low part into the high part.
1332 InFlag =
1333 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1334 } else {
1335 // Zero out the high part, effectively zero extending the input.
1336 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001337 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1338 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 }
1340 }
1341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342 if (foldedLoad) {
1343 AddToISelQueue(N1.getOperand(0));
1344 AddToISelQueue(Tmp0);
1345 AddToISelQueue(Tmp1);
1346 AddToISelQueue(Tmp2);
1347 AddToISelQueue(Tmp3);
1348 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1349 SDNode *CNode =
1350 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 InFlag = SDOperand(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001352 // Update the chain.
1353 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 } else {
1355 AddToISelQueue(N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 InFlag =
1357 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1358 }
1359
Dan Gohman242a5ba2007-09-25 18:23:27 +00001360 // Copy the division (low) result, if it is needed.
1361 if (!N.getValue(0).use_empty()) {
Dan Gohman5a199552007-10-08 18:33:35 +00001362 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1363 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001364 InFlag = Result.getValue(2);
1365 ReplaceUses(N.getValue(0), Result);
1366#ifndef NDEBUG
1367 DOUT << std::string(Indent-2, ' ') << "=> ";
1368 DEBUG(Result.Val->dump(CurDAG));
1369 DOUT << "\n";
1370#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001371 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001372 // Copy the remainder (high) result, if it is needed.
1373 if (!N.getValue(1).use_empty()) {
1374 SDOperand Result;
1375 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1376 // Prevent use of AH in a REX instruction by referencing AX instead.
1377 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001378 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1379 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001380 InFlag = Result.getValue(2);
1381 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1382 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1383 // Then truncate it down to i8.
1384 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1385 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1386 MVT::i8, Result, SRIdx), 0);
1387 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001388 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1389 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001390 InFlag = Result.getValue(2);
1391 }
1392 ReplaceUses(N.getValue(1), Result);
1393#ifndef NDEBUG
1394 DOUT << std::string(Indent-2, ' ') << "=> ";
1395 DEBUG(Result.Val->dump(CurDAG));
1396 DOUT << "\n";
1397#endif
1398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399
1400#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 Indent -= 2;
1402#endif
1403
1404 return NULL;
1405 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001406
1407 case ISD::ANY_EXTEND: {
1408 SDOperand N0 = Node->getOperand(0);
1409 AddToISelQueue(N0);
1410 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1411 SDOperand SRIdx;
1412 switch(N0.getValueType()) {
1413 case MVT::i32:
1414 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1415 break;
1416 case MVT::i16:
1417 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1418 break;
1419 case MVT::i8:
1420 if (Subtarget->is64Bit())
1421 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1422 break;
1423 default: assert(0 && "Unknown any_extend!");
1424 }
1425 if (SRIdx.Val) {
Evan Chenge1f39552007-10-12 07:55:53 +00001426 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1427 NVT, N0, SRIdx);
Christopher Lamb422213d2007-08-10 22:22:41 +00001428
1429#ifndef NDEBUG
1430 DOUT << std::string(Indent-2, ' ') << "=> ";
1431 DEBUG(ResNode->dump(CurDAG));
1432 DOUT << "\n";
1433 Indent -= 2;
1434#endif
1435 return ResNode;
1436 } // Otherwise let generated ISel handle it.
1437 }
1438 break;
1439 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001440
1441 case ISD::SIGN_EXTEND_INREG: {
1442 SDOperand N0 = Node->getOperand(0);
1443 AddToISelQueue(N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001445 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1446 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling79bb1a22007-11-01 08:51:44 +00001447 unsigned Opc = 0;
Christopher Lamb444336c2007-07-29 01:24:57 +00001448 switch (NVT) {
Christopher Lamb444336c2007-07-29 01:24:57 +00001449 case MVT::i16:
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001450 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1451 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb444336c2007-07-29 01:24:57 +00001452 break;
1453 case MVT::i32:
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001454 switch (SVT) {
1455 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1456 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1457 default: assert(0 && "Unknown sign_extend_inreg!");
1458 }
Christopher Lamb444336c2007-07-29 01:24:57 +00001459 break;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001460 case MVT::i64:
1461 switch (SVT) {
1462 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1463 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1464 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1465 default: assert(0 && "Unknown sign_extend_inreg!");
1466 }
1467 break;
1468 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb444336c2007-07-29 01:24:57 +00001469 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001470
1471 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1472
1473#ifndef NDEBUG
1474 DOUT << std::string(Indent-2, ' ') << "=> ";
1475 DEBUG(TruncOp.Val->dump(CurDAG));
1476 DOUT << "\n";
1477 DOUT << std::string(Indent-2, ' ') << "=> ";
1478 DEBUG(ResNode->dump(CurDAG));
1479 DOUT << "\n";
1480 Indent -= 2;
1481#endif
1482 return ResNode;
1483 break;
1484 }
1485
1486 case ISD::TRUNCATE: {
1487 SDOperand Input = Node->getOperand(0);
1488 AddToISelQueue(Node->getOperand(0));
1489 SDNode *ResNode = getTruncate(Input, NVT);
1490
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491#ifndef NDEBUG
1492 DOUT << std::string(Indent-2, ' ') << "=> ";
1493 DEBUG(ResNode->dump(CurDAG));
1494 DOUT << "\n";
1495 Indent -= 2;
1496#endif
Christopher Lamb444336c2007-07-29 01:24:57 +00001497 return ResNode;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 break;
1499 }
1500 }
1501
1502 SDNode *ResNode = SelectCode(N);
1503
1504#ifndef NDEBUG
1505 DOUT << std::string(Indent-2, ' ') << "=> ";
1506 if (ResNode == NULL || ResNode == N.Val)
1507 DEBUG(N.Val->dump(CurDAG));
1508 else
1509 DEBUG(ResNode->dump(CurDAG));
1510 DOUT << "\n";
1511 Indent -= 2;
1512#endif
1513
1514 return ResNode;
1515}
1516
1517bool X86DAGToDAGISel::
1518SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1519 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1520 SDOperand Op0, Op1, Op2, Op3;
1521 switch (ConstraintCode) {
1522 case 'o': // offsetable ??
1523 case 'v': // not offsetable ??
1524 default: return true;
1525 case 'm': // memory
1526 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1527 return true;
1528 break;
1529 }
1530
1531 OutOps.push_back(Op0);
1532 OutOps.push_back(Op1);
1533 OutOps.push_back(Op2);
1534 OutOps.push_back(Op3);
1535 AddToISelQueue(Op0);
1536 AddToISelQueue(Op1);
1537 AddToISelQueue(Op2);
1538 AddToISelQueue(Op3);
1539 return false;
1540}
1541
1542/// createX86ISelDag - This pass converts a legalized DAG into a
1543/// X86-specific DAG, ready for instruction scheduling.
1544///
1545FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1546 return new X86DAGToDAGISel(TM, Fast);
1547}