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