62bd46b011cd9f686d8dfd449ca19d3932d33592
[oota-llvm.git] / lib / Target / SystemZ / SystemZISelLowering.cpp
1 //===-- SystemZISelLowering.cpp - SystemZ DAG Lowering Implementation  -----==//
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 implements the SystemZTargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "systemz-lower"
15
16 #include "SystemZISelLowering.h"
17 #include "SystemZ.h"
18 #include "SystemZTargetMachine.h"
19 #include "SystemZSubtarget.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/GlobalAlias.h"
26 #include "llvm/CodeGen/CallingConvLower.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/PseudoSourceValue.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/CodeGen/ValueTypes.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/ADT/VectorExtras.h"
36 using namespace llvm;
37
38 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
39   TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
40
41   RegInfo = TM.getRegisterInfo();
42
43   // Set up the register classes.
44   addRegisterClass(MVT::i32,  SystemZ::GR32RegisterClass);
45   addRegisterClass(MVT::i64,  SystemZ::GR64RegisterClass);
46   addRegisterClass(MVT::i128, SystemZ::GR128RegisterClass);
47
48   // Compute derived properties from the register classes
49   computeRegisterProperties();
50
51   // Set shifts properties
52   setShiftAmountFlavor(Extend);
53   setShiftAmountType(MVT::i32);
54
55   // Provide all sorts of operation actions
56   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
57   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
58   setLoadExtAction(ISD::EXTLOAD,  MVT::i1, Promote);
59
60   setStackPointerRegisterToSaveRestore(SystemZ::R15D);
61   setSchedulingPreference(SchedulingForLatency);
62
63   setOperationAction(ISD::RET,              MVT::Other, Custom);
64
65   setOperationAction(ISD::BRCOND,           MVT::Other, Expand);
66   setOperationAction(ISD::BR_CC,            MVT::i32, Custom);
67   setOperationAction(ISD::BR_CC,            MVT::i64, Custom);
68
69   // FIXME: Can we lower these 2 efficiently?
70   setOperationAction(ISD::SETCC,            MVT::i32, Expand);
71   setOperationAction(ISD::SETCC,            MVT::i64, Expand);
72   setOperationAction(ISD::SELECT,           MVT::i32, Expand);
73   setOperationAction(ISD::SELECT,           MVT::i64, Expand);
74   setOperationAction(ISD::SELECT_CC,        MVT::i32, Custom);
75   setOperationAction(ISD::SELECT_CC,        MVT::i64, Custom);
76
77   // Funny enough: we don't have 64-bit signed versions of these stuff, but have
78   // unsigned.
79   setOperationAction(ISD::MULHS,            MVT::i64, Expand);
80   setOperationAction(ISD::SMUL_LOHI,        MVT::i64, Expand);
81 }
82
83 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
84   switch (Op.getOpcode()) {
85   case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
86   case ISD::RET:              return LowerRET(Op, DAG);
87   case ISD::CALL:             return LowerCALL(Op, DAG);
88   case ISD::BR_CC:            return LowerBR_CC(Op, DAG);
89   case ISD::SELECT_CC:        return LowerSELECT_CC(Op, DAG);
90   default:
91     assert(0 && "unimplemented operand");
92     return SDValue();
93   }
94 }
95
96 //===----------------------------------------------------------------------===//
97 //                      Calling Convention Implementation
98 //===----------------------------------------------------------------------===//
99
100 #include "SystemZGenCallingConv.inc"
101
102 SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
103                                                      SelectionDAG &DAG) {
104   unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
105   switch (CC) {
106   default:
107     assert(0 && "Unsupported calling convention");
108   case CallingConv::C:
109   case CallingConv::Fast:
110     return LowerCCCArguments(Op, DAG);
111   }
112 }
113
114 SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
115   CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
116   unsigned CallingConv = TheCall->getCallingConv();
117   switch (CallingConv) {
118   default:
119     assert(0 && "Unsupported calling convention");
120   case CallingConv::Fast:
121   case CallingConv::C:
122     return LowerCCCCallTo(Op, DAG, CallingConv);
123   }
124 }
125
126 /// LowerCCCArguments - transform physical registers into virtual registers and
127 /// generate load operations for arguments places on the stack.
128 // FIXME: struct return stuff
129 // FIXME: varargs
130 SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
131                                                  SelectionDAG &DAG) {
132   MachineFunction &MF = DAG.getMachineFunction();
133   MachineFrameInfo *MFI = MF.getFrameInfo();
134   MachineRegisterInfo &RegInfo = MF.getRegInfo();
135   SDValue Root = Op.getOperand(0);
136   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
137   unsigned CC = MF.getFunction()->getCallingConv();
138   DebugLoc dl = Op.getDebugLoc();
139
140   // Assign locations to all of the incoming arguments.
141   SmallVector<CCValAssign, 16> ArgLocs;
142   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
143   CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
144
145   assert(!isVarArg && "Varargs not supported yet");
146
147   SmallVector<SDValue, 16> ArgValues;
148   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
149     CCValAssign &VA = ArgLocs[i];
150     if (VA.isRegLoc()) {
151       // Arguments passed in registers
152       MVT RegVT = VA.getLocVT();
153       switch (RegVT.getSimpleVT()) {
154       default:
155         cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
156              << RegVT.getSimpleVT()
157              << "\n";
158         abort();
159       case MVT::i64:
160         unsigned VReg =
161           RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
162         RegInfo.addLiveIn(VA.getLocReg(), VReg);
163         SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
164
165         // If this is an 8/16/32-bit value, it is really passed promoted to 64
166         // bits. Insert an assert[sz]ext to capture this, then truncate to the
167         // right size.
168         if (VA.getLocInfo() == CCValAssign::SExt)
169           ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
170                                  DAG.getValueType(VA.getValVT()));
171         else if (VA.getLocInfo() == CCValAssign::ZExt)
172           ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
173                                  DAG.getValueType(VA.getValVT()));
174
175         if (VA.getLocInfo() != CCValAssign::Full)
176           ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
177
178         ArgValues.push_back(ArgValue);
179       }
180     } else {
181       // Sanity check
182       assert(VA.isMemLoc());
183       // Load the argument to a virtual register
184       unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
185       if (ObjSize > 8) {
186         cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
187              << VA.getLocVT().getSimpleVT()
188              << "\n";
189       }
190       // Create the frame index object for this incoming parameter...
191       int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
192
193       // Create the SelectionDAG nodes corresponding to a load
194       //from this parameter
195       SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
196       ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
197                                       PseudoSourceValue::getFixedStack(FI), 0));
198     }
199   }
200
201   ArgValues.push_back(Root);
202
203   // Return the new list of results.
204   return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
205                      &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
206 }
207
208 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
209 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
210 /// TODO: sret.
211 SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
212                                               unsigned CC) {
213   CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
214   SDValue Chain  = TheCall->getChain();
215   SDValue Callee = TheCall->getCallee();
216   bool isVarArg  = TheCall->isVarArg();
217   DebugLoc dl = Op.getDebugLoc();
218   MachineFunction &MF = DAG.getMachineFunction();
219
220   // Offset to first argument stack slot.
221   const unsigned FirstArgOffset = 160;
222
223   // Analyze operands of the call, assigning locations to each operand.
224   SmallVector<CCValAssign, 16> ArgLocs;
225   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
226
227   CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
228
229   // Get a count of how many bytes are to be pushed on the stack.
230   unsigned NumBytes = CCInfo.getNextStackOffset();
231
232   Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
233                                                       getPointerTy(), true));
234
235   SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
236   SmallVector<SDValue, 12> MemOpChains;
237   SDValue StackPtr;
238
239   // Walk the register/memloc assignments, inserting copies/loads.
240   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
241     CCValAssign &VA = ArgLocs[i];
242
243     // Arguments start after the 5 first operands of ISD::CALL
244     SDValue Arg = TheCall->getArg(i);
245
246     // Promote the value if needed.
247     switch (VA.getLocInfo()) {
248       default: assert(0 && "Unknown loc info!");
249       case CCValAssign::Full: break;
250       case CCValAssign::SExt:
251         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
252         break;
253       case CCValAssign::ZExt:
254         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
255         break;
256       case CCValAssign::AExt:
257         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
258         break;
259     }
260
261     // Arguments that can be passed on register must be kept at RegsToPass
262     // vector
263     if (VA.isRegLoc()) {
264       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
265     } else {
266       assert(VA.isMemLoc());
267
268       if (StackPtr.getNode() == 0)
269         StackPtr =
270           DAG.getCopyFromReg(Chain, dl,
271                              (RegInfo->hasFP(MF) ?
272                               SystemZ::R11D : SystemZ::R15D),
273                              getPointerTy());
274
275       unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
276       SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
277                                    StackPtr,
278                                    DAG.getIntPtrConstant(Offset));
279
280       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
281                                          PseudoSourceValue::getStack(), Offset));
282     }
283   }
284
285   // Transform all store nodes into one single node because all store nodes are
286   // independent of each other.
287   if (!MemOpChains.empty())
288     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
289                         &MemOpChains[0], MemOpChains.size());
290
291   // Build a sequence of copy-to-reg nodes chained together with token chain and
292   // flag operands which copy the outgoing args into registers.  The InFlag in
293   // necessary since all emited instructions must be stuck together.
294   SDValue InFlag;
295   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
296     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
297                              RegsToPass[i].second, InFlag);
298     InFlag = Chain.getValue(1);
299   }
300
301   // If the callee is a GlobalAddress node (quite common, every direct call is)
302   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
303   // Likewise ExternalSymbol -> TargetExternalSymbol.
304   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
305     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
306   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
307     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
308
309   // Returns a chain & a flag for retval copy to use.
310   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
311   SmallVector<SDValue, 8> Ops;
312   Ops.push_back(Chain);
313   Ops.push_back(Callee);
314
315   // Add argument registers to the end of the list so that they are
316   // known live into the call.
317   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
318     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
319                                   RegsToPass[i].second.getValueType()));
320
321   if (InFlag.getNode())
322     Ops.push_back(InFlag);
323
324   Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
325   InFlag = Chain.getValue(1);
326
327   // Create the CALLSEQ_END node.
328   Chain = DAG.getCALLSEQ_END(Chain,
329                              DAG.getConstant(NumBytes, getPointerTy(), true),
330                              DAG.getConstant(0, getPointerTy(), true),
331                              InFlag);
332   InFlag = Chain.getValue(1);
333
334   // Handle result values, copying them out of physregs into vregs that we
335   // return.
336   return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
337                  Op.getResNo());
338 }
339
340 /// LowerCallResult - Lower the result values of an ISD::CALL into the
341 /// appropriate copies out of appropriate physical registers.  This assumes that
342 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
343 /// being lowered. Returns a SDNode with the same number of values as the
344 /// ISD::CALL.
345 SDNode*
346 SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
347                                        CallSDNode *TheCall,
348                                        unsigned CallingConv,
349                                        SelectionDAG &DAG) {
350   bool isVarArg = TheCall->isVarArg();
351   DebugLoc dl = TheCall->getDebugLoc();
352
353   // Assign locations to each value returned by this call.
354   SmallVector<CCValAssign, 16> RVLocs;
355   CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
356
357   CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
358   SmallVector<SDValue, 8> ResultVals;
359
360   // Copy all of the result registers out of their specified physreg.
361   for (unsigned i = 0; i != RVLocs.size(); ++i) {
362     Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
363                                RVLocs[i].getValVT(), InFlag).getValue(1);
364     InFlag = Chain.getValue(2);
365     ResultVals.push_back(Chain.getValue(0));
366   }
367
368   ResultVals.push_back(Chain);
369
370   // Merge everything together with a MERGE_VALUES node.
371   return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
372                      &ResultVals[0], ResultVals.size()).getNode();
373 }
374
375
376 SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
377   // CCValAssign - represent the assignment of the return value to a location
378   SmallVector<CCValAssign, 16> RVLocs;
379   unsigned CC   = DAG.getMachineFunction().getFunction()->getCallingConv();
380   bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
381   DebugLoc dl = Op.getDebugLoc();
382
383   // CCState - Info about the registers and stack slot.
384   CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
385
386   // Analize return values of ISD::RET
387   CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
388
389   // If this is the first return lowered for this function, add the regs to the
390   // liveout set for the function.
391   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
392     for (unsigned i = 0; i != RVLocs.size(); ++i)
393       if (RVLocs[i].isRegLoc())
394         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
395   }
396
397   // The chain is always operand #0
398   SDValue Chain = Op.getOperand(0);
399   SDValue Flag;
400
401   // Copy the result values into the output registers.
402   for (unsigned i = 0; i != RVLocs.size(); ++i) {
403     CCValAssign &VA = RVLocs[i];
404     SDValue ResValue = Op.getOperand(i*2+1);
405     assert(VA.isRegLoc() && "Can only return in registers!");
406
407     // If this is an 8/16/32-bit value, it is really should be passed promoted
408     // to 64 bits.
409     if (VA.getLocInfo() == CCValAssign::SExt)
410       ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
411     else if (VA.getLocInfo() == CCValAssign::ZExt)
412       ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
413     else if (VA.getLocInfo() == CCValAssign::AExt)
414       ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
415
416     // ISD::RET => ret chain, (regnum1,val1), ...
417     // So i*2+1 index only the regnums
418     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
419
420     // Guarantee that all emitted copies are stuck together,
421     // avoiding something bad.
422     Flag = Chain.getValue(1);
423   }
424
425   if (Flag.getNode())
426     return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
427
428   // Return Void
429   return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
430 }
431
432 SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
433                                        ISD::CondCode CC, SDValue &SystemZCC,
434                                        SelectionDAG &DAG) {
435   assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
436
437   // FIXME: Emit a test if RHS is zero
438
439   bool isUnsigned = false;
440   SystemZCC::CondCodes TCC;
441   switch (CC) {
442   default: assert(0 && "Invalid integer condition!");
443   case ISD::SETEQ:
444     TCC = SystemZCC::E;
445     break;
446   case ISD::SETNE:
447     TCC = SystemZCC::NE;
448     break;
449   case ISD::SETULE:
450     isUnsigned = true;   // FALLTHROUGH
451   case ISD::SETLE:
452     TCC = SystemZCC::LE;
453     break;
454   case ISD::SETUGE:
455     isUnsigned = true;   // FALLTHROUGH
456   case ISD::SETGE:
457     TCC = SystemZCC::HE;
458     break;
459   case ISD::SETUGT:
460     isUnsigned = true;
461   case ISD::SETGT:
462     TCC = SystemZCC::H; // FALLTHROUGH
463     break;
464   case ISD::SETULT:
465     isUnsigned = true;
466   case ISD::SETLT:      // FALLTHROUGH
467     TCC = SystemZCC::L;
468     break;
469   }
470
471   SystemZCC = DAG.getConstant(TCC, MVT::i32);
472
473   DebugLoc dl = LHS.getDebugLoc();
474   return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
475                      dl, MVT::Flag, LHS, RHS);
476 }
477
478
479 SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
480   SDValue Chain = Op.getOperand(0);
481   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
482   SDValue LHS   = Op.getOperand(2);
483   SDValue RHS   = Op.getOperand(3);
484   SDValue Dest  = Op.getOperand(4);
485   DebugLoc dl   = Op.getDebugLoc();
486
487   SDValue SystemZCC;
488   SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
489   return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
490                      Chain, Dest, SystemZCC, Flag);
491 }
492
493 SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
494   SDValue LHS    = Op.getOperand(0);
495   SDValue RHS    = Op.getOperand(1);
496   SDValue TrueV  = Op.getOperand(2);
497   SDValue FalseV = Op.getOperand(3);
498   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
499   DebugLoc dl   = Op.getDebugLoc();
500
501   SDValue SystemZCC;
502   SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
503
504   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
505   SmallVector<SDValue, 4> Ops;
506   Ops.push_back(TrueV);
507   Ops.push_back(FalseV);
508   Ops.push_back(SystemZCC);
509   Ops.push_back(Flag);
510
511   return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
512 }
513
514
515 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
516   switch (Opcode) {
517   case SystemZISD::RET_FLAG:           return "SystemZISD::RET_FLAG";
518   case SystemZISD::CALL:               return "SystemZISD::CALL";
519   case SystemZISD::BRCOND:             return "SystemZISD::BRCOND";
520   case SystemZISD::CMP:                return "SystemZISD::CMP";
521   case SystemZISD::UCMP:               return "SystemZISD::UCMP";
522   case SystemZISD::SELECT:             return "SystemZISD::SELECT";
523   default: return NULL;
524   }
525 }
526
527 //===----------------------------------------------------------------------===//
528 //  Other Lowering Code
529 //===----------------------------------------------------------------------===//
530
531 MachineBasicBlock*
532 SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
533                                                    MachineBasicBlock *BB) const {
534   const SystemZInstrInfo &TII = *TM.getInstrInfo();
535   DebugLoc dl = MI->getDebugLoc();
536   assert((MI->getOpcode() == SystemZ::Select32 ||
537           MI->getOpcode() == SystemZ::Select64) &&
538          "Unexpected instr type to insert");
539
540   // To "insert" a SELECT instruction, we actually have to insert the diamond
541   // control-flow pattern.  The incoming instruction knows the destination vreg
542   // to set, the condition code register to branch on, the true/false values to
543   // select between, and a branch opcode to use.
544   const BasicBlock *LLVM_BB = BB->getBasicBlock();
545   MachineFunction::iterator I = BB;
546   ++I;
547
548   //  thisMBB:
549   //  ...
550   //   TrueVal = ...
551   //   cmpTY ccX, r1, r2
552   //   jCC copy1MBB
553   //   fallthrough --> copy0MBB
554   MachineBasicBlock *thisMBB = BB;
555   MachineFunction *F = BB->getParent();
556   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
557   MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
558   SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
559   BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
560   F->insert(I, copy0MBB);
561   F->insert(I, copy1MBB);
562   // Update machine-CFG edges by transferring all successors of the current
563   // block to the new block which will contain the Phi node for the select.
564   copy1MBB->transferSuccessors(BB);
565   // Next, add the true and fallthrough blocks as its successors.
566   BB->addSuccessor(copy0MBB);
567   BB->addSuccessor(copy1MBB);
568
569   //  copy0MBB:
570   //   %FalseValue = ...
571   //   # fallthrough to copy1MBB
572   BB = copy0MBB;
573
574   // Update machine-CFG edges
575   BB->addSuccessor(copy1MBB);
576
577   //  copy1MBB:
578   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
579   //  ...
580   BB = copy1MBB;
581   BuildMI(BB, dl, TII.get(SystemZ::PHI),
582           MI->getOperand(0).getReg())
583     .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
584     .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
585
586   F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
587   return BB;
588 }