1 //==-- AArch64ISelLowering.h - AArch64 DAG Lowering Interface ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that AArch64 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64ISELLOWERING_H
16 #define LLVM_LIB_TARGET_AARCH64_AARCH64ISELLOWERING_H
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"
26 namespace AArch64ISD {
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.
33 // Produces the full sequence of instructions for getting the thread pointer
34 // offset of a variable into X0, using the TLSDesc model.
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".
43 FCSEL, // Conditional move instruction.
44 CSINV, // Conditional select invert.
45 CSNEG, // Conditional select negate.
46 CSINC, // Conditional select increment.
48 // Pointer to the thread's local storage area. Materialised from TPIDR_EL0 on
52 SBC, // adc, sbc instructions
54 // Arithmetic instructions which write flags.
61 // Conditional compares. Operands: left,right,falsecc,cc,flags
66 // Floating point comparison
69 // Floating point max and min instructions.
76 // Scalar-to-vector duplication
83 // Vector immedate moves
92 // Vector immediate ops
96 // Vector bit select: similar to ISD::VSELECT but not all bits within an
97 // element must be identical.
100 // Vector arithmetic negation
115 // Vector shift by scalar
120 // Vector shift by scalar (again)
127 // Vector comparisons
137 // Vector zero comparisons
149 // Vector across-lanes addition
150 // Only the lower result lane is defined.
154 // Vector across-lanes min/max
155 // Only the lower result lane is defined.
161 // Vector bitwise negation
164 // Vector bitwise selection
167 // Compare-and-branch
176 // Custom prefetch handling
179 // {s|u}int to FP within a FP register.
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.
193 // NEON Load/Store with post-increment base updates
194 LD2post = ISD::FIRST_TARGET_MEMORY_OPCODE,
219 } // end namespace AArch64ISD
221 class AArch64Subtarget;
222 class AArch64TargetMachine;
224 class AArch64TargetLowering : public TargetLowering {
226 explicit AArch64TargetLowering(const TargetMachine &TM,
227 const AArch64Subtarget &STI);
229 /// Selects the correct CCAssignFn for a given CallingConvention value.
230 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg) const;
232 /// computeKnownBitsForTargetNode - Determine which of the bits specified in
233 /// Mask are known to be either zero or one and return them in the
234 /// KnownZero/KnownOne bitsets.
235 void computeKnownBitsForTargetNode(const SDValue Op, APInt &KnownZero,
236 APInt &KnownOne, const SelectionDAG &DAG,
237 unsigned Depth = 0) const override;
239 MVT getScalarShiftAmountTy(const DataLayout &DL, EVT) const override;
241 /// allowsMisalignedMemoryAccesses - Returns true if the target allows
242 /// unaligned memory accesses of the specified type.
243 bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AddrSpace = 0,
245 bool *Fast = nullptr) const override;
247 /// LowerOperation - Provide custom lowering hooks for some operations.
248 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
250 const char *getTargetNodeName(unsigned Opcode) const override;
252 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
254 /// getFunctionAlignment - Return the Log2 alignment of this function.
255 unsigned getFunctionAlignment(const Function *F) const;
257 /// Returns true if a cast between SrcAS and DestAS is a noop.
258 bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {
259 // Addrspacecasts are always noops.
263 /// createFastISel - This method returns a target specific FastISel object,
264 /// or null if the target does not support "fast" ISel.
265 FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
266 const TargetLibraryInfo *libInfo) const override;
268 bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
270 bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
272 /// isShuffleMaskLegal - Return true if the given shuffle mask can be
273 /// codegen'd directly, or if it should be stack expanded.
274 bool isShuffleMaskLegal(const SmallVectorImpl<int> &M, EVT VT) const override;
276 /// getSetCCResultType - Return the ISD::SETCC ValueType
277 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
278 EVT VT) const override;
280 SDValue ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const;
282 MachineBasicBlock *EmitF128CSEL(MachineInstr *MI,
283 MachineBasicBlock *BB) const;
286 EmitInstrWithCustomInserter(MachineInstr *MI,
287 MachineBasicBlock *MBB) const override;
289 bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
290 unsigned Intrinsic) const override;
292 bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
293 bool isTruncateFree(EVT VT1, EVT VT2) const override;
295 bool isProfitableToHoist(Instruction *I) const override;
297 bool isZExtFree(Type *Ty1, Type *Ty2) const override;
298 bool isZExtFree(EVT VT1, EVT VT2) const override;
299 bool isZExtFree(SDValue Val, EVT VT2) const override;
301 bool hasPairedLoad(Type *LoadedType,
302 unsigned &RequiredAligment) const override;
303 bool hasPairedLoad(EVT LoadedType, unsigned &RequiredAligment) const override;
305 unsigned getMaxSupportedInterleaveFactor() const override { return 4; }
307 bool lowerInterleavedLoad(LoadInst *LI,
308 ArrayRef<ShuffleVectorInst *> Shuffles,
309 ArrayRef<unsigned> Indices,
310 unsigned Factor) const override;
311 bool lowerInterleavedStore(StoreInst *SI, ShuffleVectorInst *SVI,
312 unsigned Factor) const override;
314 bool isLegalAddImmediate(int64_t) const override;
315 bool isLegalICmpImmediate(int64_t) const override;
317 EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign, unsigned SrcAlign,
318 bool IsMemset, bool ZeroMemset, bool MemcpyStrSrc,
319 MachineFunction &MF) const override;
321 /// isLegalAddressingMode - Return true if the addressing mode represented
322 /// by AM is legal for this target, for a load/store of the specified type.
323 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
324 unsigned AS) const override;
326 /// \brief Return the cost of the scaling factor used in the addressing
327 /// mode represented by AM for this target, for a load/store
328 /// of the specified type.
329 /// If the AM is supported, the return value must be >= 0.
330 /// If the AM is not supported, it returns a negative value.
331 int getScalingFactorCost(const DataLayout &DL, const AddrMode &AM, Type *Ty,
332 unsigned AS) const override;
334 /// isFMAFasterThanFMulAndFAdd - Return true if an FMA operation is faster
335 /// than a pair of fmul and fadd instructions. fmuladd intrinsics will be
336 /// expanded to FMAs when this method returns true, otherwise fmuladd is
337 /// expanded to fmul + fadd.
338 bool isFMAFasterThanFMulAndFAdd(EVT VT) const override;
340 const MCPhysReg *getScratchRegisters(CallingConv::ID CC) const override;
342 /// \brief Returns false if N is a bit extraction pattern of (X >> C) & Mask.
343 bool isDesirableToCommuteWithShift(const SDNode *N) const override;
345 /// \brief Returns true if it is beneficial to convert a load of a constant
346 /// to just the constant itself.
347 bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
348 Type *Ty) const override;
350 bool hasLoadLinkedStoreConditional() const override;
351 Value *emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
352 AtomicOrdering Ord) const override;
353 Value *emitStoreConditional(IRBuilder<> &Builder, Value *Val,
354 Value *Addr, AtomicOrdering Ord) const override;
356 bool shouldExpandAtomicLoadInIR(LoadInst *LI) const override;
357 bool shouldExpandAtomicStoreInIR(StoreInst *SI) const override;
358 TargetLoweringBase::AtomicRMWExpansionKind
359 shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override;
361 bool useLoadStackGuardNode() const override;
362 TargetLoweringBase::LegalizeTypeAction
363 getPreferredVectorAction(EVT VT) const override;
366 bool isExtFreeImpl(const Instruction *Ext) const override;
368 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
369 /// make the right decision when generating code for different targets.
370 const AArch64Subtarget *Subtarget;
372 void addTypeForNEON(EVT VT, EVT PromotedBitwiseVT);
373 void addDRTypeForNEON(MVT VT);
374 void addQRTypeForNEON(MVT VT);
377 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
378 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL,
380 SmallVectorImpl<SDValue> &InVals) const override;
382 SDValue LowerCall(CallLoweringInfo & /*CLI*/,
383 SmallVectorImpl<SDValue> &InVals) const override;
385 SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
386 CallingConv::ID CallConv, bool isVarArg,
387 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL,
388 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
389 bool isThisReturn, SDValue ThisVal) const;
391 SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
393 bool isEligibleForTailCallOptimization(
394 SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
395 bool isCalleeStructRet, bool isCallerStructRet,
396 const SmallVectorImpl<ISD::OutputArg> &Outs,
397 const SmallVectorImpl<SDValue> &OutVals,
398 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const;
400 /// Finds the incoming stack arguments which overlap the given fixed stack
401 /// object and incorporates their load into the current chain. This prevents
402 /// an upcoming store from clobbering the stack argument before it's used.
403 SDValue addTokenForArgument(SDValue Chain, SelectionDAG &DAG,
404 MachineFrameInfo *MFI, int ClobberedFI) const;
406 bool DoesCalleeRestoreStack(CallingConv::ID CallCC, bool TailCallOpt) const;
408 bool IsTailCallConvention(CallingConv::ID CallCC) const;
410 void saveVarArgRegisters(CCState &CCInfo, SelectionDAG &DAG, SDLoc DL,
411 SDValue &Chain) const;
413 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
415 const SmallVectorImpl<ISD::OutputArg> &Outs,
416 LLVMContext &Context) const override;
418 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
419 const SmallVectorImpl<ISD::OutputArg> &Outs,
420 const SmallVectorImpl<SDValue> &OutVals, SDLoc DL,
421 SelectionDAG &DAG) const override;
423 SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
424 SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
425 SDValue LowerDarwinGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
426 SDValue LowerELFGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
427 SDValue LowerELFTLSDescCallSeq(SDValue SymAddr, SDLoc DL,
428 SelectionDAG &DAG) const;
429 SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
430 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
431 SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
432 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
433 SDValue LowerSELECT_CC(ISD::CondCode CC, SDValue LHS, SDValue RHS,
434 SDValue TVal, SDValue FVal, SDLoc dl,
435 SelectionDAG &DAG) const;
436 SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
437 SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
438 SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
439 SDValue LowerAAPCS_VASTART(SDValue Op, SelectionDAG &DAG) const;
440 SDValue LowerDarwin_VASTART(SDValue Op, SelectionDAG &DAG) const;
441 SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
442 SDValue LowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
443 SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) const;
444 SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
445 SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
446 SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
447 SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
448 SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const;
449 SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
450 SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
451 SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
452 SDValue LowerVectorSRA_SRL_SHL(SDValue Op, SelectionDAG &DAG) const;
453 SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
454 SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) const;
455 SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) const;
456 SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) const;
457 SDValue LowerF128Call(SDValue Op, SelectionDAG &DAG,
458 RTLIB::Libcall Call) const;
459 SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
460 SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const;
461 SDValue LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const;
462 SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const;
463 SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
464 SDValue LowerVectorAND(SDValue Op, SelectionDAG &DAG) const;
465 SDValue LowerVectorOR(SDValue Op, SelectionDAG &DAG) const;
466 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
467 SDValue LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const;
469 SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG,
470 std::vector<SDNode *> *Created) const override;
471 unsigned combineRepeatedFPDivisors() const override;
473 ConstraintType getConstraintType(StringRef Constraint) const override;
474 unsigned getRegisterByName(const char* RegName, EVT VT,
475 SelectionDAG &DAG) const override;
477 /// Examine constraint string and operand type and determine a weight value.
478 /// The operand object must already have been set up with the operand type.
480 getSingleConstraintMatchWeight(AsmOperandInfo &info,
481 const char *constraint) const override;
483 std::pair<unsigned, const TargetRegisterClass *>
484 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
485 StringRef Constraint, MVT VT) const override;
486 void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
487 std::vector<SDValue> &Ops,
488 SelectionDAG &DAG) const override;
490 unsigned getInlineAsmMemConstraint(StringRef ConstraintCode) const override {
491 if (ConstraintCode == "Q")
492 return InlineAsm::Constraint_Q;
493 // FIXME: clang has code for 'Ump', 'Utf', 'Usa', and 'Ush' but these are
494 // followed by llvm_unreachable so we'll leave them unimplemented in
495 // the backend for now.
496 return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
499 bool isUsedByReturnOnly(SDNode *N, SDValue &Chain) const override;
500 bool mayBeEmittedAsTailCall(CallInst *CI) const override;
501 bool getIndexedAddressParts(SDNode *Op, SDValue &Base, SDValue &Offset,
502 ISD::MemIndexedMode &AM, bool &IsInc,
503 SelectionDAG &DAG) const;
504 bool getPreIndexedAddressParts(SDNode *N, SDValue &Base, SDValue &Offset,
505 ISD::MemIndexedMode &AM,
506 SelectionDAG &DAG) const override;
507 bool getPostIndexedAddressParts(SDNode *N, SDNode *Op, SDValue &Base,
508 SDValue &Offset, ISD::MemIndexedMode &AM,
509 SelectionDAG &DAG) const override;
511 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
512 SelectionDAG &DAG) const override;
514 bool functionArgumentNeedsConsecutiveRegisters(Type *Ty,
515 CallingConv::ID CallConv,
516 bool isVarArg) const override;
518 bool shouldNormalizeToSelectSequence(LLVMContext &, EVT) const override;
522 FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
523 const TargetLibraryInfo *libInfo);
524 } // end namespace AArch64
526 } // end namespace llvm