68f05ff80a2cac32984a065d937a34bd960bb636
[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       // Return
64       Ret,
65
66       EH_RETURN,
67
68       // Node used to extract integer from accumulator.
69       ExtractLOHI,
70
71       // Node used to insert integers to accumulator.
72       InsertLOHI,
73
74       // Mult nodes.
75       Mult,
76       Multu,
77
78       // MAdd/Sub nodes
79       MAdd,
80       MAddu,
81       MSub,
82       MSubu,
83
84       // DivRem(u)
85       DivRem,
86       DivRemU,
87       DivRem16,
88       DivRemU16,
89
90       BuildPairF64,
91       ExtractElementF64,
92
93       Wrapper,
94
95       DynAlloc,
96
97       Sync,
98
99       Ext,
100       Ins,
101
102       // EXTR.W instrinsic nodes.
103       EXTP,
104       EXTPDP,
105       EXTR_S_H,
106       EXTR_W,
107       EXTR_R_W,
108       EXTR_RS_W,
109       SHILO,
110       MTHLIP,
111
112       // DPA.W intrinsic nodes.
113       MULSAQ_S_W_PH,
114       MAQ_S_W_PHL,
115       MAQ_S_W_PHR,
116       MAQ_SA_W_PHL,
117       MAQ_SA_W_PHR,
118       DPAU_H_QBL,
119       DPAU_H_QBR,
120       DPSU_H_QBL,
121       DPSU_H_QBR,
122       DPAQ_S_W_PH,
123       DPSQ_S_W_PH,
124       DPAQ_SA_L_W,
125       DPSQ_SA_L_W,
126       DPA_W_PH,
127       DPS_W_PH,
128       DPAQX_S_W_PH,
129       DPAQX_SA_W_PH,
130       DPAX_W_PH,
131       DPSX_W_PH,
132       DPSQX_S_W_PH,
133       DPSQX_SA_W_PH,
134       MULSA_W_PH,
135
136       MULT,
137       MULTU,
138       MADD_DSP,
139       MADDU_DSP,
140       MSUB_DSP,
141       MSUBU_DSP,
142
143       // DSP shift nodes.
144       SHLL_DSP,
145       SHRA_DSP,
146       SHRL_DSP,
147
148       // DSP setcc and select_cc nodes.
149       SETCC_DSP,
150       SELECT_CC_DSP,
151
152       // Load/Store Left/Right nodes.
153       LWL = ISD::FIRST_TARGET_MEMORY_OPCODE,
154       LWR,
155       SWL,
156       SWR,
157       LDL,
158       LDR,
159       SDL,
160       SDR
161     };
162   }
163
164   //===--------------------------------------------------------------------===//
165   // TargetLowering Implementation
166   //===--------------------------------------------------------------------===//
167   class MipsFunctionInfo;
168
169   class MipsTargetLowering : public TargetLowering  {
170   public:
171     explicit MipsTargetLowering(MipsTargetMachine &TM);
172
173     static const MipsTargetLowering *create(MipsTargetMachine &TM);
174
175     virtual MVT getScalarShiftAmountTy(EVT LHSTy) const { return MVT::i32; }
176
177     virtual void LowerOperationWrapper(SDNode *N,
178                                        SmallVectorImpl<SDValue> &Results,
179                                        SelectionDAG &DAG) const;
180
181     /// LowerOperation - Provide custom lowering hooks for some operations.
182     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
183
184     /// ReplaceNodeResults - Replace the results of node with an illegal result
185     /// type with new values built out of custom code.
186     ///
187     virtual void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue>&Results,
188                                     SelectionDAG &DAG) const;
189
190     /// getTargetNodeName - This method returns the name of a target specific
191     //  DAG node.
192     virtual const char *getTargetNodeName(unsigned Opcode) const;
193
194     /// getSetCCResultType - get the ISD::SETCC result ValueType
195     EVT getSetCCResultType(EVT VT) const;
196
197     virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
198
199     virtual MachineBasicBlock *
200     EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const;
201
202     struct LTStr {
203       bool operator()(const char *S1, const char *S2) const {
204         return strcmp(S1, S2) < 0;
205       }
206     };
207
208   protected:
209     SDValue getGlobalReg(SelectionDAG &DAG, EVT Ty) const;
210
211     SDValue getAddrLocal(SDValue Op, SelectionDAG &DAG, bool HasMips64) const;
212
213     SDValue getAddrGlobal(SDValue Op, SelectionDAG &DAG, unsigned Flag) const;
214
215     SDValue getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
216                                   unsigned HiFlag, unsigned LoFlag) const;
217
218     /// This function fills Ops, which is the list of operands that will later
219     /// be used when a function call node is created. It also generates
220     /// copyToReg nodes to set up argument registers.
221     virtual void
222     getOpndList(SmallVectorImpl<SDValue> &Ops,
223                 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
224                 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
225                 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const;
226
227     /// ByValArgInfo - Byval argument information.
228     struct ByValArgInfo {
229       unsigned FirstIdx; // Index of the first register used.
230       unsigned NumRegs;  // Number of registers used for this argument.
231       unsigned Address;  // Offset of the stack area used to pass this argument.
232
233       ByValArgInfo() : FirstIdx(0), NumRegs(0), Address(0) {}
234     };
235
236     /// MipsCC - This class provides methods used to analyze formal and call
237     /// arguments and inquire about calling convention information.
238     class MipsCC {
239     public:
240       enum SpecialCallingConvType {
241         Mips16RetHelperConv, NoSpecialCallingConv
242       };
243
244       MipsCC(
245         CallingConv::ID CallConv, bool IsO32, CCState &Info,
246         SpecialCallingConvType SpecialCallingConv = NoSpecialCallingConv);
247
248
249       void analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
250                                bool IsVarArg, bool IsSoftFloat,
251                                const SDNode *CallNode,
252                                std::vector<ArgListEntry> &FuncArgs);
253       void analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
254                                   bool IsSoftFloat,
255                                   Function::const_arg_iterator FuncArg);
256
257       void analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
258                              bool IsSoftFloat, const SDNode *CallNode,
259                              const Type *RetTy) const;
260
261       void analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
262                          bool IsSoftFloat, const Type *RetTy) const;
263
264       const CCState &getCCInfo() const { return CCInfo; }
265
266       /// hasByValArg - Returns true if function has byval arguments.
267       bool hasByValArg() const { return !ByValArgs.empty(); }
268
269       /// regSize - Size (in number of bits) of integer registers.
270       unsigned regSize() const { return IsO32 ? 4 : 8; }
271
272       /// numIntArgRegs - Number of integer registers available for calls.
273       unsigned numIntArgRegs() const;
274
275       /// reservedArgArea - The size of the area the caller reserves for
276       /// register arguments. This is 16-byte if ABI is O32.
277       unsigned reservedArgArea() const;
278
279       /// Return pointer to array of integer argument registers.
280       const uint16_t *intArgRegs() const;
281
282       typedef SmallVector<ByValArgInfo, 2>::const_iterator byval_iterator;
283       byval_iterator byval_begin() const { return ByValArgs.begin(); }
284       byval_iterator byval_end() const { return ByValArgs.end(); }
285
286     private:
287       void handleByValArg(unsigned ValNo, MVT ValVT, MVT LocVT,
288                           CCValAssign::LocInfo LocInfo,
289                           ISD::ArgFlagsTy ArgFlags);
290
291       /// useRegsForByval - Returns true if the calling convention allows the
292       /// use of registers to pass byval arguments.
293       bool useRegsForByval() const { return CallConv != CallingConv::Fast; }
294
295       /// Return the function that analyzes fixed argument list functions.
296       llvm::CCAssignFn *fixedArgFn() const;
297
298       /// Return the function that analyzes variable argument list functions.
299       llvm::CCAssignFn *varArgFn() const;
300
301       const uint16_t *shadowRegs() const;
302
303       void allocateRegs(ByValArgInfo &ByVal, unsigned ByValSize,
304                         unsigned Align);
305
306       /// Return the type of the register which is used to pass an argument or
307       /// return a value. This function returns f64 if the argument is an i64
308       /// value which has been generated as a result of softening an f128 value.
309       /// Otherwise, it just returns VT.
310       MVT getRegVT(MVT VT, const Type *OrigTy, const SDNode *CallNode,
311                    bool IsSoftFloat) const;
312
313       template<typename Ty>
314       void analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
315                          const SDNode *CallNode, const Type *RetTy) const;
316
317       CCState &CCInfo;
318       CallingConv::ID CallConv;
319       bool IsO32;
320       SpecialCallingConvType SpecialCallingConv;
321       SmallVector<ByValArgInfo, 2> ByValArgs;
322     };
323   protected:
324     // Subtarget Info
325     const MipsSubtarget *Subtarget;
326
327     bool HasMips64, IsN64, IsO32;
328
329   private:
330
331     MipsCC::SpecialCallingConvType getSpecialCallingConv(SDValue Callee) const;
332     // Lower Operand helpers
333     SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
334                             CallingConv::ID CallConv, bool isVarArg,
335                             const SmallVectorImpl<ISD::InputArg> &Ins,
336                             DebugLoc dl, SelectionDAG &DAG,
337                             SmallVectorImpl<SDValue> &InVals,
338                             const SDNode *CallNode, const Type *RetTy) const;
339
340     // Lower Operand specifics
341     SDValue lowerBR_JT(SDValue Op, SelectionDAG &DAG) const;
342     SDValue lowerBRCOND(SDValue Op, SelectionDAG &DAG) const;
343     SDValue lowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
344     SDValue lowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
345     SDValue lowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
346     SDValue lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
347     SDValue lowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
348     SDValue lowerSELECT(SDValue Op, SelectionDAG &DAG) const;
349     SDValue lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
350     SDValue lowerSETCC(SDValue Op, SelectionDAG &DAG) const;
351     SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
352     SDValue lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
353     SDValue lowerFABS(SDValue Op, SelectionDAG &DAG) const;
354     SDValue lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
355     SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
356     SDValue lowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const;
357     SDValue lowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const;
358     SDValue lowerShiftLeftParts(SDValue Op, SelectionDAG& DAG) const;
359     SDValue lowerShiftRightParts(SDValue Op, SelectionDAG& DAG,
360                                  bool IsSRA) const;
361     SDValue lowerLOAD(SDValue Op, SelectionDAG &DAG) const;
362     SDValue lowerSTORE(SDValue Op, SelectionDAG &DAG) const;
363     SDValue lowerADD(SDValue Op, SelectionDAG &DAG) const;
364
365     /// isEligibleForTailCallOptimization - Check whether the call is eligible
366     /// for tail call optimization.
367     virtual bool
368     isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
369                                       unsigned NextStackOffset,
370                                       const MipsFunctionInfo& FI) const = 0;
371
372     /// copyByValArg - Copy argument registers which were used to pass a byval
373     /// argument to the stack. Create a stack frame object for the byval
374     /// argument.
375     void copyByValRegs(SDValue Chain, DebugLoc DL,
376                        std::vector<SDValue> &OutChains, SelectionDAG &DAG,
377                        const ISD::ArgFlagsTy &Flags,
378                        SmallVectorImpl<SDValue> &InVals,
379                        const Argument *FuncArg,
380                        const MipsCC &CC, const ByValArgInfo &ByVal) const;
381
382     /// passByValArg - Pass a byval argument in registers or on stack.
383     void passByValArg(SDValue Chain, DebugLoc DL,
384                       std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
385                       SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
386                       MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
387                       const MipsCC &CC, const ByValArgInfo &ByVal,
388                       const ISD::ArgFlagsTy &Flags, bool isLittle) const;
389
390     /// writeVarArgRegs - Write variable function arguments passed in registers
391     /// to the stack. Also create a stack frame object for the first variable
392     /// argument.
393     void writeVarArgRegs(std::vector<SDValue> &OutChains, const MipsCC &CC,
394                          SDValue Chain, DebugLoc DL, SelectionDAG &DAG) const;
395
396     virtual SDValue
397       LowerFormalArguments(SDValue Chain,
398                            CallingConv::ID CallConv, bool isVarArg,
399                            const SmallVectorImpl<ISD::InputArg> &Ins,
400                            DebugLoc dl, SelectionDAG &DAG,
401                            SmallVectorImpl<SDValue> &InVals) const;
402
403     SDValue passArgOnStack(SDValue StackPtr, unsigned Offset, SDValue Chain,
404                            SDValue Arg, DebugLoc DL, bool IsTailCall,
405                            SelectionDAG &DAG) const;
406
407     virtual SDValue
408       LowerCall(TargetLowering::CallLoweringInfo &CLI,
409                 SmallVectorImpl<SDValue> &InVals) const;
410
411     virtual bool
412       CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
413                      bool isVarArg,
414                      const SmallVectorImpl<ISD::OutputArg> &Outs,
415                      LLVMContext &Context) const;
416
417     virtual SDValue
418       LowerReturn(SDValue Chain,
419                   CallingConv::ID CallConv, bool isVarArg,
420                   const SmallVectorImpl<ISD::OutputArg> &Outs,
421                   const SmallVectorImpl<SDValue> &OutVals,
422                   DebugLoc dl, SelectionDAG &DAG) const;
423
424     // Inline asm support
425     ConstraintType getConstraintType(const std::string &Constraint) const;
426
427     /// Examine constraint string and operand type and determine a weight value.
428     /// The operand object must already have been set up with the operand type.
429     ConstraintWeight getSingleConstraintMatchWeight(
430       AsmOperandInfo &info, const char *constraint) const;
431
432     std::pair<unsigned, const TargetRegisterClass*>
433               getRegForInlineAsmConstraint(const std::string &Constraint,
434               EVT VT) const;
435
436     /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
437     /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is
438     /// true it means one of the asm constraint of the inline asm instruction
439     /// being processed is 'm'.
440     virtual void LowerAsmOperandForConstraint(SDValue Op,
441                                               std::string &Constraint,
442                                               std::vector<SDValue> &Ops,
443                                               SelectionDAG &DAG) const;
444
445     virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const;
446
447     virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
448
449     virtual EVT getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
450                                     unsigned SrcAlign,
451                                     bool IsMemset, bool ZeroMemset,
452                                     bool MemcpyStrSrc,
453                                     MachineFunction &MF) const;
454
455     /// isFPImmLegal - Returns true if the target can instruction select the
456     /// specified FP immediate natively. If false, the legalizer will
457     /// materialize the FP immediate as a load from a constant pool.
458     virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const;
459
460     virtual unsigned getJumpTableEncoding() const;
461
462     MachineBasicBlock *emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
463                     unsigned Size, unsigned BinOpcode, bool Nand = false) const;
464     MachineBasicBlock *emitAtomicBinaryPartword(MachineInstr *MI,
465                     MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
466                     bool Nand = false) const;
467     MachineBasicBlock *emitAtomicCmpSwap(MachineInstr *MI,
468                                   MachineBasicBlock *BB, unsigned Size) const;
469     MachineBasicBlock *emitAtomicCmpSwapPartword(MachineInstr *MI,
470                                   MachineBasicBlock *BB, unsigned Size) const;
471   };
472
473   /// Create MipsTargetLowering objects.
474   const MipsTargetLowering *createMips16TargetLowering(MipsTargetMachine &TM);
475   const MipsTargetLowering *createMipsSETargetLowering(MipsTargetMachine &TM);
476 }
477
478 #endif // MipsISELLOWERING_H