LLVM API Documentation

PPCISelLowering.h

Go to the documentation of this file.
00001 //===-- PPCISelLowering.h - PPC32 DAG Lowering Interface --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the interfaces that PPC uses to lower LLVM code into a
00011 // selection DAG.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TARGET_POWERPC_PPC32ISELLOWERING_H
00016 #define LLVM_TARGET_POWERPC_PPC32ISELLOWERING_H
00017 
00018 #include "llvm/Target/TargetLowering.h"
00019 #include "llvm/CodeGen/SelectionDAG.h"
00020 #include "PPC.h"
00021 #include "PPCSubtarget.h"
00022 
00023 namespace llvm {
00024   namespace PPCISD {
00025     enum NodeType {
00026       // Start the numbering where the builtin ops and target ops leave off.
00027       FIRST_NUMBER = ISD::BUILTIN_OP_END,
00028 
00029       /// FSEL - Traditional three-operand fsel node.
00030       ///
00031       FSEL,
00032       
00033       /// FCFID - The FCFID instruction, taking an f64 operand and producing
00034       /// and f64 value containing the FP representation of the integer that
00035       /// was temporarily in the f64 operand.
00036       FCFID,
00037       
00038       /// FCTI[D,W]Z - The FCTIDZ and FCTIWZ instructions, taking an f32 or f64 
00039       /// operand, producing an f64 value containing the integer representation
00040       /// of that FP value.
00041       FCTIDZ, FCTIWZ,
00042       
00043       /// STFIWX - The STFIWX instruction.  The first operand is an input token
00044       /// chain, then an f64 value to store, then an address to store it to,
00045       /// then a SRCVALUE for the address.
00046       STFIWX,
00047       
00048       // VMADDFP, VNMSUBFP - The VMADDFP and VNMSUBFP instructions, taking
00049       // three v4f32 operands and producing a v4f32 result.
00050       VMADDFP, VNMSUBFP,
00051       
00052       /// VPERM - The PPC VPERM Instruction.
00053       ///
00054       VPERM,
00055       
00056       /// Hi/Lo - These represent the high and low 16-bit parts of a global
00057       /// address respectively.  These nodes have two operands, the first of
00058       /// which must be a TargetGlobalAddress, and the second of which must be a
00059       /// Constant.  Selected naively, these turn into 'lis G+C' and 'li G+C',
00060       /// though these are usually folded into other nodes.
00061       Hi, Lo,
00062       
00063       TOC_ENTRY,
00064 
00065       /// OPRC, CHAIN = DYNALLOC(CHAIN, NEGSIZE, FRAME_INDEX)
00066       /// This instruction is lowered in PPCRegisterInfo::eliminateFrameIndex to
00067       /// compute an allocation on the stack.
00068       DYNALLOC,
00069       
00070       /// GlobalBaseReg - On Darwin, this node represents the result of the mflr
00071       /// at function entry, used for PIC code.
00072       GlobalBaseReg,
00073       
00074       /// These nodes represent the 32-bit PPC shifts that operate on 6-bit
00075       /// shift amounts.  These nodes are generated by the multi-precision shift
00076       /// code.
00077       SRL, SRA, SHL,
00078       
00079       /// EXTSW_32 - This is the EXTSW instruction for use with "32-bit"
00080       /// registers.
00081       EXTSW_32,
00082 
00083       /// STD_32 - This is the STD instruction for use with "32-bit" registers.
00084       STD_32,
00085       
00086       /// CALL - A direct function call.
00087       CALL_Darwin, CALL_SVR4,
00088       
00089       /// NOP - Special NOP which follows 64-bit SVR4 calls.
00090       NOP,
00091 
00092       /// CHAIN,FLAG = MTCTR(VAL, CHAIN[, INFLAG]) - Directly corresponds to a
00093       /// MTCTR instruction.
00094       MTCTR,
00095       
00096       /// CHAIN,FLAG = BCTRL(CHAIN, INFLAG) - Directly corresponds to a
00097       /// BCTRL instruction.
00098       BCTRL_Darwin, BCTRL_SVR4,
00099       
00100       /// Return with a flag operand, matched by 'blr'
00101       RET_FLAG,
00102       
00103       /// R32 = MFCR(CRREG, INFLAG) - Represents the MFCR/MFOCRF instructions.
00104       /// This copies the bits corresponding to the specified CRREG into the
00105       /// resultant GPR.  Bits corresponding to other CR regs are undefined.
00106       MFCR,
00107 
00108       /// RESVEC = VCMP(LHS, RHS, OPC) - Represents one of the altivec VCMP*
00109       /// instructions.  For lack of better number, we use the opcode number
00110       /// encoding for the OPC field to identify the compare.  For example, 838
00111       /// is VCMPGTSH.
00112       VCMP,
00113       
00114       /// RESVEC, OUTFLAG = VCMPo(LHS, RHS, OPC) - Represents one of the
00115       /// altivec VCMP*o instructions.  For lack of better number, we use the 
00116       /// opcode number encoding for the OPC field to identify the compare.  For
00117       /// example, 838 is VCMPGTSH.
00118       VCMPo,
00119       
00120       /// CHAIN = COND_BRANCH CHAIN, CRRC, OPC, DESTBB [, INFLAG] - This
00121       /// corresponds to the COND_BRANCH pseudo instruction.  CRRC is the
00122       /// condition register to branch on, OPC is the branch opcode to use (e.g.
00123       /// PPC::BLE), DESTBB is the destination block to branch to, and INFLAG is
00124       /// an optional input flag argument.
00125       COND_BRANCH,
00126       
00127       /// CHAIN = STBRX CHAIN, GPRC, Ptr, SRCVALUE, Type - This is a 
00128       /// byte-swapping store instruction.  It byte-swaps the low "Type" bits of
00129       /// the GPRC input, then stores it through Ptr.  Type can be either i16 or
00130       /// i32.
00131       STBRX, 
00132       
00133       /// GPRC, CHAIN = LBRX CHAIN, Ptr, SRCVALUE, Type - This is a 
00134       /// byte-swapping load instruction.  It loads "Type" bits, byte swaps it,
00135       /// then puts it in the bottom bits of the GPRC.  TYPE can be either i16
00136       /// or i32.
00137       LBRX,
00138 
00139       // The following 5 instructions are used only as part of the
00140       // long double-to-int conversion sequence.
00141 
00142       /// OUTFLAG = MFFS F8RC - This moves the FPSCR (not modelled) into the
00143       /// register.
00144       MFFS,
00145 
00146       /// OUTFLAG = MTFSB0 INFLAG - This clears a bit in the FPSCR.
00147       MTFSB0,
00148 
00149       /// OUTFLAG = MTFSB1 INFLAG - This sets a bit in the FPSCR.
00150       MTFSB1,
00151 
00152       /// F8RC, OUTFLAG = FADDRTZ F8RC, F8RC, INFLAG - This is an FADD done with
00153       /// rounding towards zero.  It has flags added so it won't move past the 
00154       /// FPSCR-setting instructions.
00155       FADDRTZ,
00156 
00157       /// MTFSF = F8RC, INFLAG - This moves the register into the FPSCR.
00158       MTFSF,
00159 
00160       /// LARX = This corresponds to PPC l{w|d}arx instrcution: load and
00161       /// reserve indexed. This is used to implement atomic operations.
00162       LARX,
00163 
00164       /// STCX = This corresponds to PPC stcx. instrcution: store conditional
00165       /// indexed. This is used to implement atomic operations.
00166       STCX,
00167 
00168       /// TC_RETURN - A tail call return.
00169       ///   operand #0 chain
00170       ///   operand #1 callee (register or absolute)
00171       ///   operand #2 stack adjustment
00172       ///   operand #3 optional in flag
00173       TC_RETURN
00174     };
00175   }
00176 
00177   /// Define some predicates that are used for node matching.
00178   namespace PPC {
00179     /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
00180     /// VPKUHUM instruction.
00181     bool isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary);
00182     
00183     /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
00184     /// VPKUWUM instruction.
00185     bool isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary);
00186 
00187     /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
00188     /// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
00189     bool isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
00190                             bool isUnary);
00191 
00192     /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
00193     /// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
00194     bool isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
00195                             bool isUnary);
00196     
00197     /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
00198     /// amount, otherwise return -1.
00199     int isVSLDOIShuffleMask(SDNode *N, bool isUnary);
00200     
00201     /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
00202     /// specifies a splat of a single element that is suitable for input to
00203     /// VSPLTB/VSPLTH/VSPLTW.
00204     bool isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize);
00205     
00206     /// isAllNegativeZeroVector - Returns true if all elements of build_vector
00207     /// are -0.0.
00208     bool isAllNegativeZeroVector(SDNode *N);
00209 
00210     /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
00211     /// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
00212     unsigned getVSPLTImmediate(SDNode *N, unsigned EltSize);
00213     
00214     /// get_VSPLTI_elt - If this is a build_vector of constants which can be
00215     /// formed by using a vspltis[bhw] instruction of the specified element
00216     /// size, return the constant being splatted.  The ByteSize field indicates
00217     /// the number of bytes of each element [124] -> [bhw].
00218     SDValue get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG);
00219   }
00220   
00221   class PPCTargetLowering : public TargetLowering {
00222     int VarArgsFrameIndex;            // FrameIndex for start of varargs area.
00223     int VarArgsStackOffset;           // StackOffset for start of stack
00224                                       // arguments.
00225     unsigned VarArgsNumGPR;           // Index of the first unused integer
00226                                       // register for parameter passing.
00227     unsigned VarArgsNumFPR;           // Index of the first unused double
00228                                       // register for parameter passing.
00229     const PPCSubtarget &PPCSubTarget;
00230   public:
00231     explicit PPCTargetLowering(PPCTargetMachine &TM);
00232     
00233     /// getTargetNodeName() - This method returns the name of a target specific
00234     /// DAG node.
00235     virtual const char *getTargetNodeName(unsigned Opcode) const;
00236 
00237     /// getSetCCResultType - Return the ISD::SETCC ValueType
00238     virtual MVT::SimpleValueType getSetCCResultType(EVT VT) const;
00239 
00240     /// getPreIndexedAddressParts - returns true by value, base pointer and
00241     /// offset pointer and addressing mode by reference if the node's address
00242     /// can be legally represented as pre-indexed load / store address.
00243     virtual bool getPreIndexedAddressParts(SDNode *N, SDValue &Base,
00244                                            SDValue &Offset,
00245                                            ISD::MemIndexedMode &AM,
00246                                            SelectionDAG &DAG) const;
00247     
00248     /// SelectAddressRegReg - Given the specified addressed, check to see if it
00249     /// can be represented as an indexed [r+r] operation.  Returns false if it
00250     /// can be more efficiently represented with [r+imm].
00251     bool SelectAddressRegReg(SDValue N, SDValue &Base, SDValue &Index,
00252                              SelectionDAG &DAG) const;
00253     
00254     /// SelectAddressRegImm - Returns true if the address N can be represented
00255     /// by a base register plus a signed 16-bit displacement [r+imm], and if it
00256     /// is not better represented as reg+reg.
00257     bool SelectAddressRegImm(SDValue N, SDValue &Disp, SDValue &Base,
00258                              SelectionDAG &DAG) const;
00259     
00260     /// SelectAddressRegRegOnly - Given the specified addressed, force it to be
00261     /// represented as an indexed [r+r] operation.
00262     bool SelectAddressRegRegOnly(SDValue N, SDValue &Base, SDValue &Index,
00263                                  SelectionDAG &DAG) const;
00264 
00265     /// SelectAddressRegImmShift - Returns true if the address N can be
00266     /// represented by a base register plus a signed 14-bit displacement
00267     /// [r+imm*4].  Suitable for use by STD and friends.
00268     bool SelectAddressRegImmShift(SDValue N, SDValue &Disp, SDValue &Base,
00269                                   SelectionDAG &DAG) const;
00270 
00271     
00272     /// LowerOperation - Provide custom lowering hooks for some operations.
00273     ///
00274     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
00275 
00276     /// ReplaceNodeResults - Replace the results of node with an illegal result
00277     /// type with new values built out of custom code.
00278     ///
00279     virtual void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue>&Results,
00280                                     SelectionDAG &DAG);
00281 
00282     virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
00283     
00284     virtual void computeMaskedBitsForTargetNode(const SDValue Op,
00285                                                 const APInt &Mask,
00286                                                 APInt &KnownZero, 
00287                                                 APInt &KnownOne,
00288                                                 const SelectionDAG &DAG,
00289                                                 unsigned Depth = 0) const;
00290 
00291     virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
00292                                                   MachineBasicBlock *MBB) const;
00293     MachineBasicBlock *EmitAtomicBinary(MachineInstr *MI, 
00294                                         MachineBasicBlock *MBB, bool is64Bit,
00295                                         unsigned BinOpcode) const;
00296     MachineBasicBlock *EmitPartwordAtomicBinary(MachineInstr *MI, 
00297                                                 MachineBasicBlock *MBB, 
00298                                             bool is8bit, unsigned Opcode) const;
00299     
00300     ConstraintType getConstraintType(const std::string &Constraint) const;
00301     std::pair<unsigned, const TargetRegisterClass*> 
00302       getRegForInlineAsmConstraint(const std::string &Constraint,
00303                                    EVT VT) const;
00304 
00305     /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
00306     /// function arguments in the caller parameter area.  This is the actual
00307     /// alignment, not its logarithm.
00308     unsigned getByValTypeAlignment(const Type *Ty) const;
00309 
00310     /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
00311     /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is
00312     /// true it means one of the asm constraint of the inline asm instruction
00313     /// being processed is 'm'.
00314     virtual void LowerAsmOperandForConstraint(SDValue Op,
00315                                               char ConstraintLetter,
00316                                               bool hasMemory,
00317                                               std::vector<SDValue> &Ops,
00318                                               SelectionDAG &DAG) const;
00319     
00320     /// isLegalAddressingMode - Return true if the addressing mode represented
00321     /// by AM is legal for this target, for a load/store of the specified type.
00322     virtual bool isLegalAddressingMode(const AddrMode &AM, const Type *Ty)const;
00323     
00324     /// isLegalAddressImmediate - Return true if the integer value can be used
00325     /// as the offset of the target addressing mode for load / store of the
00326     /// given type.
00327     virtual bool isLegalAddressImmediate(int64_t V, const Type *Ty) const;
00328 
00329     /// isLegalAddressImmediate - Return true if the GlobalValue can be used as
00330     /// the offset of the target addressing mode.
00331     virtual bool isLegalAddressImmediate(GlobalValue *GV) const;
00332 
00333     virtual bool
00334     IsEligibleForTailCallOptimization(SDValue Callee,
00335                                       unsigned CalleeCC,
00336                                       bool isVarArg,
00337                                       const SmallVectorImpl<ISD::InputArg> &Ins,
00338                                       SelectionDAG& DAG) const;
00339 
00340     virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
00341     
00342     virtual EVT getOptimalMemOpType(uint64_t Size, unsigned Align,
00343                                     bool isSrcConst, bool isSrcStr,
00344                                     SelectionDAG &DAG) const;
00345 
00346     /// getFunctionAlignment - Return the Log2 alignment of this function.
00347     virtual unsigned getFunctionAlignment(const Function *F) const;
00348 
00349   private:
00350     SDValue getFramePointerFrameIndex(SelectionDAG & DAG) const;
00351     SDValue getReturnAddrFrameIndex(SelectionDAG & DAG) const;
00352 
00353     SDValue EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
00354                                          int SPDiff,
00355                                          SDValue Chain,
00356                                          SDValue &LROpOut,
00357                                          SDValue &FPOpOut,
00358                                          bool isDarwinABI,
00359                                          DebugLoc dl);
00360 
00361     SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG);
00362     SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG);
00363     SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG);
00364     SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG);
00365     SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG);
00366     SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG);
00367     SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG);
00368     SDValue LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG);
00369     SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
00370                            int VarArgsFrameIndex, int VarArgsStackOffset,
00371                            unsigned VarArgsNumGPR, unsigned VarArgsNumFPR,
00372                            const PPCSubtarget &Subtarget);
00373     SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG, int VarArgsFrameIndex,
00374                          int VarArgsStackOffset, unsigned VarArgsNumGPR,
00375                          unsigned VarArgsNumFPR, const PPCSubtarget &Subtarget);
00376     SDValue LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
00377                                 const PPCSubtarget &Subtarget);
00378     SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
00379                                       const PPCSubtarget &Subtarget);
00380     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
00381     SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
00382     SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG);
00383     SDValue LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG);
00384     SDValue LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG);
00385     SDValue LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG);
00386     SDValue LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG);
00387     SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG);
00388     SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG);
00389     SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG);
00390     SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG);
00391     SDValue LowerMUL(SDValue Op, SelectionDAG &DAG);
00392 
00393     SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
00394                             unsigned CallConv, bool isVarArg,
00395                             const SmallVectorImpl<ISD::InputArg> &Ins,
00396                             DebugLoc dl, SelectionDAG &DAG,
00397                             SmallVectorImpl<SDValue> &InVals);
00398     SDValue FinishCall(unsigned CallConv, DebugLoc dl, bool isTailCall,
00399                        bool isVarArg,
00400                        SelectionDAG &DAG,
00401                        SmallVector<std::pair<unsigned, SDValue>, 8>
00402                          &RegsToPass,
00403                        SDValue InFlag, SDValue Chain,
00404                        SDValue &Callee,
00405                        int SPDiff, unsigned NumBytes,
00406                        const SmallVectorImpl<ISD::InputArg> &Ins,
00407                        SmallVectorImpl<SDValue> &InVals);
00408 
00409     virtual SDValue
00410       LowerFormalArguments(SDValue Chain,
00411                            unsigned CallConv, bool isVarArg,
00412                            const SmallVectorImpl<ISD::InputArg> &Ins,
00413                            DebugLoc dl, SelectionDAG &DAG,
00414                            SmallVectorImpl<SDValue> &InVals);
00415 
00416     virtual SDValue
00417       LowerCall(SDValue Chain, SDValue Callee,
00418                 unsigned CallConv, bool isVarArg, bool isTailCall,
00419                 const SmallVectorImpl<ISD::OutputArg> &Outs,
00420                 const SmallVectorImpl<ISD::InputArg> &Ins,
00421                 DebugLoc dl, SelectionDAG &DAG,
00422                 SmallVectorImpl<SDValue> &InVals);
00423 
00424     virtual SDValue
00425       LowerReturn(SDValue Chain,
00426                   unsigned CallConv, bool isVarArg,
00427                   const SmallVectorImpl<ISD::OutputArg> &Outs,
00428                   DebugLoc dl, SelectionDAG &DAG);
00429 
00430     SDValue
00431       LowerFormalArguments_Darwin(SDValue Chain,
00432                                   unsigned CallConv, bool isVarArg,
00433                                   const SmallVectorImpl<ISD::InputArg> &Ins,
00434                                   DebugLoc dl, SelectionDAG &DAG,
00435                                   SmallVectorImpl<SDValue> &InVals);
00436     SDValue
00437       LowerFormalArguments_SVR4(SDValue Chain,
00438                                 unsigned CallConv, bool isVarArg,
00439                                 const SmallVectorImpl<ISD::InputArg> &Ins,
00440                                 DebugLoc dl, SelectionDAG &DAG,
00441                                 SmallVectorImpl<SDValue> &InVals);
00442 
00443     SDValue
00444       LowerCall_Darwin(SDValue Chain, SDValue Callee,
00445                        unsigned CallConv, bool isVarArg, bool isTailCall,
00446                        const SmallVectorImpl<ISD::OutputArg> &Outs,
00447                        const SmallVectorImpl<ISD::InputArg> &Ins,
00448                        DebugLoc dl, SelectionDAG &DAG,
00449                        SmallVectorImpl<SDValue> &InVals);
00450     SDValue
00451       LowerCall_SVR4(SDValue Chain, SDValue Callee,
00452                      unsigned CallConv, bool isVarArg, bool isTailCall,
00453                      const SmallVectorImpl<ISD::OutputArg> &Outs,
00454                      const SmallVectorImpl<ISD::InputArg> &Ins,
00455                      DebugLoc dl, SelectionDAG &DAG,
00456                      SmallVectorImpl<SDValue> &InVals);
00457   };
00458 }
00459 
00460 #endif   // LLVM_TARGET_POWERPC_PPC32ISELLOWERING_H