xref: /freebsd/contrib/llvm-project/llvm/lib/Target/M68k/M68kISelLowering.cpp (revision 0ad011ececb978e22a9bff2acf76633b094f1ff6)
1 //===-- M68kISelLowering.cpp - M68k DAG Lowering Impl -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines the interfaces that M68k uses to lower LLVM code into a
11 /// selection DAG.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "M68kISelLowering.h"
16 #include "M68kCallingConv.h"
17 #include "M68kMachineFunction.h"
18 #include "M68kSubtarget.h"
19 #include "M68kTargetMachine.h"
20 #include "M68kTargetObjectFile.h"
21 
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/SelectionDAG.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/KnownBits.h"
38 #include "llvm/Support/raw_ostream.h"
39 
40 using namespace llvm;
41 
42 #define DEBUG_TYPE "M68k-isel"
43 
44 STATISTIC(NumTailCalls, "Number of tail calls");
45 
46 M68kTargetLowering::M68kTargetLowering(const M68kTargetMachine &TM,
47                                        const M68kSubtarget &STI)
48     : TargetLowering(TM), Subtarget(STI), TM(TM) {
49 
50   MVT PtrVT = MVT::i32;
51 
52   setBooleanContents(ZeroOrOneBooleanContent);
53 
54   auto *RegInfo = Subtarget.getRegisterInfo();
55   setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
56 
57   // Set up the register classes.
58   addRegisterClass(MVT::i8, &M68k::DR8RegClass);
59   addRegisterClass(MVT::i16, &M68k::XR16RegClass);
60   addRegisterClass(MVT::i32, &M68k::XR32RegClass);
61 
62   for (auto VT : MVT::integer_valuetypes()) {
63     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
64     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
65     setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
66   }
67 
68   // We don't accept any truncstore of integer registers.
69   setTruncStoreAction(MVT::i64, MVT::i32, Expand);
70   setTruncStoreAction(MVT::i64, MVT::i16, Expand);
71   setTruncStoreAction(MVT::i64, MVT::i8, Expand);
72   setTruncStoreAction(MVT::i32, MVT::i16, Expand);
73   setTruncStoreAction(MVT::i32, MVT::i8, Expand);
74   setTruncStoreAction(MVT::i16, MVT::i8, Expand);
75 
76   setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i8, Promote);
77   setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i16, Legal);
78   if (Subtarget.atLeastM68020())
79     setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i32, Legal);
80   else
81     setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i32, LibCall);
82   setOperationAction(ISD::MUL, MVT::i64, LibCall);
83 
84   for (auto OP :
85        {ISD::SREM, ISD::UREM, ISD::UDIVREM, ISD::SDIVREM,
86         ISD::MULHS, ISD::MULHU, ISD::UMUL_LOHI, ISD::SMUL_LOHI}) {
87     setOperationAction(OP, MVT::i8, Promote);
88     setOperationAction(OP, MVT::i16, Legal);
89     setOperationAction(OP, MVT::i32, LibCall);
90   }
91 
92   for (auto OP : {ISD::UMUL_LOHI, ISD::SMUL_LOHI}) {
93     setOperationAction(OP, MVT::i8, Expand);
94     setOperationAction(OP, MVT::i16, Expand);
95   }
96 
97   // FIXME It would be better to use a custom lowering
98   for (auto OP : {ISD::SMULO, ISD::UMULO}) {
99     setOperationAction(OP, MVT::i8, Expand);
100     setOperationAction(OP, MVT::i16, Expand);
101     setOperationAction(OP, MVT::i32, Expand);
102   }
103 
104   for (auto OP : {ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS})
105     setOperationAction(OP, MVT::i32, Custom);
106 
107   // Add/Sub overflow ops with MVT::Glues are lowered to CCR dependences.
108   for (auto VT : {MVT::i8, MVT::i16, MVT::i32}) {
109     setOperationAction(ISD::ADDC, VT, Custom);
110     setOperationAction(ISD::ADDE, VT, Custom);
111     setOperationAction(ISD::SUBC, VT, Custom);
112     setOperationAction(ISD::SUBE, VT, Custom);
113   }
114 
115   // SADDO and friends are legal with this setup, i hope
116   for (auto VT : {MVT::i8, MVT::i16, MVT::i32}) {
117     setOperationAction(ISD::SADDO, VT, Custom);
118     setOperationAction(ISD::UADDO, VT, Custom);
119     setOperationAction(ISD::SSUBO, VT, Custom);
120     setOperationAction(ISD::USUBO, VT, Custom);
121   }
122 
123   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
124   setOperationAction(ISD::BRCOND, MVT::Other, Custom);
125 
126   for (auto VT : {MVT::i8, MVT::i16, MVT::i32}) {
127     setOperationAction(ISD::BR_CC, VT, Expand);
128     setOperationAction(ISD::SELECT, VT, Custom);
129     setOperationAction(ISD::SELECT_CC, VT, Expand);
130     setOperationAction(ISD::SETCC, VT, Custom);
131     setOperationAction(ISD::SETCCCARRY, VT, Custom);
132   }
133 
134   for (auto VT : {MVT::i8, MVT::i16, MVT::i32}) {
135     setOperationAction(ISD::BSWAP, VT, Expand);
136     setOperationAction(ISD::CTTZ, VT, Expand);
137     setOperationAction(ISD::CTLZ, VT, Expand);
138     setOperationAction(ISD::CTPOP, VT, Expand);
139   }
140 
141   setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
142   setOperationAction(ISD::JumpTable, MVT::i32, Custom);
143   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
144   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
145   setOperationAction(ISD::ExternalSymbol, MVT::i32, Custom);
146   setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
147 
148   setOperationAction(ISD::VASTART, MVT::Other, Custom);
149   setOperationAction(ISD::VAEND, MVT::Other, Expand);
150   setOperationAction(ISD::VAARG, MVT::Other, Expand);
151   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
152 
153   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
154   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
155 
156   setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
157 
158   computeRegisterProperties(STI.getRegisterInfo());
159 
160   // We lower the `atomic-compare-and-swap` to `__sync_val_compare_and_swap`
161   // for subtarget < M68020
162   setMaxAtomicSizeInBitsSupported(32);
163   setOperationAction(ISD::ATOMIC_CMP_SWAP, {MVT::i8, MVT::i16, MVT::i32},
164                      Subtarget.atLeastM68020() ? Legal : LibCall);
165 
166   setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
167 
168   // M68k does not have native read-modify-write support, so expand all of them
169   // to `__sync_fetch_*` for target < M68020, otherwise expand to CmpxChg.
170   // See `shouldExpandAtomicRMWInIR` below.
171   setOperationAction(
172       {
173           ISD::ATOMIC_LOAD_ADD,
174           ISD::ATOMIC_LOAD_SUB,
175           ISD::ATOMIC_LOAD_AND,
176           ISD::ATOMIC_LOAD_OR,
177           ISD::ATOMIC_LOAD_XOR,
178           ISD::ATOMIC_LOAD_NAND,
179           ISD::ATOMIC_LOAD_MIN,
180           ISD::ATOMIC_LOAD_MAX,
181           ISD::ATOMIC_LOAD_UMIN,
182           ISD::ATOMIC_LOAD_UMAX,
183           ISD::ATOMIC_SWAP,
184       },
185       {MVT::i8, MVT::i16, MVT::i32}, LibCall);
186 
187   setMinFunctionAlignment(Align(2));
188 }
189 
190 TargetLoweringBase::AtomicExpansionKind
191 M68kTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
192   return Subtarget.atLeastM68020()
193              ? TargetLoweringBase::AtomicExpansionKind::CmpXChg
194              : TargetLoweringBase::AtomicExpansionKind::None;
195 }
196 
197 Register
198 M68kTargetLowering::getExceptionPointerRegister(const Constant *) const {
199   return M68k::D0;
200 }
201 
202 Register
203 M68kTargetLowering::getExceptionSelectorRegister(const Constant *) const {
204   return M68k::D1;
205 }
206 
207 unsigned
208 M68kTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
209   return StringSwitch<unsigned>(ConstraintCode)
210       .Case("Q", InlineAsm::Constraint_Q)
211       .Case("U", InlineAsm::Constraint_Um) // We borrow Constraint_Um for 'U'.
212       .Default(TargetLowering::getInlineAsmMemConstraint(ConstraintCode));
213 }
214 
215 EVT M68kTargetLowering::getSetCCResultType(const DataLayout &DL,
216                                            LLVMContext &Context, EVT VT) const {
217   // M68k SETcc producess either 0x00 or 0xFF
218   return MVT::i8;
219 }
220 
221 MVT M68kTargetLowering::getScalarShiftAmountTy(const DataLayout &DL,
222                                                EVT Ty) const {
223   if (Ty.isSimple()) {
224     return Ty.getSimpleVT();
225   }
226   return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
227 }
228 
229 #include "M68kGenCallingConv.inc"
230 
231 enum StructReturnType { NotStructReturn, RegStructReturn, StackStructReturn };
232 
233 static StructReturnType
234 callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
235   if (Outs.empty())
236     return NotStructReturn;
237 
238   const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
239   if (!Flags.isSRet())
240     return NotStructReturn;
241   if (Flags.isInReg())
242     return RegStructReturn;
243   return StackStructReturn;
244 }
245 
246 /// Determines whether a function uses struct return semantics.
247 static StructReturnType
248 argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
249   if (Ins.empty())
250     return NotStructReturn;
251 
252   const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
253   if (!Flags.isSRet())
254     return NotStructReturn;
255   if (Flags.isInReg())
256     return RegStructReturn;
257   return StackStructReturn;
258 }
259 
260 /// Make a copy of an aggregate at address specified by "Src" to address
261 /// "Dst" with size and alignment information specified by the specific
262 /// parameter attribute. The copy will be passed as a byval function parameter.
263 static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
264                                          SDValue Chain, ISD::ArgFlagsTy Flags,
265                                          SelectionDAG &DAG, const SDLoc &DL) {
266   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), DL, MVT::i32);
267 
268   return DAG.getMemcpy(
269       Chain, DL, Dst, Src, SizeNode, Flags.getNonZeroByValAlign(),
270       /*isVolatile=*/false, /*AlwaysInline=*/true,
271       /*isTailCall=*/false, MachinePointerInfo(), MachinePointerInfo());
272 }
273 
274 /// Return true if the calling convention is one that we can guarantee TCO for.
275 static bool canGuaranteeTCO(CallingConv::ID CC) { return false; }
276 
277 /// Return true if we might ever do TCO for calls with this calling convention.
278 static bool mayTailCallThisCC(CallingConv::ID CC) {
279   switch (CC) {
280   // C calling conventions:
281   case CallingConv::C:
282     return true;
283   default:
284     return canGuaranteeTCO(CC);
285   }
286 }
287 
288 /// Return true if the function is being made into a tailcall target by
289 /// changing its ABI.
290 static bool shouldGuaranteeTCO(CallingConv::ID CC, bool GuaranteedTailCallOpt) {
291   return GuaranteedTailCallOpt && canGuaranteeTCO(CC);
292 }
293 
294 /// Return true if the given stack call argument is already available in the
295 /// same position (relatively) of the caller's incoming argument stack.
296 static bool MatchingStackOffset(SDValue Arg, unsigned Offset,
297                                 ISD::ArgFlagsTy Flags, MachineFrameInfo &MFI,
298                                 const MachineRegisterInfo *MRI,
299                                 const M68kInstrInfo *TII,
300                                 const CCValAssign &VA) {
301   unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
302 
303   for (;;) {
304     // Look through nodes that don't alter the bits of the incoming value.
305     unsigned Op = Arg.getOpcode();
306     if (Op == ISD::ZERO_EXTEND || Op == ISD::ANY_EXTEND || Op == ISD::BITCAST) {
307       Arg = Arg.getOperand(0);
308       continue;
309     }
310     if (Op == ISD::TRUNCATE) {
311       const SDValue &TruncInput = Arg.getOperand(0);
312       if (TruncInput.getOpcode() == ISD::AssertZext &&
313           cast<VTSDNode>(TruncInput.getOperand(1))->getVT() ==
314               Arg.getValueType()) {
315         Arg = TruncInput.getOperand(0);
316         continue;
317       }
318     }
319     break;
320   }
321 
322   int FI = INT_MAX;
323   if (Arg.getOpcode() == ISD::CopyFromReg) {
324     Register VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
325     if (!Register::isVirtualRegister(VR))
326       return false;
327     MachineInstr *Def = MRI->getVRegDef(VR);
328     if (!Def)
329       return false;
330     if (!Flags.isByVal()) {
331       if (!TII->isLoadFromStackSlot(*Def, FI))
332         return false;
333     } else {
334       unsigned Opcode = Def->getOpcode();
335       if ((Opcode == M68k::LEA32p || Opcode == M68k::LEA32f) &&
336           Def->getOperand(1).isFI()) {
337         FI = Def->getOperand(1).getIndex();
338         Bytes = Flags.getByValSize();
339       } else
340         return false;
341     }
342   } else if (auto *Ld = dyn_cast<LoadSDNode>(Arg)) {
343     if (Flags.isByVal())
344       // ByVal argument is passed in as a pointer but it's now being
345       // dereferenced. e.g.
346       // define @foo(%struct.X* %A) {
347       //   tail call @bar(%struct.X* byval %A)
348       // }
349       return false;
350     SDValue Ptr = Ld->getBasePtr();
351     FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
352     if (!FINode)
353       return false;
354     FI = FINode->getIndex();
355   } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
356     FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
357     FI = FINode->getIndex();
358     Bytes = Flags.getByValSize();
359   } else
360     return false;
361 
362   assert(FI != INT_MAX);
363   if (!MFI.isFixedObjectIndex(FI))
364     return false;
365 
366   if (Offset != MFI.getObjectOffset(FI))
367     return false;
368 
369   if (VA.getLocVT().getSizeInBits() > Arg.getValueType().getSizeInBits()) {
370     // If the argument location is wider than the argument type, check that any
371     // extension flags match.
372     if (Flags.isZExt() != MFI.isObjectZExt(FI) ||
373         Flags.isSExt() != MFI.isObjectSExt(FI)) {
374       return false;
375     }
376   }
377 
378   return Bytes == MFI.getObjectSize(FI);
379 }
380 
381 SDValue
382 M68kTargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
383   MachineFunction &MF = DAG.getMachineFunction();
384   M68kMachineFunctionInfo *FuncInfo = MF.getInfo<M68kMachineFunctionInfo>();
385   int ReturnAddrIndex = FuncInfo->getRAIndex();
386 
387   if (ReturnAddrIndex == 0) {
388     // Set up a frame object for the return address.
389     unsigned SlotSize = Subtarget.getSlotSize();
390     ReturnAddrIndex = MF.getFrameInfo().CreateFixedObject(
391         SlotSize, -(int64_t)SlotSize, false);
392     FuncInfo->setRAIndex(ReturnAddrIndex);
393   }
394 
395   return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy(DAG.getDataLayout()));
396 }
397 
398 SDValue M68kTargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
399                                                     SDValue &OutRetAddr,
400                                                     SDValue Chain,
401                                                     bool IsTailCall, int FPDiff,
402                                                     const SDLoc &DL) const {
403   EVT VT = getPointerTy(DAG.getDataLayout());
404   OutRetAddr = getReturnAddressFrameIndex(DAG);
405 
406   // Load the "old" Return address.
407   OutRetAddr = DAG.getLoad(VT, DL, Chain, OutRetAddr, MachinePointerInfo());
408   return SDValue(OutRetAddr.getNode(), 1);
409 }
410 
411 SDValue M68kTargetLowering::EmitTailCallStoreRetAddr(
412     SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, SDValue RetFI,
413     EVT PtrVT, unsigned SlotSize, int FPDiff, const SDLoc &DL) const {
414   if (!FPDiff)
415     return Chain;
416 
417   // Calculate the new stack slot for the return address.
418   int NewFO = MF.getFrameInfo().CreateFixedObject(
419       SlotSize, (int64_t)FPDiff - SlotSize, false);
420 
421   SDValue NewFI = DAG.getFrameIndex(NewFO, PtrVT);
422   // Store the return address to the appropriate stack slot.
423   Chain = DAG.getStore(
424       Chain, DL, RetFI, NewFI,
425       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), NewFO));
426   return Chain;
427 }
428 
429 SDValue
430 M68kTargetLowering::LowerMemArgument(SDValue Chain, CallingConv::ID CallConv,
431                                      const SmallVectorImpl<ISD::InputArg> &Ins,
432                                      const SDLoc &DL, SelectionDAG &DAG,
433                                      const CCValAssign &VA,
434                                      MachineFrameInfo &MFI,
435                                      unsigned ArgIdx) const {
436   // Create the nodes corresponding to a load from this parameter slot.
437   ISD::ArgFlagsTy Flags = Ins[ArgIdx].Flags;
438   EVT ValVT;
439 
440   // If value is passed by pointer we have address passed instead of the value
441   // itself.
442   if (VA.getLocInfo() == CCValAssign::Indirect)
443     ValVT = VA.getLocVT();
444   else
445     ValVT = VA.getValVT();
446 
447   // Because we are dealing with BE architecture we need to offset loading of
448   // partial types
449   int Offset = VA.getLocMemOffset();
450   if (VA.getValVT() == MVT::i8) {
451     Offset += 3;
452   } else if (VA.getValVT() == MVT::i16) {
453     Offset += 2;
454   }
455 
456   // TODO Interrupt handlers
457   // Calculate SP offset of interrupt parameter, re-arrange the slot normally
458   // taken by a return address.
459 
460   // FIXME For now, all byval parameter objects are marked mutable. This can
461   // be changed with more analysis. In case of tail call optimization mark all
462   // arguments mutable. Since they could be overwritten by lowering of arguments
463   // in case of a tail call.
464   bool AlwaysUseMutable = shouldGuaranteeTCO(
465       CallConv, DAG.getTarget().Options.GuaranteedTailCallOpt);
466   bool IsImmutable = !AlwaysUseMutable && !Flags.isByVal();
467 
468   if (Flags.isByVal()) {
469     unsigned Bytes = Flags.getByValSize();
470     if (Bytes == 0)
471       Bytes = 1; // Don't create zero-sized stack objects.
472     int FI = MFI.CreateFixedObject(Bytes, Offset, IsImmutable);
473     // TODO Interrupt handlers
474     // Adjust SP offset of interrupt parameter.
475     return DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
476   } else {
477     int FI =
478         MFI.CreateFixedObject(ValVT.getSizeInBits() / 8, Offset, IsImmutable);
479 
480     // Set SExt or ZExt flag.
481     if (VA.getLocInfo() == CCValAssign::ZExt) {
482       MFI.setObjectZExt(FI, true);
483     } else if (VA.getLocInfo() == CCValAssign::SExt) {
484       MFI.setObjectSExt(FI, true);
485     }
486 
487     // TODO Interrupt handlers
488     // Adjust SP offset of interrupt parameter.
489 
490     SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
491     SDValue Val = DAG.getLoad(
492         ValVT, DL, Chain, FIN,
493         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
494     return VA.isExtInLoc() ? DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val)
495                            : Val;
496   }
497 }
498 
499 SDValue M68kTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
500                                              SDValue Arg, const SDLoc &DL,
501                                              SelectionDAG &DAG,
502                                              const CCValAssign &VA,
503                                              ISD::ArgFlagsTy Flags) const {
504   unsigned LocMemOffset = VA.getLocMemOffset();
505   SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, DL);
506   PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()),
507                        StackPtr, PtrOff);
508   if (Flags.isByVal())
509     return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, DL);
510 
511   return DAG.getStore(
512       Chain, DL, Arg, PtrOff,
513       MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset));
514 }
515 
516 //===----------------------------------------------------------------------===//
517 //                                   Call
518 //===----------------------------------------------------------------------===//
519 
520 SDValue M68kTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
521                                       SmallVectorImpl<SDValue> &InVals) const {
522   SelectionDAG &DAG = CLI.DAG;
523   SDLoc &DL = CLI.DL;
524   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
525   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
526   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
527   SDValue Chain = CLI.Chain;
528   SDValue Callee = CLI.Callee;
529   CallingConv::ID CallConv = CLI.CallConv;
530   bool &IsTailCall = CLI.IsTailCall;
531   bool IsVarArg = CLI.IsVarArg;
532 
533   MachineFunction &MF = DAG.getMachineFunction();
534   StructReturnType SR = callIsStructReturn(Outs);
535   bool IsSibcall = false;
536   M68kMachineFunctionInfo *MFI = MF.getInfo<M68kMachineFunctionInfo>();
537   // const M68kRegisterInfo *TRI = Subtarget.getRegisterInfo();
538 
539   if (CallConv == CallingConv::M68k_INTR)
540     report_fatal_error("M68k interrupts may not be called directly");
541 
542   auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
543   if (Attr.getValueAsBool())
544     IsTailCall = false;
545 
546   // FIXME Add tailcalls support
547 
548   bool IsMustTail = CLI.CB && CLI.CB->isMustTailCall();
549   if (IsMustTail) {
550     // Force this to be a tail call.  The verifier rules are enough to ensure
551     // that we can lower this successfully without moving the return address
552     // around.
553     IsTailCall = true;
554   } else if (IsTailCall) {
555     // Check if it's really possible to do a tail call.
556     IsTailCall = IsEligibleForTailCallOptimization(
557         Callee, CallConv, IsVarArg, SR != NotStructReturn,
558         MF.getFunction().hasStructRetAttr(), CLI.RetTy, Outs, OutVals, Ins,
559         DAG);
560 
561     // Sibcalls are automatically detected tailcalls which do not require
562     // ABI changes.
563     if (!MF.getTarget().Options.GuaranteedTailCallOpt && IsTailCall)
564       IsSibcall = true;
565 
566     if (IsTailCall)
567       ++NumTailCalls;
568   }
569 
570   assert(!(IsVarArg && canGuaranteeTCO(CallConv)) &&
571          "Var args not supported with calling convention fastcc");
572 
573   // Analyze operands of the call, assigning locations to each operand.
574   SmallVector<CCValAssign, 16> ArgLocs;
575   SmallVector<Type *, 4> ArgTypes;
576   for (const auto &Arg : CLI.getArgs())
577     ArgTypes.emplace_back(Arg.Ty);
578   M68kCCState CCInfo(ArgTypes, CallConv, IsVarArg, MF, ArgLocs,
579                      *DAG.getContext());
580   CCInfo.AnalyzeCallOperands(Outs, CC_M68k);
581 
582   // Get a count of how many bytes are to be pushed on the stack.
583   unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
584   if (IsSibcall) {
585     // This is a sibcall. The memory operands are available in caller's
586     // own caller's stack.
587     NumBytes = 0;
588   } else if (MF.getTarget().Options.GuaranteedTailCallOpt &&
589              canGuaranteeTCO(CallConv)) {
590     NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
591   }
592 
593   int FPDiff = 0;
594   if (IsTailCall && !IsSibcall && !IsMustTail) {
595     // Lower arguments at fp - stackoffset + fpdiff.
596     unsigned NumBytesCallerPushed = MFI->getBytesToPopOnReturn();
597 
598     FPDiff = NumBytesCallerPushed - NumBytes;
599 
600     // Set the delta of movement of the returnaddr stackslot.
601     // But only set if delta is greater than previous delta.
602     if (FPDiff < MFI->getTCReturnAddrDelta())
603       MFI->setTCReturnAddrDelta(FPDiff);
604   }
605 
606   unsigned NumBytesToPush = NumBytes;
607   unsigned NumBytesToPop = NumBytes;
608 
609   // If we have an inalloca argument, all stack space has already been allocated
610   // for us and be right at the top of the stack.  We don't support multiple
611   // arguments passed in memory when using inalloca.
612   if (!Outs.empty() && Outs.back().Flags.isInAlloca()) {
613     NumBytesToPush = 0;
614     if (!ArgLocs.back().isMemLoc())
615       report_fatal_error("cannot use inalloca attribute on a register "
616                          "parameter");
617     if (ArgLocs.back().getLocMemOffset() != 0)
618       report_fatal_error("any parameter with the inalloca attribute must be "
619                          "the only memory argument");
620   }
621 
622   if (!IsSibcall)
623     Chain = DAG.getCALLSEQ_START(Chain, NumBytesToPush,
624                                  NumBytes - NumBytesToPush, DL);
625 
626   SDValue RetFI;
627   // Load return address for tail calls.
628   if (IsTailCall && FPDiff)
629     Chain = EmitTailCallLoadRetAddr(DAG, RetFI, Chain, IsTailCall, FPDiff, DL);
630 
631   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
632   SmallVector<SDValue, 8> MemOpChains;
633   SDValue StackPtr;
634 
635   // Walk the register/memloc assignments, inserting copies/loads.  In the case
636   // of tail call optimization arguments are handle later.
637   const M68kRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
638   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
639     ISD::ArgFlagsTy Flags = Outs[i].Flags;
640 
641     // Skip inalloca arguments, they have already been written.
642     if (Flags.isInAlloca())
643       continue;
644 
645     CCValAssign &VA = ArgLocs[i];
646     EVT RegVT = VA.getLocVT();
647     SDValue Arg = OutVals[i];
648     bool IsByVal = Flags.isByVal();
649 
650     // Promote the value if needed.
651     switch (VA.getLocInfo()) {
652     default:
653       llvm_unreachable("Unknown loc info!");
654     case CCValAssign::Full:
655       break;
656     case CCValAssign::SExt:
657       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, RegVT, Arg);
658       break;
659     case CCValAssign::ZExt:
660       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, RegVT, Arg);
661       break;
662     case CCValAssign::AExt:
663       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, RegVT, Arg);
664       break;
665     case CCValAssign::BCvt:
666       Arg = DAG.getBitcast(RegVT, Arg);
667       break;
668     case CCValAssign::Indirect: {
669       // Store the argument.
670       SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
671       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
672       Chain = DAG.getStore(
673           Chain, DL, Arg, SpillSlot,
674           MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
675       Arg = SpillSlot;
676       break;
677     }
678     }
679 
680     if (VA.isRegLoc()) {
681       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
682     } else if (!IsSibcall && (!IsTailCall || IsByVal)) {
683       assert(VA.isMemLoc());
684       if (!StackPtr.getNode()) {
685         StackPtr = DAG.getCopyFromReg(Chain, DL, RegInfo->getStackRegister(),
686                                       getPointerTy(DAG.getDataLayout()));
687       }
688       MemOpChains.push_back(
689           LowerMemOpCallTo(Chain, StackPtr, Arg, DL, DAG, VA, Flags));
690     }
691   }
692 
693   if (!MemOpChains.empty())
694     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
695 
696   // FIXME Make sure PIC style GOT works as expected
697   // The only time GOT is really needed is for Medium-PIC static data
698   // otherwise we are happy with pc-rel or static references
699 
700   if (IsVarArg && IsMustTail) {
701     const auto &Forwards = MFI->getForwardedMustTailRegParms();
702     for (const auto &F : Forwards) {
703       SDValue Val = DAG.getCopyFromReg(Chain, DL, F.VReg, F.VT);
704       RegsToPass.push_back(std::make_pair(unsigned(F.PReg), Val));
705     }
706   }
707 
708   // For tail calls lower the arguments to the 'real' stack slots.  Sibcalls
709   // don't need this because the eligibility check rejects calls that require
710   // shuffling arguments passed in memory.
711   if (!IsSibcall && IsTailCall) {
712     // Force all the incoming stack arguments to be loaded from the stack
713     // before any new outgoing arguments are stored to the stack, because the
714     // outgoing stack slots may alias the incoming argument stack slots, and
715     // the alias isn't otherwise explicit. This is slightly more conservative
716     // than necessary, because it means that each store effectively depends
717     // on every argument instead of just those arguments it would clobber.
718     SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
719 
720     SmallVector<SDValue, 8> MemOpChains2;
721     SDValue FIN;
722     int FI = 0;
723     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
724       CCValAssign &VA = ArgLocs[i];
725       if (VA.isRegLoc())
726         continue;
727       assert(VA.isMemLoc());
728       SDValue Arg = OutVals[i];
729       ISD::ArgFlagsTy Flags = Outs[i].Flags;
730       // Skip inalloca arguments.  They don't require any work.
731       if (Flags.isInAlloca())
732         continue;
733       // Create frame index.
734       int32_t Offset = VA.getLocMemOffset() + FPDiff;
735       uint32_t OpSize = (VA.getLocVT().getSizeInBits() + 7) / 8;
736       FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true);
737       FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
738 
739       if (Flags.isByVal()) {
740         // Copy relative to framepointer.
741         SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset(), DL);
742         if (!StackPtr.getNode()) {
743           StackPtr = DAG.getCopyFromReg(Chain, DL, RegInfo->getStackRegister(),
744                                         getPointerTy(DAG.getDataLayout()));
745         }
746         Source = DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()),
747                              StackPtr, Source);
748 
749         MemOpChains2.push_back(
750             CreateCopyOfByValArgument(Source, FIN, ArgChain, Flags, DAG, DL));
751       } else {
752         // Store relative to framepointer.
753         MemOpChains2.push_back(DAG.getStore(
754             ArgChain, DL, Arg, FIN,
755             MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)));
756       }
757     }
758 
759     if (!MemOpChains2.empty())
760       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains2);
761 
762     // Store the return address to the appropriate stack slot.
763     Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetFI,
764                                      getPointerTy(DAG.getDataLayout()),
765                                      Subtarget.getSlotSize(), FPDiff, DL);
766   }
767 
768   // Build a sequence of copy-to-reg nodes chained together with token chain
769   // and flag operands which copy the outgoing args into registers.
770   SDValue InGlue;
771   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
772     Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[i].first,
773                              RegsToPass[i].second, InGlue);
774     InGlue = Chain.getValue(1);
775   }
776 
777   if (Callee->getOpcode() == ISD::GlobalAddress) {
778     // If the callee is a GlobalAddress node (quite common, every direct call
779     // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
780     // it.
781     GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee);
782 
783     // We should use extra load for direct calls to dllimported functions in
784     // non-JIT mode.
785     const GlobalValue *GV = G->getGlobal();
786     if (!GV->hasDLLImportStorageClass()) {
787       unsigned char OpFlags = Subtarget.classifyGlobalFunctionReference(GV);
788 
789       Callee = DAG.getTargetGlobalAddress(
790           GV, DL, getPointerTy(DAG.getDataLayout()), G->getOffset(), OpFlags);
791 
792       if (OpFlags == M68kII::MO_GOTPCREL) {
793 
794         // Add a wrapper.
795         Callee = DAG.getNode(M68kISD::WrapperPC, DL,
796                              getPointerTy(DAG.getDataLayout()), Callee);
797 
798         // Add extra indirection
799         Callee = DAG.getLoad(
800             getPointerTy(DAG.getDataLayout()), DL, DAG.getEntryNode(), Callee,
801             MachinePointerInfo::getGOT(DAG.getMachineFunction()));
802       }
803     }
804   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
805     const Module *Mod = DAG.getMachineFunction().getFunction().getParent();
806     unsigned char OpFlags =
807         Subtarget.classifyGlobalFunctionReference(nullptr, *Mod);
808 
809     Callee = DAG.getTargetExternalSymbol(
810         S->getSymbol(), getPointerTy(DAG.getDataLayout()), OpFlags);
811   }
812 
813   // Returns a chain & a flag for retval copy to use.
814   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
815   SmallVector<SDValue, 8> Ops;
816 
817   if (!IsSibcall && IsTailCall) {
818     Chain = DAG.getCALLSEQ_END(Chain, NumBytesToPop, 0, InGlue, DL);
819     InGlue = Chain.getValue(1);
820   }
821 
822   Ops.push_back(Chain);
823   Ops.push_back(Callee);
824 
825   if (IsTailCall)
826     Ops.push_back(DAG.getConstant(FPDiff, DL, MVT::i32));
827 
828   // Add argument registers to the end of the list so that they are known live
829   // into the call.
830   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
831     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
832                                   RegsToPass[i].second.getValueType()));
833 
834   // Add a register mask operand representing the call-preserved registers.
835   const uint32_t *Mask = RegInfo->getCallPreservedMask(MF, CallConv);
836   assert(Mask && "Missing call preserved mask for calling convention");
837 
838   Ops.push_back(DAG.getRegisterMask(Mask));
839 
840   if (InGlue.getNode())
841     Ops.push_back(InGlue);
842 
843   if (IsTailCall) {
844     MF.getFrameInfo().setHasTailCall();
845     return DAG.getNode(M68kISD::TC_RETURN, DL, NodeTys, Ops);
846   }
847 
848   Chain = DAG.getNode(M68kISD::CALL, DL, NodeTys, Ops);
849   InGlue = Chain.getValue(1);
850 
851   // Create the CALLSEQ_END node.
852   unsigned NumBytesForCalleeToPop;
853   if (M68k::isCalleePop(CallConv, IsVarArg,
854                         DAG.getTarget().Options.GuaranteedTailCallOpt)) {
855     NumBytesForCalleeToPop = NumBytes; // Callee pops everything
856   } else if (!canGuaranteeTCO(CallConv) && SR == StackStructReturn) {
857     // If this is a call to a struct-return function, the callee
858     // pops the hidden struct pointer, so we have to push it back.
859     NumBytesForCalleeToPop = 4;
860   } else {
861     NumBytesForCalleeToPop = 0; // Callee pops nothing.
862   }
863 
864   if (CLI.DoesNotReturn && !getTargetMachine().Options.TrapUnreachable) {
865     // No need to reset the stack after the call if the call doesn't return. To
866     // make the MI verify, we'll pretend the callee does it for us.
867     NumBytesForCalleeToPop = NumBytes;
868   }
869 
870   // Returns a flag for retval copy to use.
871   if (!IsSibcall) {
872     Chain = DAG.getCALLSEQ_END(Chain, NumBytesToPop, NumBytesForCalleeToPop,
873                                InGlue, DL);
874     InGlue = Chain.getValue(1);
875   }
876 
877   // Handle result values, copying them out of physregs into vregs that we
878   // return.
879   return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,
880                          InVals);
881 }
882 
883 SDValue M68kTargetLowering::LowerCallResult(
884     SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,
885     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
886     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
887 
888   // Assign locations to each value returned by this call.
889   SmallVector<CCValAssign, 16> RVLocs;
890   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
891                  *DAG.getContext());
892   CCInfo.AnalyzeCallResult(Ins, RetCC_M68k);
893 
894   // Copy all of the result registers out of their specified physreg.
895   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
896     CCValAssign &VA = RVLocs[i];
897     EVT CopyVT = VA.getLocVT();
898 
899     /// ??? is this correct?
900     Chain = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), CopyVT, InGlue)
901                 .getValue(1);
902     SDValue Val = Chain.getValue(0);
903 
904     if (VA.isExtInLoc() && VA.getValVT().getScalarType() == MVT::i1)
905       Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
906 
907     InGlue = Chain.getValue(2);
908     InVals.push_back(Val);
909   }
910 
911   return Chain;
912 }
913 
914 //===----------------------------------------------------------------------===//
915 //            Formal Arguments Calling Convention Implementation
916 //===----------------------------------------------------------------------===//
917 
918 SDValue M68kTargetLowering::LowerFormalArguments(
919     SDValue Chain, CallingConv::ID CCID, bool IsVarArg,
920     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
921     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
922   MachineFunction &MF = DAG.getMachineFunction();
923   M68kMachineFunctionInfo *MMFI = MF.getInfo<M68kMachineFunctionInfo>();
924   // const TargetFrameLowering &TFL = *Subtarget.getFrameLowering();
925 
926   MachineFrameInfo &MFI = MF.getFrameInfo();
927 
928   // Assign locations to all of the incoming arguments.
929   SmallVector<CCValAssign, 16> ArgLocs;
930   SmallVector<Type *, 4> ArgTypes;
931   for (const Argument &Arg : MF.getFunction().args())
932     ArgTypes.emplace_back(Arg.getType());
933   M68kCCState CCInfo(ArgTypes, CCID, IsVarArg, MF, ArgLocs, *DAG.getContext());
934 
935   CCInfo.AnalyzeFormalArguments(Ins, CC_M68k);
936 
937   unsigned LastVal = ~0U;
938   SDValue ArgValue;
939   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
940     CCValAssign &VA = ArgLocs[i];
941     assert(VA.getValNo() != LastVal && "Same value in different locations");
942 
943     LastVal = VA.getValNo();
944 
945     if (VA.isRegLoc()) {
946       EVT RegVT = VA.getLocVT();
947       const TargetRegisterClass *RC;
948       if (RegVT == MVT::i32)
949         RC = &M68k::XR32RegClass;
950       else
951         llvm_unreachable("Unknown argument type!");
952 
953       Register Reg = MF.addLiveIn(VA.getLocReg(), RC);
954       ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
955 
956       // If this is an 8 or 16-bit value, it is really passed promoted to 32
957       // bits.  Insert an assert[sz]ext to capture this, then truncate to the
958       // right size.
959       if (VA.getLocInfo() == CCValAssign::SExt) {
960         ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,
961                                DAG.getValueType(VA.getValVT()));
962       } else if (VA.getLocInfo() == CCValAssign::ZExt) {
963         ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,
964                                DAG.getValueType(VA.getValVT()));
965       } else if (VA.getLocInfo() == CCValAssign::BCvt) {
966         ArgValue = DAG.getBitcast(VA.getValVT(), ArgValue);
967       }
968 
969       if (VA.isExtInLoc()) {
970         ArgValue = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), ArgValue);
971       }
972     } else {
973       assert(VA.isMemLoc());
974       ArgValue = LowerMemArgument(Chain, CCID, Ins, DL, DAG, VA, MFI, i);
975     }
976 
977     // If value is passed via pointer - do a load.
978     // TODO Make sure this handling on indirect arguments is correct
979     if (VA.getLocInfo() == CCValAssign::Indirect)
980       ArgValue =
981           DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue, MachinePointerInfo());
982 
983     InVals.push_back(ArgValue);
984   }
985 
986   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
987     // Swift calling convention does not require we copy the sret argument
988     // into %D0 for the return. We don't set SRetReturnReg for Swift.
989     if (CCID == CallingConv::Swift)
990       continue;
991 
992     // ABI require that for returning structs by value we copy the sret argument
993     // into %D0 for the return. Save the argument into a virtual register so
994     // that we can access it from the return points.
995     if (Ins[i].Flags.isSRet()) {
996       unsigned Reg = MMFI->getSRetReturnReg();
997       if (!Reg) {
998         MVT PtrTy = getPointerTy(DAG.getDataLayout());
999         Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
1000         MMFI->setSRetReturnReg(Reg);
1001       }
1002       SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
1003       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
1004       break;
1005     }
1006   }
1007 
1008   unsigned StackSize = CCInfo.getStackSize();
1009   // Align stack specially for tail calls.
1010   if (shouldGuaranteeTCO(CCID, MF.getTarget().Options.GuaranteedTailCallOpt))
1011     StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
1012 
1013   // If the function takes variable number of arguments, make a frame index for
1014   // the start of the first vararg value... for expansion of llvm.va_start. We
1015   // can skip this if there are no va_start calls.
1016   if (MFI.hasVAStart()) {
1017     MMFI->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
1018   }
1019 
1020   if (IsVarArg && MFI.hasMustTailInVarArgFunc()) {
1021     // We forward some GPRs and some vector types.
1022     SmallVector<MVT, 2> RegParmTypes;
1023     MVT IntVT = MVT::i32;
1024     RegParmTypes.push_back(IntVT);
1025 
1026     // Compute the set of forwarded registers. The rest are scratch.
1027     // ??? what is this for?
1028     SmallVectorImpl<ForwardedRegister> &Forwards =
1029         MMFI->getForwardedMustTailRegParms();
1030     CCInfo.analyzeMustTailForwardedRegisters(Forwards, RegParmTypes, CC_M68k);
1031 
1032     // Copy all forwards from physical to virtual registers.
1033     for (ForwardedRegister &F : Forwards) {
1034       // FIXME Can we use a less constrained schedule?
1035       SDValue RegVal = DAG.getCopyFromReg(Chain, DL, F.VReg, F.VT);
1036       F.VReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(F.VT));
1037       Chain = DAG.getCopyToReg(Chain, DL, F.VReg, RegVal);
1038     }
1039   }
1040 
1041   // Some CCs need callee pop.
1042   if (M68k::isCalleePop(CCID, IsVarArg,
1043                         MF.getTarget().Options.GuaranteedTailCallOpt)) {
1044     MMFI->setBytesToPopOnReturn(StackSize); // Callee pops everything.
1045   } else {
1046     MMFI->setBytesToPopOnReturn(0); // Callee pops nothing.
1047     // If this is an sret function, the return should pop the hidden pointer.
1048     if (!canGuaranteeTCO(CCID) && argsAreStructReturn(Ins) == StackStructReturn)
1049       MMFI->setBytesToPopOnReturn(4);
1050   }
1051 
1052   MMFI->setArgumentStackSize(StackSize);
1053 
1054   return Chain;
1055 }
1056 
1057 //===----------------------------------------------------------------------===//
1058 //              Return Value Calling Convention Implementation
1059 //===----------------------------------------------------------------------===//
1060 
1061 bool M68kTargetLowering::CanLowerReturn(
1062     CallingConv::ID CCID, MachineFunction &MF, bool IsVarArg,
1063     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1064   SmallVector<CCValAssign, 16> RVLocs;
1065   CCState CCInfo(CCID, IsVarArg, MF, RVLocs, Context);
1066   return CCInfo.CheckReturn(Outs, RetCC_M68k);
1067 }
1068 
1069 SDValue
1070 M68kTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CCID,
1071                                 bool IsVarArg,
1072                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
1073                                 const SmallVectorImpl<SDValue> &OutVals,
1074                                 const SDLoc &DL, SelectionDAG &DAG) const {
1075   MachineFunction &MF = DAG.getMachineFunction();
1076   M68kMachineFunctionInfo *MFI = MF.getInfo<M68kMachineFunctionInfo>();
1077 
1078   SmallVector<CCValAssign, 16> RVLocs;
1079   CCState CCInfo(CCID, IsVarArg, MF, RVLocs, *DAG.getContext());
1080   CCInfo.AnalyzeReturn(Outs, RetCC_M68k);
1081 
1082   SDValue Glue;
1083   SmallVector<SDValue, 6> RetOps;
1084   // Operand #0 = Chain (updated below)
1085   RetOps.push_back(Chain);
1086   // Operand #1 = Bytes To Pop
1087   RetOps.push_back(
1088       DAG.getTargetConstant(MFI->getBytesToPopOnReturn(), DL, MVT::i32));
1089 
1090   // Copy the result values into the output registers.
1091   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
1092     CCValAssign &VA = RVLocs[i];
1093     assert(VA.isRegLoc() && "Can only return in registers!");
1094     SDValue ValToCopy = OutVals[i];
1095     EVT ValVT = ValToCopy.getValueType();
1096 
1097     // Promote values to the appropriate types.
1098     if (VA.getLocInfo() == CCValAssign::SExt)
1099       ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), ValToCopy);
1100     else if (VA.getLocInfo() == CCValAssign::ZExt)
1101       ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), ValToCopy);
1102     else if (VA.getLocInfo() == CCValAssign::AExt) {
1103       if (ValVT.isVector() && ValVT.getVectorElementType() == MVT::i1)
1104         ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), ValToCopy);
1105       else
1106         ValToCopy = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), ValToCopy);
1107     } else if (VA.getLocInfo() == CCValAssign::BCvt)
1108       ValToCopy = DAG.getBitcast(VA.getLocVT(), ValToCopy);
1109 
1110     Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), ValToCopy, Glue);
1111     Glue = Chain.getValue(1);
1112     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1113   }
1114 
1115   // Swift calling convention does not require we copy the sret argument
1116   // into %d0 for the return, and SRetReturnReg is not set for Swift.
1117 
1118   // ABI require that for returning structs by value we copy the sret argument
1119   // into %D0 for the return. Save the argument into a virtual register so that
1120   // we can access it from the return points.
1121   //
1122   // Checking Function.hasStructRetAttr() here is insufficient because the IR
1123   // may not have an explicit sret argument. If MFI.CanLowerReturn is
1124   // false, then an sret argument may be implicitly inserted in the SelDAG. In
1125   // either case MFI->setSRetReturnReg() will have been called.
1126   if (unsigned SRetReg = MFI->getSRetReturnReg()) {
1127     // ??? Can i just move this to the top and escape this explanation?
1128     // When we have both sret and another return value, we should use the
1129     // original Chain stored in RetOps[0], instead of the current Chain updated
1130     // in the above loop. If we only have sret, RetOps[0] equals to Chain.
1131 
1132     // For the case of sret and another return value, we have
1133     //   Chain_0 at the function entry
1134     //   Chain_1 = getCopyToReg(Chain_0) in the above loop
1135     // If we use Chain_1 in getCopyFromReg, we will have
1136     //   Val = getCopyFromReg(Chain_1)
1137     //   Chain_2 = getCopyToReg(Chain_1, Val) from below
1138 
1139     // getCopyToReg(Chain_0) will be glued together with
1140     // getCopyToReg(Chain_1, Val) into Unit A, getCopyFromReg(Chain_1) will be
1141     // in Unit B, and we will have cyclic dependency between Unit A and Unit B:
1142     //   Data dependency from Unit B to Unit A due to usage of Val in
1143     //     getCopyToReg(Chain_1, Val)
1144     //   Chain dependency from Unit A to Unit B
1145 
1146     // So here, we use RetOps[0] (i.e Chain_0) for getCopyFromReg.
1147     SDValue Val = DAG.getCopyFromReg(RetOps[0], DL, SRetReg,
1148                                      getPointerTy(MF.getDataLayout()));
1149 
1150     // ??? How will this work if CC does not use registers for args passing?
1151     // ??? What if I return multiple structs?
1152     unsigned RetValReg = M68k::D0;
1153     Chain = DAG.getCopyToReg(Chain, DL, RetValReg, Val, Glue);
1154     Glue = Chain.getValue(1);
1155 
1156     RetOps.push_back(
1157         DAG.getRegister(RetValReg, getPointerTy(DAG.getDataLayout())));
1158   }
1159 
1160   RetOps[0] = Chain; // Update chain.
1161 
1162   // Add the glue if we have it.
1163   if (Glue.getNode())
1164     RetOps.push_back(Glue);
1165 
1166   return DAG.getNode(M68kISD::RET, DL, MVT::Other, RetOps);
1167 }
1168 
1169 //===----------------------------------------------------------------------===//
1170 //                Fast Calling Convention (tail call) implementation
1171 //===----------------------------------------------------------------------===//
1172 
1173 //  Like std call, callee cleans arguments, convention except that ECX is
1174 //  reserved for storing the tail called function address. Only 2 registers are
1175 //  free for argument passing (inreg). Tail call optimization is performed
1176 //  provided:
1177 //                * tailcallopt is enabled
1178 //                * caller/callee are fastcc
1179 //  On M68k_64 architecture with GOT-style position independent code only
1180 //  local (within module) calls are supported at the moment. To keep the stack
1181 //  aligned according to platform abi the function GetAlignedArgumentStackSize
1182 //  ensures that argument delta is always multiples of stack alignment. (Dynamic
1183 //  linkers need this - darwin's dyld for example) If a tail called function
1184 //  callee has more arguments than the caller the caller needs to make sure that
1185 //  there is room to move the RETADDR to. This is achieved by reserving an area
1186 //  the size of the argument delta right after the original RETADDR, but before
1187 //  the saved framepointer or the spilled registers e.g. caller(arg1, arg2)
1188 //  calls callee(arg1, arg2,arg3,arg4) stack layout:
1189 //    arg1
1190 //    arg2
1191 //    RETADDR
1192 //    [ new RETADDR
1193 //      move area ]
1194 //    (possible EBP)
1195 //    ESI
1196 //    EDI
1197 //    local1 ..
1198 
1199 /// Make the stack size align e.g 16n + 12 aligned for a 16-byte align
1200 /// requirement.
1201 unsigned
1202 M68kTargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1203                                                 SelectionDAG &DAG) const {
1204   const TargetFrameLowering &TFI = *Subtarget.getFrameLowering();
1205   unsigned StackAlignment = TFI.getStackAlignment();
1206   uint64_t AlignMask = StackAlignment - 1;
1207   int64_t Offset = StackSize;
1208   unsigned SlotSize = Subtarget.getSlotSize();
1209   if ((Offset & AlignMask) <= (StackAlignment - SlotSize)) {
1210     // Number smaller than 12 so just add the difference.
1211     Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1212   } else {
1213     // Mask out lower bits, add stackalignment once plus the 12 bytes.
1214     Offset =
1215         ((~AlignMask) & Offset) + StackAlignment + (StackAlignment - SlotSize);
1216   }
1217   return Offset;
1218 }
1219 
1220 /// Check whether the call is eligible for tail call optimization. Targets
1221 /// that want to do tail call optimization should implement this function.
1222 bool M68kTargetLowering::IsEligibleForTailCallOptimization(
1223     SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg,
1224     bool IsCalleeStructRet, bool IsCallerStructRet, Type *RetTy,
1225     const SmallVectorImpl<ISD::OutputArg> &Outs,
1226     const SmallVectorImpl<SDValue> &OutVals,
1227     const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
1228   if (!mayTailCallThisCC(CalleeCC))
1229     return false;
1230 
1231   // If -tailcallopt is specified, make fastcc functions tail-callable.
1232   MachineFunction &MF = DAG.getMachineFunction();
1233   const auto &CallerF = MF.getFunction();
1234 
1235   CallingConv::ID CallerCC = CallerF.getCallingConv();
1236   bool CCMatch = CallerCC == CalleeCC;
1237 
1238   if (DAG.getTarget().Options.GuaranteedTailCallOpt) {
1239     if (canGuaranteeTCO(CalleeCC) && CCMatch)
1240       return true;
1241     return false;
1242   }
1243 
1244   // Look for obvious safe cases to perform tail call optimization that do not
1245   // require ABI changes. This is what gcc calls sibcall.
1246 
1247   // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
1248   // emit a special epilogue.
1249   const M68kRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1250   if (RegInfo->hasStackRealignment(MF))
1251     return false;
1252 
1253   // Also avoid sibcall optimization if either caller or callee uses struct
1254   // return semantics.
1255   if (IsCalleeStructRet || IsCallerStructRet)
1256     return false;
1257 
1258   // Do not sibcall optimize vararg calls unless all arguments are passed via
1259   // registers.
1260   LLVMContext &C = *DAG.getContext();
1261   if (IsVarArg && !Outs.empty()) {
1262 
1263     SmallVector<CCValAssign, 16> ArgLocs;
1264     CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, C);
1265 
1266     CCInfo.AnalyzeCallOperands(Outs, CC_M68k);
1267     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
1268       if (!ArgLocs[i].isRegLoc())
1269         return false;
1270   }
1271 
1272   // Check that the call results are passed in the same way.
1273   if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins, RetCC_M68k,
1274                                   RetCC_M68k))
1275     return false;
1276 
1277   // The callee has to preserve all registers the caller needs to preserve.
1278   const M68kRegisterInfo *TRI = Subtarget.getRegisterInfo();
1279   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1280   if (!CCMatch) {
1281     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
1282     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
1283       return false;
1284   }
1285 
1286   unsigned StackArgsSize = 0;
1287 
1288   // If the callee takes no arguments then go on to check the results of the
1289   // call.
1290   if (!Outs.empty()) {
1291     // Check if stack adjustment is needed. For now, do not do this if any
1292     // argument is passed on the stack.
1293     SmallVector<CCValAssign, 16> ArgLocs;
1294     CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, C);
1295 
1296     CCInfo.AnalyzeCallOperands(Outs, CC_M68k);
1297     StackArgsSize = CCInfo.getStackSize();
1298 
1299     if (StackArgsSize) {
1300       // Check if the arguments are already laid out in the right way as
1301       // the caller's fixed stack objects.
1302       MachineFrameInfo &MFI = MF.getFrameInfo();
1303       const MachineRegisterInfo *MRI = &MF.getRegInfo();
1304       const M68kInstrInfo *TII = Subtarget.getInstrInfo();
1305       for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1306         CCValAssign &VA = ArgLocs[i];
1307         SDValue Arg = OutVals[i];
1308         ISD::ArgFlagsTy Flags = Outs[i].Flags;
1309         if (VA.getLocInfo() == CCValAssign::Indirect)
1310           return false;
1311         if (!VA.isRegLoc()) {
1312           if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, MFI, MRI,
1313                                    TII, VA))
1314             return false;
1315         }
1316       }
1317     }
1318 
1319     bool PositionIndependent = isPositionIndependent();
1320     // If the tailcall address may be in a register, then make sure it's
1321     // possible to register allocate for it. The call address can
1322     // only target %A0 or %A1 since the tail call must be scheduled after
1323     // callee-saved registers are restored. These happen to be the same
1324     // registers used to pass 'inreg' arguments so watch out for those.
1325     if ((!isa<GlobalAddressSDNode>(Callee) &&
1326          !isa<ExternalSymbolSDNode>(Callee)) ||
1327         PositionIndependent) {
1328       unsigned NumInRegs = 0;
1329       // In PIC we need an extra register to formulate the address computation
1330       // for the callee.
1331       unsigned MaxInRegs = PositionIndependent ? 1 : 2;
1332 
1333       for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1334         CCValAssign &VA = ArgLocs[i];
1335         if (!VA.isRegLoc())
1336           continue;
1337         Register Reg = VA.getLocReg();
1338         switch (Reg) {
1339         default:
1340           break;
1341         case M68k::A0:
1342         case M68k::A1:
1343           if (++NumInRegs == MaxInRegs)
1344             return false;
1345           break;
1346         }
1347       }
1348     }
1349 
1350     const MachineRegisterInfo &MRI = MF.getRegInfo();
1351     if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
1352       return false;
1353   }
1354 
1355   bool CalleeWillPop = M68k::isCalleePop(
1356       CalleeCC, IsVarArg, MF.getTarget().Options.GuaranteedTailCallOpt);
1357 
1358   if (unsigned BytesToPop =
1359           MF.getInfo<M68kMachineFunctionInfo>()->getBytesToPopOnReturn()) {
1360     // If we have bytes to pop, the callee must pop them.
1361     bool CalleePopMatches = CalleeWillPop && BytesToPop == StackArgsSize;
1362     if (!CalleePopMatches)
1363       return false;
1364   } else if (CalleeWillPop && StackArgsSize > 0) {
1365     // If we don't have bytes to pop, make sure the callee doesn't pop any.
1366     return false;
1367   }
1368 
1369   return true;
1370 }
1371 
1372 //===----------------------------------------------------------------------===//
1373 // Custom Lower
1374 //===----------------------------------------------------------------------===//
1375 
1376 SDValue M68kTargetLowering::LowerOperation(SDValue Op,
1377                                            SelectionDAG &DAG) const {
1378   switch (Op.getOpcode()) {
1379   default:
1380     llvm_unreachable("Should not custom lower this!");
1381   case ISD::SADDO:
1382   case ISD::UADDO:
1383   case ISD::SSUBO:
1384   case ISD::USUBO:
1385   case ISD::SMULO:
1386   case ISD::UMULO:
1387     return LowerXALUO(Op, DAG);
1388   case ISD::SETCC:
1389     return LowerSETCC(Op, DAG);
1390   case ISD::SETCCCARRY:
1391     return LowerSETCCCARRY(Op, DAG);
1392   case ISD::SELECT:
1393     return LowerSELECT(Op, DAG);
1394   case ISD::BRCOND:
1395     return LowerBRCOND(Op, DAG);
1396   case ISD::ADDC:
1397   case ISD::ADDE:
1398   case ISD::SUBC:
1399   case ISD::SUBE:
1400     return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
1401   case ISD::ConstantPool:
1402     return LowerConstantPool(Op, DAG);
1403   case ISD::GlobalAddress:
1404     return LowerGlobalAddress(Op, DAG);
1405   case ISD::ExternalSymbol:
1406     return LowerExternalSymbol(Op, DAG);
1407   case ISD::BlockAddress:
1408     return LowerBlockAddress(Op, DAG);
1409   case ISD::JumpTable:
1410     return LowerJumpTable(Op, DAG);
1411   case ISD::VASTART:
1412     return LowerVASTART(Op, DAG);
1413   case ISD::DYNAMIC_STACKALLOC:
1414     return LowerDYNAMIC_STACKALLOC(Op, DAG);
1415   case ISD::SHL_PARTS:
1416     return LowerShiftLeftParts(Op, DAG);
1417   case ISD::SRA_PARTS:
1418     return LowerShiftRightParts(Op, DAG, true);
1419   case ISD::SRL_PARTS:
1420     return LowerShiftRightParts(Op, DAG, false);
1421   case ISD::ATOMIC_FENCE:
1422     return LowerATOMICFENCE(Op, DAG);
1423   case ISD::GlobalTLSAddress:
1424     return LowerGlobalTLSAddress(Op, DAG);
1425   }
1426 }
1427 
1428 SDValue M68kTargetLowering::LowerExternalSymbolCall(SelectionDAG &DAG,
1429                                                     SDLoc Loc,
1430                                                     llvm::StringRef SymbolName,
1431                                                     ArgListTy &&ArgList) const {
1432   PointerType *PtrTy = PointerType::get(*DAG.getContext(), 0);
1433   CallLoweringInfo CLI(DAG);
1434   CLI.setDebugLoc(Loc)
1435       .setChain(DAG.getEntryNode())
1436       .setLibCallee(CallingConv::C, PtrTy,
1437                     DAG.getExternalSymbol(SymbolName.data(),
1438                                           getPointerMemTy(DAG.getDataLayout())),
1439                     std::move(ArgList));
1440   return LowerCallTo(CLI).first;
1441 }
1442 
1443 SDValue M68kTargetLowering::getTLSGetAddr(GlobalAddressSDNode *GA,
1444                                           SelectionDAG &DAG,
1445                                           unsigned TargetFlags) const {
1446   SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1447   SDValue TGA = DAG.getTargetGlobalAddress(
1448       GA->getGlobal(), GA, GA->getValueType(0), GA->getOffset(), TargetFlags);
1449   SDValue Arg = DAG.getNode(ISD::ADD, SDLoc(GA), MVT::i32, GOT, TGA);
1450 
1451   PointerType *PtrTy = PointerType::get(*DAG.getContext(), 0);
1452 
1453   ArgListTy Args;
1454   ArgListEntry Entry;
1455   Entry.Node = Arg;
1456   Entry.Ty = PtrTy;
1457   Args.push_back(Entry);
1458   return LowerExternalSymbolCall(DAG, SDLoc(GA), "__tls_get_addr",
1459                                  std::move(Args));
1460 }
1461 
1462 SDValue M68kTargetLowering::getM68kReadTp(SDLoc Loc, SelectionDAG &DAG) const {
1463   return LowerExternalSymbolCall(DAG, Loc, "__m68k_read_tp", ArgListTy());
1464 }
1465 
1466 SDValue M68kTargetLowering::LowerTLSGeneralDynamic(GlobalAddressSDNode *GA,
1467                                                    SelectionDAG &DAG) const {
1468   return getTLSGetAddr(GA, DAG, M68kII::MO_TLSGD);
1469 }
1470 
1471 SDValue M68kTargetLowering::LowerTLSLocalDynamic(GlobalAddressSDNode *GA,
1472                                                  SelectionDAG &DAG) const {
1473   SDValue Addr = getTLSGetAddr(GA, DAG, M68kII::MO_TLSLDM);
1474   SDValue TGA =
1475       DAG.getTargetGlobalAddress(GA->getGlobal(), GA, GA->getValueType(0),
1476                                  GA->getOffset(), M68kII::MO_TLSLD);
1477   return DAG.getNode(ISD::ADD, SDLoc(GA), MVT::i32, TGA, Addr);
1478 }
1479 
1480 SDValue M68kTargetLowering::LowerTLSInitialExec(GlobalAddressSDNode *GA,
1481                                                 SelectionDAG &DAG) const {
1482   SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1483   SDValue Tp = getM68kReadTp(SDLoc(GA), DAG);
1484   SDValue TGA =
1485       DAG.getTargetGlobalAddress(GA->getGlobal(), GA, GA->getValueType(0),
1486                                  GA->getOffset(), M68kII::MO_TLSIE);
1487   SDValue Addr = DAG.getNode(ISD::ADD, SDLoc(GA), MVT::i32, TGA, GOT);
1488   SDValue Offset =
1489       DAG.getLoad(MVT::i32, SDLoc(GA), DAG.getEntryNode(), Addr,
1490                   MachinePointerInfo::getGOT(DAG.getMachineFunction()));
1491 
1492   return DAG.getNode(ISD::ADD, SDLoc(GA), MVT::i32, Offset, Tp);
1493 }
1494 
1495 SDValue M68kTargetLowering::LowerTLSLocalExec(GlobalAddressSDNode *GA,
1496                                               SelectionDAG &DAG) const {
1497   SDValue Tp = getM68kReadTp(SDLoc(GA), DAG);
1498   SDValue TGA =
1499       DAG.getTargetGlobalAddress(GA->getGlobal(), GA, GA->getValueType(0),
1500                                  GA->getOffset(), M68kII::MO_TLSLE);
1501   return DAG.getNode(ISD::ADD, SDLoc(GA), MVT::i32, TGA, Tp);
1502 }
1503 
1504 SDValue M68kTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1505                                                   SelectionDAG &DAG) const {
1506   assert(Subtarget.isTargetELF());
1507 
1508   auto *GA = cast<GlobalAddressSDNode>(Op);
1509   TLSModel::Model AccessModel = DAG.getTarget().getTLSModel(GA->getGlobal());
1510 
1511   switch (AccessModel) {
1512   case TLSModel::GeneralDynamic:
1513     return LowerTLSGeneralDynamic(GA, DAG);
1514   case TLSModel::LocalDynamic:
1515     return LowerTLSLocalDynamic(GA, DAG);
1516   case TLSModel::InitialExec:
1517     return LowerTLSInitialExec(GA, DAG);
1518   case TLSModel::LocalExec:
1519     return LowerTLSLocalExec(GA, DAG);
1520   }
1521 
1522   llvm_unreachable("Unexpected TLS access model type");
1523 }
1524 
1525 bool M68kTargetLowering::decomposeMulByConstant(LLVMContext &Context, EVT VT,
1526                                                 SDValue C) const {
1527   // Shifts and add instructions in M68000 and M68010 support
1528   // up to 32 bits, but mul only has 16-bit variant. So it's almost
1529   // certainly beneficial to lower 8/16/32-bit mul to their
1530   // add / shifts counterparts. But for 64-bits mul, it might be
1531   // safer to just leave it to compiler runtime implementations.
1532   return VT.bitsLE(MVT::i32) || Subtarget.atLeastM68020();
1533 }
1534 
1535 SDValue M68kTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
1536   // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
1537   // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
1538   // looks for this combo and may remove the "setcc" instruction if the "setcc"
1539   // has only one use.
1540   SDNode *N = Op.getNode();
1541   SDValue LHS = N->getOperand(0);
1542   SDValue RHS = N->getOperand(1);
1543   unsigned BaseOp = 0;
1544   unsigned Cond = 0;
1545   SDLoc DL(Op);
1546   switch (Op.getOpcode()) {
1547   default:
1548     llvm_unreachable("Unknown ovf instruction!");
1549   case ISD::SADDO:
1550     BaseOp = M68kISD::ADD;
1551     Cond = M68k::COND_VS;
1552     break;
1553   case ISD::UADDO:
1554     BaseOp = M68kISD::ADD;
1555     Cond = M68k::COND_CS;
1556     break;
1557   case ISD::SSUBO:
1558     BaseOp = M68kISD::SUB;
1559     Cond = M68k::COND_VS;
1560     break;
1561   case ISD::USUBO:
1562     BaseOp = M68kISD::SUB;
1563     Cond = M68k::COND_CS;
1564     break;
1565   }
1566 
1567   // Also sets CCR.
1568   SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i8);
1569   SDValue Arith = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
1570   SDValue SetCC = DAG.getNode(M68kISD::SETCC, DL, N->getValueType(1),
1571                               DAG.getConstant(Cond, DL, MVT::i8),
1572                               SDValue(Arith.getNode(), 1));
1573 
1574   return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Arith, SetCC);
1575 }
1576 
1577 /// Create a BTST (Bit Test) node - Test bit \p BitNo in \p Src and set
1578 /// condition according to equal/not-equal condition code \p CC.
1579 static SDValue getBitTestCondition(SDValue Src, SDValue BitNo, ISD::CondCode CC,
1580                                    const SDLoc &DL, SelectionDAG &DAG) {
1581   // If Src is i8, promote it to i32 with any_extend.  There is no i8 BTST
1582   // instruction.  Since the shift amount is in-range-or-undefined, we know
1583   // that doing a bittest on the i32 value is ok.
1584   if (Src.getValueType() == MVT::i8 || Src.getValueType() == MVT::i16)
1585     Src = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Src);
1586 
1587   // If the operand types disagree, extend the shift amount to match.  Since
1588   // BTST ignores high bits (like shifts) we can use anyextend.
1589   if (Src.getValueType() != BitNo.getValueType())
1590     BitNo = DAG.getNode(ISD::ANY_EXTEND, DL, Src.getValueType(), BitNo);
1591 
1592   SDValue BTST = DAG.getNode(M68kISD::BTST, DL, MVT::i32, Src, BitNo);
1593 
1594   // NOTE BTST sets CCR.Z flag
1595   M68k::CondCode Cond = CC == ISD::SETEQ ? M68k::COND_NE : M68k::COND_EQ;
1596   return DAG.getNode(M68kISD::SETCC, DL, MVT::i8,
1597                      DAG.getConstant(Cond, DL, MVT::i8), BTST);
1598 }
1599 
1600 /// Result of 'and' is compared against zero. Change to a BTST node if possible.
1601 static SDValue LowerAndToBTST(SDValue And, ISD::CondCode CC, const SDLoc &DL,
1602                               SelectionDAG &DAG) {
1603   SDValue Op0 = And.getOperand(0);
1604   SDValue Op1 = And.getOperand(1);
1605   if (Op0.getOpcode() == ISD::TRUNCATE)
1606     Op0 = Op0.getOperand(0);
1607   if (Op1.getOpcode() == ISD::TRUNCATE)
1608     Op1 = Op1.getOperand(0);
1609 
1610   SDValue LHS, RHS;
1611   if (Op1.getOpcode() == ISD::SHL)
1612     std::swap(Op0, Op1);
1613   if (Op0.getOpcode() == ISD::SHL) {
1614     if (isOneConstant(Op0.getOperand(0))) {
1615       // If we looked past a truncate, check that it's only truncating away
1616       // known zeros.
1617       unsigned BitWidth = Op0.getValueSizeInBits();
1618       unsigned AndBitWidth = And.getValueSizeInBits();
1619       if (BitWidth > AndBitWidth) {
1620         auto Known = DAG.computeKnownBits(Op0);
1621         if (Known.countMinLeadingZeros() < BitWidth - AndBitWidth)
1622           return SDValue();
1623       }
1624       LHS = Op1;
1625       RHS = Op0.getOperand(1);
1626     }
1627   } else if (auto *AndRHS = dyn_cast<ConstantSDNode>(Op1)) {
1628     uint64_t AndRHSVal = AndRHS->getZExtValue();
1629     SDValue AndLHS = Op0;
1630 
1631     if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
1632       LHS = AndLHS.getOperand(0);
1633       RHS = AndLHS.getOperand(1);
1634     }
1635 
1636     // Use BTST if the immediate can't be encoded in a TEST instruction.
1637     if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
1638       LHS = AndLHS;
1639       RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), DL, LHS.getValueType());
1640     }
1641   }
1642 
1643   if (LHS.getNode())
1644     return getBitTestCondition(LHS, RHS, CC, DL, DAG);
1645 
1646   return SDValue();
1647 }
1648 
1649 static M68k::CondCode TranslateIntegerM68kCC(ISD::CondCode SetCCOpcode) {
1650   switch (SetCCOpcode) {
1651   default:
1652     llvm_unreachable("Invalid integer condition!");
1653   case ISD::SETEQ:
1654     return M68k::COND_EQ;
1655   case ISD::SETGT:
1656     return M68k::COND_GT;
1657   case ISD::SETGE:
1658     return M68k::COND_GE;
1659   case ISD::SETLT:
1660     return M68k::COND_LT;
1661   case ISD::SETLE:
1662     return M68k::COND_LE;
1663   case ISD::SETNE:
1664     return M68k::COND_NE;
1665   case ISD::SETULT:
1666     return M68k::COND_CS;
1667   case ISD::SETUGE:
1668     return M68k::COND_CC;
1669   case ISD::SETUGT:
1670     return M68k::COND_HI;
1671   case ISD::SETULE:
1672     return M68k::COND_LS;
1673   }
1674 }
1675 
1676 /// Do a one-to-one translation of a ISD::CondCode to the M68k-specific
1677 /// condition code, returning the condition code and the LHS/RHS of the
1678 /// comparison to make.
1679 static unsigned TranslateM68kCC(ISD::CondCode SetCCOpcode, const SDLoc &DL,
1680                                 bool IsFP, SDValue &LHS, SDValue &RHS,
1681                                 SelectionDAG &DAG) {
1682   if (!IsFP) {
1683     if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1684       if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnes()) {
1685         // X > -1   -> X == 0, jump !sign.
1686         RHS = DAG.getConstant(0, DL, RHS.getValueType());
1687         return M68k::COND_PL;
1688       }
1689       if (SetCCOpcode == ISD::SETLT && RHSC->isZero()) {
1690         // X < 0   -> X == 0, jump on sign.
1691         return M68k::COND_MI;
1692       }
1693       if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
1694         // X < 1   -> X <= 0
1695         RHS = DAG.getConstant(0, DL, RHS.getValueType());
1696         return M68k::COND_LE;
1697       }
1698     }
1699 
1700     return TranslateIntegerM68kCC(SetCCOpcode);
1701   }
1702 
1703   // First determine if it is required or is profitable to flip the operands.
1704 
1705   // If LHS is a foldable load, but RHS is not, flip the condition.
1706   if (ISD::isNON_EXTLoad(LHS.getNode()) && !ISD::isNON_EXTLoad(RHS.getNode())) {
1707     SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1708     std::swap(LHS, RHS);
1709   }
1710 
1711   switch (SetCCOpcode) {
1712   default:
1713     break;
1714   case ISD::SETOLT:
1715   case ISD::SETOLE:
1716   case ISD::SETUGT:
1717   case ISD::SETUGE:
1718     std::swap(LHS, RHS);
1719     break;
1720   }
1721 
1722   // On a floating point condition, the flags are set as follows:
1723   // ZF  PF  CF   op
1724   //  0 | 0 | 0 | X > Y
1725   //  0 | 0 | 1 | X < Y
1726   //  1 | 0 | 0 | X == Y
1727   //  1 | 1 | 1 | unordered
1728   switch (SetCCOpcode) {
1729   default:
1730     llvm_unreachable("Condcode should be pre-legalized away");
1731   case ISD::SETUEQ:
1732   case ISD::SETEQ:
1733     return M68k::COND_EQ;
1734   case ISD::SETOLT: // flipped
1735   case ISD::SETOGT:
1736   case ISD::SETGT:
1737     return M68k::COND_HI;
1738   case ISD::SETOLE: // flipped
1739   case ISD::SETOGE:
1740   case ISD::SETGE:
1741     return M68k::COND_CC;
1742   case ISD::SETUGT: // flipped
1743   case ISD::SETULT:
1744   case ISD::SETLT:
1745     return M68k::COND_CS;
1746   case ISD::SETUGE: // flipped
1747   case ISD::SETULE:
1748   case ISD::SETLE:
1749     return M68k::COND_LS;
1750   case ISD::SETONE:
1751   case ISD::SETNE:
1752     return M68k::COND_NE;
1753   case ISD::SETOEQ:
1754   case ISD::SETUNE:
1755     return M68k::COND_INVALID;
1756   }
1757 }
1758 
1759 // Convert (truncate (srl X, N) to i1) to (bt X, N)
1760 static SDValue LowerTruncateToBTST(SDValue Op, ISD::CondCode CC,
1761                                    const SDLoc &DL, SelectionDAG &DAG) {
1762 
1763   assert(Op.getOpcode() == ISD::TRUNCATE && Op.getValueType() == MVT::i1 &&
1764          "Expected TRUNCATE to i1 node");
1765 
1766   if (Op.getOperand(0).getOpcode() != ISD::SRL)
1767     return SDValue();
1768 
1769   SDValue ShiftRight = Op.getOperand(0);
1770   return getBitTestCondition(ShiftRight.getOperand(0), ShiftRight.getOperand(1),
1771                              CC, DL, DAG);
1772 }
1773 
1774 /// \brief return true if \c Op has a use that doesn't just read flags.
1775 static bool hasNonFlagsUse(SDValue Op) {
1776   for (SDNode::use_iterator UI = Op->use_begin(), UE = Op->use_end(); UI != UE;
1777        ++UI) {
1778     SDNode *User = *UI;
1779     unsigned UOpNo = UI.getOperandNo();
1780     if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
1781       // Look pass truncate.
1782       UOpNo = User->use_begin().getOperandNo();
1783       User = *User->use_begin();
1784     }
1785 
1786     if (User->getOpcode() != ISD::BRCOND && User->getOpcode() != ISD::SETCC &&
1787         !(User->getOpcode() == ISD::SELECT && UOpNo == 0))
1788       return true;
1789   }
1790   return false;
1791 }
1792 
1793 SDValue M68kTargetLowering::EmitTest(SDValue Op, unsigned M68kCC,
1794                                      const SDLoc &DL, SelectionDAG &DAG) const {
1795 
1796   // CF and OF aren't always set the way we want. Determine which
1797   // of these we need.
1798   bool NeedCF = false;
1799   bool NeedOF = false;
1800   switch (M68kCC) {
1801   default:
1802     break;
1803   case M68k::COND_HI:
1804   case M68k::COND_CC:
1805   case M68k::COND_CS:
1806   case M68k::COND_LS:
1807     NeedCF = true;
1808     break;
1809   case M68k::COND_GT:
1810   case M68k::COND_GE:
1811   case M68k::COND_LT:
1812   case M68k::COND_LE:
1813   case M68k::COND_VS:
1814   case M68k::COND_VC: {
1815     // Check if we really need to set the
1816     // Overflow flag. If NoSignedWrap is present
1817     // that is not actually needed.
1818     switch (Op->getOpcode()) {
1819     case ISD::ADD:
1820     case ISD::SUB:
1821     case ISD::MUL:
1822     case ISD::SHL: {
1823       if (Op.getNode()->getFlags().hasNoSignedWrap())
1824         break;
1825       [[fallthrough]];
1826     }
1827     default:
1828       NeedOF = true;
1829       break;
1830     }
1831     break;
1832   }
1833   }
1834   // See if we can use the CCR value from the operand instead of
1835   // doing a separate TEST. TEST always sets OF and CF to 0, so unless
1836   // we prove that the arithmetic won't overflow, we can't use OF or CF.
1837   if (Op.getResNo() != 0 || NeedOF || NeedCF) {
1838     // Emit a CMP with 0, which is the TEST pattern.
1839     return DAG.getNode(M68kISD::CMP, DL, MVT::i8,
1840                        DAG.getConstant(0, DL, Op.getValueType()), Op);
1841   }
1842   unsigned Opcode = 0;
1843   unsigned NumOperands = 0;
1844 
1845   // Truncate operations may prevent the merge of the SETCC instruction
1846   // and the arithmetic instruction before it. Attempt to truncate the operands
1847   // of the arithmetic instruction and use a reduced bit-width instruction.
1848   bool NeedTruncation = false;
1849   SDValue ArithOp = Op;
1850   if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
1851     SDValue Arith = Op->getOperand(0);
1852     // Both the trunc and the arithmetic op need to have one user each.
1853     if (Arith->hasOneUse())
1854       switch (Arith.getOpcode()) {
1855       default:
1856         break;
1857       case ISD::ADD:
1858       case ISD::SUB:
1859       case ISD::AND:
1860       case ISD::OR:
1861       case ISD::XOR: {
1862         NeedTruncation = true;
1863         ArithOp = Arith;
1864       }
1865       }
1866   }
1867 
1868   // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
1869   // which may be the result of a CAST.  We use the variable 'Op', which is the
1870   // non-casted variable when we check for possible users.
1871   switch (ArithOp.getOpcode()) {
1872   case ISD::ADD:
1873     Opcode = M68kISD::ADD;
1874     NumOperands = 2;
1875     break;
1876   case ISD::SHL:
1877   case ISD::SRL:
1878     // If we have a constant logical shift that's only used in a comparison
1879     // against zero turn it into an equivalent AND. This allows turning it into
1880     // a TEST instruction later.
1881     if ((M68kCC == M68k::COND_EQ || M68kCC == M68k::COND_NE) &&
1882         Op->hasOneUse() && isa<ConstantSDNode>(Op->getOperand(1)) &&
1883         !hasNonFlagsUse(Op)) {
1884       EVT VT = Op.getValueType();
1885       unsigned BitWidth = VT.getSizeInBits();
1886       unsigned ShAmt = Op->getConstantOperandVal(1);
1887       if (ShAmt >= BitWidth) // Avoid undefined shifts.
1888         break;
1889       APInt Mask = ArithOp.getOpcode() == ISD::SRL
1890                        ? APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt)
1891                        : APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt);
1892       if (!Mask.isSignedIntN(32)) // Avoid large immediates.
1893         break;
1894       Op = DAG.getNode(ISD::AND, DL, VT, Op->getOperand(0),
1895                        DAG.getConstant(Mask, DL, VT));
1896     }
1897     break;
1898 
1899   case ISD::AND:
1900     // If the primary 'and' result isn't used, don't bother using
1901     // M68kISD::AND, because a TEST instruction will be better.
1902     if (!hasNonFlagsUse(Op)) {
1903       SDValue Op0 = ArithOp->getOperand(0);
1904       SDValue Op1 = ArithOp->getOperand(1);
1905       EVT VT = ArithOp.getValueType();
1906       bool IsAndn = isBitwiseNot(Op0) || isBitwiseNot(Op1);
1907       bool IsLegalAndnType = VT == MVT::i32 || VT == MVT::i64;
1908 
1909       // But if we can combine this into an ANDN operation, then create an AND
1910       // now and allow it to be pattern matched into an ANDN.
1911       if (/*!Subtarget.hasBMI() ||*/ !IsAndn || !IsLegalAndnType)
1912         break;
1913     }
1914     [[fallthrough]];
1915   case ISD::SUB:
1916   case ISD::OR:
1917   case ISD::XOR:
1918     // Due to the ISEL shortcoming noted above, be conservative if this op is
1919     // likely to be selected as part of a load-modify-store instruction.
1920     for (const auto *U : Op.getNode()->uses())
1921       if (U->getOpcode() == ISD::STORE)
1922         goto default_case;
1923 
1924     // Otherwise use a regular CCR-setting instruction.
1925     switch (ArithOp.getOpcode()) {
1926     default:
1927       llvm_unreachable("unexpected operator!");
1928     case ISD::SUB:
1929       Opcode = M68kISD::SUB;
1930       break;
1931     case ISD::XOR:
1932       Opcode = M68kISD::XOR;
1933       break;
1934     case ISD::AND:
1935       Opcode = M68kISD::AND;
1936       break;
1937     case ISD::OR:
1938       Opcode = M68kISD::OR;
1939       break;
1940     }
1941 
1942     NumOperands = 2;
1943     break;
1944   case M68kISD::ADD:
1945   case M68kISD::SUB:
1946   case M68kISD::OR:
1947   case M68kISD::XOR:
1948   case M68kISD::AND:
1949     return SDValue(Op.getNode(), 1);
1950   default:
1951   default_case:
1952     break;
1953   }
1954 
1955   // If we found that truncation is beneficial, perform the truncation and
1956   // update 'Op'.
1957   if (NeedTruncation) {
1958     EVT VT = Op.getValueType();
1959     SDValue WideVal = Op->getOperand(0);
1960     EVT WideVT = WideVal.getValueType();
1961     unsigned ConvertedOp = 0;
1962     // Use a target machine opcode to prevent further DAGCombine
1963     // optimizations that may separate the arithmetic operations
1964     // from the setcc node.
1965     switch (WideVal.getOpcode()) {
1966     default:
1967       break;
1968     case ISD::ADD:
1969       ConvertedOp = M68kISD::ADD;
1970       break;
1971     case ISD::SUB:
1972       ConvertedOp = M68kISD::SUB;
1973       break;
1974     case ISD::AND:
1975       ConvertedOp = M68kISD::AND;
1976       break;
1977     case ISD::OR:
1978       ConvertedOp = M68kISD::OR;
1979       break;
1980     case ISD::XOR:
1981       ConvertedOp = M68kISD::XOR;
1982       break;
1983     }
1984 
1985     if (ConvertedOp) {
1986       const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1987       if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
1988         SDValue V0 = DAG.getNode(ISD::TRUNCATE, DL, VT, WideVal.getOperand(0));
1989         SDValue V1 = DAG.getNode(ISD::TRUNCATE, DL, VT, WideVal.getOperand(1));
1990         Op = DAG.getNode(ConvertedOp, DL, VT, V0, V1);
1991       }
1992     }
1993   }
1994 
1995   if (Opcode == 0) {
1996     // Emit a CMP with 0, which is the TEST pattern.
1997     return DAG.getNode(M68kISD::CMP, DL, MVT::i8,
1998                        DAG.getConstant(0, DL, Op.getValueType()), Op);
1999   }
2000   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i8);
2001   SmallVector<SDValue, 4> Ops(Op->op_begin(), Op->op_begin() + NumOperands);
2002 
2003   SDValue New = DAG.getNode(Opcode, DL, VTs, Ops);
2004   DAG.ReplaceAllUsesWith(Op, New);
2005   return SDValue(New.getNode(), 1);
2006 }
2007 
2008 /// \brief Return true if the condition is an unsigned comparison operation.
2009 static bool isM68kCCUnsigned(unsigned M68kCC) {
2010   switch (M68kCC) {
2011   default:
2012     llvm_unreachable("Invalid integer condition!");
2013   case M68k::COND_EQ:
2014   case M68k::COND_NE:
2015   case M68k::COND_CS:
2016   case M68k::COND_HI:
2017   case M68k::COND_LS:
2018   case M68k::COND_CC:
2019     return true;
2020   case M68k::COND_GT:
2021   case M68k::COND_GE:
2022   case M68k::COND_LT:
2023   case M68k::COND_LE:
2024     return false;
2025   }
2026 }
2027 
2028 SDValue M68kTargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned M68kCC,
2029                                     const SDLoc &DL, SelectionDAG &DAG) const {
2030   if (isNullConstant(Op1))
2031     return EmitTest(Op0, M68kCC, DL, DAG);
2032 
2033   assert(!(isa<ConstantSDNode>(Op1) && Op0.getValueType() == MVT::i1) &&
2034          "Unexpected comparison operation for MVT::i1 operands");
2035 
2036   if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
2037        Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
2038     // Only promote the compare up to I32 if it is a 16 bit operation
2039     // with an immediate.  16 bit immediates are to be avoided.
2040     if ((Op0.getValueType() == MVT::i16 &&
2041          (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1))) &&
2042         !DAG.getMachineFunction().getFunction().hasMinSize()) {
2043       unsigned ExtendOp =
2044           isM68kCCUnsigned(M68kCC) ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND;
2045       Op0 = DAG.getNode(ExtendOp, DL, MVT::i32, Op0);
2046       Op1 = DAG.getNode(ExtendOp, DL, MVT::i32, Op1);
2047     }
2048     // Use SUB instead of CMP to enable CSE between SUB and CMP.
2049     SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i8);
2050     SDValue Sub = DAG.getNode(M68kISD::SUB, DL, VTs, Op0, Op1);
2051     return SDValue(Sub.getNode(), 1);
2052   }
2053   return DAG.getNode(M68kISD::CMP, DL, MVT::i8, Op0, Op1);
2054 }
2055 
2056 /// Result of 'and' or 'trunc to i1' is compared against zero.
2057 /// Change to a BTST node if possible.
2058 SDValue M68kTargetLowering::LowerToBTST(SDValue Op, ISD::CondCode CC,
2059                                         const SDLoc &DL,
2060                                         SelectionDAG &DAG) const {
2061   if (Op.getOpcode() == ISD::AND)
2062     return LowerAndToBTST(Op, CC, DL, DAG);
2063   if (Op.getOpcode() == ISD::TRUNCATE && Op.getValueType() == MVT::i1)
2064     return LowerTruncateToBTST(Op, CC, DL, DAG);
2065   return SDValue();
2066 }
2067 
2068 SDValue M68kTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2069   MVT VT = Op.getSimpleValueType();
2070   assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
2071 
2072   SDValue Op0 = Op.getOperand(0);
2073   SDValue Op1 = Op.getOperand(1);
2074   SDLoc DL(Op);
2075   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2076 
2077   // Optimize to BTST if possible.
2078   // Lower (X & (1 << N)) == 0 to BTST(X, N).
2079   // Lower ((X >>u N) & 1) != 0 to BTST(X, N).
2080   // Lower ((X >>s N) & 1) != 0 to BTST(X, N).
2081   // Lower (trunc (X >> N) to i1) to BTST(X, N).
2082   if (Op0.hasOneUse() && isNullConstant(Op1) &&
2083       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
2084     if (SDValue NewSetCC = LowerToBTST(Op0, CC, DL, DAG)) {
2085       if (VT == MVT::i1)
2086         return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, NewSetCC);
2087       return NewSetCC;
2088     }
2089   }
2090 
2091   // Look for X == 0, X == 1, X != 0, or X != 1.  We can simplify some forms of
2092   // these.
2093   if ((isOneConstant(Op1) || isNullConstant(Op1)) &&
2094       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
2095 
2096     // If the input is a setcc, then reuse the input setcc or use a new one with
2097     // the inverted condition.
2098     if (Op0.getOpcode() == M68kISD::SETCC) {
2099       M68k::CondCode CCode = (M68k::CondCode)Op0.getConstantOperandVal(0);
2100       bool Invert = (CC == ISD::SETNE) ^ isNullConstant(Op1);
2101       if (!Invert)
2102         return Op0;
2103 
2104       CCode = M68k::GetOppositeBranchCondition(CCode);
2105       SDValue SetCC =
2106           DAG.getNode(M68kISD::SETCC, DL, MVT::i8,
2107                       DAG.getConstant(CCode, DL, MVT::i8), Op0.getOperand(1));
2108       if (VT == MVT::i1)
2109         return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC);
2110       return SetCC;
2111     }
2112   }
2113   if (Op0.getValueType() == MVT::i1 && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
2114     if (isOneConstant(Op1)) {
2115       ISD::CondCode NewCC = ISD::GlobalISel::getSetCCInverse(CC, true);
2116       return DAG.getSetCC(DL, VT, Op0, DAG.getConstant(0, DL, MVT::i1), NewCC);
2117     }
2118     if (!isNullConstant(Op1)) {
2119       SDValue Xor = DAG.getNode(ISD::XOR, DL, MVT::i1, Op0, Op1);
2120       return DAG.getSetCC(DL, VT, Xor, DAG.getConstant(0, DL, MVT::i1), CC);
2121     }
2122   }
2123 
2124   bool IsFP = Op1.getSimpleValueType().isFloatingPoint();
2125   unsigned M68kCC = TranslateM68kCC(CC, DL, IsFP, Op0, Op1, DAG);
2126   if (M68kCC == M68k::COND_INVALID)
2127     return SDValue();
2128 
2129   SDValue CCR = EmitCmp(Op0, Op1, M68kCC, DL, DAG);
2130   return DAG.getNode(M68kISD::SETCC, DL, MVT::i8,
2131                      DAG.getConstant(M68kCC, DL, MVT::i8), CCR);
2132 }
2133 
2134 SDValue M68kTargetLowering::LowerSETCCCARRY(SDValue Op,
2135                                             SelectionDAG &DAG) const {
2136   SDValue LHS = Op.getOperand(0);
2137   SDValue RHS = Op.getOperand(1);
2138   SDValue Carry = Op.getOperand(2);
2139   SDValue Cond = Op.getOperand(3);
2140   SDLoc DL(Op);
2141 
2142   assert(LHS.getSimpleValueType().isInteger() && "SETCCCARRY is integer only.");
2143   M68k::CondCode CC = TranslateIntegerM68kCC(cast<CondCodeSDNode>(Cond)->get());
2144 
2145   EVT CarryVT = Carry.getValueType();
2146   APInt NegOne = APInt::getAllOnes(CarryVT.getScalarSizeInBits());
2147   Carry = DAG.getNode(M68kISD::ADD, DL, DAG.getVTList(CarryVT, MVT::i32), Carry,
2148                       DAG.getConstant(NegOne, DL, CarryVT));
2149 
2150   SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
2151   SDValue Cmp =
2152       DAG.getNode(M68kISD::SUBX, DL, VTs, LHS, RHS, Carry.getValue(1));
2153 
2154   return DAG.getNode(M68kISD::SETCC, DL, MVT::i8,
2155                      DAG.getConstant(CC, DL, MVT::i8), Cmp.getValue(1));
2156 }
2157 
2158 /// Return true if opcode is a M68k logical comparison.
2159 static bool isM68kLogicalCmp(SDValue Op) {
2160   unsigned Opc = Op.getNode()->getOpcode();
2161   if (Opc == M68kISD::CMP)
2162     return true;
2163   if (Op.getResNo() == 1 &&
2164       (Opc == M68kISD::ADD || Opc == M68kISD::SUB || Opc == M68kISD::ADDX ||
2165        Opc == M68kISD::SUBX || Opc == M68kISD::SMUL || Opc == M68kISD::UMUL ||
2166        Opc == M68kISD::OR || Opc == M68kISD::XOR || Opc == M68kISD::AND))
2167     return true;
2168 
2169   if (Op.getResNo() == 2 && Opc == M68kISD::UMUL)
2170     return true;
2171 
2172   return false;
2173 }
2174 
2175 static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
2176   if (V.getOpcode() != ISD::TRUNCATE)
2177     return false;
2178 
2179   SDValue VOp0 = V.getOperand(0);
2180   unsigned InBits = VOp0.getValueSizeInBits();
2181   unsigned Bits = V.getValueSizeInBits();
2182   return DAG.MaskedValueIsZero(VOp0,
2183                                APInt::getHighBitsSet(InBits, InBits - Bits));
2184 }
2185 
2186 SDValue M68kTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2187   bool addTest = true;
2188   SDValue Cond = Op.getOperand(0);
2189   SDValue Op1 = Op.getOperand(1);
2190   SDValue Op2 = Op.getOperand(2);
2191   SDLoc DL(Op);
2192   SDValue CC;
2193 
2194   if (Cond.getOpcode() == ISD::SETCC) {
2195     if (SDValue NewCond = LowerSETCC(Cond, DAG))
2196       Cond = NewCond;
2197   }
2198 
2199   // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
2200   // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
2201   // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
2202   // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
2203   if (Cond.getOpcode() == M68kISD::SETCC &&
2204       Cond.getOperand(1).getOpcode() == M68kISD::CMP &&
2205       isNullConstant(Cond.getOperand(1).getOperand(0))) {
2206     SDValue Cmp = Cond.getOperand(1);
2207 
2208     unsigned CondCode =
2209         cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
2210 
2211     if ((isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) &&
2212         (CondCode == M68k::COND_EQ || CondCode == M68k::COND_NE)) {
2213       SDValue Y = isAllOnesConstant(Op2) ? Op1 : Op2;
2214 
2215       SDValue CmpOp0 = Cmp.getOperand(1);
2216       // Apply further optimizations for special cases
2217       // (select (x != 0), -1, 0) -> neg & sbb
2218       // (select (x == 0), 0, -1) -> neg & sbb
2219       if (isNullConstant(Y) &&
2220           (isAllOnesConstant(Op1) == (CondCode == M68k::COND_NE))) {
2221 
2222         SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
2223 
2224         SDValue Neg =
2225             DAG.getNode(M68kISD::SUB, DL, VTs,
2226                         DAG.getConstant(0, DL, CmpOp0.getValueType()), CmpOp0);
2227 
2228         SDValue Res = DAG.getNode(M68kISD::SETCC_CARRY, DL, Op.getValueType(),
2229                                   DAG.getConstant(M68k::COND_CS, DL, MVT::i8),
2230                                   SDValue(Neg.getNode(), 1));
2231         return Res;
2232       }
2233 
2234       Cmp = DAG.getNode(M68kISD::CMP, DL, MVT::i8,
2235                         DAG.getConstant(1, DL, CmpOp0.getValueType()), CmpOp0);
2236 
2237       SDValue Res = // Res = 0 or -1.
2238           DAG.getNode(M68kISD::SETCC_CARRY, DL, Op.getValueType(),
2239                       DAG.getConstant(M68k::COND_CS, DL, MVT::i8), Cmp);
2240 
2241       if (isAllOnesConstant(Op1) != (CondCode == M68k::COND_EQ))
2242         Res = DAG.getNOT(DL, Res, Res.getValueType());
2243 
2244       if (!isNullConstant(Op2))
2245         Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
2246       return Res;
2247     }
2248   }
2249 
2250   // Look past (and (setcc_carry (cmp ...)), 1).
2251   if (Cond.getOpcode() == ISD::AND &&
2252       Cond.getOperand(0).getOpcode() == M68kISD::SETCC_CARRY &&
2253       isOneConstant(Cond.getOperand(1)))
2254     Cond = Cond.getOperand(0);
2255 
2256   // If condition flag is set by a M68kISD::CMP, then use it as the condition
2257   // setting operand in place of the M68kISD::SETCC.
2258   unsigned CondOpcode = Cond.getOpcode();
2259   if (CondOpcode == M68kISD::SETCC || CondOpcode == M68kISD::SETCC_CARRY) {
2260     CC = Cond.getOperand(0);
2261 
2262     SDValue Cmp = Cond.getOperand(1);
2263     unsigned Opc = Cmp.getOpcode();
2264 
2265     bool IllegalFPCMov = false;
2266 
2267     if ((isM68kLogicalCmp(Cmp) && !IllegalFPCMov) || Opc == M68kISD::BTST) {
2268       Cond = Cmp;
2269       addTest = false;
2270     }
2271   } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
2272              CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
2273              CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) {
2274     SDValue LHS = Cond.getOperand(0);
2275     SDValue RHS = Cond.getOperand(1);
2276     unsigned MxOpcode;
2277     unsigned MxCond;
2278     SDVTList VTs;
2279     switch (CondOpcode) {
2280     case ISD::UADDO:
2281       MxOpcode = M68kISD::ADD;
2282       MxCond = M68k::COND_CS;
2283       break;
2284     case ISD::SADDO:
2285       MxOpcode = M68kISD::ADD;
2286       MxCond = M68k::COND_VS;
2287       break;
2288     case ISD::USUBO:
2289       MxOpcode = M68kISD::SUB;
2290       MxCond = M68k::COND_CS;
2291       break;
2292     case ISD::SSUBO:
2293       MxOpcode = M68kISD::SUB;
2294       MxCond = M68k::COND_VS;
2295       break;
2296     case ISD::UMULO:
2297       MxOpcode = M68kISD::UMUL;
2298       MxCond = M68k::COND_VS;
2299       break;
2300     case ISD::SMULO:
2301       MxOpcode = M68kISD::SMUL;
2302       MxCond = M68k::COND_VS;
2303       break;
2304     default:
2305       llvm_unreachable("unexpected overflowing operator");
2306     }
2307     if (CondOpcode == ISD::UMULO)
2308       VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(), MVT::i32);
2309     else
2310       VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
2311 
2312     SDValue MxOp = DAG.getNode(MxOpcode, DL, VTs, LHS, RHS);
2313 
2314     if (CondOpcode == ISD::UMULO)
2315       Cond = MxOp.getValue(2);
2316     else
2317       Cond = MxOp.getValue(1);
2318 
2319     CC = DAG.getConstant(MxCond, DL, MVT::i8);
2320     addTest = false;
2321   }
2322 
2323   if (addTest) {
2324     // Look past the truncate if the high bits are known zero.
2325     if (isTruncWithZeroHighBitsInput(Cond, DAG))
2326       Cond = Cond.getOperand(0);
2327 
2328     // We know the result of AND is compared against zero. Try to match
2329     // it to BT.
2330     if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
2331       if (SDValue NewSetCC = LowerToBTST(Cond, ISD::SETNE, DL, DAG)) {
2332         CC = NewSetCC.getOperand(0);
2333         Cond = NewSetCC.getOperand(1);
2334         addTest = false;
2335       }
2336     }
2337   }
2338 
2339   if (addTest) {
2340     CC = DAG.getConstant(M68k::COND_NE, DL, MVT::i8);
2341     Cond = EmitTest(Cond, M68k::COND_NE, DL, DAG);
2342   }
2343 
2344   // a <  b ? -1 :  0 -> RES = ~setcc_carry
2345   // a <  b ?  0 : -1 -> RES = setcc_carry
2346   // a >= b ? -1 :  0 -> RES = setcc_carry
2347   // a >= b ?  0 : -1 -> RES = ~setcc_carry
2348   if (Cond.getOpcode() == M68kISD::SUB) {
2349     unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
2350 
2351     if ((CondCode == M68k::COND_CC || CondCode == M68k::COND_CS) &&
2352         (isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) &&
2353         (isNullConstant(Op1) || isNullConstant(Op2))) {
2354       SDValue Res =
2355           DAG.getNode(M68kISD::SETCC_CARRY, DL, Op.getValueType(),
2356                       DAG.getConstant(M68k::COND_CS, DL, MVT::i8), Cond);
2357       if (isAllOnesConstant(Op1) != (CondCode == M68k::COND_CS))
2358         return DAG.getNOT(DL, Res, Res.getValueType());
2359       return Res;
2360     }
2361   }
2362 
2363   // M68k doesn't have an i8 cmov. If both operands are the result of a
2364   // truncate widen the cmov and push the truncate through. This avoids
2365   // introducing a new branch during isel and doesn't add any extensions.
2366   if (Op.getValueType() == MVT::i8 && Op1.getOpcode() == ISD::TRUNCATE &&
2367       Op2.getOpcode() == ISD::TRUNCATE) {
2368     SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
2369     if (T1.getValueType() == T2.getValueType() &&
2370         // Block CopyFromReg so partial register stalls are avoided.
2371         T1.getOpcode() != ISD::CopyFromReg &&
2372         T2.getOpcode() != ISD::CopyFromReg) {
2373       SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
2374       SDValue Cmov = DAG.getNode(M68kISD::CMOV, DL, VTs, T2, T1, CC, Cond);
2375       return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
2376     }
2377   }
2378 
2379   // M68kISD::CMOV means set the result (which is operand 1) to the RHS if
2380   // condition is true.
2381   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
2382   SDValue Ops[] = {Op2, Op1, CC, Cond};
2383   return DAG.getNode(M68kISD::CMOV, DL, VTs, Ops);
2384 }
2385 
2386 /// Return true if node is an ISD::AND or ISD::OR of two M68k::SETcc nodes
2387 /// each of which has no other use apart from the AND / OR.
2388 static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
2389   Opc = Op.getOpcode();
2390   if (Opc != ISD::OR && Opc != ISD::AND)
2391     return false;
2392   return (M68k::IsSETCC(Op.getOperand(0).getOpcode()) &&
2393           Op.getOperand(0).hasOneUse() &&
2394           M68k::IsSETCC(Op.getOperand(1).getOpcode()) &&
2395           Op.getOperand(1).hasOneUse());
2396 }
2397 
2398 /// Return true if node is an ISD::XOR of a M68kISD::SETCC and 1 and that the
2399 /// SETCC node has a single use.
2400 static bool isXor1OfSetCC(SDValue Op) {
2401   if (Op.getOpcode() != ISD::XOR)
2402     return false;
2403   if (isOneConstant(Op.getOperand(1)))
2404     return Op.getOperand(0).getOpcode() == M68kISD::SETCC &&
2405            Op.getOperand(0).hasOneUse();
2406   return false;
2407 }
2408 
2409 SDValue M68kTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
2410   bool AddTest = true;
2411   SDValue Chain = Op.getOperand(0);
2412   SDValue Cond = Op.getOperand(1);
2413   SDValue Dest = Op.getOperand(2);
2414   SDLoc DL(Op);
2415   SDValue CC;
2416   bool Inverted = false;
2417 
2418   if (Cond.getOpcode() == ISD::SETCC) {
2419     // Check for setcc([su]{add,sub}o == 0).
2420     if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
2421         isNullConstant(Cond.getOperand(1)) &&
2422         Cond.getOperand(0).getResNo() == 1 &&
2423         (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
2424          Cond.getOperand(0).getOpcode() == ISD::UADDO ||
2425          Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
2426          Cond.getOperand(0).getOpcode() == ISD::USUBO)) {
2427       Inverted = true;
2428       Cond = Cond.getOperand(0);
2429     } else {
2430       if (SDValue NewCond = LowerSETCC(Cond, DAG))
2431         Cond = NewCond;
2432     }
2433   }
2434 
2435   // Look pass (and (setcc_carry (cmp ...)), 1).
2436   if (Cond.getOpcode() == ISD::AND &&
2437       Cond.getOperand(0).getOpcode() == M68kISD::SETCC_CARRY &&
2438       isOneConstant(Cond.getOperand(1)))
2439     Cond = Cond.getOperand(0);
2440 
2441   // If condition flag is set by a M68kISD::CMP, then use it as the condition
2442   // setting operand in place of the M68kISD::SETCC.
2443   unsigned CondOpcode = Cond.getOpcode();
2444   if (CondOpcode == M68kISD::SETCC || CondOpcode == M68kISD::SETCC_CARRY) {
2445     CC = Cond.getOperand(0);
2446 
2447     SDValue Cmp = Cond.getOperand(1);
2448     unsigned Opc = Cmp.getOpcode();
2449 
2450     if (isM68kLogicalCmp(Cmp) || Opc == M68kISD::BTST) {
2451       Cond = Cmp;
2452       AddTest = false;
2453     } else {
2454       switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
2455       default:
2456         break;
2457       case M68k::COND_VS:
2458       case M68k::COND_CS:
2459         // These can only come from an arithmetic instruction with overflow,
2460         // e.g. SADDO, UADDO.
2461         Cond = Cond.getNode()->getOperand(1);
2462         AddTest = false;
2463         break;
2464       }
2465     }
2466   }
2467   CondOpcode = Cond.getOpcode();
2468   if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
2469       CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO) {
2470     SDValue LHS = Cond.getOperand(0);
2471     SDValue RHS = Cond.getOperand(1);
2472     unsigned MxOpcode;
2473     unsigned MxCond;
2474     SDVTList VTs;
2475     // Keep this in sync with LowerXALUO, otherwise we might create redundant
2476     // instructions that can't be removed afterwards (i.e. M68kISD::ADD and
2477     // M68kISD::INC).
2478     switch (CondOpcode) {
2479     case ISD::UADDO:
2480       MxOpcode = M68kISD::ADD;
2481       MxCond = M68k::COND_CS;
2482       break;
2483     case ISD::SADDO:
2484       MxOpcode = M68kISD::ADD;
2485       MxCond = M68k::COND_VS;
2486       break;
2487     case ISD::USUBO:
2488       MxOpcode = M68kISD::SUB;
2489       MxCond = M68k::COND_CS;
2490       break;
2491     case ISD::SSUBO:
2492       MxOpcode = M68kISD::SUB;
2493       MxCond = M68k::COND_VS;
2494       break;
2495     case ISD::UMULO:
2496       MxOpcode = M68kISD::UMUL;
2497       MxCond = M68k::COND_VS;
2498       break;
2499     case ISD::SMULO:
2500       MxOpcode = M68kISD::SMUL;
2501       MxCond = M68k::COND_VS;
2502       break;
2503     default:
2504       llvm_unreachable("unexpected overflowing operator");
2505     }
2506 
2507     if (Inverted)
2508       MxCond = M68k::GetOppositeBranchCondition((M68k::CondCode)MxCond);
2509 
2510     if (CondOpcode == ISD::UMULO)
2511       VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(), MVT::i8);
2512     else
2513       VTs = DAG.getVTList(LHS.getValueType(), MVT::i8);
2514 
2515     SDValue MxOp = DAG.getNode(MxOpcode, DL, VTs, LHS, RHS);
2516 
2517     if (CondOpcode == ISD::UMULO)
2518       Cond = MxOp.getValue(2);
2519     else
2520       Cond = MxOp.getValue(1);
2521 
2522     CC = DAG.getConstant(MxCond, DL, MVT::i8);
2523     AddTest = false;
2524   } else {
2525     unsigned CondOpc;
2526     if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
2527       SDValue Cmp = Cond.getOperand(0).getOperand(1);
2528       if (CondOpc == ISD::OR) {
2529         // Also, recognize the pattern generated by an FCMP_UNE. We can emit
2530         // two branches instead of an explicit OR instruction with a
2531         // separate test.
2532         if (Cmp == Cond.getOperand(1).getOperand(1) && isM68kLogicalCmp(Cmp)) {
2533           CC = Cond.getOperand(0).getOperand(0);
2534           Chain = DAG.getNode(M68kISD::BRCOND, DL, Op.getValueType(), Chain,
2535                               Dest, CC, Cmp);
2536           CC = Cond.getOperand(1).getOperand(0);
2537           Cond = Cmp;
2538           AddTest = false;
2539         }
2540       } else { // ISD::AND
2541         // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
2542         // two branches instead of an explicit AND instruction with a
2543         // separate test. However, we only do this if this block doesn't
2544         // have a fall-through edge, because this requires an explicit
2545         // jmp when the condition is false.
2546         if (Cmp == Cond.getOperand(1).getOperand(1) && isM68kLogicalCmp(Cmp) &&
2547             Op.getNode()->hasOneUse()) {
2548           M68k::CondCode CCode =
2549               (M68k::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
2550           CCode = M68k::GetOppositeBranchCondition(CCode);
2551           CC = DAG.getConstant(CCode, DL, MVT::i8);
2552           SDNode *User = *Op.getNode()->use_begin();
2553           // Look for an unconditional branch following this conditional branch.
2554           // We need this because we need to reverse the successors in order
2555           // to implement FCMP_OEQ.
2556           if (User->getOpcode() == ISD::BR) {
2557             SDValue FalseBB = User->getOperand(1);
2558             SDNode *NewBR =
2559                 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
2560             assert(NewBR == User);
2561             (void)NewBR;
2562             Dest = FalseBB;
2563 
2564             Chain = DAG.getNode(M68kISD::BRCOND, DL, Op.getValueType(), Chain,
2565                                 Dest, CC, Cmp);
2566             M68k::CondCode CCode =
2567                 (M68k::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
2568             CCode = M68k::GetOppositeBranchCondition(CCode);
2569             CC = DAG.getConstant(CCode, DL, MVT::i8);
2570             Cond = Cmp;
2571             AddTest = false;
2572           }
2573         }
2574       }
2575     } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
2576       // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
2577       // It should be transformed during dag combiner except when the condition
2578       // is set by a arithmetics with overflow node.
2579       M68k::CondCode CCode =
2580           (M68k::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
2581       CCode = M68k::GetOppositeBranchCondition(CCode);
2582       CC = DAG.getConstant(CCode, DL, MVT::i8);
2583       Cond = Cond.getOperand(0).getOperand(1);
2584       AddTest = false;
2585     }
2586   }
2587 
2588   if (AddTest) {
2589     // Look pass the truncate if the high bits are known zero.
2590     if (isTruncWithZeroHighBitsInput(Cond, DAG))
2591       Cond = Cond.getOperand(0);
2592 
2593     // We know the result is compared against zero. Try to match it to BT.
2594     if (Cond.hasOneUse()) {
2595       if (SDValue NewSetCC = LowerToBTST(Cond, ISD::SETNE, DL, DAG)) {
2596         CC = NewSetCC.getOperand(0);
2597         Cond = NewSetCC.getOperand(1);
2598         AddTest = false;
2599       }
2600     }
2601   }
2602 
2603   if (AddTest) {
2604     M68k::CondCode MxCond = Inverted ? M68k::COND_EQ : M68k::COND_NE;
2605     CC = DAG.getConstant(MxCond, DL, MVT::i8);
2606     Cond = EmitTest(Cond, MxCond, DL, DAG);
2607   }
2608   return DAG.getNode(M68kISD::BRCOND, DL, Op.getValueType(), Chain, Dest, CC,
2609                      Cond);
2610 }
2611 
2612 SDValue M68kTargetLowering::LowerADDC_ADDE_SUBC_SUBE(SDValue Op,
2613                                                      SelectionDAG &DAG) const {
2614   MVT VT = Op.getNode()->getSimpleValueType(0);
2615 
2616   // Let legalize expand this if it isn't a legal type yet.
2617   if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
2618     return SDValue();
2619 
2620   SDVTList VTs = DAG.getVTList(VT, MVT::i8);
2621 
2622   unsigned Opc;
2623   bool ExtraOp = false;
2624   switch (Op.getOpcode()) {
2625   default:
2626     llvm_unreachable("Invalid code");
2627   case ISD::ADDC:
2628     Opc = M68kISD::ADD;
2629     break;
2630   case ISD::ADDE:
2631     Opc = M68kISD::ADDX;
2632     ExtraOp = true;
2633     break;
2634   case ISD::SUBC:
2635     Opc = M68kISD::SUB;
2636     break;
2637   case ISD::SUBE:
2638     Opc = M68kISD::SUBX;
2639     ExtraOp = true;
2640     break;
2641   }
2642 
2643   if (!ExtraOp)
2644     return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1));
2645   return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1),
2646                      Op.getOperand(2));
2647 }
2648 
2649 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2650 // their target countpart wrapped in the M68kISD::Wrapper node. Suppose N is
2651 // one of the above mentioned nodes. It has to be wrapped because otherwise
2652 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2653 // be used to form addressing mode. These wrapped nodes will be selected
2654 // into MOV32ri.
2655 SDValue M68kTargetLowering::LowerConstantPool(SDValue Op,
2656                                               SelectionDAG &DAG) const {
2657   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2658 
2659   // In PIC mode (unless we're in PCRel PIC mode) we add an offset to the
2660   // global base reg.
2661   unsigned char OpFlag = Subtarget.classifyLocalReference(nullptr);
2662 
2663   unsigned WrapperKind = M68kISD::Wrapper;
2664   if (M68kII::isPCRelGlobalReference(OpFlag)) {
2665     WrapperKind = M68kISD::WrapperPC;
2666   }
2667 
2668   MVT PtrVT = getPointerTy(DAG.getDataLayout());
2669   SDValue Result = DAG.getTargetConstantPool(
2670       CP->getConstVal(), PtrVT, CP->getAlign(), CP->getOffset(), OpFlag);
2671 
2672   SDLoc DL(CP);
2673   Result = DAG.getNode(WrapperKind, DL, PtrVT, Result);
2674 
2675   // With PIC, the address is actually $g + Offset.
2676   if (M68kII::isGlobalRelativeToPICBase(OpFlag)) {
2677     Result = DAG.getNode(ISD::ADD, DL, PtrVT,
2678                          DAG.getNode(M68kISD::GLOBAL_BASE_REG, SDLoc(), PtrVT),
2679                          Result);
2680   }
2681 
2682   return Result;
2683 }
2684 
2685 SDValue M68kTargetLowering::LowerExternalSymbol(SDValue Op,
2686                                                 SelectionDAG &DAG) const {
2687   const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2688 
2689   // In PIC mode (unless we're in PCRel PIC mode) we add an offset to the
2690   // global base reg.
2691   const Module *Mod = DAG.getMachineFunction().getFunction().getParent();
2692   unsigned char OpFlag = Subtarget.classifyExternalReference(*Mod);
2693 
2694   unsigned WrapperKind = M68kISD::Wrapper;
2695   if (M68kII::isPCRelGlobalReference(OpFlag)) {
2696     WrapperKind = M68kISD::WrapperPC;
2697   }
2698 
2699   auto PtrVT = getPointerTy(DAG.getDataLayout());
2700   SDValue Result = DAG.getTargetExternalSymbol(Sym, PtrVT, OpFlag);
2701 
2702   SDLoc DL(Op);
2703   Result = DAG.getNode(WrapperKind, DL, PtrVT, Result);
2704 
2705   // With PIC, the address is actually $g + Offset.
2706   if (M68kII::isGlobalRelativeToPICBase(OpFlag)) {
2707     Result = DAG.getNode(ISD::ADD, DL, PtrVT,
2708                          DAG.getNode(M68kISD::GLOBAL_BASE_REG, SDLoc(), PtrVT),
2709                          Result);
2710   }
2711 
2712   // For symbols that require a load from a stub to get the address, emit the
2713   // load.
2714   if (M68kII::isGlobalStubReference(OpFlag)) {
2715     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2716                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
2717   }
2718 
2719   return Result;
2720 }
2721 
2722 SDValue M68kTargetLowering::LowerBlockAddress(SDValue Op,
2723                                               SelectionDAG &DAG) const {
2724   unsigned char OpFlags = Subtarget.classifyBlockAddressReference();
2725   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
2726   int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
2727   SDLoc DL(Op);
2728   auto PtrVT = getPointerTy(DAG.getDataLayout());
2729 
2730   // Create the TargetBlockAddressAddress node.
2731   SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset, OpFlags);
2732 
2733   if (M68kII::isPCRelBlockReference(OpFlags)) {
2734     Result = DAG.getNode(M68kISD::WrapperPC, DL, PtrVT, Result);
2735   } else {
2736     Result = DAG.getNode(M68kISD::Wrapper, DL, PtrVT, Result);
2737   }
2738 
2739   // With PIC, the address is actually $g + Offset.
2740   if (M68kII::isGlobalRelativeToPICBase(OpFlags)) {
2741     Result =
2742         DAG.getNode(ISD::ADD, DL, PtrVT,
2743                     DAG.getNode(M68kISD::GLOBAL_BASE_REG, DL, PtrVT), Result);
2744   }
2745 
2746   return Result;
2747 }
2748 
2749 SDValue M68kTargetLowering::LowerGlobalAddress(const GlobalValue *GV,
2750                                                const SDLoc &DL, int64_t Offset,
2751                                                SelectionDAG &DAG) const {
2752   unsigned char OpFlags = Subtarget.classifyGlobalReference(GV);
2753   auto PtrVT = getPointerTy(DAG.getDataLayout());
2754 
2755   // Create the TargetGlobalAddress node, folding in the constant
2756   // offset if it is legal.
2757   SDValue Result;
2758   if (M68kII::isDirectGlobalReference(OpFlags)) {
2759     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Offset);
2760     Offset = 0;
2761   } else {
2762     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags);
2763   }
2764 
2765   if (M68kII::isPCRelGlobalReference(OpFlags))
2766     Result = DAG.getNode(M68kISD::WrapperPC, DL, PtrVT, Result);
2767   else
2768     Result = DAG.getNode(M68kISD::Wrapper, DL, PtrVT, Result);
2769 
2770   // With PIC, the address is actually $g + Offset.
2771   if (M68kII::isGlobalRelativeToPICBase(OpFlags)) {
2772     Result =
2773         DAG.getNode(ISD::ADD, DL, PtrVT,
2774                     DAG.getNode(M68kISD::GLOBAL_BASE_REG, DL, PtrVT), Result);
2775   }
2776 
2777   // For globals that require a load from a stub to get the address, emit the
2778   // load.
2779   if (M68kII::isGlobalStubReference(OpFlags)) {
2780     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2781                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
2782   }
2783 
2784   // If there was a non-zero offset that we didn't fold, create an explicit
2785   // addition for it.
2786   if (Offset != 0) {
2787     Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
2788                          DAG.getConstant(Offset, DL, PtrVT));
2789   }
2790 
2791   return Result;
2792 }
2793 
2794 SDValue M68kTargetLowering::LowerGlobalAddress(SDValue Op,
2795                                                SelectionDAG &DAG) const {
2796   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2797   int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
2798   return LowerGlobalAddress(GV, SDLoc(Op), Offset, DAG);
2799 }
2800 
2801 //===----------------------------------------------------------------------===//
2802 // Custom Lower Jump Table
2803 //===----------------------------------------------------------------------===//
2804 
2805 SDValue M68kTargetLowering::LowerJumpTable(SDValue Op,
2806                                            SelectionDAG &DAG) const {
2807   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
2808 
2809   // In PIC mode (unless we're in PCRel PIC mode) we add an offset to the
2810   // global base reg.
2811   unsigned char OpFlag = Subtarget.classifyLocalReference(nullptr);
2812 
2813   unsigned WrapperKind = M68kISD::Wrapper;
2814   if (M68kII::isPCRelGlobalReference(OpFlag)) {
2815     WrapperKind = M68kISD::WrapperPC;
2816   }
2817 
2818   auto PtrVT = getPointerTy(DAG.getDataLayout());
2819   SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
2820   SDLoc DL(JT);
2821   Result = DAG.getNode(WrapperKind, DL, PtrVT, Result);
2822 
2823   // With PIC, the address is actually $g + Offset.
2824   if (M68kII::isGlobalRelativeToPICBase(OpFlag)) {
2825     Result = DAG.getNode(ISD::ADD, DL, PtrVT,
2826                          DAG.getNode(M68kISD::GLOBAL_BASE_REG, SDLoc(), PtrVT),
2827                          Result);
2828   }
2829 
2830   return Result;
2831 }
2832 
2833 unsigned M68kTargetLowering::getJumpTableEncoding() const {
2834   return Subtarget.getJumpTableEncoding();
2835 }
2836 
2837 const MCExpr *M68kTargetLowering::LowerCustomJumpTableEntry(
2838     const MachineJumpTableInfo *MJTI, const MachineBasicBlock *MBB,
2839     unsigned uid, MCContext &Ctx) const {
2840   return MCSymbolRefExpr::create(MBB->getSymbol(), MCSymbolRefExpr::VK_GOTOFF,
2841                                  Ctx);
2842 }
2843 
2844 SDValue M68kTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2845                                                      SelectionDAG &DAG) const {
2846   if (getJumpTableEncoding() == MachineJumpTableInfo::EK_Custom32)
2847     return DAG.getNode(M68kISD::GLOBAL_BASE_REG, SDLoc(),
2848                        getPointerTy(DAG.getDataLayout()));
2849 
2850   // MachineJumpTableInfo::EK_LabelDifference32 entry
2851   return Table;
2852 }
2853 
2854 // NOTE This only used for MachineJumpTableInfo::EK_LabelDifference32 entries
2855 const MCExpr *M68kTargetLowering::getPICJumpTableRelocBaseExpr(
2856     const MachineFunction *MF, unsigned JTI, MCContext &Ctx) const {
2857   return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx);
2858 }
2859 
2860 M68kTargetLowering::ConstraintType
2861 M68kTargetLowering::getConstraintType(StringRef Constraint) const {
2862   if (Constraint.size() > 0) {
2863     switch (Constraint[0]) {
2864     case 'a':
2865     case 'd':
2866       return C_RegisterClass;
2867     case 'I':
2868     case 'J':
2869     case 'K':
2870     case 'L':
2871     case 'M':
2872     case 'N':
2873     case 'O':
2874     case 'P':
2875       return C_Immediate;
2876     case 'C':
2877       if (Constraint.size() == 2)
2878         switch (Constraint[1]) {
2879         case '0':
2880         case 'i':
2881         case 'j':
2882           return C_Immediate;
2883         default:
2884           break;
2885         }
2886       break;
2887     case 'Q':
2888     case 'U':
2889       return C_Memory;
2890     default:
2891       break;
2892     }
2893   }
2894 
2895   return TargetLowering::getConstraintType(Constraint);
2896 }
2897 
2898 void M68kTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2899                                                       std::string &Constraint,
2900                                                       std::vector<SDValue> &Ops,
2901                                                       SelectionDAG &DAG) const {
2902   SDValue Result;
2903 
2904   if (Constraint.size() == 1) {
2905     // Constant constraints
2906     switch (Constraint[0]) {
2907     case 'I':
2908     case 'J':
2909     case 'K':
2910     case 'L':
2911     case 'M':
2912     case 'N':
2913     case 'O':
2914     case 'P': {
2915       auto *C = dyn_cast<ConstantSDNode>(Op);
2916       if (!C)
2917         return;
2918 
2919       int64_t Val = C->getSExtValue();
2920       switch (Constraint[0]) {
2921       case 'I': // constant integer in the range [1,8]
2922         if (Val > 0 && Val <= 8)
2923           break;
2924         return;
2925       case 'J': // constant signed 16-bit integer
2926         if (isInt<16>(Val))
2927           break;
2928         return;
2929       case 'K': // constant that is NOT in the range of [-0x80, 0x80)
2930         if (Val < -0x80 || Val >= 0x80)
2931           break;
2932         return;
2933       case 'L': // constant integer in the range [-8,-1]
2934         if (Val < 0 && Val >= -8)
2935           break;
2936         return;
2937       case 'M': // constant that is NOT in the range of [-0x100, 0x100]
2938         if (Val < -0x100 || Val >= 0x100)
2939           break;
2940         return;
2941       case 'N': // constant integer in the range [24,31]
2942         if (Val >= 24 && Val <= 31)
2943           break;
2944         return;
2945       case 'O': // constant integer 16
2946         if (Val == 16)
2947           break;
2948         return;
2949       case 'P': // constant integer in the range [8,15]
2950         if (Val >= 8 && Val <= 15)
2951           break;
2952         return;
2953       default:
2954         llvm_unreachable("Unhandled constant constraint");
2955       }
2956 
2957       Result = DAG.getTargetConstant(Val, SDLoc(Op), Op.getValueType());
2958       break;
2959     }
2960     default:
2961       break;
2962     }
2963   }
2964 
2965   if (Constraint.size() == 2) {
2966     switch (Constraint[0]) {
2967     case 'C':
2968       // Constant constraints start with 'C'
2969       switch (Constraint[1]) {
2970       case '0':
2971       case 'i':
2972       case 'j': {
2973         auto *C = dyn_cast<ConstantSDNode>(Op);
2974         if (!C)
2975           break;
2976 
2977         int64_t Val = C->getSExtValue();
2978         switch (Constraint[1]) {
2979         case '0': // constant integer 0
2980           if (!Val)
2981             break;
2982           return;
2983         case 'i': // constant integer
2984           break;
2985         case 'j': // integer constant that doesn't fit in 16 bits
2986           if (!isInt<16>(C->getSExtValue()))
2987             break;
2988           return;
2989         default:
2990           llvm_unreachable("Unhandled constant constraint");
2991         }
2992 
2993         Result = DAG.getTargetConstant(Val, SDLoc(Op), Op.getValueType());
2994         break;
2995       }
2996       default:
2997         break;
2998       }
2999       break;
3000     default:
3001       break;
3002     }
3003   }
3004 
3005   if (Result.getNode()) {
3006     Ops.push_back(Result);
3007     return;
3008   }
3009 
3010   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3011 }
3012 
3013 std::pair<unsigned, const TargetRegisterClass *>
3014 M68kTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3015                                                  StringRef Constraint,
3016                                                  MVT VT) const {
3017   if (Constraint.size() == 1) {
3018     switch (Constraint[0]) {
3019     case 'r':
3020     case 'd':
3021       switch (VT.SimpleTy) {
3022       case MVT::i8:
3023         return std::make_pair(0U, &M68k::DR8RegClass);
3024       case MVT::i16:
3025         return std::make_pair(0U, &M68k::DR16RegClass);
3026       case MVT::i32:
3027         return std::make_pair(0U, &M68k::DR32RegClass);
3028       default:
3029         break;
3030       }
3031       break;
3032     case 'a':
3033       switch (VT.SimpleTy) {
3034       case MVT::i16:
3035         return std::make_pair(0U, &M68k::AR16RegClass);
3036       case MVT::i32:
3037         return std::make_pair(0U, &M68k::AR32RegClass);
3038       default:
3039         break;
3040       }
3041       break;
3042     default:
3043       break;
3044     }
3045   }
3046 
3047   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3048 }
3049 
3050 /// Determines whether the callee is required to pop its own arguments.
3051 /// Callee pop is necessary to support tail calls.
3052 bool M68k::isCalleePop(CallingConv::ID CallingConv, bool IsVarArg,
3053                        bool GuaranteeTCO) {
3054   return false;
3055 }
3056 
3057 // Return true if it is OK for this CMOV pseudo-opcode to be cascaded
3058 // together with other CMOV pseudo-opcodes into a single basic-block with
3059 // conditional jump around it.
3060 static bool isCMOVPseudo(MachineInstr &MI) {
3061   switch (MI.getOpcode()) {
3062   case M68k::CMOV8d:
3063   case M68k::CMOV16d:
3064   case M68k::CMOV32r:
3065     return true;
3066 
3067   default:
3068     return false;
3069   }
3070 }
3071 
3072 // The CCR operand of SelectItr might be missing a kill marker
3073 // because there were multiple uses of CCR, and ISel didn't know
3074 // which to mark. Figure out whether SelectItr should have had a
3075 // kill marker, and set it if it should. Returns the correct kill
3076 // marker value.
3077 static bool checkAndUpdateCCRKill(MachineBasicBlock::iterator SelectItr,
3078                                   MachineBasicBlock *BB,
3079                                   const TargetRegisterInfo *TRI) {
3080   // Scan forward through BB for a use/def of CCR.
3081   MachineBasicBlock::iterator miI(std::next(SelectItr));
3082   for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
3083     const MachineInstr &mi = *miI;
3084     if (mi.readsRegister(M68k::CCR))
3085       return false;
3086     if (mi.definesRegister(M68k::CCR))
3087       break; // Should have kill-flag - update below.
3088   }
3089 
3090   // If we hit the end of the block, check whether CCR is live into a
3091   // successor.
3092   if (miI == BB->end())
3093     for (const auto *SBB : BB->successors())
3094       if (SBB->isLiveIn(M68k::CCR))
3095         return false;
3096 
3097   // We found a def, or hit the end of the basic block and CCR wasn't live
3098   // out. SelectMI should have a kill flag on CCR.
3099   SelectItr->addRegisterKilled(M68k::CCR, TRI);
3100   return true;
3101 }
3102 
3103 MachineBasicBlock *
3104 M68kTargetLowering::EmitLoweredSelect(MachineInstr &MI,
3105                                       MachineBasicBlock *MBB) const {
3106   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3107   DebugLoc DL = MI.getDebugLoc();
3108 
3109   // To "insert" a SELECT_CC instruction, we actually have to insert the
3110   // diamond control-flow pattern.  The incoming instruction knows the
3111   // destination vreg to set, the condition code register to branch on, the
3112   // true/false values to select between, and a branch opcode to use.
3113   const BasicBlock *BB = MBB->getBasicBlock();
3114   MachineFunction::iterator It = ++MBB->getIterator();
3115 
3116   //  ThisMBB:
3117   //  ...
3118   //   TrueVal = ...
3119   //   cmp ccX, r1, r2
3120   //   bcc Copy1MBB
3121   //   fallthrough --> Copy0MBB
3122   MachineBasicBlock *ThisMBB = MBB;
3123   MachineFunction *F = MBB->getParent();
3124 
3125   // This code lowers all pseudo-CMOV instructions. Generally it lowers these
3126   // as described above, by inserting a MBB, and then making a PHI at the join
3127   // point to select the true and false operands of the CMOV in the PHI.
3128   //
3129   // The code also handles two different cases of multiple CMOV opcodes
3130   // in a row.
3131   //
3132   // Case 1:
3133   // In this case, there are multiple CMOVs in a row, all which are based on
3134   // the same condition setting (or the exact opposite condition setting).
3135   // In this case we can lower all the CMOVs using a single inserted MBB, and
3136   // then make a number of PHIs at the join point to model the CMOVs. The only
3137   // trickiness here, is that in a case like:
3138   //
3139   // t2 = CMOV cond1 t1, f1
3140   // t3 = CMOV cond1 t2, f2
3141   //
3142   // when rewriting this into PHIs, we have to perform some renaming on the
3143   // temps since you cannot have a PHI operand refer to a PHI result earlier
3144   // in the same block.  The "simple" but wrong lowering would be:
3145   //
3146   // t2 = PHI t1(BB1), f1(BB2)
3147   // t3 = PHI t2(BB1), f2(BB2)
3148   //
3149   // but clearly t2 is not defined in BB1, so that is incorrect. The proper
3150   // renaming is to note that on the path through BB1, t2 is really just a
3151   // copy of t1, and do that renaming, properly generating:
3152   //
3153   // t2 = PHI t1(BB1), f1(BB2)
3154   // t3 = PHI t1(BB1), f2(BB2)
3155   //
3156   // Case 2, we lower cascaded CMOVs such as
3157   //
3158   //   (CMOV (CMOV F, T, cc1), T, cc2)
3159   //
3160   // to two successives branches.
3161   MachineInstr *CascadedCMOV = nullptr;
3162   MachineInstr *LastCMOV = &MI;
3163   M68k::CondCode CC = M68k::CondCode(MI.getOperand(3).getImm());
3164   M68k::CondCode OppCC = M68k::GetOppositeBranchCondition(CC);
3165   MachineBasicBlock::iterator NextMIIt =
3166       std::next(MachineBasicBlock::iterator(MI));
3167 
3168   // Check for case 1, where there are multiple CMOVs with the same condition
3169   // first.  Of the two cases of multiple CMOV lowerings, case 1 reduces the
3170   // number of jumps the most.
3171 
3172   if (isCMOVPseudo(MI)) {
3173     // See if we have a string of CMOVS with the same condition.
3174     while (NextMIIt != MBB->end() && isCMOVPseudo(*NextMIIt) &&
3175            (NextMIIt->getOperand(3).getImm() == CC ||
3176             NextMIIt->getOperand(3).getImm() == OppCC)) {
3177       LastCMOV = &*NextMIIt;
3178       ++NextMIIt;
3179     }
3180   }
3181 
3182   // This checks for case 2, but only do this if we didn't already find
3183   // case 1, as indicated by LastCMOV == MI.
3184   if (LastCMOV == &MI && NextMIIt != MBB->end() &&
3185       NextMIIt->getOpcode() == MI.getOpcode() &&
3186       NextMIIt->getOperand(2).getReg() == MI.getOperand(2).getReg() &&
3187       NextMIIt->getOperand(1).getReg() == MI.getOperand(0).getReg() &&
3188       NextMIIt->getOperand(1).isKill()) {
3189     CascadedCMOV = &*NextMIIt;
3190   }
3191 
3192   MachineBasicBlock *Jcc1MBB = nullptr;
3193 
3194   // If we have a cascaded CMOV, we lower it to two successive branches to
3195   // the same block.  CCR is used by both, so mark it as live in the second.
3196   if (CascadedCMOV) {
3197     Jcc1MBB = F->CreateMachineBasicBlock(BB);
3198     F->insert(It, Jcc1MBB);
3199     Jcc1MBB->addLiveIn(M68k::CCR);
3200   }
3201 
3202   MachineBasicBlock *Copy0MBB = F->CreateMachineBasicBlock(BB);
3203   MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(BB);
3204   F->insert(It, Copy0MBB);
3205   F->insert(It, SinkMBB);
3206 
3207   // If the CCR register isn't dead in the terminator, then claim that it's
3208   // live into the sink and copy blocks.
3209   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
3210 
3211   MachineInstr *LastCCRSUser = CascadedCMOV ? CascadedCMOV : LastCMOV;
3212   if (!LastCCRSUser->killsRegister(M68k::CCR) &&
3213       !checkAndUpdateCCRKill(LastCCRSUser, MBB, TRI)) {
3214     Copy0MBB->addLiveIn(M68k::CCR);
3215     SinkMBB->addLiveIn(M68k::CCR);
3216   }
3217 
3218   // Transfer the remainder of MBB and its successor edges to SinkMBB.
3219   SinkMBB->splice(SinkMBB->begin(), MBB,
3220                   std::next(MachineBasicBlock::iterator(LastCMOV)), MBB->end());
3221   SinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3222 
3223   // Add the true and fallthrough blocks as its successors.
3224   if (CascadedCMOV) {
3225     // The fallthrough block may be Jcc1MBB, if we have a cascaded CMOV.
3226     MBB->addSuccessor(Jcc1MBB);
3227 
3228     // In that case, Jcc1MBB will itself fallthrough the Copy0MBB, and
3229     // jump to the SinkMBB.
3230     Jcc1MBB->addSuccessor(Copy0MBB);
3231     Jcc1MBB->addSuccessor(SinkMBB);
3232   } else {
3233     MBB->addSuccessor(Copy0MBB);
3234   }
3235 
3236   // The true block target of the first (or only) branch is always SinkMBB.
3237   MBB->addSuccessor(SinkMBB);
3238 
3239   // Create the conditional branch instruction.
3240   unsigned Opc = M68k::GetCondBranchFromCond(CC);
3241   BuildMI(MBB, DL, TII->get(Opc)).addMBB(SinkMBB);
3242 
3243   if (CascadedCMOV) {
3244     unsigned Opc2 = M68k::GetCondBranchFromCond(
3245         (M68k::CondCode)CascadedCMOV->getOperand(3).getImm());
3246     BuildMI(Jcc1MBB, DL, TII->get(Opc2)).addMBB(SinkMBB);
3247   }
3248 
3249   //  Copy0MBB:
3250   //   %FalseValue = ...
3251   //   # fallthrough to SinkMBB
3252   Copy0MBB->addSuccessor(SinkMBB);
3253 
3254   //  SinkMBB:
3255   //   %Result = phi [ %FalseValue, Copy0MBB ], [ %TrueValue, ThisMBB ]
3256   //  ...
3257   MachineBasicBlock::iterator MIItBegin = MachineBasicBlock::iterator(MI);
3258   MachineBasicBlock::iterator MIItEnd =
3259       std::next(MachineBasicBlock::iterator(LastCMOV));
3260   MachineBasicBlock::iterator SinkInsertionPoint = SinkMBB->begin();
3261   DenseMap<unsigned, std::pair<unsigned, unsigned>> RegRewriteTable;
3262   MachineInstrBuilder MIB;
3263 
3264   // As we are creating the PHIs, we have to be careful if there is more than
3265   // one.  Later CMOVs may reference the results of earlier CMOVs, but later
3266   // PHIs have to reference the individual true/false inputs from earlier PHIs.
3267   // That also means that PHI construction must work forward from earlier to
3268   // later, and that the code must maintain a mapping from earlier PHI's
3269   // destination registers, and the registers that went into the PHI.
3270 
3271   for (MachineBasicBlock::iterator MIIt = MIItBegin; MIIt != MIItEnd; ++MIIt) {
3272     Register DestReg = MIIt->getOperand(0).getReg();
3273     Register Op1Reg = MIIt->getOperand(1).getReg();
3274     Register Op2Reg = MIIt->getOperand(2).getReg();
3275 
3276     // If this CMOV we are generating is the opposite condition from
3277     // the jump we generated, then we have to swap the operands for the
3278     // PHI that is going to be generated.
3279     if (MIIt->getOperand(3).getImm() == OppCC)
3280       std::swap(Op1Reg, Op2Reg);
3281 
3282     if (RegRewriteTable.find(Op1Reg) != RegRewriteTable.end())
3283       Op1Reg = RegRewriteTable[Op1Reg].first;
3284 
3285     if (RegRewriteTable.find(Op2Reg) != RegRewriteTable.end())
3286       Op2Reg = RegRewriteTable[Op2Reg].second;
3287 
3288     MIB =
3289         BuildMI(*SinkMBB, SinkInsertionPoint, DL, TII->get(M68k::PHI), DestReg)
3290             .addReg(Op1Reg)
3291             .addMBB(Copy0MBB)
3292             .addReg(Op2Reg)
3293             .addMBB(ThisMBB);
3294 
3295     // Add this PHI to the rewrite table.
3296     RegRewriteTable[DestReg] = std::make_pair(Op1Reg, Op2Reg);
3297   }
3298 
3299   // If we have a cascaded CMOV, the second Jcc provides the same incoming
3300   // value as the first Jcc (the True operand of the SELECT_CC/CMOV nodes).
3301   if (CascadedCMOV) {
3302     MIB.addReg(MI.getOperand(2).getReg()).addMBB(Jcc1MBB);
3303     // Copy the PHI result to the register defined by the second CMOV.
3304     BuildMI(*SinkMBB, std::next(MachineBasicBlock::iterator(MIB.getInstr())),
3305             DL, TII->get(TargetOpcode::COPY),
3306             CascadedCMOV->getOperand(0).getReg())
3307         .addReg(MI.getOperand(0).getReg());
3308     CascadedCMOV->eraseFromParent();
3309   }
3310 
3311   // Now remove the CMOV(s).
3312   for (MachineBasicBlock::iterator MIIt = MIItBegin; MIIt != MIItEnd;)
3313     (MIIt++)->eraseFromParent();
3314 
3315   return SinkMBB;
3316 }
3317 
3318 MachineBasicBlock *
3319 M68kTargetLowering::EmitLoweredSegAlloca(MachineInstr &MI,
3320                                          MachineBasicBlock *BB) const {
3321   llvm_unreachable("Cannot lower Segmented Stack Alloca with stack-split on");
3322 }
3323 
3324 MachineBasicBlock *
3325 M68kTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
3326                                                 MachineBasicBlock *BB) const {
3327   switch (MI.getOpcode()) {
3328   default:
3329     llvm_unreachable("Unexpected instr type to insert");
3330   case M68k::CMOV8d:
3331   case M68k::CMOV16d:
3332   case M68k::CMOV32r:
3333     return EmitLoweredSelect(MI, BB);
3334   case M68k::SALLOCA:
3335     return EmitLoweredSegAlloca(MI, BB);
3336   }
3337 }
3338 
3339 SDValue M68kTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
3340   MachineFunction &MF = DAG.getMachineFunction();
3341   auto PtrVT = getPointerTy(MF.getDataLayout());
3342   M68kMachineFunctionInfo *FuncInfo = MF.getInfo<M68kMachineFunctionInfo>();
3343 
3344   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
3345   SDLoc DL(Op);
3346 
3347   // vastart just stores the address of the VarArgsFrameIndex slot into the
3348   // memory location argument.
3349   SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3350   return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
3351                       MachinePointerInfo(SV));
3352 }
3353 
3354 SDValue M68kTargetLowering::LowerATOMICFENCE(SDValue Op,
3355                                              SelectionDAG &DAG) const {
3356   // Lower to a memory barrier created from inline asm.
3357   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3358   LLVMContext &Ctx = *DAG.getContext();
3359 
3360   const unsigned Flags = InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore |
3361                          InlineAsm::Extra_HasSideEffects;
3362   const SDValue AsmOperands[4] = {
3363       Op.getOperand(0), // Input chain
3364       DAG.getTargetExternalSymbol(
3365           "", TLI.getProgramPointerTy(
3366                   DAG.getDataLayout())),   // Empty inline asm string
3367       DAG.getMDNode(MDNode::get(Ctx, {})), // (empty) srcloc
3368       DAG.getTargetConstant(Flags, SDLoc(Op),
3369                             TLI.getPointerTy(DAG.getDataLayout())), // Flags
3370   };
3371 
3372   return DAG.getNode(ISD::INLINEASM, SDLoc(Op),
3373                      DAG.getVTList(MVT::Other, MVT::Glue), AsmOperands);
3374 }
3375 
3376 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3377 // Calls to _alloca are needed to probe the stack when allocating more than 4k
3378 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
3379 // that the guard pages used by the OS virtual memory manager are allocated in
3380 // correct sequence.
3381 SDValue M68kTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
3382                                                     SelectionDAG &DAG) const {
3383   MachineFunction &MF = DAG.getMachineFunction();
3384   bool SplitStack = MF.shouldSplitStack();
3385 
3386   SDLoc DL(Op);
3387 
3388   // Get the inputs.
3389   SDNode *Node = Op.getNode();
3390   SDValue Chain = Op.getOperand(0);
3391   SDValue Size = Op.getOperand(1);
3392   unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3393   EVT VT = Node->getValueType(0);
3394 
3395   // Chain the dynamic stack allocation so that it doesn't modify the stack
3396   // pointer when other instructions are using the stack.
3397   Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
3398 
3399   SDValue Result;
3400   if (SplitStack) {
3401     auto &MRI = MF.getRegInfo();
3402     auto SPTy = getPointerTy(DAG.getDataLayout());
3403     auto *ARClass = getRegClassFor(SPTy);
3404     Register Vreg = MRI.createVirtualRegister(ARClass);
3405     Chain = DAG.getCopyToReg(Chain, DL, Vreg, Size);
3406     Result = DAG.getNode(M68kISD::SEG_ALLOCA, DL, SPTy, Chain,
3407                          DAG.getRegister(Vreg, SPTy));
3408   } else {
3409     auto &TLI = DAG.getTargetLoweringInfo();
3410     Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
3411     assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
3412                     " not tell us which reg is the stack pointer!");
3413 
3414     SDValue SP = DAG.getCopyFromReg(Chain, DL, SPReg, VT);
3415     Chain = SP.getValue(1);
3416     const TargetFrameLowering &TFI = *Subtarget.getFrameLowering();
3417     unsigned StackAlign = TFI.getStackAlignment();
3418     Result = DAG.getNode(ISD::SUB, DL, VT, SP, Size); // Value
3419     if (Align > StackAlign)
3420       Result = DAG.getNode(ISD::AND, DL, VT, Result,
3421                            DAG.getConstant(-(uint64_t)Align, DL, VT));
3422     Chain = DAG.getCopyToReg(Chain, DL, SPReg, Result); // Output chain
3423   }
3424 
3425   Chain = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), DL);
3426 
3427   SDValue Ops[2] = {Result, Chain};
3428   return DAG.getMergeValues(Ops, DL);
3429 }
3430 
3431 SDValue M68kTargetLowering::LowerShiftLeftParts(SDValue Op,
3432                                                 SelectionDAG &DAG) const {
3433   SDLoc DL(Op);
3434   SDValue Lo = Op.getOperand(0);
3435   SDValue Hi = Op.getOperand(1);
3436   SDValue Shamt = Op.getOperand(2);
3437   EVT VT = Lo.getValueType();
3438 
3439   // if Shamt - register size < 0: // Shamt < register size
3440   //   Lo = Lo << Shamt
3441   //   Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (register size - 1 ^ Shamt))
3442   // else:
3443   //   Lo = 0
3444   //   Hi = Lo << (Shamt - register size)
3445 
3446   SDValue Zero = DAG.getConstant(0, DL, VT);
3447   SDValue One = DAG.getConstant(1, DL, VT);
3448   SDValue MinusRegisterSize = DAG.getConstant(-32, DL, VT);
3449   SDValue RegisterSizeMinus1 = DAG.getConstant(32 - 1, DL, VT);
3450   SDValue ShamtMinusRegisterSize =
3451       DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusRegisterSize);
3452   SDValue RegisterSizeMinus1Shamt =
3453       DAG.getNode(ISD::XOR, DL, VT, RegisterSizeMinus1, Shamt);
3454 
3455   SDValue LoTrue = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
3456   SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, One);
3457   SDValue ShiftRightLo =
3458       DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, RegisterSizeMinus1Shamt);
3459   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
3460   SDValue HiTrue = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
3461   SDValue HiFalse = DAG.getNode(ISD::SHL, DL, VT, Lo, ShamtMinusRegisterSize);
3462 
3463   SDValue CC =
3464       DAG.getSetCC(DL, MVT::i8, ShamtMinusRegisterSize, Zero, ISD::SETLT);
3465 
3466   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, Zero);
3467   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
3468 
3469   return DAG.getMergeValues({Lo, Hi}, DL);
3470 }
3471 
3472 SDValue M68kTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
3473                                                  bool IsSRA) const {
3474   SDLoc DL(Op);
3475   SDValue Lo = Op.getOperand(0);
3476   SDValue Hi = Op.getOperand(1);
3477   SDValue Shamt = Op.getOperand(2);
3478   EVT VT = Lo.getValueType();
3479 
3480   // SRA expansion:
3481   //   if Shamt - register size < 0: // Shamt < register size
3482   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (register size - 1 ^ Shamt))
3483   //     Hi = Hi >>s Shamt
3484   //   else:
3485   //     Lo = Hi >>s (Shamt - register size);
3486   //     Hi = Hi >>s (register size - 1)
3487   //
3488   // SRL expansion:
3489   //   if Shamt - register size < 0: // Shamt < register size
3490   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (register size - 1 ^ Shamt))
3491   //     Hi = Hi >>u Shamt
3492   //   else:
3493   //     Lo = Hi >>u (Shamt - register size);
3494   //     Hi = 0;
3495 
3496   unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
3497 
3498   SDValue Zero = DAG.getConstant(0, DL, VT);
3499   SDValue One = DAG.getConstant(1, DL, VT);
3500   SDValue MinusRegisterSize = DAG.getConstant(-32, DL, VT);
3501   SDValue RegisterSizeMinus1 = DAG.getConstant(32 - 1, DL, VT);
3502   SDValue ShamtMinusRegisterSize =
3503       DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusRegisterSize);
3504   SDValue RegisterSizeMinus1Shamt =
3505       DAG.getNode(ISD::XOR, DL, VT, RegisterSizeMinus1, Shamt);
3506 
3507   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
3508   SDValue ShiftLeftHi1 = DAG.getNode(ISD::SHL, DL, VT, Hi, One);
3509   SDValue ShiftLeftHi =
3510       DAG.getNode(ISD::SHL, DL, VT, ShiftLeftHi1, RegisterSizeMinus1Shamt);
3511   SDValue LoTrue = DAG.getNode(ISD::OR, DL, VT, ShiftRightLo, ShiftLeftHi);
3512   SDValue HiTrue = DAG.getNode(ShiftRightOp, DL, VT, Hi, Shamt);
3513   SDValue LoFalse =
3514       DAG.getNode(ShiftRightOp, DL, VT, Hi, ShamtMinusRegisterSize);
3515   SDValue HiFalse =
3516       IsSRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, RegisterSizeMinus1) : Zero;
3517 
3518   SDValue CC =
3519       DAG.getSetCC(DL, MVT::i8, ShamtMinusRegisterSize, Zero, ISD::SETLT);
3520 
3521   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, LoFalse);
3522   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
3523 
3524   return DAG.getMergeValues({Lo, Hi}, DL);
3525 }
3526 
3527 //===----------------------------------------------------------------------===//
3528 // DAG Combine
3529 //===----------------------------------------------------------------------===//
3530 
3531 static SDValue getSETCC(M68k::CondCode Cond, SDValue CCR, const SDLoc &dl,
3532                         SelectionDAG &DAG) {
3533   return DAG.getNode(M68kISD::SETCC, dl, MVT::i8,
3534                      DAG.getConstant(Cond, dl, MVT::i8), CCR);
3535 }
3536 // When legalizing carry, we create carries via add X, -1
3537 // If that comes from an actual carry, via setcc, we use the
3538 // carry directly.
3539 static SDValue combineCarryThroughADD(SDValue CCR) {
3540   if (CCR.getOpcode() == M68kISD::ADD) {
3541     if (isAllOnesConstant(CCR.getOperand(1))) {
3542       SDValue Carry = CCR.getOperand(0);
3543       while (Carry.getOpcode() == ISD::TRUNCATE ||
3544              Carry.getOpcode() == ISD::ZERO_EXTEND ||
3545              Carry.getOpcode() == ISD::SIGN_EXTEND ||
3546              Carry.getOpcode() == ISD::ANY_EXTEND ||
3547              (Carry.getOpcode() == ISD::AND &&
3548               isOneConstant(Carry.getOperand(1))))
3549         Carry = Carry.getOperand(0);
3550       if (Carry.getOpcode() == M68kISD::SETCC ||
3551           Carry.getOpcode() == M68kISD::SETCC_CARRY) {
3552         if (Carry.getConstantOperandVal(0) == M68k::COND_CS)
3553           return Carry.getOperand(1);
3554       }
3555     }
3556   }
3557 
3558   return SDValue();
3559 }
3560 
3561 /// Optimize a CCR definition used according to the condition code \p CC into
3562 /// a simpler CCR value, potentially returning a new \p CC and replacing uses
3563 /// of chain values.
3564 static SDValue combineSetCCCCR(SDValue CCR, M68k::CondCode &CC,
3565                                SelectionDAG &DAG,
3566                                const M68kSubtarget &Subtarget) {
3567   if (CC == M68k::COND_CS)
3568     if (SDValue Flags = combineCarryThroughADD(CCR))
3569       return Flags;
3570 
3571   return SDValue();
3572 }
3573 
3574 // Optimize  RES = M68kISD::SETCC CONDCODE, CCR_INPUT
3575 static SDValue combineM68kSetCC(SDNode *N, SelectionDAG &DAG,
3576                                 const M68kSubtarget &Subtarget) {
3577   SDLoc DL(N);
3578   M68k::CondCode CC = M68k::CondCode(N->getConstantOperandVal(0));
3579   SDValue CCR = N->getOperand(1);
3580 
3581   // Try to simplify the CCR and condition code operands.
3582   if (SDValue Flags = combineSetCCCCR(CCR, CC, DAG, Subtarget))
3583     return getSETCC(CC, Flags, DL, DAG);
3584 
3585   return SDValue();
3586 }
3587 static SDValue combineM68kBrCond(SDNode *N, SelectionDAG &DAG,
3588                                  const M68kSubtarget &Subtarget) {
3589   SDLoc DL(N);
3590   M68k::CondCode CC = M68k::CondCode(N->getConstantOperandVal(2));
3591   SDValue CCR = N->getOperand(3);
3592 
3593   // Try to simplify the CCR and condition code operands.
3594   // Make sure to not keep references to operands, as combineSetCCCCR can
3595   // RAUW them under us.
3596   if (SDValue Flags = combineSetCCCCR(CCR, CC, DAG, Subtarget)) {
3597     SDValue Cond = DAG.getConstant(CC, DL, MVT::i8);
3598     return DAG.getNode(M68kISD::BRCOND, DL, N->getVTList(), N->getOperand(0),
3599                        N->getOperand(1), Cond, Flags);
3600   }
3601 
3602   return SDValue();
3603 }
3604 
3605 static SDValue combineSUBX(SDNode *N, SelectionDAG &DAG) {
3606   if (SDValue Flags = combineCarryThroughADD(N->getOperand(2))) {
3607     MVT VT = N->getSimpleValueType(0);
3608     SDVTList VTs = DAG.getVTList(VT, MVT::i32);
3609     return DAG.getNode(M68kISD::SUBX, SDLoc(N), VTs, N->getOperand(0),
3610                        N->getOperand(1), Flags);
3611   }
3612 
3613   return SDValue();
3614 }
3615 
3616 // Optimize RES, CCR = M68kISD::ADDX LHS, RHS, CCR
3617 static SDValue combineADDX(SDNode *N, SelectionDAG &DAG,
3618                            TargetLowering::DAGCombinerInfo &DCI) {
3619   if (SDValue Flags = combineCarryThroughADD(N->getOperand(2))) {
3620     MVT VT = N->getSimpleValueType(0);
3621     SDVTList VTs = DAG.getVTList(VT, MVT::i32);
3622     return DAG.getNode(M68kISD::ADDX, SDLoc(N), VTs, N->getOperand(0),
3623                        N->getOperand(1), Flags);
3624   }
3625 
3626   return SDValue();
3627 }
3628 
3629 SDValue M68kTargetLowering::PerformDAGCombine(SDNode *N,
3630                                               DAGCombinerInfo &DCI) const {
3631   SelectionDAG &DAG = DCI.DAG;
3632   switch (N->getOpcode()) {
3633   case M68kISD::SUBX:
3634     return combineSUBX(N, DAG);
3635   case M68kISD::ADDX:
3636     return combineADDX(N, DAG, DCI);
3637   case M68kISD::SETCC:
3638     return combineM68kSetCC(N, DAG, Subtarget);
3639   case M68kISD::BRCOND:
3640     return combineM68kBrCond(N, DAG, Subtarget);
3641   }
3642 
3643   return SDValue();
3644 }
3645 
3646 //===----------------------------------------------------------------------===//
3647 // M68kISD Node Names
3648 //===----------------------------------------------------------------------===//
3649 const char *M68kTargetLowering::getTargetNodeName(unsigned Opcode) const {
3650   switch (Opcode) {
3651   case M68kISD::CALL:
3652     return "M68kISD::CALL";
3653   case M68kISD::TAIL_CALL:
3654     return "M68kISD::TAIL_CALL";
3655   case M68kISD::RET:
3656     return "M68kISD::RET";
3657   case M68kISD::TC_RETURN:
3658     return "M68kISD::TC_RETURN";
3659   case M68kISD::ADD:
3660     return "M68kISD::ADD";
3661   case M68kISD::SUB:
3662     return "M68kISD::SUB";
3663   case M68kISD::ADDX:
3664     return "M68kISD::ADDX";
3665   case M68kISD::SUBX:
3666     return "M68kISD::SUBX";
3667   case M68kISD::SMUL:
3668     return "M68kISD::SMUL";
3669   case M68kISD::UMUL:
3670     return "M68kISD::UMUL";
3671   case M68kISD::OR:
3672     return "M68kISD::OR";
3673   case M68kISD::XOR:
3674     return "M68kISD::XOR";
3675   case M68kISD::AND:
3676     return "M68kISD::AND";
3677   case M68kISD::CMP:
3678     return "M68kISD::CMP";
3679   case M68kISD::BTST:
3680     return "M68kISD::BTST";
3681   case M68kISD::SELECT:
3682     return "M68kISD::SELECT";
3683   case M68kISD::CMOV:
3684     return "M68kISD::CMOV";
3685   case M68kISD::BRCOND:
3686     return "M68kISD::BRCOND";
3687   case M68kISD::SETCC:
3688     return "M68kISD::SETCC";
3689   case M68kISD::SETCC_CARRY:
3690     return "M68kISD::SETCC_CARRY";
3691   case M68kISD::GLOBAL_BASE_REG:
3692     return "M68kISD::GLOBAL_BASE_REG";
3693   case M68kISD::Wrapper:
3694     return "M68kISD::Wrapper";
3695   case M68kISD::WrapperPC:
3696     return "M68kISD::WrapperPC";
3697   case M68kISD::SEG_ALLOCA:
3698     return "M68kISD::SEG_ALLOCA";
3699   default:
3700     return NULL;
3701   }
3702 }
3703 
3704 CCAssignFn *M68kTargetLowering::getCCAssignFn(CallingConv::ID CC, bool Return,
3705                                               bool IsVarArg) const {
3706   if (Return)
3707     return RetCC_M68k_C;
3708   else
3709     return CC_M68k_C;
3710 }
3711