hoist the new isel interpreter out of DAGISelHeader.h
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAGISel.h
1 //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- 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 implements the SelectionDAGISel class, which is used as the common
11 // base class for SelectionDAG-based instruction selectors.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_ISEL_H
16 #define LLVM_CODEGEN_SELECTIONDAG_ISEL_H
17
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Constant.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23
24 namespace llvm {
25   class FastISel;
26   class SelectionDAGBuilder;
27   class SDValue;
28   class MachineRegisterInfo;
29   class MachineBasicBlock;
30   class MachineFunction;
31   class MachineInstr;
32   class MachineModuleInfo;
33   class DwarfWriter;
34   class TargetLowering;
35   class TargetInstrInfo;
36   class FunctionLoweringInfo;
37   class ScheduleHazardRecognizer;
38   class GCFunctionInfo;
39   class ScheduleDAGSDNodes;
40  
41 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
42 /// pattern-matching instruction selectors.
43 class SelectionDAGISel : public MachineFunctionPass {
44 public:
45   const TargetMachine &TM;
46   TargetLowering &TLI;
47   FunctionLoweringInfo *FuncInfo;
48   MachineFunction *MF;
49   MachineRegisterInfo *RegInfo;
50   SelectionDAG *CurDAG;
51   SelectionDAGBuilder *SDB;
52   MachineBasicBlock *BB;
53   AliasAnalysis *AA;
54   GCFunctionInfo *GFI;
55   CodeGenOpt::Level OptLevel;
56   static char ID;
57
58   explicit SelectionDAGISel(TargetMachine &tm,
59                             CodeGenOpt::Level OL = CodeGenOpt::Default);
60   virtual ~SelectionDAGISel();
61   
62   TargetLowering &getTargetLowering() { return TLI; }
63
64   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
65
66   virtual bool runOnMachineFunction(MachineFunction &MF);
67
68   unsigned MakeReg(EVT VT);
69
70   virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
71   virtual void InstructionSelect() = 0;
72   
73   void SelectRootInit() {
74     DAGSize = CurDAG->AssignTopologicalOrder();
75   }
76
77   /// SelectInlineAsmMemoryOperand - Select the specified address as a target
78   /// addressing mode, according to the specified constraint code.  If this does
79   /// not match or is not implemented, return true.  The resultant operands
80   /// (which will appear in the machine instruction) should be added to the
81   /// OutOps vector.
82   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
83                                             char ConstraintCode,
84                                             std::vector<SDValue> &OutOps) {
85     return true;
86   }
87
88   /// IsProfitableToFold - Returns true if it's profitable to fold the specific
89   /// operand node N of U during instruction selection that starts at Root.
90   virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
91
92   /// IsLegalToFold - Returns true if the specific operand node N of
93   /// U can be folded during instruction selection that starts at Root.
94   virtual bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root) const;
95
96   /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
97   /// to use for this target when scheduling the DAG.
98   virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
99   
100   
101   // Opcodes used by the DAG state machine:
102   enum BuiltinOpcodes {
103     OPC_Scope,
104     OPC_RecordNode,
105     OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3, 
106     OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
107     OPC_RecordMemRef,
108     OPC_CaptureFlagInput,
109     OPC_MoveChild,
110     OPC_MoveParent,
111     OPC_CheckSame,
112     OPC_CheckPatternPredicate,
113     OPC_CheckPredicate,
114     OPC_CheckOpcode,
115     OPC_CheckMultiOpcode,
116     OPC_CheckType,
117     OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
118     OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
119     OPC_CheckChild6Type, OPC_CheckChild7Type,
120     OPC_CheckInteger,
121     OPC_CheckCondCode,
122     OPC_CheckValueType,
123     OPC_CheckComplexPat,
124     OPC_CheckAndImm, OPC_CheckOrImm,
125     OPC_CheckFoldableChainNode,
126     OPC_CheckChainCompatible,
127     
128     OPC_EmitInteger,
129     OPC_EmitRegister,
130     OPC_EmitConvertToTarget,
131     OPC_EmitMergeInputChains,
132     OPC_EmitCopyToReg,
133     OPC_EmitNodeXForm,
134     OPC_EmitNode,
135     OPC_MorphNodeTo,
136     OPC_MarkFlagResults,
137     OPC_CompleteMatch
138   };
139   
140   enum {
141     OPFL_None       = 0,     // Node has no chain or flag input and isn't variadic.
142     OPFL_Chain      = 1,     // Node has a chain input.
143     OPFL_FlagInput  = 2,     // Node has a flag input.
144     OPFL_FlagOutput = 4,     // Node has a flag output.
145     OPFL_MemRefs    = 8,     // Node gets accumulated MemRefs.
146     OPFL_Variadic0  = 1<<4,  // Node is variadic, root has 0 fixed inputs.
147     OPFL_Variadic1  = 2<<4,  // Node is variadic, root has 1 fixed inputs.
148     OPFL_Variadic2  = 3<<4,  // Node is variadic, root has 2 fixed inputs.
149     OPFL_Variadic3  = 4<<4,  // Node is variadic, root has 3 fixed inputs.
150     OPFL_Variadic4  = 5<<4,  // Node is variadic, root has 4 fixed inputs.
151     OPFL_Variadic5  = 6<<4,  // Node is variadic, root has 5 fixed inputs.
152     OPFL_Variadic6  = 7<<4,  // Node is variadic, root has 6 fixed inputs.
153     
154     OPFL_VariadicInfo = OPFL_Variadic6
155   };
156   
157   /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
158   /// number of fixed arity values that should be skipped when copying from the
159   /// root.
160   static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
161     return ((Flags&OPFL_VariadicInfo) >> 4)-1;
162   }
163   
164   
165 protected:
166   /// DAGSize - Size of DAG being instruction selected.
167   ///
168   unsigned DAGSize;
169
170   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
171   /// by tblgen.  Others should not call it.
172   void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
173
174   // Calls to these predicates are generated by tblgen.
175   bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
176                     int64_t DesiredMaskS) const;
177   bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
178                     int64_t DesiredMaskS) const;
179   
180   
181   /// CheckPatternPredicate - This function is generated by tblgen in the
182   /// target.  It runs the specified pattern predicate and returns true if it
183   /// succeeds or false if it fails.  The number is a private implementation
184   /// detail to the code tblgen produces.
185   virtual bool CheckPatternPredicate(unsigned PredNo) const {
186     assert(0 && "Tblgen should generate the implementation of this!");
187     return 0;
188   }
189
190   /// CheckNodePredicate - This function is generated by tblgen in the target.
191   /// It runs node predicate number PredNo and returns true if it succeeds or
192   /// false if it fails.  The number is a private implementation
193   /// detail to the code tblgen produces.
194   virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
195     assert(0 && "Tblgen should generate the implementation of this!");
196     return 0;
197   }
198   
199   virtual bool CheckComplexPattern(SDNode *Root, SDValue N, unsigned PatternNo,
200                                    SmallVectorImpl<SDValue> &Result) {
201     assert(0 && "Tblgen should generate the implementation of this!");
202     return false;
203   }
204   
205   virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
206     assert(0 && "Tblgen shoudl generate this!");
207     return SDValue();
208   }
209
210   
211   // Calls to these functions are generated by tblgen.
212   SDNode *Select_INLINEASM(SDNode *N);
213   SDNode *Select_UNDEF(SDNode *N);
214   SDNode *Select_EH_LABEL(SDNode *N);
215   
216   SDNode *SelectCodeCommon(SDNode *NodeToMatch,
217                            const unsigned char *MatcherTable,
218                            unsigned TableSize);
219   void CannotYetSelect(SDNode *N);
220   void CannotYetSelectIntrinsic(SDNode *N);
221
222 private:
223   void SelectAllBasicBlocks(Function &Fn, MachineFunction &MF,
224                             MachineModuleInfo *MMI,
225                             DwarfWriter *DW,
226                             const TargetInstrInfo &TII);
227   void FinishBasicBlock();
228
229   void SelectBasicBlock(BasicBlock *LLVMBB,
230                         BasicBlock::iterator Begin,
231                         BasicBlock::iterator End,
232                         bool &HadTailCall);
233   void CodeGenAndEmitDAG();
234   void LowerArguments(BasicBlock *BB);
235   
236   void ShrinkDemandedOps();
237   void ComputeLiveOutVRegInfo();
238
239   void HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB);
240
241   bool HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, FastISel *F);
242
243   /// Create the scheduler. If a specific scheduler was specified
244   /// via the SchedulerRegistry, use it, otherwise select the
245   /// one preferred by the target.
246   ///
247   ScheduleDAGSDNodes *CreateScheduler();
248 };
249
250 }
251
252 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */