Remove an argument-less call to getSubtargetImpl from TargetLoweringBase.
[oota-llvm.git] / lib / Target / BPF / BPFISelLowering.cpp
1 //===-- BPFISelLowering.cpp - BPF 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 defines the interfaces that BPF uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "BPFISelLowering.h"
16 #include "BPF.h"
17 #include "BPFTargetMachine.h"
18 #include "BPFSubtarget.h"
19 #include "llvm/CodeGen/CallingConvLower.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
26 #include "llvm/CodeGen/ValueTypes.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/IR/DiagnosticInfo.h"
32 #include "llvm/IR/DiagnosticPrinter.h"
33 using namespace llvm;
34
35 #define DEBUG_TYPE "bpf-lower"
36
37 namespace {
38
39 // Diagnostic information for unimplemented or unsupported feature reporting.
40 class DiagnosticInfoUnsupported : public DiagnosticInfo {
41 private:
42   // Debug location where this diagnostic is triggered.
43   DebugLoc DLoc;
44   const Twine &Description;
45   const Function &Fn;
46   SDValue Value;
47
48   static int KindID;
49
50   static int getKindID() {
51     if (KindID == 0)
52       KindID = llvm::getNextAvailablePluginDiagnosticKind();
53     return KindID;
54   }
55
56 public:
57   DiagnosticInfoUnsupported(SDLoc DLoc, const Function &Fn, const Twine &Desc,
58                             SDValue Value)
59       : DiagnosticInfo(getKindID(), DS_Error), DLoc(DLoc.getDebugLoc()),
60         Description(Desc), Fn(Fn), Value(Value) {}
61
62   void print(DiagnosticPrinter &DP) const override {
63     std::string Str;
64     raw_string_ostream OS(Str);
65
66     if (DLoc.isUnknown() == false) {
67       DILocation DIL(DLoc.getAsMDNode(Fn.getContext()));
68       StringRef Filename = DIL.getFilename();
69       unsigned Line = DIL.getLineNumber();
70       unsigned Column = DIL.getColumnNumber();
71       OS << Filename << ':' << Line << ':' << Column << ' ';
72     }
73
74     OS << "in function " << Fn.getName() << ' ' << *Fn.getFunctionType() << '\n'
75        << Description;
76     if (Value)
77       Value->print(OS);
78     OS << '\n';
79     OS.flush();
80     DP << Str;
81   }
82
83   static bool classof(const DiagnosticInfo *DI) {
84     return DI->getKind() == getKindID();
85   }
86 };
87
88 int DiagnosticInfoUnsupported::KindID = 0;
89 }
90
91 BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
92                                      const BPFSubtarget &STI)
93     : TargetLowering(TM) {
94
95   // Set up the register classes.
96   addRegisterClass(MVT::i64, &BPF::GPRRegClass);
97
98   // Compute derived properties from the register classes
99   computeRegisterProperties(STI.getRegisterInfo());
100
101   setStackPointerRegisterToSaveRestore(BPF::R11);
102
103   setOperationAction(ISD::BR_CC, MVT::i64, Custom);
104   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
105   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
106   setOperationAction(ISD::SETCC, MVT::i64, Expand);
107   setOperationAction(ISD::SELECT, MVT::i64, Expand);
108   setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
109
110   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
111
112   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
113   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
114   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
115
116   setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
117   setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
118   setOperationAction(ISD::SREM, MVT::i64, Expand);
119   setOperationAction(ISD::UREM, MVT::i64, Expand);
120
121   setOperationAction(ISD::MULHU, MVT::i64, Expand);
122   setOperationAction(ISD::MULHS, MVT::i64, Expand);
123   setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
124   setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
125
126   setOperationAction(ISD::ADDC, MVT::i64, Expand);
127   setOperationAction(ISD::ADDE, MVT::i64, Expand);
128   setOperationAction(ISD::SUBC, MVT::i64, Expand);
129   setOperationAction(ISD::SUBE, MVT::i64, Expand);
130
131   // no UNDEF allowed
132   setOperationAction(ISD::UNDEF, MVT::i64, Expand);
133
134   setOperationAction(ISD::ROTR, MVT::i64, Expand);
135   setOperationAction(ISD::ROTL, MVT::i64, Expand);
136   setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
137   setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
138   setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
139
140   setOperationAction(ISD::BSWAP, MVT::i64, Expand);
141   setOperationAction(ISD::CTTZ, MVT::i64, Custom);
142   setOperationAction(ISD::CTLZ, MVT::i64, Custom);
143   setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Custom);
144   setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
145   setOperationAction(ISD::CTPOP, MVT::i64, Expand);
146
147   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
148   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
149   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
150   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
151
152   // Extended load operations for i1 types must be promoted
153   for (MVT VT : MVT::integer_valuetypes()) {
154     setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
155     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
156     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
157
158     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
159     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Expand);
160     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
161   }
162
163   setBooleanContents(ZeroOrOneBooleanContent);
164
165   // Function alignments (log2)
166   setMinFunctionAlignment(3);
167   setPrefFunctionAlignment(3);
168
169   // inline memcpy() for kernel to see explicit copy
170   MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 128;
171   MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 128;
172   MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = 128;
173 }
174
175 SDValue BPFTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
176   switch (Op.getOpcode()) {
177   case ISD::BR_CC:
178     return LowerBR_CC(Op, DAG);
179   case ISD::GlobalAddress:
180     return LowerGlobalAddress(Op, DAG);
181   case ISD::SELECT_CC:
182     return LowerSELECT_CC(Op, DAG);
183   default:
184     llvm_unreachable("unimplemented operand");
185   }
186 }
187
188 // Calling Convention Implementation
189 #include "BPFGenCallingConv.inc"
190
191 SDValue BPFTargetLowering::LowerFormalArguments(
192     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
193     const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
194     SmallVectorImpl<SDValue> &InVals) const {
195   switch (CallConv) {
196   default:
197     llvm_unreachable("Unsupported calling convention");
198   case CallingConv::C:
199   case CallingConv::Fast:
200     break;
201   }
202
203   MachineFunction &MF = DAG.getMachineFunction();
204   MachineRegisterInfo &RegInfo = MF.getRegInfo();
205
206   // Assign locations to all of the incoming arguments.
207   SmallVector<CCValAssign, 16> ArgLocs;
208   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
209   CCInfo.AnalyzeFormalArguments(Ins, CC_BPF64);
210
211   for (auto &VA : ArgLocs) {
212     if (VA.isRegLoc()) {
213       // Arguments passed in registers
214       EVT RegVT = VA.getLocVT();
215       switch (RegVT.getSimpleVT().SimpleTy) {
216       default: {
217         errs() << "LowerFormalArguments Unhandled argument type: "
218                << RegVT.getSimpleVT().SimpleTy << '\n';
219         llvm_unreachable(0);
220       }
221       case MVT::i64:
222         unsigned VReg = RegInfo.createVirtualRegister(&BPF::GPRRegClass);
223         RegInfo.addLiveIn(VA.getLocReg(), VReg);
224         SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, RegVT);
225
226         // If this is an 8/16/32-bit value, it is really passed promoted to 64
227         // bits. Insert an assert[sz]ext to capture this, then truncate to the
228         // right size.
229         if (VA.getLocInfo() == CCValAssign::SExt)
230           ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,
231                                  DAG.getValueType(VA.getValVT()));
232         else if (VA.getLocInfo() == CCValAssign::ZExt)
233           ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,
234                                  DAG.getValueType(VA.getValVT()));
235
236         if (VA.getLocInfo() != CCValAssign::Full)
237           ArgValue = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), ArgValue);
238
239         InVals.push_back(ArgValue);
240       }
241     } else {
242       DiagnosticInfoUnsupported Err(DL, *MF.getFunction(),
243                                     "defined with too many args", SDValue());
244       DAG.getContext()->diagnose(Err);
245     }
246   }
247
248   if (IsVarArg || MF.getFunction()->hasStructRetAttr()) {
249     DiagnosticInfoUnsupported Err(
250         DL, *MF.getFunction(),
251         "functions with VarArgs or StructRet are not supported", SDValue());
252     DAG.getContext()->diagnose(Err);
253   }
254
255   return Chain;
256 }
257
258 SDValue BPFTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
259                                      SmallVectorImpl<SDValue> &InVals) const {
260   SelectionDAG &DAG = CLI.DAG;
261   auto &Outs = CLI.Outs;
262   auto &OutVals = CLI.OutVals;
263   auto &Ins = CLI.Ins;
264   SDValue Chain = CLI.Chain;
265   SDValue Callee = CLI.Callee;
266   bool &IsTailCall = CLI.IsTailCall;
267   CallingConv::ID CallConv = CLI.CallConv;
268   bool IsVarArg = CLI.IsVarArg;
269   MachineFunction &MF = DAG.getMachineFunction();
270
271   // BPF target does not support tail call optimization.
272   IsTailCall = false;
273
274   switch (CallConv) {
275   default:
276     report_fatal_error("Unsupported calling convention");
277   case CallingConv::Fast:
278   case CallingConv::C:
279     break;
280   }
281
282   // Analyze operands of the call, assigning locations to each operand.
283   SmallVector<CCValAssign, 16> ArgLocs;
284   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
285
286   CCInfo.AnalyzeCallOperands(Outs, CC_BPF64);
287
288   unsigned NumBytes = CCInfo.getNextStackOffset();
289
290   if (Outs.size() >= 6) {
291     DiagnosticInfoUnsupported Err(CLI.DL, *MF.getFunction(),
292                                   "too many args to ", Callee);
293     DAG.getContext()->diagnose(Err);
294   }
295
296   for (auto &Arg : Outs) {
297     ISD::ArgFlagsTy Flags = Arg.Flags;
298     if (!Flags.isByVal())
299       continue;
300
301     DiagnosticInfoUnsupported Err(CLI.DL, *MF.getFunction(),
302                                   "pass by value not supported ", Callee);
303     DAG.getContext()->diagnose(Err);
304   }
305
306   Chain = DAG.getCALLSEQ_START(
307       Chain, DAG.getConstant(NumBytes, getPointerTy(), true), CLI.DL);
308
309   SmallVector<std::pair<unsigned, SDValue>, 5> RegsToPass;
310
311   // Walk arg assignments
312   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
313     CCValAssign &VA = ArgLocs[i];
314     SDValue Arg = OutVals[i];
315
316     // Promote the value if needed.
317     switch (VA.getLocInfo()) {
318     default:
319       llvm_unreachable("Unknown loc info");
320     case CCValAssign::Full:
321       break;
322     case CCValAssign::SExt:
323       Arg = DAG.getNode(ISD::SIGN_EXTEND, CLI.DL, VA.getLocVT(), Arg);
324       break;
325     case CCValAssign::ZExt:
326       Arg = DAG.getNode(ISD::ZERO_EXTEND, CLI.DL, VA.getLocVT(), Arg);
327       break;
328     case CCValAssign::AExt:
329       Arg = DAG.getNode(ISD::ANY_EXTEND, CLI.DL, VA.getLocVT(), Arg);
330       break;
331     }
332
333     // Push arguments into RegsToPass vector
334     if (VA.isRegLoc())
335       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
336     else
337       llvm_unreachable("call arg pass bug");
338   }
339
340   SDValue InFlag;
341
342   // Build a sequence of copy-to-reg nodes chained together with token chain and
343   // flag operands which copy the outgoing args into registers.  The InFlag in
344   // necessary since all emitted instructions must be stuck together.
345   for (auto &Reg : RegsToPass) {
346     Chain = DAG.getCopyToReg(Chain, CLI.DL, Reg.first, Reg.second, InFlag);
347     InFlag = Chain.getValue(1);
348   }
349
350   // If the callee is a GlobalAddress node (quite common, every direct call is)
351   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
352   // Likewise ExternalSymbol -> TargetExternalSymbol.
353   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
354     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), CLI.DL, getPointerTy(),
355                                         G->getOffset(), 0);
356   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
357     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy(), 0);
358
359   // Returns a chain & a flag for retval copy to use.
360   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
361   SmallVector<SDValue, 8> Ops;
362   Ops.push_back(Chain);
363   Ops.push_back(Callee);
364
365   // Add argument registers to the end of the list so that they are
366   // known live into the call.
367   for (auto &Reg : RegsToPass)
368     Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
369
370   if (InFlag.getNode())
371     Ops.push_back(InFlag);
372
373   Chain = DAG.getNode(BPFISD::CALL, CLI.DL, NodeTys, Ops);
374   InFlag = Chain.getValue(1);
375
376   // Create the CALLSEQ_END node.
377   Chain = DAG.getCALLSEQ_END(
378       Chain, DAG.getConstant(NumBytes, getPointerTy(), true),
379       DAG.getConstant(0, getPointerTy(), true), InFlag, CLI.DL);
380   InFlag = Chain.getValue(1);
381
382   // Handle result values, copying them out of physregs into vregs that we
383   // return.
384   return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, CLI.DL, DAG,
385                          InVals);
386 }
387
388 SDValue
389 BPFTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
390                                bool IsVarArg,
391                                const SmallVectorImpl<ISD::OutputArg> &Outs,
392                                const SmallVectorImpl<SDValue> &OutVals,
393                                SDLoc DL, SelectionDAG &DAG) const {
394
395   // CCValAssign - represent the assignment of the return value to a location
396   SmallVector<CCValAssign, 16> RVLocs;
397   MachineFunction &MF = DAG.getMachineFunction();
398
399   // CCState - Info about the registers and stack slot.
400   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
401
402   if (MF.getFunction()->getReturnType()->isAggregateType()) {
403     DiagnosticInfoUnsupported Err(DL, *MF.getFunction(),
404                                   "only integer returns supported", SDValue());
405     DAG.getContext()->diagnose(Err);
406   }
407
408   // Analize return values.
409   CCInfo.AnalyzeReturn(Outs, RetCC_BPF64);
410
411   SDValue Flag;
412   SmallVector<SDValue, 4> RetOps(1, Chain);
413
414   // Copy the result values into the output registers.
415   for (unsigned i = 0; i != RVLocs.size(); ++i) {
416     CCValAssign &VA = RVLocs[i];
417     assert(VA.isRegLoc() && "Can only return in registers!");
418
419     Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVals[i], Flag);
420
421     // Guarantee that all emitted copies are stuck together,
422     // avoiding something bad.
423     Flag = Chain.getValue(1);
424     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
425   }
426
427   unsigned Opc = BPFISD::RET_FLAG;
428   RetOps[0] = Chain; // Update chain.
429
430   // Add the flag if we have it.
431   if (Flag.getNode())
432     RetOps.push_back(Flag);
433
434   return DAG.getNode(Opc, DL, MVT::Other, RetOps);
435 }
436
437 SDValue BPFTargetLowering::LowerCallResult(
438     SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
439     const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
440     SmallVectorImpl<SDValue> &InVals) const {
441
442   MachineFunction &MF = DAG.getMachineFunction();
443   // Assign locations to each value returned by this call.
444   SmallVector<CCValAssign, 16> RVLocs;
445   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
446
447   if (Ins.size() >= 2) {
448     DiagnosticInfoUnsupported Err(DL, *MF.getFunction(),
449                                   "only small returns supported", SDValue());
450     DAG.getContext()->diagnose(Err);
451   }
452
453   CCInfo.AnalyzeCallResult(Ins, RetCC_BPF64);
454
455   // Copy all of the result registers out of their specified physreg.
456   for (auto &Val : RVLocs) {
457     Chain = DAG.getCopyFromReg(Chain, DL, Val.getLocReg(),
458                                Val.getValVT(), InFlag).getValue(1);
459     InFlag = Chain.getValue(2);
460     InVals.push_back(Chain.getValue(0));
461   }
462
463   return Chain;
464 }
465
466 static void NegateCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
467   switch (CC) {
468   default:
469     break;
470   case ISD::SETULT:
471   case ISD::SETULE:
472   case ISD::SETLT:
473   case ISD::SETLE:
474     CC = ISD::getSetCCSwappedOperands(CC);
475     std::swap(LHS, RHS);
476     break;
477   }
478 }
479
480 SDValue BPFTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
481   SDValue Chain = Op.getOperand(0);
482   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
483   SDValue LHS = Op.getOperand(2);
484   SDValue RHS = Op.getOperand(3);
485   SDValue Dest = Op.getOperand(4);
486   SDLoc DL(Op);
487
488   NegateCC(LHS, RHS, CC);
489
490   return DAG.getNode(BPFISD::BR_CC, DL, Op.getValueType(), Chain, LHS, RHS,
491                      DAG.getConstant(CC, MVT::i64), Dest);
492 }
493
494 SDValue BPFTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
495   SDValue LHS = Op.getOperand(0);
496   SDValue RHS = Op.getOperand(1);
497   SDValue TrueV = Op.getOperand(2);
498   SDValue FalseV = Op.getOperand(3);
499   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
500   SDLoc DL(Op);
501
502   NegateCC(LHS, RHS, CC);
503
504   SDValue TargetCC = DAG.getConstant(CC, MVT::i64);
505
506   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
507   SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
508
509   return DAG.getNode(BPFISD::SELECT_CC, DL, VTs, Ops);
510 }
511
512 const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
513   switch (Opcode) {
514   default:
515     return NULL;
516   case BPFISD::RET_FLAG:
517     return "BPFISD::RET_FLAG";
518   case BPFISD::CALL:
519     return "BPFISD::CALL";
520   case BPFISD::SELECT_CC:
521     return "BPFISD::SELECT_CC";
522   case BPFISD::BR_CC:
523     return "BPFISD::BR_CC";
524   case BPFISD::Wrapper:
525     return "BPFISD::Wrapper";
526   }
527 }
528
529 SDValue BPFTargetLowering::LowerGlobalAddress(SDValue Op,
530                                               SelectionDAG &DAG) const {
531   SDLoc DL(Op);
532   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
533   SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i64);
534
535   return DAG.getNode(BPFISD::Wrapper, DL, MVT::i64, GA);
536 }
537
538 MachineBasicBlock *
539 BPFTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
540                                                MachineBasicBlock *BB) const {
541   unsigned Opc = MI->getOpcode();
542
543   const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
544   DebugLoc DL = MI->getDebugLoc();
545
546   assert(Opc == BPF::Select && "Unexpected instr type to insert");
547
548   // To "insert" a SELECT instruction, we actually have to insert the diamond
549   // control-flow pattern.  The incoming instruction knows the destination vreg
550   // to set, the condition code register to branch on, the true/false values to
551   // select between, and a branch opcode to use.
552   const BasicBlock *LLVM_BB = BB->getBasicBlock();
553   MachineFunction::iterator I = BB;
554   ++I;
555
556   // ThisMBB:
557   // ...
558   //  TrueVal = ...
559   //  jmp_XX r1, r2 goto Copy1MBB
560   //  fallthrough --> Copy0MBB
561   MachineBasicBlock *ThisMBB = BB;
562   MachineFunction *F = BB->getParent();
563   MachineBasicBlock *Copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
564   MachineBasicBlock *Copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
565
566   F->insert(I, Copy0MBB);
567   F->insert(I, Copy1MBB);
568   // Update machine-CFG edges by transferring all successors of the current
569   // block to the new block which will contain the Phi node for the select.
570   Copy1MBB->splice(Copy1MBB->begin(), BB,
571                    std::next(MachineBasicBlock::iterator(MI)), BB->end());
572   Copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
573   // Next, add the true and fallthrough blocks as its successors.
574   BB->addSuccessor(Copy0MBB);
575   BB->addSuccessor(Copy1MBB);
576
577   // Insert Branch if Flag
578   unsigned LHS = MI->getOperand(1).getReg();
579   unsigned RHS = MI->getOperand(2).getReg();
580   int CC = MI->getOperand(3).getImm();
581   switch (CC) {
582   case ISD::SETGT:
583     BuildMI(BB, DL, TII.get(BPF::JSGT_rr))
584         .addReg(LHS)
585         .addReg(RHS)
586         .addMBB(Copy1MBB);
587     break;
588   case ISD::SETUGT:
589     BuildMI(BB, DL, TII.get(BPF::JUGT_rr))
590         .addReg(LHS)
591         .addReg(RHS)
592         .addMBB(Copy1MBB);
593     break;
594   case ISD::SETGE:
595     BuildMI(BB, DL, TII.get(BPF::JSGE_rr))
596         .addReg(LHS)
597         .addReg(RHS)
598         .addMBB(Copy1MBB);
599     break;
600   case ISD::SETUGE:
601     BuildMI(BB, DL, TII.get(BPF::JUGE_rr))
602         .addReg(LHS)
603         .addReg(RHS)
604         .addMBB(Copy1MBB);
605     break;
606   case ISD::SETEQ:
607     BuildMI(BB, DL, TII.get(BPF::JEQ_rr))
608         .addReg(LHS)
609         .addReg(RHS)
610         .addMBB(Copy1MBB);
611     break;
612   case ISD::SETNE:
613     BuildMI(BB, DL, TII.get(BPF::JNE_rr))
614         .addReg(LHS)
615         .addReg(RHS)
616         .addMBB(Copy1MBB);
617     break;
618   default:
619     report_fatal_error("unimplemented select CondCode " + Twine(CC));
620   }
621
622   // Copy0MBB:
623   //  %FalseValue = ...
624   //  # fallthrough to Copy1MBB
625   BB = Copy0MBB;
626
627   // Update machine-CFG edges
628   BB->addSuccessor(Copy1MBB);
629
630   // Copy1MBB:
631   //  %Result = phi [ %FalseValue, Copy0MBB ], [ %TrueValue, ThisMBB ]
632   // ...
633   BB = Copy1MBB;
634   BuildMI(*BB, BB->begin(), DL, TII.get(BPF::PHI), MI->getOperand(0).getReg())
635       .addReg(MI->getOperand(5).getReg())
636       .addMBB(Copy0MBB)
637       .addReg(MI->getOperand(4).getReg())
638       .addMBB(ThisMBB);
639
640   MI->eraseFromParent(); // The pseudo instruction is gone now.
641   return BB;
642 }