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