1 //===-- M68kISelLowering.cpp - M68k DAG Lowering Impl -----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines the interfaces that M68k uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #include "M68kISelLowering.h"
16 #include "M68kCallingConv.h"
17 #include "M68kMachineFunction.h"
18 #include "M68kSubtarget.h"
19 #include "M68kTargetMachine.h"
20 #include "M68kTargetObjectFile.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/SelectionDAG.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/KnownBits.h"
38 #include "llvm/Support/raw_ostream.h"
42 #define DEBUG_TYPE "M68k-isel"
44 STATISTIC(NumTailCalls
, "Number of tail calls");
46 M68kTargetLowering::M68kTargetLowering(const M68kTargetMachine
&TM
,
47 const M68kSubtarget
&STI
)
48 : TargetLowering(TM
), Subtarget(STI
), TM(TM
) {
52 setBooleanContents(ZeroOrOneBooleanContent
);
54 auto *RegInfo
= Subtarget
.getRegisterInfo();
55 setStackPointerRegisterToSaveRestore(RegInfo
->getStackRegister());
57 // Set up the register classes.
58 addRegisterClass(MVT::i8
, &M68k::DR8RegClass
);
59 addRegisterClass(MVT::i16
, &M68k::XR16RegClass
);
60 addRegisterClass(MVT::i32
, &M68k::XR32RegClass
);
62 for (auto VT
: MVT::integer_valuetypes()) {
63 setLoadExtAction(ISD::SEXTLOAD
, VT
, MVT::i1
, Promote
);
64 setLoadExtAction(ISD::ZEXTLOAD
, VT
, MVT::i1
, Promote
);
65 setLoadExtAction(ISD::EXTLOAD
, VT
, MVT::i1
, Promote
);
68 // We don't accept any truncstore of integer registers.
69 setTruncStoreAction(MVT::i64
, MVT::i32
, Expand
);
70 setTruncStoreAction(MVT::i64
, MVT::i16
, Expand
);
71 setTruncStoreAction(MVT::i64
, MVT::i8
, Expand
);
72 setTruncStoreAction(MVT::i32
, MVT::i16
, Expand
);
73 setTruncStoreAction(MVT::i32
, MVT::i8
, Expand
);
74 setTruncStoreAction(MVT::i16
, MVT::i8
, Expand
);
76 setOperationAction({ISD::MUL
, ISD::SDIV
, ISD::UDIV
}, MVT::i8
, Promote
);
77 setOperationAction({ISD::MUL
, ISD::SDIV
, ISD::UDIV
}, MVT::i16
, Legal
);
78 if (Subtarget
.atLeastM68020())
79 setOperationAction({ISD::MUL
, ISD::SDIV
, ISD::UDIV
}, MVT::i32
, Legal
);
81 setOperationAction({ISD::MUL
, ISD::SDIV
, ISD::UDIV
}, MVT::i32
, LibCall
);
82 setOperationAction(ISD::MUL
, MVT::i64
, LibCall
);
85 {ISD::SREM
, ISD::UREM
, ISD::UDIVREM
, ISD::SDIVREM
,
86 ISD::MULHS
, ISD::MULHU
, ISD::UMUL_LOHI
, ISD::SMUL_LOHI
}) {
87 setOperationAction(OP
, MVT::i8
, Promote
);
88 setOperationAction(OP
, MVT::i16
, Legal
);
89 setOperationAction(OP
, MVT::i32
, LibCall
);
92 for (auto OP
: {ISD::UMUL_LOHI
, ISD::SMUL_LOHI
}) {
93 setOperationAction(OP
, MVT::i8
, Expand
);
94 setOperationAction(OP
, MVT::i16
, Expand
);
97 // FIXME It would be better to use a custom lowering
98 for (auto OP
: {ISD::SMULO
, ISD::UMULO
}) {
99 setOperationAction(OP
, MVT::i8
, Expand
);
100 setOperationAction(OP
, MVT::i16
, Expand
);
101 setOperationAction(OP
, MVT::i32
, Expand
);
104 for (auto OP
: {ISD::SHL_PARTS
, ISD::SRA_PARTS
, ISD::SRL_PARTS
})
105 setOperationAction(OP
, MVT::i32
, Custom
);
107 // Add/Sub overflow ops with MVT::Glues are lowered to CCR dependences.
108 for (auto VT
: {MVT::i8
, MVT::i16
, MVT::i32
}) {
109 setOperationAction(ISD::ADDC
, VT
, Custom
);
110 setOperationAction(ISD::ADDE
, VT
, Custom
);
111 setOperationAction(ISD::SUBC
, VT
, Custom
);
112 setOperationAction(ISD::SUBE
, VT
, Custom
);
115 // SADDO and friends are legal with this setup, i hope
116 for (auto VT
: {MVT::i8
, MVT::i16
, MVT::i32
}) {
117 setOperationAction(ISD::SADDO
, VT
, Custom
);
118 setOperationAction(ISD::UADDO
, VT
, Custom
);
119 setOperationAction(ISD::SSUBO
, VT
, Custom
);
120 setOperationAction(ISD::USUBO
, VT
, Custom
);
123 setOperationAction(ISD::BR_JT
, MVT::Other
, Expand
);
124 setOperationAction(ISD::BRCOND
, MVT::Other
, Custom
);
126 for (auto VT
: {MVT::i8
, MVT::i16
, MVT::i32
}) {
127 setOperationAction(ISD::BR_CC
, VT
, Expand
);
128 setOperationAction(ISD::SELECT
, VT
, Custom
);
129 setOperationAction(ISD::SELECT_CC
, VT
, Expand
);
130 setOperationAction(ISD::SETCC
, VT
, Custom
);
131 setOperationAction(ISD::SETCCCARRY
, VT
, Custom
);
134 for (auto VT
: {MVT::i8
, MVT::i16
, MVT::i32
}) {
135 setOperationAction(ISD::BSWAP
, VT
, Expand
);
136 setOperationAction(ISD::CTTZ
, VT
, Expand
);
137 setOperationAction(ISD::CTLZ
, VT
, Expand
);
138 setOperationAction(ISD::CTPOP
, VT
, Expand
);
141 setOperationAction(ISD::ConstantPool
, MVT::i32
, Custom
);
142 setOperationAction(ISD::JumpTable
, MVT::i32
, Custom
);
143 setOperationAction(ISD::GlobalAddress
, MVT::i32
, Custom
);
144 setOperationAction(ISD::GlobalTLSAddress
, MVT::i32
, Custom
);
145 setOperationAction(ISD::ExternalSymbol
, MVT::i32
, Custom
);
146 setOperationAction(ISD::BlockAddress
, MVT::i32
, Custom
);
148 setOperationAction(ISD::VASTART
, MVT::Other
, Custom
);
149 setOperationAction(ISD::VAEND
, MVT::Other
, Expand
);
150 setOperationAction(ISD::VAARG
, MVT::Other
, Expand
);
151 setOperationAction(ISD::VACOPY
, MVT::Other
, Expand
);
153 setOperationAction(ISD::STACKSAVE
, MVT::Other
, Expand
);
154 setOperationAction(ISD::STACKRESTORE
, MVT::Other
, Expand
);
156 setOperationAction(ISD::DYNAMIC_STACKALLOC
, PtrVT
, Custom
);
158 computeRegisterProperties(STI
.getRegisterInfo());
160 // We lower the `atomic-compare-and-swap` to `__sync_val_compare_and_swap`
161 // for subtarget < M68020
162 setMaxAtomicSizeInBitsSupported(32);
163 setOperationAction(ISD::ATOMIC_CMP_SWAP
, {MVT::i8
, MVT::i16
, MVT::i32
},
164 Subtarget
.atLeastM68020() ? Legal
: LibCall
);
166 setOperationAction(ISD::ATOMIC_FENCE
, MVT::Other
, Custom
);
168 // M68k does not have native read-modify-write support, so expand all of them
169 // to `__sync_fetch_*` for target < M68020, otherwise expand to CmpxChg.
170 // See `shouldExpandAtomicRMWInIR` below.
173 ISD::ATOMIC_LOAD_ADD
,
174 ISD::ATOMIC_LOAD_SUB
,
175 ISD::ATOMIC_LOAD_AND
,
177 ISD::ATOMIC_LOAD_XOR
,
178 ISD::ATOMIC_LOAD_NAND
,
179 ISD::ATOMIC_LOAD_MIN
,
180 ISD::ATOMIC_LOAD_MAX
,
181 ISD::ATOMIC_LOAD_UMIN
,
182 ISD::ATOMIC_LOAD_UMAX
,
185 {MVT::i8
, MVT::i16
, MVT::i32
}, LibCall
);
187 setMinFunctionAlignment(Align(2));
190 TargetLoweringBase::AtomicExpansionKind
191 M68kTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst
*RMW
) const {
192 return Subtarget
.atLeastM68020()
193 ? TargetLoweringBase::AtomicExpansionKind::CmpXChg
194 : TargetLoweringBase::AtomicExpansionKind::None
;
198 M68kTargetLowering::getExceptionPointerRegister(const Constant
*) const {
203 M68kTargetLowering::getExceptionSelectorRegister(const Constant
*) const {
207 InlineAsm::ConstraintCode
208 M68kTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode
) const {
209 return StringSwitch
<InlineAsm::ConstraintCode
>(ConstraintCode
)
210 .Case("Q", InlineAsm::ConstraintCode::Q
)
211 // We borrow ConstraintCode::Um for 'U'.
212 .Case("U", InlineAsm::ConstraintCode::Um
)
213 .Default(TargetLowering::getInlineAsmMemConstraint(ConstraintCode
));
216 EVT
M68kTargetLowering::getSetCCResultType(const DataLayout
&DL
,
217 LLVMContext
&Context
, EVT VT
) const {
218 // M68k SETcc producess either 0x00 or 0xFF
222 MVT
M68kTargetLowering::getScalarShiftAmountTy(const DataLayout
&DL
,
225 return Ty
.getSimpleVT();
227 return MVT::getIntegerVT(DL
.getPointerSizeInBits(0));
230 #include "M68kGenCallingConv.inc"
232 enum StructReturnType
{ NotStructReturn
, RegStructReturn
, StackStructReturn
};
234 static StructReturnType
235 callIsStructReturn(const SmallVectorImpl
<ISD::OutputArg
> &Outs
) {
237 return NotStructReturn
;
239 const ISD::ArgFlagsTy
&Flags
= Outs
[0].Flags
;
241 return NotStructReturn
;
243 return RegStructReturn
;
244 return StackStructReturn
;
247 /// Determines whether a function uses struct return semantics.
248 static StructReturnType
249 argsAreStructReturn(const SmallVectorImpl
<ISD::InputArg
> &Ins
) {
251 return NotStructReturn
;
253 const ISD::ArgFlagsTy
&Flags
= Ins
[0].Flags
;
255 return NotStructReturn
;
257 return RegStructReturn
;
258 return StackStructReturn
;
261 /// Make a copy of an aggregate at address specified by "Src" to address
262 /// "Dst" with size and alignment information specified by the specific
263 /// parameter attribute. The copy will be passed as a byval function parameter.
264 static SDValue
CreateCopyOfByValArgument(SDValue Src
, SDValue Dst
,
265 SDValue Chain
, ISD::ArgFlagsTy Flags
,
266 SelectionDAG
&DAG
, const SDLoc
&DL
) {
267 SDValue SizeNode
= DAG
.getConstant(Flags
.getByValSize(), DL
, MVT::i32
);
269 return DAG
.getMemcpy(
270 Chain
, DL
, Dst
, Src
, SizeNode
, Flags
.getNonZeroByValAlign(),
271 /*isVolatile=*/false, /*AlwaysInline=*/true,
272 /*isTailCall=*/false, MachinePointerInfo(), MachinePointerInfo());
275 /// Return true if the calling convention is one that we can guarantee TCO for.
276 static bool canGuaranteeTCO(CallingConv::ID CC
) { return false; }
278 /// Return true if we might ever do TCO for calls with this calling convention.
279 static bool mayTailCallThisCC(CallingConv::ID CC
) {
281 // C calling conventions:
285 return canGuaranteeTCO(CC
);
289 /// Return true if the function is being made into a tailcall target by
290 /// changing its ABI.
291 static bool shouldGuaranteeTCO(CallingConv::ID CC
, bool GuaranteedTailCallOpt
) {
292 return GuaranteedTailCallOpt
&& canGuaranteeTCO(CC
);
295 /// Return true if the given stack call argument is already available in the
296 /// same position (relatively) of the caller's incoming argument stack.
297 static bool MatchingStackOffset(SDValue Arg
, unsigned Offset
,
298 ISD::ArgFlagsTy Flags
, MachineFrameInfo
&MFI
,
299 const MachineRegisterInfo
*MRI
,
300 const M68kInstrInfo
*TII
,
301 const CCValAssign
&VA
) {
302 unsigned Bytes
= Arg
.getValueType().getSizeInBits() / 8;
305 // Look through nodes that don't alter the bits of the incoming value.
306 unsigned Op
= Arg
.getOpcode();
307 if (Op
== ISD::ZERO_EXTEND
|| Op
== ISD::ANY_EXTEND
|| Op
== ISD::BITCAST
) {
308 Arg
= Arg
.getOperand(0);
311 if (Op
== ISD::TRUNCATE
) {
312 const SDValue
&TruncInput
= Arg
.getOperand(0);
313 if (TruncInput
.getOpcode() == ISD::AssertZext
&&
314 cast
<VTSDNode
>(TruncInput
.getOperand(1))->getVT() ==
315 Arg
.getValueType()) {
316 Arg
= TruncInput
.getOperand(0);
324 if (Arg
.getOpcode() == ISD::CopyFromReg
) {
325 Register VR
= cast
<RegisterSDNode
>(Arg
.getOperand(1))->getReg();
326 if (!Register::isVirtualRegister(VR
))
328 MachineInstr
*Def
= MRI
->getVRegDef(VR
);
331 if (!Flags
.isByVal()) {
332 if (!TII
->isLoadFromStackSlot(*Def
, FI
))
335 unsigned Opcode
= Def
->getOpcode();
336 if ((Opcode
== M68k::LEA32p
|| Opcode
== M68k::LEA32f
) &&
337 Def
->getOperand(1).isFI()) {
338 FI
= Def
->getOperand(1).getIndex();
339 Bytes
= Flags
.getByValSize();
343 } else if (auto *Ld
= dyn_cast
<LoadSDNode
>(Arg
)) {
345 // ByVal argument is passed in as a pointer but it's now being
346 // dereferenced. e.g.
347 // define @foo(%struct.X* %A) {
348 // tail call @bar(%struct.X* byval %A)
351 SDValue Ptr
= Ld
->getBasePtr();
352 FrameIndexSDNode
*FINode
= dyn_cast
<FrameIndexSDNode
>(Ptr
);
355 FI
= FINode
->getIndex();
356 } else if (Arg
.getOpcode() == ISD::FrameIndex
&& Flags
.isByVal()) {
357 FrameIndexSDNode
*FINode
= cast
<FrameIndexSDNode
>(Arg
);
358 FI
= FINode
->getIndex();
359 Bytes
= Flags
.getByValSize();
363 assert(FI
!= INT_MAX
);
364 if (!MFI
.isFixedObjectIndex(FI
))
367 if (Offset
!= MFI
.getObjectOffset(FI
))
370 if (VA
.getLocVT().getSizeInBits() > Arg
.getValueType().getSizeInBits()) {
371 // If the argument location is wider than the argument type, check that any
372 // extension flags match.
373 if (Flags
.isZExt() != MFI
.isObjectZExt(FI
) ||
374 Flags
.isSExt() != MFI
.isObjectSExt(FI
)) {
379 return Bytes
== MFI
.getObjectSize(FI
);
383 M68kTargetLowering::getReturnAddressFrameIndex(SelectionDAG
&DAG
) const {
384 MachineFunction
&MF
= DAG
.getMachineFunction();
385 M68kMachineFunctionInfo
*FuncInfo
= MF
.getInfo
<M68kMachineFunctionInfo
>();
386 int ReturnAddrIndex
= FuncInfo
->getRAIndex();
388 if (ReturnAddrIndex
== 0) {
389 // Set up a frame object for the return address.
390 unsigned SlotSize
= Subtarget
.getSlotSize();
391 ReturnAddrIndex
= MF
.getFrameInfo().CreateFixedObject(
392 SlotSize
, -(int64_t)SlotSize
, false);
393 FuncInfo
->setRAIndex(ReturnAddrIndex
);
396 return DAG
.getFrameIndex(ReturnAddrIndex
, getPointerTy(DAG
.getDataLayout()));
399 SDValue
M68kTargetLowering::EmitTailCallLoadRetAddr(SelectionDAG
&DAG
,
402 bool IsTailCall
, int FPDiff
,
403 const SDLoc
&DL
) const {
404 EVT VT
= getPointerTy(DAG
.getDataLayout());
405 OutRetAddr
= getReturnAddressFrameIndex(DAG
);
407 // Load the "old" Return address.
408 OutRetAddr
= DAG
.getLoad(VT
, DL
, Chain
, OutRetAddr
, MachinePointerInfo());
409 return SDValue(OutRetAddr
.getNode(), 1);
412 SDValue
M68kTargetLowering::EmitTailCallStoreRetAddr(
413 SelectionDAG
&DAG
, MachineFunction
&MF
, SDValue Chain
, SDValue RetFI
,
414 EVT PtrVT
, unsigned SlotSize
, int FPDiff
, const SDLoc
&DL
) const {
418 // Calculate the new stack slot for the return address.
419 int NewFO
= MF
.getFrameInfo().CreateFixedObject(
420 SlotSize
, (int64_t)FPDiff
- SlotSize
, false);
422 SDValue NewFI
= DAG
.getFrameIndex(NewFO
, PtrVT
);
423 // Store the return address to the appropriate stack slot.
424 Chain
= DAG
.getStore(
425 Chain
, DL
, RetFI
, NewFI
,
426 MachinePointerInfo::getFixedStack(DAG
.getMachineFunction(), NewFO
));
431 M68kTargetLowering::LowerMemArgument(SDValue Chain
, CallingConv::ID CallConv
,
432 const SmallVectorImpl
<ISD::InputArg
> &Ins
,
433 const SDLoc
&DL
, SelectionDAG
&DAG
,
434 const CCValAssign
&VA
,
435 MachineFrameInfo
&MFI
,
436 unsigned ArgIdx
) const {
437 // Create the nodes corresponding to a load from this parameter slot.
438 ISD::ArgFlagsTy Flags
= Ins
[ArgIdx
].Flags
;
441 // If value is passed by pointer we have address passed instead of the value
443 if (VA
.getLocInfo() == CCValAssign::Indirect
)
444 ValVT
= VA
.getLocVT();
446 ValVT
= VA
.getValVT();
448 // Because we are dealing with BE architecture we need to offset loading of
450 int Offset
= VA
.getLocMemOffset();
451 if (VA
.getValVT() == MVT::i8
) {
453 } else if (VA
.getValVT() == MVT::i16
) {
457 // TODO Interrupt handlers
458 // Calculate SP offset of interrupt parameter, re-arrange the slot normally
459 // taken by a return address.
461 // FIXME For now, all byval parameter objects are marked mutable. This can
462 // be changed with more analysis. In case of tail call optimization mark all
463 // arguments mutable. Since they could be overwritten by lowering of arguments
464 // in case of a tail call.
465 bool AlwaysUseMutable
= shouldGuaranteeTCO(
466 CallConv
, DAG
.getTarget().Options
.GuaranteedTailCallOpt
);
467 bool IsImmutable
= !AlwaysUseMutable
&& !Flags
.isByVal();
469 if (Flags
.isByVal()) {
470 unsigned Bytes
= Flags
.getByValSize();
472 Bytes
= 1; // Don't create zero-sized stack objects.
473 int FI
= MFI
.CreateFixedObject(Bytes
, Offset
, IsImmutable
);
474 // TODO Interrupt handlers
475 // Adjust SP offset of interrupt parameter.
476 return DAG
.getFrameIndex(FI
, getPointerTy(DAG
.getDataLayout()));
479 MFI
.CreateFixedObject(ValVT
.getSizeInBits() / 8, Offset
, IsImmutable
);
481 // Set SExt or ZExt flag.
482 if (VA
.getLocInfo() == CCValAssign::ZExt
) {
483 MFI
.setObjectZExt(FI
, true);
484 } else if (VA
.getLocInfo() == CCValAssign::SExt
) {
485 MFI
.setObjectSExt(FI
, true);
488 // TODO Interrupt handlers
489 // Adjust SP offset of interrupt parameter.
491 SDValue FIN
= DAG
.getFrameIndex(FI
, getPointerTy(DAG
.getDataLayout()));
492 SDValue Val
= DAG
.getLoad(
493 ValVT
, DL
, Chain
, FIN
,
494 MachinePointerInfo::getFixedStack(DAG
.getMachineFunction(), FI
));
495 return VA
.isExtInLoc() ? DAG
.getNode(ISD::TRUNCATE
, DL
, VA
.getValVT(), Val
)
500 SDValue
M68kTargetLowering::LowerMemOpCallTo(SDValue Chain
, SDValue StackPtr
,
501 SDValue Arg
, const SDLoc
&DL
,
503 const CCValAssign
&VA
,
504 ISD::ArgFlagsTy Flags
) const {
505 unsigned LocMemOffset
= VA
.getLocMemOffset();
506 SDValue PtrOff
= DAG
.getIntPtrConstant(LocMemOffset
, DL
);
507 PtrOff
= DAG
.getNode(ISD::ADD
, DL
, getPointerTy(DAG
.getDataLayout()),
510 return CreateCopyOfByValArgument(Arg
, PtrOff
, Chain
, Flags
, DAG
, DL
);
513 Chain
, DL
, Arg
, PtrOff
,
514 MachinePointerInfo::getStack(DAG
.getMachineFunction(), LocMemOffset
));
517 //===----------------------------------------------------------------------===//
519 //===----------------------------------------------------------------------===//
521 SDValue
M68kTargetLowering::LowerCall(TargetLowering::CallLoweringInfo
&CLI
,
522 SmallVectorImpl
<SDValue
> &InVals
) const {
523 SelectionDAG
&DAG
= CLI
.DAG
;
525 SmallVectorImpl
<ISD::OutputArg
> &Outs
= CLI
.Outs
;
526 SmallVectorImpl
<SDValue
> &OutVals
= CLI
.OutVals
;
527 SmallVectorImpl
<ISD::InputArg
> &Ins
= CLI
.Ins
;
528 SDValue Chain
= CLI
.Chain
;
529 SDValue Callee
= CLI
.Callee
;
530 CallingConv::ID CallConv
= CLI
.CallConv
;
531 bool &IsTailCall
= CLI
.IsTailCall
;
532 bool IsVarArg
= CLI
.IsVarArg
;
534 MachineFunction
&MF
= DAG
.getMachineFunction();
535 StructReturnType SR
= callIsStructReturn(Outs
);
536 bool IsSibcall
= false;
537 M68kMachineFunctionInfo
*MFI
= MF
.getInfo
<M68kMachineFunctionInfo
>();
538 // const M68kRegisterInfo *TRI = Subtarget.getRegisterInfo();
540 if (CallConv
== CallingConv::M68k_INTR
)
541 report_fatal_error("M68k interrupts may not be called directly");
543 auto Attr
= MF
.getFunction().getFnAttribute("disable-tail-calls");
544 if (Attr
.getValueAsBool())
547 // FIXME Add tailcalls support
549 bool IsMustTail
= CLI
.CB
&& CLI
.CB
->isMustTailCall();
551 // Force this to be a tail call. The verifier rules are enough to ensure
552 // that we can lower this successfully without moving the return address
555 } else if (IsTailCall
) {
556 // Check if it's really possible to do a tail call.
557 IsTailCall
= IsEligibleForTailCallOptimization(
558 Callee
, CallConv
, IsVarArg
, SR
!= NotStructReturn
,
559 MF
.getFunction().hasStructRetAttr(), CLI
.RetTy
, Outs
, OutVals
, Ins
,
562 // Sibcalls are automatically detected tailcalls which do not require
564 if (!MF
.getTarget().Options
.GuaranteedTailCallOpt
&& IsTailCall
)
571 assert(!(IsVarArg
&& canGuaranteeTCO(CallConv
)) &&
572 "Var args not supported with calling convention fastcc");
574 // Analyze operands of the call, assigning locations to each operand.
575 SmallVector
<CCValAssign
, 16> ArgLocs
;
576 SmallVector
<Type
*, 4> ArgTypes
;
577 for (const auto &Arg
: CLI
.getArgs())
578 ArgTypes
.emplace_back(Arg
.Ty
);
579 M68kCCState
CCInfo(ArgTypes
, CallConv
, IsVarArg
, MF
, ArgLocs
,
581 CCInfo
.AnalyzeCallOperands(Outs
, CC_M68k
);
583 // Get a count of how many bytes are to be pushed on the stack.
584 unsigned NumBytes
= CCInfo
.getAlignedCallFrameSize();
586 // This is a sibcall. The memory operands are available in caller's
587 // own caller's stack.
589 } else if (MF
.getTarget().Options
.GuaranteedTailCallOpt
&&
590 canGuaranteeTCO(CallConv
)) {
591 NumBytes
= GetAlignedArgumentStackSize(NumBytes
, DAG
);
595 if (IsTailCall
&& !IsSibcall
&& !IsMustTail
) {
596 // Lower arguments at fp - stackoffset + fpdiff.
597 unsigned NumBytesCallerPushed
= MFI
->getBytesToPopOnReturn();
599 FPDiff
= NumBytesCallerPushed
- NumBytes
;
601 // Set the delta of movement of the returnaddr stackslot.
602 // But only set if delta is greater than previous delta.
603 if (FPDiff
< MFI
->getTCReturnAddrDelta())
604 MFI
->setTCReturnAddrDelta(FPDiff
);
607 unsigned NumBytesToPush
= NumBytes
;
608 unsigned NumBytesToPop
= NumBytes
;
610 // If we have an inalloca argument, all stack space has already been allocated
611 // for us and be right at the top of the stack. We don't support multiple
612 // arguments passed in memory when using inalloca.
613 if (!Outs
.empty() && Outs
.back().Flags
.isInAlloca()) {
615 if (!ArgLocs
.back().isMemLoc())
616 report_fatal_error("cannot use inalloca attribute on a register "
618 if (ArgLocs
.back().getLocMemOffset() != 0)
619 report_fatal_error("any parameter with the inalloca attribute must be "
620 "the only memory argument");
624 Chain
= DAG
.getCALLSEQ_START(Chain
, NumBytesToPush
,
625 NumBytes
- NumBytesToPush
, DL
);
628 // Load return address for tail calls.
629 if (IsTailCall
&& FPDiff
)
630 Chain
= EmitTailCallLoadRetAddr(DAG
, RetFI
, Chain
, IsTailCall
, FPDiff
, DL
);
632 SmallVector
<std::pair
<unsigned, SDValue
>, 8> RegsToPass
;
633 SmallVector
<SDValue
, 8> MemOpChains
;
636 // Walk the register/memloc assignments, inserting copies/loads. In the case
637 // of tail call optimization arguments are handle later.
638 const M68kRegisterInfo
*RegInfo
= Subtarget
.getRegisterInfo();
639 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
640 ISD::ArgFlagsTy Flags
= Outs
[i
].Flags
;
642 // Skip inalloca arguments, they have already been written.
643 if (Flags
.isInAlloca())
646 CCValAssign
&VA
= ArgLocs
[i
];
647 EVT RegVT
= VA
.getLocVT();
648 SDValue Arg
= OutVals
[i
];
649 bool IsByVal
= Flags
.isByVal();
651 // Promote the value if needed.
652 switch (VA
.getLocInfo()) {
654 llvm_unreachable("Unknown loc info!");
655 case CCValAssign::Full
:
657 case CCValAssign::SExt
:
658 Arg
= DAG
.getNode(ISD::SIGN_EXTEND
, DL
, RegVT
, Arg
);
660 case CCValAssign::ZExt
:
661 Arg
= DAG
.getNode(ISD::ZERO_EXTEND
, DL
, RegVT
, Arg
);
663 case CCValAssign::AExt
:
664 Arg
= DAG
.getNode(ISD::ANY_EXTEND
, DL
, RegVT
, Arg
);
666 case CCValAssign::BCvt
:
667 Arg
= DAG
.getBitcast(RegVT
, Arg
);
669 case CCValAssign::Indirect
: {
670 // Store the argument.
671 SDValue SpillSlot
= DAG
.CreateStackTemporary(VA
.getValVT());
672 int FI
= cast
<FrameIndexSDNode
>(SpillSlot
)->getIndex();
673 Chain
= DAG
.getStore(
674 Chain
, DL
, Arg
, SpillSlot
,
675 MachinePointerInfo::getFixedStack(DAG
.getMachineFunction(), FI
));
682 RegsToPass
.push_back(std::make_pair(VA
.getLocReg(), Arg
));
683 } else if (!IsSibcall
&& (!IsTailCall
|| IsByVal
)) {
684 assert(VA
.isMemLoc());
685 if (!StackPtr
.getNode()) {
686 StackPtr
= DAG
.getCopyFromReg(Chain
, DL
, RegInfo
->getStackRegister(),
687 getPointerTy(DAG
.getDataLayout()));
689 MemOpChains
.push_back(
690 LowerMemOpCallTo(Chain
, StackPtr
, Arg
, DL
, DAG
, VA
, Flags
));
694 if (!MemOpChains
.empty())
695 Chain
= DAG
.getNode(ISD::TokenFactor
, DL
, MVT::Other
, MemOpChains
);
697 // FIXME Make sure PIC style GOT works as expected
698 // The only time GOT is really needed is for Medium-PIC static data
699 // otherwise we are happy with pc-rel or static references
701 if (IsVarArg
&& IsMustTail
) {
702 const auto &Forwards
= MFI
->getForwardedMustTailRegParms();
703 for (const auto &F
: Forwards
) {
704 SDValue Val
= DAG
.getCopyFromReg(Chain
, DL
, F
.VReg
, F
.VT
);
705 RegsToPass
.push_back(std::make_pair(unsigned(F
.PReg
), Val
));
709 // For tail calls lower the arguments to the 'real' stack slots. Sibcalls
710 // don't need this because the eligibility check rejects calls that require
711 // shuffling arguments passed in memory.
712 if (!IsSibcall
&& IsTailCall
) {
713 // Force all the incoming stack arguments to be loaded from the stack
714 // before any new outgoing arguments are stored to the stack, because the
715 // outgoing stack slots may alias the incoming argument stack slots, and
716 // the alias isn't otherwise explicit. This is slightly more conservative
717 // than necessary, because it means that each store effectively depends
718 // on every argument instead of just those arguments it would clobber.
719 SDValue ArgChain
= DAG
.getStackArgumentTokenFactor(Chain
);
721 SmallVector
<SDValue
, 8> MemOpChains2
;
724 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
725 CCValAssign
&VA
= ArgLocs
[i
];
728 assert(VA
.isMemLoc());
729 SDValue Arg
= OutVals
[i
];
730 ISD::ArgFlagsTy Flags
= Outs
[i
].Flags
;
731 // Skip inalloca arguments. They don't require any work.
732 if (Flags
.isInAlloca())
734 // Create frame index.
735 int32_t Offset
= VA
.getLocMemOffset() + FPDiff
;
736 uint32_t OpSize
= (VA
.getLocVT().getSizeInBits() + 7) / 8;
737 FI
= MF
.getFrameInfo().CreateFixedObject(OpSize
, Offset
, true);
738 FIN
= DAG
.getFrameIndex(FI
, getPointerTy(DAG
.getDataLayout()));
740 if (Flags
.isByVal()) {
741 // Copy relative to framepointer.
742 SDValue Source
= DAG
.getIntPtrConstant(VA
.getLocMemOffset(), DL
);
743 if (!StackPtr
.getNode()) {
744 StackPtr
= DAG
.getCopyFromReg(Chain
, DL
, RegInfo
->getStackRegister(),
745 getPointerTy(DAG
.getDataLayout()));
747 Source
= DAG
.getNode(ISD::ADD
, DL
, getPointerTy(DAG
.getDataLayout()),
750 MemOpChains2
.push_back(
751 CreateCopyOfByValArgument(Source
, FIN
, ArgChain
, Flags
, DAG
, DL
));
753 // Store relative to framepointer.
754 MemOpChains2
.push_back(DAG
.getStore(
755 ArgChain
, DL
, Arg
, FIN
,
756 MachinePointerInfo::getFixedStack(DAG
.getMachineFunction(), FI
)));
760 if (!MemOpChains2
.empty())
761 Chain
= DAG
.getNode(ISD::TokenFactor
, DL
, MVT::Other
, MemOpChains2
);
763 // Store the return address to the appropriate stack slot.
764 Chain
= EmitTailCallStoreRetAddr(DAG
, MF
, Chain
, RetFI
,
765 getPointerTy(DAG
.getDataLayout()),
766 Subtarget
.getSlotSize(), FPDiff
, DL
);
769 // Build a sequence of copy-to-reg nodes chained together with token chain
770 // and flag operands which copy the outgoing args into registers.
772 for (unsigned i
= 0, e
= RegsToPass
.size(); i
!= e
; ++i
) {
773 Chain
= DAG
.getCopyToReg(Chain
, DL
, RegsToPass
[i
].first
,
774 RegsToPass
[i
].second
, InGlue
);
775 InGlue
= Chain
.getValue(1);
778 if (Callee
->getOpcode() == ISD::GlobalAddress
) {
779 // If the callee is a GlobalAddress node (quite common, every direct call
780 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
782 GlobalAddressSDNode
*G
= cast
<GlobalAddressSDNode
>(Callee
);
784 // We should use extra load for direct calls to dllimported functions in
786 const GlobalValue
*GV
= G
->getGlobal();
787 if (!GV
->hasDLLImportStorageClass()) {
788 unsigned char OpFlags
= Subtarget
.classifyGlobalFunctionReference(GV
);
790 Callee
= DAG
.getTargetGlobalAddress(
791 GV
, DL
, getPointerTy(DAG
.getDataLayout()), G
->getOffset(), OpFlags
);
793 if (OpFlags
== M68kII::MO_GOTPCREL
) {
796 Callee
= DAG
.getNode(M68kISD::WrapperPC
, DL
,
797 getPointerTy(DAG
.getDataLayout()), Callee
);
799 // Add extra indirection
800 Callee
= DAG
.getLoad(
801 getPointerTy(DAG
.getDataLayout()), DL
, DAG
.getEntryNode(), Callee
,
802 MachinePointerInfo::getGOT(DAG
.getMachineFunction()));
805 } else if (ExternalSymbolSDNode
*S
= dyn_cast
<ExternalSymbolSDNode
>(Callee
)) {
806 const Module
*Mod
= DAG
.getMachineFunction().getFunction().getParent();
807 unsigned char OpFlags
=
808 Subtarget
.classifyGlobalFunctionReference(nullptr, *Mod
);
810 Callee
= DAG
.getTargetExternalSymbol(
811 S
->getSymbol(), getPointerTy(DAG
.getDataLayout()), OpFlags
);
814 // Returns a chain & a flag for retval copy to use.
815 SDVTList NodeTys
= DAG
.getVTList(MVT::Other
, MVT::Glue
);
816 SmallVector
<SDValue
, 8> Ops
;
818 if (!IsSibcall
&& IsTailCall
) {
819 Chain
= DAG
.getCALLSEQ_END(Chain
, NumBytesToPop
, 0, InGlue
, DL
);
820 InGlue
= Chain
.getValue(1);
823 Ops
.push_back(Chain
);
824 Ops
.push_back(Callee
);
827 Ops
.push_back(DAG
.getConstant(FPDiff
, DL
, MVT::i32
));
829 // Add argument registers to the end of the list so that they are known live
831 for (unsigned i
= 0, e
= RegsToPass
.size(); i
!= e
; ++i
)
832 Ops
.push_back(DAG
.getRegister(RegsToPass
[i
].first
,
833 RegsToPass
[i
].second
.getValueType()));
835 // Add a register mask operand representing the call-preserved registers.
836 const uint32_t *Mask
= RegInfo
->getCallPreservedMask(MF
, CallConv
);
837 assert(Mask
&& "Missing call preserved mask for calling convention");
839 Ops
.push_back(DAG
.getRegisterMask(Mask
));
841 if (InGlue
.getNode())
842 Ops
.push_back(InGlue
);
845 MF
.getFrameInfo().setHasTailCall();
846 return DAG
.getNode(M68kISD::TC_RETURN
, DL
, NodeTys
, Ops
);
849 Chain
= DAG
.getNode(M68kISD::CALL
, DL
, NodeTys
, Ops
);
850 InGlue
= Chain
.getValue(1);
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;
862 NumBytesForCalleeToPop
= 0; // Callee pops nothing.
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
;
871 // Returns a flag for retval copy to use.
873 Chain
= DAG
.getCALLSEQ_END(Chain
, NumBytesToPop
, NumBytesForCalleeToPop
,
875 InGlue
= Chain
.getValue(1);
878 // Handle result values, copying them out of physregs into vregs that we
880 return LowerCallResult(Chain
, InGlue
, CallConv
, IsVarArg
, Ins
, DL
, DAG
,
884 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 {
889 // Assign locations to each value returned by this call.
890 SmallVector
<CCValAssign
, 16> RVLocs
;
891 CCState
CCInfo(CallConv
, IsVarArg
, DAG
.getMachineFunction(), RVLocs
,
893 CCInfo
.AnalyzeCallResult(Ins
, RetCC_M68k
);
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();
900 /// ??? is this correct?
901 Chain
= DAG
.getCopyFromReg(Chain
, DL
, VA
.getLocReg(), CopyVT
, InGlue
)
903 SDValue Val
= Chain
.getValue(0);
905 if (VA
.isExtInLoc() && VA
.getValVT().getScalarType() == MVT::i1
)
906 Val
= DAG
.getNode(ISD::TRUNCATE
, DL
, VA
.getValVT(), Val
);
908 InGlue
= Chain
.getValue(2);
909 InVals
.push_back(Val
);
915 //===----------------------------------------------------------------------===//
916 // Formal Arguments Calling Convention Implementation
917 //===----------------------------------------------------------------------===//
919 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();
927 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
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());
936 CCInfo
.AnalyzeFormalArguments(Ins
, CC_M68k
);
938 unsigned LastVal
= ~0U;
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");
944 LastVal
= VA
.getValNo();
947 EVT RegVT
= VA
.getLocVT();
948 const TargetRegisterClass
*RC
;
949 if (RegVT
== MVT::i32
)
950 RC
= &M68k::XR32RegClass
;
952 llvm_unreachable("Unknown argument type!");
954 Register Reg
= MF
.addLiveIn(VA
.getLocReg(), RC
);
955 ArgValue
= DAG
.getCopyFromReg(Chain
, DL
, Reg
, RegVT
);
957 // If this is an 8 or 16-bit value, it is really passed promoted to 32
958 // bits. Insert an assert[sz]ext to capture this, then truncate to the
960 if (VA
.getLocInfo() == CCValAssign::SExt
) {
961 ArgValue
= DAG
.getNode(ISD::AssertSext
, DL
, RegVT
, ArgValue
,
962 DAG
.getValueType(VA
.getValVT()));
963 } else if (VA
.getLocInfo() == CCValAssign::ZExt
) {
964 ArgValue
= DAG
.getNode(ISD::AssertZext
, DL
, RegVT
, ArgValue
,
965 DAG
.getValueType(VA
.getValVT()));
966 } else if (VA
.getLocInfo() == CCValAssign::BCvt
) {
967 ArgValue
= DAG
.getBitcast(VA
.getValVT(), ArgValue
);
970 if (VA
.isExtInLoc()) {
971 ArgValue
= DAG
.getNode(ISD::TRUNCATE
, DL
, VA
.getValVT(), ArgValue
);
974 assert(VA
.isMemLoc());
975 ArgValue
= LowerMemArgument(Chain
, CCID
, Ins
, DL
, DAG
, VA
, MFI
, i
);
978 // If value is passed via pointer - do a load.
979 // TODO Make sure this handling on indirect arguments is correct
980 if (VA
.getLocInfo() == CCValAssign::Indirect
)
982 DAG
.getLoad(VA
.getValVT(), DL
, Chain
, ArgValue
, MachinePointerInfo());
984 InVals
.push_back(ArgValue
);
987 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
988 // Swift calling convention does not require we copy the sret argument
989 // into %D0 for the return. We don't set SRetReturnReg for Swift.
990 if (CCID
== CallingConv::Swift
)
993 // ABI require that for returning structs by value we copy the sret argument
994 // into %D0 for the return. Save the argument into a virtual register so
995 // that we can access it from the return points.
996 if (Ins
[i
].Flags
.isSRet()) {
997 unsigned Reg
= MMFI
->getSRetReturnReg();
999 MVT PtrTy
= getPointerTy(DAG
.getDataLayout());
1000 Reg
= MF
.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy
));
1001 MMFI
->setSRetReturnReg(Reg
);
1003 SDValue Copy
= DAG
.getCopyToReg(DAG
.getEntryNode(), DL
, Reg
, InVals
[i
]);
1004 Chain
= DAG
.getNode(ISD::TokenFactor
, DL
, MVT::Other
, Copy
, Chain
);
1009 unsigned StackSize
= CCInfo
.getStackSize();
1010 // Align stack specially for tail calls.
1011 if (shouldGuaranteeTCO(CCID
, MF
.getTarget().Options
.GuaranteedTailCallOpt
))
1012 StackSize
= GetAlignedArgumentStackSize(StackSize
, DAG
);
1014 // If the function takes variable number of arguments, make a frame index for
1015 // the start of the first vararg value... for expansion of llvm.va_start. We
1016 // can skip this if there are no va_start calls.
1017 if (MFI
.hasVAStart()) {
1018 MMFI
->setVarArgsFrameIndex(MFI
.CreateFixedObject(1, StackSize
, true));
1021 if (IsVarArg
&& MFI
.hasMustTailInVarArgFunc()) {
1022 // We forward some GPRs and some vector types.
1023 SmallVector
<MVT
, 2> RegParmTypes
;
1024 MVT IntVT
= MVT::i32
;
1025 RegParmTypes
.push_back(IntVT
);
1027 // Compute the set of forwarded registers. The rest are scratch.
1028 // ??? what is this for?
1029 SmallVectorImpl
<ForwardedRegister
> &Forwards
=
1030 MMFI
->getForwardedMustTailRegParms();
1031 CCInfo
.analyzeMustTailForwardedRegisters(Forwards
, RegParmTypes
, CC_M68k
);
1033 // Copy all forwards from physical to virtual registers.
1034 for (ForwardedRegister
&F
: Forwards
) {
1035 // FIXME Can we use a less constrained schedule?
1036 SDValue RegVal
= DAG
.getCopyFromReg(Chain
, DL
, F
.VReg
, F
.VT
);
1037 F
.VReg
= MF
.getRegInfo().createVirtualRegister(getRegClassFor(F
.VT
));
1038 Chain
= DAG
.getCopyToReg(Chain
, DL
, F
.VReg
, RegVal
);
1042 // Some CCs need callee pop.
1043 if (M68k::isCalleePop(CCID
, IsVarArg
,
1044 MF
.getTarget().Options
.GuaranteedTailCallOpt
)) {
1045 MMFI
->setBytesToPopOnReturn(StackSize
); // Callee pops everything.
1047 MMFI
->setBytesToPopOnReturn(0); // Callee pops nothing.
1048 // If this is an sret function, the return should pop the hidden pointer.
1049 if (!canGuaranteeTCO(CCID
) && argsAreStructReturn(Ins
) == StackStructReturn
)
1050 MMFI
->setBytesToPopOnReturn(4);
1053 MMFI
->setArgumentStackSize(StackSize
);
1058 //===----------------------------------------------------------------------===//
1059 // Return Value Calling Convention Implementation
1060 //===----------------------------------------------------------------------===//
1062 bool M68kTargetLowering::CanLowerReturn(
1063 CallingConv::ID CCID
, MachineFunction
&MF
, bool IsVarArg
,
1064 const SmallVectorImpl
<ISD::OutputArg
> &Outs
, LLVMContext
&Context
) const {
1065 SmallVector
<CCValAssign
, 16> RVLocs
;
1066 CCState
CCInfo(CCID
, IsVarArg
, MF
, RVLocs
, Context
);
1067 return CCInfo
.CheckReturn(Outs
, RetCC_M68k
);
1071 M68kTargetLowering::LowerReturn(SDValue Chain
, CallingConv::ID CCID
,
1073 const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
1074 const SmallVectorImpl
<SDValue
> &OutVals
,
1075 const SDLoc
&DL
, SelectionDAG
&DAG
) const {
1076 MachineFunction
&MF
= DAG
.getMachineFunction();
1077 M68kMachineFunctionInfo
*MFI
= MF
.getInfo
<M68kMachineFunctionInfo
>();
1079 SmallVector
<CCValAssign
, 16> RVLocs
;
1080 CCState
CCInfo(CCID
, IsVarArg
, MF
, RVLocs
, *DAG
.getContext());
1081 CCInfo
.AnalyzeReturn(Outs
, RetCC_M68k
);
1084 SmallVector
<SDValue
, 6> RetOps
;
1085 // Operand #0 = Chain (updated below)
1086 RetOps
.push_back(Chain
);
1087 // Operand #1 = Bytes To Pop
1089 DAG
.getTargetConstant(MFI
->getBytesToPopOnReturn(), DL
, MVT::i32
));
1091 // Copy the result values into the output registers.
1092 for (unsigned i
= 0, e
= RVLocs
.size(); i
!= e
; ++i
) {
1093 CCValAssign
&VA
= RVLocs
[i
];
1094 assert(VA
.isRegLoc() && "Can only return in registers!");
1095 SDValue ValToCopy
= OutVals
[i
];
1096 EVT ValVT
= ValToCopy
.getValueType();
1098 // Promote values to the appropriate types.
1099 if (VA
.getLocInfo() == CCValAssign::SExt
)
1100 ValToCopy
= DAG
.getNode(ISD::SIGN_EXTEND
, DL
, VA
.getLocVT(), ValToCopy
);
1101 else if (VA
.getLocInfo() == CCValAssign::ZExt
)
1102 ValToCopy
= DAG
.getNode(ISD::ZERO_EXTEND
, DL
, VA
.getLocVT(), ValToCopy
);
1103 else if (VA
.getLocInfo() == CCValAssign::AExt
) {
1104 if (ValVT
.isVector() && ValVT
.getVectorElementType() == MVT::i1
)
1105 ValToCopy
= DAG
.getNode(ISD::SIGN_EXTEND
, DL
, VA
.getLocVT(), ValToCopy
);
1107 ValToCopy
= DAG
.getNode(ISD::ANY_EXTEND
, DL
, VA
.getLocVT(), ValToCopy
);
1108 } else if (VA
.getLocInfo() == CCValAssign::BCvt
)
1109 ValToCopy
= DAG
.getBitcast(VA
.getLocVT(), ValToCopy
);
1111 Chain
= DAG
.getCopyToReg(Chain
, DL
, VA
.getLocReg(), ValToCopy
, Glue
);
1112 Glue
= Chain
.getValue(1);
1113 RetOps
.push_back(DAG
.getRegister(VA
.getLocReg(), VA
.getLocVT()));
1116 // Swift calling convention does not require we copy the sret argument
1117 // into %d0 for the return, and SRetReturnReg is not set for Swift.
1119 // ABI require that for returning structs by value we copy the sret argument
1120 // into %D0 for the return. Save the argument into a virtual register so that
1121 // we can access it from the return points.
1123 // Checking Function.hasStructRetAttr() here is insufficient because the IR
1124 // may not have an explicit sret argument. If MFI.CanLowerReturn is
1125 // false, then an sret argument may be implicitly inserted in the SelDAG. In
1126 // either case MFI->setSRetReturnReg() will have been called.
1127 if (unsigned SRetReg
= MFI
->getSRetReturnReg()) {
1128 // ??? Can i just move this to the top and escape this explanation?
1129 // When we have both sret and another return value, we should use the
1130 // original Chain stored in RetOps[0], instead of the current Chain updated
1131 // in the above loop. If we only have sret, RetOps[0] equals to Chain.
1133 // For the case of sret and another return value, we have
1134 // Chain_0 at the function entry
1135 // Chain_1 = getCopyToReg(Chain_0) in the above loop
1136 // If we use Chain_1 in getCopyFromReg, we will have
1137 // Val = getCopyFromReg(Chain_1)
1138 // Chain_2 = getCopyToReg(Chain_1, Val) from below
1140 // getCopyToReg(Chain_0) will be glued together with
1141 // getCopyToReg(Chain_1, Val) into Unit A, getCopyFromReg(Chain_1) will be
1142 // in Unit B, and we will have cyclic dependency between Unit A and Unit B:
1143 // Data dependency from Unit B to Unit A due to usage of Val in
1144 // getCopyToReg(Chain_1, Val)
1145 // Chain dependency from Unit A to Unit B
1147 // So here, we use RetOps[0] (i.e Chain_0) for getCopyFromReg.
1148 SDValue Val
= DAG
.getCopyFromReg(RetOps
[0], DL
, SRetReg
,
1149 getPointerTy(MF
.getDataLayout()));
1151 // ??? How will this work if CC does not use registers for args passing?
1152 // ??? What if I return multiple structs?
1153 unsigned RetValReg
= M68k::D0
;
1154 Chain
= DAG
.getCopyToReg(Chain
, DL
, RetValReg
, Val
, Glue
);
1155 Glue
= Chain
.getValue(1);
1158 DAG
.getRegister(RetValReg
, getPointerTy(DAG
.getDataLayout())));
1161 RetOps
[0] = Chain
; // Update chain.
1163 // Add the glue if we have it.
1165 RetOps
.push_back(Glue
);
1167 return DAG
.getNode(M68kISD::RET
, DL
, MVT::Other
, RetOps
);
1170 //===----------------------------------------------------------------------===//
1171 // Fast Calling Convention (tail call) implementation
1172 //===----------------------------------------------------------------------===//
1174 // Like std call, callee cleans arguments, convention except that ECX is
1175 // reserved for storing the tail called function address. Only 2 registers are
1176 // free for argument passing (inreg). Tail call optimization is performed
1178 // * tailcallopt is enabled
1179 // * caller/callee are fastcc
1180 // On M68k_64 architecture with GOT-style position independent code only
1181 // local (within module) calls are supported at the moment. To keep the stack
1182 // aligned according to platform abi the function GetAlignedArgumentStackSize
1183 // ensures that argument delta is always multiples of stack alignment. (Dynamic
1184 // linkers need this - darwin's dyld for example) If a tail called function
1185 // callee has more arguments than the caller the caller needs to make sure that
1186 // there is room to move the RETADDR to. This is achieved by reserving an area
1187 // the size of the argument delta right after the original RETADDR, but before
1188 // the saved framepointer or the spilled registers e.g. caller(arg1, arg2)
1189 // calls callee(arg1, arg2,arg3,arg4) stack layout:
1200 /// Make the stack size align e.g 16n + 12 aligned for a 16-byte align
1203 M68kTargetLowering::GetAlignedArgumentStackSize(unsigned StackSize
,
1204 SelectionDAG
&DAG
) const {
1205 const TargetFrameLowering
&TFI
= *Subtarget
.getFrameLowering();
1206 unsigned StackAlignment
= TFI
.getStackAlignment();
1207 uint64_t AlignMask
= StackAlignment
- 1;
1208 int64_t Offset
= StackSize
;
1209 unsigned SlotSize
= Subtarget
.getSlotSize();
1210 if ((Offset
& AlignMask
) <= (StackAlignment
- SlotSize
)) {
1211 // Number smaller than 12 so just add the difference.
1212 Offset
+= ((StackAlignment
- SlotSize
) - (Offset
& AlignMask
));
1214 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1216 ((~AlignMask
) & Offset
) + StackAlignment
+ (StackAlignment
- SlotSize
);
1221 /// Check whether the call is eligible for tail call optimization. Targets
1222 /// that want to do tail call optimization should implement this function.
1223 bool M68kTargetLowering::IsEligibleForTailCallOptimization(
1224 SDValue Callee
, CallingConv::ID CalleeCC
, bool IsVarArg
,
1225 bool IsCalleeStructRet
, bool IsCallerStructRet
, Type
*RetTy
,
1226 const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
1227 const SmallVectorImpl
<SDValue
> &OutVals
,
1228 const SmallVectorImpl
<ISD::InputArg
> &Ins
, SelectionDAG
&DAG
) const {
1229 if (!mayTailCallThisCC(CalleeCC
))
1232 // If -tailcallopt is specified, make fastcc functions tail-callable.
1233 MachineFunction
&MF
= DAG
.getMachineFunction();
1234 const auto &CallerF
= MF
.getFunction();
1236 CallingConv::ID CallerCC
= CallerF
.getCallingConv();
1237 bool CCMatch
= CallerCC
== CalleeCC
;
1239 if (DAG
.getTarget().Options
.GuaranteedTailCallOpt
) {
1240 if (canGuaranteeTCO(CalleeCC
) && CCMatch
)
1245 // Look for obvious safe cases to perform tail call optimization that do not
1246 // require ABI changes. This is what gcc calls sibcall.
1248 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
1249 // emit a special epilogue.
1250 const M68kRegisterInfo
*RegInfo
= Subtarget
.getRegisterInfo();
1251 if (RegInfo
->hasStackRealignment(MF
))
1254 // Also avoid sibcall optimization if either caller or callee uses struct
1255 // return semantics.
1256 if (IsCalleeStructRet
|| IsCallerStructRet
)
1259 // Do not sibcall optimize vararg calls unless all arguments are passed via
1261 LLVMContext
&C
= *DAG
.getContext();
1262 if (IsVarArg
&& !Outs
.empty()) {
1264 SmallVector
<CCValAssign
, 16> ArgLocs
;
1265 CCState
CCInfo(CalleeCC
, IsVarArg
, MF
, ArgLocs
, C
);
1267 CCInfo
.AnalyzeCallOperands(Outs
, CC_M68k
);
1268 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
)
1269 if (!ArgLocs
[i
].isRegLoc())
1273 // Check that the call results are passed in the same way.
1274 if (!CCState::resultsCompatible(CalleeCC
, CallerCC
, MF
, C
, Ins
, RetCC_M68k
,
1278 // The callee has to preserve all registers the caller needs to preserve.
1279 const M68kRegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
1280 const uint32_t *CallerPreserved
= TRI
->getCallPreservedMask(MF
, CallerCC
);
1282 const uint32_t *CalleePreserved
= TRI
->getCallPreservedMask(MF
, CalleeCC
);
1283 if (!TRI
->regmaskSubsetEqual(CallerPreserved
, CalleePreserved
))
1287 unsigned StackArgsSize
= 0;
1289 // If the callee takes no arguments then go on to check the results of the
1291 if (!Outs
.empty()) {
1292 // Check if stack adjustment is needed. For now, do not do this if any
1293 // argument is passed on the stack.
1294 SmallVector
<CCValAssign
, 16> ArgLocs
;
1295 CCState
CCInfo(CalleeCC
, IsVarArg
, MF
, ArgLocs
, C
);
1297 CCInfo
.AnalyzeCallOperands(Outs
, CC_M68k
);
1298 StackArgsSize
= CCInfo
.getStackSize();
1300 if (StackArgsSize
) {
1301 // Check if the arguments are already laid out in the right way as
1302 // the caller's fixed stack objects.
1303 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1304 const MachineRegisterInfo
*MRI
= &MF
.getRegInfo();
1305 const M68kInstrInfo
*TII
= Subtarget
.getInstrInfo();
1306 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1307 CCValAssign
&VA
= ArgLocs
[i
];
1308 SDValue Arg
= OutVals
[i
];
1309 ISD::ArgFlagsTy Flags
= Outs
[i
].Flags
;
1310 if (VA
.getLocInfo() == CCValAssign::Indirect
)
1312 if (!VA
.isRegLoc()) {
1313 if (!MatchingStackOffset(Arg
, VA
.getLocMemOffset(), Flags
, MFI
, MRI
,
1320 bool PositionIndependent
= isPositionIndependent();
1321 // If the tailcall address may be in a register, then make sure it's
1322 // possible to register allocate for it. The call address can
1323 // only target %A0 or %A1 since the tail call must be scheduled after
1324 // callee-saved registers are restored. These happen to be the same
1325 // registers used to pass 'inreg' arguments so watch out for those.
1326 if ((!isa
<GlobalAddressSDNode
>(Callee
) &&
1327 !isa
<ExternalSymbolSDNode
>(Callee
)) ||
1328 PositionIndependent
) {
1329 unsigned NumInRegs
= 0;
1330 // In PIC we need an extra register to formulate the address computation
1332 unsigned MaxInRegs
= PositionIndependent
? 1 : 2;
1334 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1335 CCValAssign
&VA
= ArgLocs
[i
];
1338 Register Reg
= VA
.getLocReg();
1344 if (++NumInRegs
== MaxInRegs
)
1351 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1352 if (!parametersInCSRMatch(MRI
, CallerPreserved
, ArgLocs
, OutVals
))
1356 bool CalleeWillPop
= M68k::isCalleePop(
1357 CalleeCC
, IsVarArg
, MF
.getTarget().Options
.GuaranteedTailCallOpt
);
1359 if (unsigned BytesToPop
=
1360 MF
.getInfo
<M68kMachineFunctionInfo
>()->getBytesToPopOnReturn()) {
1361 // If we have bytes to pop, the callee must pop them.
1362 bool CalleePopMatches
= CalleeWillPop
&& BytesToPop
== StackArgsSize
;
1363 if (!CalleePopMatches
)
1365 } else if (CalleeWillPop
&& StackArgsSize
> 0) {
1366 // If we don't have bytes to pop, make sure the callee doesn't pop any.
1373 //===----------------------------------------------------------------------===//
1375 //===----------------------------------------------------------------------===//
1377 SDValue
M68kTargetLowering::LowerOperation(SDValue Op
,
1378 SelectionDAG
&DAG
) const {
1379 switch (Op
.getOpcode()) {
1381 llvm_unreachable("Should not custom lower this!");
1388 return LowerXALUO(Op
, DAG
);
1390 return LowerSETCC(Op
, DAG
);
1391 case ISD::SETCCCARRY
:
1392 return LowerSETCCCARRY(Op
, DAG
);
1394 return LowerSELECT(Op
, DAG
);
1396 return LowerBRCOND(Op
, DAG
);
1401 return LowerADDC_ADDE_SUBC_SUBE(Op
, DAG
);
1402 case ISD::ConstantPool
:
1403 return LowerConstantPool(Op
, DAG
);
1404 case ISD::GlobalAddress
:
1405 return LowerGlobalAddress(Op
, DAG
);
1406 case ISD::ExternalSymbol
:
1407 return LowerExternalSymbol(Op
, DAG
);
1408 case ISD::BlockAddress
:
1409 return LowerBlockAddress(Op
, DAG
);
1410 case ISD::JumpTable
:
1411 return LowerJumpTable(Op
, DAG
);
1413 return LowerVASTART(Op
, DAG
);
1414 case ISD::DYNAMIC_STACKALLOC
:
1415 return LowerDYNAMIC_STACKALLOC(Op
, DAG
);
1416 case ISD::SHL_PARTS
:
1417 return LowerShiftLeftParts(Op
, DAG
);
1418 case ISD::SRA_PARTS
:
1419 return LowerShiftRightParts(Op
, DAG
, true);
1420 case ISD::SRL_PARTS
:
1421 return LowerShiftRightParts(Op
, DAG
, false);
1422 case ISD::ATOMIC_FENCE
:
1423 return LowerATOMICFENCE(Op
, DAG
);
1424 case ISD::GlobalTLSAddress
:
1425 return LowerGlobalTLSAddress(Op
, DAG
);
1429 SDValue
M68kTargetLowering::LowerExternalSymbolCall(SelectionDAG
&DAG
,
1431 llvm::StringRef SymbolName
,
1432 ArgListTy
&&ArgList
) const {
1433 PointerType
*PtrTy
= PointerType::get(*DAG
.getContext(), 0);
1434 CallLoweringInfo
CLI(DAG
);
1435 CLI
.setDebugLoc(Loc
)
1436 .setChain(DAG
.getEntryNode())
1437 .setLibCallee(CallingConv::C
, PtrTy
,
1438 DAG
.getExternalSymbol(SymbolName
.data(),
1439 getPointerMemTy(DAG
.getDataLayout())),
1440 std::move(ArgList
));
1441 return LowerCallTo(CLI
).first
;
1444 SDValue
M68kTargetLowering::getTLSGetAddr(GlobalAddressSDNode
*GA
,
1446 unsigned TargetFlags
) const {
1447 SDValue GOT
= DAG
.getGLOBAL_OFFSET_TABLE(MVT::i32
);
1448 SDValue TGA
= DAG
.getTargetGlobalAddress(
1449 GA
->getGlobal(), GA
, GA
->getValueType(0), GA
->getOffset(), TargetFlags
);
1450 SDValue Arg
= DAG
.getNode(ISD::ADD
, SDLoc(GA
), MVT::i32
, GOT
, TGA
);
1452 PointerType
*PtrTy
= PointerType::get(*DAG
.getContext(), 0);
1458 Args
.push_back(Entry
);
1459 return LowerExternalSymbolCall(DAG
, SDLoc(GA
), "__tls_get_addr",
1463 SDValue
M68kTargetLowering::getM68kReadTp(SDLoc Loc
, SelectionDAG
&DAG
) const {
1464 return LowerExternalSymbolCall(DAG
, Loc
, "__m68k_read_tp", ArgListTy());
1467 SDValue
M68kTargetLowering::LowerTLSGeneralDynamic(GlobalAddressSDNode
*GA
,
1468 SelectionDAG
&DAG
) const {
1469 return getTLSGetAddr(GA
, DAG
, M68kII::MO_TLSGD
);
1472 SDValue
M68kTargetLowering::LowerTLSLocalDynamic(GlobalAddressSDNode
*GA
,
1473 SelectionDAG
&DAG
) const {
1474 SDValue Addr
= getTLSGetAddr(GA
, DAG
, M68kII::MO_TLSLDM
);
1476 DAG
.getTargetGlobalAddress(GA
->getGlobal(), GA
, GA
->getValueType(0),
1477 GA
->getOffset(), M68kII::MO_TLSLD
);
1478 return DAG
.getNode(ISD::ADD
, SDLoc(GA
), MVT::i32
, TGA
, Addr
);
1481 SDValue
M68kTargetLowering::LowerTLSInitialExec(GlobalAddressSDNode
*GA
,
1482 SelectionDAG
&DAG
) const {
1483 SDValue GOT
= DAG
.getGLOBAL_OFFSET_TABLE(MVT::i32
);
1484 SDValue Tp
= getM68kReadTp(SDLoc(GA
), DAG
);
1486 DAG
.getTargetGlobalAddress(GA
->getGlobal(), GA
, GA
->getValueType(0),
1487 GA
->getOffset(), M68kII::MO_TLSIE
);
1488 SDValue Addr
= DAG
.getNode(ISD::ADD
, SDLoc(GA
), MVT::i32
, TGA
, GOT
);
1490 DAG
.getLoad(MVT::i32
, SDLoc(GA
), DAG
.getEntryNode(), Addr
,
1491 MachinePointerInfo::getGOT(DAG
.getMachineFunction()));
1493 return DAG
.getNode(ISD::ADD
, SDLoc(GA
), MVT::i32
, Offset
, Tp
);
1496 SDValue
M68kTargetLowering::LowerTLSLocalExec(GlobalAddressSDNode
*GA
,
1497 SelectionDAG
&DAG
) const {
1498 SDValue Tp
= getM68kReadTp(SDLoc(GA
), DAG
);
1500 DAG
.getTargetGlobalAddress(GA
->getGlobal(), GA
, GA
->getValueType(0),
1501 GA
->getOffset(), M68kII::MO_TLSLE
);
1502 return DAG
.getNode(ISD::ADD
, SDLoc(GA
), MVT::i32
, TGA
, Tp
);
1505 SDValue
M68kTargetLowering::LowerGlobalTLSAddress(SDValue Op
,
1506 SelectionDAG
&DAG
) const {
1507 assert(Subtarget
.isTargetELF());
1509 auto *GA
= cast
<GlobalAddressSDNode
>(Op
);
1510 TLSModel::Model AccessModel
= DAG
.getTarget().getTLSModel(GA
->getGlobal());
1512 switch (AccessModel
) {
1513 case TLSModel::GeneralDynamic
:
1514 return LowerTLSGeneralDynamic(GA
, DAG
);
1515 case TLSModel::LocalDynamic
:
1516 return LowerTLSLocalDynamic(GA
, DAG
);
1517 case TLSModel::InitialExec
:
1518 return LowerTLSInitialExec(GA
, DAG
);
1519 case TLSModel::LocalExec
:
1520 return LowerTLSLocalExec(GA
, DAG
);
1523 llvm_unreachable("Unexpected TLS access model type");
1526 bool M68kTargetLowering::decomposeMulByConstant(LLVMContext
&Context
, EVT VT
,
1528 // Shifts and add instructions in M68000 and M68010 support
1529 // up to 32 bits, but mul only has 16-bit variant. So it's almost
1530 // certainly beneficial to lower 8/16/32-bit mul to their
1531 // add / shifts counterparts. But for 64-bits mul, it might be
1532 // safer to just leave it to compiler runtime implementations.
1533 return VT
.bitsLE(MVT::i32
) || Subtarget
.atLeastM68020();
1536 SDValue
M68kTargetLowering::LowerXALUO(SDValue Op
, SelectionDAG
&DAG
) const {
1537 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
1538 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
1539 // looks for this combo and may remove the "setcc" instruction if the "setcc"
1540 // has only one use.
1541 SDNode
*N
= Op
.getNode();
1542 SDValue LHS
= N
->getOperand(0);
1543 SDValue RHS
= N
->getOperand(1);
1544 unsigned BaseOp
= 0;
1547 switch (Op
.getOpcode()) {
1549 llvm_unreachable("Unknown ovf instruction!");
1551 BaseOp
= M68kISD::ADD
;
1552 Cond
= M68k::COND_VS
;
1555 BaseOp
= M68kISD::ADD
;
1556 Cond
= M68k::COND_CS
;
1559 BaseOp
= M68kISD::SUB
;
1560 Cond
= M68k::COND_VS
;
1563 BaseOp
= M68kISD::SUB
;
1564 Cond
= M68k::COND_CS
;
1569 SDVTList VTs
= DAG
.getVTList(N
->getValueType(0), MVT::i8
);
1570 SDValue Arith
= DAG
.getNode(BaseOp
, DL
, VTs
, LHS
, RHS
);
1571 SDValue SetCC
= DAG
.getNode(M68kISD::SETCC
, DL
, N
->getValueType(1),
1572 DAG
.getConstant(Cond
, DL
, MVT::i8
),
1573 SDValue(Arith
.getNode(), 1));
1575 return DAG
.getNode(ISD::MERGE_VALUES
, DL
, N
->getVTList(), Arith
, SetCC
);
1578 /// Create a BTST (Bit Test) node - Test bit \p BitNo in \p Src and set
1579 /// condition according to equal/not-equal condition code \p CC.
1580 static SDValue
getBitTestCondition(SDValue Src
, SDValue BitNo
, ISD::CondCode CC
,
1581 const SDLoc
&DL
, SelectionDAG
&DAG
) {
1582 // If Src is i8, promote it to i32 with any_extend. There is no i8 BTST
1583 // instruction. Since the shift amount is in-range-or-undefined, we know
1584 // that doing a bittest on the i32 value is ok.
1585 if (Src
.getValueType() == MVT::i8
|| Src
.getValueType() == MVT::i16
)
1586 Src
= DAG
.getNode(ISD::ANY_EXTEND
, DL
, MVT::i32
, Src
);
1588 // If the operand types disagree, extend the shift amount to match. Since
1589 // BTST ignores high bits (like shifts) we can use anyextend.
1590 if (Src
.getValueType() != BitNo
.getValueType())
1591 BitNo
= DAG
.getNode(ISD::ANY_EXTEND
, DL
, Src
.getValueType(), BitNo
);
1593 SDValue BTST
= DAG
.getNode(M68kISD::BTST
, DL
, MVT::i32
, Src
, BitNo
);
1595 // NOTE BTST sets CCR.Z flag
1596 M68k::CondCode Cond
= CC
== ISD::SETEQ
? M68k::COND_NE
: M68k::COND_EQ
;
1597 return DAG
.getNode(M68kISD::SETCC
, DL
, MVT::i8
,
1598 DAG
.getConstant(Cond
, DL
, MVT::i8
), BTST
);
1601 /// Result of 'and' is compared against zero. Change to a BTST node if possible.
1602 static SDValue
LowerAndToBTST(SDValue And
, ISD::CondCode CC
, const SDLoc
&DL
,
1603 SelectionDAG
&DAG
) {
1604 SDValue Op0
= And
.getOperand(0);
1605 SDValue Op1
= And
.getOperand(1);
1606 if (Op0
.getOpcode() == ISD::TRUNCATE
)
1607 Op0
= Op0
.getOperand(0);
1608 if (Op1
.getOpcode() == ISD::TRUNCATE
)
1609 Op1
= Op1
.getOperand(0);
1612 if (Op1
.getOpcode() == ISD::SHL
)
1613 std::swap(Op0
, Op1
);
1614 if (Op0
.getOpcode() == ISD::SHL
) {
1615 if (isOneConstant(Op0
.getOperand(0))) {
1616 // If we looked past a truncate, check that it's only truncating away
1618 unsigned BitWidth
= Op0
.getValueSizeInBits();
1619 unsigned AndBitWidth
= And
.getValueSizeInBits();
1620 if (BitWidth
> AndBitWidth
) {
1621 auto Known
= DAG
.computeKnownBits(Op0
);
1622 if (Known
.countMinLeadingZeros() < BitWidth
- AndBitWidth
)
1626 RHS
= Op0
.getOperand(1);
1628 } else if (auto *AndRHS
= dyn_cast
<ConstantSDNode
>(Op1
)) {
1629 uint64_t AndRHSVal
= AndRHS
->getZExtValue();
1630 SDValue AndLHS
= Op0
;
1632 if (AndRHSVal
== 1 && AndLHS
.getOpcode() == ISD::SRL
) {
1633 LHS
= AndLHS
.getOperand(0);
1634 RHS
= AndLHS
.getOperand(1);
1637 // Use BTST if the immediate can't be encoded in a TEST instruction.
1638 if (!isUInt
<32>(AndRHSVal
) && isPowerOf2_64(AndRHSVal
)) {
1640 RHS
= DAG
.getConstant(Log2_64_Ceil(AndRHSVal
), DL
, LHS
.getValueType());
1645 return getBitTestCondition(LHS
, RHS
, CC
, DL
, DAG
);
1650 static M68k::CondCode
TranslateIntegerM68kCC(ISD::CondCode SetCCOpcode
) {
1651 switch (SetCCOpcode
) {
1653 llvm_unreachable("Invalid integer condition!");
1655 return M68k::COND_EQ
;
1657 return M68k::COND_GT
;
1659 return M68k::COND_GE
;
1661 return M68k::COND_LT
;
1663 return M68k::COND_LE
;
1665 return M68k::COND_NE
;
1667 return M68k::COND_CS
;
1669 return M68k::COND_CC
;
1671 return M68k::COND_HI
;
1673 return M68k::COND_LS
;
1677 /// Do a one-to-one translation of a ISD::CondCode to the M68k-specific
1678 /// condition code, returning the condition code and the LHS/RHS of the
1679 /// comparison to make.
1680 static unsigned TranslateM68kCC(ISD::CondCode SetCCOpcode
, const SDLoc
&DL
,
1681 bool IsFP
, SDValue
&LHS
, SDValue
&RHS
,
1682 SelectionDAG
&DAG
) {
1684 if (ConstantSDNode
*RHSC
= dyn_cast
<ConstantSDNode
>(RHS
)) {
1685 if (SetCCOpcode
== ISD::SETGT
&& RHSC
->isAllOnes()) {
1686 // X > -1 -> X == 0, jump !sign.
1687 RHS
= DAG
.getConstant(0, DL
, RHS
.getValueType());
1688 return M68k::COND_PL
;
1690 if (SetCCOpcode
== ISD::SETLT
&& RHSC
->isZero()) {
1691 // X < 0 -> X == 0, jump on sign.
1692 return M68k::COND_MI
;
1694 if (SetCCOpcode
== ISD::SETLT
&& RHSC
->getZExtValue() == 1) {
1696 RHS
= DAG
.getConstant(0, DL
, RHS
.getValueType());
1697 return M68k::COND_LE
;
1701 return TranslateIntegerM68kCC(SetCCOpcode
);
1704 // First determine if it is required or is profitable to flip the operands.
1706 // If LHS is a foldable load, but RHS is not, flip the condition.
1707 if (ISD::isNON_EXTLoad(LHS
.getNode()) && !ISD::isNON_EXTLoad(RHS
.getNode())) {
1708 SetCCOpcode
= getSetCCSwappedOperands(SetCCOpcode
);
1709 std::swap(LHS
, RHS
);
1712 switch (SetCCOpcode
) {
1719 std::swap(LHS
, RHS
);
1723 // On a floating point condition, the flags are set as follows:
1725 // 0 | 0 | 0 | X > Y
1726 // 0 | 0 | 1 | X < Y
1727 // 1 | 0 | 0 | X == Y
1728 // 1 | 1 | 1 | unordered
1729 switch (SetCCOpcode
) {
1731 llvm_unreachable("Condcode should be pre-legalized away");
1734 return M68k::COND_EQ
;
1735 case ISD::SETOLT
: // flipped
1738 return M68k::COND_HI
;
1739 case ISD::SETOLE
: // flipped
1742 return M68k::COND_CC
;
1743 case ISD::SETUGT
: // flipped
1746 return M68k::COND_CS
;
1747 case ISD::SETUGE
: // flipped
1750 return M68k::COND_LS
;
1753 return M68k::COND_NE
;
1756 return M68k::COND_INVALID
;
1760 // Convert (truncate (srl X, N) to i1) to (bt X, N)
1761 static SDValue
LowerTruncateToBTST(SDValue Op
, ISD::CondCode CC
,
1762 const SDLoc
&DL
, SelectionDAG
&DAG
) {
1764 assert(Op
.getOpcode() == ISD::TRUNCATE
&& Op
.getValueType() == MVT::i1
&&
1765 "Expected TRUNCATE to i1 node");
1767 if (Op
.getOperand(0).getOpcode() != ISD::SRL
)
1770 SDValue ShiftRight
= Op
.getOperand(0);
1771 return getBitTestCondition(ShiftRight
.getOperand(0), ShiftRight
.getOperand(1),
1775 /// \brief return true if \c Op has a use that doesn't just read flags.
1776 static bool hasNonFlagsUse(SDValue Op
) {
1777 for (SDNode::use_iterator UI
= Op
->use_begin(), UE
= Op
->use_end(); UI
!= UE
;
1780 unsigned UOpNo
= UI
.getOperandNo();
1781 if (User
->getOpcode() == ISD::TRUNCATE
&& User
->hasOneUse()) {
1782 // Look pass truncate.
1783 UOpNo
= User
->use_begin().getOperandNo();
1784 User
= *User
->use_begin();
1787 if (User
->getOpcode() != ISD::BRCOND
&& User
->getOpcode() != ISD::SETCC
&&
1788 !(User
->getOpcode() == ISD::SELECT
&& UOpNo
== 0))
1794 SDValue
M68kTargetLowering::EmitTest(SDValue Op
, unsigned M68kCC
,
1795 const SDLoc
&DL
, SelectionDAG
&DAG
) const {
1797 // CF and OF aren't always set the way we want. Determine which
1798 // of these we need.
1799 bool NeedCF
= false;
1800 bool NeedOF
= false;
1815 case M68k::COND_VC
: {
1816 // Check if we really need to set the
1817 // Overflow flag. If NoSignedWrap is present
1818 // that is not actually needed.
1819 switch (Op
->getOpcode()) {
1824 if (Op
.getNode()->getFlags().hasNoSignedWrap())
1835 // See if we can use the CCR value from the operand instead of
1836 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
1837 // we prove that the arithmetic won't overflow, we can't use OF or CF.
1838 if (Op
.getResNo() != 0 || NeedOF
|| NeedCF
) {
1839 // Emit a CMP with 0, which is the TEST pattern.
1840 return DAG
.getNode(M68kISD::CMP
, DL
, MVT::i8
,
1841 DAG
.getConstant(0, DL
, Op
.getValueType()), Op
);
1843 unsigned Opcode
= 0;
1844 unsigned NumOperands
= 0;
1846 // Truncate operations may prevent the merge of the SETCC instruction
1847 // and the arithmetic instruction before it. Attempt to truncate the operands
1848 // of the arithmetic instruction and use a reduced bit-width instruction.
1849 bool NeedTruncation
= false;
1850 SDValue ArithOp
= Op
;
1851 if (Op
->getOpcode() == ISD::TRUNCATE
&& Op
->hasOneUse()) {
1852 SDValue Arith
= Op
->getOperand(0);
1853 // Both the trunc and the arithmetic op need to have one user each.
1854 if (Arith
->hasOneUse())
1855 switch (Arith
.getOpcode()) {
1863 NeedTruncation
= true;
1869 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
1870 // which may be the result of a CAST. We use the variable 'Op', which is the
1871 // non-casted variable when we check for possible users.
1872 switch (ArithOp
.getOpcode()) {
1874 Opcode
= M68kISD::ADD
;
1879 // If we have a constant logical shift that's only used in a comparison
1880 // against zero turn it into an equivalent AND. This allows turning it into
1881 // a TEST instruction later.
1882 if ((M68kCC
== M68k::COND_EQ
|| M68kCC
== M68k::COND_NE
) &&
1883 Op
->hasOneUse() && isa
<ConstantSDNode
>(Op
->getOperand(1)) &&
1884 !hasNonFlagsUse(Op
)) {
1885 EVT VT
= Op
.getValueType();
1886 unsigned BitWidth
= VT
.getSizeInBits();
1887 unsigned ShAmt
= Op
->getConstantOperandVal(1);
1888 if (ShAmt
>= BitWidth
) // Avoid undefined shifts.
1890 APInt Mask
= ArithOp
.getOpcode() == ISD::SRL
1891 ? APInt::getHighBitsSet(BitWidth
, BitWidth
- ShAmt
)
1892 : APInt::getLowBitsSet(BitWidth
, BitWidth
- ShAmt
);
1893 if (!Mask
.isSignedIntN(32)) // Avoid large immediates.
1895 Op
= DAG
.getNode(ISD::AND
, DL
, VT
, Op
->getOperand(0),
1896 DAG
.getConstant(Mask
, DL
, VT
));
1901 // If the primary 'and' result isn't used, don't bother using
1902 // M68kISD::AND, because a TEST instruction will be better.
1903 if (!hasNonFlagsUse(Op
)) {
1904 SDValue Op0
= ArithOp
->getOperand(0);
1905 SDValue Op1
= ArithOp
->getOperand(1);
1906 EVT VT
= ArithOp
.getValueType();
1907 bool IsAndn
= isBitwiseNot(Op0
) || isBitwiseNot(Op1
);
1908 bool IsLegalAndnType
= VT
== MVT::i32
|| VT
== MVT::i64
;
1910 // But if we can combine this into an ANDN operation, then create an AND
1911 // now and allow it to be pattern matched into an ANDN.
1912 if (/*!Subtarget.hasBMI() ||*/ !IsAndn
|| !IsLegalAndnType
)
1919 // Due to the ISEL shortcoming noted above, be conservative if this op is
1920 // likely to be selected as part of a load-modify-store instruction.
1921 for (const auto *U
: Op
.getNode()->uses())
1922 if (U
->getOpcode() == ISD::STORE
)
1925 // Otherwise use a regular CCR-setting instruction.
1926 switch (ArithOp
.getOpcode()) {
1928 llvm_unreachable("unexpected operator!");
1930 Opcode
= M68kISD::SUB
;
1933 Opcode
= M68kISD::XOR
;
1936 Opcode
= M68kISD::AND
;
1939 Opcode
= M68kISD::OR
;
1950 return SDValue(Op
.getNode(), 1);
1956 // If we found that truncation is beneficial, perform the truncation and
1958 if (NeedTruncation
) {
1959 EVT VT
= Op
.getValueType();
1960 SDValue WideVal
= Op
->getOperand(0);
1961 EVT WideVT
= WideVal
.getValueType();
1962 unsigned ConvertedOp
= 0;
1963 // Use a target machine opcode to prevent further DAGCombine
1964 // optimizations that may separate the arithmetic operations
1965 // from the setcc node.
1966 switch (WideVal
.getOpcode()) {
1970 ConvertedOp
= M68kISD::ADD
;
1973 ConvertedOp
= M68kISD::SUB
;
1976 ConvertedOp
= M68kISD::AND
;
1979 ConvertedOp
= M68kISD::OR
;
1982 ConvertedOp
= M68kISD::XOR
;
1987 const TargetLowering
&TLI
= DAG
.getTargetLoweringInfo();
1988 if (TLI
.isOperationLegal(WideVal
.getOpcode(), WideVT
)) {
1989 SDValue V0
= DAG
.getNode(ISD::TRUNCATE
, DL
, VT
, WideVal
.getOperand(0));
1990 SDValue V1
= DAG
.getNode(ISD::TRUNCATE
, DL
, VT
, WideVal
.getOperand(1));
1991 Op
= DAG
.getNode(ConvertedOp
, DL
, VT
, V0
, V1
);
1997 // Emit a CMP with 0, which is the TEST pattern.
1998 return DAG
.getNode(M68kISD::CMP
, DL
, MVT::i8
,
1999 DAG
.getConstant(0, DL
, Op
.getValueType()), Op
);
2001 SDVTList VTs
= DAG
.getVTList(Op
.getValueType(), MVT::i8
);
2002 SmallVector
<SDValue
, 4> Ops(Op
->op_begin(), Op
->op_begin() + NumOperands
);
2004 SDValue New
= DAG
.getNode(Opcode
, DL
, VTs
, Ops
);
2005 DAG
.ReplaceAllUsesWith(Op
, New
);
2006 return SDValue(New
.getNode(), 1);
2009 /// \brief Return true if the condition is an unsigned comparison operation.
2010 static bool isM68kCCUnsigned(unsigned M68kCC
) {
2013 llvm_unreachable("Invalid integer condition!");
2029 SDValue
M68kTargetLowering::EmitCmp(SDValue Op0
, SDValue Op1
, unsigned M68kCC
,
2030 const SDLoc
&DL
, SelectionDAG
&DAG
) const {
2031 if (isNullConstant(Op1
))
2032 return EmitTest(Op0
, M68kCC
, DL
, DAG
);
2034 assert(!(isa
<ConstantSDNode
>(Op1
) && Op0
.getValueType() == MVT::i1
) &&
2035 "Unexpected comparison operation for MVT::i1 operands");
2037 if ((Op0
.getValueType() == MVT::i8
|| Op0
.getValueType() == MVT::i16
||
2038 Op0
.getValueType() == MVT::i32
|| Op0
.getValueType() == MVT::i64
)) {
2039 // Only promote the compare up to I32 if it is a 16 bit operation
2040 // with an immediate. 16 bit immediates are to be avoided.
2041 if ((Op0
.getValueType() == MVT::i16
&&
2042 (isa
<ConstantSDNode
>(Op0
) || isa
<ConstantSDNode
>(Op1
))) &&
2043 !DAG
.getMachineFunction().getFunction().hasMinSize()) {
2045 isM68kCCUnsigned(M68kCC
) ? ISD::ZERO_EXTEND
: ISD::SIGN_EXTEND
;
2046 Op0
= DAG
.getNode(ExtendOp
, DL
, MVT::i32
, Op0
);
2047 Op1
= DAG
.getNode(ExtendOp
, DL
, MVT::i32
, Op1
);
2049 // Use SUB instead of CMP to enable CSE between SUB and CMP.
2050 SDVTList VTs
= DAG
.getVTList(Op0
.getValueType(), MVT::i8
);
2051 SDValue Sub
= DAG
.getNode(M68kISD::SUB
, DL
, VTs
, Op0
, Op1
);
2052 return SDValue(Sub
.getNode(), 1);
2054 return DAG
.getNode(M68kISD::CMP
, DL
, MVT::i8
, Op0
, Op1
);
2057 /// Result of 'and' or 'trunc to i1' is compared against zero.
2058 /// Change to a BTST node if possible.
2059 SDValue
M68kTargetLowering::LowerToBTST(SDValue Op
, ISD::CondCode CC
,
2061 SelectionDAG
&DAG
) const {
2062 if (Op
.getOpcode() == ISD::AND
)
2063 return LowerAndToBTST(Op
, CC
, DL
, DAG
);
2064 if (Op
.getOpcode() == ISD::TRUNCATE
&& Op
.getValueType() == MVT::i1
)
2065 return LowerTruncateToBTST(Op
, CC
, DL
, DAG
);
2069 SDValue
M68kTargetLowering::LowerSETCC(SDValue Op
, SelectionDAG
&DAG
) const {
2070 MVT VT
= Op
.getSimpleValueType();
2071 assert(VT
== MVT::i8
&& "SetCC type must be 8-bit integer");
2073 SDValue Op0
= Op
.getOperand(0);
2074 SDValue Op1
= Op
.getOperand(1);
2076 ISD::CondCode CC
= cast
<CondCodeSDNode
>(Op
.getOperand(2))->get();
2078 // Optimize to BTST if possible.
2079 // Lower (X & (1 << N)) == 0 to BTST(X, N).
2080 // Lower ((X >>u N) & 1) != 0 to BTST(X, N).
2081 // Lower ((X >>s N) & 1) != 0 to BTST(X, N).
2082 // Lower (trunc (X >> N) to i1) to BTST(X, N).
2083 if (Op0
.hasOneUse() && isNullConstant(Op1
) &&
2084 (CC
== ISD::SETEQ
|| CC
== ISD::SETNE
)) {
2085 if (SDValue NewSetCC
= LowerToBTST(Op0
, CC
, DL
, DAG
)) {
2087 return DAG
.getNode(ISD::TRUNCATE
, DL
, MVT::i1
, NewSetCC
);
2092 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
2094 if ((isOneConstant(Op1
) || isNullConstant(Op1
)) &&
2095 (CC
== ISD::SETEQ
|| CC
== ISD::SETNE
)) {
2097 // If the input is a setcc, then reuse the input setcc or use a new one with
2098 // the inverted condition.
2099 if (Op0
.getOpcode() == M68kISD::SETCC
) {
2100 M68k::CondCode CCode
= (M68k::CondCode
)Op0
.getConstantOperandVal(0);
2101 bool Invert
= (CC
== ISD::SETNE
) ^ isNullConstant(Op1
);
2105 CCode
= M68k::GetOppositeBranchCondition(CCode
);
2107 DAG
.getNode(M68kISD::SETCC
, DL
, MVT::i8
,
2108 DAG
.getConstant(CCode
, DL
, MVT::i8
), Op0
.getOperand(1));
2110 return DAG
.getNode(ISD::TRUNCATE
, DL
, MVT::i1
, SetCC
);
2114 if (Op0
.getValueType() == MVT::i1
&& (CC
== ISD::SETEQ
|| CC
== ISD::SETNE
)) {
2115 if (isOneConstant(Op1
)) {
2116 ISD::CondCode NewCC
= ISD::GlobalISel::getSetCCInverse(CC
, true);
2117 return DAG
.getSetCC(DL
, VT
, Op0
, DAG
.getConstant(0, DL
, MVT::i1
), NewCC
);
2119 if (!isNullConstant(Op1
)) {
2120 SDValue Xor
= DAG
.getNode(ISD::XOR
, DL
, MVT::i1
, Op0
, Op1
);
2121 return DAG
.getSetCC(DL
, VT
, Xor
, DAG
.getConstant(0, DL
, MVT::i1
), CC
);
2125 bool IsFP
= Op1
.getSimpleValueType().isFloatingPoint();
2126 unsigned M68kCC
= TranslateM68kCC(CC
, DL
, IsFP
, Op0
, Op1
, DAG
);
2127 if (M68kCC
== M68k::COND_INVALID
)
2130 SDValue CCR
= EmitCmp(Op0
, Op1
, M68kCC
, DL
, DAG
);
2131 return DAG
.getNode(M68kISD::SETCC
, DL
, MVT::i8
,
2132 DAG
.getConstant(M68kCC
, DL
, MVT::i8
), CCR
);
2135 SDValue
M68kTargetLowering::LowerSETCCCARRY(SDValue Op
,
2136 SelectionDAG
&DAG
) const {
2137 SDValue LHS
= Op
.getOperand(0);
2138 SDValue RHS
= Op
.getOperand(1);
2139 SDValue Carry
= Op
.getOperand(2);
2140 SDValue Cond
= Op
.getOperand(3);
2143 assert(LHS
.getSimpleValueType().isInteger() && "SETCCCARRY is integer only.");
2144 M68k::CondCode CC
= TranslateIntegerM68kCC(cast
<CondCodeSDNode
>(Cond
)->get());
2146 EVT CarryVT
= Carry
.getValueType();
2147 APInt NegOne
= APInt::getAllOnes(CarryVT
.getScalarSizeInBits());
2148 Carry
= DAG
.getNode(M68kISD::ADD
, DL
, DAG
.getVTList(CarryVT
, MVT::i32
), Carry
,
2149 DAG
.getConstant(NegOne
, DL
, CarryVT
));
2151 SDVTList VTs
= DAG
.getVTList(LHS
.getValueType(), MVT::i32
);
2153 DAG
.getNode(M68kISD::SUBX
, DL
, VTs
, LHS
, RHS
, Carry
.getValue(1));
2155 return DAG
.getNode(M68kISD::SETCC
, DL
, MVT::i8
,
2156 DAG
.getConstant(CC
, DL
, MVT::i8
), Cmp
.getValue(1));
2159 /// Return true if opcode is a M68k logical comparison.
2160 static bool isM68kLogicalCmp(SDValue Op
) {
2161 unsigned Opc
= Op
.getNode()->getOpcode();
2162 if (Opc
== M68kISD::CMP
)
2164 if (Op
.getResNo() == 1 &&
2165 (Opc
== M68kISD::ADD
|| Opc
== M68kISD::SUB
|| Opc
== M68kISD::ADDX
||
2166 Opc
== M68kISD::SUBX
|| Opc
== M68kISD::SMUL
|| Opc
== M68kISD::UMUL
||
2167 Opc
== M68kISD::OR
|| Opc
== M68kISD::XOR
|| Opc
== M68kISD::AND
))
2170 if (Op
.getResNo() == 2 && Opc
== M68kISD::UMUL
)
2176 static bool isTruncWithZeroHighBitsInput(SDValue V
, SelectionDAG
&DAG
) {
2177 if (V
.getOpcode() != ISD::TRUNCATE
)
2180 SDValue VOp0
= V
.getOperand(0);
2181 unsigned InBits
= VOp0
.getValueSizeInBits();
2182 unsigned Bits
= V
.getValueSizeInBits();
2183 return DAG
.MaskedValueIsZero(VOp0
,
2184 APInt::getHighBitsSet(InBits
, InBits
- Bits
));
2187 SDValue
M68kTargetLowering::LowerSELECT(SDValue Op
, SelectionDAG
&DAG
) const {
2188 bool addTest
= true;
2189 SDValue Cond
= Op
.getOperand(0);
2190 SDValue Op1
= Op
.getOperand(1);
2191 SDValue Op2
= Op
.getOperand(2);
2195 if (Cond
.getOpcode() == ISD::SETCC
) {
2196 if (SDValue NewCond
= LowerSETCC(Cond
, DAG
))
2200 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
2201 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
2202 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
2203 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
2204 if (Cond
.getOpcode() == M68kISD::SETCC
&&
2205 Cond
.getOperand(1).getOpcode() == M68kISD::CMP
&&
2206 isNullConstant(Cond
.getOperand(1).getOperand(0))) {
2207 SDValue Cmp
= Cond
.getOperand(1);
2210 cast
<ConstantSDNode
>(Cond
.getOperand(0))->getZExtValue();
2212 if ((isAllOnesConstant(Op1
) || isAllOnesConstant(Op2
)) &&
2213 (CondCode
== M68k::COND_EQ
|| CondCode
== M68k::COND_NE
)) {
2214 SDValue Y
= isAllOnesConstant(Op2
) ? Op1
: Op2
;
2216 SDValue CmpOp0
= Cmp
.getOperand(1);
2217 // Apply further optimizations for special cases
2218 // (select (x != 0), -1, 0) -> neg & sbb
2219 // (select (x == 0), 0, -1) -> neg & sbb
2220 if (isNullConstant(Y
) &&
2221 (isAllOnesConstant(Op1
) == (CondCode
== M68k::COND_NE
))) {
2223 SDVTList VTs
= DAG
.getVTList(CmpOp0
.getValueType(), MVT::i32
);
2226 DAG
.getNode(M68kISD::SUB
, DL
, VTs
,
2227 DAG
.getConstant(0, DL
, CmpOp0
.getValueType()), CmpOp0
);
2229 SDValue Res
= DAG
.getNode(M68kISD::SETCC_CARRY
, DL
, Op
.getValueType(),
2230 DAG
.getConstant(M68k::COND_CS
, DL
, MVT::i8
),
2231 SDValue(Neg
.getNode(), 1));
2235 Cmp
= DAG
.getNode(M68kISD::CMP
, DL
, MVT::i8
,
2236 DAG
.getConstant(1, DL
, CmpOp0
.getValueType()), CmpOp0
);
2238 SDValue Res
= // Res = 0 or -1.
2239 DAG
.getNode(M68kISD::SETCC_CARRY
, DL
, Op
.getValueType(),
2240 DAG
.getConstant(M68k::COND_CS
, DL
, MVT::i8
), Cmp
);
2242 if (isAllOnesConstant(Op1
) != (CondCode
== M68k::COND_EQ
))
2243 Res
= DAG
.getNOT(DL
, Res
, Res
.getValueType());
2245 if (!isNullConstant(Op2
))
2246 Res
= DAG
.getNode(ISD::OR
, DL
, Res
.getValueType(), Res
, Y
);
2251 // Look past (and (setcc_carry (cmp ...)), 1).
2252 if (Cond
.getOpcode() == ISD::AND
&&
2253 Cond
.getOperand(0).getOpcode() == M68kISD::SETCC_CARRY
&&
2254 isOneConstant(Cond
.getOperand(1)))
2255 Cond
= Cond
.getOperand(0);
2257 // If condition flag is set by a M68kISD::CMP, then use it as the condition
2258 // setting operand in place of the M68kISD::SETCC.
2259 unsigned CondOpcode
= Cond
.getOpcode();
2260 if (CondOpcode
== M68kISD::SETCC
|| CondOpcode
== M68kISD::SETCC_CARRY
) {
2261 CC
= Cond
.getOperand(0);
2263 SDValue Cmp
= Cond
.getOperand(1);
2264 unsigned Opc
= Cmp
.getOpcode();
2266 bool IllegalFPCMov
= false;
2268 if ((isM68kLogicalCmp(Cmp
) && !IllegalFPCMov
) || Opc
== M68kISD::BTST
) {
2272 } else if (CondOpcode
== ISD::USUBO
|| CondOpcode
== ISD::SSUBO
||
2273 CondOpcode
== ISD::UADDO
|| CondOpcode
== ISD::SADDO
||
2274 CondOpcode
== ISD::UMULO
|| CondOpcode
== ISD::SMULO
) {
2275 SDValue LHS
= Cond
.getOperand(0);
2276 SDValue RHS
= Cond
.getOperand(1);
2280 switch (CondOpcode
) {
2282 MxOpcode
= M68kISD::ADD
;
2283 MxCond
= M68k::COND_CS
;
2286 MxOpcode
= M68kISD::ADD
;
2287 MxCond
= M68k::COND_VS
;
2290 MxOpcode
= M68kISD::SUB
;
2291 MxCond
= M68k::COND_CS
;
2294 MxOpcode
= M68kISD::SUB
;
2295 MxCond
= M68k::COND_VS
;
2298 MxOpcode
= M68kISD::UMUL
;
2299 MxCond
= M68k::COND_VS
;
2302 MxOpcode
= M68kISD::SMUL
;
2303 MxCond
= M68k::COND_VS
;
2306 llvm_unreachable("unexpected overflowing operator");
2308 if (CondOpcode
== ISD::UMULO
)
2309 VTs
= DAG
.getVTList(LHS
.getValueType(), LHS
.getValueType(), MVT::i32
);
2311 VTs
= DAG
.getVTList(LHS
.getValueType(), MVT::i32
);
2313 SDValue MxOp
= DAG
.getNode(MxOpcode
, DL
, VTs
, LHS
, RHS
);
2315 if (CondOpcode
== ISD::UMULO
)
2316 Cond
= MxOp
.getValue(2);
2318 Cond
= MxOp
.getValue(1);
2320 CC
= DAG
.getConstant(MxCond
, DL
, MVT::i8
);
2325 // Look past the truncate if the high bits are known zero.
2326 if (isTruncWithZeroHighBitsInput(Cond
, DAG
))
2327 Cond
= Cond
.getOperand(0);
2329 // We know the result of AND is compared against zero. Try to match
2331 if (Cond
.getOpcode() == ISD::AND
&& Cond
.hasOneUse()) {
2332 if (SDValue NewSetCC
= LowerToBTST(Cond
, ISD::SETNE
, DL
, DAG
)) {
2333 CC
= NewSetCC
.getOperand(0);
2334 Cond
= NewSetCC
.getOperand(1);
2341 CC
= DAG
.getConstant(M68k::COND_NE
, DL
, MVT::i8
);
2342 Cond
= EmitTest(Cond
, M68k::COND_NE
, DL
, DAG
);
2345 // a < b ? -1 : 0 -> RES = ~setcc_carry
2346 // a < b ? 0 : -1 -> RES = setcc_carry
2347 // a >= b ? -1 : 0 -> RES = setcc_carry
2348 // a >= b ? 0 : -1 -> RES = ~setcc_carry
2349 if (Cond
.getOpcode() == M68kISD::SUB
) {
2350 unsigned CondCode
= cast
<ConstantSDNode
>(CC
)->getZExtValue();
2352 if ((CondCode
== M68k::COND_CC
|| CondCode
== M68k::COND_CS
) &&
2353 (isAllOnesConstant(Op1
) || isAllOnesConstant(Op2
)) &&
2354 (isNullConstant(Op1
) || isNullConstant(Op2
))) {
2356 DAG
.getNode(M68kISD::SETCC_CARRY
, DL
, Op
.getValueType(),
2357 DAG
.getConstant(M68k::COND_CS
, DL
, MVT::i8
), Cond
);
2358 if (isAllOnesConstant(Op1
) != (CondCode
== M68k::COND_CS
))
2359 return DAG
.getNOT(DL
, Res
, Res
.getValueType());
2364 // M68k doesn't have an i8 cmov. If both operands are the result of a
2365 // truncate widen the cmov and push the truncate through. This avoids
2366 // introducing a new branch during isel and doesn't add any extensions.
2367 if (Op
.getValueType() == MVT::i8
&& Op1
.getOpcode() == ISD::TRUNCATE
&&
2368 Op2
.getOpcode() == ISD::TRUNCATE
) {
2369 SDValue T1
= Op1
.getOperand(0), T2
= Op2
.getOperand(0);
2370 if (T1
.getValueType() == T2
.getValueType() &&
2371 // Block CopyFromReg so partial register stalls are avoided.
2372 T1
.getOpcode() != ISD::CopyFromReg
&&
2373 T2
.getOpcode() != ISD::CopyFromReg
) {
2374 SDVTList VTs
= DAG
.getVTList(T1
.getValueType(), MVT::Glue
);
2375 SDValue Cmov
= DAG
.getNode(M68kISD::CMOV
, DL
, VTs
, T2
, T1
, CC
, Cond
);
2376 return DAG
.getNode(ISD::TRUNCATE
, DL
, Op
.getValueType(), Cmov
);
2380 // M68kISD::CMOV means set the result (which is operand 1) to the RHS if
2381 // condition is true.
2382 SDVTList VTs
= DAG
.getVTList(Op
.getValueType(), MVT::Glue
);
2383 SDValue Ops
[] = {Op2
, Op1
, CC
, Cond
};
2384 return DAG
.getNode(M68kISD::CMOV
, DL
, VTs
, Ops
);
2387 /// Return true if node is an ISD::AND or ISD::OR of two M68k::SETcc nodes
2388 /// each of which has no other use apart from the AND / OR.
2389 static bool isAndOrOfSetCCs(SDValue Op
, unsigned &Opc
) {
2390 Opc
= Op
.getOpcode();
2391 if (Opc
!= ISD::OR
&& Opc
!= ISD::AND
)
2393 return (M68k::IsSETCC(Op
.getOperand(0).getOpcode()) &&
2394 Op
.getOperand(0).hasOneUse() &&
2395 M68k::IsSETCC(Op
.getOperand(1).getOpcode()) &&
2396 Op
.getOperand(1).hasOneUse());
2399 /// Return true if node is an ISD::XOR of a M68kISD::SETCC and 1 and that the
2400 /// SETCC node has a single use.
2401 static bool isXor1OfSetCC(SDValue Op
) {
2402 if (Op
.getOpcode() != ISD::XOR
)
2404 if (isOneConstant(Op
.getOperand(1)))
2405 return Op
.getOperand(0).getOpcode() == M68kISD::SETCC
&&
2406 Op
.getOperand(0).hasOneUse();
2410 SDValue
M68kTargetLowering::LowerBRCOND(SDValue Op
, SelectionDAG
&DAG
) const {
2411 bool AddTest
= true;
2412 SDValue Chain
= Op
.getOperand(0);
2413 SDValue Cond
= Op
.getOperand(1);
2414 SDValue Dest
= Op
.getOperand(2);
2417 bool Inverted
= false;
2419 if (Cond
.getOpcode() == ISD::SETCC
) {
2420 // Check for setcc([su]{add,sub}o == 0).
2421 if (cast
<CondCodeSDNode
>(Cond
.getOperand(2))->get() == ISD::SETEQ
&&
2422 isNullConstant(Cond
.getOperand(1)) &&
2423 Cond
.getOperand(0).getResNo() == 1 &&
2424 (Cond
.getOperand(0).getOpcode() == ISD::SADDO
||
2425 Cond
.getOperand(0).getOpcode() == ISD::UADDO
||
2426 Cond
.getOperand(0).getOpcode() == ISD::SSUBO
||
2427 Cond
.getOperand(0).getOpcode() == ISD::USUBO
)) {
2429 Cond
= Cond
.getOperand(0);
2431 if (SDValue NewCond
= LowerSETCC(Cond
, DAG
))
2436 // Look pass (and (setcc_carry (cmp ...)), 1).
2437 if (Cond
.getOpcode() == ISD::AND
&&
2438 Cond
.getOperand(0).getOpcode() == M68kISD::SETCC_CARRY
&&
2439 isOneConstant(Cond
.getOperand(1)))
2440 Cond
= Cond
.getOperand(0);
2442 // If condition flag is set by a M68kISD::CMP, then use it as the condition
2443 // setting operand in place of the M68kISD::SETCC.
2444 unsigned CondOpcode
= Cond
.getOpcode();
2445 if (CondOpcode
== M68kISD::SETCC
|| CondOpcode
== M68kISD::SETCC_CARRY
) {
2446 CC
= Cond
.getOperand(0);
2448 SDValue Cmp
= Cond
.getOperand(1);
2449 unsigned Opc
= Cmp
.getOpcode();
2451 if (isM68kLogicalCmp(Cmp
) || Opc
== M68kISD::BTST
) {
2455 switch (cast
<ConstantSDNode
>(CC
)->getZExtValue()) {
2460 // These can only come from an arithmetic instruction with overflow,
2461 // e.g. SADDO, UADDO.
2462 Cond
= Cond
.getNode()->getOperand(1);
2468 CondOpcode
= Cond
.getOpcode();
2469 if (CondOpcode
== ISD::UADDO
|| CondOpcode
== ISD::SADDO
||
2470 CondOpcode
== ISD::USUBO
|| CondOpcode
== ISD::SSUBO
) {
2471 SDValue LHS
= Cond
.getOperand(0);
2472 SDValue RHS
= Cond
.getOperand(1);
2476 // Keep this in sync with LowerXALUO, otherwise we might create redundant
2477 // instructions that can't be removed afterwards (i.e. M68kISD::ADD and
2479 switch (CondOpcode
) {
2481 MxOpcode
= M68kISD::ADD
;
2482 MxCond
= M68k::COND_CS
;
2485 MxOpcode
= M68kISD::ADD
;
2486 MxCond
= M68k::COND_VS
;
2489 MxOpcode
= M68kISD::SUB
;
2490 MxCond
= M68k::COND_CS
;
2493 MxOpcode
= M68kISD::SUB
;
2494 MxCond
= M68k::COND_VS
;
2497 MxOpcode
= M68kISD::UMUL
;
2498 MxCond
= M68k::COND_VS
;
2501 MxOpcode
= M68kISD::SMUL
;
2502 MxCond
= M68k::COND_VS
;
2505 llvm_unreachable("unexpected overflowing operator");
2509 MxCond
= M68k::GetOppositeBranchCondition((M68k::CondCode
)MxCond
);
2511 if (CondOpcode
== ISD::UMULO
)
2512 VTs
= DAG
.getVTList(LHS
.getValueType(), LHS
.getValueType(), MVT::i8
);
2514 VTs
= DAG
.getVTList(LHS
.getValueType(), MVT::i8
);
2516 SDValue MxOp
= DAG
.getNode(MxOpcode
, DL
, VTs
, LHS
, RHS
);
2518 if (CondOpcode
== ISD::UMULO
)
2519 Cond
= MxOp
.getValue(2);
2521 Cond
= MxOp
.getValue(1);
2523 CC
= DAG
.getConstant(MxCond
, DL
, MVT::i8
);
2527 if (Cond
.hasOneUse() && isAndOrOfSetCCs(Cond
, CondOpc
)) {
2528 SDValue Cmp
= Cond
.getOperand(0).getOperand(1);
2529 if (CondOpc
== ISD::OR
) {
2530 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
2531 // two branches instead of an explicit OR instruction with a
2533 if (Cmp
== Cond
.getOperand(1).getOperand(1) && isM68kLogicalCmp(Cmp
)) {
2534 CC
= Cond
.getOperand(0).getOperand(0);
2535 Chain
= DAG
.getNode(M68kISD::BRCOND
, DL
, Op
.getValueType(), Chain
,
2537 CC
= Cond
.getOperand(1).getOperand(0);
2541 } else { // ISD::AND
2542 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
2543 // two branches instead of an explicit AND instruction with a
2544 // separate test. However, we only do this if this block doesn't
2545 // have a fall-through edge, because this requires an explicit
2546 // jmp when the condition is false.
2547 if (Cmp
== Cond
.getOperand(1).getOperand(1) && isM68kLogicalCmp(Cmp
) &&
2548 Op
.getNode()->hasOneUse()) {
2549 M68k::CondCode CCode
=
2550 (M68k::CondCode
)Cond
.getOperand(0).getConstantOperandVal(0);
2551 CCode
= M68k::GetOppositeBranchCondition(CCode
);
2552 CC
= DAG
.getConstant(CCode
, DL
, MVT::i8
);
2553 SDNode
*User
= *Op
.getNode()->use_begin();
2554 // Look for an unconditional branch following this conditional branch.
2555 // We need this because we need to reverse the successors in order
2556 // to implement FCMP_OEQ.
2557 if (User
->getOpcode() == ISD::BR
) {
2558 SDValue FalseBB
= User
->getOperand(1);
2560 DAG
.UpdateNodeOperands(User
, User
->getOperand(0), Dest
);
2561 assert(NewBR
== User
);
2565 Chain
= DAG
.getNode(M68kISD::BRCOND
, DL
, Op
.getValueType(), Chain
,
2567 M68k::CondCode CCode
=
2568 (M68k::CondCode
)Cond
.getOperand(1).getConstantOperandVal(0);
2569 CCode
= M68k::GetOppositeBranchCondition(CCode
);
2570 CC
= DAG
.getConstant(CCode
, DL
, MVT::i8
);
2576 } else if (Cond
.hasOneUse() && isXor1OfSetCC(Cond
)) {
2577 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
2578 // It should be transformed during dag combiner except when the condition
2579 // is set by a arithmetics with overflow node.
2580 M68k::CondCode CCode
=
2581 (M68k::CondCode
)Cond
.getOperand(0).getConstantOperandVal(0);
2582 CCode
= M68k::GetOppositeBranchCondition(CCode
);
2583 CC
= DAG
.getConstant(CCode
, DL
, MVT::i8
);
2584 Cond
= Cond
.getOperand(0).getOperand(1);
2590 // Look pass the truncate if the high bits are known zero.
2591 if (isTruncWithZeroHighBitsInput(Cond
, DAG
))
2592 Cond
= Cond
.getOperand(0);
2594 // We know the result is compared against zero. Try to match it to BT.
2595 if (Cond
.hasOneUse()) {
2596 if (SDValue NewSetCC
= LowerToBTST(Cond
, ISD::SETNE
, DL
, DAG
)) {
2597 CC
= NewSetCC
.getOperand(0);
2598 Cond
= NewSetCC
.getOperand(1);
2605 M68k::CondCode MxCond
= Inverted
? M68k::COND_EQ
: M68k::COND_NE
;
2606 CC
= DAG
.getConstant(MxCond
, DL
, MVT::i8
);
2607 Cond
= EmitTest(Cond
, MxCond
, DL
, DAG
);
2609 return DAG
.getNode(M68kISD::BRCOND
, DL
, Op
.getValueType(), Chain
, Dest
, CC
,
2613 SDValue
M68kTargetLowering::LowerADDC_ADDE_SUBC_SUBE(SDValue Op
,
2614 SelectionDAG
&DAG
) const {
2615 MVT VT
= Op
.getNode()->getSimpleValueType(0);
2617 // Let legalize expand this if it isn't a legal type yet.
2618 if (!DAG
.getTargetLoweringInfo().isTypeLegal(VT
))
2621 SDVTList VTs
= DAG
.getVTList(VT
, MVT::i8
);
2624 bool ExtraOp
= false;
2625 switch (Op
.getOpcode()) {
2627 llvm_unreachable("Invalid code");
2632 Opc
= M68kISD::ADDX
;
2639 Opc
= M68kISD::SUBX
;
2645 return DAG
.getNode(Opc
, SDLoc(Op
), VTs
, Op
.getOperand(0), Op
.getOperand(1));
2646 return DAG
.getNode(Opc
, SDLoc(Op
), VTs
, Op
.getOperand(0), Op
.getOperand(1),
2650 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2651 // their target countpart wrapped in the M68kISD::Wrapper node. Suppose N is
2652 // one of the above mentioned nodes. It has to be wrapped because otherwise
2653 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2654 // be used to form addressing mode. These wrapped nodes will be selected
2656 SDValue
M68kTargetLowering::LowerConstantPool(SDValue Op
,
2657 SelectionDAG
&DAG
) const {
2658 ConstantPoolSDNode
*CP
= cast
<ConstantPoolSDNode
>(Op
);
2660 // In PIC mode (unless we're in PCRel PIC mode) we add an offset to the
2662 unsigned char OpFlag
= Subtarget
.classifyLocalReference(nullptr);
2664 unsigned WrapperKind
= M68kISD::Wrapper
;
2665 if (M68kII::isPCRelGlobalReference(OpFlag
)) {
2666 WrapperKind
= M68kISD::WrapperPC
;
2669 MVT PtrVT
= getPointerTy(DAG
.getDataLayout());
2670 SDValue Result
= DAG
.getTargetConstantPool(
2671 CP
->getConstVal(), PtrVT
, CP
->getAlign(), CP
->getOffset(), OpFlag
);
2674 Result
= DAG
.getNode(WrapperKind
, DL
, PtrVT
, Result
);
2676 // With PIC, the address is actually $g + Offset.
2677 if (M68kII::isGlobalRelativeToPICBase(OpFlag
)) {
2678 Result
= DAG
.getNode(ISD::ADD
, DL
, PtrVT
,
2679 DAG
.getNode(M68kISD::GLOBAL_BASE_REG
, SDLoc(), PtrVT
),
2686 SDValue
M68kTargetLowering::LowerExternalSymbol(SDValue Op
,
2687 SelectionDAG
&DAG
) const {
2688 const char *Sym
= cast
<ExternalSymbolSDNode
>(Op
)->getSymbol();
2690 // In PIC mode (unless we're in PCRel PIC mode) we add an offset to the
2692 const Module
*Mod
= DAG
.getMachineFunction().getFunction().getParent();
2693 unsigned char OpFlag
= Subtarget
.classifyExternalReference(*Mod
);
2695 unsigned WrapperKind
= M68kISD::Wrapper
;
2696 if (M68kII::isPCRelGlobalReference(OpFlag
)) {
2697 WrapperKind
= M68kISD::WrapperPC
;
2700 auto PtrVT
= getPointerTy(DAG
.getDataLayout());
2701 SDValue Result
= DAG
.getTargetExternalSymbol(Sym
, PtrVT
, OpFlag
);
2704 Result
= DAG
.getNode(WrapperKind
, DL
, PtrVT
, Result
);
2706 // With PIC, the address is actually $g + Offset.
2707 if (M68kII::isGlobalRelativeToPICBase(OpFlag
)) {
2708 Result
= DAG
.getNode(ISD::ADD
, DL
, PtrVT
,
2709 DAG
.getNode(M68kISD::GLOBAL_BASE_REG
, SDLoc(), PtrVT
),
2713 // For symbols that require a load from a stub to get the address, emit the
2715 if (M68kII::isGlobalStubReference(OpFlag
)) {
2716 Result
= DAG
.getLoad(PtrVT
, DL
, DAG
.getEntryNode(), Result
,
2717 MachinePointerInfo::getGOT(DAG
.getMachineFunction()));
2723 SDValue
M68kTargetLowering::LowerBlockAddress(SDValue Op
,
2724 SelectionDAG
&DAG
) const {
2725 unsigned char OpFlags
= Subtarget
.classifyBlockAddressReference();
2726 const BlockAddress
*BA
= cast
<BlockAddressSDNode
>(Op
)->getBlockAddress();
2727 int64_t Offset
= cast
<BlockAddressSDNode
>(Op
)->getOffset();
2729 auto PtrVT
= getPointerTy(DAG
.getDataLayout());
2731 // Create the TargetBlockAddressAddress node.
2732 SDValue Result
= DAG
.getTargetBlockAddress(BA
, PtrVT
, Offset
, OpFlags
);
2734 if (M68kII::isPCRelBlockReference(OpFlags
)) {
2735 Result
= DAG
.getNode(M68kISD::WrapperPC
, DL
, PtrVT
, Result
);
2737 Result
= DAG
.getNode(M68kISD::Wrapper
, DL
, PtrVT
, Result
);
2740 // With PIC, the address is actually $g + Offset.
2741 if (M68kII::isGlobalRelativeToPICBase(OpFlags
)) {
2743 DAG
.getNode(ISD::ADD
, DL
, PtrVT
,
2744 DAG
.getNode(M68kISD::GLOBAL_BASE_REG
, DL
, PtrVT
), Result
);
2750 SDValue
M68kTargetLowering::LowerGlobalAddress(const GlobalValue
*GV
,
2751 const SDLoc
&DL
, int64_t Offset
,
2752 SelectionDAG
&DAG
) const {
2753 unsigned char OpFlags
= Subtarget
.classifyGlobalReference(GV
);
2754 auto PtrVT
= getPointerTy(DAG
.getDataLayout());
2756 // Create the TargetGlobalAddress node, folding in the constant
2757 // offset if it is legal.
2759 if (M68kII::isDirectGlobalReference(OpFlags
)) {
2760 Result
= DAG
.getTargetGlobalAddress(GV
, DL
, PtrVT
, Offset
);
2763 Result
= DAG
.getTargetGlobalAddress(GV
, DL
, PtrVT
, 0, OpFlags
);
2766 if (M68kII::isPCRelGlobalReference(OpFlags
))
2767 Result
= DAG
.getNode(M68kISD::WrapperPC
, DL
, PtrVT
, Result
);
2769 Result
= DAG
.getNode(M68kISD::Wrapper
, DL
, PtrVT
, Result
);
2771 // With PIC, the address is actually $g + Offset.
2772 if (M68kII::isGlobalRelativeToPICBase(OpFlags
)) {
2774 DAG
.getNode(ISD::ADD
, DL
, PtrVT
,
2775 DAG
.getNode(M68kISD::GLOBAL_BASE_REG
, DL
, PtrVT
), Result
);
2778 // For globals that require a load from a stub to get the address, emit the
2780 if (M68kII::isGlobalStubReference(OpFlags
)) {
2781 Result
= DAG
.getLoad(PtrVT
, DL
, DAG
.getEntryNode(), Result
,
2782 MachinePointerInfo::getGOT(DAG
.getMachineFunction()));
2785 // If there was a non-zero offset that we didn't fold, create an explicit
2788 Result
= DAG
.getNode(ISD::ADD
, DL
, PtrVT
, Result
,
2789 DAG
.getConstant(Offset
, DL
, PtrVT
));
2795 SDValue
M68kTargetLowering::LowerGlobalAddress(SDValue Op
,
2796 SelectionDAG
&DAG
) const {
2797 const GlobalValue
*GV
= cast
<GlobalAddressSDNode
>(Op
)->getGlobal();
2798 int64_t Offset
= cast
<GlobalAddressSDNode
>(Op
)->getOffset();
2799 return LowerGlobalAddress(GV
, SDLoc(Op
), Offset
, DAG
);
2802 //===----------------------------------------------------------------------===//
2803 // Custom Lower Jump Table
2804 //===----------------------------------------------------------------------===//
2806 SDValue
M68kTargetLowering::LowerJumpTable(SDValue Op
,
2807 SelectionDAG
&DAG
) const {
2808 JumpTableSDNode
*JT
= cast
<JumpTableSDNode
>(Op
);
2810 // In PIC mode (unless we're in PCRel PIC mode) we add an offset to the
2812 unsigned char OpFlag
= Subtarget
.classifyLocalReference(nullptr);
2814 unsigned WrapperKind
= M68kISD::Wrapper
;
2815 if (M68kII::isPCRelGlobalReference(OpFlag
)) {
2816 WrapperKind
= M68kISD::WrapperPC
;
2819 auto PtrVT
= getPointerTy(DAG
.getDataLayout());
2820 SDValue Result
= DAG
.getTargetJumpTable(JT
->getIndex(), PtrVT
, OpFlag
);
2822 Result
= DAG
.getNode(WrapperKind
, DL
, PtrVT
, Result
);
2824 // With PIC, the address is actually $g + Offset.
2825 if (M68kII::isGlobalRelativeToPICBase(OpFlag
)) {
2826 Result
= DAG
.getNode(ISD::ADD
, DL
, PtrVT
,
2827 DAG
.getNode(M68kISD::GLOBAL_BASE_REG
, SDLoc(), PtrVT
),
2834 unsigned M68kTargetLowering::getJumpTableEncoding() const {
2835 return Subtarget
.getJumpTableEncoding();
2838 const MCExpr
*M68kTargetLowering::LowerCustomJumpTableEntry(
2839 const MachineJumpTableInfo
*MJTI
, const MachineBasicBlock
*MBB
,
2840 unsigned uid
, MCContext
&Ctx
) const {
2841 return MCSymbolRefExpr::create(MBB
->getSymbol(), MCSymbolRefExpr::VK_GOTOFF
,
2845 SDValue
M68kTargetLowering::getPICJumpTableRelocBase(SDValue Table
,
2846 SelectionDAG
&DAG
) const {
2847 if (getJumpTableEncoding() == MachineJumpTableInfo::EK_Custom32
)
2848 return DAG
.getNode(M68kISD::GLOBAL_BASE_REG
, SDLoc(),
2849 getPointerTy(DAG
.getDataLayout()));
2851 // MachineJumpTableInfo::EK_LabelDifference32 entry
2855 // NOTE This only used for MachineJumpTableInfo::EK_LabelDifference32 entries
2856 const MCExpr
*M68kTargetLowering::getPICJumpTableRelocBaseExpr(
2857 const MachineFunction
*MF
, unsigned JTI
, MCContext
&Ctx
) const {
2858 return MCSymbolRefExpr::create(MF
->getJTISymbol(JTI
, Ctx
), Ctx
);
2861 M68kTargetLowering::ConstraintType
2862 M68kTargetLowering::getConstraintType(StringRef Constraint
) const {
2863 if (Constraint
.size() > 0) {
2864 switch (Constraint
[0]) {
2867 return C_RegisterClass
;
2878 if (Constraint
.size() == 2)
2879 switch (Constraint
[1]) {
2896 return TargetLowering::getConstraintType(Constraint
);
2899 void M68kTargetLowering::LowerAsmOperandForConstraint(SDValue Op
,
2900 StringRef Constraint
,
2901 std::vector
<SDValue
> &Ops
,
2902 SelectionDAG
&DAG
) const {
2905 if (Constraint
.size() == 1) {
2906 // Constant constraints
2907 switch (Constraint
[0]) {
2916 auto *C
= dyn_cast
<ConstantSDNode
>(Op
);
2920 int64_t Val
= C
->getSExtValue();
2921 switch (Constraint
[0]) {
2922 case 'I': // constant integer in the range [1,8]
2923 if (Val
> 0 && Val
<= 8)
2926 case 'J': // constant signed 16-bit integer
2930 case 'K': // constant that is NOT in the range of [-0x80, 0x80)
2931 if (Val
< -0x80 || Val
>= 0x80)
2934 case 'L': // constant integer in the range [-8,-1]
2935 if (Val
< 0 && Val
>= -8)
2938 case 'M': // constant that is NOT in the range of [-0x100, 0x100]
2939 if (Val
< -0x100 || Val
>= 0x100)
2942 case 'N': // constant integer in the range [24,31]
2943 if (Val
>= 24 && Val
<= 31)
2946 case 'O': // constant integer 16
2950 case 'P': // constant integer in the range [8,15]
2951 if (Val
>= 8 && Val
<= 15)
2955 llvm_unreachable("Unhandled constant constraint");
2958 Result
= DAG
.getTargetConstant(Val
, SDLoc(Op
), Op
.getValueType());
2966 if (Constraint
.size() == 2) {
2967 switch (Constraint
[0]) {
2969 // Constant constraints start with 'C'
2970 switch (Constraint
[1]) {
2974 auto *C
= dyn_cast
<ConstantSDNode
>(Op
);
2978 int64_t Val
= C
->getSExtValue();
2979 switch (Constraint
[1]) {
2980 case '0': // constant integer 0
2984 case 'i': // constant integer
2986 case 'j': // integer constant that doesn't fit in 16 bits
2987 if (!isInt
<16>(C
->getSExtValue()))
2991 llvm_unreachable("Unhandled constant constraint");
2994 Result
= DAG
.getTargetConstant(Val
, SDLoc(Op
), Op
.getValueType());
3006 if (Result
.getNode()) {
3007 Ops
.push_back(Result
);
3011 TargetLowering::LowerAsmOperandForConstraint(Op
, Constraint
, Ops
, DAG
);
3014 std::pair
<unsigned, const TargetRegisterClass
*>
3015 M68kTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo
*TRI
,
3016 StringRef Constraint
,
3018 if (Constraint
.size() == 1) {
3019 switch (Constraint
[0]) {
3022 switch (VT
.SimpleTy
) {
3024 return std::make_pair(0U, &M68k::DR8RegClass
);
3026 return std::make_pair(0U, &M68k::DR16RegClass
);
3028 return std::make_pair(0U, &M68k::DR32RegClass
);
3034 switch (VT
.SimpleTy
) {
3036 return std::make_pair(0U, &M68k::AR16RegClass
);
3038 return std::make_pair(0U, &M68k::AR32RegClass
);
3048 return TargetLowering::getRegForInlineAsmConstraint(TRI
, Constraint
, VT
);
3051 /// Determines whether the callee is required to pop its own arguments.
3052 /// Callee pop is necessary to support tail calls.
3053 bool M68k::isCalleePop(CallingConv::ID CC
, bool IsVarArg
, bool GuaranteeTCO
) {
3054 return CC
== CallingConv::M68k_RTD
&& !IsVarArg
;
3057 // Return true if it is OK for this CMOV pseudo-opcode to be cascaded
3058 // together with other CMOV pseudo-opcodes into a single basic-block with
3059 // conditional jump around it.
3060 static bool isCMOVPseudo(MachineInstr
&MI
) {
3061 switch (MI
.getOpcode()) {
3072 // The CCR operand of SelectItr might be missing a kill marker
3073 // because there were multiple uses of CCR, and ISel didn't know
3074 // which to mark. Figure out whether SelectItr should have had a
3075 // kill marker, and set it if it should. Returns the correct kill
3077 static bool checkAndUpdateCCRKill(MachineBasicBlock::iterator SelectItr
,
3078 MachineBasicBlock
*BB
,
3079 const TargetRegisterInfo
*TRI
) {
3080 // Scan forward through BB for a use/def of CCR.
3081 MachineBasicBlock::iterator
miI(std::next(SelectItr
));
3082 for (MachineBasicBlock::iterator miE
= BB
->end(); miI
!= miE
; ++miI
) {
3083 const MachineInstr
&mi
= *miI
;
3084 if (mi
.readsRegister(M68k::CCR
))
3086 if (mi
.definesRegister(M68k::CCR
))
3087 break; // Should have kill-flag - update below.
3090 // If we hit the end of the block, check whether CCR is live into a
3092 if (miI
== BB
->end())
3093 for (const auto *SBB
: BB
->successors())
3094 if (SBB
->isLiveIn(M68k::CCR
))
3097 // We found a def, or hit the end of the basic block and CCR wasn't live
3098 // out. SelectMI should have a kill flag on CCR.
3099 SelectItr
->addRegisterKilled(M68k::CCR
, TRI
);
3104 M68kTargetLowering::EmitLoweredSelect(MachineInstr
&MI
,
3105 MachineBasicBlock
*MBB
) const {
3106 const TargetInstrInfo
*TII
= Subtarget
.getInstrInfo();
3107 DebugLoc DL
= MI
.getDebugLoc();
3109 // To "insert" a SELECT_CC instruction, we actually have to insert the
3110 // diamond control-flow pattern. The incoming instruction knows the
3111 // destination vreg to set, the condition code register to branch on, the
3112 // true/false values to select between, and a branch opcode to use.
3113 const BasicBlock
*BB
= MBB
->getBasicBlock();
3114 MachineFunction::iterator It
= ++MBB
->getIterator();
3121 // fallthrough --> Copy0MBB
3122 MachineBasicBlock
*ThisMBB
= MBB
;
3123 MachineFunction
*F
= MBB
->getParent();
3125 // This code lowers all pseudo-CMOV instructions. Generally it lowers these
3126 // as described above, by inserting a MBB, and then making a PHI at the join
3127 // point to select the true and false operands of the CMOV in the PHI.
3129 // The code also handles two different cases of multiple CMOV opcodes
3133 // In this case, there are multiple CMOVs in a row, all which are based on
3134 // the same condition setting (or the exact opposite condition setting).
3135 // In this case we can lower all the CMOVs using a single inserted MBB, and
3136 // then make a number of PHIs at the join point to model the CMOVs. The only
3137 // trickiness here, is that in a case like:
3139 // t2 = CMOV cond1 t1, f1
3140 // t3 = CMOV cond1 t2, f2
3142 // when rewriting this into PHIs, we have to perform some renaming on the
3143 // temps since you cannot have a PHI operand refer to a PHI result earlier
3144 // in the same block. The "simple" but wrong lowering would be:
3146 // t2 = PHI t1(BB1), f1(BB2)
3147 // t3 = PHI t2(BB1), f2(BB2)
3149 // but clearly t2 is not defined in BB1, so that is incorrect. The proper
3150 // renaming is to note that on the path through BB1, t2 is really just a
3151 // copy of t1, and do that renaming, properly generating:
3153 // t2 = PHI t1(BB1), f1(BB2)
3154 // t3 = PHI t1(BB1), f2(BB2)
3156 // Case 2, we lower cascaded CMOVs such as
3158 // (CMOV (CMOV F, T, cc1), T, cc2)
3160 // to two successives branches.
3161 MachineInstr
*CascadedCMOV
= nullptr;
3162 MachineInstr
*LastCMOV
= &MI
;
3163 M68k::CondCode CC
= M68k::CondCode(MI
.getOperand(3).getImm());
3164 M68k::CondCode OppCC
= M68k::GetOppositeBranchCondition(CC
);
3165 MachineBasicBlock::iterator NextMIIt
=
3166 std::next(MachineBasicBlock::iterator(MI
));
3168 // Check for case 1, where there are multiple CMOVs with the same condition
3169 // first. Of the two cases of multiple CMOV lowerings, case 1 reduces the
3170 // number of jumps the most.
3172 if (isCMOVPseudo(MI
)) {
3173 // See if we have a string of CMOVS with the same condition.
3174 while (NextMIIt
!= MBB
->end() && isCMOVPseudo(*NextMIIt
) &&
3175 (NextMIIt
->getOperand(3).getImm() == CC
||
3176 NextMIIt
->getOperand(3).getImm() == OppCC
)) {
3177 LastCMOV
= &*NextMIIt
;
3182 // This checks for case 2, but only do this if we didn't already find
3183 // case 1, as indicated by LastCMOV == MI.
3184 if (LastCMOV
== &MI
&& NextMIIt
!= MBB
->end() &&
3185 NextMIIt
->getOpcode() == MI
.getOpcode() &&
3186 NextMIIt
->getOperand(2).getReg() == MI
.getOperand(2).getReg() &&
3187 NextMIIt
->getOperand(1).getReg() == MI
.getOperand(0).getReg() &&
3188 NextMIIt
->getOperand(1).isKill()) {
3189 CascadedCMOV
= &*NextMIIt
;
3192 MachineBasicBlock
*Jcc1MBB
= nullptr;
3194 // If we have a cascaded CMOV, we lower it to two successive branches to
3195 // the same block. CCR is used by both, so mark it as live in the second.
3197 Jcc1MBB
= F
->CreateMachineBasicBlock(BB
);
3198 F
->insert(It
, Jcc1MBB
);
3199 Jcc1MBB
->addLiveIn(M68k::CCR
);
3202 MachineBasicBlock
*Copy0MBB
= F
->CreateMachineBasicBlock(BB
);
3203 MachineBasicBlock
*SinkMBB
= F
->CreateMachineBasicBlock(BB
);
3204 F
->insert(It
, Copy0MBB
);
3205 F
->insert(It
, SinkMBB
);
3207 // Set the call frame size on entry to the new basic blocks.
3208 unsigned CallFrameSize
= TII
->getCallFrameSizeAt(MI
);
3209 Copy0MBB
->setCallFrameSize(CallFrameSize
);
3210 SinkMBB
->setCallFrameSize(CallFrameSize
);
3212 // If the CCR register isn't dead in the terminator, then claim that it's
3213 // live into the sink and copy blocks.
3214 const TargetRegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
3216 MachineInstr
*LastCCRSUser
= CascadedCMOV
? CascadedCMOV
: LastCMOV
;
3217 if (!LastCCRSUser
->killsRegister(M68k::CCR
) &&
3218 !checkAndUpdateCCRKill(LastCCRSUser
, MBB
, TRI
)) {
3219 Copy0MBB
->addLiveIn(M68k::CCR
);
3220 SinkMBB
->addLiveIn(M68k::CCR
);
3223 // Transfer the remainder of MBB and its successor edges to SinkMBB.
3224 SinkMBB
->splice(SinkMBB
->begin(), MBB
,
3225 std::next(MachineBasicBlock::iterator(LastCMOV
)), MBB
->end());
3226 SinkMBB
->transferSuccessorsAndUpdatePHIs(MBB
);
3228 // Add the true and fallthrough blocks as its successors.
3230 // The fallthrough block may be Jcc1MBB, if we have a cascaded CMOV.
3231 MBB
->addSuccessor(Jcc1MBB
);
3233 // In that case, Jcc1MBB will itself fallthrough the Copy0MBB, and
3234 // jump to the SinkMBB.
3235 Jcc1MBB
->addSuccessor(Copy0MBB
);
3236 Jcc1MBB
->addSuccessor(SinkMBB
);
3238 MBB
->addSuccessor(Copy0MBB
);
3241 // The true block target of the first (or only) branch is always SinkMBB.
3242 MBB
->addSuccessor(SinkMBB
);
3244 // Create the conditional branch instruction.
3245 unsigned Opc
= M68k::GetCondBranchFromCond(CC
);
3246 BuildMI(MBB
, DL
, TII
->get(Opc
)).addMBB(SinkMBB
);
3249 unsigned Opc2
= M68k::GetCondBranchFromCond(
3250 (M68k::CondCode
)CascadedCMOV
->getOperand(3).getImm());
3251 BuildMI(Jcc1MBB
, DL
, TII
->get(Opc2
)).addMBB(SinkMBB
);
3255 // %FalseValue = ...
3256 // # fallthrough to SinkMBB
3257 Copy0MBB
->addSuccessor(SinkMBB
);
3260 // %Result = phi [ %FalseValue, Copy0MBB ], [ %TrueValue, ThisMBB ]
3262 MachineBasicBlock::iterator MIItBegin
= MachineBasicBlock::iterator(MI
);
3263 MachineBasicBlock::iterator MIItEnd
=
3264 std::next(MachineBasicBlock::iterator(LastCMOV
));
3265 MachineBasicBlock::iterator SinkInsertionPoint
= SinkMBB
->begin();
3266 DenseMap
<unsigned, std::pair
<unsigned, unsigned>> RegRewriteTable
;
3267 MachineInstrBuilder MIB
;
3269 // As we are creating the PHIs, we have to be careful if there is more than
3270 // one. Later CMOVs may reference the results of earlier CMOVs, but later
3271 // PHIs have to reference the individual true/false inputs from earlier PHIs.
3272 // That also means that PHI construction must work forward from earlier to
3273 // later, and that the code must maintain a mapping from earlier PHI's
3274 // destination registers, and the registers that went into the PHI.
3276 for (MachineBasicBlock::iterator MIIt
= MIItBegin
; MIIt
!= MIItEnd
; ++MIIt
) {
3277 Register DestReg
= MIIt
->getOperand(0).getReg();
3278 Register Op1Reg
= MIIt
->getOperand(1).getReg();
3279 Register Op2Reg
= MIIt
->getOperand(2).getReg();
3281 // If this CMOV we are generating is the opposite condition from
3282 // the jump we generated, then we have to swap the operands for the
3283 // PHI that is going to be generated.
3284 if (MIIt
->getOperand(3).getImm() == OppCC
)
3285 std::swap(Op1Reg
, Op2Reg
);
3287 if (RegRewriteTable
.find(Op1Reg
) != RegRewriteTable
.end())
3288 Op1Reg
= RegRewriteTable
[Op1Reg
].first
;
3290 if (RegRewriteTable
.find(Op2Reg
) != RegRewriteTable
.end())
3291 Op2Reg
= RegRewriteTable
[Op2Reg
].second
;
3294 BuildMI(*SinkMBB
, SinkInsertionPoint
, DL
, TII
->get(M68k::PHI
), DestReg
)
3300 // Add this PHI to the rewrite table.
3301 RegRewriteTable
[DestReg
] = std::make_pair(Op1Reg
, Op2Reg
);
3304 // If we have a cascaded CMOV, the second Jcc provides the same incoming
3305 // value as the first Jcc (the True operand of the SELECT_CC/CMOV nodes).
3307 MIB
.addReg(MI
.getOperand(2).getReg()).addMBB(Jcc1MBB
);
3308 // Copy the PHI result to the register defined by the second CMOV.
3309 BuildMI(*SinkMBB
, std::next(MachineBasicBlock::iterator(MIB
.getInstr())),
3310 DL
, TII
->get(TargetOpcode::COPY
),
3311 CascadedCMOV
->getOperand(0).getReg())
3312 .addReg(MI
.getOperand(0).getReg());
3313 CascadedCMOV
->eraseFromParent();
3316 // Now remove the CMOV(s).
3317 for (MachineBasicBlock::iterator MIIt
= MIItBegin
; MIIt
!= MIItEnd
;)
3318 (MIIt
++)->eraseFromParent();
3324 M68kTargetLowering::EmitLoweredSegAlloca(MachineInstr
&MI
,
3325 MachineBasicBlock
*BB
) const {
3326 llvm_unreachable("Cannot lower Segmented Stack Alloca with stack-split on");
3330 M68kTargetLowering::EmitInstrWithCustomInserter(MachineInstr
&MI
,
3331 MachineBasicBlock
*BB
) const {
3332 switch (MI
.getOpcode()) {
3334 llvm_unreachable("Unexpected instr type to insert");
3338 return EmitLoweredSelect(MI
, BB
);
3340 return EmitLoweredSegAlloca(MI
, BB
);
3344 SDValue
M68kTargetLowering::LowerVASTART(SDValue Op
, SelectionDAG
&DAG
) const {
3345 MachineFunction
&MF
= DAG
.getMachineFunction();
3346 auto PtrVT
= getPointerTy(MF
.getDataLayout());
3347 M68kMachineFunctionInfo
*FuncInfo
= MF
.getInfo
<M68kMachineFunctionInfo
>();
3349 const Value
*SV
= cast
<SrcValueSDNode
>(Op
.getOperand(2))->getValue();
3352 // vastart just stores the address of the VarArgsFrameIndex slot into the
3353 // memory location argument.
3354 SDValue FR
= DAG
.getFrameIndex(FuncInfo
->getVarArgsFrameIndex(), PtrVT
);
3355 return DAG
.getStore(Op
.getOperand(0), DL
, FR
, Op
.getOperand(1),
3356 MachinePointerInfo(SV
));
3359 SDValue
M68kTargetLowering::LowerATOMICFENCE(SDValue Op
,
3360 SelectionDAG
&DAG
) const {
3361 // Lower to a memory barrier created from inline asm.
3362 const TargetLowering
&TLI
= DAG
.getTargetLoweringInfo();
3363 LLVMContext
&Ctx
= *DAG
.getContext();
3365 const unsigned Flags
= InlineAsm::Extra_MayLoad
| InlineAsm::Extra_MayStore
|
3366 InlineAsm::Extra_HasSideEffects
;
3367 const SDValue AsmOperands
[4] = {
3368 Op
.getOperand(0), // Input chain
3369 DAG
.getTargetExternalSymbol(
3370 "", TLI
.getProgramPointerTy(
3371 DAG
.getDataLayout())), // Empty inline asm string
3372 DAG
.getMDNode(MDNode::get(Ctx
, {})), // (empty) srcloc
3373 DAG
.getTargetConstant(Flags
, SDLoc(Op
),
3374 TLI
.getPointerTy(DAG
.getDataLayout())), // Flags
3377 return DAG
.getNode(ISD::INLINEASM
, SDLoc(Op
),
3378 DAG
.getVTList(MVT::Other
, MVT::Glue
), AsmOperands
);
3381 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3382 // Calls to _alloca are needed to probe the stack when allocating more than 4k
3383 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
3384 // that the guard pages used by the OS virtual memory manager are allocated in
3385 // correct sequence.
3386 SDValue
M68kTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op
,
3387 SelectionDAG
&DAG
) const {
3388 MachineFunction
&MF
= DAG
.getMachineFunction();
3389 bool SplitStack
= MF
.shouldSplitStack();
3394 SDNode
*Node
= Op
.getNode();
3395 SDValue Chain
= Op
.getOperand(0);
3396 SDValue Size
= Op
.getOperand(1);
3397 unsigned Align
= cast
<ConstantSDNode
>(Op
.getOperand(2))->getZExtValue();
3398 EVT VT
= Node
->getValueType(0);
3400 // Chain the dynamic stack allocation so that it doesn't modify the stack
3401 // pointer when other instructions are using the stack.
3402 Chain
= DAG
.getCALLSEQ_START(Chain
, 0, 0, DL
);
3406 auto &MRI
= MF
.getRegInfo();
3407 auto SPTy
= getPointerTy(DAG
.getDataLayout());
3408 auto *ARClass
= getRegClassFor(SPTy
);
3409 Register Vreg
= MRI
.createVirtualRegister(ARClass
);
3410 Chain
= DAG
.getCopyToReg(Chain
, DL
, Vreg
, Size
);
3411 Result
= DAG
.getNode(M68kISD::SEG_ALLOCA
, DL
, SPTy
, Chain
,
3412 DAG
.getRegister(Vreg
, SPTy
));
3414 auto &TLI
= DAG
.getTargetLoweringInfo();
3415 Register SPReg
= TLI
.getStackPointerRegisterToSaveRestore();
3416 assert(SPReg
&& "Target cannot require DYNAMIC_STACKALLOC expansion and"
3417 " not tell us which reg is the stack pointer!");
3419 SDValue SP
= DAG
.getCopyFromReg(Chain
, DL
, SPReg
, VT
);
3420 Chain
= SP
.getValue(1);
3421 const TargetFrameLowering
&TFI
= *Subtarget
.getFrameLowering();
3422 unsigned StackAlign
= TFI
.getStackAlignment();
3423 Result
= DAG
.getNode(ISD::SUB
, DL
, VT
, SP
, Size
); // Value
3424 if (Align
> StackAlign
)
3425 Result
= DAG
.getNode(ISD::AND
, DL
, VT
, Result
,
3426 DAG
.getConstant(-(uint64_t)Align
, DL
, VT
));
3427 Chain
= DAG
.getCopyToReg(Chain
, DL
, SPReg
, Result
); // Output chain
3430 Chain
= DAG
.getCALLSEQ_END(Chain
, 0, 0, SDValue(), DL
);
3432 SDValue Ops
[2] = {Result
, Chain
};
3433 return DAG
.getMergeValues(Ops
, DL
);
3436 SDValue
M68kTargetLowering::LowerShiftLeftParts(SDValue Op
,
3437 SelectionDAG
&DAG
) const {
3439 SDValue Lo
= Op
.getOperand(0);
3440 SDValue Hi
= Op
.getOperand(1);
3441 SDValue Shamt
= Op
.getOperand(2);
3442 EVT VT
= Lo
.getValueType();
3444 // if Shamt - register size < 0: // Shamt < register size
3446 // Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (register size - 1 ^ Shamt))
3449 // Hi = Lo << (Shamt - register size)
3451 SDValue Zero
= DAG
.getConstant(0, DL
, VT
);
3452 SDValue One
= DAG
.getConstant(1, DL
, VT
);
3453 SDValue MinusRegisterSize
= DAG
.getConstant(-32, DL
, VT
);
3454 SDValue RegisterSizeMinus1
= DAG
.getConstant(32 - 1, DL
, VT
);
3455 SDValue ShamtMinusRegisterSize
=
3456 DAG
.getNode(ISD::ADD
, DL
, VT
, Shamt
, MinusRegisterSize
);
3457 SDValue RegisterSizeMinus1Shamt
=
3458 DAG
.getNode(ISD::XOR
, DL
, VT
, RegisterSizeMinus1
, Shamt
);
3460 SDValue LoTrue
= DAG
.getNode(ISD::SHL
, DL
, VT
, Lo
, Shamt
);
3461 SDValue ShiftRight1Lo
= DAG
.getNode(ISD::SRL
, DL
, VT
, Lo
, One
);
3462 SDValue ShiftRightLo
=
3463 DAG
.getNode(ISD::SRL
, DL
, VT
, ShiftRight1Lo
, RegisterSizeMinus1Shamt
);
3464 SDValue ShiftLeftHi
= DAG
.getNode(ISD::SHL
, DL
, VT
, Hi
, Shamt
);
3465 SDValue HiTrue
= DAG
.getNode(ISD::OR
, DL
, VT
, ShiftLeftHi
, ShiftRightLo
);
3466 SDValue HiFalse
= DAG
.getNode(ISD::SHL
, DL
, VT
, Lo
, ShamtMinusRegisterSize
);
3469 DAG
.getSetCC(DL
, MVT::i8
, ShamtMinusRegisterSize
, Zero
, ISD::SETLT
);
3471 Lo
= DAG
.getNode(ISD::SELECT
, DL
, VT
, CC
, LoTrue
, Zero
);
3472 Hi
= DAG
.getNode(ISD::SELECT
, DL
, VT
, CC
, HiTrue
, HiFalse
);
3474 return DAG
.getMergeValues({Lo
, Hi
}, DL
);
3477 SDValue
M68kTargetLowering::LowerShiftRightParts(SDValue Op
, SelectionDAG
&DAG
,
3480 SDValue Lo
= Op
.getOperand(0);
3481 SDValue Hi
= Op
.getOperand(1);
3482 SDValue Shamt
= Op
.getOperand(2);
3483 EVT VT
= Lo
.getValueType();
3486 // if Shamt - register size < 0: // Shamt < register size
3487 // Lo = (Lo >>u Shamt) | ((Hi << 1) << (register size - 1 ^ Shamt))
3488 // Hi = Hi >>s Shamt
3490 // Lo = Hi >>s (Shamt - register size);
3491 // Hi = Hi >>s (register size - 1)
3494 // if Shamt - register size < 0: // Shamt < register size
3495 // Lo = (Lo >>u Shamt) | ((Hi << 1) << (register size - 1 ^ Shamt))
3496 // Hi = Hi >>u Shamt
3498 // Lo = Hi >>u (Shamt - register size);
3501 unsigned ShiftRightOp
= IsSRA
? ISD::SRA
: ISD::SRL
;
3503 SDValue Zero
= DAG
.getConstant(0, DL
, VT
);
3504 SDValue One
= DAG
.getConstant(1, DL
, VT
);
3505 SDValue MinusRegisterSize
= DAG
.getConstant(-32, DL
, VT
);
3506 SDValue RegisterSizeMinus1
= DAG
.getConstant(32 - 1, DL
, VT
);
3507 SDValue ShamtMinusRegisterSize
=
3508 DAG
.getNode(ISD::ADD
, DL
, VT
, Shamt
, MinusRegisterSize
);
3509 SDValue RegisterSizeMinus1Shamt
=
3510 DAG
.getNode(ISD::XOR
, DL
, VT
, RegisterSizeMinus1
, Shamt
);
3512 SDValue ShiftRightLo
= DAG
.getNode(ISD::SRL
, DL
, VT
, Lo
, Shamt
);
3513 SDValue ShiftLeftHi1
= DAG
.getNode(ISD::SHL
, DL
, VT
, Hi
, One
);
3514 SDValue ShiftLeftHi
=
3515 DAG
.getNode(ISD::SHL
, DL
, VT
, ShiftLeftHi1
, RegisterSizeMinus1Shamt
);
3516 SDValue LoTrue
= DAG
.getNode(ISD::OR
, DL
, VT
, ShiftRightLo
, ShiftLeftHi
);
3517 SDValue HiTrue
= DAG
.getNode(ShiftRightOp
, DL
, VT
, Hi
, Shamt
);
3519 DAG
.getNode(ShiftRightOp
, DL
, VT
, Hi
, ShamtMinusRegisterSize
);
3521 IsSRA
? DAG
.getNode(ISD::SRA
, DL
, VT
, Hi
, RegisterSizeMinus1
) : Zero
;
3524 DAG
.getSetCC(DL
, MVT::i8
, ShamtMinusRegisterSize
, Zero
, ISD::SETLT
);
3526 Lo
= DAG
.getNode(ISD::SELECT
, DL
, VT
, CC
, LoTrue
, LoFalse
);
3527 Hi
= DAG
.getNode(ISD::SELECT
, DL
, VT
, CC
, HiTrue
, HiFalse
);
3529 return DAG
.getMergeValues({Lo
, Hi
}, DL
);
3532 //===----------------------------------------------------------------------===//
3534 //===----------------------------------------------------------------------===//
3536 static SDValue
getSETCC(M68k::CondCode Cond
, SDValue CCR
, const SDLoc
&dl
,
3537 SelectionDAG
&DAG
) {
3538 return DAG
.getNode(M68kISD::SETCC
, dl
, MVT::i8
,
3539 DAG
.getConstant(Cond
, dl
, MVT::i8
), CCR
);
3541 // When legalizing carry, we create carries via add X, -1
3542 // If that comes from an actual carry, via setcc, we use the
3544 static SDValue
combineCarryThroughADD(SDValue CCR
) {
3545 if (CCR
.getOpcode() == M68kISD::ADD
) {
3546 if (isAllOnesConstant(CCR
.getOperand(1))) {
3547 SDValue Carry
= CCR
.getOperand(0);
3548 while (Carry
.getOpcode() == ISD::TRUNCATE
||
3549 Carry
.getOpcode() == ISD::ZERO_EXTEND
||
3550 Carry
.getOpcode() == ISD::SIGN_EXTEND
||
3551 Carry
.getOpcode() == ISD::ANY_EXTEND
||
3552 (Carry
.getOpcode() == ISD::AND
&&
3553 isOneConstant(Carry
.getOperand(1))))
3554 Carry
= Carry
.getOperand(0);
3555 if (Carry
.getOpcode() == M68kISD::SETCC
||
3556 Carry
.getOpcode() == M68kISD::SETCC_CARRY
) {
3557 if (Carry
.getConstantOperandVal(0) == M68k::COND_CS
)
3558 return Carry
.getOperand(1);
3566 /// Optimize a CCR definition used according to the condition code \p CC into
3567 /// a simpler CCR value, potentially returning a new \p CC and replacing uses
3568 /// of chain values.
3569 static SDValue
combineSetCCCCR(SDValue CCR
, M68k::CondCode
&CC
,
3571 const M68kSubtarget
&Subtarget
) {
3572 if (CC
== M68k::COND_CS
)
3573 if (SDValue Flags
= combineCarryThroughADD(CCR
))
3579 // Optimize RES = M68kISD::SETCC CONDCODE, CCR_INPUT
3580 static SDValue
combineM68kSetCC(SDNode
*N
, SelectionDAG
&DAG
,
3581 const M68kSubtarget
&Subtarget
) {
3583 M68k::CondCode CC
= M68k::CondCode(N
->getConstantOperandVal(0));
3584 SDValue CCR
= N
->getOperand(1);
3586 // Try to simplify the CCR and condition code operands.
3587 if (SDValue Flags
= combineSetCCCCR(CCR
, CC
, DAG
, Subtarget
))
3588 return getSETCC(CC
, Flags
, DL
, DAG
);
3592 static SDValue
combineM68kBrCond(SDNode
*N
, SelectionDAG
&DAG
,
3593 const M68kSubtarget
&Subtarget
) {
3595 M68k::CondCode CC
= M68k::CondCode(N
->getConstantOperandVal(2));
3596 SDValue CCR
= N
->getOperand(3);
3598 // Try to simplify the CCR and condition code operands.
3599 // Make sure to not keep references to operands, as combineSetCCCCR can
3600 // RAUW them under us.
3601 if (SDValue Flags
= combineSetCCCCR(CCR
, CC
, DAG
, Subtarget
)) {
3602 SDValue Cond
= DAG
.getConstant(CC
, DL
, MVT::i8
);
3603 return DAG
.getNode(M68kISD::BRCOND
, DL
, N
->getVTList(), N
->getOperand(0),
3604 N
->getOperand(1), Cond
, Flags
);
3610 static SDValue
combineSUBX(SDNode
*N
, SelectionDAG
&DAG
) {
3611 if (SDValue Flags
= combineCarryThroughADD(N
->getOperand(2))) {
3612 MVT VT
= N
->getSimpleValueType(0);
3613 SDVTList VTs
= DAG
.getVTList(VT
, MVT::i32
);
3614 return DAG
.getNode(M68kISD::SUBX
, SDLoc(N
), VTs
, N
->getOperand(0),
3615 N
->getOperand(1), Flags
);
3621 // Optimize RES, CCR = M68kISD::ADDX LHS, RHS, CCR
3622 static SDValue
combineADDX(SDNode
*N
, SelectionDAG
&DAG
,
3623 TargetLowering::DAGCombinerInfo
&DCI
) {
3624 if (SDValue Flags
= combineCarryThroughADD(N
->getOperand(2))) {
3625 MVT VT
= N
->getSimpleValueType(0);
3626 SDVTList VTs
= DAG
.getVTList(VT
, MVT::i32
);
3627 return DAG
.getNode(M68kISD::ADDX
, SDLoc(N
), VTs
, N
->getOperand(0),
3628 N
->getOperand(1), Flags
);
3634 SDValue
M68kTargetLowering::PerformDAGCombine(SDNode
*N
,
3635 DAGCombinerInfo
&DCI
) const {
3636 SelectionDAG
&DAG
= DCI
.DAG
;
3637 switch (N
->getOpcode()) {
3639 return combineSUBX(N
, DAG
);
3641 return combineADDX(N
, DAG
, DCI
);
3642 case M68kISD::SETCC
:
3643 return combineM68kSetCC(N
, DAG
, Subtarget
);
3644 case M68kISD::BRCOND
:
3645 return combineM68kBrCond(N
, DAG
, Subtarget
);
3651 //===----------------------------------------------------------------------===//
3652 // M68kISD Node Names
3653 //===----------------------------------------------------------------------===//
3654 const char *M68kTargetLowering::getTargetNodeName(unsigned Opcode
) const {
3657 return "M68kISD::CALL";
3658 case M68kISD::TAIL_CALL
:
3659 return "M68kISD::TAIL_CALL";
3661 return "M68kISD::RET";
3662 case M68kISD::TC_RETURN
:
3663 return "M68kISD::TC_RETURN";
3665 return "M68kISD::ADD";
3667 return "M68kISD::SUB";
3669 return "M68kISD::ADDX";
3671 return "M68kISD::SUBX";
3673 return "M68kISD::SMUL";
3675 return "M68kISD::UMUL";
3677 return "M68kISD::OR";
3679 return "M68kISD::XOR";
3681 return "M68kISD::AND";
3683 return "M68kISD::CMP";
3685 return "M68kISD::BTST";
3686 case M68kISD::SELECT
:
3687 return "M68kISD::SELECT";
3689 return "M68kISD::CMOV";
3690 case M68kISD::BRCOND
:
3691 return "M68kISD::BRCOND";
3692 case M68kISD::SETCC
:
3693 return "M68kISD::SETCC";
3694 case M68kISD::SETCC_CARRY
:
3695 return "M68kISD::SETCC_CARRY";
3696 case M68kISD::GLOBAL_BASE_REG
:
3697 return "M68kISD::GLOBAL_BASE_REG";
3698 case M68kISD::Wrapper
:
3699 return "M68kISD::Wrapper";
3700 case M68kISD::WrapperPC
:
3701 return "M68kISD::WrapperPC";
3702 case M68kISD::SEG_ALLOCA
:
3703 return "M68kISD::SEG_ALLOCA";
3709 CCAssignFn
*M68kTargetLowering::getCCAssignFn(CallingConv::ID CC
, bool Return
,
3710 bool IsVarArg
) const {
3712 return RetCC_M68k_C
;