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