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