blob: 9ce8fc4da286a43d205395211ce2b12044279bdf [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner84e66db2007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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
Dan Gohman14a66442008-08-23 02:25:05 +000018#include "llvm/BasicBlock.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Pass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022
23namespace llvm {
Dan Gohmanca4857a2008-09-03 23:12:08 +000024 class FastISel;
Dan Gohman7ec20512009-11-23 18:04:58 +000025 class SelectionDAGBuilder;
Dan Gohman8181bd12008-07-27 21:46:04 +000026 class SDValue;
Chris Lattner1b989192007-12-31 04:13:23 +000027 class MachineRegisterInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028 class MachineBasicBlock;
29 class MachineFunction;
30 class MachineInstr;
31 class TargetLowering;
Dan Gohman9dd43582008-10-14 23:54:11 +000032 class TargetInstrInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 class FunctionLoweringInfo;
Dan Gohmandd6547d2009-01-15 22:18:12 +000034 class ScheduleHazardRecognizer;
Gordon Henriksen1aed5992008-08-17 18:44:35 +000035 class GCFunctionInfo;
Dan Gohman7c968a82009-02-11 04:27:20 +000036 class ScheduleDAGSDNodes;
Chris Lattner083b9e12010-09-05 02:18:34 +000037 class LoadInst;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
39/// SelectionDAGISel - This is the common base class used for SelectionDAG-based
40/// pattern-matching instruction selectors.
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000041class SelectionDAGISel : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042public:
Dan Gohman96eb47a2009-01-15 19:20:50 +000043 const TargetMachine &TM;
Dan Gohmandbb121b2010-04-17 15:26:15 +000044 const TargetLowering &TLI;
Dan Gohman0f2d71d2008-08-27 23:52:12 +000045 FunctionLoweringInfo *FuncInfo;
Dan Gohman96eb47a2009-01-15 19:20:50 +000046 MachineFunction *MF;
47 MachineRegisterInfo *RegInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 SelectionDAG *CurDAG;
Dan Gohman7ec20512009-11-23 18:04:58 +000049 SelectionDAGBuilder *SDB;
Dan Gohmancc863aa2007-08-27 16:26:13 +000050 AliasAnalysis *AA;
Gordon Henriksen1aed5992008-08-17 18:44:35 +000051 GCFunctionInfo *GFI;
Bill Wendling5ed22ac2009-04-29 23:29:43 +000052 CodeGenOpt::Level OptLevel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 static char ID;
54
Dan Gohmanb9305c02010-04-21 01:34:56 +000055 explicit SelectionDAGISel(const TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000056 CodeGenOpt::Level OL = CodeGenOpt::Default);
Dan Gohman0f2d71d2008-08-27 23:52:12 +000057 virtual ~SelectionDAGISel();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
Dan Gohmandbb121b2010-04-17 15:26:15 +000059 const TargetLowering &getTargetLowering() { return TLI; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
62
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000063 virtual bool runOnMachineFunction(MachineFunction &MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064
Dan Gohmanc5a13c62010-04-14 20:17:22 +000065 virtual void EmitFunctionEntryCode() {}
Evan Cheng34fd4f32008-06-30 20:45:06 +000066
Chris Lattner6411e3e2010-03-02 06:34:30 +000067 /// 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 /// 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.
Dan Gohman8181bd12008-07-27 21:46:04 +000083 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +000085 std::vector<SDValue> &OutOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 return true;
87 }
88
Evan Chengf80681e2010-02-15 19:41:07 +000089 /// 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.
Dan Gohmandbb121b2010-04-17 15:26:15 +000095 /// FIXME: This is a static member function because the PIC16 target,
96 /// which uses it during lowering.
97 static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
98 CodeGenOpt::Level OptLevel,
99 bool IgnoreChains = false);
Anton Korobeynikovda76d322009-05-08 18:51:58 +0000100
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
102 /// to use for this target when scheduling the DAG.
Dan Gohmandd6547d2009-01-15 22:18:12 +0000103 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
Chris Lattner6efe0242010-02-28 21:58:42 +0000105
106 // Opcodes used by the DAG state machine:
107 enum BuiltinOpcodes {
108 OPC_Scope,
109 OPC_RecordNode,
110 OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3,
111 OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
112 OPC_RecordMemRef,
113 OPC_CaptureFlagInput,
114 OPC_MoveChild,
115 OPC_MoveParent,
116 OPC_CheckSame,
117 OPC_CheckPatternPredicate,
118 OPC_CheckPredicate,
119 OPC_CheckOpcode,
Chris Lattner2e9e6842010-03-01 06:59:22 +0000120 OPC_SwitchOpcode,
Chris Lattner6efe0242010-02-28 21:58:42 +0000121 OPC_CheckType,
Chris Lattnerc4188ac2010-03-03 06:28:15 +0000122 OPC_SwitchType,
Chris Lattner6efe0242010-02-28 21:58:42 +0000123 OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
124 OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
125 OPC_CheckChild6Type, OPC_CheckChild7Type,
Chris Lattner7e3cfb32010-02-28 22:14:32 +0000126 OPC_CheckInteger,
Chris Lattner6efe0242010-02-28 21:58:42 +0000127 OPC_CheckCondCode,
128 OPC_CheckValueType,
129 OPC_CheckComplexPat,
Chris Lattner7e3cfb32010-02-28 22:14:32 +0000130 OPC_CheckAndImm, OPC_CheckOrImm,
Chris Lattner6efe0242010-02-28 21:58:42 +0000131 OPC_CheckFoldableChainNode,
Chris Lattner6efe0242010-02-28 21:58:42 +0000132
Chris Lattner7e3cfb32010-02-28 22:14:32 +0000133 OPC_EmitInteger,
Chris Lattner6efe0242010-02-28 21:58:42 +0000134 OPC_EmitRegister,
135 OPC_EmitConvertToTarget,
136 OPC_EmitMergeInputChains,
Chris Lattner05a383a2010-03-28 05:50:16 +0000137 OPC_EmitMergeInputChains1_0,
138 OPC_EmitMergeInputChains1_1,
Chris Lattner6efe0242010-02-28 21:58:42 +0000139 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
Chris Lattner65ff4332010-02-28 22:37:22 +0000164 /// 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172protected:
Evan Cheng220dfe82008-07-01 18:49:06 +0000173 /// DAGSize - Size of DAG being instruction selected.
174 ///
175 unsigned DAGSize;
Chris Lattner4bef1f42010-03-02 06:04:12 +0000176
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
Evan Cheng220dfe82008-07-01 18:49:06 +0000225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
227 /// by tblgen. Others should not call it.
Dan Gohman14a66442008-08-23 02:25:05 +0000228 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229
Chris Lattner7f695332010-03-03 07:31:15 +0000230
231public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 // Calls to these predicates are generated by tblgen.
Dan Gohman8181bd12008-07-27 21:46:04 +0000233 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmand6098272007-07-24 23:00:27 +0000234 int64_t DesiredMaskS) const;
Dan Gohman8181bd12008-07-27 21:46:04 +0000235 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmand6098272007-07-24 23:00:27 +0000236 int64_t DesiredMaskS) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +0000238
239 /// CheckPatternPredicate - This function is generated by tblgen in the
240 /// target. It runs the specified pattern predicate and returns true if it
241 /// succeeds or false if it fails. The number is a private implementation
242 /// detail to the code tblgen produces.
243 virtual bool CheckPatternPredicate(unsigned PredNo) const {
244 assert(0 && "Tblgen should generate the implementation of this!");
245 return 0;
246 }
247
Dan Gohman661aabb2010-02-22 04:10:52 +0000248 /// CheckNodePredicate - This function is generated by tblgen in the target.
249 /// It runs node predicate number PredNo and returns true if it succeeds or
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +0000250 /// false if it fails. The number is a private implementation
251 /// detail to the code tblgen produces.
252 virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
253 assert(0 && "Tblgen should generate the implementation of this!");
254 return 0;
255 }
256
Chris Lattner6dedbda2010-09-21 20:37:12 +0000257 virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,
258 unsigned PatternNo,
Chris Lattner4a37ffb22010-02-17 00:41:34 +0000259 SmallVectorImpl<SDValue> &Result) {
260 assert(0 && "Tblgen should generate the implementation of this!");
261 return false;
262 }
263
Chris Lattner80f35612010-02-21 03:15:11 +0000264 virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
265 assert(0 && "Tblgen shoudl generate this!");
266 return SDValue();
267 }
268
Chris Lattner7f695332010-03-03 07:31:15 +0000269 SDNode *SelectCodeCommon(SDNode *NodeToMatch,
270 const unsigned char *MatcherTable,
271 unsigned TableSize);
272
273private:
Chris Lattner80f35612010-02-21 03:15:11 +0000274
Dan Gohmanb65e3ac2009-10-29 22:30:23 +0000275 // Calls to these functions are generated by tblgen.
Dan Gohman5f082a72010-01-05 01:24:18 +0000276 SDNode *Select_INLINEASM(SDNode *N);
277 SDNode *Select_UNDEF(SDNode *N);
Dan Gohman5f082a72010-01-05 01:24:18 +0000278 void CannotYetSelect(SDNode *N);
Dan Gohmanb65e3ac2009-10-29 22:30:23 +0000279
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280private:
Chris Lattner6411e3e2010-03-02 06:34:30 +0000281 void DoInstructionSelection();
Chris Lattnerad031062010-03-02 06:55:04 +0000282 SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
283 const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo);
Chris Lattner6411e3e2010-03-02 06:34:30 +0000284
Dan Gohmanb4482cf2010-07-10 09:00:22 +0000285 void PrepareEHLandingPad();
Dan Gohman36c56d02010-04-15 01:51:59 +0000286 void SelectAllBasicBlocks(const Function &Fn);
Chris Lattner083b9e12010-09-05 02:18:34 +0000287 bool TryToFoldFastISelLoad(const LoadInst *LI, FastISel *FastIS);
Dan Gohmanb4482cf2010-07-10 09:00:22 +0000288 void FinishBasicBlock();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289
Dan Gohmanb4482cf2010-07-10 09:00:22 +0000290 void SelectBasicBlock(BasicBlock::const_iterator Begin,
291 BasicBlock::const_iterator End,
292 bool &HadTailCall);
293 void CodeGenAndEmitDAG();
Dan Gohman36c56d02010-04-15 01:51:59 +0000294 void LowerArguments(const BasicBlock *BB);
Chris Lattner68068cc2008-06-17 06:09:18 +0000295
Dan Gohman14a66442008-08-23 02:25:05 +0000296 void ComputeLiveOutVRegInfo();
297
Dan Gohman29800e22009-02-06 18:26:51 +0000298 /// 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 ///
Dan Gohman7c968a82009-02-11 04:27:20 +0000302 ScheduleDAGSDNodes *CreateScheduler();
Chris Lattner24187412010-03-01 18:47:11 +0000303
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;
Chris Lattner385f39f2010-03-02 07:50:03 +0000307
308 void UpdateChainsAndFlags(SDNode *NodeToMatch, SDValue InputChain,
309 const SmallVectorImpl<SDNode*> &ChainNodesMatched,
310 SDValue InputFlag,const SmallVectorImpl<SDNode*> &F,
311 bool isMorphNodeTo);
312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313};
314
315}
316
317#endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */