LLVM API Documentation

PIC16ISelLowering.h

Go to the documentation of this file.
00001 //===-- PIC16ISelLowering.h - PIC16 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 PIC16 uses to lower LLVM code into a
00011 // selection DAG.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef PIC16ISELLOWERING_H
00016 #define PIC16ISELLOWERING_H
00017 
00018 #include "PIC16.h"
00019 #include "PIC16Subtarget.h"
00020 #include "llvm/CodeGen/SelectionDAG.h"
00021 #include "llvm/Target/TargetLowering.h"
00022 #include <map>
00023 
00024 namespace llvm {
00025   namespace PIC16ISD {
00026     enum NodeType {
00027       // Start the numbering from where ISD NodeType finishes.
00028       FIRST_NUMBER = ISD::BUILTIN_OP_END,
00029 
00030       Lo,            // Low 8-bits of GlobalAddress.
00031       Hi,            // High 8-bits of GlobalAddress.
00032       PIC16Load,
00033       PIC16LdArg,   // This is replica of PIC16Load but used to load function 
00034                     // arguments and is being used for facilitating for some 
00035                     // store removal optimizations. 
00036 
00037       PIC16LdWF,
00038       PIC16Store,
00039       PIC16StWF,
00040       Banksel,
00041       MTLO,          // Move to low part of FSR
00042       MTHI,          // Move to high part of FSR
00043       MTPCLATH,      // Move to PCLATCH
00044       PIC16Connect,  // General connector for PIC16 nodes
00045       BCF,
00046       LSLF,          // PIC16 Logical shift left
00047       LRLF,          // PIC16 Logical shift right
00048       RLF,           // Rotate left through carry
00049       RRF,           // Rotate right through carry
00050       CALL,          // PIC16 Call instruction 
00051       CALLW,         // PIC16 CALLW instruction 
00052       SUBCC,         // Compare for equality or inequality.
00053       SELECT_ICC,    // Psuedo to be caught in schedular and expanded to brcond.
00054       BRCOND,        // Conditional branch.
00055       RET,           // Return.
00056       Dummy
00057     };
00058 
00059     // Keep track of different address spaces. 
00060     enum AddressSpace {
00061       RAM_SPACE = 0,   // RAM address space
00062       ROM_SPACE = 1    // ROM address space number is 1
00063     };
00064     enum PIC16Libcall {
00065       MUL_I8 = RTLIB::UNKNOWN_LIBCALL + 1,
00066       SRA_I8,
00067       SLL_I8,
00068       SRL_I8,
00069       PIC16UnknownCall
00070     };
00071   }
00072 
00073 
00074   //===--------------------------------------------------------------------===//
00075   // TargetLowering Implementation
00076   //===--------------------------------------------------------------------===//
00077   class PIC16TargetLowering : public TargetLowering {
00078   public:
00079     explicit PIC16TargetLowering(PIC16TargetMachine &TM);
00080 
00081     /// getTargetNodeName - This method returns the name of a target specific
00082     /// DAG node.
00083     virtual const char *getTargetNodeName(unsigned Opcode) const;
00084     /// getSetCCResultType - Return the ISD::SETCC ValueType
00085     virtual MVT::SimpleValueType getSetCCResultType(EVT ValType) const;
00086     SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
00087     SDValue LowerMUL(SDValue Op, SelectionDAG &DAG);
00088     SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
00089     SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
00090     SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
00091     // Call returns
00092     SDValue 
00093     LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
00094                           const SmallVectorImpl<ISD::InputArg> &Ins,
00095                           DebugLoc dl, SelectionDAG &DAG,
00096                           SmallVectorImpl<SDValue> &InVals);
00097     SDValue 
00098     LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
00099                              SDValue DataAddr_Lo, SDValue DataAddr_Hi,
00100                             const SmallVectorImpl<ISD::InputArg> &Ins,
00101                             DebugLoc dl, SelectionDAG &DAG,
00102                             SmallVectorImpl<SDValue> &InVals);
00103 
00104     // Call arguments
00105     SDValue 
00106     LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
00107                              const SmallVectorImpl<ISD::OutputArg> &Outs,
00108                              DebugLoc dl, SelectionDAG &DAG);
00109 
00110     SDValue 
00111     LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
00112                                SDValue DataAddr_Lo, SDValue DataAddr_Hi, 
00113                                const SmallVectorImpl<ISD::OutputArg> &Outs,
00114                                const SmallVectorImpl<ISD::InputArg> &Ins,
00115                                DebugLoc dl, SelectionDAG &DAG);
00116 
00117     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
00118     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
00119     SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
00120                         SelectionDAG &DAG, DebugLoc dl);
00121     virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
00122                                                   MachineBasicBlock *MBB) const;
00123 
00124 
00125     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
00126     virtual void ReplaceNodeResults(SDNode *N,
00127                                     SmallVectorImpl<SDValue> &Results,
00128                                     SelectionDAG &DAG);
00129     virtual void LowerOperationWrapper(SDNode *N,
00130                                        SmallVectorImpl<SDValue> &Results,
00131                                        SelectionDAG &DAG);
00132 
00133     virtual SDValue
00134     LowerFormalArguments(SDValue Chain,
00135                          unsigned CallConv,
00136                          bool isVarArg,
00137                          const SmallVectorImpl<ISD::InputArg> &Ins,
00138                          DebugLoc dl, SelectionDAG &DAG,
00139                          SmallVectorImpl<SDValue> &InVals);
00140 
00141     virtual SDValue
00142       LowerCall(SDValue Chain, SDValue Callee,
00143                 unsigned CallConv, bool isVarArg, bool isTailCall,
00144                 const SmallVectorImpl<ISD::OutputArg> &Outs,
00145                 const SmallVectorImpl<ISD::InputArg> &Ins,
00146                 DebugLoc dl, SelectionDAG &DAG,
00147                 SmallVectorImpl<SDValue> &InVals);
00148 
00149     virtual SDValue
00150       LowerReturn(SDValue Chain,
00151                   unsigned CallConv, bool isVarArg,
00152                   const SmallVectorImpl<ISD::OutputArg> &Outs,
00153                   DebugLoc dl, SelectionDAG &DAG);
00154 
00155     SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
00156     SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
00157     SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
00158     SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
00159     SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
00160 
00161     SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
00162     SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
00163     SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
00164 
00165     // This function returns the Tmp Offset for FrameIndex. If any TmpOffset 
00166     // already exists for the FI then it returns the same else it creates the 
00167     // new offset and returns.
00168     unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size); 
00169     void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); }
00170     void InitReservedFrameCount(const Function *F); 
00171 
00172     // Return the size of Tmp variable 
00173     unsigned GetTmpSize() { return TmpSize; }
00174     void SetTmpSize(unsigned Size) { TmpSize = Size; }
00175 
00176     /// getFunctionAlignment - Return the Log2 alignment of this function.
00177     virtual unsigned getFunctionAlignment(const Function *) const {
00178       // FIXME: The function never seems to be aligned.
00179       return 1;
00180     }
00181   private:
00182     // If the Node is a BUILD_PAIR representing a direct Address,
00183     // then this function will return true.
00184     bool isDirectAddress(const SDValue &Op);
00185 
00186     // If the Node is a DirectAddress in ROM_SPACE then this 
00187     // function will return true
00188     bool isRomAddress(const SDValue &Op);
00189 
00190     // Extract the Lo and Hi component of Op. 
00191     void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo, 
00192                           SDValue &Hi); 
00193 
00194 
00195     // Load pointer can be a direct or indirect address. In PIC16 direct
00196     // addresses need Banksel and Indirect addresses need to be loaded to
00197     // FSR first. Handle address specific cases here.
00198     void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain, 
00199                          SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
00200 
00201     // FrameIndex should be broken down into ExternalSymbol and FrameOffset. 
00202     void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES, 
00203                             int &Offset);
00204 
00205     // For indirect calls data address of the callee frame need to be
00206     // extracted. This function fills the arguments DataAddr_Lo and 
00207     // DataAddr_Hi with the address of the callee frame.
00208     void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
00209                         SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
00210                         SelectionDAG &DAG); 
00211 
00212     // We can not have both operands of a binary operation in W.
00213     // This function is used to put one operand on stack and generate a load.
00214     SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl); 
00215 
00216     // This function checks if we need to put an operand of an operation on
00217     // stack and generate a load or not.
00218     bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp); 
00219 
00220     /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
00221     /// make the right decision when generating code for different targets.
00222     const PIC16Subtarget *Subtarget;
00223 
00224 
00225     // Extending the LIB Call framework of LLVM
00226     // to hold the names of PIC16Libcalls.
00227     const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall]; 
00228 
00229     // To set and retrieve the lib call names.
00230     void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
00231     const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
00232 
00233     // Make PIC16 Libcall.
00234     SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, EVT RetVT, 
00235                              const SDValue *Ops, unsigned NumOps, bool isSigned,
00236                              SelectionDAG &DAG, DebugLoc dl);
00237 
00238     // Check if operation has a direct load operand.
00239     inline bool isDirectLoad(const SDValue Op);
00240 
00241   private:
00242     // The frameindexes generated for spill/reload are stack based.
00243     // This maps maintain zero based indexes for these FIs.
00244     std::map<unsigned, unsigned> FiTmpOffsetMap;
00245     unsigned TmpSize;
00246 
00247     // These are the frames for return value and argument passing 
00248     // These FrameIndices will be expanded to foo.frame external symbol
00249     // and all others will be expanded to foo.tmp external symbol.
00250     unsigned ReservedFrameCount; 
00251   };
00252 } // namespace llvm
00253 
00254 #endif // PIC16ISELLOWERING_H