X86ISelDAGToDAG.cpp revision 020d2e8e7aa36692af13c1215fdd6248a6d9e950
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 "isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
20#include "X86ISelLowering.h"
21#include "llvm/GlobalValue.h"
22#include "llvm/Instructions.h"
23#include "llvm/Support/CFG.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/CodeGen/SelectionDAGISel.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/ADT/Statistic.h"
33#include <iostream>
34#include <set>
35using namespace llvm;
36
37//===----------------------------------------------------------------------===//
38//                      Pattern Matcher Implementation
39//===----------------------------------------------------------------------===//
40
41namespace {
42  /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
43  /// SDOperand's instead of register numbers for the leaves of the matched
44  /// tree.
45  struct X86ISelAddressMode {
46    enum {
47      RegBase,
48      FrameIndexBase,
49      ConstantPoolBase
50    } BaseType;
51
52    struct {            // This is really a union, discriminated by BaseType!
53      SDOperand Reg;
54      int FrameIndex;
55    } Base;
56
57    unsigned Scale;
58    SDOperand IndexReg;
59    unsigned Disp;
60    GlobalValue *GV;
61
62    X86ISelAddressMode()
63      : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
64    }
65  };
66}
67
68namespace {
69  Statistic<>
70  NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
71
72  //===--------------------------------------------------------------------===//
73  /// ISel - X86 specific code to select X86 machine instructions for
74  /// SelectionDAG operations.
75  ///
76  class X86DAGToDAGISel : public SelectionDAGISel {
77    /// ContainsFPCode - Every instruction we select that uses or defines a FP
78    /// register should set this to true.
79    bool ContainsFPCode;
80
81    /// X86Lowering - This object fully describes how to lower LLVM code to an
82    /// X86-specific SelectionDAG.
83    X86TargetLowering X86Lowering;
84
85    /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
86    /// make the right decision when generating code for different targets.
87    const X86Subtarget *Subtarget;
88
89    unsigned GlobalBaseReg;
90  public:
91    X86DAGToDAGISel(TargetMachine &TM)
92      : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
93      Subtarget = &TM.getSubtarget<X86Subtarget>();
94    }
95
96    virtual bool runOnFunction(Function &Fn) {
97      // Make sure we re-emit a set of the global base reg if necessary
98      GlobalBaseReg = 0;
99      return SelectionDAGISel::runOnFunction(Fn);
100    }
101
102    virtual const char *getPassName() const {
103      return "X86 DAG->DAG Instruction Selection";
104    }
105
106    /// InstructionSelectBasicBlock - This callback is invoked by
107    /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
108    virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
109
110    virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
111
112// Include the pieces autogenerated from the target description.
113#include "X86GenDAGISel.inc"
114
115  private:
116    void Select(SDOperand &Result, SDOperand N);
117
118    bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
119    bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
120                    SDOperand &Index, SDOperand &Disp);
121    bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
122                       SDOperand &Index, SDOperand &Disp);
123    bool TryFoldLoad(SDOperand P, SDOperand N,
124                     SDOperand &Base, SDOperand &Scale,
125                     SDOperand &Index, SDOperand &Disp);
126
127    inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
128                                   SDOperand &Scale, SDOperand &Index,
129                                   SDOperand &Disp) {
130      Base  = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
131        CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
132      Scale = getI8Imm(AM.Scale);
133      Index = AM.IndexReg;
134      Disp  = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
135        : getI32Imm(AM.Disp);
136    }
137
138    /// getI8Imm - Return a target constant with the specified value, of type
139    /// i8.
140    inline SDOperand getI8Imm(unsigned Imm) {
141      return CurDAG->getTargetConstant(Imm, MVT::i8);
142    }
143
144    /// getI16Imm - Return a target constant with the specified value, of type
145    /// i16.
146    inline SDOperand getI16Imm(unsigned Imm) {
147      return CurDAG->getTargetConstant(Imm, MVT::i16);
148    }
149
150    /// getI32Imm - Return a target constant with the specified value, of type
151    /// i32.
152    inline SDOperand getI32Imm(unsigned Imm) {
153      return CurDAG->getTargetConstant(Imm, MVT::i32);
154    }
155
156    /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
157    /// base register.  Return the virtual register that holds this value.
158    SDOperand getGlobalBaseReg();
159
160#ifndef NDEBUG
161    unsigned Indent;
162#endif
163  };
164}
165
166/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
167/// when it has created a SelectionDAG for us to codegen.
168void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
169  DEBUG(BB->dump());
170  MachineFunction::iterator FirstMBB = BB;
171
172  // Codegen the basic block.
173#ifndef NDEBUG
174  DEBUG(std::cerr << "===== Instruction selection begins:\n");
175  Indent = 0;
176#endif
177  DAG.setRoot(SelectRoot(DAG.getRoot()));
178#ifndef NDEBUG
179  DEBUG(std::cerr << "===== Instruction selection ends:\n");
180#endif
181  CodeGenMap.clear();
182  DAG.RemoveDeadNodes();
183
184  // Emit machine code to BB.
185  ScheduleAndEmitDAG(DAG);
186
187  // If we are emitting FP stack code, scan the basic block to determine if this
188  // block defines any FP values.  If so, put an FP_REG_KILL instruction before
189  // the terminator of the block.
190  if (!Subtarget->hasSSE2()) {
191    // Note that FP stack instructions *are* used in SSE code when returning
192    // values, but these are not live out of the basic block, so we don't need
193    // an FP_REG_KILL in this case either.
194    bool ContainsFPCode = false;
195
196    // Scan all of the machine instructions in these MBBs, checking for FP
197    // stores.
198    MachineFunction::iterator MBBI = FirstMBB;
199    do {
200      for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
201           !ContainsFPCode && I != E; ++I) {
202        for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
203          if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
204              MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
205              RegMap->getRegClass(I->getOperand(0).getReg()) ==
206                X86::RFPRegisterClass) {
207            ContainsFPCode = true;
208            break;
209          }
210        }
211      }
212    } while (!ContainsFPCode && &*(MBBI++) != BB);
213
214    // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
215    // a copy of the input value in this block.
216    if (!ContainsFPCode) {
217      // Final check, check LLVM BB's that are successors to the LLVM BB
218      // corresponding to BB for FP PHI nodes.
219      const BasicBlock *LLVMBB = BB->getBasicBlock();
220      const PHINode *PN;
221      for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
222           !ContainsFPCode && SI != E; ++SI) {
223        for (BasicBlock::const_iterator II = SI->begin();
224             (PN = dyn_cast<PHINode>(II)); ++II) {
225          if (PN->getType()->isFloatingPoint()) {
226            ContainsFPCode = true;
227            break;
228          }
229        }
230      }
231    }
232
233    // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
234    if (ContainsFPCode) {
235      BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
236      ++NumFPKill;
237    }
238  }
239}
240
241/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
242/// the main function.
243static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
244                                   MachineFrameInfo *MFI) {
245  // Switch the FPU to 64-bit precision mode for better compatibility and speed.
246  int CWFrameIdx = MFI->CreateStackObject(2, 2);
247  addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
248
249  // Set the high part to be 64-bit precision.
250  addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
251                    CWFrameIdx, 1).addImm(2);
252
253  // Reload the modified control word now.
254  addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
255}
256
257void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
258  // If this is main, emit special code for main.
259  MachineBasicBlock *BB = MF.begin();
260  if (Fn.hasExternalLinkage() && Fn.getName() == "main")
261    EmitSpecialCodeForMain(BB, MF.getFrameInfo());
262}
263
264/// MatchAddress - Add the specified node to the specified addressing mode,
265/// returning true if it cannot be done.  This just pattern matches for the
266/// addressing mode
267bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
268                                   bool isRoot) {
269  bool StopHere = false;
270  // If N has already been selected, we may or may not want to fold its
271  // operands into the addressing mode. It will result in code duplication!
272  // FIXME: Right now we do. That is, as long as the selected target node
273  // does not produce a chain. This may require a more sophisticated heuristics.
274  std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
275  if (CGMI != CodeGenMap.end()) {
276    if (isRoot)
277      // Stop here if it is a root. It's probably not profitable to go deeper.
278      StopHere = true;
279    else {
280      for (unsigned i = 0, e = CGMI->second.Val->getNumValues(); i != e; ++i) {
281        if (CGMI->second.Val->getValueType(i) == MVT::Other)
282          StopHere = true;
283      }
284    }
285  }
286
287  switch (N.getOpcode()) {
288  default: break;
289  case ISD::FrameIndex:
290    if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
291      AM.BaseType = X86ISelAddressMode::FrameIndexBase;
292      AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
293      return false;
294    }
295    break;
296
297  case ISD::ConstantPool:
298    if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
299      if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) {
300        AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
301        AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
302                                                    CP->getAlignment());
303        return false;
304      }
305    }
306    break;
307
308  case ISD::GlobalAddress:
309    if (AM.GV == 0) {
310      AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
311      return false;
312    }
313    break;
314
315  case X86ISD::Wrapper:
316    if (ConstantPoolSDNode *CP =
317        dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
318      if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
319        AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
320        AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
321                                                    CP->getAlignment());
322        return false;
323      }
324    } else if (GlobalAddressSDNode *G =
325               dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
326      if (AM.GV == 0) {
327        AM.GV = cast<GlobalAddressSDNode>(N.getOperand(0))->getGlobal();
328        return false;
329      }
330    }
331    break;
332
333  case ISD::Constant:
334    AM.Disp += cast<ConstantSDNode>(N)->getValue();
335    return false;
336
337  case ISD::SHL:
338    if (!StopHere && AM.IndexReg.Val == 0 && AM.Scale == 1)
339      if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
340        unsigned Val = CN->getValue();
341        if (Val == 1 || Val == 2 || Val == 3) {
342          AM.Scale = 1 << Val;
343          SDOperand ShVal = N.Val->getOperand(0);
344
345          // Okay, we know that we have a scale by now.  However, if the scaled
346          // value is an add of something and a constant, we can fold the
347          // constant into the disp field here.
348          if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
349              isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
350            AM.IndexReg = ShVal.Val->getOperand(0);
351            ConstantSDNode *AddVal =
352              cast<ConstantSDNode>(ShVal.Val->getOperand(1));
353            AM.Disp += AddVal->getValue() << Val;
354          } else {
355            AM.IndexReg = ShVal;
356          }
357          return false;
358        }
359      }
360    break;
361
362  case ISD::MUL:
363    // X*[3,5,9] -> X+X*[2,4,8]
364    if (!StopHere && AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
365        AM.Base.Reg.Val == 0)
366      if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
367        if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
368          AM.Scale = unsigned(CN->getValue())-1;
369
370          SDOperand MulVal = N.Val->getOperand(0);
371          SDOperand Reg;
372
373          // Okay, we know that we have a scale by now.  However, if the scaled
374          // value is an add of something and a constant, we can fold the
375          // constant into the disp field here.
376          if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
377              isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
378            Reg = MulVal.Val->getOperand(0);
379            ConstantSDNode *AddVal =
380              cast<ConstantSDNode>(MulVal.Val->getOperand(1));
381            AM.Disp += AddVal->getValue() * CN->getValue();
382          } else {
383            Reg = N.Val->getOperand(0);
384          }
385
386          AM.IndexReg = AM.Base.Reg = Reg;
387          return false;
388        }
389    break;
390
391  case ISD::ADD: {
392    if (!StopHere) {
393      X86ISelAddressMode Backup = AM;
394      if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
395          !MatchAddress(N.Val->getOperand(1), AM, false))
396        return false;
397      AM = Backup;
398      if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
399          !MatchAddress(N.Val->getOperand(0), AM, false))
400        return false;
401      AM = Backup;
402    }
403    break;
404  }
405  }
406
407  // Is the base register already occupied?
408  if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
409    // TargetConstantPool cannot be anything but the base.
410    if (N.getOpcode() == ISD::TargetConstantPool)
411      return true;
412
413    // If so, check to see if the scale index register is set.
414    if (AM.IndexReg.Val == 0) {
415      AM.IndexReg = N;
416      AM.Scale = 1;
417      return false;
418    }
419
420    // Otherwise, we cannot select it.
421    return true;
422  }
423
424  // Default, generate it as a register.
425  AM.BaseType = X86ISelAddressMode::RegBase;
426  AM.Base.Reg = N;
427  return false;
428}
429
430/// SelectAddr - returns true if it is able pattern match an addressing mode.
431/// It returns the operands which make up the maximal addressing mode it can
432/// match by reference.
433bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
434                                 SDOperand &Index, SDOperand &Disp) {
435  X86ISelAddressMode AM;
436  if (MatchAddress(N, AM))
437    return false;
438
439  if (AM.BaseType == X86ISelAddressMode::RegBase) {
440    if (!AM.Base.Reg.Val)
441      AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
442  }
443
444  if (!AM.IndexReg.Val)
445    AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
446
447  getAddressOperands(AM, Base, Scale, Index, Disp);
448  return true;
449}
450
451bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
452                                  SDOperand &Base, SDOperand &Scale,
453                                  SDOperand &Index, SDOperand &Disp) {
454  if (N.getOpcode() == ISD::LOAD &&
455      N.hasOneUse() &&
456      !CodeGenMap.count(N.getValue(0)) &&
457      (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
458    return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
459  return false;
460}
461
462static bool isRegister0(SDOperand Op) {
463  if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
464    return (R->getReg() == 0);
465  return false;
466}
467
468/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
469/// mode it matches can be cost effectively emitted as an LEA instruction.
470/// For X86, it always is unless it's just a (Reg + const).
471bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
472                                    SDOperand &Scale,
473                                    SDOperand &Index, SDOperand &Disp) {
474  X86ISelAddressMode AM;
475  if (!MatchAddress(N, AM)) {
476    bool SelectIndex = false;
477    bool Check       = false;
478    if (AM.BaseType == X86ISelAddressMode::RegBase) {
479      if (AM.Base.Reg.Val)
480        Check = true;
481      else
482        AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
483    }
484
485    if (AM.IndexReg.Val)
486      SelectIndex = true;
487    else
488      AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
489
490    if (Check) {
491      unsigned Complexity = 0;
492      if (AM.Scale > 1)
493        Complexity++;
494      if (SelectIndex)
495        Complexity++;
496      if (AM.GV) {
497        Complexity++;
498        if (AM.Disp)
499          Complexity++;
500      } else if (AM.Disp > 1)
501        Complexity++;
502      // Suppose base == %eax and it has multiple uses, then instead of
503      //   movl %eax, %ecx
504      //   addl $8, %ecx
505      // use
506      //   leal 8(%eax), %ecx.
507      // FIXME: If the other uses ended up being scheduled ahead of the leal
508      // then it would have been better to use the addl. The proper way to
509      // handle this is with using  X86InstrInfo::convertToThreeAddress hook.
510      // From an email:
511      // BTW, this problem is the one that inspired the
512      // "X86InstrInfo::convertToThreeAddress" hook (which would handle this
513      // the "right" way).  Unfortunately the X86 implementation of this is
514      // disabled, because we don't currently have enough information handy to
515      // know that the flags from the add is dead when the hook is called (from
516      // the register allocator).
517      if (AM.Base.Reg.Val->use_size() > 1)
518        Complexity++;
519      if (Complexity <= 1)
520        return false;
521    }
522
523    getAddressOperands(AM, Base, Scale, Index, Disp);
524    return true;
525  }
526  return false;
527}
528
529/// getGlobalBaseReg - Output the instructions required to put the
530/// base address to use for accessing globals into a register.
531///
532SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
533  if (!GlobalBaseReg) {
534    // Insert the set of GlobalBaseReg into the first MBB of the function
535    MachineBasicBlock &FirstMBB = BB->getParent()->front();
536    MachineBasicBlock::iterator MBBI = FirstMBB.begin();
537    SSARegMap *RegMap = BB->getParent()->getSSARegMap();
538    // FIXME: when we get to LP64, we will need to create the appropriate
539    // type of register here.
540    GlobalBaseReg = RegMap->createVirtualRegister(X86::R32RegisterClass);
541    BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
542    BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
543  }
544  return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
545}
546
547void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
548  SDNode *Node = N.Val;
549  MVT::ValueType NVT = Node->getValueType(0);
550  unsigned Opc, MOpc;
551  unsigned Opcode = Node->getOpcode();
552
553#ifndef NDEBUG
554  DEBUG(std::cerr << std::string(Indent, ' '));
555  DEBUG(std::cerr << "Selecting: ");
556  DEBUG(Node->dump(CurDAG));
557  DEBUG(std::cerr << "\n");
558  Indent += 2;
559#endif
560
561  if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
562    Result = N;
563#ifndef NDEBUG
564    DEBUG(std::cerr << std::string(Indent-2, ' '));
565    DEBUG(std::cerr << "== ");
566    DEBUG(Node->dump(CurDAG));
567    DEBUG(std::cerr << "\n");
568    Indent -= 2;
569#endif
570    return;   // Already selected.
571  }
572
573  std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
574  if (CGMI != CodeGenMap.end()) {
575    Result = CGMI->second;
576#ifndef NDEBUG
577    DEBUG(std::cerr << std::string(Indent-2, ' '));
578    DEBUG(std::cerr << "== ");
579    DEBUG(Result.Val->dump(CurDAG));
580    DEBUG(std::cerr << "\n");
581    Indent -= 2;
582#endif
583    return;
584  }
585
586  switch (Opcode) {
587    default: break;
588    case X86ISD::GlobalBaseReg:
589      Result = getGlobalBaseReg();
590      return;
591
592    case X86ISD::Wrapper: {
593      // It's beneficial to manully select the wrapper nodes here rather
594      // then using tablgen'd code to match this. We do not want to mutate the
595      // node to MOV32ri and we do not want to record this in CodeGenMap.
596      // We want to allow the wrapped leaf nodes be duplicated so they can
597      // be used in addressing modes.
598      // e.g.
599      //       0xa59e4a0: i32 = TargetGlobalAddress <xxx> 0
600      //   0xa59e740: i32 = X86ISD::Wrapper 0xa59e4a0
601      // ...
602      // 0xa59e880: i32 = add 0xa59e740, 0xa59e800
603      // ...
604      //       0xa59e880: <multiple use>
605      //   0xa59e970: i32 = add 0xa59e880, 0xa59e910
606      // ...
607      // 0xa59ea60: i32,ch = load 0xa589780, 0xa59e970, 0xa59ea00
608      // ...
609      //          0xa59e880: <multiple use>
610      //        0xa59eb60: ch = CopyToReg 0xa59ea60:1, 0xa59eaf0, 0xa59e880
611      // By allowing the TargetGlobalAddress to be duplicated, it can appear
612      // in the load address as well as an operand of the add.
613      Result = SDOperand(CurDAG->getTargetNode(X86::MOV32ri, MVT::i32,
614                                               N.getOperand(0)), 0);
615#ifndef NDEBUG
616      DEBUG(std::cerr << std::string(Indent-2, ' '));
617      DEBUG(std::cerr << "== ");
618      DEBUG(Result.Val->dump(CurDAG));
619      DEBUG(std::cerr << "\n");
620      Indent -= 2;
621#endif
622      return;
623    }
624
625    case ISD::MULHU:
626    case ISD::MULHS: {
627      if (Opcode == ISD::MULHU)
628        switch (NVT) {
629        default: assert(0 && "Unsupported VT!");
630        case MVT::i8:  Opc = X86::MUL8r;  MOpc = X86::MUL8m;  break;
631        case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
632        case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
633        }
634      else
635        switch (NVT) {
636        default: assert(0 && "Unsupported VT!");
637        case MVT::i8:  Opc = X86::IMUL8r;  MOpc = X86::IMUL8m;  break;
638        case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
639        case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
640        }
641
642      unsigned LoReg, HiReg;
643      switch (NVT) {
644      default: assert(0 && "Unsupported VT!");
645      case MVT::i8:  LoReg = X86::AL;  HiReg = X86::AH;  break;
646      case MVT::i16: LoReg = X86::AX;  HiReg = X86::DX;  break;
647      case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
648      }
649
650      SDOperand N0 = Node->getOperand(0);
651      SDOperand N1 = Node->getOperand(1);
652
653      bool foldedLoad = false;
654      SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
655      foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
656      // MULHU and MULHS are commmutative
657      if (!foldedLoad) {
658        foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
659        if (foldedLoad) {
660          N0 = Node->getOperand(1);
661          N1 = Node->getOperand(0);
662        }
663      }
664
665      SDOperand Chain;
666      if (foldedLoad)
667        Select(Chain, N1.getOperand(0));
668      else
669        Chain = CurDAG->getEntryNode();
670
671      SDOperand InFlag(0, 0);
672      Select(N0, N0);
673      Chain  = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
674                                    N0, InFlag);
675      InFlag = Chain.getValue(1);
676
677      if (foldedLoad) {
678        Select(Tmp0, Tmp0);
679        Select(Tmp1, Tmp1);
680        Select(Tmp2, Tmp2);
681        Select(Tmp3, Tmp3);
682        SDNode *CNode =
683          CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
684                                Tmp2, Tmp3, Chain, InFlag);
685        Chain  = SDOperand(CNode, 0);
686        InFlag = SDOperand(CNode, 1);
687      } else {
688        Select(N1, N1);
689        InFlag =
690          SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
691      }
692
693      Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
694      CodeGenMap[N.getValue(0)] = Result;
695      if (foldedLoad) {
696        CodeGenMap[N1.getValue(1)] = Result.getValue(1);
697        AddHandleReplacement(N1.Val, 1, Result.Val, 1);
698      }
699
700#ifndef NDEBUG
701      DEBUG(std::cerr << std::string(Indent-2, ' '));
702      DEBUG(std::cerr << "== ");
703      DEBUG(Result.Val->dump(CurDAG));
704      DEBUG(std::cerr << "\n");
705      Indent -= 2;
706#endif
707      return;
708    }
709
710    case ISD::SDIV:
711    case ISD::UDIV:
712    case ISD::SREM:
713    case ISD::UREM: {
714      bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
715      bool isDiv    = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
716      if (!isSigned)
717        switch (NVT) {
718        default: assert(0 && "Unsupported VT!");
719        case MVT::i8:  Opc = X86::DIV8r;  MOpc = X86::DIV8m;  break;
720        case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
721        case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
722        }
723      else
724        switch (NVT) {
725        default: assert(0 && "Unsupported VT!");
726        case MVT::i8:  Opc = X86::IDIV8r;  MOpc = X86::IDIV8m;  break;
727        case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
728        case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
729        }
730
731      unsigned LoReg, HiReg;
732      unsigned ClrOpcode, SExtOpcode;
733      switch (NVT) {
734      default: assert(0 && "Unsupported VT!");
735      case MVT::i8:
736        LoReg = X86::AL;  HiReg = X86::AH;
737        ClrOpcode  = X86::MOV8ri;
738        SExtOpcode = X86::CBW;
739        break;
740      case MVT::i16:
741        LoReg = X86::AX;  HiReg = X86::DX;
742        ClrOpcode  = X86::MOV16ri;
743        SExtOpcode = X86::CWD;
744        break;
745      case MVT::i32:
746        LoReg = X86::EAX; HiReg = X86::EDX;
747        ClrOpcode  = X86::MOV32ri;
748        SExtOpcode = X86::CDQ;
749        break;
750      }
751
752      SDOperand N0 = Node->getOperand(0);
753      SDOperand N1 = Node->getOperand(1);
754
755      bool foldedLoad = false;
756      SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
757      foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
758      SDOperand Chain;
759      if (foldedLoad)
760        Select(Chain, N1.getOperand(0));
761      else
762        Chain = CurDAG->getEntryNode();
763
764      SDOperand InFlag(0, 0);
765      Select(N0, N0);
766      Chain  = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
767                                    N0, InFlag);
768      InFlag = Chain.getValue(1);
769
770      if (isSigned) {
771        // Sign extend the low part into the high part.
772        InFlag =
773          SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
774      } else {
775        // Zero out the high part, effectively zero extending the input.
776        SDOperand ClrNode =
777          SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT,
778                                         CurDAG->getTargetConstant(0, NVT)), 0);
779        Chain  = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
780                                      ClrNode, InFlag);
781        InFlag = Chain.getValue(1);
782      }
783
784      if (foldedLoad) {
785        Select(Tmp0, Tmp0);
786        Select(Tmp1, Tmp1);
787        Select(Tmp2, Tmp2);
788        Select(Tmp3, Tmp3);
789        SDNode *CNode =
790          CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
791                                Tmp2, Tmp3, Chain, InFlag);
792        Chain  = SDOperand(CNode, 0);
793        InFlag = SDOperand(CNode, 1);
794      } else {
795        Select(N1, N1);
796        InFlag =
797          SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
798      }
799
800      Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
801                                      NVT, InFlag);
802      CodeGenMap[N.getValue(0)] = Result;
803      if (foldedLoad) {
804        CodeGenMap[N1.getValue(1)] = Result.getValue(1);
805        AddHandleReplacement(N1.Val, 1, Result.Val, 1);
806      }
807
808#ifndef NDEBUG
809      DEBUG(std::cerr << std::string(Indent-2, ' '));
810      DEBUG(std::cerr << "== ");
811      DEBUG(Result.Val->dump(CurDAG));
812      DEBUG(std::cerr << "\n");
813      Indent -= 2;
814#endif
815      return;
816    }
817
818    case ISD::TRUNCATE: {
819      unsigned Reg;
820      MVT::ValueType VT;
821      switch (Node->getOperand(0).getValueType()) {
822        default: assert(0 && "Unknown truncate!");
823        case MVT::i16: Reg = X86::AX;  Opc = X86::MOV16rr; VT = MVT::i16; break;
824        case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
825      }
826      SDOperand Tmp0, Tmp1;
827      Select(Tmp0, Node->getOperand(0));
828      Select(Tmp1, SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0));
829      SDOperand InFlag = SDOperand(0,0);
830      Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(), Reg, Tmp1, InFlag);
831      SDOperand Chain = Result.getValue(0);
832      InFlag = Result.getValue(1);
833
834      switch (NVT) {
835        default: assert(0 && "Unknown truncate!");
836        case MVT::i8:  Reg = X86::AL;  Opc = X86::MOV8rr;  VT = MVT::i8;  break;
837        case MVT::i16: Reg = X86::AX;  Opc = X86::MOV16rr; VT = MVT::i16; break;
838      }
839
840      Result = CurDAG->getCopyFromReg(Chain, Reg, VT, InFlag);
841      if (N.Val->hasOneUse())
842        Result = CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
843      else
844        Result = CodeGenMap[N] =
845          SDOperand(CurDAG->getTargetNode(Opc, VT, Result), 0);
846
847#ifndef NDEBUG
848      DEBUG(std::cerr << std::string(Indent-2, ' '));
849      DEBUG(std::cerr << "== ");
850      DEBUG(Result.Val->dump(CurDAG));
851      DEBUG(std::cerr << "\n");
852      Indent -= 2;
853#endif
854      return;
855    }
856  }
857
858  SelectCode(Result, N);
859#ifndef NDEBUG
860  DEBUG(std::cerr << std::string(Indent-2, ' '));
861  DEBUG(std::cerr << "=> ");
862  DEBUG(Result.Val->dump(CurDAG));
863  DEBUG(std::cerr << "\n");
864  Indent -= 2;
865#endif
866}
867
868/// createX86ISelDag - This pass converts a legalized DAG into a
869/// X86-specific DAG, ready for instruction scheduling.
870///
871FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
872  return new X86DAGToDAGISel(TM);
873}
874