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