deb197cadd5249b52506e08eb91c538ac259c769
[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 was developed by the Evan Cheng and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a DAG pattern matching instruction selector for X86,
11 // converting from a legalized dag to a X86 dag.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "x86-isel"
16 #include "X86.h"
17 #include "X86InstrBuilder.h"
18 #include "X86ISelLowering.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/GlobalValue.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Type.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/SSARegMap.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/ADT/Statistic.h"
38 #include <queue>
39 #include <set>
40 using namespace llvm;
41
42 STATISTIC(NumFPKill   , "Number of FP_REG_KILL instructions added");
43 STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
44
45
46 //===----------------------------------------------------------------------===//
47 //                      Pattern Matcher Implementation
48 //===----------------------------------------------------------------------===//
49
50 namespace {
51   /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52   /// SDOperand's instead of register numbers for the leaves of the matched
53   /// tree.
54   struct X86ISelAddressMode {
55     enum {
56       RegBase,
57       FrameIndexBase
58     } BaseType;
59
60     struct {            // This is really a union, discriminated by BaseType!
61       SDOperand Reg;
62       int FrameIndex;
63     } Base;
64
65     bool isRIPRel;     // RIP relative?
66     unsigned Scale;
67     SDOperand IndexReg; 
68     unsigned Disp;
69     GlobalValue *GV;
70     Constant *CP;
71     const char *ES;
72     int JT;
73     unsigned Align;    // CP alignment.
74
75     X86ISelAddressMode()
76       : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77         GV(0), CP(0), ES(0), JT(-1), Align(0) {
78     }
79   };
80 }
81
82 namespace {
83   //===--------------------------------------------------------------------===//
84   /// ISel - X86 specific code to select X86 machine instructions for
85   /// SelectionDAG operations.
86   ///
87   class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
88     /// ContainsFPCode - Every instruction we select that uses or defines a FP
89     /// register should set this to true.
90     bool ContainsFPCode;
91
92     /// FastISel - Enable fast(er) instruction selection.
93     ///
94     bool FastISel;
95
96     /// TM - Keep a reference to X86TargetMachine.
97     ///
98     X86TargetMachine &TM;
99
100     /// X86Lowering - This object fully describes how to lower LLVM code to an
101     /// X86-specific SelectionDAG.
102     X86TargetLowering X86Lowering;
103
104     /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
105     /// make the right decision when generating code for different targets.
106     const X86Subtarget *Subtarget;
107
108     /// GlobalBaseReg - keeps track of the virtual register mapped onto global
109     /// base register.
110     unsigned GlobalBaseReg;
111
112   public:
113     X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
114       : SelectionDAGISel(X86Lowering),
115         ContainsFPCode(false), FastISel(fast), TM(tm),
116         X86Lowering(*TM.getTargetLowering()),
117         Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
118
119     virtual bool runOnFunction(Function &Fn) {
120       // Make sure we re-emit a set of the global base reg if necessary
121       GlobalBaseReg = 0;
122       return SelectionDAGISel::runOnFunction(Fn);
123     }
124    
125     virtual const char *getPassName() const {
126       return "X86 DAG->DAG Instruction Selection";
127     }
128
129     /// InstructionSelectBasicBlock - This callback is invoked by
130     /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
131     virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
132
133     virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
134
135     virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root);
136
137 // Include the pieces autogenerated from the target description.
138 #include "X86GenDAGISel.inc"
139
140   private:
141     SDNode *Select(SDOperand N);
142
143     bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
144                       bool isRoot = true, unsigned Depth = 0);
145     bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
146                     SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
147     bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
148                        SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
149     bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
150                              SDOperand N, SDOperand &Base, SDOperand &Scale,
151                              SDOperand &Index, SDOperand &Disp,
152                              SDOperand &InChain, SDOperand &OutChain);
153     bool TryFoldLoad(SDOperand P, SDOperand N,
154                      SDOperand &Base, SDOperand &Scale,
155                      SDOperand &Index, SDOperand &Disp);
156     void InstructionSelectPreprocess(SelectionDAG &DAG);
157
158     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
159     /// inline asm expressions.
160     virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
161                                               char ConstraintCode,
162                                               std::vector<SDOperand> &OutOps,
163                                               SelectionDAG &DAG);
164     
165     void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
166
167     inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, 
168                                    SDOperand &Scale, SDOperand &Index,
169                                    SDOperand &Disp) {
170       Base  = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
171         CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
172         AM.Base.Reg;
173       Scale = getI8Imm(AM.Scale);
174       Index = AM.IndexReg;
175       // These are 32-bit even in 64-bit mode since RIP relative offset
176       // is 32-bit.
177       if (AM.GV)
178         Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
179       else if (AM.CP)
180         Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
181       else if (AM.ES)
182         Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
183       else if (AM.JT != -1)
184         Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
185       else
186         Disp = getI32Imm(AM.Disp);
187     }
188
189     /// getI8Imm - Return a target constant with the specified value, of type
190     /// i8.
191     inline SDOperand getI8Imm(unsigned Imm) {
192       return CurDAG->getTargetConstant(Imm, MVT::i8);
193     }
194
195     /// getI16Imm - Return a target constant with the specified value, of type
196     /// i16.
197     inline SDOperand getI16Imm(unsigned Imm) {
198       return CurDAG->getTargetConstant(Imm, MVT::i16);
199     }
200
201     /// getI32Imm - Return a target constant with the specified value, of type
202     /// i32.
203     inline SDOperand getI32Imm(unsigned Imm) {
204       return CurDAG->getTargetConstant(Imm, MVT::i32);
205     }
206
207     /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
208     /// base register.  Return the virtual register that holds this value.
209     SDNode *getGlobalBaseReg();
210
211 #ifndef NDEBUG
212     unsigned Indent;
213 #endif
214   };
215 }
216
217 static SDNode *findFlagUse(SDNode *N) {
218   unsigned FlagResNo = N->getNumValues()-1;
219   for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
220     SDNode *User = *I;
221     for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
222       SDOperand Op = User->getOperand(i);
223       if (Op.Val == N && Op.ResNo == FlagResNo)
224         return User;
225     }
226   }
227   return NULL;
228 }
229
230 static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
231                           SDNode *Root, SDNode *Skip, bool &found,
232                           std::set<SDNode *> &Visited) {
233   if (found ||
234       Use->getNodeId() > Def->getNodeId() ||
235       !Visited.insert(Use).second)
236     return;
237
238   for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
239     SDNode *N = Use->getOperand(i).Val;
240     if (N == Skip)
241       continue;
242     if (N == Def) {
243       if (Use == ImmedUse)
244         continue; // Immediate use is ok.
245       if (Use == Root) {
246         assert(Use->getOpcode() == ISD::STORE ||
247                Use->getOpcode() == X86ISD::CMP);
248         continue;
249       }
250       found = true;
251       break;
252     }
253     findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
254   }
255 }
256
257 /// isNonImmUse - Start searching from Root up the DAG to check is Def can
258 /// be reached. Return true if that's the case. However, ignore direct uses
259 /// by ImmedUse (which would be U in the example illustrated in
260 /// CanBeFoldedBy) and by Root (which can happen in the store case).
261 /// FIXME: to be really generic, we should allow direct use by any node
262 /// that is being folded. But realisticly since we only fold loads which
263 /// have one non-chain use, we only need to watch out for load/op/store
264 /// and load/op/cmp case where the root (store / cmp) may reach the load via
265 /// its chain operand.
266 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
267                                SDNode *Skip = NULL) {
268   std::set<SDNode *> Visited;
269   bool found = false;
270   findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
271   return found;
272 }
273
274
275 bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) {
276   if (FastISel) return false;
277
278   // If U use can somehow reach N through another path then U can't fold N or
279   // it will create a cycle. e.g. In the following diagram, U can reach N
280   // through X. If N is folded into into U, then X is both a predecessor and
281   // a successor of U.
282   //
283   //         [ N ]
284   //         ^  ^
285   //         |  |
286   //        /   \---
287   //      /        [X]
288   //      |         ^
289   //     [U]--------|
290
291   if (isNonImmUse(Root, N, U))
292     return false;
293
294   // If U produces a flag, then it gets (even more) interesting. Since it
295   // would have been "glued" together with its flag use, we need to check if
296   // it might reach N:
297   //
298   //       [ N ]
299   //        ^ ^
300   //        | |
301   //       [U] \--
302   //        ^   [TF]
303   //        |    ^
304   //        |    |
305   //         \  /
306   //          [FU]
307   //
308   // If FU (flag use) indirectly reach N (the load), and U fold N (call it
309   // NU), then TF is a predecessor of FU and a successor of NU. But since
310   // NU and FU are flagged together, this effectively creates a cycle.
311   bool HasFlagUse = false;
312   MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
313   while ((VT == MVT::Flag && !Root->use_empty())) {
314     SDNode *FU = findFlagUse(Root);
315     if (FU == NULL)
316       break;
317     else {
318       Root = FU;
319       HasFlagUse = true;
320     }
321     VT = Root->getValueType(Root->getNumValues()-1);
322   }
323
324   if (HasFlagUse)
325     return !isNonImmUse(Root, N, Root, U);
326   return true;
327 }
328
329 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
330 /// and move load below the TokenFactor. Replace store's chain operand with
331 /// load's chain result.
332 static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
333                                  SDOperand Store, SDOperand TF) {
334   std::vector<SDOperand> Ops;
335   for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
336     if (Load.Val == TF.Val->getOperand(i).Val)
337       Ops.push_back(Load.Val->getOperand(0));
338     else
339       Ops.push_back(TF.Val->getOperand(i));
340   DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
341   DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
342   DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
343                          Store.getOperand(2), Store.getOperand(3));
344 }
345
346 /// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
347 /// selector to pick more load-modify-store instructions. This is a common
348 /// case:
349 ///
350 ///     [Load chain]
351 ///         ^
352 ///         |
353 ///       [Load]
354 ///       ^    ^
355 ///       |    |
356 ///      /      \-
357 ///     /         |
358 /// [TokenFactor] [Op]
359 ///     ^          ^
360 ///     |          |
361 ///      \        /
362 ///       \      /
363 ///       [Store]
364 ///
365 /// The fact the store's chain operand != load's chain will prevent the
366 /// (store (op (load))) instruction from being selected. We can transform it to:
367 ///
368 ///     [Load chain]
369 ///         ^
370 ///         |
371 ///    [TokenFactor]
372 ///         ^
373 ///         |
374 ///       [Load]
375 ///       ^    ^
376 ///       |    |
377 ///       |     \- 
378 ///       |       | 
379 ///       |     [Op]
380 ///       |       ^
381 ///       |       |
382 ///       \      /
383 ///        \    /
384 ///       [Store]
385 void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
386   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
387          E = DAG.allnodes_end(); I != E; ++I) {
388     if (!ISD::isNON_TRUNCStore(I))
389       continue;
390     SDOperand Chain = I->getOperand(0);
391     if (Chain.Val->getOpcode() != ISD::TokenFactor)
392       continue;
393
394     SDOperand N1 = I->getOperand(1);
395     SDOperand N2 = I->getOperand(2);
396     if (MVT::isFloatingPoint(N1.getValueType()) ||
397         MVT::isVector(N1.getValueType()) ||
398         !N1.hasOneUse())
399       continue;
400
401     bool RModW = false;
402     SDOperand Load;
403     unsigned Opcode = N1.Val->getOpcode();
404     switch (Opcode) {
405       case ISD::ADD:
406       case ISD::MUL:
407       case ISD::AND:
408       case ISD::OR:
409       case ISD::XOR:
410       case ISD::ADDC:
411       case ISD::ADDE: {
412         SDOperand N10 = N1.getOperand(0);
413         SDOperand N11 = N1.getOperand(1);
414         if (ISD::isNON_EXTLoad(N10.Val))
415           RModW = true;
416         else if (ISD::isNON_EXTLoad(N11.Val)) {
417           RModW = true;
418           std::swap(N10, N11);
419         }
420         RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
421           (N10.getOperand(1) == N2) &&
422           (N10.Val->getValueType(0) == N1.getValueType());
423         if (RModW)
424           Load = N10;
425         break;
426       }
427       case ISD::SUB:
428       case ISD::SHL:
429       case ISD::SRA:
430       case ISD::SRL:
431       case ISD::ROTL:
432       case ISD::ROTR:
433       case ISD::SUBC:
434       case ISD::SUBE:
435       case X86ISD::SHLD:
436       case X86ISD::SHRD: {
437         SDOperand N10 = N1.getOperand(0);
438         if (ISD::isNON_EXTLoad(N10.Val))
439           RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
440             (N10.getOperand(1) == N2) &&
441             (N10.Val->getValueType(0) == N1.getValueType());
442         if (RModW)
443           Load = N10;
444         break;
445       }
446     }
447
448     if (RModW) {
449       MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
450       ++NumLoadMoved;
451     }
452   }
453 }
454
455 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
456 /// when it has created a SelectionDAG for us to codegen.
457 void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
458   DEBUG(BB->dump());
459   MachineFunction::iterator FirstMBB = BB;
460
461   if (!FastISel)
462     InstructionSelectPreprocess(DAG);
463
464   // Codegen the basic block.
465 #ifndef NDEBUG
466   DOUT << "===== Instruction selection begins:\n";
467   Indent = 0;
468 #endif
469   DAG.setRoot(SelectRoot(DAG.getRoot()));
470 #ifndef NDEBUG
471   DOUT << "===== Instruction selection ends:\n";
472 #endif
473
474   DAG.RemoveDeadNodes();
475
476   // Emit machine code to BB. 
477   ScheduleAndEmitDAG(DAG);
478   
479   // If we are emitting FP stack code, scan the basic block to determine if this
480   // block defines any FP values.  If so, put an FP_REG_KILL instruction before
481   // the terminator of the block.
482   if (!Subtarget->hasSSE2()) {
483     // Note that FP stack instructions *are* used in SSE code when returning
484     // values, but these are not live out of the basic block, so we don't need
485     // an FP_REG_KILL in this case either.
486     bool ContainsFPCode = false;
487     
488     // Scan all of the machine instructions in these MBBs, checking for FP
489     // stores.
490     MachineFunction::iterator MBBI = FirstMBB;
491     do {
492       for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
493            !ContainsFPCode && I != E; ++I) {
494         for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
495           if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
496               MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
497               RegMap->getRegClass(I->getOperand(0).getReg()) == 
498                 X86::RFPRegisterClass) {
499             ContainsFPCode = true;
500             break;
501           }
502         }
503       }
504     } while (!ContainsFPCode && &*(MBBI++) != BB);
505     
506     // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
507     // a copy of the input value in this block.
508     if (!ContainsFPCode) {
509       // Final check, check LLVM BB's that are successors to the LLVM BB
510       // corresponding to BB for FP PHI nodes.
511       const BasicBlock *LLVMBB = BB->getBasicBlock();
512       const PHINode *PN;
513       for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
514            !ContainsFPCode && SI != E; ++SI) {
515         for (BasicBlock::const_iterator II = SI->begin();
516              (PN = dyn_cast<PHINode>(II)); ++II) {
517           if (PN->getType()->isFloatingPoint()) {
518             ContainsFPCode = true;
519             break;
520           }
521         }
522       }
523     }
524
525     // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
526     if (ContainsFPCode) {
527       BuildMI(*BB, BB->getFirstTerminator(),
528               TM.getInstrInfo()->get(X86::FP_REG_KILL));
529       ++NumFPKill;
530     }
531   }
532 }
533
534 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
535 /// the main function.
536 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
537                                              MachineFrameInfo *MFI) {
538   const TargetInstrInfo *TII = TM.getInstrInfo();
539   if (Subtarget->isTargetCygMing())
540     BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
541
542   // Switch the FPU to 64-bit precision mode for better compatibility and speed.
543   int CWFrameIdx = MFI->CreateStackObject(2, 2);
544   addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
545
546   // Set the high part to be 64-bit precision.
547   addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)),
548                     CWFrameIdx, 1).addImm(2);
549
550   // Reload the modified control word now.
551   addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
552 }
553
554 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
555   // If this is main, emit special code for main.
556   MachineBasicBlock *BB = MF.begin();
557   if (Fn.hasExternalLinkage() && Fn.getName() == "main")
558     EmitSpecialCodeForMain(BB, MF.getFrameInfo());
559 }
560
561 /// MatchAddress - Add the specified node to the specified addressing mode,
562 /// returning true if it cannot be done.  This just pattern matches for the
563 /// addressing mode
564 bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
565                                    bool isRoot, unsigned Depth) {
566   if (Depth > 5) {
567     // Default, generate it as a register.
568     AM.BaseType = X86ISelAddressMode::RegBase;
569     AM.Base.Reg = N;
570     return false;
571   }
572   
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();
586   bool Available = isSelected(id);
587
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.
609     if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
610       bool isStatic = TM.getRelocationModel() == Reloc::Static;
611       SDOperand N0 = N.getOperand(0);
612       if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
613         GlobalValue *GV = G->getGlobal();
614         bool isAbs32 = !is64Bit || isStatic;
615         if (isAbs32 || isRoot) {
616           AM.GV = GV;
617           AM.Disp += G->getOffset();
618           AM.isRIPRel = !isAbs32;
619           return false;
620         }
621       } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
622         if (!is64Bit || isStatic || isRoot) {
623           AM.CP = CP->getConstVal();
624           AM.Align = CP->getAlignment();
625           AM.Disp += CP->getOffset();
626           AM.isRIPRel = !isStatic;
627           return false;
628         }
629       } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
630         if (isStatic || isRoot) {
631           AM.ES = S->getSymbol();
632           AM.isRIPRel = !isStatic;
633           return false;
634         }
635       } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
636         if (isStatic || isRoot) {
637           AM.JT = J->getIndex();
638           AM.isRIPRel = !isStatic;
639           return false;
640         }
641       }
642     }
643     break;
644   }
645
646   case ISD::FrameIndex:
647     if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
648       AM.BaseType = X86ISelAddressMode::FrameIndexBase;
649       AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
650       return false;
651     }
652     break;
653
654   case ISD::SHL:
655     if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
656       if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
657         unsigned Val = CN->getValue();
658         if (Val == 1 || Val == 2 || Val == 3) {
659           AM.Scale = 1 << Val;
660           SDOperand ShVal = N.Val->getOperand(0);
661
662           // Okay, we know that we have a scale by now.  However, if the scaled
663           // value is an add of something and a constant, we can fold the
664           // constant into the disp field here.
665           if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
666               isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
667             AM.IndexReg = ShVal.Val->getOperand(0);
668             ConstantSDNode *AddVal =
669               cast<ConstantSDNode>(ShVal.Val->getOperand(1));
670             uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
671             if (isInt32(Disp))
672               AM.Disp = Disp;
673             else
674               AM.IndexReg = ShVal;
675           } else {
676             AM.IndexReg = ShVal;
677           }
678           return false;
679         }
680       }
681     break;
682
683   case ISD::MUL:
684     // X*[3,5,9] -> X+X*[2,4,8]
685     if (!Available &&
686         AM.BaseType == X86ISelAddressMode::RegBase &&
687         AM.Base.Reg.Val == 0 &&
688         AM.IndexReg.Val == 0) {
689       if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
690         if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
691           AM.Scale = unsigned(CN->getValue())-1;
692
693           SDOperand MulVal = N.Val->getOperand(0);
694           SDOperand Reg;
695
696           // Okay, we know that we have a scale by now.  However, if the scaled
697           // value is an add of something and a constant, we can fold the
698           // constant into the disp field here.
699           if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
700               isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
701             Reg = MulVal.Val->getOperand(0);
702             ConstantSDNode *AddVal =
703               cast<ConstantSDNode>(MulVal.Val->getOperand(1));
704             uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
705             if (isInt32(Disp))
706               AM.Disp = Disp;
707             else
708               Reg = N.Val->getOperand(0);
709           } else {
710             Reg = N.Val->getOperand(0);
711           }
712
713           AM.IndexReg = AM.Base.Reg = Reg;
714           return false;
715         }
716     }
717     break;
718
719   case ISD::ADD:
720     if (!Available) {
721       X86ISelAddressMode Backup = AM;
722       if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
723           !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
724         return false;
725       AM = Backup;
726       if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
727           !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
728         return false;
729       AM = Backup;
730     }
731     break;
732
733   case ISD::OR:
734     // Handle "X | C" as "X + C" iff X is known to have C bits clear.
735     if (!Available) {
736       if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
737         X86ISelAddressMode Backup = AM;
738         // Start with the LHS as an addr mode.
739         if (!MatchAddress(N.getOperand(0), AM, false) &&
740             // Address could not have picked a GV address for the displacement.
741             AM.GV == NULL &&
742             // On x86-64, the resultant disp must fit in 32-bits.
743             isInt32(AM.Disp + CN->getSignExtended()) &&
744             // Check to see if the LHS & C is zero.
745             TLI.MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
746           AM.Disp += CN->getValue();
747           return false;
748         }
749         AM = Backup;
750       }
751     }
752     break;
753   }
754
755   // Is the base register already occupied?
756   if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
757     // If so, check to see if the scale index register is set.
758     if (AM.IndexReg.Val == 0) {
759       AM.IndexReg = N;
760       AM.Scale = 1;
761       return false;
762     }
763
764     // Otherwise, we cannot select it.
765     return true;
766   }
767
768   // Default, generate it as a register.
769   AM.BaseType = X86ISelAddressMode::RegBase;
770   AM.Base.Reg = N;
771   return false;
772 }
773
774 /// SelectAddr - returns true if it is able pattern match an addressing mode.
775 /// It returns the operands which make up the maximal addressing mode it can
776 /// match by reference.
777 bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
778                                  SDOperand &Scale, SDOperand &Index,
779                                  SDOperand &Disp) {
780   X86ISelAddressMode AM;
781   if (MatchAddress(N, AM))
782     return false;
783
784   MVT::ValueType VT = N.getValueType();
785   if (AM.BaseType == X86ISelAddressMode::RegBase) {
786     if (!AM.Base.Reg.Val)
787       AM.Base.Reg = CurDAG->getRegister(0, VT);
788   }
789
790   if (!AM.IndexReg.Val)
791     AM.IndexReg = CurDAG->getRegister(0, VT);
792
793   getAddressOperands(AM, Base, Scale, Index, Disp);
794   return true;
795 }
796
797 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
798 /// constant +0.0.
799 static inline bool isZeroNode(SDOperand Elt) {
800   return ((isa<ConstantSDNode>(Elt) &&
801   cast<ConstantSDNode>(Elt)->getValue() == 0) ||
802   (isa<ConstantFPSDNode>(Elt) &&
803   cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
804 }
805
806
807 /// SelectScalarSSELoad - Match a scalar SSE load.  In particular, we want to
808 /// match a load whose top elements are either undef or zeros.  The load flavor
809 /// is derived from the type of N, which is either v4f32 or v2f64.
810 bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
811                                           SDOperand N, SDOperand &Base,
812                                           SDOperand &Scale, SDOperand &Index,
813                                           SDOperand &Disp, SDOperand &InChain,
814                                           SDOperand &OutChain) {
815   if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
816     InChain = N.getOperand(0).getValue(1);
817     if (ISD::isNON_EXTLoad(InChain.Val) &&
818         InChain.getValue(0).hasOneUse() &&
819         N.hasOneUse() &&
820         CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
821       LoadSDNode *LD = cast<LoadSDNode>(InChain);
822       if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
823         return false;
824       OutChain = LD->getChain();
825       return true;
826     }
827   }
828
829   // Also handle the case where we explicitly require zeros in the top
830   // elements.  This is a vector shuffle from the zero vector.
831   if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
832       N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
833       N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR && 
834       N.getOperand(1).Val->hasOneUse() &&
835       ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
836       N.getOperand(1).getOperand(0).hasOneUse()) {
837     // Check to see if the BUILD_VECTOR is building a zero vector.
838     SDOperand BV = N.getOperand(0);
839     for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
840       if (!isZeroNode(BV.getOperand(i)) &&
841           BV.getOperand(i).getOpcode() != ISD::UNDEF)
842         return false;  // Not a zero/undef vector.
843     // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
844     // from the LHS.
845     unsigned VecWidth = BV.getNumOperands();
846     SDOperand ShufMask = N.getOperand(2);
847     assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
848     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
849       if (C->getValue() == VecWidth) {
850         for (unsigned i = 1; i != VecWidth; ++i) {
851           if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
852             // ok.
853           } else {
854             ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
855             if (C->getValue() >= VecWidth) return false;
856           }
857         }
858       }
859       
860       // Okay, this is a zero extending load.  Fold it.
861       LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
862       if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
863         return false;
864       OutChain = LD->getChain();
865       InChain = SDOperand(LD, 1);
866       return true;
867     }
868   }
869   return false;
870 }
871
872
873 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
874 /// mode it matches can be cost effectively emitted as an LEA instruction.
875 bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
876                                     SDOperand &Base, SDOperand &Scale,
877                                     SDOperand &Index, SDOperand &Disp) {
878   X86ISelAddressMode AM;
879   if (MatchAddress(N, AM))
880     return false;
881
882   MVT::ValueType VT = N.getValueType();
883   unsigned Complexity = 0;
884   if (AM.BaseType == X86ISelAddressMode::RegBase)
885     if (AM.Base.Reg.Val)
886       Complexity = 1;
887     else
888       AM.Base.Reg = CurDAG->getRegister(0, VT);
889   else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
890     Complexity = 4;
891
892   if (AM.IndexReg.Val)
893     Complexity++;
894   else
895     AM.IndexReg = CurDAG->getRegister(0, VT);
896
897   // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
898   // a simple shift.
899   if (AM.Scale > 1)
900     Complexity++;
901
902   // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
903   // to a LEA. This is determined with some expermentation but is by no means
904   // optimal (especially for code size consideration). LEA is nice because of
905   // its three-address nature. Tweak the cost function again when we can run
906   // convertToThreeAddress() at register allocation time.
907   if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
908     // For X86-64, we should always use lea to materialize RIP relative
909     // addresses.
910     if (Subtarget->is64Bit())
911       Complexity = 4;
912     else
913       Complexity += 2;
914   }
915
916   if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
917     Complexity++;
918
919   if (Complexity > 2) {
920     getAddressOperands(AM, Base, Scale, Index, Disp);
921     return true;
922   }
923   return false;
924 }
925
926 bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
927                                   SDOperand &Base, SDOperand &Scale,
928                                   SDOperand &Index, SDOperand &Disp) {
929   if (ISD::isNON_EXTLoad(N.Val) &&
930       N.hasOneUse() &&
931       CanBeFoldedBy(N.Val, P.Val, P.Val))
932     return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
933   return false;
934 }
935
936 /// getGlobalBaseReg - Output the instructions required to put the
937 /// base address to use for accessing globals into a register.
938 ///
939 SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
940   assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
941   if (!GlobalBaseReg) {
942     // Insert the set of GlobalBaseReg into the first MBB of the function
943     MachineBasicBlock &FirstMBB = BB->getParent()->front();
944     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
945     SSARegMap *RegMap = BB->getParent()->getSSARegMap();
946     unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
947     
948     const TargetInstrInfo *TII = TM.getInstrInfo();
949     BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
950     BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
951     
952     // If we're using vanilla 'GOT' PIC style, we should use relative addressing
953     // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
954     if (TM.getRelocationModel() == Reloc::PIC_ &&
955         Subtarget->isPICStyleGOT()) {
956       GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
957       BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
958         addReg(PC).
959         addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
960     } else {
961       GlobalBaseReg = PC;
962     }
963     
964   }
965   return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
966 }
967
968 static SDNode *FindCallStartFromCall(SDNode *Node) {
969   if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
970     assert(Node->getOperand(0).getValueType() == MVT::Other &&
971          "Node doesn't have a token chain argument!");
972   return FindCallStartFromCall(Node->getOperand(0).Val);
973 }
974
975 SDNode *X86DAGToDAGISel::Select(SDOperand N) {
976   SDNode *Node = N.Val;
977   MVT::ValueType NVT = Node->getValueType(0);
978   unsigned Opc, MOpc;
979   unsigned Opcode = Node->getOpcode();
980
981 #ifndef NDEBUG
982   DOUT << std::string(Indent, ' ') << "Selecting: ";
983   DEBUG(Node->dump(CurDAG));
984   DOUT << "\n";
985   Indent += 2;
986 #endif
987
988   if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
989 #ifndef NDEBUG
990     DOUT << std::string(Indent-2, ' ') << "== ";
991     DEBUG(Node->dump(CurDAG));
992     DOUT << "\n";
993     Indent -= 2;
994 #endif
995     return NULL;   // Already selected.
996   }
997
998   switch (Opcode) {
999     default: break;
1000     case X86ISD::GlobalBaseReg: 
1001       return getGlobalBaseReg();
1002
1003     case ISD::ADD: {
1004       // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1005       // code and is matched first so to prevent it from being turned into
1006       // LEA32r X+c.
1007       // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1008       MVT::ValueType PtrVT = TLI.getPointerTy();
1009       SDOperand N0 = N.getOperand(0);
1010       SDOperand N1 = N.getOperand(1);
1011       if (N.Val->getValueType(0) == PtrVT &&
1012           N0.getOpcode() == X86ISD::Wrapper &&
1013           N1.getOpcode() == ISD::Constant) {
1014         unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1015         SDOperand C(0, 0);
1016         // TODO: handle ExternalSymbolSDNode.
1017         if (GlobalAddressSDNode *G =
1018             dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1019           C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1020                                              G->getOffset() + Offset);
1021         } else if (ConstantPoolSDNode *CP =
1022                    dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1023           C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1024                                             CP->getAlignment(),
1025                                             CP->getOffset()+Offset);
1026         }
1027
1028         if (C.Val) {
1029           if (Subtarget->is64Bit()) {
1030             SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1031                                 CurDAG->getRegister(0, PtrVT), C };
1032             return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1033           } else
1034             return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1035         }
1036       }
1037
1038       // Other cases are handled by auto-generated code.
1039       break;
1040     }
1041
1042     case ISD::MULHU:
1043     case ISD::MULHS: {
1044       if (Opcode == ISD::MULHU)
1045         switch (NVT) {
1046         default: assert(0 && "Unsupported VT!");
1047         case MVT::i8:  Opc = X86::MUL8r;  MOpc = X86::MUL8m;  break;
1048         case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1049         case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1050         case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1051         }
1052       else
1053         switch (NVT) {
1054         default: assert(0 && "Unsupported VT!");
1055         case MVT::i8:  Opc = X86::IMUL8r;  MOpc = X86::IMUL8m;  break;
1056         case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1057         case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1058         case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1059         }
1060
1061       unsigned LoReg, HiReg;
1062       switch (NVT) {
1063       default: assert(0 && "Unsupported VT!");
1064       case MVT::i8:  LoReg = X86::AL;  HiReg = X86::AH;  break;
1065       case MVT::i16: LoReg = X86::AX;  HiReg = X86::DX;  break;
1066       case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1067       case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1068       }
1069
1070       SDOperand N0 = Node->getOperand(0);
1071       SDOperand N1 = Node->getOperand(1);
1072
1073       bool foldedLoad = false;
1074       SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1075       foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1076       // MULHU and MULHS are commmutative
1077       if (!foldedLoad) {
1078         foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
1079         if (foldedLoad) {
1080           N0 = Node->getOperand(1);
1081           N1 = Node->getOperand(0);
1082         }
1083       }
1084
1085       SDOperand Chain;
1086       if (foldedLoad) {
1087         Chain = N1.getOperand(0);
1088         AddToISelQueue(Chain);
1089       } else
1090         Chain = CurDAG->getEntryNode();
1091
1092       SDOperand InFlag(0, 0);
1093       AddToISelQueue(N0);
1094       Chain  = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
1095                                     N0, InFlag);
1096       InFlag = Chain.getValue(1);
1097
1098       if (foldedLoad) {
1099         AddToISelQueue(Tmp0);
1100         AddToISelQueue(Tmp1);
1101         AddToISelQueue(Tmp2);
1102         AddToISelQueue(Tmp3);
1103         SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
1104         SDNode *CNode =
1105           CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1106         Chain  = SDOperand(CNode, 0);
1107         InFlag = SDOperand(CNode, 1);
1108       } else {
1109         AddToISelQueue(N1);
1110         InFlag =
1111           SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1112       }
1113
1114       SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
1115       ReplaceUses(N.getValue(0), Result);
1116       if (foldedLoad)
1117         ReplaceUses(N1.getValue(1), Result.getValue(1));
1118
1119 #ifndef NDEBUG
1120       DOUT << std::string(Indent-2, ' ') << "=> ";
1121       DEBUG(Result.Val->dump(CurDAG));
1122       DOUT << "\n";
1123       Indent -= 2;
1124 #endif
1125       return NULL;
1126     }
1127       
1128     case ISD::SDIV:
1129     case ISD::UDIV:
1130     case ISD::SREM:
1131     case ISD::UREM: {
1132       bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1133       bool isDiv    = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1134       if (!isSigned)
1135         switch (NVT) {
1136         default: assert(0 && "Unsupported VT!");
1137         case MVT::i8:  Opc = X86::DIV8r;  MOpc = X86::DIV8m;  break;
1138         case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1139         case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1140         case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1141         }
1142       else
1143         switch (NVT) {
1144         default: assert(0 && "Unsupported VT!");
1145         case MVT::i8:  Opc = X86::IDIV8r;  MOpc = X86::IDIV8m;  break;
1146         case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1147         case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1148         case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1149         }
1150
1151       unsigned LoReg, HiReg;
1152       unsigned ClrOpcode, SExtOpcode;
1153       switch (NVT) {
1154       default: assert(0 && "Unsupported VT!");
1155       case MVT::i8:
1156         LoReg = X86::AL;  HiReg = X86::AH;
1157         ClrOpcode  = 0;
1158         SExtOpcode = X86::CBW;
1159         break;
1160       case MVT::i16:
1161         LoReg = X86::AX;  HiReg = X86::DX;
1162         ClrOpcode  = X86::MOV16r0;
1163         SExtOpcode = X86::CWD;
1164         break;
1165       case MVT::i32:
1166         LoReg = X86::EAX; HiReg = X86::EDX;
1167         ClrOpcode  = X86::MOV32r0;
1168         SExtOpcode = X86::CDQ;
1169         break;
1170       case MVT::i64:
1171         LoReg = X86::RAX; HiReg = X86::RDX;
1172         ClrOpcode  = X86::MOV64r0;
1173         SExtOpcode = X86::CQO;
1174         break;
1175       }
1176
1177       SDOperand N0 = Node->getOperand(0);
1178       SDOperand N1 = Node->getOperand(1);
1179       SDOperand InFlag(0, 0);
1180       if (NVT == MVT::i8 && !isSigned) {
1181         // Special case for div8, just use a move with zero extension to AX to
1182         // clear the upper 8 bits (AH).
1183         SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1184         if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1185           SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1186           AddToISelQueue(N0.getOperand(0));
1187           AddToISelQueue(Tmp0);
1188           AddToISelQueue(Tmp1);
1189           AddToISelQueue(Tmp2);
1190           AddToISelQueue(Tmp3);
1191           Move =
1192             SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1193                                             Ops, 5), 0);
1194           Chain = Move.getValue(1);
1195           ReplaceUses(N0.getValue(1), Chain);
1196         } else {
1197           AddToISelQueue(N0);
1198           Move =
1199             SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1200           Chain = CurDAG->getEntryNode();
1201         }
1202         Chain  = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
1203         InFlag = Chain.getValue(1);
1204       } else {
1205         AddToISelQueue(N0);
1206         InFlag =
1207           CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1208                                InFlag).getValue(1);
1209         if (isSigned) {
1210           // Sign extend the low part into the high part.
1211           InFlag =
1212             SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1213         } else {
1214           // Zero out the high part, effectively zero extending the input.
1215           SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1216           InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1217                                         InFlag).getValue(1);
1218         }
1219       }
1220
1221       SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1222       bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1223       if (foldedLoad) {
1224         AddToISelQueue(N1.getOperand(0));
1225         AddToISelQueue(Tmp0);
1226         AddToISelQueue(Tmp1);
1227         AddToISelQueue(Tmp2);
1228         AddToISelQueue(Tmp3);
1229         SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
1230         SDNode *CNode =
1231           CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
1232         Chain  = SDOperand(CNode, 0);
1233         InFlag = SDOperand(CNode, 1);
1234       } else {
1235         AddToISelQueue(N1);
1236         Chain = CurDAG->getEntryNode();
1237         InFlag =
1238           SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
1239       }
1240
1241       SDOperand Result =
1242         CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag);
1243       ReplaceUses(N.getValue(0), Result);
1244       if (foldedLoad)
1245         ReplaceUses(N1.getValue(1), Result.getValue(1));
1246
1247 #ifndef NDEBUG
1248       DOUT << std::string(Indent-2, ' ') << "=> ";
1249       DEBUG(Result.Val->dump(CurDAG));
1250       DOUT << "\n";
1251       Indent -= 2;
1252 #endif
1253
1254       return NULL;
1255     }
1256
1257     case ISD::TRUNCATE: {
1258       if (!Subtarget->is64Bit() && NVT == MVT::i8) {
1259         unsigned Opc2;
1260         MVT::ValueType VT;
1261         switch (Node->getOperand(0).getValueType()) {
1262         default: assert(0 && "Unknown truncate!");
1263         case MVT::i16:
1264           Opc = X86::MOV16to16_;
1265           VT = MVT::i16;
1266           Opc2 = X86::TRUNC_16_to8;
1267           break;
1268         case MVT::i32:
1269           Opc = X86::MOV32to32_;
1270           VT = MVT::i32;
1271           Opc2 = X86::TRUNC_32_to8;
1272           break;
1273         }
1274
1275         AddToISelQueue(Node->getOperand(0));
1276         SDOperand Tmp =
1277           SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
1278         SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
1279       
1280 #ifndef NDEBUG
1281         DOUT << std::string(Indent-2, ' ') << "=> ";
1282         DEBUG(ResNode->dump(CurDAG));
1283         DOUT << "\n";
1284         Indent -= 2;
1285 #endif
1286         return ResNode;
1287       }
1288
1289       break;
1290     }
1291   }
1292
1293   SDNode *ResNode = SelectCode(N);
1294
1295 #ifndef NDEBUG
1296   DOUT << std::string(Indent-2, ' ') << "=> ";
1297   if (ResNode == NULL || ResNode == N.Val)
1298     DEBUG(N.Val->dump(CurDAG));
1299   else
1300     DEBUG(ResNode->dump(CurDAG));
1301   DOUT << "\n";
1302   Indent -= 2;
1303 #endif
1304
1305   return ResNode;
1306 }
1307
1308 bool X86DAGToDAGISel::
1309 SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1310                              std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1311   SDOperand Op0, Op1, Op2, Op3;
1312   switch (ConstraintCode) {
1313   case 'o':   // offsetable        ??
1314   case 'v':   // not offsetable    ??
1315   default: return true;
1316   case 'm':   // memory
1317     if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1318       return true;
1319     break;
1320   }
1321   
1322   OutOps.push_back(Op0);
1323   OutOps.push_back(Op1);
1324   OutOps.push_back(Op2);
1325   OutOps.push_back(Op3);
1326   AddToISelQueue(Op0);
1327   AddToISelQueue(Op1);
1328   AddToISelQueue(Op2);
1329   AddToISelQueue(Op3);
1330   return false;
1331 }
1332
1333 /// createX86ISelDag - This pass converts a legalized DAG into a 
1334 /// X86-specific DAG, ready for instruction scheduling.
1335 ///
1336 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1337   return new X86DAGToDAGISel(TM, Fast);
1338 }