move some code out of DAGISelHeader up to SelectionDAGISel.h where it
[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_SwitchOpcode,
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     
127     OPC_EmitInteger,
128     OPC_EmitRegister,
129     OPC_EmitConvertToTarget,
130     OPC_EmitMergeInputChains,
131     OPC_EmitCopyToReg,
132     OPC_EmitNodeXForm,
133     OPC_EmitNode,
134     OPC_MorphNodeTo,
135     OPC_MarkFlagResults,
136     OPC_CompleteMatch
137   };
138   
139   enum {
140     OPFL_None       = 0,     // Node has no chain or flag input and isn't variadic.
141     OPFL_Chain      = 1,     // Node has a chain input.
142     OPFL_FlagInput  = 2,     // Node has a flag input.
143     OPFL_FlagOutput = 4,     // Node has a flag output.
144     OPFL_MemRefs    = 8,     // Node gets accumulated MemRefs.
145     OPFL_Variadic0  = 1<<4,  // Node is variadic, root has 0 fixed inputs.
146     OPFL_Variadic1  = 2<<4,  // Node is variadic, root has 1 fixed inputs.
147     OPFL_Variadic2  = 3<<4,  // Node is variadic, root has 2 fixed inputs.
148     OPFL_Variadic3  = 4<<4,  // Node is variadic, root has 3 fixed inputs.
149     OPFL_Variadic4  = 5<<4,  // Node is variadic, root has 4 fixed inputs.
150     OPFL_Variadic5  = 6<<4,  // Node is variadic, root has 5 fixed inputs.
151     OPFL_Variadic6  = 7<<4,  // Node is variadic, root has 6 fixed inputs.
152     
153     OPFL_VariadicInfo = OPFL_Variadic6
154   };
155   
156   /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
157   /// number of fixed arity values that should be skipped when copying from the
158   /// root.
159   static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
160     return ((Flags&OPFL_VariadicInfo) >> 4)-1;
161   }
162   
163   
164 protected:
165   /// DAGSize - Size of DAG being instruction selected.
166   ///
167   unsigned DAGSize;
168   
169   /// ISelPosition - Node iterator marking the current position of
170   /// instruction selection as it procedes through the topologically-sorted
171   /// node list.
172   SelectionDAG::allnodes_iterator ISelPosition;
173
174   
175   /// ISelUpdater - helper class to handle updates of the 
176   /// instruction selection graph.
177   class ISelUpdater : public SelectionDAG::DAGUpdateListener {
178     SelectionDAG::allnodes_iterator &ISelPosition;
179   public:
180     explicit ISelUpdater(SelectionDAG::allnodes_iterator &isp)
181       : ISelPosition(isp) {}
182     
183     /// NodeDeleted - Handle nodes deleted from the graph. If the
184     /// node being deleted is the current ISelPosition node, update
185     /// ISelPosition.
186     ///
187     virtual void NodeDeleted(SDNode *N, SDNode *E) {
188       if (ISelPosition == SelectionDAG::allnodes_iterator(N))
189         ++ISelPosition;
190     }
191     
192     /// NodeUpdated - Ignore updates for now.
193     virtual void NodeUpdated(SDNode *N) {}
194   };
195   
196   /// ReplaceUses - replace all uses of the old node F with the use
197   /// of the new node T.
198   void ReplaceUses(SDValue F, SDValue T) {
199     ISelUpdater ISU(ISelPosition);
200     CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISU);
201   }
202   
203   /// ReplaceUses - replace all uses of the old nodes F with the use
204   /// of the new nodes T.
205   void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
206     ISelUpdater ISU(ISelPosition);
207     CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num, &ISU);
208   }
209   
210   /// ReplaceUses - replace all uses of the old node F with the use
211   /// of the new node T.
212   void ReplaceUses(SDNode *F, SDNode *T) {
213     ISelUpdater ISU(ISelPosition);
214     CurDAG->ReplaceAllUsesWith(F, T, &ISU);
215   }
216   
217
218   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
219   /// by tblgen.  Others should not call it.
220   void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
221
222   // Calls to these predicates are generated by tblgen.
223   bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
224                     int64_t DesiredMaskS) const;
225   bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
226                     int64_t DesiredMaskS) const;
227   
228   
229   /// CheckPatternPredicate - This function is generated by tblgen in the
230   /// target.  It runs the specified pattern predicate and returns true if it
231   /// succeeds or false if it fails.  The number is a private implementation
232   /// detail to the code tblgen produces.
233   virtual bool CheckPatternPredicate(unsigned PredNo) const {
234     assert(0 && "Tblgen should generate the implementation of this!");
235     return 0;
236   }
237
238   /// CheckNodePredicate - This function is generated by tblgen in the target.
239   /// It runs node predicate number PredNo and returns true if it succeeds or
240   /// false if it fails.  The number is a private implementation
241   /// detail to the code tblgen produces.
242   virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
243     assert(0 && "Tblgen should generate the implementation of this!");
244     return 0;
245   }
246   
247   virtual bool CheckComplexPattern(SDNode *Root, SDValue N, unsigned PatternNo,
248                                    SmallVectorImpl<SDValue> &Result) {
249     assert(0 && "Tblgen should generate the implementation of this!");
250     return false;
251   }
252   
253   virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
254     assert(0 && "Tblgen shoudl generate this!");
255     return SDValue();
256   }
257
258   
259   // Calls to these functions are generated by tblgen.
260   SDNode *Select_INLINEASM(SDNode *N);
261   SDNode *Select_UNDEF(SDNode *N);
262   SDNode *Select_EH_LABEL(SDNode *N);
263   
264   SDNode *SelectCodeCommon(SDNode *NodeToMatch,
265                            const unsigned char *MatcherTable,
266                            unsigned TableSize);
267   void CannotYetSelect(SDNode *N);
268   void CannotYetSelectIntrinsic(SDNode *N);
269
270 private:
271   void SelectAllBasicBlocks(Function &Fn, MachineFunction &MF,
272                             MachineModuleInfo *MMI,
273                             DwarfWriter *DW,
274                             const TargetInstrInfo &TII);
275   void FinishBasicBlock();
276
277   void SelectBasicBlock(BasicBlock *LLVMBB,
278                         BasicBlock::iterator Begin,
279                         BasicBlock::iterator End,
280                         bool &HadTailCall);
281   void CodeGenAndEmitDAG();
282   void LowerArguments(BasicBlock *BB);
283   
284   void ShrinkDemandedOps();
285   void ComputeLiveOutVRegInfo();
286
287   void HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB);
288
289   bool HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, FastISel *F);
290
291   /// Create the scheduler. If a specific scheduler was specified
292   /// via the SchedulerRegistry, use it, otherwise select the
293   /// one preferred by the target.
294   ///
295   ScheduleDAGSDNodes *CreateScheduler();
296   
297   /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
298   /// state machines that start with a OPC_SwitchOpcode node.
299   std::vector<unsigned> OpcodeOffset;
300 };
301
302 }
303
304 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */