Revert the assert for MUL_LOHI with an unused high result; Chris
[oota-llvm.git] / lib / Target / X86 / X86ISelDAGToDAG.cpp
1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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
15 #define DEBUG_TYPE "x86-isel"
16 #include "X86.h"
17 #include "X86InstrBuilder.h"
18 #include "X86ISelLowering.h"
19 #include "X86MachineFunctionInfo.h"
20 #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"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/SelectionDAGISel.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/ADT/Statistic.h"
40 #include <queue>
41 #include <set>
42 using namespace llvm;
43
44 STATISTIC(NumFPKill   , "Number of FP_REG_KILL instructions added");
45 STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
46
47 namespace {
48   static cl::opt<bool>
49   AlwaysFoldAndInTest("always-fold-and-in-test",
50                 cl::desc("Always fold and operation in test"),
51                 cl::init(true), cl::Hidden);
52 }
53
54 //===----------------------------------------------------------------------===//
55 //                      Pattern Matcher Implementation
56 //===----------------------------------------------------------------------===//
57
58 namespace {
59   /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
60   /// SDOperand's instead of register numbers for the leaves of the matched
61   /// tree.
62   struct X86ISelAddressMode {
63     enum {
64       RegBase,
65       FrameIndexBase
66     } BaseType;
67
68     struct {            // This is really a union, discriminated by BaseType!
69       SDOperand Reg;
70       int FrameIndex;
71     } Base;
72
73     bool isRIPRel;     // RIP as base?
74     unsigned Scale;
75     SDOperand IndexReg; 
76     unsigned Disp;
77     GlobalValue *GV;
78     Constant *CP;
79     const char *ES;
80     int JT;
81     unsigned Align;    // CP alignment.
82
83     X86ISelAddressMode()
84       : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
85         GV(0), CP(0), ES(0), JT(-1), Align(0) {
86     }
87   };
88 }
89
90 namespace {
91   //===--------------------------------------------------------------------===//
92   /// ISel - X86 specific code to select X86 machine instructions for
93   /// SelectionDAG operations.
94   ///
95   class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
96     /// ContainsFPCode - Every instruction we select that uses or defines a FP
97     /// register should set this to true.
98     bool ContainsFPCode;
99
100     /// FastISel - Enable fast(er) instruction selection.
101     ///
102     bool FastISel;
103
104     /// TM - Keep a reference to X86TargetMachine.
105     ///
106     X86TargetMachine &TM;
107
108     /// X86Lowering - This object fully describes how to lower LLVM code to an
109     /// X86-specific SelectionDAG.
110     X86TargetLowering X86Lowering;
111
112     /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
113     /// make the right decision when generating code for different targets.
114     const X86Subtarget *Subtarget;
115
116     /// GlobalBaseReg - keeps track of the virtual register mapped onto global
117     /// base register.
118     unsigned GlobalBaseReg;
119
120   public:
121     X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
122       : SelectionDAGISel(X86Lowering),
123         ContainsFPCode(false), FastISel(fast), TM(tm),
124         X86Lowering(*TM.getTargetLowering()),
125         Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
126
127     virtual bool runOnFunction(Function &Fn) {
128       // Make sure we re-emit a set of the global base reg if necessary
129       GlobalBaseReg = 0;
130       return SelectionDAGISel::runOnFunction(Fn);
131     }
132    
133     virtual const char *getPassName() const {
134       return "X86 DAG->DAG Instruction Selection";
135     }
136
137     /// InstructionSelectBasicBlock - This callback is invoked by
138     /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
139     virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
140
141     virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
142
143     virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
144
145 // Include the pieces autogenerated from the target description.
146 #include "X86GenDAGISel.inc"
147
148   private:
149     SDNode *Select(SDOperand N);
150
151     bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
152                       bool isRoot = true, unsigned Depth = 0);
153     bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
154                           bool isRoot, unsigned Depth);
155     bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
156                     SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
157     bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
158                        SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
159     bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
160                              SDOperand N, SDOperand &Base, SDOperand &Scale,
161                              SDOperand &Index, SDOperand &Disp,
162                              SDOperand &InChain, SDOperand &OutChain);
163     bool TryFoldLoad(SDOperand P, SDOperand N,
164                      SDOperand &Base, SDOperand &Scale,
165                      SDOperand &Index, SDOperand &Disp);
166     void PreprocessForRMW(SelectionDAG &DAG);
167     void PreprocessForFPConvert(SelectionDAG &DAG);
168
169     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
170     /// inline asm expressions.
171     virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
172                                               char ConstraintCode,
173                                               std::vector<SDOperand> &OutOps,
174                                               SelectionDAG &DAG);
175     
176     void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
177
178     inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, 
179                                    SDOperand &Scale, SDOperand &Index,
180                                    SDOperand &Disp) {
181       Base  = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
182         CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
183         AM.Base.Reg;
184       Scale = getI8Imm(AM.Scale);
185       Index = AM.IndexReg;
186       // These are 32-bit even in 64-bit mode since RIP relative offset
187       // is 32-bit.
188       if (AM.GV)
189         Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
190       else if (AM.CP)
191         Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
192       else if (AM.ES)
193         Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
194       else if (AM.JT != -1)
195         Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
196       else
197         Disp = getI32Imm(AM.Disp);
198     }
199
200     /// getI8Imm - Return a target constant with the specified value, of type
201     /// i8.
202     inline SDOperand getI8Imm(unsigned Imm) {
203       return CurDAG->getTargetConstant(Imm, MVT::i8);
204     }
205
206     /// getI16Imm - Return a target constant with the specified value, of type
207     /// i16.
208     inline SDOperand getI16Imm(unsigned Imm) {
209       return CurDAG->getTargetConstant(Imm, MVT::i16);
210     }
211
212     /// getI32Imm - Return a target constant with the specified value, of type
213     /// i32.
214     inline SDOperand getI32Imm(unsigned Imm) {
215       return CurDAG->getTargetConstant(Imm, MVT::i32);
216     }
217
218     /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
219     /// base register.  Return the virtual register that holds this value.
220     SDNode *getGlobalBaseReg();
221
222     /// getTruncate - return an SDNode that implements a subreg based truncate
223     /// of the specified operand to the the specified value type.
224     SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
225
226 #ifndef NDEBUG
227     unsigned Indent;
228 #endif
229   };
230 }
231
232 static SDNode *findFlagUse(SDNode *N) {
233   unsigned FlagResNo = N->getNumValues()-1;
234   for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
235     SDNode *User = *I;
236     for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
237       SDOperand Op = User->getOperand(i);
238       if (Op.Val == N && Op.ResNo == FlagResNo)
239         return User;
240     }
241   }
242   return NULL;
243 }
244
245 static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
246                           SDNode *Root, SDNode *Skip, bool &found,
247                           std::set<SDNode *> &Visited) {
248   if (found ||
249       Use->getNodeId() > Def->getNodeId() ||
250       !Visited.insert(Use).second)
251     return;
252
253   for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
254     SDNode *N = Use->getOperand(i).Val;
255     if (N == Skip)
256       continue;
257     if (N == Def) {
258       if (Use == ImmedUse)
259         continue; // Immediate use is ok.
260       if (Use == Root) {
261         assert(Use->getOpcode() == ISD::STORE ||
262                Use->getOpcode() == X86ISD::CMP);
263         continue;
264       }
265       found = true;
266       break;
267     }
268     findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
269   }
270 }
271
272 /// isNonImmUse - Start searching from Root up the DAG to check is Def can
273 /// be reached. Return true if that's the case. However, ignore direct uses
274 /// by ImmedUse (which would be U in the example illustrated in
275 /// CanBeFoldedBy) and by Root (which can happen in the store case).
276 /// FIXME: to be really generic, we should allow direct use by any node
277 /// that is being folded. But realisticly since we only fold loads which
278 /// have one non-chain use, we only need to watch out for load/op/store
279 /// and load/op/cmp case where the root (store / cmp) may reach the load via
280 /// its chain operand.
281 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
282                                SDNode *Skip = NULL) {
283   std::set<SDNode *> Visited;
284   bool found = false;
285   findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
286   return found;
287 }
288
289
290 bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
291   if (FastISel) return false;
292
293   // If U use can somehow reach N through another path then U can't fold N or
294   // it will create a cycle. e.g. In the following diagram, U can reach N
295   // through X. If N is folded into into U, then X is both a predecessor and
296   // a successor of U.
297   //
298   //         [ N ]
299   //         ^  ^
300   //         |  |
301   //        /   \---
302   //      /        [X]
303   //      |         ^
304   //     [U]--------|
305
306   if (isNonImmUse(Root, N, U))
307     return false;
308
309   // If U produces a flag, then it gets (even more) interesting. Since it
310   // would have been "glued" together with its flag use, we need to check if
311   // it might reach N:
312   //
313   //       [ N ]
314   //        ^ ^
315   //        | |
316   //       [U] \--
317   //        ^   [TF]
318   //        |    ^
319   //        |    |
320   //         \  /
321   //          [FU]
322   //
323   // If FU (flag use) indirectly reach N (the load), and U fold N (call it
324   // NU), then TF is a predecessor of FU and a successor of NU. But since
325   // NU and FU are flagged together, this effectively creates a cycle.
326   bool HasFlagUse = false;
327   MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
328   while ((VT == MVT::Flag && !Root->use_empty())) {
329     SDNode *FU = findFlagUse(Root);
330     if (FU == NULL)
331       break;
332     else {
333       Root = FU;
334       HasFlagUse = true;
335     }
336     VT = Root->getValueType(Root->getNumValues()-1);
337   }
338
339   if (HasFlagUse)
340     return !isNonImmUse(Root, N, Root, U);
341   return true;
342 }
343
344 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
345 /// and move load below the TokenFactor. Replace store's chain operand with
346 /// load's chain result.
347 static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
348                                  SDOperand Store, SDOperand TF) {
349   std::vector<SDOperand> Ops;
350   for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
351     if (Load.Val == TF.Val->getOperand(i).Val)
352       Ops.push_back(Load.Val->getOperand(0));
353     else
354       Ops.push_back(TF.Val->getOperand(i));
355   DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
356   DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
357   DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
358                          Store.getOperand(2), Store.getOperand(3));
359 }
360
361 /// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
362 /// This is only run if not in -fast mode (aka -O0).
363 /// This allows the instruction selector to pick more read-modify-write
364 /// instructions. This is a common case:
365 ///
366 ///     [Load chain]
367 ///         ^
368 ///         |
369 ///       [Load]
370 ///       ^    ^
371 ///       |    |
372 ///      /      \-
373 ///     /         |
374 /// [TokenFactor] [Op]
375 ///     ^          ^
376 ///     |          |
377 ///      \        /
378 ///       \      /
379 ///       [Store]
380 ///
381 /// The fact the store's chain operand != load's chain will prevent the
382 /// (store (op (load))) instruction from being selected. We can transform it to:
383 ///
384 ///     [Load chain]
385 ///         ^
386 ///         |
387 ///    [TokenFactor]
388 ///         ^
389 ///         |
390 ///       [Load]
391 ///       ^    ^
392 ///       |    |
393 ///       |     \- 
394 ///       |       | 
395 ///       |     [Op]
396 ///       |       ^
397 ///       |       |
398 ///       \      /
399 ///        \    /
400 ///       [Store]
401 void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
402   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
403          E = DAG.allnodes_end(); I != E; ++I) {
404     if (!ISD::isNON_TRUNCStore(I))
405       continue;
406     SDOperand Chain = I->getOperand(0);
407     if (Chain.Val->getOpcode() != ISD::TokenFactor)
408       continue;
409
410     SDOperand N1 = I->getOperand(1);
411     SDOperand N2 = I->getOperand(2);
412     if (MVT::isFloatingPoint(N1.getValueType()) ||
413         MVT::isVector(N1.getValueType()) ||
414         !N1.hasOneUse())
415       continue;
416
417     bool RModW = false;
418     SDOperand Load;
419     unsigned Opcode = N1.Val->getOpcode();
420     switch (Opcode) {
421       case ISD::ADD:
422       case ISD::MUL:
423       case ISD::AND:
424       case ISD::OR:
425       case ISD::XOR:
426       case ISD::ADDC:
427       case ISD::ADDE: {
428         SDOperand N10 = N1.getOperand(0);
429         SDOperand N11 = N1.getOperand(1);
430         if (ISD::isNON_EXTLoad(N10.Val))
431           RModW = true;
432         else if (ISD::isNON_EXTLoad(N11.Val)) {
433           RModW = true;
434           std::swap(N10, N11);
435         }
436         RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
437           (N10.getOperand(1) == N2) &&
438           (N10.Val->getValueType(0) == N1.getValueType());
439         if (RModW)
440           Load = N10;
441         break;
442       }
443       case ISD::SUB:
444       case ISD::SHL:
445       case ISD::SRA:
446       case ISD::SRL:
447       case ISD::ROTL:
448       case ISD::ROTR:
449       case ISD::SUBC:
450       case ISD::SUBE:
451       case X86ISD::SHLD:
452       case X86ISD::SHRD: {
453         SDOperand N10 = N1.getOperand(0);
454         if (ISD::isNON_EXTLoad(N10.Val))
455           RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
456             (N10.getOperand(1) == N2) &&
457             (N10.Val->getValueType(0) == N1.getValueType());
458         if (RModW)
459           Load = N10;
460         break;
461       }
462     }
463
464     if (RModW) {
465       MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
466       ++NumLoadMoved;
467     }
468   }
469 }
470
471
472 /// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
473 /// nodes that target the FP stack to be store and load to the stack.  This is a
474 /// gross hack.  We would like to simply mark these as being illegal, but when
475 /// we do that, legalize produces these when it expands calls, then expands
476 /// these in the same legalize pass.  We would like dag combine to be able to
477 /// hack on these between the call expansion and the node legalization.  As such
478 /// this pass basically does "really late" legalization of these inline with the
479 /// X86 isel pass.
480 void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
481   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
482        E = DAG.allnodes_end(); I != E; ) {
483     SDNode *N = I++;  // Preincrement iterator to avoid invalidation issues.
484     if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
485       continue;
486     
487     // If the source and destination are SSE registers, then this is a legal
488     // conversion that should not be lowered.
489     MVT::ValueType SrcVT = N->getOperand(0).getValueType();
490     MVT::ValueType DstVT = N->getValueType(0);
491     bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
492     bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
493     if (SrcIsSSE && DstIsSSE)
494       continue;
495
496     // If this is an FPStack extension (but not a truncation), it is a noop.
497     if (!SrcIsSSE && !DstIsSSE && N->getOpcode() == ISD::FP_EXTEND)
498       continue;
499     
500     // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
501     // FPStack has extload and truncstore.  SSE can fold direct loads into other
502     // operations.  Based on this, decide what we want to do.
503     MVT::ValueType MemVT;
504     if (N->getOpcode() == ISD::FP_ROUND)
505       MemVT = DstVT;  // FP_ROUND must use DstVT, we can't do a 'trunc load'.
506     else
507       MemVT = SrcIsSSE ? SrcVT : DstVT;
508     
509     SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
510     
511     // FIXME: optimize the case where the src/dest is a load or store?
512     SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
513                                         MemTmp, NULL, 0, MemVT);
514     SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
515                                       NULL, 0, MemVT);
516
517     // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
518     // extload we created.  This will cause general havok on the dag because
519     // anything below the conversion could be folded into other existing nodes.
520     // To avoid invalidating 'I', back it up to the convert node.
521     --I;
522     DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
523     
524     // Now that we did that, the node is dead.  Increment the iterator to the
525     // next node to process, then delete N.
526     ++I;
527     DAG.DeleteNode(N);
528   }  
529 }
530
531 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
532 /// when it has created a SelectionDAG for us to codegen.
533 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
534   DEBUG(BB->dump());
535   MachineFunction::iterator FirstMBB = BB;
536
537   if (!FastISel)
538     PreprocessForRMW(DAG);
539
540   // FIXME: This should only happen when not -fast.
541   PreprocessForFPConvert(DAG);
542
543   // Codegen the basic block.
544 #ifndef NDEBUG
545   DOUT << "===== Instruction selection begins:\n";
546   Indent = 0;
547 #endif
548   DAG.setRoot(SelectRoot(DAG.getRoot()));
549 #ifndef NDEBUG
550   DOUT << "===== Instruction selection ends:\n";
551 #endif
552
553   DAG.RemoveDeadNodes();
554
555   // Emit machine code to BB. 
556   ScheduleAndEmitDAG(DAG);
557   
558   // If we are emitting FP stack code, scan the basic block to determine if this
559   // block defines any FP values.  If so, put an FP_REG_KILL instruction before
560   // the terminator of the block.
561
562   // Note that FP stack instructions are used in all modes for long double,
563   // so we always need to do this check.
564   // Also note that it's possible for an FP stack register to be live across
565   // an instruction that produces multiple basic blocks (SSE CMOV) so we
566   // must check all the generated basic blocks.
567
568   // Scan all of the machine instructions in these MBBs, checking for FP
569   // stores.  (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
570   MachineFunction::iterator MBBI = FirstMBB;
571   do {
572     bool ContainsFPCode = false;
573     for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
574          !ContainsFPCode && I != E; ++I) {
575       if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
576         const TargetRegisterClass *clas;
577         for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
578           if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
579               TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
580               ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) == 
581                  X86::RFP32RegisterClass ||
582                clas == X86::RFP64RegisterClass ||
583                clas == X86::RFP80RegisterClass)) {
584             ContainsFPCode = true;
585             break;
586           }
587         }
588       }
589     }
590     // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
591     // a copy of the input value in this block.  In SSE mode, we only care about
592     // 80-bit values.
593     if (!ContainsFPCode) {
594       // Final check, check LLVM BB's that are successors to the LLVM BB
595       // corresponding to BB for FP PHI nodes.
596       const BasicBlock *LLVMBB = BB->getBasicBlock();
597       const PHINode *PN;
598       for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
599            !ContainsFPCode && SI != E; ++SI) {
600         for (BasicBlock::const_iterator II = SI->begin();
601              (PN = dyn_cast<PHINode>(II)); ++II) {
602           if (PN->getType()==Type::X86_FP80Ty ||
603               (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
604               (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
605             ContainsFPCode = true;
606             break;
607           }
608         }
609       }
610     }
611     // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
612     if (ContainsFPCode) {
613       BuildMI(*MBBI, MBBI->getFirstTerminator(),
614               TM.getInstrInfo()->get(X86::FP_REG_KILL));
615       ++NumFPKill;
616     }
617   } while (&*(MBBI++) != BB);
618 }
619
620 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
621 /// the main function.
622 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
623                                              MachineFrameInfo *MFI) {
624   const TargetInstrInfo *TII = TM.getInstrInfo();
625   if (Subtarget->isTargetCygMing())
626     BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
627 }
628
629 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
630   // If this is main, emit special code for main.
631   MachineBasicBlock *BB = MF.begin();
632   if (Fn.hasExternalLinkage() && Fn.getName() == "main")
633     EmitSpecialCodeForMain(BB, MF.getFrameInfo());
634 }
635
636 /// MatchAddress - Add the specified node to the specified addressing mode,
637 /// returning true if it cannot be done.  This just pattern matches for the
638 /// addressing mode.
639 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
640                                    bool isRoot, unsigned Depth) {
641   // Limit recursion.
642   if (Depth > 5)
643     return MatchAddressBase(N, AM, isRoot, Depth);
644   
645   // RIP relative addressing: %rip + 32-bit displacement!
646   if (AM.isRIPRel) {
647     if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
648       int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
649       if (isInt32(AM.Disp + Val)) {
650         AM.Disp += Val;
651         return false;
652       }
653     }
654     return true;
655   }
656
657   int id = N.Val->getNodeId();
658   bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
659
660   switch (N.getOpcode()) {
661   default: break;
662   case ISD::Constant: {
663     int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
664     if (isInt32(AM.Disp + Val)) {
665       AM.Disp += Val;
666       return false;
667     }
668     break;
669   }
670
671   case X86ISD::Wrapper: {
672     bool is64Bit = Subtarget->is64Bit();
673     // Under X86-64 non-small code model, GV (and friends) are 64-bits.
674     // Also, base and index reg must be 0 in order to use rip as base.
675     if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
676                     AM.Base.Reg.Val || AM.IndexReg.Val))
677       break;
678     if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
679       break;
680     // If value is available in a register both base and index components have
681     // been picked, we can't fit the result available in the register in the
682     // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
683     if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
684       SDOperand N0 = N.getOperand(0);
685       if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
686         GlobalValue *GV = G->getGlobal();
687         AM.GV = GV;
688         AM.Disp += G->getOffset();
689         AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
690           Subtarget->isPICStyleRIPRel();
691         return false;
692       } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
693         AM.CP = CP->getConstVal();
694         AM.Align = CP->getAlignment();
695         AM.Disp += CP->getOffset();
696         AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
697           Subtarget->isPICStyleRIPRel();
698         return false;
699       } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
700         AM.ES = S->getSymbol();
701         AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
702           Subtarget->isPICStyleRIPRel();
703         return false;
704       } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
705         AM.JT = J->getIndex();
706         AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
707           Subtarget->isPICStyleRIPRel();
708         return false;
709       }
710     }
711     break;
712   }
713
714   case ISD::FrameIndex:
715     if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
716       AM.BaseType = X86ISelAddressMode::FrameIndexBase;
717       AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
718       return false;
719     }
720     break;
721
722   case ISD::SHL:
723     if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
724       break;
725       
726     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
727       unsigned Val = CN->getValue();
728       if (Val == 1 || Val == 2 || Val == 3) {
729         AM.Scale = 1 << Val;
730         SDOperand ShVal = N.Val->getOperand(0);
731
732         // Okay, we know that we have a scale by now.  However, if the scaled
733         // value is an add of something and a constant, we can fold the
734         // constant into the disp field here.
735         if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
736             isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
737           AM.IndexReg = ShVal.Val->getOperand(0);
738           ConstantSDNode *AddVal =
739             cast<ConstantSDNode>(ShVal.Val->getOperand(1));
740           uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
741           if (isInt32(Disp))
742             AM.Disp = Disp;
743           else
744             AM.IndexReg = ShVal;
745         } else {
746           AM.IndexReg = ShVal;
747         }
748         return false;
749       }
750     break;
751     }
752
753   case ISD::SMUL_LOHI:
754   case ISD::UMUL_LOHI:
755     // A mul_lohi where we need the low part can be folded as a plain multiply.
756     if (N.ResNo != 0) break;
757     // FALL THROUGH
758   case ISD::MUL:
759     // X*[3,5,9] -> X+X*[2,4,8]
760     if (!AlreadySelected &&
761         AM.BaseType == X86ISelAddressMode::RegBase &&
762         AM.Base.Reg.Val == 0 &&
763         AM.IndexReg.Val == 0 &&
764         !AM.isRIPRel) {
765       if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
766         if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
767           AM.Scale = unsigned(CN->getValue())-1;
768
769           SDOperand MulVal = N.Val->getOperand(0);
770           SDOperand Reg;
771
772           // Okay, we know that we have a scale by now.  However, if the scaled
773           // value is an add of something and a constant, we can fold the
774           // constant into the disp field here.
775           if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
776               isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
777             Reg = MulVal.Val->getOperand(0);
778             ConstantSDNode *AddVal =
779               cast<ConstantSDNode>(MulVal.Val->getOperand(1));
780             uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
781             if (isInt32(Disp))
782               AM.Disp = Disp;
783             else
784               Reg = N.Val->getOperand(0);
785           } else {
786             Reg = N.Val->getOperand(0);
787           }
788
789           AM.IndexReg = AM.Base.Reg = Reg;
790           return false;
791         }
792     }
793     break;
794
795   case ISD::ADD:
796     if (!AlreadySelected) {
797       X86ISelAddressMode Backup = AM;
798       if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
799           !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
800         return false;
801       AM = Backup;
802       if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
803           !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
804         return false;
805       AM = Backup;
806     }
807     break;
808
809   case ISD::OR:
810     // Handle "X | C" as "X + C" iff X is known to have C bits clear.
811     if (AlreadySelected) break;
812       
813     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
814       X86ISelAddressMode Backup = AM;
815       // Start with the LHS as an addr mode.
816       if (!MatchAddress(N.getOperand(0), AM, false) &&
817           // Address could not have picked a GV address for the displacement.
818           AM.GV == NULL &&
819           // On x86-64, the resultant disp must fit in 32-bits.
820           isInt32(AM.Disp + CN->getSignExtended()) &&
821           // Check to see if the LHS & C is zero.
822           CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
823         AM.Disp += CN->getValue();
824         return false;
825       }
826       AM = Backup;
827     }
828     break;
829       
830   case ISD::AND: {
831     // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
832     // allows us to fold the shift into this addressing mode.
833     if (AlreadySelected) break;
834     SDOperand Shift = N.getOperand(0);
835     if (Shift.getOpcode() != ISD::SHL) break;
836     
837     // Scale must not be used already.
838     if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
839
840     // Not when RIP is used as the base.
841     if (AM.isRIPRel) break;
842       
843     ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
844     ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
845     if (!C1 || !C2) break;
846
847     // Not likely to be profitable if either the AND or SHIFT node has more
848     // than one use (unless all uses are for address computation). Besides,
849     // isel mechanism requires their node ids to be reused.
850     if (!N.hasOneUse() || !Shift.hasOneUse())
851       break;
852     
853     // Verify that the shift amount is something we can fold.
854     unsigned ShiftCst = C1->getValue();
855     if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
856       break;
857     
858     // Get the new AND mask, this folds to a constant.
859     SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
860                                            SDOperand(C2, 0), SDOperand(C1, 0));
861     SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
862                                        Shift.getOperand(0), NewANDMask);
863     NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
864     NewAND.Val->setNodeId(N.Val->getNodeId());
865     
866     AM.Scale = 1 << ShiftCst;
867     AM.IndexReg = NewAND;
868     return false;
869   }
870   }
871
872   return MatchAddressBase(N, AM, isRoot, Depth);
873 }
874
875 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
876 /// specified addressing mode without any further recursion.
877 bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
878                                        bool isRoot, unsigned Depth) {
879   // Is the base register already occupied?
880   if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
881     // If so, check to see if the scale index register is set.
882     if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
883       AM.IndexReg = N;
884       AM.Scale = 1;
885       return false;
886     }
887
888     // Otherwise, we cannot select it.
889     return true;
890   }
891
892   // Default, generate it as a register.
893   AM.BaseType = X86ISelAddressMode::RegBase;
894   AM.Base.Reg = N;
895   return false;
896 }
897
898 /// SelectAddr - returns true if it is able pattern match an addressing mode.
899 /// It returns the operands which make up the maximal addressing mode it can
900 /// match by reference.
901 bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
902                                  SDOperand &Scale, SDOperand &Index,
903                                  SDOperand &Disp) {
904   X86ISelAddressMode AM;
905   if (MatchAddress(N, AM))
906     return false;
907
908   MVT::ValueType VT = N.getValueType();
909   if (AM.BaseType == X86ISelAddressMode::RegBase) {
910     if (!AM.Base.Reg.Val)
911       AM.Base.Reg = CurDAG->getRegister(0, VT);
912   }
913
914   if (!AM.IndexReg.Val)
915     AM.IndexReg = CurDAG->getRegister(0, VT);
916
917   getAddressOperands(AM, Base, Scale, Index, Disp);
918   return true;
919 }
920
921 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
922 /// constant +0.0.
923 static inline bool isZeroNode(SDOperand Elt) {
924   return ((isa<ConstantSDNode>(Elt) &&
925   cast<ConstantSDNode>(Elt)->getValue() == 0) ||
926   (isa<ConstantFPSDNode>(Elt) &&
927   cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
928 }
929
930
931 /// SelectScalarSSELoad - Match a scalar SSE load.  In particular, we want to
932 /// match a load whose top elements are either undef or zeros.  The load flavor
933 /// is derived from the type of N, which is either v4f32 or v2f64.
934 bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
935                                           SDOperand N, SDOperand &Base,
936                                           SDOperand &Scale, SDOperand &Index,
937                                           SDOperand &Disp, SDOperand &InChain,
938                                           SDOperand &OutChain) {
939   if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
940     InChain = N.getOperand(0).getValue(1);
941     if (ISD::isNON_EXTLoad(InChain.Val) &&
942         InChain.getValue(0).hasOneUse() &&
943         N.hasOneUse() &&
944         CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
945       LoadSDNode *LD = cast<LoadSDNode>(InChain);
946       if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
947         return false;
948       OutChain = LD->getChain();
949       return true;
950     }
951   }
952
953   // Also handle the case where we explicitly require zeros in the top
954   // elements.  This is a vector shuffle from the zero vector.
955   if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
956       // Check to see if the top elements are all zeros (or bitcast of zeros).
957       ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
958       N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR && 
959       N.getOperand(1).Val->hasOneUse() &&
960       ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
961       N.getOperand(1).getOperand(0).hasOneUse()) {
962     // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
963     // from the LHS.
964     unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
965     SDOperand ShufMask = N.getOperand(2);
966     assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
967     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
968       if (C->getValue() == VecWidth) {
969         for (unsigned i = 1; i != VecWidth; ++i) {
970           if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
971             // ok.
972           } else {
973             ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
974             if (C->getValue() >= VecWidth) return false;
975           }
976         }
977       }
978       
979       // Okay, this is a zero extending load.  Fold it.
980       LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
981       if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
982         return false;
983       OutChain = LD->getChain();
984       InChain = SDOperand(LD, 1);
985       return true;
986     }
987   }
988   return false;
989 }
990
991
992 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
993 /// mode it matches can be cost effectively emitted as an LEA instruction.
994 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
995                                     SDOperand &Base, SDOperand &Scale,
996                                     SDOperand &Index, SDOperand &Disp) {
997   X86ISelAddressMode AM;
998   if (MatchAddress(N, AM))
999     return false;
1000
1001   MVT::ValueType VT = N.getValueType();
1002   unsigned Complexity = 0;
1003   if (AM.BaseType == X86ISelAddressMode::RegBase)
1004     if (AM.Base.Reg.Val)
1005       Complexity = 1;
1006     else
1007       AM.Base.Reg = CurDAG->getRegister(0, VT);
1008   else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1009     Complexity = 4;
1010
1011   if (AM.IndexReg.Val)
1012     Complexity++;
1013   else
1014     AM.IndexReg = CurDAG->getRegister(0, VT);
1015
1016   // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1017   // a simple shift.
1018   if (AM.Scale > 1)
1019     Complexity++;
1020
1021   // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1022   // to a LEA. This is determined with some expermentation but is by no means
1023   // optimal (especially for code size consideration). LEA is nice because of
1024   // its three-address nature. Tweak the cost function again when we can run
1025   // convertToThreeAddress() at register allocation time.
1026   if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1027     // For X86-64, we should always use lea to materialize RIP relative
1028     // addresses.
1029     if (Subtarget->is64Bit())
1030       Complexity = 4;
1031     else
1032       Complexity += 2;
1033   }
1034
1035   if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1036     Complexity++;
1037
1038   if (Complexity > 2) {
1039     getAddressOperands(AM, Base, Scale, Index, Disp);
1040     return true;
1041   }
1042   return false;
1043 }
1044
1045 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1046                                   SDOperand &Base, SDOperand &Scale,
1047                                   SDOperand &Index, SDOperand &Disp) {
1048   if (ISD::isNON_EXTLoad(N.Val) &&
1049       N.hasOneUse() &&
1050       CanBeFoldedBy(N.Val, P.Val, P.Val))
1051     return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1052   return false;
1053 }
1054
1055 /// getGlobalBaseReg - Output the instructions required to put the
1056 /// base address to use for accessing globals into a register.
1057 ///
1058 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
1059   assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
1060   if (!GlobalBaseReg) {
1061     // Insert the set of GlobalBaseReg into the first MBB of the function
1062     MachineFunction *MF = BB->getParent();
1063     MachineBasicBlock &FirstMBB = MF->front();
1064     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
1065     MachineRegisterInfo &RegInfo = MF->getRegInfo();
1066     unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
1067     
1068     const TargetInstrInfo *TII = TM.getInstrInfo();
1069     // Operand of MovePCtoStack is completely ignored by asm printer. It's
1070     // only used in JIT code emission as displacement to pc.
1071     BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
1072     
1073     // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1074     // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
1075     if (TM.getRelocationModel() == Reloc::PIC_ &&
1076         Subtarget->isPICStyleGOT()) {
1077       GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
1078       BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1079         .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
1080     } else {
1081       GlobalBaseReg = PC;
1082     }
1083     
1084   }
1085   return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
1086 }
1087
1088 static SDNode *FindCallStartFromCall(SDNode *Node) {
1089   if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1090     assert(Node->getOperand(0).getValueType() == MVT::Other &&
1091          "Node doesn't have a token chain argument!");
1092   return FindCallStartFromCall(Node->getOperand(0).Val);
1093 }
1094
1095 SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1096     SDOperand SRIdx;
1097     switch (VT) {
1098     case MVT::i8:
1099       SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1100       // Ensure that the source register has an 8-bit subreg on 32-bit targets
1101       if (!Subtarget->is64Bit()) { 
1102         unsigned Opc;
1103         MVT::ValueType VT;
1104         switch (N0.getValueType()) {
1105         default: assert(0 && "Unknown truncate!");
1106         case MVT::i16:
1107           Opc = X86::MOV16to16_;
1108           VT = MVT::i16;
1109           break;
1110         case MVT::i32:
1111           Opc = X86::MOV32to32_;
1112           VT = MVT::i32;
1113           break;
1114         }
1115         N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1116         return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1117                                      VT, N0, SRIdx, N0.getValue(1));
1118       }
1119       break;
1120     case MVT::i16:
1121       SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1122       break;
1123     case MVT::i32:
1124       SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1125       break;
1126     default: assert(0 && "Unknown truncate!"); break;
1127     }
1128     return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
1129 }
1130
1131
1132 SDNode *X86DAGToDAGISel::Select(SDOperand N) {
1133   SDNode *Node = N.Val;
1134   MVT::ValueType NVT = Node->getValueType(0);
1135   unsigned Opc, MOpc;
1136   unsigned Opcode = Node->getOpcode();
1137
1138 #ifndef NDEBUG
1139   DOUT << std::string(Indent, ' ') << "Selecting: ";
1140   DEBUG(Node->dump(CurDAG));
1141   DOUT << "\n";
1142   Indent += 2;
1143 #endif
1144
1145   if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
1146 #ifndef NDEBUG
1147     DOUT << std::string(Indent-2, ' ') << "== ";
1148     DEBUG(Node->dump(CurDAG));
1149     DOUT << "\n";
1150     Indent -= 2;
1151 #endif
1152     return NULL;   // Already selected.
1153   }
1154
1155   switch (Opcode) {
1156     default: break;
1157     case X86ISD::GlobalBaseReg: 
1158       return getGlobalBaseReg();
1159
1160     case X86ISD::FP_GET_RESULT2: {
1161       SDOperand Chain = N.getOperand(0);
1162       SDOperand InFlag = N.getOperand(1);
1163       AddToISelQueue(Chain);
1164       AddToISelQueue(InFlag);
1165       std::vector<MVT::ValueType> Tys;
1166       Tys.push_back(MVT::f80);
1167       Tys.push_back(MVT::f80);
1168       Tys.push_back(MVT::Other);
1169       Tys.push_back(MVT::Flag);
1170       SDOperand Ops[] = { Chain, InFlag };
1171       SDNode *ResNode = CurDAG->getTargetNode(X86::FpGETRESULT80x2, Tys,
1172                                               Ops, 2);
1173       Chain = SDOperand(ResNode, 2);
1174       InFlag = SDOperand(ResNode, 3);
1175       ReplaceUses(SDOperand(N.Val, 2), Chain);
1176       ReplaceUses(SDOperand(N.Val, 3), InFlag);
1177       return ResNode;
1178     }
1179
1180     case ISD::ADD: {
1181       // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1182       // code and is matched first so to prevent it from being turned into
1183       // LEA32r X+c.
1184       // In 64-bit small code size mode, use LEA to take advantage of
1185       // RIP-relative addressing.
1186       if (TM.getCodeModel() != CodeModel::Small)
1187         break;
1188       MVT::ValueType PtrVT = TLI.getPointerTy();
1189       SDOperand N0 = N.getOperand(0);
1190       SDOperand N1 = N.getOperand(1);
1191       if (N.Val->getValueType(0) == PtrVT &&
1192           N0.getOpcode() == X86ISD::Wrapper &&
1193           N1.getOpcode() == ISD::Constant) {
1194         unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1195         SDOperand C(0, 0);
1196         // TODO: handle ExternalSymbolSDNode.
1197         if (GlobalAddressSDNode *G =
1198             dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1199           C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1200                                              G->getOffset() + Offset);
1201         } else if (ConstantPoolSDNode *CP =
1202                    dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1203           C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1204                                             CP->getAlignment(),
1205                                             CP->getOffset()+Offset);
1206         }
1207
1208         if (C.Val) {
1209           if (Subtarget->is64Bit()) {
1210             SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1211                                 CurDAG->getRegister(0, PtrVT), C };
1212             return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1213           } else
1214             return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1215         }
1216       }
1217
1218       // Other cases are handled by auto-generated code.
1219       break;
1220     }
1221
1222     case ISD::SMUL_LOHI:
1223     case ISD::UMUL_LOHI: {
1224       SDOperand N0 = Node->getOperand(0);
1225       SDOperand N1 = Node->getOperand(1);
1226
1227       bool isSigned = Opcode == ISD::SMUL_LOHI;
1228       if (!isSigned)
1229         switch (NVT) {
1230         default: assert(0 && "Unsupported VT!");
1231         case MVT::i8:  Opc = X86::MUL8r;  MOpc = X86::MUL8m;  break;
1232         case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1233         case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1234         case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1235         }
1236       else
1237         switch (NVT) {
1238         default: assert(0 && "Unsupported VT!");
1239         case MVT::i8:  Opc = X86::IMUL8r;  MOpc = X86::IMUL8m;  break;
1240         case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1241         case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1242         case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1243         }
1244
1245       unsigned LoReg, HiReg;
1246       switch (NVT) {
1247       default: assert(0 && "Unsupported VT!");
1248       case MVT::i8:  LoReg = X86::AL;  HiReg = X86::AH;  break;
1249       case MVT::i16: LoReg = X86::AX;  HiReg = X86::DX;  break;
1250       case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1251       case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1252       }
1253
1254       SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1255       bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1256       // multiplty is commmutative
1257       if (!foldedLoad) {
1258         foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1259         if (foldedLoad)
1260           std::swap(N0, N1);
1261       }
1262
1263       AddToISelQueue(N0);
1264       SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1265                                               N0, SDOperand()).getValue(1);
1266
1267       if (foldedLoad) {
1268         AddToISelQueue(N1.getOperand(0));
1269         AddToISelQueue(Tmp0);
1270         AddToISelQueue(Tmp1);
1271         AddToISelQueue(Tmp2);
1272         AddToISelQueue(Tmp3);
1273         SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1274         SDNode *CNode =
1275           CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1276         InFlag = SDOperand(CNode, 1);
1277         // Update the chain.
1278         ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
1279       } else {
1280         AddToISelQueue(N1);
1281         InFlag =
1282           SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1283       }
1284
1285       // Copy the low half of the result, if it is needed.
1286       if (!N.getValue(0).use_empty()) {
1287         SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1288                                                   LoReg, NVT, InFlag);
1289         InFlag = Result.getValue(2);
1290         ReplaceUses(N.getValue(0), Result);
1291 #ifndef NDEBUG
1292         DOUT << std::string(Indent-2, ' ') << "=> ";
1293         DEBUG(Result.Val->dump(CurDAG));
1294         DOUT << "\n";
1295 #endif
1296       }
1297       // Copy the high half of the result, if it is needed.
1298       if (!N.getValue(1).use_empty()) {
1299         SDOperand Result;
1300         if (HiReg == X86::AH && Subtarget->is64Bit()) {
1301           // Prevent use of AH in a REX instruction by referencing AX instead.
1302           // Shift it down 8 bits.
1303           Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1304                                           X86::AX, MVT::i16, InFlag);
1305           InFlag = Result.getValue(2);
1306           Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1307                                        CurDAG->getTargetConstant(8, MVT::i8)), 0);
1308           // Then truncate it down to i8.
1309           SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1310           Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1311                                                    MVT::i8, Result, SRIdx), 0);
1312         } else {
1313           Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1314                                           HiReg, NVT, InFlag);
1315           InFlag = Result.getValue(2);
1316         }
1317         ReplaceUses(N.getValue(1), Result);
1318 #ifndef NDEBUG
1319         DOUT << std::string(Indent-2, ' ') << "=> ";
1320         DEBUG(Result.Val->dump(CurDAG));
1321         DOUT << "\n";
1322 #endif
1323       }
1324
1325 #ifndef NDEBUG
1326       Indent -= 2;
1327 #endif
1328
1329       return NULL;
1330     }
1331       
1332     case ISD::SDIVREM:
1333     case ISD::UDIVREM: {
1334       SDOperand N0 = Node->getOperand(0);
1335       SDOperand N1 = Node->getOperand(1);
1336
1337       bool isSigned = Opcode == ISD::SDIVREM;
1338       if (!isSigned)
1339         switch (NVT) {
1340         default: assert(0 && "Unsupported VT!");
1341         case MVT::i8:  Opc = X86::DIV8r;  MOpc = X86::DIV8m;  break;
1342         case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1343         case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1344         case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1345         }
1346       else
1347         switch (NVT) {
1348         default: assert(0 && "Unsupported VT!");
1349         case MVT::i8:  Opc = X86::IDIV8r;  MOpc = X86::IDIV8m;  break;
1350         case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1351         case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1352         case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1353         }
1354
1355       unsigned LoReg, HiReg;
1356       unsigned ClrOpcode, SExtOpcode;
1357       switch (NVT) {
1358       default: assert(0 && "Unsupported VT!");
1359       case MVT::i8:
1360         LoReg = X86::AL;  HiReg = X86::AH;
1361         ClrOpcode  = 0;
1362         SExtOpcode = X86::CBW;
1363         break;
1364       case MVT::i16:
1365         LoReg = X86::AX;  HiReg = X86::DX;
1366         ClrOpcode  = X86::MOV16r0;
1367         SExtOpcode = X86::CWD;
1368         break;
1369       case MVT::i32:
1370         LoReg = X86::EAX; HiReg = X86::EDX;
1371         ClrOpcode  = X86::MOV32r0;
1372         SExtOpcode = X86::CDQ;
1373         break;
1374       case MVT::i64:
1375         LoReg = X86::RAX; HiReg = X86::RDX;
1376         ClrOpcode  = X86::MOV64r0;
1377         SExtOpcode = X86::CQO;
1378         break;
1379       }
1380
1381       SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1382       bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1383
1384       SDOperand InFlag;
1385       if (NVT == MVT::i8 && !isSigned) {
1386         // Special case for div8, just use a move with zero extension to AX to
1387         // clear the upper 8 bits (AH).
1388         SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1389         if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1390           SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1391           AddToISelQueue(N0.getOperand(0));
1392           AddToISelQueue(Tmp0);
1393           AddToISelQueue(Tmp1);
1394           AddToISelQueue(Tmp2);
1395           AddToISelQueue(Tmp3);
1396           Move =
1397             SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1398                                             Ops, 5), 0);
1399           Chain = Move.getValue(1);
1400           ReplaceUses(N0.getValue(1), Chain);
1401         } else {
1402           AddToISelQueue(N0);
1403           Move =
1404             SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1405           Chain = CurDAG->getEntryNode();
1406         }
1407         Chain  = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
1408         InFlag = Chain.getValue(1);
1409       } else {
1410         AddToISelQueue(N0);
1411         InFlag =
1412           CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1413                                LoReg, N0, SDOperand()).getValue(1);
1414         if (isSigned) {
1415           // Sign extend the low part into the high part.
1416           InFlag =
1417             SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1418         } else {
1419           // Zero out the high part, effectively zero extending the input.
1420           SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1421           InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1422                                         ClrNode, InFlag).getValue(1);
1423         }
1424       }
1425
1426       if (foldedLoad) {
1427         AddToISelQueue(N1.getOperand(0));
1428         AddToISelQueue(Tmp0);
1429         AddToISelQueue(Tmp1);
1430         AddToISelQueue(Tmp2);
1431         AddToISelQueue(Tmp3);
1432         SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1433         SDNode *CNode =
1434           CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1435         InFlag = SDOperand(CNode, 1);
1436         // Update the chain.
1437         ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
1438       } else {
1439         AddToISelQueue(N1);
1440         InFlag =
1441           SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1442       }
1443
1444       // Copy the division (low) result, if it is needed.
1445       if (!N.getValue(0).use_empty()) {
1446         SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1447                                                   LoReg, NVT, InFlag);
1448         InFlag = Result.getValue(2);
1449         ReplaceUses(N.getValue(0), Result);
1450 #ifndef NDEBUG
1451         DOUT << std::string(Indent-2, ' ') << "=> ";
1452         DEBUG(Result.Val->dump(CurDAG));
1453         DOUT << "\n";
1454 #endif
1455       }
1456       // Copy the remainder (high) result, if it is needed.
1457       if (!N.getValue(1).use_empty()) {
1458         SDOperand Result;
1459         if (HiReg == X86::AH && Subtarget->is64Bit()) {
1460           // Prevent use of AH in a REX instruction by referencing AX instead.
1461           // Shift it down 8 bits.
1462           Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1463                                           X86::AX, MVT::i16, InFlag);
1464           InFlag = Result.getValue(2);
1465           Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1466                                        CurDAG->getTargetConstant(8, MVT::i8)), 0);
1467           // Then truncate it down to i8.
1468           SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1469           Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1470                                                    MVT::i8, Result, SRIdx), 0);
1471         } else {
1472           Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1473                                           HiReg, NVT, InFlag);
1474           InFlag = Result.getValue(2);
1475         }
1476         ReplaceUses(N.getValue(1), Result);
1477 #ifndef NDEBUG
1478         DOUT << std::string(Indent-2, ' ') << "=> ";
1479         DEBUG(Result.Val->dump(CurDAG));
1480         DOUT << "\n";
1481 #endif
1482       }
1483
1484 #ifndef NDEBUG
1485       Indent -= 2;
1486 #endif
1487
1488       return NULL;
1489     }
1490
1491     case ISD::ANY_EXTEND: {
1492       SDOperand N0 = Node->getOperand(0);
1493       AddToISelQueue(N0);
1494       if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1495         SDOperand SRIdx;
1496         switch(N0.getValueType()) {
1497         case MVT::i32:
1498           SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1499           break;
1500         case MVT::i16:
1501           SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1502           break;
1503         case MVT::i8:
1504           if (Subtarget->is64Bit())
1505             SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1506           break;
1507         default: assert(0 && "Unknown any_extend!");
1508         }
1509         if (SRIdx.Val) {
1510           SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1511                                                   NVT, N0, SRIdx);
1512
1513 #ifndef NDEBUG
1514           DOUT << std::string(Indent-2, ' ') << "=> ";
1515           DEBUG(ResNode->dump(CurDAG));
1516           DOUT << "\n";
1517           Indent -= 2;
1518 #endif
1519           return ResNode;
1520         } // Otherwise let generated ISel handle it.
1521       }
1522       break;
1523     }
1524     
1525     case ISD::SIGN_EXTEND_INREG: {
1526       SDOperand N0 = Node->getOperand(0);
1527       AddToISelQueue(N0);
1528       
1529       MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1530       SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
1531       unsigned Opc = 0;
1532       switch (NVT) {
1533       case MVT::i16:
1534         if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1535         else assert(0 && "Unknown sign_extend_inreg!");
1536         break;
1537       case MVT::i32:
1538         switch (SVT) {
1539         case MVT::i8:  Opc = X86::MOVSX32rr8;  break;
1540         case MVT::i16: Opc = X86::MOVSX32rr16; break;
1541         default: assert(0 && "Unknown sign_extend_inreg!");
1542         }
1543         break;
1544       case MVT::i64:
1545         switch (SVT) {
1546         case MVT::i8:  Opc = X86::MOVSX64rr8;  break;
1547         case MVT::i16: Opc = X86::MOVSX64rr16; break;
1548         case MVT::i32: Opc = X86::MOVSX64rr32; break;
1549         default: assert(0 && "Unknown sign_extend_inreg!");
1550         }
1551         break;
1552       default: assert(0 && "Unknown sign_extend_inreg!");
1553       }
1554       
1555       SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1556       
1557 #ifndef NDEBUG
1558       DOUT << std::string(Indent-2, ' ') << "=> ";
1559       DEBUG(TruncOp.Val->dump(CurDAG));
1560       DOUT << "\n";
1561       DOUT << std::string(Indent-2, ' ') << "=> ";
1562       DEBUG(ResNode->dump(CurDAG));
1563       DOUT << "\n";
1564       Indent -= 2;
1565 #endif
1566       return ResNode;
1567       break;
1568     }
1569     
1570     case ISD::TRUNCATE: {
1571       SDOperand Input = Node->getOperand(0);
1572       AddToISelQueue(Node->getOperand(0));
1573       SDNode *ResNode = getTruncate(Input, NVT);
1574       
1575 #ifndef NDEBUG
1576         DOUT << std::string(Indent-2, ' ') << "=> ";
1577         DEBUG(ResNode->dump(CurDAG));
1578         DOUT << "\n";
1579         Indent -= 2;
1580 #endif
1581       return ResNode;
1582       break;
1583     }
1584   }
1585
1586   SDNode *ResNode = SelectCode(N);
1587
1588 #ifndef NDEBUG
1589   DOUT << std::string(Indent-2, ' ') << "=> ";
1590   if (ResNode == NULL || ResNode == N.Val)
1591     DEBUG(N.Val->dump(CurDAG));
1592   else
1593     DEBUG(ResNode->dump(CurDAG));
1594   DOUT << "\n";
1595   Indent -= 2;
1596 #endif
1597
1598   return ResNode;
1599 }
1600
1601 bool X86DAGToDAGISel::
1602 SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1603                              std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1604   SDOperand Op0, Op1, Op2, Op3;
1605   switch (ConstraintCode) {
1606   case 'o':   // offsetable        ??
1607   case 'v':   // not offsetable    ??
1608   default: return true;
1609   case 'm':   // memory
1610     if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1611       return true;
1612     break;
1613   }
1614   
1615   OutOps.push_back(Op0);
1616   OutOps.push_back(Op1);
1617   OutOps.push_back(Op2);
1618   OutOps.push_back(Op3);
1619   AddToISelQueue(Op0);
1620   AddToISelQueue(Op1);
1621   AddToISelQueue(Op2);
1622   AddToISelQueue(Op3);
1623   return false;
1624 }
1625
1626 /// createX86ISelDag - This pass converts a legalized DAG into a 
1627 /// X86-specific DAG, ready for instruction scheduling.
1628 ///
1629 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1630   return new X86DAGToDAGISel(TM, Fast);
1631 }