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