LLVM API Documentation

SelectionDAGISel.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- 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 implements the SelectionDAGISel class, which is used as the common
00011 // base class for SelectionDAG-based instruction selectors.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_SELECTIONDAGISEL_H
00016 #define LLVM_CODEGEN_SELECTIONDAGISEL_H
00017 
00018 #include "llvm/CodeGen/MachineFunctionPass.h"
00019 #include "llvm/CodeGen/SelectionDAG.h"
00020 #include "llvm/IR/BasicBlock.h"
00021 #include "llvm/Pass.h"
00022 
00023 namespace llvm {
00024   class FastISel;
00025   class SelectionDAGBuilder;
00026   class SDValue;
00027   class MachineRegisterInfo;
00028   class MachineBasicBlock;
00029   class MachineFunction;
00030   class MachineInstr;
00031   class TargetLowering;
00032   class TargetLibraryInfo;
00033   class TargetInstrInfo;
00034   class TargetTransformInfo;
00035   class FunctionLoweringInfo;
00036   class ScheduleHazardRecognizer;
00037   class GCFunctionInfo;
00038   class ScheduleDAGSDNodes;
00039   class LoadInst;
00040 
00041 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
00042 /// pattern-matching instruction selectors.
00043 class SelectionDAGISel : public MachineFunctionPass {
00044 public:
00045   const TargetMachine &TM;
00046   const TargetLowering &TLI;
00047   const TargetLibraryInfo *LibInfo;
00048   const TargetTransformInfo *TTI;
00049   FunctionLoweringInfo *FuncInfo;
00050   MachineFunction *MF;
00051   MachineRegisterInfo *RegInfo;
00052   SelectionDAG *CurDAG;
00053   SelectionDAGBuilder *SDB;
00054   AliasAnalysis *AA;
00055   GCFunctionInfo *GFI;
00056   CodeGenOpt::Level OptLevel;
00057   static char ID;
00058 
00059   explicit SelectionDAGISel(const TargetMachine &tm,
00060                             CodeGenOpt::Level OL = CodeGenOpt::Default);
00061   virtual ~SelectionDAGISel();
00062 
00063   const TargetLowering &getTargetLowering() { return TLI; }
00064 
00065   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
00066 
00067   virtual bool runOnMachineFunction(MachineFunction &MF);
00068 
00069   virtual void EmitFunctionEntryCode() {}
00070 
00071   /// PreprocessISelDAG - This hook allows targets to hack on the graph before
00072   /// instruction selection starts.
00073   virtual void PreprocessISelDAG() {}
00074 
00075   /// PostprocessISelDAG() - This hook allows the target to hack on the graph
00076   /// right after selection.
00077   virtual void PostprocessISelDAG() {}
00078 
00079   /// Select - Main hook targets implement to select a node.
00080   virtual SDNode *Select(SDNode *N) = 0;
00081 
00082   /// SelectInlineAsmMemoryOperand - Select the specified address as a target
00083   /// addressing mode, according to the specified constraint code.  If this does
00084   /// not match or is not implemented, return true.  The resultant operands
00085   /// (which will appear in the machine instruction) should be added to the
00086   /// OutOps vector.
00087   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
00088                                             char ConstraintCode,
00089                                             std::vector<SDValue> &OutOps) {
00090     return true;
00091   }
00092 
00093   /// IsProfitableToFold - Returns true if it's profitable to fold the specific
00094   /// operand node N of U during instruction selection that starts at Root.
00095   virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
00096 
00097   /// IsLegalToFold - Returns true if the specific operand node N of
00098   /// U can be folded during instruction selection that starts at Root.
00099   /// FIXME: This is a static member function because the MSP430/X86
00100   /// targets, which uses it during isel.  This could become a proper member.
00101   static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
00102                             CodeGenOpt::Level OptLevel,
00103                             bool IgnoreChains = false);
00104 
00105   // Opcodes used by the DAG state machine:
00106   enum BuiltinOpcodes {
00107     OPC_Scope,
00108     OPC_RecordNode,
00109     OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3,
00110     OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
00111     OPC_RecordMemRef,
00112     OPC_CaptureGlueInput,
00113     OPC_MoveChild,
00114     OPC_MoveParent,
00115     OPC_CheckSame,
00116     OPC_CheckPatternPredicate,
00117     OPC_CheckPredicate,
00118     OPC_CheckOpcode,
00119     OPC_SwitchOpcode,
00120     OPC_CheckType,
00121     OPC_SwitchType,
00122     OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
00123     OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
00124     OPC_CheckChild6Type, OPC_CheckChild7Type,
00125     OPC_CheckInteger,
00126     OPC_CheckCondCode,
00127     OPC_CheckValueType,
00128     OPC_CheckComplexPat,
00129     OPC_CheckAndImm, OPC_CheckOrImm,
00130     OPC_CheckFoldableChainNode,
00131 
00132     OPC_EmitInteger,
00133     OPC_EmitRegister,
00134     OPC_EmitRegister2,
00135     OPC_EmitConvertToTarget,
00136     OPC_EmitMergeInputChains,
00137     OPC_EmitMergeInputChains1_0,
00138     OPC_EmitMergeInputChains1_1,
00139     OPC_EmitCopyToReg,
00140     OPC_EmitNodeXForm,
00141     OPC_EmitNode,
00142     OPC_MorphNodeTo,
00143     OPC_MarkGlueResults,
00144     OPC_CompleteMatch
00145   };
00146 
00147   enum {
00148     OPFL_None       = 0,  // Node has no chain or glue input and isn't variadic.
00149     OPFL_Chain      = 1,     // Node has a chain input.
00150     OPFL_GlueInput  = 2,     // Node has a glue input.
00151     OPFL_GlueOutput = 4,     // Node has a glue output.
00152     OPFL_MemRefs    = 8,     // Node gets accumulated MemRefs.
00153     OPFL_Variadic0  = 1<<4,  // Node is variadic, root has 0 fixed inputs.
00154     OPFL_Variadic1  = 2<<4,  // Node is variadic, root has 1 fixed inputs.
00155     OPFL_Variadic2  = 3<<4,  // Node is variadic, root has 2 fixed inputs.
00156     OPFL_Variadic3  = 4<<4,  // Node is variadic, root has 3 fixed inputs.
00157     OPFL_Variadic4  = 5<<4,  // Node is variadic, root has 4 fixed inputs.
00158     OPFL_Variadic5  = 6<<4,  // Node is variadic, root has 5 fixed inputs.
00159     OPFL_Variadic6  = 7<<4,  // Node is variadic, root has 6 fixed inputs.
00160 
00161     OPFL_VariadicInfo = OPFL_Variadic6
00162   };
00163 
00164   /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
00165   /// number of fixed arity values that should be skipped when copying from the
00166   /// root.
00167   static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
00168     return ((Flags&OPFL_VariadicInfo) >> 4)-1;
00169   }
00170 
00171 
00172 protected:
00173   /// DAGSize - Size of DAG being instruction selected.
00174   ///
00175   unsigned DAGSize;
00176 
00177   /// ReplaceUses - replace all uses of the old node F with the use
00178   /// of the new node T.
00179   void ReplaceUses(SDValue F, SDValue T) {
00180     CurDAG->ReplaceAllUsesOfValueWith(F, T);
00181   }
00182 
00183   /// ReplaceUses - replace all uses of the old nodes F with the use
00184   /// of the new nodes T.
00185   void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
00186     CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num);
00187   }
00188 
00189   /// ReplaceUses - replace all uses of the old node F with the use
00190   /// of the new node T.
00191   void ReplaceUses(SDNode *F, SDNode *T) {
00192     CurDAG->ReplaceAllUsesWith(F, T);
00193   }
00194 
00195 
00196   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
00197   /// by tblgen.  Others should not call it.
00198   void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
00199 
00200 
00201 public:
00202   // Calls to these predicates are generated by tblgen.
00203   bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
00204                     int64_t DesiredMaskS) const;
00205   bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
00206                     int64_t DesiredMaskS) const;
00207 
00208 
00209   /// CheckPatternPredicate - This function is generated by tblgen in the
00210   /// target.  It runs the specified pattern predicate and returns true if it
00211   /// succeeds or false if it fails.  The number is a private implementation
00212   /// detail to the code tblgen produces.
00213   virtual bool CheckPatternPredicate(unsigned PredNo) const {
00214     llvm_unreachable("Tblgen should generate the implementation of this!");
00215   }
00216 
00217   /// CheckNodePredicate - This function is generated by tblgen in the target.
00218   /// It runs node predicate number PredNo and returns true if it succeeds or
00219   /// false if it fails.  The number is a private implementation
00220   /// detail to the code tblgen produces.
00221   virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
00222     llvm_unreachable("Tblgen should generate the implementation of this!");
00223   }
00224 
00225   virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,
00226                                    unsigned PatternNo,
00227                         SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {
00228     llvm_unreachable("Tblgen should generate the implementation of this!");
00229   }
00230 
00231   virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
00232     llvm_unreachable("Tblgen should generate this!");
00233   }
00234 
00235   SDNode *SelectCodeCommon(SDNode *NodeToMatch,
00236                            const unsigned char *MatcherTable,
00237                            unsigned TableSize);
00238 
00239 private:
00240 
00241   // Calls to these functions are generated by tblgen.
00242   SDNode *Select_INLINEASM(SDNode *N);
00243   SDNode *Select_UNDEF(SDNode *N);
00244   void CannotYetSelect(SDNode *N);
00245 
00246 private:
00247   void DoInstructionSelection();
00248   SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
00249                     const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo);
00250 
00251   void PrepareEHLandingPad();
00252 
00253   /// \brief Perform instruction selection on all basic blocks in the function.
00254   void SelectAllBasicBlocks(const Function &Fn);
00255 
00256   /// \brief Perform instruction selection on a single basic block, for
00257   /// instructions between \p Begin and \p End.  \p HadTailCall will be set
00258   /// to true if a call in the block was translated as a tail call.
00259   void SelectBasicBlock(BasicBlock::const_iterator Begin,
00260                         BasicBlock::const_iterator End,
00261                         bool &HadTailCall);
00262   void FinishBasicBlock();
00263 
00264   void CodeGenAndEmitDAG();
00265 
00266   /// \brief Generate instructions for lowering the incoming arguments of the
00267   /// given function.
00268   void LowerArguments(const Function &F);
00269 
00270   void ComputeLiveOutVRegInfo();
00271 
00272   /// Create the scheduler. If a specific scheduler was specified
00273   /// via the SchedulerRegistry, use it, otherwise select the
00274   /// one preferred by the target.
00275   ///
00276   ScheduleDAGSDNodes *CreateScheduler();
00277 
00278   /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
00279   /// state machines that start with a OPC_SwitchOpcode node.
00280   std::vector<unsigned> OpcodeOffset;
00281 
00282   void UpdateChainsAndGlue(SDNode *NodeToMatch, SDValue InputChain,
00283                            const SmallVectorImpl<SDNode*> &ChainNodesMatched,
00284                            SDValue InputGlue, const SmallVectorImpl<SDNode*> &F,
00285                            bool isMorphNodeTo);
00286 
00287 };
00288 
00289 }
00290 
00291 #endif /* LLVM_CODEGEN_SELECTIONDAGISEL_H */