This code implements most of mips16 hardfloat as it is done by gcc.
[oota-llvm.git] / lib / Target / Mips / MipsISelLowering.h
1 //===-- MipsISelLowering.h - Mips 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 Mips uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef MipsISELLOWERING_H
16 #define MipsISELLOWERING_H
17
18 #include "Mips.h"
19 #include "MipsSubtarget.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/Target/TargetLowering.h"
23
24 namespace llvm {
25   namespace MipsISD {
26     enum NodeType {
27       // Start the numbering from where ISD NodeType finishes.
28       FIRST_NUMBER = ISD::BUILTIN_OP_END,
29
30       // Jump and link (call)
31       JmpLink,
32
33       // Tail call
34       TailCall,
35
36       // Get the Higher 16 bits from a 32-bit immediate
37       // No relation with Mips Hi register
38       Hi,
39
40       // Get the Lower 16 bits from a 32-bit immediate
41       // No relation with Mips Lo register
42       Lo,
43
44       // Handle gp_rel (small data/bss sections) relocation.
45       GPRel,
46
47       // Thread Pointer
48       ThreadPointer,
49
50       // Floating Point Branch Conditional
51       FPBrcond,
52
53       // Floating Point Compare
54       FPCmp,
55
56       // Floating Point Conditional Moves
57       CMovFP_T,
58       CMovFP_F,
59
60       // Floating Point Rounding
61       FPRound,
62
63       // Return
64       Ret,
65
66       // MAdd/Sub nodes
67       MAdd,
68       MAddu,
69       MSub,
70       MSubu,
71
72       // DivRem(u)
73       DivRem,
74       DivRemU,
75
76       BuildPairF64,
77       ExtractElementF64,
78
79       Wrapper,
80
81       DynAlloc,
82
83       Sync,
84
85       Ext,
86       Ins,
87
88       // EXTR.W instrinsic nodes.
89       EXTP,
90       EXTPDP,
91       EXTR_S_H,
92       EXTR_W,
93       EXTR_R_W,
94       EXTR_RS_W,
95       SHILO,
96       MTHLIP,
97
98       // DPA.W intrinsic nodes.
99       MULSAQ_S_W_PH,
100       MAQ_S_W_PHL,
101       MAQ_S_W_PHR,
102       MAQ_SA_W_PHL,
103       MAQ_SA_W_PHR,
104       DPAU_H_QBL,
105       DPAU_H_QBR,
106       DPSU_H_QBL,
107       DPSU_H_QBR,
108       DPAQ_S_W_PH,
109       DPSQ_S_W_PH,
110       DPAQ_SA_L_W,
111       DPSQ_SA_L_W,
112       DPA_W_PH,
113       DPS_W_PH,
114       DPAQX_S_W_PH,
115       DPAQX_SA_W_PH,
116       DPAX_W_PH,
117       DPSX_W_PH,
118       DPSQX_S_W_PH,
119       DPSQX_SA_W_PH,
120       MULSA_W_PH,
121
122       MULT,
123       MULTU,
124       MADD_DSP,
125       MADDU_DSP,
126       MSUB_DSP,
127       MSUBU_DSP,
128
129       // Load/Store Left/Right nodes.
130       LWL = ISD::FIRST_TARGET_MEMORY_OPCODE,
131       LWR,
132       SWL,
133       SWR,
134       LDL,
135       LDR,
136       SDL,
137       SDR
138     };
139   }
140
141   //===--------------------------------------------------------------------===//
142   // TargetLowering Implementation
143   //===--------------------------------------------------------------------===//
144   class MipsFunctionInfo;
145
146   class MipsTargetLowering : public TargetLowering  {
147   public:
148     explicit MipsTargetLowering(MipsTargetMachine &TM);
149
150     virtual MVT getShiftAmountTy(EVT LHSTy) const { return MVT::i32; }
151
152     virtual bool allowsUnalignedMemoryAccesses (EVT VT, bool *Fast) const;
153
154     virtual void LowerOperationWrapper(SDNode *N,
155                                        SmallVectorImpl<SDValue> &Results,
156                                        SelectionDAG &DAG) const;
157
158     /// LowerOperation - Provide custom lowering hooks for some operations.
159     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
160
161     /// ReplaceNodeResults - Replace the results of node with an illegal result
162     /// type with new values built out of custom code.
163     ///
164     virtual void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue>&Results,
165                                     SelectionDAG &DAG) const;
166
167     /// getTargetNodeName - This method returns the name of a target specific
168     //  DAG node.
169     virtual const char *getTargetNodeName(unsigned Opcode) const;
170
171     /// getSetCCResultType - get the ISD::SETCC result ValueType
172     EVT getSetCCResultType(EVT VT) const;
173
174     virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
175   private:
176
177     void setMips16HardFloatLibCalls();
178
179     /// ByValArgInfo - Byval argument information.
180     struct ByValArgInfo {
181       unsigned FirstIdx; // Index of the first register used.
182       unsigned NumRegs;  // Number of registers used for this argument.
183       unsigned Address;  // Offset of the stack area used to pass this argument.
184
185       ByValArgInfo() : FirstIdx(0), NumRegs(0), Address(0) {}
186     };
187
188     /// MipsCC - This class provides methods used to analyze formal and call
189     /// arguments and inquire about calling convention information.
190     class MipsCC {
191     public:
192       MipsCC(CallingConv::ID CallConv, bool IsVarArg, bool IsO32,
193              CCState &Info);
194
195       void analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs);
196       void analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins);
197       void handleByValArg(unsigned ValNo, MVT ValVT, MVT LocVT,
198                           CCValAssign::LocInfo LocInfo,
199                           ISD::ArgFlagsTy ArgFlags);
200
201       const CCState &getCCInfo() const { return CCInfo; }
202
203       /// hasByValArg - Returns true if function has byval arguments.
204       bool hasByValArg() const { return !ByValArgs.empty(); }
205
206       /// useRegsForByval - Returns true if the calling convention allows the
207       /// use of registers to pass byval arguments.
208       bool useRegsForByval() const { return UseRegsForByval; }
209
210       /// regSize - Size (in number of bits) of integer registers.
211       unsigned regSize() const { return RegSize; }
212
213       /// numIntArgRegs - Number of integer registers available for calls.
214       unsigned numIntArgRegs() const { return NumIntArgRegs; }
215
216       /// reservedArgArea - The size of the area the caller reserves for
217       /// register arguments. This is 16-byte if ABI is O32.
218       unsigned reservedArgArea() const { return ReservedArgArea; }
219
220       /// intArgRegs - Pointer to array of integer registers.
221       const uint16_t *intArgRegs() const { return IntArgRegs; }
222
223       typedef SmallVector<ByValArgInfo, 2>::const_iterator byval_iterator;
224       byval_iterator byval_begin() const { return ByValArgs.begin(); }
225       byval_iterator byval_end() const { return ByValArgs.end(); }
226
227     private:
228       void allocateRegs(ByValArgInfo &ByVal, unsigned ByValSize,
229                         unsigned Align);
230
231       CCState &CCInfo;
232       bool UseRegsForByval;
233       unsigned RegSize;
234       unsigned NumIntArgRegs;
235       unsigned ReservedArgArea;
236       const uint16_t *IntArgRegs, *ShadowRegs;
237       SmallVector<ByValArgInfo, 2> ByValArgs;
238       llvm::CCAssignFn *FixedFn, *VarFn;
239     };
240
241     // Subtarget Info
242     const MipsSubtarget *Subtarget;
243
244     bool HasMips64, IsN64, IsO32;
245
246     // Lower Operand helpers
247     SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
248                             CallingConv::ID CallConv, bool isVarArg,
249                             const SmallVectorImpl<ISD::InputArg> &Ins,
250                             DebugLoc dl, SelectionDAG &DAG,
251                             SmallVectorImpl<SDValue> &InVals) const;
252
253     // Lower Operand specifics
254     SDValue LowerBRCOND(SDValue Op, SelectionDAG &DAG) const;
255     SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
256     SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
257     SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
258     SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
259     SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
260     SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
261     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
262     SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
263     SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
264     SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
265     SDValue LowerFABS(SDValue Op, SelectionDAG &DAG) const;
266     SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
267     SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
268     SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG& DAG) const;
269     SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const;
270     SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG& DAG) const;
271     SDValue LowerShiftRightParts(SDValue Op, SelectionDAG& DAG,
272                                  bool IsSRA) const;
273     SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) const;
274     SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
275     SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
276     SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const;
277     SDValue LowerADD(SDValue Op, SelectionDAG &DAG) const;
278
279     /// IsEligibleForTailCallOptimization - Check whether the call is eligible
280     /// for tail call optimization.
281     bool IsEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
282                                            unsigned NextStackOffset,
283                                            const MipsFunctionInfo& FI) const;
284
285     /// copyByValArg - Copy argument registers which were used to pass a byval
286     /// argument to the stack. Create a stack frame object for the byval
287     /// argument.
288     void copyByValRegs(SDValue Chain, DebugLoc DL,
289                        std::vector<SDValue> &OutChains, SelectionDAG &DAG,
290                        const ISD::ArgFlagsTy &Flags,
291                        SmallVectorImpl<SDValue> &InVals,
292                        const Argument *FuncArg,
293                        const MipsCC &CC, const ByValArgInfo &ByVal) const;
294
295     /// passByValArg - Pass a byval argument in registers or on stack.
296     void passByValArg(SDValue Chain, DebugLoc DL,
297                       SmallVector<std::pair<unsigned, SDValue>, 16> &RegsToPass,
298                       SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
299                       MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
300                       const MipsCC &CC, const ByValArgInfo &ByVal,
301                       const ISD::ArgFlagsTy &Flags, bool isLittle) const;
302
303     /// writeVarArgRegs - Write variable function arguments passed in registers
304     /// to the stack. Also create a stack frame object for the first variable
305     /// argument.
306     void writeVarArgRegs(std::vector<SDValue> &OutChains, const MipsCC &CC,
307                          SDValue Chain, DebugLoc DL, SelectionDAG &DAG) const;
308
309     virtual SDValue
310       LowerFormalArguments(SDValue Chain,
311                            CallingConv::ID CallConv, bool isVarArg,
312                            const SmallVectorImpl<ISD::InputArg> &Ins,
313                            DebugLoc dl, SelectionDAG &DAG,
314                            SmallVectorImpl<SDValue> &InVals) const;
315
316     SDValue passArgOnStack(SDValue StackPtr, unsigned Offset, SDValue Chain,
317                            SDValue Arg, DebugLoc DL, bool IsTailCall,
318                            SelectionDAG &DAG) const;
319
320     virtual SDValue
321       LowerCall(TargetLowering::CallLoweringInfo &CLI,
322                 SmallVectorImpl<SDValue> &InVals) const;
323
324     virtual bool
325       CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
326                      bool isVarArg,
327                      const SmallVectorImpl<ISD::OutputArg> &Outs,
328                      LLVMContext &Context) const;
329
330     virtual SDValue
331       LowerReturn(SDValue Chain,
332                   CallingConv::ID CallConv, bool isVarArg,
333                   const SmallVectorImpl<ISD::OutputArg> &Outs,
334                   const SmallVectorImpl<SDValue> &OutVals,
335                   DebugLoc dl, SelectionDAG &DAG) const;
336
337     virtual MachineBasicBlock *
338       EmitInstrWithCustomInserter(MachineInstr *MI,
339                                   MachineBasicBlock *MBB) const;
340
341     // Inline asm support
342     ConstraintType getConstraintType(const std::string &Constraint) const;
343
344     /// Examine constraint string and operand type and determine a weight value.
345     /// The operand object must already have been set up with the operand type.
346     ConstraintWeight getSingleConstraintMatchWeight(
347       AsmOperandInfo &info, const char *constraint) const;
348
349     std::pair<unsigned, const TargetRegisterClass*>
350               getRegForInlineAsmConstraint(const std::string &Constraint,
351               EVT VT) const;
352
353     /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
354     /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is
355     /// true it means one of the asm constraint of the inline asm instruction
356     /// being processed is 'm'.
357     virtual void LowerAsmOperandForConstraint(SDValue Op,
358                                               std::string &Constraint,
359                                               std::vector<SDValue> &Ops,
360                                               SelectionDAG &DAG) const;
361
362     virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const;
363
364     virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
365
366     virtual EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
367                                     unsigned SrcAlign,
368                                     bool IsMemset, bool ZeroMemset,
369                                     bool MemcpyStrSrc,
370                                     MachineFunction &MF) const;
371
372     /// isFPImmLegal - Returns true if the target can instruction select the
373     /// specified FP immediate natively. If false, the legalizer will
374     /// materialize the FP immediate as a load from a constant pool.
375     virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const;
376
377     virtual unsigned getJumpTableEncoding() const;
378
379     MachineBasicBlock *EmitBPOSGE32(MachineInstr *MI,
380                                     MachineBasicBlock *BB) const;
381     MachineBasicBlock *EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
382                     unsigned Size, unsigned BinOpcode, bool Nand = false) const;
383     MachineBasicBlock *EmitAtomicBinaryPartword(MachineInstr *MI,
384                     MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
385                     bool Nand = false) const;
386     MachineBasicBlock *EmitAtomicCmpSwap(MachineInstr *MI,
387                                   MachineBasicBlock *BB, unsigned Size) const;
388     MachineBasicBlock *EmitAtomicCmpSwapPartword(MachineInstr *MI,
389                                   MachineBasicBlock *BB, unsigned Size) const;
390   };
391 }
392
393 #endif // MipsISELLOWERING_H