summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Target/M68k/M68kISelLowering.h
blob: 9375a99962eb6f4da58bb8ae3af586eda347a622 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//===-- M68kISelLowering.h - M68k DAG Lowering Interface --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines the interfaces that M68k uses to lower LLVM code into a
/// selection DAG.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_M68K_M68KISELLOWERING_H
#define LLVM_LIB_TARGET_M68K_M68KISELLOWERING_H

#include "M68k.h"

#include "llvm/CodeGen/CallingConvLower.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/Function.h"

#include <deque>

namespace llvm {
namespace M68kISD {

/// M68k Specific DAG nodes
enum NodeType {
  /// Start the numbering from where ISD NodeType finishes.
  FIRST_NUMBER = ISD::BUILTIN_OP_END,

  CALL,
  RET,
  TAIL_CALL,
  TC_RETURN,

  /// M68k compare and logical compare instructions. Subtracts the source
  /// operand from the destination data register and sets the condition
  /// codes according to the result. Immediate always goes first.
  CMP,

  /// M68k bit-test instructions.
  BTST,

  /// M68k Select
  SELECT,

  /// M68k SetCC. Operand 0 is condition code, and operand 1 is the CCR
  /// operand, usually produced by a CMP instruction.
  SETCC,

  // Same as SETCC except it's materialized with a subx and the value is all
  // one's or all zero's.
  SETCC_CARRY, // R = carry_bit ? ~0 : 0

  /// M68k conditional moves. Operand 0 and operand 1 are the two values
  /// to select from. Operand 2 is the condition code, and operand 3 is the
  /// flag operand produced by a CMP or TEST instruction. It also writes a
  /// flag result.
  CMOV,

  /// M68k conditional branches. Operand 0 is the chain operand, operand 1
  /// is the block to branch if condition is true, operand 2 is the
  /// condition code, and operand 3 is the flag operand produced by a CMP
  /// or TEST instruction.
  BRCOND,

  // Arithmetic operations with CCR results.
  ADD,
  SUB,
  ADDX,
  SUBX,
  SMUL,
  UMUL,
  OR,
  XOR,
  AND,

  // GlobalBaseReg,
  GLOBAL_BASE_REG,

  /// A wrapper node for TargetConstantPool,
  /// TargetExternalSymbol, and TargetGlobalAddress.
  Wrapper,

  /// Special wrapper used under M68k PIC mode for PC
  /// relative displacements.
  WrapperPC,

  // For allocating variable amounts of stack space when using
  // segmented stacks. Check if the current stacklet has enough space, and
  // falls back to heap allocation if not.
  SEG_ALLOCA,
};
} // namespace M68kISD

/// Define some predicates that are used for node matching.
namespace M68k {

/// Determines whether the callee is required to pop its
/// own arguments. Callee pop is necessary to support tail calls.
bool isCalleePop(CallingConv::ID CallingConv, bool IsVarArg, bool GuaranteeTCO);

} // end namespace M68k

//===--------------------------------------------------------------------===//
// TargetLowering Implementation
//===--------------------------------------------------------------------===//

class M68kMachineFunctionInfo;
class M68kSubtarget;

class M68kTargetLowering : public TargetLowering {
  const M68kSubtarget &Subtarget;
  const M68kTargetMachine &TM;

public:
  explicit M68kTargetLowering(const M68kTargetMachine &TM,
                              const M68kSubtarget &STI);

  static const M68kTargetLowering *create(const M68kTargetMachine &TM,
                                          const M68kSubtarget &STI);

  const char *getTargetNodeName(unsigned Opcode) const override;

  /// Return the value type to use for ISD::SETCC.
  EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
                         EVT VT) const override;

  /// EVT is not used in-tree, but is used by out-of-tree target.
  virtual MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override;

  /// Provide custom lowering hooks for some operations.
  SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;

  /// Return the entry encoding for a jump table in the current function.
  /// The returned value is a member of the  MachineJumpTableInfo::JTEntryKind
  /// enum.
  unsigned getJumpTableEncoding() const override;

  const MCExpr *LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
                                          const MachineBasicBlock *MBB,
                                          unsigned uid,
                                          MCContext &Ctx) const override;

  /// Returns relocation base for the given PIC jumptable.
  SDValue getPICJumpTableRelocBase(SDValue Table,
                                   SelectionDAG &DAG) const override;

  /// This returns the relocation base for the given PIC jumptable,
  /// the same as getPICJumpTableRelocBase, but as an MCExpr.
  const MCExpr *getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
                                             unsigned JTI,
                                             MCContext &Ctx) const override;

  ConstraintType getConstraintType(StringRef ConstraintStr) const override;

  std::pair<unsigned, const TargetRegisterClass *>
  getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
                               StringRef Constraint, MVT VT) const override;

  // Lower operand with C_Immediate and C_Other constraint type
  void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
                                    std::vector<SDValue> &Ops,
                                    SelectionDAG &DAG) const override;

  MachineBasicBlock *
  EmitInstrWithCustomInserter(MachineInstr &MI,
                              MachineBasicBlock *MBB) const override;

  CCAssignFn *getCCAssignFn(CallingConv::ID CC, bool Return,
                            bool IsVarArg) const;

