Fix coding style violations. Remove white spaces and tabs.
[oota-llvm.git] / lib / Target / Mips / MipsISelDAGToDAG.cpp
1 //===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===//
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 an instruction selector for the MIPS target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mips-isel"
15 #include "Mips.h"
16 #include "MipsAnalyzeImmediate.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "MCTargetDesc/MipsBaseInfo.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/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/CodeGen/SelectionDAGNodes.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
38 using namespace llvm;
39
40 //===----------------------------------------------------------------------===//
41 // Instruction Selector Implementation
42 //===----------------------------------------------------------------------===//
43
44 //===----------------------------------------------------------------------===//
45 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46 // instructions for SelectionDAG operations.
47 //===----------------------------------------------------------------------===//
48 namespace {
49
50 class MipsDAGToDAGISel : public SelectionDAGISel {
51
52   /// TM - Keep a reference to MipsTargetMachine.
53   MipsTargetMachine &TM;
54
55   /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
56   /// make the right decision when generating code for different targets.
57   const MipsSubtarget &Subtarget;
58
59 public:
60   explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
61   SelectionDAGISel(tm),
62   TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
63
64   // Pass Name
65   virtual const char *getPassName() const {
66     return "MIPS DAG->DAG Pattern Instruction Selection";
67   }
68
69   virtual bool runOnMachineFunction(MachineFunction &MF);
70
71 private:
72   // Include the pieces autogenerated from the target description.
73   #include "MipsGenDAGISel.inc"
74
75   /// getTargetMachine - Return a reference to the TargetMachine, casted
76   /// to the target-specific type.
77   const MipsTargetMachine &getTargetMachine() {
78     return static_cast<const MipsTargetMachine &>(TM);
79   }
80
81   /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
82   /// to the target-specific type.
83   const MipsInstrInfo *getInstrInfo() {
84     return getTargetMachine().getInstrInfo();
85   }
86
87   SDNode *getGlobalBaseReg();
88
89   std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl,
90                                          EVT Ty, bool HasLo, bool HasHi);
91
92   SDNode *Select(SDNode *N);
93
94   // Complex Pattern.
95   bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset);
96
97   // getImm - Return a target constant with the specified value.
98   inline SDValue getImm(const SDNode *Node, unsigned Imm) {
99     return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
100   }
101
102   void ProcessFunctionAfterISel(MachineFunction &MF);
103   bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&);
104   void InitGlobalBaseReg(MachineFunction &MF);
105
106   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
107                                             char ConstraintCode,
108                                             std::vector<SDValue> &OutOps);
109 };
110
111 }
112
113 // Insert instructions to initialize the global base register in the
114 // first MBB of the function. When the ABI is O32 and the relocation model is
115 // PIC, the necessary instructions are emitted later to prevent optimization
116 // passes from moving them.
117 void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
118   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
119
120   MachineBasicBlock &MBB = MF.front();
121   MachineBasicBlock::iterator I = MBB.begin();
122   MachineRegisterInfo &RegInfo = MF.getRegInfo();
123   const MipsRegisterInfo *TargetRegInfo = TM.getRegisterInfo();
124   const MipsInstrInfo *MII = TM.getInstrInfo();
125   const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
126   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
127   unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
128   int FI = MipsFI->initGlobalRegFI();
129
130   const TargetRegisterClass *RC = Subtarget.isABI_N64() ?
131     (const TargetRegisterClass*)&Mips::CPU64RegsRegClass :
132     (const TargetRegisterClass*)&Mips::CPURegsRegClass;
133
134   V0 = RegInfo.createVirtualRegister(RC);
135   V1 = RegInfo.createVirtualRegister(RC);
136
137   if (Subtarget.isABI_N64()) {
138     MF.getRegInfo().addLiveIn(Mips::T9_64);
139     MBB.addLiveIn(Mips::T9_64);
140
141     // lui $v0, %hi(%neg(%gp_rel(fname)))
142     // daddu $v1, $v0, $t9
143     // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
144     const GlobalValue *FName = MF.getFunction();
145     BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
146       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
147     BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
148       .addReg(Mips::T9_64);
149     BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
150       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
151     MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
152                              TargetRegInfo);
153     return;
154   }
155
156   if (MF.getTarget().getRelocationModel() == Reloc::Static) {
157     // Set global register to __gnu_local_gp.
158     //
159     // lui   $v0, %hi(__gnu_local_gp)
160     // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
161     BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
162       .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
163     BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
164       .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
165     MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
166                              TargetRegInfo);
167     return;
168   }
169
170   MF.getRegInfo().addLiveIn(Mips::T9);
171   MBB.addLiveIn(Mips::T9);
172
173   if (Subtarget.isABI_N32()) {
174     // lui $v0, %hi(%neg(%gp_rel(fname)))
175     // addu $v1, $v0, $t9
176     // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
177     const GlobalValue *FName = MF.getFunction();
178     BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
179       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
180     BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
181     BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
182       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
183     MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
184                              TargetRegInfo);
185     return;
186   }
187
188   assert(Subtarget.isABI_O32());
189
190   // For O32 ABI, the following instruction sequence is emitted to initialize
191   // the global base register:
192   //
193   //  0. lui   $2, %hi(_gp_disp)
194   //  1. addiu $2, $2, %lo(_gp_disp)
195   //  2. addu  $globalbasereg, $2, $t9
196   //
197   // We emit only the last instruction here.
198   //
199   // GNU linker requires that the first two instructions appear at the beginning
200   // of a function and no instructions be inserted before or between them.
201   // The two instructions are emitted during lowering to MC layer in order to
202   // avoid any reordering.
203   //
204   // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
205   // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
206   // reads it.
207   MF.getRegInfo().addLiveIn(Mips::V0);
208   MBB.addLiveIn(Mips::V0);
209   BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
210     .addReg(Mips::V0).addReg(Mips::T9);
211   MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC, TargetRegInfo);
212 }
213
214 bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
215                                               const MachineInstr& MI) {
216   unsigned DstReg = 0, ZeroReg = 0;
217
218   // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
219   if ((MI.getOpcode() == Mips::ADDiu) &&
220       (MI.getOperand(1).getReg() == Mips::ZERO) &&
221       (MI.getOperand(2).getImm() == 0)) {
222     DstReg = MI.getOperand(0).getReg();
223     ZeroReg = Mips::ZERO;
224   } else if ((MI.getOpcode() == Mips::DADDiu) &&
225              (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
226              (MI.getOperand(2).getImm() == 0)) {
227     DstReg = MI.getOperand(0).getReg();
228     ZeroReg = Mips::ZERO_64;
229   }
230
231   if (!DstReg)
232     return false;
233
234   // Replace uses with ZeroReg.
235   for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
236        E = MRI->use_end(); U != E; ++U) {
237     MachineOperand &MO = U.getOperand();
238     MachineInstr *MI = MO.getParent();
239
240     // Do not replace if it is a phi's operand or is tied to def operand.
241     if (MI->isPHI() || MI->isRegTiedToDefOperand(U.getOperandNo()) ||
242         MI->isPseudo())
243       continue;
244
245     MO.setReg(ZeroReg);
246   }
247
248   return true;
249 }
250
251 void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
252   InitGlobalBaseReg(MF);
253
254   MachineRegisterInfo *MRI = &MF.getRegInfo();
255
256   for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
257        ++MFI)
258     for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
259       ReplaceUsesWithZeroReg(MRI, *I);
260 }
261
262 bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
263   bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
264
265   ProcessFunctionAfterISel(MF);
266
267   return Ret;
268 }
269
270 /// getGlobalBaseReg - Output the instructions required to put the
271 /// GOT address into a register.
272 SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
273   unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
274   return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
275 }
276
277 /// ComplexPattern used on MipsInstrInfo
278 /// Used on Mips Load/Store instructions
279 bool MipsDAGToDAGISel::
280 SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
281   EVT ValTy = Addr.getValueType();
282
283   // If Parent is an unaligned f32 load or store, select a (base + index)
284   // floating point load/store instruction (luxc1 or suxc1).
285   const LSBaseSDNode *LS = 0;
286
287   if (Parent && (LS = dyn_cast<LSBaseSDNode>(Parent))) {
288     EVT VT = LS->getMemoryVT();
289
290     if (VT.getSizeInBits() / 8 > LS->getAlignment()) {
291       assert(TLI.allowsUnalignedMemoryAccesses(VT) &&
292              "Unaligned loads/stores not supported for this type.");
293       if (VT == MVT::f32)
294         return false;
295     }
296   }
297
298   // if Address is FI, get the TargetFrameIndex.
299   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
300     Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
301     Offset = CurDAG->getTargetConstant(0, ValTy);
302     return true;
303   }
304
305   // on PIC code Load GA
306   if (Addr.getOpcode() == MipsISD::Wrapper) {
307     Base   = Addr.getOperand(0);
308     Offset = Addr.getOperand(1);
309     return true;
310   }
311
312   if (TM.getRelocationModel() != Reloc::PIC_) {
313     if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
314         Addr.getOpcode() == ISD::TargetGlobalAddress))
315       return false;
316   }
317
318   // Addresses of the form FI+const or FI|const
319   if (CurDAG->isBaseWithConstantOffset(Addr)) {
320     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
321     if (isInt<16>(CN->getSExtValue())) {
322
323       // If the first operand is a FI, get the TargetFI Node
324       if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
325                                   (Addr.getOperand(0)))
326         Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
327       else
328         Base = Addr.getOperand(0);
329
330       Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
331       return true;
332     }
333   }
334
335   // Operand is a result from an ADD.
336   if (Addr.getOpcode() == ISD::ADD) {
337     // When loading from constant pools, load the lower address part in
338     // the instruction itself. Example, instead of:
339     //  lui $2, %hi($CPI1_0)
340     //  addiu $2, $2, %lo($CPI1_0)
341     //  lwc1 $f0, 0($2)
342     // Generate:
343     //  lui $2, %hi($CPI1_0)
344     //  lwc1 $f0, %lo($CPI1_0)($2)
345     if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
346       SDValue LoVal = Addr.getOperand(1), Opnd0 = LoVal.getOperand(0);
347       if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
348           isa<JumpTableSDNode>(Opnd0)) {
349         Base = Addr.getOperand(0);
350         Offset = Opnd0;
351         return true;
352       }
353     }
354
355     // If an indexed floating point load/store can be emitted, return false.
356     if (LS &&
357         (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
358         Subtarget.hasMips32r2Or64())
359       return false;
360   }
361
362   Base   = Addr;
363   Offset = CurDAG->getTargetConstant(0, ValTy);
364   return true;
365 }
366
367 /// Select multiply instructions.
368 std::pair<SDNode*, SDNode*>
369 MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
370                              bool HasLo, bool HasHi) {
371   SDNode *Lo = 0, *Hi = 0;
372   SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
373                                        N->getOperand(1));
374   SDValue InFlag = SDValue(Mul, 0);
375
376   if (HasLo) {
377     Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
378                                 Ty, MVT::Glue, InFlag);
379     InFlag = SDValue(Lo, 1);
380   }
381   if (HasHi)
382     Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
383                                 Ty, InFlag);
384
385   return std::make_pair(Lo, Hi);
386 }
387
388
389 /// Select instructions not customized! Used for
390 /// expanded, promoted and normal instructions
391 SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
392   unsigned Opcode = Node->getOpcode();
393   DebugLoc dl = Node->getDebugLoc();
394
395   // Dump information about the Node being selected
396   DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
397
398   // If we have a custom node, we already have selected!
399   if (Node->isMachineOpcode()) {
400     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
401     return NULL;
402   }
403
404   ///
405   // Instruction Selection not handled by the auto-generated
406   // tablegen selection should be handled here.
407   ///
408   EVT NodeTy = Node->getValueType(0);
409   unsigned MultOpc;
410
411   switch(Opcode) {
412   default: break;
413
414   case ISD::SUBE:
415   case ISD::ADDE: {
416     SDValue InFlag = Node->getOperand(2), CmpLHS;
417     unsigned Opc = InFlag.getOpcode(); (void)Opc;
418     assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
419             (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
420            "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
421
422     unsigned MOp;
423     if (Opcode == ISD::ADDE) {
424       CmpLHS = InFlag.getValue(0);
425       MOp = Mips::ADDu;
426     } else {
427       CmpLHS = InFlag.getOperand(0);
428       MOp = Mips::SUBu;
429     }
430
431     SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
432
433     SDValue LHS = Node->getOperand(0);
434     SDValue RHS = Node->getOperand(1);
435
436     EVT VT = LHS.getValueType();
437     SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
438     SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
439                                               SDValue(Carry,0), RHS);
440
441     return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
442                                 LHS, SDValue(AddCarry,0));
443   }
444
445   /// Mul with two results
446   case ISD::SMUL_LOHI:
447   case ISD::UMUL_LOHI: {
448     if (NodeTy == MVT::i32)
449       MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
450     else
451       MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
452
453     std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
454                                                   true, true);
455
456     if (!SDValue(Node, 0).use_empty())
457       ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
458
459     if (!SDValue(Node, 1).use_empty())
460       ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
461
462     return NULL;
463   }
464
465   /// Special Muls
466   case ISD::MUL: {
467     // Mips32 has a 32-bit three operand mul instruction.
468     if (Subtarget.hasMips32() && NodeTy == MVT::i32)
469       break;
470     return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
471                       dl, NodeTy, true, false).first;
472   }
473   case ISD::MULHS:
474   case ISD::MULHU: {
475     if (NodeTy == MVT::i32)
476       MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
477     else
478       MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
479
480     return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
481   }
482
483   // Get target GOT address.
484   case ISD::GLOBAL_OFFSET_TABLE:
485     return getGlobalBaseReg();
486
487   case ISD::ConstantFP: {
488     ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
489     if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
490       if (Subtarget.hasMips64()) {
491         SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
492                                               Mips::ZERO_64, MVT::i64);
493         return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
494       }
495
496       SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
497                                             Mips::ZERO, MVT::i32);
498       return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
499                                     Zero);
500     }
501     break;
502   }
503
504   case ISD::Constant: {
505     const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
506     unsigned Size = CN->getValueSizeInBits(0);
507
508     if (Size == 32)
509       break;
510
511     MipsAnalyzeImmediate AnalyzeImm;
512     int64_t Imm = CN->getSExtValue();
513
514     const MipsAnalyzeImmediate::InstSeq &Seq =
515       AnalyzeImm.Analyze(Imm, Size, false);
516
517     MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
518     DebugLoc DL = CN->getDebugLoc();
519     SDNode *RegOpnd;
520     SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
521                                                 MVT::i64);
522
523     // The first instruction can be a LUi which is different from other
524     // instructions (ADDiu, ORI and SLL) in that it does not have a register
525     // operand.
526     if (Inst->Opc == Mips::LUi64)
527       RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
528     else
529       RegOpnd =
530         CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
531                                CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
532                                ImmOpnd);
533
534     // The remaining instructions in the sequence are handled here.
535     for (++Inst; Inst != Seq.end(); ++Inst) {
536       ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
537                                           MVT::i64);
538       RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
539                                        SDValue(RegOpnd, 0), ImmOpnd);
540     }
541
542     return RegOpnd;
543   }
544
545   case MipsISD::ThreadPointer: {
546     EVT PtrVT = TLI.getPointerTy();
547     unsigned RdhwrOpc, SrcReg, DestReg;
548
549     if (PtrVT == MVT::i32) {
550       RdhwrOpc = Mips::RDHWR;
551       SrcReg = Mips::HWR29;
552       DestReg = Mips::V1;
553     } else {
554       RdhwrOpc = Mips::RDHWR64;
555       SrcReg = Mips::HWR29_64;
556       DestReg = Mips::V1_64;
557     }
558
559     SDNode *Rdhwr =
560       CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
561                              Node->getValueType(0),
562                              CurDAG->getRegister(SrcReg, PtrVT));
563     SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
564                                          SDValue(Rdhwr, 0));
565     SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
566     ReplaceUses(SDValue(Node, 0), ResNode);
567     return ResNode.getNode();
568   }
569   }
570
571   // Select the default instruction
572   SDNode *ResNode = SelectCode(Node);
573
574   DEBUG(errs() << "=> ");
575   if (ResNode == NULL || ResNode == Node)
576     DEBUG(Node->dump(CurDAG));
577   else
578     DEBUG(ResNode->dump(CurDAG));
579   DEBUG(errs() << "\n");
580   return ResNode;
581 }
582
583 bool MipsDAGToDAGISel::
584 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
585                              std::vector<SDValue> &OutOps) {
586   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
587   OutOps.push_back(Op);
588   return false;
589 }
590
591 /// createMipsISelDag - This pass converts a legalized DAG into a
592 /// MIPS-specific DAG, ready for instruction scheduling.
593 FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
594   return new MipsDAGToDAGISel(TM);
595 }