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