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