Mutate TargetLowering::shouldExpandAtomicRMWInIR to specifically dictate how AtomicRM...
[oota-llvm.git] / lib / Target / AArch64 / AArch64ISelLowering.h
1 //==-- AArch64ISelLowering.h - AArch64 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 AArch64 uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64ISELLOWERING_H
16 #define LLVM_LIB_TARGET_AARCH64_AARCH64ISELLOWERING_H
17
18 #include "llvm/CodeGen/CallingConvLower.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/Target/TargetLowering.h"
23
24 namespace llvm {
25
26 namespace AArch64ISD {
27
28 enum {
29   FIRST_NUMBER = ISD::BUILTIN_OP_END,
30   WrapperLarge, // 4-instruction MOVZ/MOVK sequence for 64-bit addresses.
31   CALL,         // Function call.
32
33   // Produces the full sequence of instructions for getting the thread pointer
34   // offset of a variable into X0, using the TLSDesc model.
35   TLSDESC_CALLSEQ,
36   ADRP,     // Page address of a TargetGlobalAddress operand.
37   ADDlow,   // Add the low 12 bits of a TargetGlobalAddress operand.
38   LOADgot,  // Load from automatically generated descriptor (e.g. Global
39             // Offset Table, TLS record).
40   RET_FLAG, // Return with a flag operand. Operand 0 is the chain operand.
41   BRCOND,   // Conditional branch instruction; "b.cond".
42   CSEL,
43   FCSEL, // Conditional move instruction.
44   CSINV, // Conditional select invert.
45   CSNEG, // Conditional select negate.
46   CSINC, // Conditional select increment.
47
48   // Pointer to the thread's local storage area. Materialised from TPIDR_EL0 on
49   // ELF.
50   THREAD_POINTER,
51   ADC,
52   SBC, // adc, sbc instructions
53
54   // Arithmetic instructions which write flags.
55   ADDS,
56   SUBS,
57   ADCS,
58   SBCS,
59   ANDS,
60
61   // Floating point comparison
62   FCMP,
63
64   // Floating point max and min instructions.
65   FMAX,
66   FMIN,
67
68   // Scalar extract
69   EXTR,
70
71   // Scalar-to-vector duplication
72   DUP,
73   DUPLANE8,
74   DUPLANE16,
75   DUPLANE32,
76   DUPLANE64,
77
78   // Vector immedate moves
79   MOVI,
80   MOVIshift,
81   MOVIedit,
82   MOVImsl,
83   FMOV,
84   MVNIshift,
85   MVNImsl,
86
87   // Vector immediate ops
88   BICi,
89   ORRi,
90
91   // Vector bit select: similar to ISD::VSELECT but not all bits within an
92   // element must be identical.
93   BSL,
94
95   // Vector arithmetic negation
96   NEG,
97
98   // Vector shuffles
99   ZIP1,
100   ZIP2,
101   UZP1,
102   UZP2,
103   TRN1,
104   TRN2,
105   REV16,
106   REV32,
107   REV64,
108   EXT,
109
110   // Vector shift by scalar
111   VSHL,
112   VLSHR,
113   VASHR,
114
115   // Vector shift by scalar (again)
116   SQSHL_I,
117   UQSHL_I,
118   SQSHLU_I,
119   SRSHR_I,
120   URSHR_I,
121
122   // Vector comparisons
123   CMEQ,
124   CMGE,
125   CMGT,
126   CMHI,
127   CMHS,
128   FCMEQ,
129   FCMGE,
130   FCMGT,
131
132   // Vector zero comparisons
133   CMEQz,
134   CMGEz,
135   CMGTz,
136   CMLEz,
137   CMLTz,
138   FCMEQz,
139   FCMGEz,
140   FCMGTz,
141   FCMLEz,
142   FCMLTz,
143
144   // Vector bitwise negation
145   NOT,
146
147   // Vector bitwise selection
148   BIT,
149
150   // Compare-and-branch
151   CBZ,
152   CBNZ,
153   TBZ,
154   TBNZ,
155
156   // Tail calls
157   TC_RETURN,
158
159   // Custom prefetch handling
160   PREFETCH,
161
162   // {s|u}int to FP within a FP register.
163   SITOF,
164   UITOF,
165
166   /// Natural vector cast. ISD::BITCAST is not natural in the big-endian
167   /// world w.r.t vectors; which causes additional REV instructions to be
168   /// generated to compensate for the byte-swapping. But sometimes we do
169   /// need to re-interpret the data in SIMD vector registers in big-endian
170   /// mode without emitting such REV instructions.
171   NVCAST,
172
173   SMULL,
174   UMULL,
175
176   // NEON Load/Store with post-increment base updates
177   LD2post = ISD::FIRST_TARGET_MEMORY_OPCODE,
178   LD3post,
179   LD4post,
180   ST2post,
181   ST3post,
182   ST4post,
183   LD1x2post,
184   LD1x3post,
185   LD1x4post,
186   ST1x2post,
187   ST1x3post,
188   ST1x4post,
189   LD1DUPpost,
190   LD2DUPpost,
191   LD3DUPpost,
192   LD4DUPpost,
193   LD1LANEpost,
194   LD2LANEpost,
195   LD3LANEpost,
196   LD4LANEpost,
197   ST2LANEpost,
198   ST3LANEpost,
199   ST4LANEpost
200 };
201
202 } // end namespace AArch64ISD
203
204 class AArch64Subtarget;
205 class AArch64TargetMachine;
206
207 class AArch64TargetLowering : public TargetLowering {
208   bool RequireStrictAlign;
209
210 public:
211   explicit AArch64TargetLowering(const TargetMachine &TM,
212                                  const AArch64Subtarget &STI);
213
214   /// Selects the correct CCAssignFn for a given CallingConvention value.
215   CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg) const;
216
217   /// computeKnownBitsForTargetNode - Determine which of the bits specified in
218   /// Mask are known to be either zero or one and return them in the
219   /// KnownZero/KnownOne bitsets.
220   void computeKnownBitsForTargetNode(const SDValue Op, APInt &KnownZero,
221                                      APInt &KnownOne, const SelectionDAG &DAG,
222                                      unsigned Depth = 0) const override;
223
224   MVT getScalarShiftAmountTy(EVT LHSTy) const override;
225
226   /// allowsMisalignedMemoryAccesses - Returns true if the target allows
227   /// unaligned memory accesses of the specified type.
228   bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AddrSpace = 0,
229                                       unsigned Align = 1,
230                                       bool *Fast = nullptr) const override {
231     if (RequireStrictAlign)
232       return false;
233     // FIXME: True for Cyclone, but not necessary others.
234     if (Fast)
235       *Fast = true;
236     return true;
237   }
238
239   /// LowerOperation - Provide custom lowering hooks for some operations.
240   SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
241
242   const char *getTargetNodeName(unsigned Opcode) const override;
243
244   SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
245
246   /// getFunctionAlignment - Return the Log2 alignment of this function.
247   unsigned getFunctionAlignment(const Function *F) const;
248
249   /// Returns true if a cast between SrcAS and DestAS is a noop.
250   bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {
251     // Addrspacecasts are always noops.
252     return true;
253   }
254
255   /// createFastISel - This method returns a target specific FastISel object,
256   /// or null if the target does not support "fast" ISel.
257   FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
258                            const TargetLibraryInfo *libInfo) const override;
259
260   bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
261
262   bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
263
264   /// isShuffleMaskLegal - Return true if the given shuffle mask can be
265   /// codegen'd directly, or if it should be stack expanded.
266   bool isShuffleMaskLegal(const SmallVectorImpl<int> &M, EVT VT) const override;
267
268   /// getSetCCResultType - Return the ISD::SETCC ValueType
269   EVT getSetCCResultType(LLVMContext &Context, EVT VT) const override;
270
271   SDValue ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const;
272
273   MachineBasicBlock *EmitF128CSEL(MachineInstr *MI,
274                                   MachineBasicBlock *BB) const;
275
276   MachineBasicBlock *
277   EmitInstrWithCustomInserter(MachineInstr *MI,
278                               MachineBasicBlock *MBB) const override;
279
280   bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
281                           unsigned Intrinsic) const override;
282
283   bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
284   bool isTruncateFree(EVT VT1, EVT VT2) const override;
285
286   bool isProfitableToHoist(Instruction *I) const override;
287
288   bool isZExtFree(Type *Ty1, Type *Ty2) const override;
289   bool isZExtFree(EVT VT1, EVT VT2) const override;
290   bool isZExtFree(SDValue Val, EVT VT2) const override;
291
292   bool hasPairedLoad(Type *LoadedType,
293                      unsigned &RequiredAligment) const override;
294   bool hasPairedLoad(EVT LoadedType, unsigned &RequiredAligment) const override;
295
296   bool isLegalAddImmediate(int64_t) const override;
297   bool isLegalICmpImmediate(int64_t) const override;
298
299   EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign, unsigned SrcAlign,
300                           bool IsMemset, bool ZeroMemset, bool MemcpyStrSrc,
301                           MachineFunction &MF) const override;
302
303   /// isLegalAddressingMode - Return true if the addressing mode represented
304   /// by AM is legal for this target, for a load/store of the specified type.
305   bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const override;
306
307   /// \brief Return the cost of the scaling factor used in the addressing
308   /// mode represented by AM for this target, for a load/store
309   /// of the specified type.
310   /// If the AM is supported, the return value must be >= 0.
311   /// If the AM is not supported, it returns a negative value.
312   int getScalingFactorCost(const AddrMode &AM, Type *Ty) const override;
313
314   /// isFMAFasterThanFMulAndFAdd - Return true if an FMA operation is faster
315   /// than a pair of fmul and fadd instructions. fmuladd intrinsics will be
316   /// expanded to FMAs when this method returns true, otherwise fmuladd is
317   /// expanded to fmul + fadd.
318   bool isFMAFasterThanFMulAndFAdd(EVT VT) const override;
319
320   const MCPhysReg *getScratchRegisters(CallingConv::ID CC) const override;
321
322   /// \brief Returns false if N is a bit extraction pattern of (X >> C) & Mask.
323   bool isDesirableToCommuteWithShift(const SDNode *N) const override;
324
325   /// \brief Returns true if it is beneficial to convert a load of a constant
326   /// to just the constant itself.
327   bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
328                                          Type *Ty) const override;
329
330   bool hasLoadLinkedStoreConditional() const override;
331   Value *emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
332                         AtomicOrdering Ord) const override;
333   Value *emitStoreConditional(IRBuilder<> &Builder, Value *Val,
334                               Value *Addr, AtomicOrdering Ord) const override;
335
336   bool shouldExpandAtomicLoadInIR(LoadInst *LI) const override;
337   bool shouldExpandAtomicStoreInIR(StoreInst *SI) const override;
338   TargetLoweringBase::AtomicRMWExpansionKind
339   shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override;
340
341   bool useLoadStackGuardNode() const override;
342   TargetLoweringBase::LegalizeTypeAction
343   getPreferredVectorAction(EVT VT) const override;
344
345 private:
346   /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
347   /// make the right decision when generating code for different targets.
348   const AArch64Subtarget *Subtarget;
349
350   void addTypeForNEON(EVT VT, EVT PromotedBitwiseVT);
351   void addDRTypeForNEON(MVT VT);
352   void addQRTypeForNEON(MVT VT);
353
354   SDValue
355   LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
356                        const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL,
357                        SelectionDAG &DAG,
358                        SmallVectorImpl<SDValue> &InVals) const override;
359
360   SDValue LowerCall(CallLoweringInfo & /*CLI*/,
361                     SmallVectorImpl<SDValue> &InVals) const override;
362
363   SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
364                           CallingConv::ID CallConv, bool isVarArg,
365                           const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL,
366                           SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
367                           bool isThisReturn, SDValue ThisVal) const;
368
369   bool isEligibleForTailCallOptimization(
370       SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
371       bool isCalleeStructRet, bool isCallerStructRet,
372       const SmallVectorImpl<ISD::OutputArg> &Outs,
373       const SmallVectorImpl<SDValue> &OutVals,
374       const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const;
375
376   /// Finds the incoming stack arguments which overlap the given fixed stack
377   /// object and incorporates their load into the current chain. This prevents
378   /// an upcoming store from clobbering the stack argument before it's used.
379   SDValue addTokenForArgument(SDValue Chain, SelectionDAG &DAG,
380                               MachineFrameInfo *MFI, int ClobberedFI) const;
381
382   bool DoesCalleeRestoreStack(CallingConv::ID CallCC, bool TailCallOpt) const;
383
384   bool IsTailCallConvention(CallingConv::ID CallCC) const;
385
386   void saveVarArgRegisters(CCState &CCInfo, SelectionDAG &DAG, SDLoc DL,
387                            SDValue &Chain) const;
388
389   bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
390                       bool isVarArg,
391                       const SmallVectorImpl<ISD::OutputArg> &Outs,
392                       LLVMContext &Context) const override;
393
394   SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
395                       const SmallVectorImpl<ISD::OutputArg> &Outs,
396                       const SmallVectorImpl<SDValue> &OutVals, SDLoc DL,
397                       SelectionDAG &DAG) const override;
398
399   SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
400   SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
401   SDValue LowerDarwinGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
402   SDValue LowerELFGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
403   SDValue LowerELFTLSDescCallSeq(SDValue SymAddr, SDLoc DL,
404                                  SelectionDAG &DAG) const;
405   SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
406   SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
407   SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
408   SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
409   SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
410   SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
411   SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
412   SDValue LowerAAPCS_VASTART(SDValue Op, SelectionDAG &DAG) const;
413   SDValue LowerDarwin_VASTART(SDValue Op, SelectionDAG &DAG) const;
414   SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
415   SDValue LowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
416   SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) const;
417   SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
418   SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
419   SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
420   SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
421   SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const;
422   SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
423   SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
424   SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
425   SDValue LowerVectorSRA_SRL_SHL(SDValue Op, SelectionDAG &DAG) const;
426   SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
427   SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) const;
428   SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) const;
429   SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) const;
430   SDValue LowerF128Call(SDValue Op, SelectionDAG &DAG,
431                         RTLIB::Libcall Call) const;
432   SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
433   SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const;
434   SDValue LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const;
435   SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const;
436   SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
437   SDValue LowerVectorAND(SDValue Op, SelectionDAG &DAG) const;
438   SDValue LowerVectorOR(SDValue Op, SelectionDAG &DAG) const;
439   SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
440   SDValue LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const;
441
442   SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG,
443                         std::vector<SDNode *> *Created) const override;
444   bool combineRepeatedFPDivisors(unsigned NumUsers) const override;
445
446   ConstraintType
447   getConstraintType(const std::string &Constraint) const override;
448   unsigned getRegisterByName(const char* RegName, EVT VT) const override;
449
450   /// Examine constraint string and operand type and determine a weight value.
451   /// The operand object must already have been set up with the operand type.
452   ConstraintWeight
453   getSingleConstraintMatchWeight(AsmOperandInfo &info,
454                                  const char *constraint) const override;
455
456   std::pair<unsigned, const TargetRegisterClass *>
457   getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
458                                const std::string &Constraint,
459                                MVT VT) const override;
460   void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
461                                     std::vector<SDValue> &Ops,
462                                     SelectionDAG &DAG) const override;
463
464   bool isUsedByReturnOnly(SDNode *N, SDValue &Chain) const override;
465   bool mayBeEmittedAsTailCall(CallInst *CI) const override;
466   bool getIndexedAddressParts(SDNode *Op, SDValue &Base, SDValue &Offset,
467                               ISD::MemIndexedMode &AM, bool &IsInc,
468                               SelectionDAG &DAG) const;
469   bool getPreIndexedAddressParts(SDNode *N, SDValue &Base, SDValue &Offset,
470                                  ISD::MemIndexedMode &AM,
471                                  SelectionDAG &DAG) const override;
472   bool getPostIndexedAddressParts(SDNode *N, SDNode *Op, SDValue &Base,
473                                   SDValue &Offset, ISD::MemIndexedMode &AM,
474                                   SelectionDAG &DAG) const override;
475
476   void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
477                           SelectionDAG &DAG) const override;
478
479   bool functionArgumentNeedsConsecutiveRegisters(Type *Ty,
480                                                  CallingConv::ID CallConv,
481                                                  bool isVarArg) const override;
482 };
483
484 namespace AArch64 {
485 FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
486                          const TargetLibraryInfo *libInfo);
487 } // end namespace AArch64
488
489 } // end namespace llvm
490
491 #endif