ARM64: initial backend import
[oota-llvm.git] / lib / Target / ARM64 / ARM64ISelLowering.h
1 //==-- ARM64ISelLowering.h - ARM64 DAG Lowering Interface --------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interfaces that ARM64 uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_ARM64_ISELLOWERING_H
16 #define LLVM_TARGET_ARM64_ISELLOWERING_H
17
18 #include "llvm/CodeGen/CallingConvLower.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/Target/TargetLowering.h"
22
23 namespace llvm {
24
25 namespace ARM64ISD {
26
27 enum {
28   FIRST_NUMBER = ISD::BUILTIN_OP_END,
29   WrapperLarge, // 4-instruction MOVZ/MOVK sequence for 64-bit addresses.
30   CALL,         // Function call.
31
32   // Almost the same as a normal call node, except that a TLSDesc relocation is
33   // needed so the linker can relax it correctly if possible.
34   TLSDESC_CALL,
35   ADRP,     // Page address of a TargetGlobalAddress operand.
36   ADDlow,   // Add the low 12 bits of a TargetGlobalAddress operand.
37   LOADgot,  // Load from automatically generated descriptor (e.g. Global
38             // Offset Table, TLS record).
39   RET_FLAG, // Return with a flag operand. Operand 0 is the chain operand.
40   BRCOND,   // Conditional branch instruction; "b.cond".
41   CSEL,
42   FCSEL, // Conditional move instruction.
43   CSINV, // Conditional select invert.
44   CSNEG, // Conditional select negate.
45   CSINC, // Conditional select increment.
46
47   // Pointer to the thread's local storage area. Materialised from TPIDR_EL0 on
48   // ELF.
49   THREAD_POINTER,
50   ADC,
51   SBC, // adc, sbc instructions
52
53   // Arithmetic instructions which write flags.
54   ADDS,
55   SUBS,
56   ADCS,
57   SBCS,
58   ANDS,
59
60   // Floating point comparison
61   FCMP,
62
63   // Floating point max and min instructions.
64   FMAX,
65   FMIN,
66
67   // Scalar extract
68   EXTR,
69
70   // Scalar-to-vector duplication
71   DUP,
72   DUPLANE8,
73   DUPLANE16,
74   DUPLANE32,
75   DUPLANE64,
76
77   // Vector immedate moves
78   MOVI,
79   MOVIshift,
80   MOVIedit,
81   MOVImsl,
82   FMOV,
83   MVNIshift,
84   MVNImsl,
85
86   // Vector immediate ops
87   BICi,
88   ORRi,
89
90   // Vector arithmetic negation
91   NEG,
92
93   // Vector shuffles
94   ZIP1,
95   ZIP2,
96   UZP1,
97   UZP2,
98   TRN1,
99   TRN2,
100   REV16,
101   REV32,
102   REV64,
103   EXT,
104
105   // Vector shift by scalar
106   VSHL,
107   VLSHR,
108   VASHR,
109
110   // Vector shift by scalar (again)
111   SQSHL_I,
112   UQSHL_I,
113   SQSHLU_I,
114   SRSHR_I,
115   URSHR_I,
116
117   // Vector comparisons
118   CMEQ,
119   CMGE,
120   CMGT,
121   CMHI,
122   CMHS,
123   FCMEQ,
124   FCMGE,
125   FCMGT,
126
127   // Vector zero comparisons
128   CMEQz,
129   CMGEz,
130   CMGTz,
131   CMLEz,
132   CMLTz,
133   FCMEQz,
134   FCMGEz,
135   FCMGTz,
136   FCMLEz,
137   FCMLTz,
138
139   // Vector bitwise negation
140   NOT,
141
142   // Vector bitwise selection
143   BIT,
144
145   // Compare-and-branch
146   CBZ,
147   CBNZ,
148   TBZ,
149   TBNZ,
150
151   // Tail calls
152   TC_RETURN,
153
154   // Custom prefetch handling
155   PREFETCH,
156
157   // {s|u}int to FP within a FP register.
158   SITOF,
159   UITOF
160 };
161
162 } // end namespace ARM64ISD
163
164 class ARM64Subtarget;
165 class ARM64TargetMachine;
166
167 class ARM64TargetLowering : public TargetLowering {
168   bool RequireStrictAlign;
169
170 public:
171   explicit ARM64TargetLowering(ARM64TargetMachine &TM);
172
173   /// Selects the correct CCAssignFn for a the given CallingConvention
174   /// value.
175   CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg) const;
176
177   /// computeMaskedBitsForTargetNode - Determine which of the bits specified in
178   /// Mask are known to be either zero or one and return them in the
179   /// KnownZero/KnownOne bitsets.
180   void computeMaskedBitsForTargetNode(const SDValue Op, APInt &KnownZero,
181                                       APInt &KnownOne, const SelectionDAG &DAG,
182                                       unsigned Depth = 0) const;
183
184   virtual MVT getScalarShiftAmountTy(EVT LHSTy) const;
185
186   /// allowsUnalignedMemoryAccesses - Returns true if the target allows
187   /// unaligned memory accesses. of the specified type.
188   virtual bool allowsUnalignedMemoryAccesses(EVT VT, unsigned AddrSpace = 0,
189                                              bool *Fast = 0) const {
190     if (RequireStrictAlign)
191       return false;
192     // FIXME: True for Cyclone, but not necessary others.
193     if (Fast)
194       *Fast = true;
195     return true;
196   }
197
198   /// LowerOperation - Provide custom lowering hooks for some operations.
199   virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
200
201   virtual const char *getTargetNodeName(unsigned Opcode) const;
202
203   virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
204
205   /// getFunctionAlignment - Return the Log2 alignment of this function.
206   virtual unsigned getFunctionAlignment(const Function *F) const;
207
208   /// getMaximalGlobalOffset - Returns the maximal possible offset which can
209   /// be used for loads / stores from the global.
210   virtual unsigned getMaximalGlobalOffset() const;
211
212   /// Returns true if a cast between SrcAS and DestAS is a noop.
213   virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const {
214     // Addrspacecasts are always noops.
215     return true;
216   }
217
218   /// createFastISel - This method returns a target specific FastISel object,
219   /// or null if the target does not support "fast" ISel.
220   virtual FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
221                                    const TargetLibraryInfo *libInfo) const;
222
223   virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
224
225   virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const;
226
227   /// isShuffleMaskLegal - Return true if the given shuffle mask can be
228   /// codegen'd directly, or if it should be stack expanded.
229   virtual bool isShuffleMaskLegal(const SmallVectorImpl<int> &M, EVT VT) const;
230
231   /// getSetCCResultType - Return the ISD::SETCC ValueType
232   virtual EVT getSetCCResultType(LLVMContext &Context, EVT VT) const;
233
234   SDValue ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const;
235
236   MachineBasicBlock *EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
237                                       unsigned Size, unsigned BinOpcode) const;
238   MachineBasicBlock *EmitAtomicCmpSwap(MachineInstr *MI, MachineBasicBlock *BB,
239                                        unsigned Size) const;
240   MachineBasicBlock *EmitAtomicBinary128(MachineInstr *MI,
241                                          MachineBasicBlock *BB,
242                                          unsigned BinOpcodeLo,
243                                          unsigned BinOpcodeHi) const;
244   MachineBasicBlock *EmitAtomicCmpSwap128(MachineInstr *MI,
245                                           MachineBasicBlock *BB) const;
246   MachineBasicBlock *EmitAtomicMinMax128(MachineInstr *MI,
247                                          MachineBasicBlock *BB,
248                                          unsigned CondCode) const;
249   MachineBasicBlock *EmitF128CSEL(MachineInstr *MI,
250                                   MachineBasicBlock *BB) const;
251
252   virtual MachineBasicBlock *
253   EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const;
254
255   virtual bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
256                                   unsigned Intrinsic) const;
257
258   virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
259   virtual bool isTruncateFree(EVT VT1, EVT VT2) const;
260
261   virtual bool isZExtFree(Type *Ty1, Type *Ty2) const;
262   virtual bool isZExtFree(EVT VT1, EVT VT2) const;
263   virtual bool isZExtFree(SDValue Val, EVT VT2) const;
264
265   virtual bool hasPairedLoad(Type *LoadedType,
266                              unsigned &RequiredAligment) const;
267   virtual bool hasPairedLoad(EVT LoadedType, unsigned &RequiredAligment) const;
268
269   virtual bool isLegalAddImmediate(int64_t) const;
270   virtual bool isLegalICmpImmediate(int64_t) const;
271
272   virtual EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
273                                   unsigned SrcAlign, bool IsMemset,
274                                   bool ZeroMemset, bool MemcpyStrSrc,
275                                   MachineFunction &MF) const;
276
277   /// isLegalAddressingMode - Return true if the addressing mode represented
278   /// by AM is legal for this target, for a load/store of the specified type.
279   virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const;
280
281   /// \brief Return the cost of the scaling factor used in the addressing
282   /// mode represented by AM for this target, for a load/store
283   /// of the specified type.
284   /// If the AM is supported, the return value must be >= 0.
285   /// If the AM is not supported, it returns a negative value.
286   virtual int getScalingFactorCost(const AddrMode &AM, Type *Ty) const;
287
288   /// isFMAFasterThanFMulAndFAdd - Return true if an FMA operation is faster
289   /// than a pair of fmul and fadd instructions. fmuladd intrinsics will be
290   /// expanded to FMAs when this method returns true, otherwise fmuladd is
291   /// expanded to fmul + fadd.
292   virtual bool isFMAFasterThanFMulAndFAdd(EVT VT) const;
293
294   virtual const uint16_t *getScratchRegisters(CallingConv::ID CC) const;
295
296   virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
297                                                  Type *Ty) const;
298
299 private:
300   /// Subtarget - Keep a pointer to the ARM64Subtarget around so that we can
301   /// make the right decision when generating code for different targets.
302   const ARM64Subtarget *Subtarget;
303
304   void addTypeForNEON(EVT VT, EVT PromotedBitwiseVT);
305   void addDRTypeForNEON(MVT VT);
306   void addQRTypeForNEON(MVT VT);
307
308   virtual SDValue
309   LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
310                        const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL,
311                        SelectionDAG &DAG,
312                        SmallVectorImpl<SDValue> &InVals) const;
313
314   virtual SDValue LowerCall(CallLoweringInfo & /*CLI*/,
315                             SmallVectorImpl<SDValue> &InVals) const;
316
317   SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
318                           CallingConv::ID CallConv, bool isVarArg,
319                           const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL,
320                           SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
321                           bool isThisReturn, SDValue ThisVal) const;
322
323   bool isEligibleForTailCallOptimization(
324       SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
325       bool isCalleeStructRet, bool isCallerStructRet,
326       const SmallVectorImpl<ISD::OutputArg> &Outs,
327       const SmallVectorImpl<SDValue> &OutVals,
328       const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const;
329
330   void saveVarArgRegisters(CCState &CCInfo, SelectionDAG &DAG, SDLoc DL,
331                            SDValue &Chain) const;
332
333   virtual bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
334                               bool isVarArg,
335                               const SmallVectorImpl<ISD::OutputArg> &Outs,
336                               LLVMContext &Context) const;
337
338   virtual SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv,
339                               bool isVarArg,
340                               const SmallVectorImpl<ISD::OutputArg> &Outs,
341                               const SmallVectorImpl<SDValue> &OutVals, SDLoc DL,
342                               SelectionDAG &DAG) const;
343
344   SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
345   SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
346   SDValue LowerDarwinGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
347   SDValue LowerELFGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
348   SDValue LowerELFTLSDescCall(SDValue SymAddr, SDValue DescAddr, SDLoc DL,
349                               SelectionDAG &DAG) const;
350   SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
351   SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
352   SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
353   SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
354   SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
355   SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
356   SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
357   SDValue LowerAAPCS_VASTART(SDValue Op, SelectionDAG &DAG) const;
358   SDValue LowerDarwin_VASTART(SDValue Op, SelectionDAG &DAG) const;
359   SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
360   SDValue LowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
361   SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) const;
362   SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
363   SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
364   SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
365   SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
366   SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const;
367   SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
368   SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
369   SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
370   SDValue LowerVectorSRA_SRL_SHL(SDValue Op, SelectionDAG &DAG) const;
371   SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
372   SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) const;
373   SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) const;
374   SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) const;
375   SDValue LowerF128Call(SDValue Op, SelectionDAG &DAG,
376                         RTLIB::Libcall Call) const;
377   SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
378   SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const;
379   SDValue LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const;
380   SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const;
381   SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
382   SDValue LowerVectorAND(SDValue Op, SelectionDAG &DAG) const;
383   SDValue LowerVectorOR(SDValue Op, SelectionDAG &DAG) const;
384   SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
385   SDValue LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const;
386
387   ConstraintType getConstraintType(const std::string &Constraint) const;
388
389   /// Examine constraint string and operand type and determine a weight value.
390   /// The operand object must already have been set up with the operand type.
391   ConstraintWeight getSingleConstraintMatchWeight(AsmOperandInfo &info,
392                                                   const char *constraint) const;
393
394   std::pair<unsigned, const TargetRegisterClass *>
395   getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const;
396   void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
397                                     std::vector<SDValue> &Ops,
398                                     SelectionDAG &DAG) const;
399
400   bool isUsedByReturnOnly(SDNode *N, SDValue &Chain) const;
401   bool mayBeEmittedAsTailCall(CallInst *CI) const;
402   bool getIndexedAddressParts(SDNode *Op, SDValue &Base, SDValue &Offset,
403                               ISD::MemIndexedMode &AM, bool &IsInc,
404                               SelectionDAG &DAG) const;
405   bool getPreIndexedAddressParts(SDNode *N, SDValue &Base, SDValue &Offset,
406                                  ISD::MemIndexedMode &AM,
407                                  SelectionDAG &DAG) const;
408   bool getPostIndexedAddressParts(SDNode *N, SDNode *Op, SDValue &Base,
409                                   SDValue &Offset, ISD::MemIndexedMode &AM,
410                                   SelectionDAG &DAG) const;
411
412   void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
413                           SelectionDAG &DAG) const;
414 };
415
416 namespace ARM64 {
417 FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
418                          const TargetLibraryInfo *libInfo);
419 } // end namespace ARM64
420
421 } // end namespace llvm
422
423 #endif // LLVM_TARGET_ARM64_ISELLOWERING_H