Define new itin classes for ARM <-> VFP reg moves to distinguish from NEON ops. Defin...
[oota-llvm.git] / lib / Target / SystemZ / SystemZISelDAGToDAG.cpp
1 //==-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ ---===//
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 SystemZ target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SystemZ.h"
15 #include "SystemZISelLowering.h"
16 #include "SystemZTargetMachine.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/SelectionDAGISel.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33
34 static const unsigned subreg_even32 = 1;
35 static const unsigned subreg_odd32  = 2;
36 static const unsigned subreg_even   = 3;
37 static const unsigned subreg_odd    = 4;
38
39 namespace {
40   /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
41   /// instead of register numbers for the leaves of the matched tree.
42   struct SystemZRRIAddressMode {
43     enum {
44       RegBase,
45       FrameIndexBase
46     } BaseType;
47
48     struct {            // This is really a union, discriminated by BaseType!
49       SDValue Reg;
50       int FrameIndex;
51     } Base;
52
53     SDValue IndexReg;
54     int64_t Disp;
55     bool isRI;
56
57     SystemZRRIAddressMode(bool RI = false)
58       : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) {
59     }
60
61     void dump() {
62       errs() << "SystemZRRIAddressMode " << this << '\n';
63       if (BaseType == RegBase) {
64         errs() << "Base.Reg ";
65         if (Base.Reg.getNode() != 0)
66           Base.Reg.getNode()->dump();
67         else
68           errs() << "nul";
69         errs() << '\n';
70       } else {
71         errs() << " Base.FrameIndex " << Base.FrameIndex << '\n';
72       }
73       if (!isRI) {
74         errs() << "IndexReg ";
75         if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
76         else errs() << "nul";
77       }
78       errs() << " Disp " << Disp << '\n';
79     }
80   };
81 }
82
83 /// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
84 /// instructions for SelectionDAG operations.
85 ///
86 namespace {
87   class SystemZDAGToDAGISel : public SelectionDAGISel {
88     SystemZTargetLowering &Lowering;
89     const SystemZSubtarget &Subtarget;
90
91     void getAddressOperandsRI(const SystemZRRIAddressMode &AM,
92                             SDValue &Base, SDValue &Disp);
93     void getAddressOperands(const SystemZRRIAddressMode &AM,
94                             SDValue &Base, SDValue &Disp,
95                             SDValue &Index);
96
97   public:
98     SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
99       : SelectionDAGISel(TM, OptLevel),
100         Lowering(*TM.getTargetLowering()),
101         Subtarget(*TM.getSubtargetImpl()) { }
102
103     virtual const char *getPassName() const {
104       return "SystemZ DAG->DAG Pattern Instruction Selection";
105     }
106
107     /// getI8Imm - Return a target constant with the specified value, of type
108     /// i8.
109     inline SDValue getI8Imm(uint64_t Imm) {
110       return CurDAG->getTargetConstant(Imm, MVT::i8);
111     }
112
113     /// getI16Imm - Return a target constant with the specified value, of type
114     /// i16.
115     inline SDValue getI16Imm(uint64_t Imm) {
116       return CurDAG->getTargetConstant(Imm, MVT::i16);
117     }
118
119     /// getI32Imm - Return a target constant with the specified value, of type
120     /// i32.
121     inline SDValue getI32Imm(uint64_t Imm) {
122       return CurDAG->getTargetConstant(Imm, MVT::i32);
123     }
124
125     // Include the pieces autogenerated from the target description.
126     #include "SystemZGenDAGISel.inc"
127
128   private:
129     bool SelectAddrRI12Only(SDNode *Op, SDValue& Addr,
130                             SDValue &Base, SDValue &Disp);
131     bool SelectAddrRI12(SDNode *Op, SDValue& Addr,
132                         SDValue &Base, SDValue &Disp,
133                         bool is12BitOnly = false);
134     bool SelectAddrRI(SDNode *Op, SDValue& Addr,
135                       SDValue &Base, SDValue &Disp);
136     bool SelectAddrRRI12(SDNode *Op, SDValue Addr,
137                          SDValue &Base, SDValue &Disp, SDValue &Index);
138     bool SelectAddrRRI20(SDNode *Op, SDValue Addr,
139                          SDValue &Base, SDValue &Disp, SDValue &Index);
140     bool SelectLAAddr(SDNode *Op, SDValue Addr,
141                       SDValue &Base, SDValue &Disp, SDValue &Index);
142
143     SDNode *Select(SDNode *Node);
144
145     bool TryFoldLoad(SDNode *P, SDValue N,
146                      SDValue &Base, SDValue &Disp, SDValue &Index);
147
148     bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
149                       bool is12Bit, unsigned Depth = 0);
150     bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
151     bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
152                         bool is12Bit);
153   };
154 }  // end anonymous namespace
155
156 /// createSystemZISelDag - This pass converts a legalized DAG into a
157 /// SystemZ-specific DAG, ready for instruction scheduling.
158 ///
159 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
160                                         CodeGenOpt::Level OptLevel) {
161   return new SystemZDAGToDAGISel(TM, OptLevel);
162 }
163
164 /// isImmSExt20 - This method tests to see if the node is either a 32-bit
165 /// or 64-bit immediate, and if the value can be accurately represented as a
166 /// sign extension from a 20-bit value. If so, this returns true and the
167 /// immediate.
168 static bool isImmSExt20(int64_t Val, int64_t &Imm) {
169   if (Val >= -524288 && Val <= 524287) {
170     Imm = Val;
171     return true;
172   }
173   return false;
174 }
175
176 /// isImmZExt12 - This method tests to see if the node is either a 32-bit
177 /// or 64-bit immediate, and if the value can be accurately represented as a
178 /// zero extension from a 12-bit value. If so, this returns true and the
179 /// immediate.
180 static bool isImmZExt12(int64_t Val, int64_t &Imm) {
181   if (Val >= 0 && Val <= 0xFFF) {
182     Imm = Val;
183     return true;
184   }
185   return false;
186 }
187
188 /// MatchAddress - Add the specified node to the specified addressing mode,
189 /// returning true if it cannot be done.  This just pattern matches for the
190 /// addressing mode.
191 bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
192                                        bool is12Bit, unsigned Depth) {
193   DebugLoc dl = N.getDebugLoc();
194   DEBUG(errs() << "MatchAddress: "; AM.dump());
195   // Limit recursion.
196   if (Depth > 5)
197     return MatchAddressBase(N, AM);
198
199   // FIXME: We can perform better here. If we have something like
200   // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
201   // imm into addressing mode.
202   switch (N.getOpcode()) {
203   default: break;
204   case ISD::Constant: {
205     int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
206     int64_t Imm = 0;
207     bool Match = (is12Bit ?
208                   isImmZExt12(AM.Disp + Val, Imm) :
209                   isImmSExt20(AM.Disp + Val, Imm));
210     if (Match) {
211       AM.Disp = Imm;
212       return false;
213     }
214     break;
215   }
216
217   case ISD::FrameIndex:
218     if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
219         AM.Base.Reg.getNode() == 0) {
220       AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
221       AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
222       return false;
223     }
224     break;
225
226   case ISD::SUB: {
227     // Given A-B, if A can be completely folded into the address and
228     // the index field with the index field unused, use -B as the index.
229     // This is a win if a has multiple parts that can be folded into
230     // the address. Also, this saves a mov if the base register has
231     // other uses, since it avoids a two-address sub instruction, however
232     // it costs an additional mov if the index register has other uses.
233
234     // Test if the LHS of the sub can be folded.
235     SystemZRRIAddressMode Backup = AM;
236     if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
237       AM = Backup;
238       break;
239     }
240     // Test if the index field is free for use.
241     if (AM.IndexReg.getNode() || AM.isRI) {
242       AM = Backup;
243       break;
244     }
245
246     // If the base is a register with multiple uses, this transformation may
247     // save a mov. Otherwise it's probably better not to do it.
248     if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
249         (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
250       AM = Backup;
251       break;
252     }
253
254     // Ok, the transformation is legal and appears profitable. Go for it.
255     SDValue RHS = N.getNode()->getOperand(1);
256     SDValue Zero = CurDAG->getConstant(0, N.getValueType());
257     SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
258     AM.IndexReg = Neg;
259
260     // Insert the new nodes into the topological ordering.
261     if (Zero.getNode()->getNodeId() == -1 ||
262         Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
263       CurDAG->RepositionNode(N.getNode(), Zero.getNode());
264       Zero.getNode()->setNodeId(N.getNode()->getNodeId());
265     }
266     if (Neg.getNode()->getNodeId() == -1 ||
267         Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
268       CurDAG->RepositionNode(N.getNode(), Neg.getNode());
269       Neg.getNode()->setNodeId(N.getNode()->getNodeId());
270     }
271     return false;
272   }
273
274   case ISD::ADD: {
275     SystemZRRIAddressMode Backup = AM;
276     if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
277         !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
278       return false;
279     AM = Backup;
280     if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
281         !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
282       return false;
283     AM = Backup;
284
285     // If we couldn't fold both operands into the address at the same time,
286     // see if we can just put each operand into a register and fold at least
287     // the add.
288     if (!AM.isRI &&
289         AM.BaseType == SystemZRRIAddressMode::RegBase &&
290         !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
291       AM.Base.Reg = N.getNode()->getOperand(0);
292       AM.IndexReg = N.getNode()->getOperand(1);
293       return false;
294     }
295     break;
296   }
297
298   case ISD::OR:
299     // Handle "X | C" as "X + C" iff X is known to have C bits clear.
300     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
301       SystemZRRIAddressMode Backup = AM;
302       int64_t Offset = CN->getSExtValue();
303       int64_t Imm = 0;
304       bool MatchOffset = (is12Bit ?
305                           isImmZExt12(AM.Disp + Offset, Imm) :
306                           isImmSExt20(AM.Disp + Offset, Imm));
307       // The resultant disp must fit in 12 or 20-bits.
308       if (MatchOffset &&
309           // LHS should be an addr mode.
310           !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
311           // Check to see if the LHS & C is zero.
312           CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
313         AM.Disp = Imm;
314         return false;
315       }
316       AM = Backup;
317     }
318     break;
319   }
320
321   return MatchAddressBase(N, AM);
322 }
323
324 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
325 /// specified addressing mode without any further recursion.
326 bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
327                                            SystemZRRIAddressMode &AM) {
328   // Is the base register already occupied?
329   if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
330     // If so, check to see if the index register is set.
331     if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
332       AM.IndexReg = N;
333       return false;
334     }
335
336     // Otherwise, we cannot select it.
337     return true;
338   }
339
340   // Default, generate it as a register.
341   AM.BaseType = SystemZRRIAddressMode::RegBase;
342   AM.Base.Reg = N;
343   return false;
344 }
345
346 void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
347                                                SDValue &Base, SDValue &Disp) {
348   if (AM.BaseType == SystemZRRIAddressMode::RegBase)
349     Base = AM.Base.Reg;
350   else
351     Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
352   Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
353 }
354
355 void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
356                                              SDValue &Base, SDValue &Disp,
357                                              SDValue &Index) {
358   getAddressOperandsRI(AM, Base, Disp);
359   Index = AM.IndexReg;
360 }
361
362 /// Returns true if the address can be represented by a base register plus
363 /// an unsigned 12-bit displacement [r+imm].
364 bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDNode *Op, SDValue& Addr,
365                                              SDValue &Base, SDValue &Disp) {
366   return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true);
367 }
368
369 bool SystemZDAGToDAGISel::SelectAddrRI12(SDNode *Op, SDValue& Addr,
370                                          SDValue &Base, SDValue &Disp,
371                                          bool is12BitOnly) {
372   SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
373   bool Done = false;
374
375   if (!Addr.hasOneUse()) {
376     unsigned Opcode = Addr.getOpcode();
377     if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
378       // If we are able to fold N into addressing mode, then we'll allow it even
379       // if N has multiple uses. In general, addressing computation is used as
380       // addresses by all of its uses. But watch out for CopyToReg uses, that
381       // means the address computation is liveout. It will be computed by a LA
382       // so we want to avoid computing the address twice.
383       for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
384              UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
385         if (UI->getOpcode() == ISD::CopyToReg) {
386           MatchAddressBase(Addr, AM12);
387           Done = true;
388           break;
389         }
390       }
391     }
392   }
393   if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
394     return false;
395
396   // Check, whether we can match stuff using 20-bit displacements
397   if (!Done && !is12BitOnly &&
398       !MatchAddress(Addr, AM20, /* is12Bit */ false))
399     if (AM12.Disp == 0 && AM20.Disp != 0)
400       return false;
401
402   DEBUG(errs() << "MatchAddress (final): "; AM12.dump());
403
404   EVT VT = Addr.getValueType();
405   if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
406     if (!AM12.Base.Reg.getNode())
407       AM12.Base.Reg = CurDAG->getRegister(0, VT);
408   }
409
410   assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
411
412   getAddressOperandsRI(AM12, Base, Disp);
413
414   return true;
415 }
416
417 /// Returns true if the address can be represented by a base register plus
418 /// a signed 20-bit displacement [r+imm].
419 bool SystemZDAGToDAGISel::SelectAddrRI(SDNode *Op, SDValue& Addr,
420                                        SDValue &Base, SDValue &Disp) {
421   SystemZRRIAddressMode AM(/*isRI*/true);
422   bool Done = false;
423
424   if (!Addr.hasOneUse()) {
425     unsigned Opcode = Addr.getOpcode();
426     if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
427       // If we are able to fold N into addressing mode, then we'll allow it even
428       // if N has multiple uses. In general, addressing computation is used as
429       // addresses by all of its uses. But watch out for CopyToReg uses, that
430       // means the address computation is liveout. It will be computed by a LA
431       // so we want to avoid computing the address twice.
432       for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
433              UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
434         if (UI->getOpcode() == ISD::CopyToReg) {
435           MatchAddressBase(Addr, AM);
436           Done = true;
437           break;
438         }
439       }
440     }
441   }
442   if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
443     return false;
444
445   DEBUG(errs() << "MatchAddress (final): "; AM.dump());
446
447   EVT VT = Addr.getValueType();
448   if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
449     if (!AM.Base.Reg.getNode())
450       AM.Base.Reg = CurDAG->getRegister(0, VT);
451   }
452
453   assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
454
455   getAddressOperandsRI(AM, Base, Disp);
456
457   return true;
458 }
459
460 /// Returns true if the address can be represented by a base register plus
461 /// index register plus an unsigned 12-bit displacement [base + idx + imm].
462 bool SystemZDAGToDAGISel::SelectAddrRRI12(SDNode *Op, SDValue Addr,
463                                 SDValue &Base, SDValue &Disp, SDValue &Index) {
464   SystemZRRIAddressMode AM20, AM12;
465   bool Done = false;
466
467   if (!Addr.hasOneUse()) {
468     unsigned Opcode = Addr.getOpcode();
469     if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
470       // If we are able to fold N into addressing mode, then we'll allow it even
471       // if N has multiple uses. In general, addressing computation is used as
472       // addresses by all of its uses. But watch out for CopyToReg uses, that
473       // means the address computation is liveout. It will be computed by a LA
474       // so we want to avoid computing the address twice.
475       for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
476              UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
477         if (UI->getOpcode() == ISD::CopyToReg) {
478           MatchAddressBase(Addr, AM12);
479           Done = true;
480           break;
481         }
482       }
483     }
484   }
485   if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
486     return false;
487
488   // Check, whether we can match stuff using 20-bit displacements
489   if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
490     if (AM12.Disp == 0 && AM20.Disp != 0)
491       return false;
492
493   DEBUG(errs() << "MatchAddress (final): "; AM12.dump());
494
495   EVT VT = Addr.getValueType();
496   if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
497     if (!AM12.Base.Reg.getNode())
498       AM12.Base.Reg = CurDAG->getRegister(0, VT);
499   }
500
501   if (!AM12.IndexReg.getNode())
502     AM12.IndexReg = CurDAG->getRegister(0, VT);
503
504   getAddressOperands(AM12, Base, Disp, Index);
505
506   return true;
507 }
508
509 /// Returns true if the address can be represented by a base register plus
510 /// index register plus a signed 20-bit displacement [base + idx + imm].
511 bool SystemZDAGToDAGISel::SelectAddrRRI20(SDNode *Op, SDValue Addr,
512                                 SDValue &Base, SDValue &Disp, SDValue &Index) {
513   SystemZRRIAddressMode AM;
514   bool Done = false;
515
516   if (!Addr.hasOneUse()) {
517     unsigned Opcode = Addr.getOpcode();
518     if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
519       // If we are able to fold N into addressing mode, then we'll allow it even
520       // if N has multiple uses. In general, addressing computation is used as
521       // addresses by all of its uses. But watch out for CopyToReg uses, that
522       // means the address computation is liveout. It will be computed by a LA
523       // so we want to avoid computing the address twice.
524       for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
525              UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
526         if (UI->getOpcode() == ISD::CopyToReg) {
527           MatchAddressBase(Addr, AM);
528           Done = true;
529           break;
530         }
531       }
532     }
533   }
534   if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
535     return false;
536
537   DEBUG(errs() << "MatchAddress (final): "; AM.dump());
538
539   EVT VT = Addr.getValueType();
540   if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
541     if (!AM.Base.Reg.getNode())
542       AM.Base.Reg = CurDAG->getRegister(0, VT);
543   }
544
545   if (!AM.IndexReg.getNode())
546     AM.IndexReg = CurDAG->getRegister(0, VT);
547
548   getAddressOperands(AM, Base, Disp, Index);
549
550   return true;
551 }
552
553 /// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
554 /// mode it matches can be cost effectively emitted as an LA/LAY instruction.
555 bool SystemZDAGToDAGISel::SelectLAAddr(SDNode *Op, SDValue Addr,
556                                   SDValue &Base, SDValue &Disp, SDValue &Index) {
557   SystemZRRIAddressMode AM;
558
559   if (MatchAddress(Addr, AM, false))
560     return false;
561
562   EVT VT = Addr.getValueType();
563   unsigned Complexity = 0;
564   if (AM.BaseType == SystemZRRIAddressMode::RegBase)
565     if (AM.Base.Reg.getNode())
566       Complexity = 1;
567     else
568       AM.Base.Reg = CurDAG->getRegister(0, VT);
569   else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
570     Complexity = 4;
571
572   if (AM.IndexReg.getNode())
573     Complexity += 1;
574   else
575     AM.IndexReg = CurDAG->getRegister(0, VT);
576
577   if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
578     Complexity += 1;
579
580   if (Complexity > 2) {
581     getAddressOperands(AM, Base, Disp, Index);
582     return true;
583   }
584
585   return false;
586 }
587
588 bool SystemZDAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
589                                  SDValue &Base, SDValue &Disp, SDValue &Index) {
590   if (ISD::isNON_EXTLoad(N.getNode()) &&
591       IsLegalToFold(N, P, P))
592     return SelectAddrRRI20(P, N.getOperand(1), Base, Disp, Index);
593   return false;
594 }
595
596 SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
597   EVT NVT = Node->getValueType(0);
598   DebugLoc dl = Node->getDebugLoc();
599   unsigned Opcode = Node->getOpcode();
600
601   // Dump information about the Node being selected
602   DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
603
604   // If we have a custom node, we already have selected!
605   if (Node->isMachineOpcode()) {
606     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
607     return NULL; // Already selected.
608   }
609
610   switch (Opcode) {
611   default: break;
612   case ISD::SDIVREM: {
613     unsigned Opc, MOpc;
614     SDValue N0 = Node->getOperand(0);
615     SDValue N1 = Node->getOperand(1);
616
617     EVT ResVT;
618     bool is32Bit = false;
619     switch (NVT.getSimpleVT().SimpleTy) {
620     default: assert(0 && "Unsupported VT!");
621     case MVT::i32:
622       Opc = SystemZ::SDIVREM32r; MOpc = SystemZ::SDIVREM32m;
623       ResVT = MVT::v2i64;
624       is32Bit = true;
625       break;
626     case MVT::i64:
627       Opc = SystemZ::SDIVREM64r; MOpc = SystemZ::SDIVREM64m;
628       ResVT = MVT::v2i64;
629       break;
630     }
631
632     SDValue Tmp0, Tmp1, Tmp2;
633     bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2);
634
635     // Prepare the dividend
636     SDNode *Dividend;
637     if (is32Bit)
638       Dividend = CurDAG->getMachineNode(SystemZ::MOVSX64rr32, dl, MVT::i64, N0);
639     else
640       Dividend = N0.getNode();
641
642     // Insert prepared dividend into suitable 'subreg'
643     SDNode *Tmp = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
644                                          dl, ResVT);
645     Dividend =
646       CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl, ResVT,
647                              SDValue(Tmp, 0), SDValue(Dividend, 0),
648                              CurDAG->getTargetConstant(subreg_odd, MVT::i32));
649
650     SDNode *Result;
651     SDValue DivVal = SDValue(Dividend, 0);
652     if (foldedLoad) {
653       SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
654       Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other,
655                                       Ops, array_lengthof(Ops));
656       // Update the chain.
657       ReplaceUses(N1.getValue(1), SDValue(Result, 1));
658     } else {
659       Result = CurDAG->getMachineNode(Opc, dl, ResVT, SDValue(Dividend, 0), N1);
660     }
661
662     // Copy the division (odd subreg) result, if it is needed.
663     if (!SDValue(Node, 0).use_empty()) {
664       unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
665       SDNode *Div = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
666                                            dl, NVT,
667                                            SDValue(Result, 0),
668                                            CurDAG->getTargetConstant(SubRegIdx,
669                                                                      MVT::i32));
670
671       ReplaceUses(SDValue(Node, 0), SDValue(Div, 0));
672       DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
673     }
674
675     // Copy the remainder (even subreg) result, if it is needed.
676     if (!SDValue(Node, 1).use_empty()) {
677       unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even);
678       SDNode *Rem = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
679                                            dl, NVT,
680                                            SDValue(Result, 0),
681                                            CurDAG->getTargetConstant(SubRegIdx,
682                                                                      MVT::i32));
683
684       ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0));
685       DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
686     }
687
688     return NULL;
689   }
690   case ISD::UDIVREM: {
691     unsigned Opc, MOpc, ClrOpc;
692     SDValue N0 = Node->getOperand(0);
693     SDValue N1 = Node->getOperand(1);
694     EVT ResVT;
695
696     bool is32Bit = false;
697     switch (NVT.getSimpleVT().SimpleTy) {
698     default: assert(0 && "Unsupported VT!");
699     case MVT::i32:
700       Opc = SystemZ::UDIVREM32r; MOpc = SystemZ::UDIVREM32m;
701       ClrOpc = SystemZ::MOV64Pr0_even;
702       ResVT = MVT::v2i32;
703       is32Bit = true;
704       break;
705     case MVT::i64:
706       Opc = SystemZ::UDIVREM64r; MOpc = SystemZ::UDIVREM64m;
707       ClrOpc = SystemZ::MOV128r0_even;
708       ResVT = MVT::v2i64;
709       break;
710     }
711
712     SDValue Tmp0, Tmp1, Tmp2;
713     bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2);
714
715     // Prepare the dividend
716     SDNode *Dividend = N0.getNode();
717
718     // Insert prepared dividend into suitable 'subreg'
719     SDNode *Tmp = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
720                                          dl, ResVT);
721     {
722       unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
723       Dividend =
724         CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl, ResVT,
725                                SDValue(Tmp, 0), SDValue(Dividend, 0),
726                                CurDAG->getTargetConstant(SubRegIdx, MVT::i32));
727     }
728
729     // Zero out even subreg
730     Dividend = CurDAG->getMachineNode(ClrOpc, dl, ResVT, SDValue(Dividend, 0));
731
732     SDValue DivVal = SDValue(Dividend, 0);
733     SDNode *Result;
734     if (foldedLoad) {
735       SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
736       Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other,
737                                       Ops, array_lengthof(Ops));
738       // Update the chain.
739       ReplaceUses(N1.getValue(1), SDValue(Result, 1));
740     } else {
741       Result = CurDAG->getMachineNode(Opc, dl, ResVT, DivVal, N1);
742     }
743
744     // Copy the division (odd subreg) result, if it is needed.
745     if (!SDValue(Node, 0).use_empty()) {
746       unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
747       SDNode *Div = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
748                                            dl, NVT,
749                                            SDValue(Result, 0),
750                                            CurDAG->getTargetConstant(SubRegIdx,
751                                                                      MVT::i32));
752       ReplaceUses(SDValue(Node, 0), SDValue(Div, 0));
753       DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
754     }
755
756     // Copy the remainder (even subreg) result, if it is needed.
757     if (!SDValue(Node, 1).use_empty()) {
758       unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even);
759       SDNode *Rem = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
760                                            dl, NVT,
761                                            SDValue(Result, 0),
762                                            CurDAG->getTargetConstant(SubRegIdx,
763                                                                      MVT::i32));
764       ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0));
765       DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
766     }
767
768     return NULL;
769   }
770   }
771
772   // Select the default instruction
773   SDNode *ResNode = SelectCode(Node);
774
775   DEBUG(errs() << "=> ";
776         if (ResNode == NULL || ResNode == Node)
777           Node->dump(CurDAG);
778         else
779           ResNode->dump(CurDAG);
780         errs() << "\n";
781         );
782   return ResNode;
783 }