introduce a new SwitchTypeMatcher node (which is analogous to
[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   
72   /// PreprocessISelDAG - This hook allows targets to hack on the graph before
73   /// instruction selection starts.
74   virtual void PreprocessISelDAG() {}
75   
76   /// PostprocessISelDAG() - This hook allows the target to hack on the graph
77   /// right after selection.
78   virtual void PostprocessISelDAG() {}
79   
80   /// Select - Main hook targets implement to select a node.
81   virtual SDNode *Select(SDNode *N) = 0;
82   
83   /// SelectInlineAsmMemoryOperand - Select the specified address as a target
84   /// addressing mode, according to the specified constraint code.  If this does
85   /// not match or is not implemented, return true.  The resultant operands
86   /// (which will appear in the machine instruction) should be added to the
87   /// OutOps vector.
88   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
89                                             char ConstraintCode,
90                                             std::vector<SDValue> &OutOps) {
91     return true;
92   }
93
94   /// IsProfitableToFold - Returns true if it's profitable to fold the specific
95   /// operand node N of U during instruction selection that starts at Root.
96   virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
97
98   /// IsLegalToFold - Returns true if the specific operand node N of
99   /// U can be folded during instruction selection that starts at Root.
100   bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
101                      bool IgnoreChains = false) const;
102
103   /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
104   /// to use for this target when scheduling the DAG.
105   virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
106   
107   
108   // Opcodes used by the DAG state machine:
109   enum BuiltinOpcodes {
110     OPC_Scope,
111     OPC_RecordNode,
112     OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3, 
113     OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
114     OPC_RecordMemRef,
115     OPC_CaptureFlagInput,
116     OPC_MoveChild,
117     OPC_MoveParent,
118     OPC_CheckSame,
119     OPC_CheckPatternPredicate,
120     OPC_CheckPredicate,
121     OPC_CheckOpcode,
122     OPC_SwitchOpcode,
123     OPC_CheckType,
124     OPC_SwitchType,
125     OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
126     OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
127     OPC_CheckChild6Type, OPC_CheckChild7Type,
128     OPC_CheckInteger,
129     OPC_CheckCondCode,
130     OPC_CheckValueType,
131     OPC_CheckComplexPat,
132     OPC_CheckAndImm, OPC_CheckOrImm,
133     OPC_CheckFoldableChainNode,
134     
135     OPC_EmitInteger,
136     OPC_EmitRegister,
137     OPC_EmitConvertToTarget,
138     OPC_EmitMergeInputChains,
139     OPC_EmitCopyToReg,
140     OPC_EmitNodeXForm,
141     OPC_EmitNode,
142     OPC_MorphNodeTo,
143     OPC_MarkFlagResults,
144     OPC_CompleteMatch
145   };
146   
147   enum {
148     OPFL_None       = 0,     // Node has no chain or flag input and isn't variadic.
149     OPFL_Chain      = 1,     // Node has a chain input.
150     OPFL_FlagInput  = 2,     // Node has a flag input.
151     OPFL_FlagOutput = 4,     // Node has a flag output.
152     OPFL_MemRefs    = 8,     // Node gets accumulated MemRefs.
153     OPFL_Variadic0  = 1<<4,  // Node is variadic, root has 0 fixed inputs.
154     OPFL_Variadic1  = 2<<4,  // Node is variadic, root has 1 fixed inputs.
155     OPFL_Variadic2  = 3<<4,  // Node is variadic, root has 2 fixed inputs.
156     OPFL_Variadic3  = 4<<4,  // Node is variadic, root has 3 fixed inputs.
157     OPFL_Variadic4  = 5<<4,  // Node is variadic, root has 4 fixed inputs.
158     OPFL_Variadic5  = 6<<4,  // Node is variadic, root has 5 fixed inputs.
159     OPFL_Variadic6  = 7<<4,  // Node is variadic, root has 6 fixed inputs.
160     
161     OPFL_VariadicInfo = OPFL_Variadic6
162   };
163   
164   /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
165   /// number of fixed arity values that should be skipped when copying from the
166   /// root.
167   static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
168     return ((Flags&OPFL_VariadicInfo) >> 4)-1;
169   }
170   
171   
172 protected:
173   /// DAGSize - Size of DAG being instruction selected.
174   ///
175   unsigned DAGSize;
176   
177   /// ISelPosition - Node iterator marking the current position of
178   /// instruction selection as it procedes through the topologically-sorted
179   /// node list.
180   SelectionDAG::allnodes_iterator ISelPosition;
181
182   
183   /// ISelUpdater - helper class to handle updates of the 
184   /// instruction selection graph.
185   class ISelUpdater : public SelectionDAG::DAGUpdateListener {
186     SelectionDAG::allnodes_iterator &ISelPosition;
187   public:
188     explicit ISelUpdater(SelectionDAG::allnodes_iterator &isp)
189       : ISelPosition(isp) {}
190     
191     /// NodeDeleted - Handle nodes deleted from the graph. If the
192     /// node being deleted is the current ISelPosition node, update
193     /// ISelPosition.
194     ///
195     virtual void NodeDeleted(SDNode *N, SDNode *E) {
196       if (ISelPosition == SelectionDAG::allnodes_iterator(N))
197         ++ISelPosition;
198     }
199     
200     /// NodeUpdated - Ignore updates for now.
201     virtual void NodeUpdated(SDNode *N) {}
202   };
203   
204   /// ReplaceUses - replace all uses of the old node F with the use
205   /// of the new node T.
206   void ReplaceUses(SDValue F, SDValue T) {
207     ISelUpdater ISU(ISelPosition);
208     CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISU);
209   }
210   
211   /// ReplaceUses - replace all uses of the old nodes F with the use
212   /// of the new nodes T.
213   void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
214     ISelUpdater ISU(ISelPosition);
215     CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num, &ISU);
216   }
217   
218   /// ReplaceUses - replace all uses of the old node F with the use
219   /// of the new node T.
220   void ReplaceUses(SDNode *F, SDNode *T) {
221     ISelUpdater ISU(ISelPosition);
222     CurDAG->ReplaceAllUsesWith(F, T, &ISU);
223   }
224   
225
226   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
227   /// by tblgen.  Others should not call it.
228   void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
229
230   // Calls to these predicates are generated by tblgen.
231   bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
232                     int64_t DesiredMaskS) const;
233   bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
234                     int64_t DesiredMaskS) const;
235   
236   
237   /// CheckPatternPredicate - This function is generated by tblgen in the
238   /// target.  It runs the specified pattern predicate and returns true if it
239   /// succeeds or false if it fails.  The number is a private implementation
240   /// detail to the code tblgen produces.
241   virtual bool CheckPatternPredicate(unsigned PredNo) const {
242     assert(0 && "Tblgen should generate the implementation of this!");
243     return 0;
244   }
245
246   /// CheckNodePredicate - This function is generated by tblgen in the target.
247   /// It runs node predicate number PredNo and returns true if it succeeds or
248   /// false if it fails.  The number is a private implementation
249   /// detail to the code tblgen produces.
250   virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
251     assert(0 && "Tblgen should generate the implementation of this!");
252     return 0;
253   }
254   
255   virtual bool CheckComplexPattern(SDNode *Root, SDValue N, unsigned PatternNo,
256                                    SmallVectorImpl<SDValue> &Result) {
257     assert(0 && "Tblgen should generate the implementation of this!");
258     return false;
259   }
260   
261   virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
262     assert(0 && "Tblgen shoudl generate this!");
263     return SDValue();
264   }
265
266   
267   // Calls to these functions are generated by tblgen.
268   SDNode *Select_INLINEASM(SDNode *N);
269   SDNode *Select_UNDEF(SDNode *N);
270   SDNode *Select_EH_LABEL(SDNode *N);
271   
272   SDNode *SelectCodeCommon(SDNode *NodeToMatch,
273                            const unsigned char *MatcherTable,
274                            unsigned TableSize);
275   void CannotYetSelect(SDNode *N);
276   void CannotYetSelectIntrinsic(SDNode *N);
277
278 private:
279   void DoInstructionSelection();
280   SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
281                     const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo);
282   
283   void SelectAllBasicBlocks(Function &Fn, MachineFunction &MF,
284                             MachineModuleInfo *MMI,
285                             DwarfWriter *DW,
286                             const TargetInstrInfo &TII);
287   void FinishBasicBlock();
288
289   void SelectBasicBlock(BasicBlock *LLVMBB,
290                         BasicBlock::iterator Begin,
291                         BasicBlock::iterator End,
292                         bool &HadTailCall);
293   void CodeGenAndEmitDAG();
294   void LowerArguments(BasicBlock *BB);
295   
296   void ShrinkDemandedOps();
297   void ComputeLiveOutVRegInfo();
298
299   void HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB);
300
301   bool HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, FastISel *F);
302
303   /// Create the scheduler. If a specific scheduler was specified
304   /// via the SchedulerRegistry, use it, otherwise select the
305   /// one preferred by the target.
306   ///
307   ScheduleDAGSDNodes *CreateScheduler();
308   
309   /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
310   /// state machines that start with a OPC_SwitchOpcode node.
311   std::vector<unsigned> OpcodeOffset;
312   
313   void UpdateChainsAndFlags(SDNode *NodeToMatch, SDValue InputChain,
314                             const SmallVectorImpl<SDNode*> &ChainNodesMatched,
315                             SDValue InputFlag,const SmallVectorImpl<SDNode*> &F,
316                             bool isMorphNodeTo);
317     
318 };
319
320 }
321
322 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */