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