private:
  unsigned GetAlignedArgumentStackSize(unsigned StackSize,
                                       SelectionDAG &DAG) const;

  SDValue getReturnAddressFrameIndex(SelectionDAG &DAG) const;

  /// Emit a load of return address if tail call
  /// optimization is performed and it is required.
  SDValue EmitTailCallLoadRetAddr(SelectionDAG &DAG, SDValue &OutRetAddr,
                                  SDValue Chain, bool IsTailCall, int FPDiff,
                                  const SDLoc &DL) const;

  /// Emit a store of the return address if tail call
  /// optimization is performed and it is required (FPDiff!=0).
  SDValue EmitTailCallStoreRetAddr(SelectionDAG &DAG, MachineFunction &MF,
                                   SDValue Chain, SDValue RetAddrFrIdx,
                                   EVT PtrVT, unsigned SlotSize, int FPDiff,
                                   const SDLoc &DL) const;

  SDValue LowerMemArgument(SDValue Chain, CallingConv::ID CallConv,
                           const SmallVectorImpl<ISD::InputArg> &ArgInfo,
                           const SDLoc &DL, SelectionDAG &DAG,
                           const CCValAssign &VA, MachineFrameInfo &MFI,
                           unsigned ArgIdx) const;

  SDValue LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, SDValue Arg,
                           const SDLoc &DL, SelectionDAG &DAG,
                           const CCValAssign &VA, ISD::ArgFlagsTy Flags) const;

  SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerToBTST(SDValue And, ISD::CondCode CC, const SDLoc &DL,
                      SelectionDAG &DAG) const;
  SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerSETCCCARRY(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerBRCOND(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerGlobalAddress(const GlobalValue *GV, const SDLoc &DL,
                             int64_t Offset, SelectionDAG &DAG) const;
  SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
  SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;

  SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
                          CallingConv::ID CallConv, bool IsVarArg,
                          const SmallVectorImpl<ISD::InputArg> &Ins,
                          const SDLoc &DL, SelectionDAG &DAG,
                          SmallVectorImpl<SDValue> &InVals) const;

  /// LowerFormalArguments - transform physical registers into virtual
  /// registers and generate load operations for arguments places on the stack.
  SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CCID,
                               bool IsVarArg,
                               const SmallVectorImpl<ISD::InputArg> &Ins,
                               const SDLoc &DL, SelectionDAG &DAG,
                               SmallVectorImpl<SDValue> &InVals) const override;

  SDValue LowerCall(CallLoweringInfo &CLI,
                    SmallVectorImpl<SDValue> &InVals) const override;

  /// Lower the result values of a call into the
  /// appropriate copies out of appropriate physical registers.
  SDValue LowerReturn(SDValue Chain, CallingConv::ID CCID, bool IsVarArg,
                      const SmallVectorImpl<ISD::OutputArg> &Outs,
                      const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
                      SelectionDAG &DAG) const override;

  bool decomposeMulByConstant(LLVMContext &Context, EVT VT,
                              SDValue C) const override;

  MachineBasicBlock *EmitLoweredSelect(MachineInstr &I,
                                       MachineBasicBlock *MBB) const;
  MachineBasicBlock *EmitLoweredSegAlloca(MachineInstr &MI,
                                          MachineBasicBlock *BB) const;

  /// Emit nodes that will be selected as "test Op0,Op0", or something
  /// equivalent, for use with the given M68k condition code.
  SDValue EmitTest(SDValue Op0, unsigned M68kCC, const SDLoc &dl,
                   SelectionDAG &DAG) const;

  /// Emit nodes that will be selected as "cmp Op0,Op1", or something
  /// equivalent, for use with the given M68k condition code.
  SDValue EmitCmp(SDValue Op0, SDValue Op1, unsigned M68kCC, const SDLoc &dl,
                  SelectionDAG &DAG) const;

  /// Check whether the call is eligible for tail call optimization. Targets
  /// that want to do tail call optimization should implement this function.
  bool IsEligibleForTailCallOptimization(
      SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg,
      bool IsCalleeStructRet, bool IsCallerStructRet, Type *RetTy,
      const SmallVectorImpl<ISD::OutputArg> &Outs,
      const SmallVectorImpl<SDValue> &OutVals,
      const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const;

  SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
};
} // namespace llvm

#endif // LLVM_LIB_TARGET_M68K_M68KISELLOWERING_H