]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/SelectionDAGISel.h
MFV of r219685, tzdata2011d
[FreeBSD/FreeBSD.git] / contrib / llvm / 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   class LoadInst;
38
39 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
40 /// pattern-matching instruction selectors.
41 class SelectionDAGISel : public MachineFunctionPass {
42 public:
43   const TargetMachine &TM;
44   const TargetLowering &TLI;
45   FunctionLoweringInfo *FuncInfo;
46   MachineFunction *MF;
47   MachineRegisterInfo *RegInfo;
48   SelectionDAG *CurDAG;
49   SelectionDAGBuilder *SDB;
50   AliasAnalysis *AA;
51   GCFunctionInfo *GFI;
52   CodeGenOpt::Level OptLevel;
53   static char ID;
54
55   explicit SelectionDAGISel(const TargetMachine &tm,
56                             CodeGenOpt::Level OL = CodeGenOpt::Default);
57   virtual ~SelectionDAGISel();
58
59   const 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   /// FIXME: This is a static member function because the MSP430/SystemZ/X86
96   /// targets, which uses it during isel.  This could become a proper member.
97   static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
98                             CodeGenOpt::Level OptLevel,
99                             bool IgnoreChains = false);
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_CaptureGlueInput,
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_SwitchType,
118     OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
119     OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
120     OPC_CheckChild6Type, OPC_CheckChild7Type,
121     OPC_CheckInteger,
122     OPC_CheckCondCode,
123     OPC_CheckValueType,
124     OPC_CheckComplexPat,
125     OPC_CheckAndImm, OPC_CheckOrImm,
126     OPC_CheckFoldableChainNode,
127
128     OPC_EmitInteger,
129     OPC_EmitRegister,
130     OPC_EmitConvertToTarget,
131     OPC_EmitMergeInputChains,
132     OPC_EmitMergeInputChains1_0,
133     OPC_EmitMergeInputChains1_1,
134     OPC_EmitCopyToReg,
135     OPC_EmitNodeXForm,
136     OPC_EmitNode,
137     OPC_MorphNodeTo,
138     OPC_MarkGlueResults,
139     OPC_CompleteMatch
140   };
141
142   enum {
143     OPFL_None       = 0,  // Node has no chain or glue input and isn't variadic.
144     OPFL_Chain      = 1,     // Node has a chain input.
145     OPFL_GlueInput  = 2,     // Node has a glue input.
146     OPFL_GlueOutput = 4,     // Node has a glue output.
147     OPFL_MemRefs    = 8,     // Node gets accumulated MemRefs.
148     OPFL_Variadic0  = 1<<4,  // Node is variadic, root has 0 fixed inputs.
149     OPFL_Variadic1  = 2<<4,  // Node is variadic, root has 1 fixed inputs.
150     OPFL_Variadic2  = 3<<4,  // Node is variadic, root has 2 fixed inputs.
151     OPFL_Variadic3  = 4<<4,  // Node is variadic, root has 3 fixed inputs.
152     OPFL_Variadic4  = 5<<4,  // Node is variadic, root has 4 fixed inputs.
153     OPFL_Variadic5  = 6<<4,  // Node is variadic, root has 5 fixed inputs.
154     OPFL_Variadic6  = 7<<4,  // Node is variadic, root has 6 fixed inputs.
155
156     OPFL_VariadicInfo = OPFL_Variadic6
157   };
158
159   /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
160   /// number of fixed arity values that should be skipped when copying from the
161   /// root.
162   static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
163     return ((Flags&OPFL_VariadicInfo) >> 4)-1;
164   }
165
166
167 protected:
168   /// DAGSize - Size of DAG being instruction selected.
169   ///
170   unsigned DAGSize;
171
172   /// ISelPosition - Node iterator marking the current position of
173   /// instruction selection as it procedes through the topologically-sorted
174   /// node list.
175   SelectionDAG::allnodes_iterator ISelPosition;
176
177
178   /// ISelUpdater - helper class to handle updates of the
179   /// instruction selection graph.
180   class ISelUpdater : public SelectionDAG::DAGUpdateListener {
181     SelectionDAG::allnodes_iterator &ISelPosition;
182   public:
183     explicit ISelUpdater(SelectionDAG::allnodes_iterator &isp)
184       : ISelPosition(isp) {}
185
186     /// NodeDeleted - Handle nodes deleted from the graph. If the
187     /// node being deleted is the current ISelPosition node, update
188     /// ISelPosition.
189     ///
190     virtual void NodeDeleted(SDNode *N, SDNode *E) {
191       if (ISelPosition == SelectionDAG::allnodes_iterator(N))
192         ++ISelPosition;
193     }
194
195     /// NodeUpdated - Ignore updates for now.
196     virtual void NodeUpdated(SDNode *N) {}
197   };
198
199   /// ReplaceUses - replace all uses of the old node F with the use
200   /// of the new node T.
201   void ReplaceUses(SDValue F, SDValue T) {
202     ISelUpdater ISU(ISelPosition);
203     CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISU);
204   }
205
206   /// ReplaceUses - replace all uses of the old nodes F with the use
207   /// of the new nodes T.
208   void ReplaceUses(const SDValue *F, const SDValue *T, unsigned Num) {
209     ISelUpdater ISU(ISelPosition);
210     CurDAG->ReplaceAllUsesOfValuesWith(F, T, Num, &ISU);
211   }
212
213   /// ReplaceUses - replace all uses of the old node F with the use
214   /// of the new node T.
215   void ReplaceUses(SDNode *F, SDNode *T) {
216     ISelUpdater ISU(ISelPosition);
217     CurDAG->ReplaceAllUsesWith(F, T, &ISU);
218   }
219
220
221   /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
222   /// by tblgen.  Others should not call it.
223   void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
224
225
226 public:
227   // Calls to these predicates are generated by tblgen.
228   bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
229                     int64_t DesiredMaskS) const;
230   bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
231                     int64_t DesiredMaskS) const;
232
233
234   /// CheckPatternPredicate - This function is generated by tblgen in the
235   /// target.  It runs the specified pattern predicate and returns true if it
236   /// succeeds or false if it fails.  The number is a private implementation
237   /// detail to the code tblgen produces.
238   virtual bool CheckPatternPredicate(unsigned PredNo) const {
239     assert(0 && "Tblgen should generate the implementation of this!");
240     return 0;
241   }
242
243   /// CheckNodePredicate - This function is generated by tblgen in the target.
244   /// It runs node predicate number PredNo and returns true if it succeeds or
245   /// false if it fails.  The number is a private implementation
246   /// detail to the code tblgen produces.
247   virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
248     assert(0 && "Tblgen should generate the implementation of this!");
249     return 0;
250   }
251
252   virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,
253                                    unsigned PatternNo,
254                         SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {
255     assert(0 && "Tblgen should generate the implementation of this!");
256     return false;
257   }
258
259   virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
260     assert(0 && "Tblgen shoudl generate this!");
261     return SDValue();
262   }
263
264   SDNode *SelectCodeCommon(SDNode *NodeToMatch,
265                            const unsigned char *MatcherTable,
266                            unsigned TableSize);
267
268 private:
269
270   // Calls to these functions are generated by tblgen.
271   SDNode *Select_INLINEASM(SDNode *N);
272   SDNode *Select_UNDEF(SDNode *N);
273   void CannotYetSelect(SDNode *N);
274
275 private:
276   void DoInstructionSelection();
277   SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
278                     const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo);
279
280   void PrepareEHLandingPad();
281   void SelectAllBasicBlocks(const Function &Fn);
282   bool TryToFoldFastISelLoad(const LoadInst *LI, FastISel *FastIS);
283   void FinishBasicBlock();
284
285   void SelectBasicBlock(BasicBlock::const_iterator Begin,
286                         BasicBlock::const_iterator End,
287                         bool &HadTailCall);
288   void CodeGenAndEmitDAG();
289   void LowerArguments(const BasicBlock *BB);
290
291   void ComputeLiveOutVRegInfo();
292
293   /// Create the scheduler. If a specific scheduler was specified
294   /// via the SchedulerRegistry, use it, otherwise select the
295   /// one preferred by the target.
296   ///
297   ScheduleDAGSDNodes *CreateScheduler();
298
299   /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
300   /// state machines that start with a OPC_SwitchOpcode node.
301   std::vector<unsigned> OpcodeOffset;
302
303   void UpdateChainsAndGlue(SDNode *NodeToMatch, SDValue InputChain,
304                            const SmallVectorImpl<SDNode*> &ChainNodesMatched,
305                            SDValue InputGlue, const SmallVectorImpl<SDNode*> &F,
306                            bool isMorphNodeTo);
307
308 };
309
310 }
311
312 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */