AVX-512: Added VPERM* instructons and MOV* zmm-to-zmm instructions.
[oota-llvm.git] / lib / Target / X86 / X86ISelLowering.h
1 //===-- X86ISelLowering.h - X86 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 X86 uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef X86ISELLOWERING_H
16 #define X86ISELLOWERING_H
17
18 #include "X86MachineFunctionInfo.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/FastISel.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/Target/TargetOptions.h"
26
27 namespace llvm {
28   namespace X86ISD {
29     // X86 Specific DAG Nodes
30     enum NodeType {
31       // Start the numbering where the builtin ops leave off.
32       FIRST_NUMBER = ISD::BUILTIN_OP_END,
33
34       /// BSF - Bit scan forward.
35       /// BSR - Bit scan reverse.
36       BSF,
37       BSR,
38
39       /// SHLD, SHRD - Double shift instructions. These correspond to
40       /// X86::SHLDxx and X86::SHRDxx instructions.
41       SHLD,
42       SHRD,
43
44       /// FAND - Bitwise logical AND of floating point values. This corresponds
45       /// to X86::ANDPS or X86::ANDPD.
46       FAND,
47
48       /// FOR - Bitwise logical OR of floating point values. This corresponds
49       /// to X86::ORPS or X86::ORPD.
50       FOR,
51
52       /// FXOR - Bitwise logical XOR of floating point values. This corresponds
53       /// to X86::XORPS or X86::XORPD.
54       FXOR,
55
56       /// FANDN - Bitwise logical ANDNOT of floating point values. This
57       /// corresponds to X86::ANDNPS or X86::ANDNPD.
58       FANDN,
59
60       /// FSRL - Bitwise logical right shift of floating point values. These
61       /// corresponds to X86::PSRLDQ.
62       FSRL,
63
64       /// CALL - These operations represent an abstract X86 call
65       /// instruction, which includes a bunch of information.  In particular the
66       /// operands of these node are:
67       ///
68       ///     #0 - The incoming token chain
69       ///     #1 - The callee
70       ///     #2 - The number of arg bytes the caller pushes on the stack.
71       ///     #3 - The number of arg bytes the callee pops off the stack.
72       ///     #4 - The value to pass in AL/AX/EAX (optional)
73       ///     #5 - The value to pass in DL/DX/EDX (optional)
74       ///
75       /// The result values of these nodes are:
76       ///
77       ///     #0 - The outgoing token chain
78       ///     #1 - The first register result value (optional)
79       ///     #2 - The second register result value (optional)
80       ///
81       CALL,
82
83       /// RDTSC_DAG - This operation implements the lowering for
84       /// readcyclecounter
85       RDTSC_DAG,
86
87       /// X86 compare and logical compare instructions.
88       CMP, COMI, UCOMI,
89
90       /// X86 bit-test instructions.
91       BT,
92
93       /// X86 SetCC. Operand 0 is condition code, and operand 1 is the EFLAGS
94       /// operand, usually produced by a CMP instruction.
95       SETCC,
96
97       // Same as SETCC except it's materialized with a sbb and the value is all
98       // one's or all zero's.
99       SETCC_CARRY,  // R = carry_bit ? ~0 : 0
100
101       /// X86 FP SETCC, implemented with CMP{cc}SS/CMP{cc}SD.
102       /// Operands are two FP values to compare; result is a mask of
103       /// 0s or 1s.  Generally DTRT for C/C++ with NaNs.
104       FSETCCss, FSETCCsd,
105
106       /// X86 MOVMSK{pd|ps}, extracts sign bits of two or four FP values,
107       /// result in an integer GPR.  Needs masking for scalar result.
108       FGETSIGNx86,
109
110       /// X86 conditional moves. Operand 0 and operand 1 are the two values
111       /// to select from. Operand 2 is the condition code, and operand 3 is the
112       /// flag operand produced by a CMP or TEST instruction. It also writes a
113       /// flag result.
114       CMOV,
115
116       /// X86 conditional branches. Operand 0 is the chain operand, operand 1
117       /// is the block to branch if condition is true, operand 2 is the
118       /// condition code, and operand 3 is the flag operand produced by a CMP
119       /// or TEST instruction.
120       BRCOND,
121
122       /// Return with a flag operand. Operand 0 is the chain operand, operand
123       /// 1 is the number of bytes of stack to pop.
124       RET_FLAG,
125
126       /// REP_STOS - Repeat fill, corresponds to X86::REP_STOSx.
127       REP_STOS,
128
129       /// REP_MOVS - Repeat move, corresponds to X86::REP_MOVSx.
130       REP_MOVS,
131
132       /// GlobalBaseReg - On Darwin, this node represents the result of the popl
133       /// at function entry, used for PIC code.
134       GlobalBaseReg,
135
136       /// Wrapper - A wrapper node for TargetConstantPool,
137       /// TargetExternalSymbol, and TargetGlobalAddress.
138       Wrapper,
139
140       /// WrapperRIP - Special wrapper used under X86-64 PIC mode for RIP
141       /// relative displacements.
142       WrapperRIP,
143
144       /// MOVDQ2Q - Copies a 64-bit value from the low word of an XMM vector
145       /// to an MMX vector.  If you think this is too close to the previous
146       /// mnemonic, so do I; blame Intel.
147       MOVDQ2Q,
148
149       /// MMX_MOVD2W - Copies a 32-bit value from the low word of a MMX
150       /// vector to a GPR.
151       MMX_MOVD2W,
152
153       /// PEXTRB - Extract an 8-bit value from a vector and zero extend it to
154       /// i32, corresponds to X86::PEXTRB.
155       PEXTRB,
156
157       /// PEXTRW - Extract a 16-bit value from a vector and zero extend it to
158       /// i32, corresponds to X86::PEXTRW.
159       PEXTRW,
160
161       /// INSERTPS - Insert any element of a 4 x float vector into any element
162       /// of a destination 4 x floatvector.
163       INSERTPS,
164
165       /// PINSRB - Insert the lower 8-bits of a 32-bit value to a vector,
166       /// corresponds to X86::PINSRB.
167       PINSRB,
168
169       /// PINSRW - Insert the lower 16-bits of a 32-bit value to a vector,
170       /// corresponds to X86::PINSRW.
171       PINSRW, MMX_PINSRW,
172
173       /// PSHUFB - Shuffle 16 8-bit values within a vector.
174       PSHUFB,
175
176       /// ANDNP - Bitwise Logical AND NOT of Packed FP values.
177       ANDNP,
178
179       /// PSIGN - Copy integer sign.
180       PSIGN,
181
182       /// BLENDV - Blend where the selector is a register.
183       BLENDV,
184
185       /// BLENDI - Blend where the selector is an immediate.
186       BLENDI,
187
188       // SUBUS - Integer sub with unsigned saturation.
189       SUBUS,
190
191       /// HADD - Integer horizontal add.
192       HADD,
193
194       /// HSUB - Integer horizontal sub.
195       HSUB,
196
197       /// FHADD - Floating point horizontal add.
198       FHADD,
199
200       /// FHSUB - Floating point horizontal sub.
201       FHSUB,
202
203       /// UMAX, UMIN - Unsigned integer max and min.
204       UMAX, UMIN,
205
206       /// SMAX, SMIN - Signed integer max and min.
207       SMAX, SMIN,
208
209       /// FMAX, FMIN - Floating point max and min.
210       ///
211       FMAX, FMIN,
212
213       /// FMAXC, FMINC - Commutative FMIN and FMAX.
214       FMAXC, FMINC,
215
216       /// FRSQRT, FRCP - Floating point reciprocal-sqrt and reciprocal
217       /// approximation.  Note that these typically require refinement
218       /// in order to obtain suitable precision.
219       FRSQRT, FRCP,
220
221       // TLSADDR - Thread Local Storage.
222       TLSADDR,
223
224       // TLSBASEADDR - Thread Local Storage. A call to get the start address
225       // of the TLS block for the current module.
226       TLSBASEADDR,
227
228       // TLSCALL - Thread Local Storage.  When calling to an OS provided
229       // thunk at the address from an earlier relocation.
230       TLSCALL,
231
232       // EH_RETURN - Exception Handling helpers.
233       EH_RETURN,
234
235       // EH_SJLJ_SETJMP - SjLj exception handling setjmp.
236       EH_SJLJ_SETJMP,
237
238       // EH_SJLJ_LONGJMP - SjLj exception handling longjmp.
239       EH_SJLJ_LONGJMP,
240
241       /// TC_RETURN - Tail call return. See X86TargetLowering::LowerCall for
242       /// the list of operands.
243       TC_RETURN,
244
245       // VZEXT_MOVL - Vector move low and zero extend.
246       VZEXT_MOVL,
247
248       // VSEXT_MOVL - Vector move low and sign extend.
249       VSEXT_MOVL,
250
251       // VZEXT - Vector integer zero-extend.
252       VZEXT,
253
254       // VSEXT - Vector integer signed-extend.
255       VSEXT,
256
257       // VFPEXT - Vector FP extend.
258       VFPEXT,
259
260       // VFPROUND - Vector FP round.
261       VFPROUND,
262
263       // VSHL, VSRL - 128-bit vector logical left / right shift
264       VSHLDQ, VSRLDQ,
265
266       // VSHL, VSRL, VSRA - Vector shift elements
267       VSHL, VSRL, VSRA,
268
269       // VSHLI, VSRLI, VSRAI - Vector shift elements by immediate
270       VSHLI, VSRLI, VSRAI,
271
272       // CMPP - Vector packed double/float comparison.
273       CMPP,
274
275       // PCMP* - Vector integer comparisons.
276       PCMPEQ, PCMPGT,
277
278       // ADD, SUB, SMUL, etc. - Arithmetic operations with FLAGS results.
279       ADD, SUB, ADC, SBB, SMUL,
280       INC, DEC, OR, XOR, AND,
281
282       BLSI,   // BLSI - Extract lowest set isolated bit
283       BLSMSK, // BLSMSK - Get mask up to lowest set bit
284       BLSR,   // BLSR - Reset lowest set bit
285
286       UMUL, // LOW, HI, FLAGS = umul LHS, RHS
287
288       // MUL_IMM - X86 specific multiply by immediate.
289       MUL_IMM,
290
291       // PTEST - Vector bitwise comparisons
292       PTEST,
293
294       // TESTP - Vector packed fp sign bitwise comparisons
295       TESTP,
296
297       // OR/AND test for masks
298       KORTEST,
299       KTEST,
300
301       // Several flavors of instructions with vector shuffle behaviors.
302       PALIGNR,
303       PSHUFD,
304       PSHUFHW,
305       PSHUFLW,
306       SHUFP,
307       MOVDDUP,
308       MOVSHDUP,
309       MOVSLDUP,
310       MOVLHPS,
311       MOVLHPD,
312       MOVHLPS,
313       MOVLPS,
314       MOVLPD,
315       MOVSD,
316       MOVSS,
317       UNPCKL,
318       UNPCKH,
319       VPERMILP,
320       VPERMV,
321       VPERMV3,
322       VPERMI,
323       VPERM2X128,
324       VBROADCAST,
325       // masked broadcast
326       VBROADCASTM,
327
328       // PMULUDQ - Vector multiply packed unsigned doubleword integers
329       PMULUDQ,
330
331       // FMA nodes
332       FMADD,
333       FNMADD,
334       FMSUB,
335       FNMSUB,
336       FMADDSUB,
337       FMSUBADD,
338
339       // VASTART_SAVE_XMM_REGS - Save xmm argument registers to the stack,
340       // according to %al. An operator is needed so that this can be expanded
341       // with control flow.
342       VASTART_SAVE_XMM_REGS,
343
344       // WIN_ALLOCA - Windows's _chkstk call to do stack probing.
345       WIN_ALLOCA,
346
347       // SEG_ALLOCA - For allocating variable amounts of stack space when using
348       // segmented stacks. Check if the current stacklet has enough space, and
349       // falls back to heap allocation if not.
350       SEG_ALLOCA,
351
352       // WIN_FTOL - Windows's _ftol2 runtime routine to do fptoui.
353       WIN_FTOL,
354
355       // Memory barrier
356       MEMBARRIER,
357       MFENCE,
358       SFENCE,
359       LFENCE,
360
361       // FNSTSW16r - Store FP status word into i16 register.
362       FNSTSW16r,
363
364       // SAHF - Store contents of %ah into %eflags.
365       SAHF,
366
367       // RDRAND - Get a random integer and indicate whether it is valid in CF.
368       RDRAND,
369
370       // RDSEED - Get a NIST SP800-90B & C compliant random integer and
371       // indicate whether it is valid in CF.
372       RDSEED,
373
374       // PCMP*STRI
375       PCMPISTRI,
376       PCMPESTRI,
377
378       // XTEST - Test if in transactional execution.
379       XTEST,
380
381       // ATOMADD64_DAG, ATOMSUB64_DAG, ATOMOR64_DAG, ATOMAND64_DAG,
382       // ATOMXOR64_DAG, ATOMNAND64_DAG, ATOMSWAP64_DAG -
383       // Atomic 64-bit binary operations.
384       ATOMADD64_DAG = ISD::FIRST_TARGET_MEMORY_OPCODE,
385       ATOMSUB64_DAG,
386       ATOMOR64_DAG,
387       ATOMXOR64_DAG,
388       ATOMAND64_DAG,
389       ATOMNAND64_DAG,
390       ATOMMAX64_DAG,
391       ATOMMIN64_DAG,
392       ATOMUMAX64_DAG,
393       ATOMUMIN64_DAG,
394       ATOMSWAP64_DAG,
395
396       // LCMPXCHG_DAG, LCMPXCHG8_DAG, LCMPXCHG16_DAG - Compare and swap.
397       LCMPXCHG_DAG,
398       LCMPXCHG8_DAG,
399       LCMPXCHG16_DAG,
400
401       // VZEXT_LOAD - Load, scalar_to_vector, and zero extend.
402       VZEXT_LOAD,
403
404       // FNSTCW16m - Store FP control world into i16 memory.
405       FNSTCW16m,
406
407       /// FP_TO_INT*_IN_MEM - This instruction implements FP_TO_SINT with the
408       /// integer destination in memory and a FP reg source.  This corresponds
409       /// to the X86::FIST*m instructions and the rounding mode change stuff. It
410       /// has two inputs (token chain and address) and two outputs (int value
411       /// and token chain).
412       FP_TO_INT16_IN_MEM,
413       FP_TO_INT32_IN_MEM,
414       FP_TO_INT64_IN_MEM,
415
416       /// FILD, FILD_FLAG - This instruction implements SINT_TO_FP with the
417       /// integer source in memory and FP reg result.  This corresponds to the
418       /// X86::FILD*m instructions. It has three inputs (token chain, address,
419       /// and source type) and two outputs (FP value and token chain). FILD_FLAG
420       /// also produces a flag).
421       FILD,
422       FILD_FLAG,
423
424       /// FLD - This instruction implements an extending load to FP stack slots.
425       /// This corresponds to the X86::FLD32m / X86::FLD64m. It takes a chain
426       /// operand, ptr to load from, and a ValueType node indicating the type
427       /// to load to.
428       FLD,
429
430       /// FST - This instruction implements a truncating store to FP stack
431       /// slots. This corresponds to the X86::FST32m / X86::FST64m. It takes a
432       /// chain operand, value to store, address, and a ValueType to store it
433       /// as.
434       FST,
435
436       /// VAARG_64 - This instruction grabs the address of the next argument
437       /// from a va_list. (reads and modifies the va_list in memory)
438       VAARG_64
439
440       // WARNING: Do not add anything in the end unless you want the node to
441       // have memop! In fact, starting from ATOMADD64_DAG all opcodes will be
442       // thought as target memory ops!
443     };
444   }
445
446   /// Define some predicates that are used for node matching.
447   namespace X86 {
448     /// isVEXTRACT128Index - Return true if the specified
449     /// EXTRACT_SUBVECTOR operand specifies a vector extract that is
450     /// suitable for input to VEXTRACTF128, VEXTRACTI128 instructions.
451     bool isVEXTRACT128Index(SDNode *N);
452
453     /// isVINSERT128Index - Return true if the specified
454     /// INSERT_SUBVECTOR operand specifies a subvector insert that is
455     /// suitable for input to VINSERTF128, VINSERTI128 instructions.
456     bool isVINSERT128Index(SDNode *N);
457
458     /// isVEXTRACT256Index - Return true if the specified
459     /// EXTRACT_SUBVECTOR operand specifies a vector extract that is
460     /// suitable for input to VEXTRACTF64X4, VEXTRACTI64X4 instructions.
461     bool isVEXTRACT256Index(SDNode *N);
462
463     /// isVINSERT256Index - Return true if the specified
464     /// INSERT_SUBVECTOR operand specifies a subvector insert that is
465     /// suitable for input to VINSERTF64X4, VINSERTI64X4 instructions.
466     bool isVINSERT256Index(SDNode *N);
467
468     /// getExtractVEXTRACT128Immediate - Return the appropriate
469     /// immediate to extract the specified EXTRACT_SUBVECTOR index
470     /// with VEXTRACTF128, VEXTRACTI128 instructions.
471     unsigned getExtractVEXTRACT128Immediate(SDNode *N);
472
473     /// getInsertVINSERT128Immediate - Return the appropriate
474     /// immediate to insert at the specified INSERT_SUBVECTOR index
475     /// with VINSERTF128, VINSERT128 instructions.
476     unsigned getInsertVINSERT128Immediate(SDNode *N);
477
478     /// getExtractVEXTRACT256Immediate - Return the appropriate
479     /// immediate to extract the specified EXTRACT_SUBVECTOR index
480     /// with VEXTRACTF64X4, VEXTRACTI64x4 instructions.
481     unsigned getExtractVEXTRACT256Immediate(SDNode *N);
482
483     /// getInsertVINSERT256Immediate - Return the appropriate
484     /// immediate to insert at the specified INSERT_SUBVECTOR index
485     /// with VINSERTF64x4, VINSERTI64x4 instructions.
486     unsigned getInsertVINSERT256Immediate(SDNode *N);
487
488     /// isZeroNode - Returns true if Elt is a constant zero or a floating point
489     /// constant +0.0.
490     bool isZeroNode(SDValue Elt);
491
492     /// isOffsetSuitableForCodeModel - Returns true of the given offset can be
493     /// fit into displacement field of the instruction.
494     bool isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
495                                       bool hasSymbolicDisplacement = true);
496
497
498     /// isCalleePop - Determines whether the callee is required to pop its
499     /// own arguments. Callee pop is necessary to support tail calls.
500     bool isCalleePop(CallingConv::ID CallingConv,
501                      bool is64Bit, bool IsVarArg, bool TailCallOpt);
502   }
503
504   //===--------------------------------------------------------------------===//
505   //  X86TargetLowering - X86 Implementation of the TargetLowering interface
506   class X86TargetLowering : public TargetLowering {
507   public:
508     explicit X86TargetLowering(X86TargetMachine &TM);
509
510     virtual unsigned getJumpTableEncoding() const;
511
512     virtual MVT getScalarShiftAmountTy(EVT LHSTy) const { return MVT::i8; }
513
514     virtual const MCExpr *
515     LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
516                               const MachineBasicBlock *MBB, unsigned uid,
517                               MCContext &Ctx) const;
518
519     /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
520     /// jumptable.
521     virtual SDValue getPICJumpTableRelocBase(SDValue Table,
522                                              SelectionDAG &DAG) const;
523     virtual const MCExpr *
524     getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
525                                  unsigned JTI, MCContext &Ctx) const;
526
527     /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
528     /// function arguments in the caller parameter area. For X86, aggregates
529     /// that contains are placed at 16-byte boundaries while the rest are at
530     /// 4-byte boundaries.
531     virtual unsigned getByValTypeAlignment(Type *Ty) const;
532
533     /// getOptimalMemOpType - Returns the target specific optimal type for load
534     /// and store operations as a result of memset, memcpy, and memmove
535     /// lowering. If DstAlign is zero that means it's safe to destination
536     /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
537     /// means there isn't a need to check it against alignment requirement,
538     /// probably because the source does not need to be loaded. If 'IsMemset' is
539     /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
540     /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
541     /// source is constant so it does not need to be loaded.
542     /// It returns EVT::Other if the type should be determined using generic
543     /// target-independent logic.
544     virtual EVT
545     getOptimalMemOpType(uint64_t Size, unsigned DstAlign, unsigned SrcAlign,
546                         bool IsMemset, bool ZeroMemset, bool MemcpyStrSrc,
547                         MachineFunction &MF) const;
548
549     /// isSafeMemOpType - Returns true if it's safe to use load / store of the
550     /// specified type to expand memcpy / memset inline. This is mostly true
551     /// for all types except for some special cases. For example, on X86
552     /// targets without SSE2 f64 load / store are done with fldl / fstpl which
553     /// also does type conversion. Note the specified type doesn't have to be
554     /// legal as the hook is used before type legalization.
555     virtual bool isSafeMemOpType(MVT VT) const;
556
557     /// allowsUnalignedMemoryAccesses - Returns true if the target allows
558     /// unaligned memory accesses. of the specified type. Returns whether it
559     /// is "fast" by reference in the second argument.
560     virtual bool allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const;
561
562     /// LowerOperation - Provide custom lowering hooks for some operations.
563     ///
564     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
565
566     /// ReplaceNodeResults - Replace the results of node with an illegal result
567     /// type with new values built out of custom code.
568     ///
569     virtual void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue>&Results,
570                                     SelectionDAG &DAG) const;
571
572
573     virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
574
575     /// isTypeDesirableForOp - Return true if the target has native support for
576     /// the specified value type and it is 'desirable' to use the type for the
577     /// given node type. e.g. On x86 i16 is legal, but undesirable since i16
578     /// instruction encodings are longer and some i16 instructions are slow.
579     virtual bool isTypeDesirableForOp(unsigned Opc, EVT VT) const;
580
581     /// isTypeDesirable - Return true if the target has native support for the
582     /// specified value type and it is 'desirable' to use the type. e.g. On x86
583     /// i16 is legal, but undesirable since i16 instruction encodings are longer
584     /// and some i16 instructions are slow.
585     virtual bool IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const;
586
587     virtual MachineBasicBlock *
588       EmitInstrWithCustomInserter(MachineInstr *MI,
589                                   MachineBasicBlock *MBB) const;
590
591
592     /// getTargetNodeName - This method returns the name of a target specific
593     /// DAG node.
594     virtual const char *getTargetNodeName(unsigned Opcode) const;
595
596     /// getSetCCResultType - Return the value type to use for ISD::SETCC.
597     virtual EVT getSetCCResultType(LLVMContext &Context, EVT VT) const;
598
599     /// computeMaskedBitsForTargetNode - Determine which of the bits specified
600     /// in Mask are known to be either zero or one and return them in the
601     /// KnownZero/KnownOne bitsets.
602     virtual void computeMaskedBitsForTargetNode(const SDValue Op,
603                                                 APInt &KnownZero,
604                                                 APInt &KnownOne,
605                                                 const SelectionDAG &DAG,
606                                                 unsigned Depth = 0) const;
607
608     // ComputeNumSignBitsForTargetNode - Determine the number of bits in the
609     // operation that are sign bits.
610     virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op,
611                                                      unsigned Depth) const;
612
613     virtual bool
614     isGAPlusOffset(SDNode *N, const GlobalValue* &GA, int64_t &Offset) const;
615
616     SDValue getReturnAddressFrameIndex(SelectionDAG &DAG) const;
617
618     virtual bool ExpandInlineAsm(CallInst *CI) const;
619
620     ConstraintType getConstraintType(const std::string &Constraint) const;
621
622     /// Examine constraint string and operand type and determine a weight value.
623     /// The operand object must already have been set up with the operand type.
624     virtual ConstraintWeight getSingleConstraintMatchWeight(
625       AsmOperandInfo &info, const char *constraint) const;
626
627     virtual const char *LowerXConstraint(EVT ConstraintVT) const;
628
629     /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
630     /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is
631     /// true it means one of the asm constraint of the inline asm instruction
632     /// being processed is 'm'.
633     virtual void LowerAsmOperandForConstraint(SDValue Op,
634                                               std::string &Constraint,
635                                               std::vector<SDValue> &Ops,
636                                               SelectionDAG &DAG) const;
637
638     /// getRegForInlineAsmConstraint - Given a physical register constraint
639     /// (e.g. {edx}), return the register number and the register class for the
640     /// register.  This should only be used for C_Register constraints.  On
641     /// error, this returns a register number of 0.
642     std::pair<unsigned, const TargetRegisterClass*>
643       getRegForInlineAsmConstraint(const std::string &Constraint,
644                                    MVT VT) const;
645
646     /// isLegalAddressingMode - Return true if the addressing mode represented
647     /// by AM is legal for this target, for a load/store of the specified type.
648     virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty)const;
649
650     /// isLegalICmpImmediate - Return true if the specified immediate is legal
651     /// icmp immediate, that is the target has icmp instructions which can
652     /// compare a register against the immediate without having to materialize
653     /// the immediate into a register.
654     virtual bool isLegalICmpImmediate(int64_t Imm) const;
655
656     /// isLegalAddImmediate - Return true if the specified immediate is legal
657     /// add immediate, that is the target has add instructions which can
658     /// add a register and the immediate without having to materialize
659     /// the immediate into a register.
660     virtual bool isLegalAddImmediate(int64_t Imm) const;
661
662     /// isTruncateFree - Return true if it's free to truncate a value of
663     /// type Ty1 to type Ty2. e.g. On x86 it's free to truncate a i32 value in
664     /// register EAX to i16 by referencing its sub-register AX.
665     virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
666     virtual bool isTruncateFree(EVT VT1, EVT VT2) const;
667
668     virtual bool allowTruncateForTailCall(Type *Ty1, Type *Ty2) const;
669
670     /// isZExtFree - Return true if any actual instruction that defines a
671     /// value of type Ty1 implicit zero-extends the value to Ty2 in the result
672     /// register. This does not necessarily include registers defined in
673     /// unknown ways, such as incoming arguments, or copies from unknown
674     /// virtual registers. Also, if isTruncateFree(Ty2, Ty1) is true, this
675     /// does not necessarily apply to truncate instructions. e.g. on x86-64,
676     /// all instructions that define 32-bit values implicit zero-extend the
677     /// result out to 64 bits.
678     virtual bool isZExtFree(Type *Ty1, Type *Ty2) const;
679     virtual bool isZExtFree(EVT VT1, EVT VT2) const;
680     virtual bool isZExtFree(SDValue Val, EVT VT2) const;
681
682     /// isFMAFasterThanFMulAndFAdd - Return true if an FMA operation is faster
683     /// than a pair of fmul and fadd instructions. fmuladd intrinsics will be
684     /// expanded to FMAs when this method returns true, otherwise fmuladd is
685     /// expanded to fmul + fadd.
686     virtual bool isFMAFasterThanFMulAndFAdd(EVT VT) const;
687
688     /// isNarrowingProfitable - Return true if it's profitable to narrow
689     /// operations of type VT1 to VT2. e.g. on x86, it's profitable to narrow
690     /// from i32 to i8 but not from i32 to i16.
691     virtual bool isNarrowingProfitable(EVT VT1, EVT VT2) const;
692
693     /// isFPImmLegal - Returns true if the target can instruction select the
694     /// specified FP immediate natively. If false, the legalizer will
695     /// materialize the FP immediate as a load from a constant pool.
696     virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const;
697
698     /// isShuffleMaskLegal - Targets can use this to indicate that they only
699     /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
700     /// By default, if a target supports the VECTOR_SHUFFLE node, all mask
701     /// values are assumed to be legal.
702     virtual bool isShuffleMaskLegal(const SmallVectorImpl<int> &Mask,
703                                     EVT VT) const;
704
705     /// isVectorClearMaskLegal - Similar to isShuffleMaskLegal. This is
706     /// used by Targets can use this to indicate if there is a suitable
707     /// VECTOR_SHUFFLE that can be used to replace a VAND with a constant
708     /// pool entry.
709     virtual bool isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
710                                         EVT VT) const;
711
712     /// ShouldShrinkFPConstant - If true, then instruction selection should
713     /// seek to shrink the FP constant of the specified type to a smaller type
714     /// in order to save space and / or reduce runtime.
715     virtual bool ShouldShrinkFPConstant(EVT VT) const {
716       // Don't shrink FP constpool if SSE2 is available since cvtss2sd is more
717       // expensive than a straight movsd. On the other hand, it's important to
718       // shrink long double fp constant since fldt is very slow.
719       return !X86ScalarSSEf64 || VT == MVT::f80;
720     }
721
722     const X86Subtarget* getSubtarget() const {
723       return Subtarget;
724     }
725
726     /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
727     /// computed in an SSE register, not on the X87 floating point stack.
728     bool isScalarFPTypeInSSEReg(EVT VT) const {
729       return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
730       (VT == MVT::f32 && X86ScalarSSEf32);   // f32 is when SSE1
731     }
732
733     /// isTargetFTOL - Return true if the target uses the MSVC _ftol2 routine
734     /// for fptoui.
735     bool isTargetFTOL() const {
736       return Subtarget->isTargetWindows() && !Subtarget->is64Bit();
737     }
738
739     /// isIntegerTypeFTOL - Return true if the MSVC _ftol2 routine should be
740     /// used for fptoui to the given type.
741     bool isIntegerTypeFTOL(EVT VT) const {
742       return isTargetFTOL() && VT == MVT::i64;
743     }
744
745     /// createFastISel - This method returns a target specific FastISel object,
746     /// or null if the target does not support "fast" ISel.
747     virtual FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
748                                      const TargetLibraryInfo *libInfo) const;
749
750     /// getStackCookieLocation - Return true if the target stores stack
751     /// protector cookies at a fixed offset in some non-standard address
752     /// space, and populates the address space and offset as
753     /// appropriate.
754     virtual bool getStackCookieLocation(unsigned &AddressSpace, unsigned &Offset) const;
755
756     SDValue BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain, SDValue StackSlot,
757                       SelectionDAG &DAG) const;
758
759     /// \brief Reset the operation actions based on target options.
760     virtual void resetOperationActions();
761
762   protected:
763     std::pair<const TargetRegisterClass*, uint8_t>
764     findRepresentativeClass(MVT VT) const;
765
766   private:
767     /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
768     /// make the right decision when generating code for different targets.
769     const X86Subtarget *Subtarget;
770     const DataLayout *TD;
771
772     /// Used to store the TargetOptions so that we don't waste time resetting
773     /// the operation actions unless we have to.
774     TargetOptions TO;
775
776     /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
777     /// floating point ops.
778     /// When SSE is available, use it for f32 operations.
779     /// When SSE2 is available, use it for f64 operations.
780     bool X86ScalarSSEf32;
781     bool X86ScalarSSEf64;
782
783     /// LegalFPImmediates - A list of legal fp immediates.
784     std::vector<APFloat> LegalFPImmediates;
785
786     /// addLegalFPImmediate - Indicate that this x86 target can instruction
787     /// select the specified FP immediate natively.
788     void addLegalFPImmediate(const APFloat& Imm) {
789       LegalFPImmediates.push_back(Imm);
790     }
791
792     SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
793                             CallingConv::ID CallConv, bool isVarArg,
794                             const SmallVectorImpl<ISD::InputArg> &Ins,
795                             SDLoc dl, SelectionDAG &DAG,
796                             SmallVectorImpl<SDValue> &InVals) const;
797     SDValue LowerMemArgument(SDValue Chain,
798                              CallingConv::ID CallConv,
799                              const SmallVectorImpl<ISD::InputArg> &ArgInfo,
800                              SDLoc dl, SelectionDAG &DAG,
801                              const CCValAssign &VA,  MachineFrameInfo *MFI,
802                               unsigned i) const;
803     SDValue LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, SDValue Arg,
804                              SDLoc dl, SelectionDAG &DAG,
805                              const CCValAssign &VA,
806                              ISD::ArgFlagsTy Flags) const;
807
808     // Call lowering helpers.
809
810     /// IsEligibleForTailCallOptimization - Check whether the call is eligible
811     /// for tail call optimization. Targets which want to do tail call
812     /// optimization should implement this function.
813     bool IsEligibleForTailCallOptimization(SDValue Callee,
814                                            CallingConv::ID CalleeCC,
815                                            bool isVarArg,
816                                            bool isCalleeStructRet,
817                                            bool isCallerStructRet,
818                                            Type *RetTy,
819                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
820                                     const SmallVectorImpl<SDValue> &OutVals,
821                                     const SmallVectorImpl<ISD::InputArg> &Ins,
822                                            SelectionDAG& DAG) const;
823     bool IsCalleePop(bool isVarArg, CallingConv::ID CallConv) const;
824     SDValue EmitTailCallLoadRetAddr(SelectionDAG &DAG, SDValue &OutRetAddr,
825                                 SDValue Chain, bool IsTailCall, bool Is64Bit,
826                                 int FPDiff, SDLoc dl) const;
827
828     unsigned GetAlignedArgumentStackSize(unsigned StackSize,
829                                          SelectionDAG &DAG) const;
830
831     std::pair<SDValue,SDValue> FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
832                                                bool isSigned,
833                                                bool isReplace) const;
834
835     SDValue LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, SDLoc dl,
836                                    SelectionDAG &DAG) const;
837     SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
838     SDValue LowerBUILD_VECTORvXi1(SDValue Op, SelectionDAG &DAG) const;
839     SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
840     SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
841     SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
842     SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
843     SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
844     SDValue LowerGlobalAddress(const GlobalValue *GV, SDLoc dl,
845                                int64_t Offset, SelectionDAG &DAG) const;
846     SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
847     SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
848     SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const;
849     SDValue LowerShiftParts(SDValue Op, SelectionDAG &DAG) const;
850     SDValue LowerBITCAST(SDValue op, SelectionDAG &DAG) const;
851     SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
852     SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
853     SDValue LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) const;
854     SDValue LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) const;
855     SDValue lowerUINT_TO_FP_vec(SDValue Op, SelectionDAG &DAG) const;
856     SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const;
857     SDValue LowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const;
858     SDValue LowerZERO_EXTEND_AVX512(SDValue Op, SelectionDAG &DAG) const;
859     SDValue LowerSIGN_EXTEND(SDValue Op, SelectionDAG &DAG) const;
860     SDValue LowerSIGN_EXTEND_AVX512(SDValue Op, SelectionDAG &DAG) const;
861     SDValue LowerANY_EXTEND(SDValue Op, SelectionDAG &DAG) const;
862     SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const;
863     SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const;
864     SDValue LowerFABS(SDValue Op, SelectionDAG &DAG) const;
865     SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG) const;
866     SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
867     SDValue LowerToBT(SDValue And, ISD::CondCode CC,
868                       SDLoc dl, SelectionDAG &DAG) const;
869     SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
870     SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
871     SDValue LowerBRCOND(SDValue Op, SelectionDAG &DAG) const;
872     SDValue LowerMEMSET(SDValue Op, SelectionDAG &DAG) const;
873     SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
874     SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
875     SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
876     SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) const;
877     SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
878     SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
879     SDValue LowerFRAME_TO_ARGS_OFFSET(SDValue Op, SelectionDAG &DAG) const;
880     SDValue LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const;
881     SDValue lowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const;
882     SDValue lowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const;
883     SDValue LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
884     SDValue LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) const;
885     SDValue LowerShift(SDValue Op, SelectionDAG &DAG) const;
886     SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) const;
887     SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
888     SDValue LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const;
889
890     // Utility functions to help LowerVECTOR_SHUFFLE & LowerBUILD_VECTOR
891     SDValue LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const;
892     SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const;
893     SDValue buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const;
894
895     SDValue LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const;
896
897     SDValue LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const;
898
899     virtual SDValue
900       LowerFormalArguments(SDValue Chain,
901                            CallingConv::ID CallConv, bool isVarArg,
902                            const SmallVectorImpl<ISD::InputArg> &Ins,
903                            SDLoc dl, SelectionDAG &DAG,
904                            SmallVectorImpl<SDValue> &InVals) const;
905     virtual SDValue
906       LowerCall(CallLoweringInfo &CLI,
907                 SmallVectorImpl<SDValue> &InVals) const;
908
909     virtual SDValue
910       LowerReturn(SDValue Chain,
911                   CallingConv::ID CallConv, bool isVarArg,
912                   const SmallVectorImpl<ISD::OutputArg> &Outs,
913                   const SmallVectorImpl<SDValue> &OutVals,
914                   SDLoc dl, SelectionDAG &DAG) const;
915
916     virtual bool isUsedByReturnOnly(SDNode *N, SDValue &Chain) const;
917
918     virtual bool mayBeEmittedAsTailCall(CallInst *CI) const;
919
920     virtual MVT
921     getTypeForExtArgOrReturn(MVT VT, ISD::NodeType ExtendKind) const;
922
923     virtual bool
924     CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
925                    bool isVarArg,
926                    const SmallVectorImpl<ISD::OutputArg> &Outs,
927                    LLVMContext &Context) const;
928
929     /// Utility function to emit atomic-load-arith operations (and, or, xor,
930     /// nand, max, min, umax, umin). It takes the corresponding instruction to
931     /// expand, the associated machine basic block, and the associated X86
932     /// opcodes for reg/reg.
933     MachineBasicBlock *EmitAtomicLoadArith(MachineInstr *MI,
934                                            MachineBasicBlock *MBB) const;
935
936     /// Utility function to emit atomic-load-arith operations (and, or, xor,
937     /// nand, add, sub, swap) for 64-bit operands on 32-bit target.
938     MachineBasicBlock *EmitAtomicLoadArith6432(MachineInstr *MI,
939                                                MachineBasicBlock *MBB) const;
940
941     // Utility function to emit the low-level va_arg code for X86-64.
942     MachineBasicBlock *EmitVAARG64WithCustomInserter(
943                        MachineInstr *MI,
944                        MachineBasicBlock *MBB) const;
945
946     /// Utility function to emit the xmm reg save portion of va_start.
947     MachineBasicBlock *EmitVAStartSaveXMMRegsWithCustomInserter(
948                                                    MachineInstr *BInstr,
949                                                    MachineBasicBlock *BB) const;
950
951     MachineBasicBlock *EmitLoweredSelect(MachineInstr *I,
952                                          MachineBasicBlock *BB) const;
953
954     MachineBasicBlock *EmitLoweredWinAlloca(MachineInstr *MI,
955                                               MachineBasicBlock *BB) const;
956
957     MachineBasicBlock *EmitLoweredSegAlloca(MachineInstr *MI,
958                                             MachineBasicBlock *BB,
959                                             bool Is64Bit) const;
960
961     MachineBasicBlock *EmitLoweredTLSCall(MachineInstr *MI,
962                                           MachineBasicBlock *BB) const;
963
964     MachineBasicBlock *emitLoweredTLSAddr(MachineInstr *MI,
965                                           MachineBasicBlock *BB) const;
966
967     MachineBasicBlock *emitEHSjLjSetJmp(MachineInstr *MI,
968                                         MachineBasicBlock *MBB) const;
969
970     MachineBasicBlock *emitEHSjLjLongJmp(MachineInstr *MI,
971                                          MachineBasicBlock *MBB) const;
972
973     /// Emit nodes that will be selected as "test Op0,Op0", or something
974     /// equivalent, for use with the given x86 condition code.
975     SDValue EmitTest(SDValue Op0, unsigned X86CC, SelectionDAG &DAG) const;
976
977     /// Emit nodes that will be selected as "cmp Op0,Op1", or something
978     /// equivalent, for use with the given x86 condition code.
979     SDValue EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
980                     SelectionDAG &DAG) const;
981
982     /// Convert a comparison if required by the subtarget.
983     SDValue ConvertCmpIfNecessary(SDValue Cmp, SelectionDAG &DAG) const;
984   };
985
986   namespace X86 {
987     FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
988                              const TargetLibraryInfo *libInfo);
989   }
990 }
991
992 #endif    // X86ISELLOWERING_H