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