Improve varags handling, with testcases. Patch by Sasa Stankovic
[oota-llvm.git] / lib / Target / ARM / ARMISelLowering.h
1 //===-- ARMISelLowering.h - ARM 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 ARM uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef ARMISELLOWERING_H
16 #define ARMISELLOWERING_H
17
18 #include "ARMSubtarget.h"
19 #include "llvm/Target/TargetLowering.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/CodeGen/FastISel.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include <vector>
25
26 namespace llvm {
27   class ARMConstantPoolValue;
28
29   namespace ARMISD {
30     // ARM Specific DAG Nodes
31     enum NodeType {
32       // Start the numbering where the builtin ops and target ops leave off.
33       FIRST_NUMBER = ISD::BUILTIN_OP_END,
34
35       Wrapper,      // Wrapper - A wrapper node for TargetConstantPool,
36                     // TargetExternalSymbol, and TargetGlobalAddress.
37       WrapperDYN,   // WrapperDYN - A wrapper node for TargetGlobalAddress in
38                     // DYN mode.
39       WrapperPIC,   // WrapperPIC - A wrapper node for TargetGlobalAddress in
40                     // PIC mode.
41       WrapperJT,    // WrapperJT - A wrapper node for TargetJumpTable
42
43       CALL,         // Function call.
44       CALL_PRED,    // Function call that's predicable.
45       CALL_NOLINK,  // Function call with branch not branch-and-link.
46       tCALL,        // Thumb function call.
47       BRCOND,       // Conditional branch.
48       BR_JT,        // Jumptable branch.
49       BR2_JT,       // Jumptable branch (2 level - jumptable entry is a jump).
50       RET_FLAG,     // Return with a flag operand.
51
52       PIC_ADD,      // Add with a PC operand and a PIC label.
53
54       CMP,          // ARM compare instructions.
55       CMPZ,         // ARM compare that sets only Z flag.
56       CMPFP,        // ARM VFP compare instruction, sets FPSCR.
57       CMPFPw0,      // ARM VFP compare against zero instruction, sets FPSCR.
58       FMSTAT,       // ARM fmstat instruction.
59       CMOV,         // ARM conditional move instructions.
60
61       BCC_i64,
62
63       RBIT,         // ARM bitreverse instruction
64
65       FTOSI,        // FP to sint within a FP register.
66       FTOUI,        // FP to uint within a FP register.
67       SITOF,        // sint to FP within a FP register.
68       UITOF,        // uint to FP within a FP register.
69
70       SRL_FLAG,     // V,Flag = srl_flag X -> srl X, 1 + save carry out.
71       SRA_FLAG,     // V,Flag = sra_flag X -> sra X, 1 + save carry out.
72       RRX,          // V = RRX X, Flag     -> srl X, 1 + shift in carry flag.
73
74       VMOVRRD,      // double to two gprs.
75       VMOVDRR,      // Two gprs to double.
76
77       EH_SJLJ_SETJMP,         // SjLj exception handling setjmp.
78       EH_SJLJ_LONGJMP,        // SjLj exception handling longjmp.
79       EH_SJLJ_DISPATCHSETUP,  // SjLj exception handling dispatch setup.
80
81       TC_RETURN,    // Tail call return pseudo.
82
83       THREAD_POINTER,
84
85       DYN_ALLOC,    // Dynamic allocation on the stack.
86
87       MEMBARRIER,   // Memory barrier (DMB)
88       MEMBARRIER_MCR, // Memory barrier (MCR)
89
90       PRELOAD,      // Preload
91       
92       VCEQ,         // Vector compare equal.
93       VCEQZ,        // Vector compare equal to zero.
94       VCGE,         // Vector compare greater than or equal.
95       VCGEZ,        // Vector compare greater than or equal to zero.
96       VCLEZ,        // Vector compare less than or equal to zero.
97       VCGEU,        // Vector compare unsigned greater than or equal.
98       VCGT,         // Vector compare greater than.
99       VCGTZ,        // Vector compare greater than zero.
100       VCLTZ,        // Vector compare less than zero.
101       VCGTU,        // Vector compare unsigned greater than.
102       VTST,         // Vector test bits.
103
104       // Vector shift by immediate:
105       VSHL,         // ...left
106       VSHRs,        // ...right (signed)
107       VSHRu,        // ...right (unsigned)
108       VSHLLs,       // ...left long (signed)
109       VSHLLu,       // ...left long (unsigned)
110       VSHLLi,       // ...left long (with maximum shift count)
111       VSHRN,        // ...right narrow
112
113       // Vector rounding shift by immediate:
114       VRSHRs,       // ...right (signed)
115       VRSHRu,       // ...right (unsigned)
116       VRSHRN,       // ...right narrow
117
118       // Vector saturating shift by immediate:
119       VQSHLs,       // ...left (signed)
120       VQSHLu,       // ...left (unsigned)
121       VQSHLsu,      // ...left (signed to unsigned)
122       VQSHRNs,      // ...right narrow (signed)
123       VQSHRNu,      // ...right narrow (unsigned)
124       VQSHRNsu,     // ...right narrow (signed to unsigned)
125
126       // Vector saturating rounding shift by immediate:
127       VQRSHRNs,     // ...right narrow (signed)
128       VQRSHRNu,     // ...right narrow (unsigned)
129       VQRSHRNsu,    // ...right narrow (signed to unsigned)
130
131       // Vector shift and insert:
132       VSLI,         // ...left
133       VSRI,         // ...right
134
135       // Vector get lane (VMOV scalar to ARM core register)
136       // (These are used for 8- and 16-bit element types only.)
137       VGETLANEu,    // zero-extend vector extract element
138       VGETLANEs,    // sign-extend vector extract element
139
140       // Vector move immediate and move negated immediate:
141       VMOVIMM,
142       VMVNIMM,
143
144       // Vector duplicate:
145       VDUP,
146       VDUPLANE,
147
148       // Vector shuffles:
149       VEXT,         // extract
150       VREV64,       // reverse elements within 64-bit doublewords
151       VREV32,       // reverse elements within 32-bit words
152       VREV16,       // reverse elements within 16-bit halfwords
153       VZIP,         // zip (interleave)
154       VUZP,         // unzip (deinterleave)
155       VTRN,         // transpose
156
157       // Vector multiply long:
158       VMULLs,       // ...signed
159       VMULLu,       // ...unsigned
160
161       // Operands of the standard BUILD_VECTOR node are not legalized, which
162       // is fine if BUILD_VECTORs are always lowered to shuffles or other
163       // operations, but for ARM some BUILD_VECTORs are legal as-is and their
164       // operands need to be legalized.  Define an ARM-specific version of
165       // BUILD_VECTOR for this purpose.
166       BUILD_VECTOR,
167
168       // Floating-point max and min:
169       FMAX,
170       FMIN,
171
172       // Bit-field insert
173       BFI,
174       
175       // Vector OR with immediate
176       VORRIMM,
177       // Vector AND with NOT of immediate
178       VBICIMM,
179
180       // Vector load N-element structure to all lanes:
181       VLD2DUP = ISD::FIRST_TARGET_MEMORY_OPCODE,
182       VLD3DUP,
183       VLD4DUP,
184
185       // NEON loads with post-increment base updates:
186       VLD1_UPD,
187       VLD2_UPD,
188       VLD3_UPD,
189       VLD4_UPD,
190       VLD2LN_UPD,
191       VLD3LN_UPD,
192       VLD4LN_UPD,
193       VLD2DUP_UPD,
194       VLD3DUP_UPD,
195       VLD4DUP_UPD,
196
197       // NEON stores with post-increment base updates:
198       VST1_UPD,
199       VST2_UPD,
200       VST3_UPD,
201       VST4_UPD,
202       VST2LN_UPD,
203       VST3LN_UPD,
204       VST4LN_UPD
205     };
206   }
207
208   /// Define some predicates that are used for node matching.
209   namespace ARM {
210     /// getVFPf32Imm / getVFPf64Imm - If the given fp immediate can be
211     /// materialized with a VMOV.f32 / VMOV.f64 (i.e. fconsts / fconstd)
212     /// instruction, returns its 8-bit integer representation. Otherwise,
213     /// returns -1.
214     int getVFPf32Imm(const APFloat &FPImm);
215     int getVFPf64Imm(const APFloat &FPImm);
216     bool isBitFieldInvertedMask(unsigned v);
217   }
218
219   //===--------------------------------------------------------------------===//
220   //  ARMTargetLowering - ARM Implementation of the TargetLowering interface
221
222   class ARMTargetLowering : public TargetLowering {
223   public:
224     explicit ARMTargetLowering(TargetMachine &TM);
225
226     virtual unsigned getJumpTableEncoding(void) const;
227
228     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
229
230     /// ReplaceNodeResults - Replace the results of node with an illegal result
231     /// type with new values built out of custom code.
232     ///
233     virtual void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue>&Results,
234                                     SelectionDAG &DAG) const;
235
236     virtual const char *getTargetNodeName(unsigned Opcode) const;
237
238     virtual MachineBasicBlock *
239       EmitInstrWithCustomInserter(MachineInstr *MI,
240                                   MachineBasicBlock *MBB) const;
241
242     virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
243
244     bool isDesirableToTransformToIntegerOp(unsigned Opc, EVT VT) const;
245
246     /// allowsUnalignedMemoryAccesses - Returns true if the target allows
247     /// unaligned memory accesses. of the specified type.
248     /// FIXME: Add getOptimalMemOpType to implement memcpy with NEON?
249     virtual bool allowsUnalignedMemoryAccesses(EVT VT) const;
250
251     /// isLegalAddressingMode - Return true if the addressing mode represented
252     /// by AM is legal for this target, for a load/store of the specified type.
253     virtual bool isLegalAddressingMode(const AddrMode &AM, const Type *Ty)const;
254     bool isLegalT2ScaledAddressingMode(const AddrMode &AM, EVT VT) const;
255
256     /// isLegalICmpImmediate - Return true if the specified immediate is legal
257     /// icmp immediate, that is the target has icmp instructions which can
258     /// compare a register against the immediate without having to materialize
259     /// the immediate into a register.
260     virtual bool isLegalICmpImmediate(int64_t Imm) const;
261
262     /// getPreIndexedAddressParts - returns true by value, base pointer and
263     /// offset pointer and addressing mode by reference if the node's address
264     /// can be legally represented as pre-indexed load / store address.
265     virtual bool getPreIndexedAddressParts(SDNode *N, SDValue &Base,
266                                            SDValue &Offset,
267                                            ISD::MemIndexedMode &AM,
268                                            SelectionDAG &DAG) const;
269
270     /// getPostIndexedAddressParts - returns true by value, base pointer and
271     /// offset pointer and addressing mode by reference if this node can be
272     /// combined with a load / store to form a post-indexed load / store.
273     virtual bool getPostIndexedAddressParts(SDNode *N, SDNode *Op,
274                                             SDValue &Base, SDValue &Offset,
275                                             ISD::MemIndexedMode &AM,
276                                             SelectionDAG &DAG) const;
277
278     virtual void computeMaskedBitsForTargetNode(const SDValue Op,
279                                                 const APInt &Mask,
280                                                 APInt &KnownZero,
281                                                 APInt &KnownOne,
282                                                 const SelectionDAG &DAG,
283                                                 unsigned Depth) const;
284
285
286     virtual bool ExpandInlineAsm(CallInst *CI) const;
287
288     ConstraintType getConstraintType(const std::string &Constraint) const;
289
290     /// Examine constraint string and operand type and determine a weight value.
291     /// The operand object must already have been set up with the operand type.
292     ConstraintWeight getSingleConstraintMatchWeight(
293       AsmOperandInfo &info, const char *constraint) const;
294
295     std::pair<unsigned, const TargetRegisterClass*>
296       getRegForInlineAsmConstraint(const std::string &Constraint,
297                                    EVT VT) const;
298     std::vector<unsigned>
299     getRegClassForInlineAsmConstraint(const std::string &Constraint,
300                                       EVT VT) const;
301
302     /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
303     /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is
304     /// true it means one of the asm constraint of the inline asm instruction
305     /// being processed is 'm'.
306     virtual void LowerAsmOperandForConstraint(SDValue Op,
307                                               char ConstraintLetter,
308                                               std::vector<SDValue> &Ops,
309                                               SelectionDAG &DAG) const;
310
311     const ARMSubtarget* getSubtarget() const {
312       return Subtarget;
313     }
314
315     /// getRegClassFor - Return the register class that should be used for the
316     /// specified value type.
317     virtual TargetRegisterClass *getRegClassFor(EVT VT) const;
318
319     /// getFunctionAlignment - Return the Log2 alignment of this function.
320     virtual unsigned getFunctionAlignment(const Function *F) const;
321
322     /// getMaximalGlobalOffset - Returns the maximal possible offset which can
323     /// be used for loads / stores from the global.
324     virtual unsigned getMaximalGlobalOffset() const;
325
326     /// createFastISel - This method returns a target specific FastISel object,
327     /// or null if the target does not support "fast" ISel.
328     virtual FastISel *createFastISel(FunctionLoweringInfo &funcInfo) const;
329
330     Sched::Preference getSchedulingPreference(SDNode *N) const;
331
332     bool isShuffleMaskLegal(const SmallVectorImpl<int> &M, EVT VT) const;
333     bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
334
335     /// isFPImmLegal - Returns true if the target can instruction select the
336     /// specified FP immediate natively. If false, the legalizer will
337     /// materialize the FP immediate as a load from a constant pool.
338     virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const;
339
340     virtual bool getTgtMemIntrinsic(IntrinsicInfo &Info,
341                                     const CallInst &I,
342                                     unsigned Intrinsic) const;
343   protected:
344     std::pair<const TargetRegisterClass*, uint8_t>
345     findRepresentativeClass(EVT VT) const;
346
347   private:
348     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
349     /// make the right decision when generating code for different targets.
350     const ARMSubtarget *Subtarget;
351
352     const TargetRegisterInfo *RegInfo;
353
354     const InstrItineraryData *Itins;
355
356     /// ARMPCLabelIndex - Keep track of the number of ARM PC labels created.
357     ///
358     unsigned ARMPCLabelIndex;
359
360     void addTypeForNEON(EVT VT, EVT PromotedLdStVT, EVT PromotedBitwiseVT);
361     void addDRTypeForNEON(EVT VT);
362     void addQRTypeForNEON(EVT VT);
363
364     typedef SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPassVector;
365     void PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
366                           SDValue Chain, SDValue &Arg,
367                           RegsToPassVector &RegsToPass,
368                           CCValAssign &VA, CCValAssign &NextVA,
369                           SDValue &StackPtr,
370                           SmallVector<SDValue, 8> &MemOpChains,
371                           ISD::ArgFlagsTy Flags) const;
372     SDValue GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
373                                  SDValue &Root, SelectionDAG &DAG,
374                                  DebugLoc dl) const;
375
376     CCAssignFn *CCAssignFnForNode(CallingConv::ID CC, bool Return,
377                                   bool isVarArg) const;
378     SDValue LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, SDValue Arg,
379                              DebugLoc dl, SelectionDAG &DAG,
380                              const CCValAssign &VA,
381                              ISD::ArgFlagsTy Flags) const;
382     SDValue LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const;
383     SDValue LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const;
384     SDValue LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG) const;
385     SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
386                                     const ARMSubtarget *Subtarget) const;
387     SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
388     SDValue LowerGlobalAddressDarwin(SDValue Op, SelectionDAG &DAG) const;
389     SDValue LowerGlobalAddressELF(SDValue Op, SelectionDAG &DAG) const;
390     SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
391     SDValue LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
392                                             SelectionDAG &DAG) const;
393     SDValue LowerToTLSExecModels(GlobalAddressSDNode *GA,
394                                    SelectionDAG &DAG) const;
395     SDValue LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG) const;
396     SDValue LowerBR_JT(SDValue Op, SelectionDAG &DAG) const;
397     SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
398     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
399     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
400     SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
401     SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
402     SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
403     SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) const;
404     SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
405     SDValue LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) const;
406     SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 
407                               const ARMSubtarget *ST) const;
408
409     SDValue ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const;
410
411     SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
412                             CallingConv::ID CallConv, bool isVarArg,
413                             const SmallVectorImpl<ISD::InputArg> &Ins,
414                             DebugLoc dl, SelectionDAG &DAG,
415                             SmallVectorImpl<SDValue> &InVals) const;
416
417     virtual SDValue
418       LowerFormalArguments(SDValue Chain,
419                            CallingConv::ID CallConv, bool isVarArg,
420                            const SmallVectorImpl<ISD::InputArg> &Ins,
421                            DebugLoc dl, SelectionDAG &DAG,
422                            SmallVectorImpl<SDValue> &InVals) const;
423
424     virtual SDValue
425       LowerCall(SDValue Chain, SDValue Callee,
426                 CallingConv::ID CallConv, bool isVarArg,
427                 bool &isTailCall,
428                 const SmallVectorImpl<ISD::OutputArg> &Outs,
429                 const SmallVectorImpl<SDValue> &OutVals,
430                 const SmallVectorImpl<ISD::InputArg> &Ins,
431                 DebugLoc dl, SelectionDAG &DAG,
432                 SmallVectorImpl<SDValue> &InVals) const;
433
434     /// HandleByVal - Target-specific cleanup for ByVal support.
435     virtual void HandleByVal(CCState *) const;
436
437     /// IsEligibleForTailCallOptimization - Check whether the call is eligible
438     /// for tail call optimization. Targets which want to do tail call
439     /// optimization should implement this function.
440     bool IsEligibleForTailCallOptimization(SDValue Callee,
441                                            CallingConv::ID CalleeCC,
442                                            bool isVarArg,
443                                            bool isCalleeStructRet,
444                                            bool isCallerStructRet,
445                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
446                                     const SmallVectorImpl<SDValue> &OutVals,
447                                     const SmallVectorImpl<ISD::InputArg> &Ins,
448                                            SelectionDAG& DAG) const;
449     virtual SDValue
450       LowerReturn(SDValue Chain,
451                   CallingConv::ID CallConv, bool isVarArg,
452                   const SmallVectorImpl<ISD::OutputArg> &Outs,
453                   const SmallVectorImpl<SDValue> &OutVals,
454                   DebugLoc dl, SelectionDAG &DAG) const;
455
456     virtual bool isUsedByReturnOnly(SDNode *N) const;
457
458     SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
459                       SDValue &ARMcc, SelectionDAG &DAG, DebugLoc dl) const;
460     SDValue getVFPCmp(SDValue LHS, SDValue RHS,
461                       SelectionDAG &DAG, DebugLoc dl) const;
462     SDValue duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const;
463
464     SDValue OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const;
465
466     MachineBasicBlock *EmitAtomicCmpSwap(MachineInstr *MI,
467                                          MachineBasicBlock *BB,
468                                          unsigned Size) const;
469     MachineBasicBlock *EmitAtomicBinary(MachineInstr *MI,
470                                         MachineBasicBlock *BB,
471                                         unsigned Size,
472                                         unsigned BinOpcode) const;
473
474   };
475   
476   enum NEONModImmType {
477     VMOVModImm,
478     VMVNModImm,
479     OtherModImm
480   };
481   
482   
483   namespace ARM {
484     FastISel *createFastISel(FunctionLoweringInfo &funcInfo);
485   }
486 }
487
488 #endif  // ARMISELLOWERING_H