blob: 7be99d7a56d3d6cc8d68848342f8a7f03da8386d [file] [log] [blame]
Chris Lattnercc524ca2005-01-07 07:46:03 +00001//===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===//
Misha Brukmanea61c352005-04-21 20:39:54 +00002//
Chris Lattnercc524ca2005-01-07 07:46:03 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanea61c352005-04-21 20:39:54 +00007//
Chris Lattnercc524ca2005-01-07 07:46:03 +00008//===----------------------------------------------------------------------===//
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 Gohmanf350b272008-08-23 02:25:05 +000018#include "llvm/BasicBlock.h"
Chris Lattnercc524ca2005-01-07 07:46:03 +000019#include "llvm/Pass.h"
Evan Chengfe8dc2e2006-08-07 22:16:08 +000020#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanad2afc22009-07-31 18:16:33 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnercc524ca2005-01-07 07:46:03 +000022
23namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +000024 class FastISel;
Dan Gohman2048b852009-11-23 18:04:58 +000025 class SelectionDAGBuilder;
Dan Gohman475871a2008-07-27 21:46:04 +000026 class SDValue;
Chris Lattner84bc5422007-12-31 04:13:23 +000027 class MachineRegisterInfo;
Chris Lattnercc524ca2005-01-07 07:46:03 +000028 class MachineBasicBlock;
29 class MachineFunction;
30 class MachineInstr;
31 class TargetLowering;
Dan Gohmandd5b58a2008-10-14 23:54:11 +000032 class TargetInstrInfo;
Chris Lattnercc524ca2005-01-07 07:46:03 +000033 class FunctionLoweringInfo;
Dan Gohmanfc54c552009-01-15 22:18:12 +000034 class ScheduleHazardRecognizer;
Gordon Henriksen5eca0752008-08-17 18:44:35 +000035 class GCFunctionInfo;
Dan Gohman47ac0f02009-02-11 04:27:20 +000036 class ScheduleDAGSDNodes;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +000037
Chris Lattnercc524ca2005-01-07 07:46:03 +000038/// SelectionDAGISel - This is the common base class used for SelectionDAG-based
39/// pattern-matching instruction selectors.
Dan Gohmanad2afc22009-07-31 18:16:33 +000040class SelectionDAGISel : public MachineFunctionPass {
Chris Lattnercc524ca2005-01-07 07:46:03 +000041public:
Dan Gohman79ce2762009-01-15 19:20:50 +000042 const TargetMachine &TM;
Chris Lattnercc524ca2005-01-07 07:46:03 +000043 TargetLowering &TLI;
Dan Gohman7c3234c2008-08-27 23:52:12 +000044 FunctionLoweringInfo *FuncInfo;
Dan Gohman79ce2762009-01-15 19:20:50 +000045 MachineFunction *MF;
46 MachineRegisterInfo *RegInfo;
Chris Lattnercc524ca2005-01-07 07:46:03 +000047 SelectionDAG *CurDAG;
Dan Gohman2048b852009-11-23 18:04:58 +000048 SelectionDAGBuilder *SDB;
Chris Lattnercc524ca2005-01-07 07:46:03 +000049 MachineBasicBlock *BB;
Dan Gohman5f43f922007-08-27 16:26:13 +000050 AliasAnalysis *AA;
Gordon Henriksen5eca0752008-08-17 18:44:35 +000051 GCFunctionInfo *GFI;
Bill Wendling98a366d2009-04-29 23:29:43 +000052 CodeGenOpt::Level OptLevel;
Devang Patel19974732007-05-03 01:11:54 +000053 static char ID;
Chris Lattnercc524ca2005-01-07 07:46:03 +000054
Bill Wendling98a366d2009-04-29 23:29:43 +000055 explicit SelectionDAGISel(TargetMachine &tm,
56 CodeGenOpt::Level OL = CodeGenOpt::Default);
Dan Gohman7c3234c2008-08-27 23:52:12 +000057 virtual ~SelectionDAGISel();
Jim Laskey9373beb2006-08-01 19:14:14 +000058
59 TargetLowering &getTargetLowering() { return TLI; }
Chris Lattnercc524ca2005-01-07 07:46:03 +000060
Chris Lattnerc809b682005-08-17 06:46:50 +000061 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Chris Lattnercc524ca2005-01-07 07:46:03 +000062
Dan Gohmanad2afc22009-07-31 18:16:33 +000063 virtual bool runOnMachineFunction(MachineFunction &MF);
Chris Lattnercc524ca2005-01-07 07:46:03 +000064
Dan Gohman64652652010-04-14 20:17:22 +000065 virtual void EmitFunctionEntryCode() {}
Evan Chengdb8d56b2008-06-30 20:45:06 +000066
Chris Lattner7c306da2010-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
Chris Lattner4c12e712006-02-24 02:12:52 +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 Gohman475871a2008-07-27 21:46:04 +000083 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattner4c12e712006-02-24 02:12:52 +000084 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +000085 std::vector<SDValue> &OutOps) {
Chris Lattner4c12e712006-02-24 02:12:52 +000086 return true;
87 }
Evan Chengb2c121a2006-07-27 06:36:49 +000088
Evan Cheng014bf212010-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.
Chris Lattner6b7f39c2010-03-02 22:30:08 +000095 bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
96 bool IgnoreChains = false) const;
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +000097
Jim Laskey9ff542f2006-08-01 18:29:48 +000098 /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
99 /// to use for this target when scheduling the DAG.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000100 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
Jim Laskey9ff542f2006-08-01 18:29:48 +0000101
Chris Lattneraa6d7082010-02-28 21:58:42 +0000102
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,
Chris Lattnereb669212010-03-01 06:59:22 +0000117 OPC_SwitchOpcode,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000118 OPC_CheckType,
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000119 OPC_SwitchType,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000120 OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
121 OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
122 OPC_CheckChild6Type, OPC_CheckChild7Type,
Chris Lattner53106542010-02-28 22:14:32 +0000123 OPC_CheckInteger,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000124 OPC_CheckCondCode,
125 OPC_CheckValueType,
126 OPC_CheckComplexPat,
Chris Lattner53106542010-02-28 22:14:32 +0000127 OPC_CheckAndImm, OPC_CheckOrImm,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000128 OPC_CheckFoldableChainNode,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000129
Chris Lattner53106542010-02-28 22:14:32 +0000130 OPC_EmitInteger,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000131 OPC_EmitRegister,
132 OPC_EmitConvertToTarget,
133 OPC_EmitMergeInputChains,
Chris Lattneraa4e3392010-03-28 05:50:16 +0000134 OPC_EmitMergeInputChains1_0,
135 OPC_EmitMergeInputChains1_1,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000136 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
Chris Lattner2a49d572010-02-28 22:37:22 +0000161 /// 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
Chris Lattner66bac3c2005-08-18 18:44:33 +0000169protected:
Evan Chenge2c0a4f2008-07-01 18:49:06 +0000170 /// DAGSize - Size of DAG being instruction selected.
171 ///
172 unsigned DAGSize;
Chris Lattner17b4b172010-03-02 06:04:12 +0000173
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
Evan Chenge2c0a4f2008-07-01 18:49:06 +0000222
Chris Lattner4c12e712006-02-24 02:12:52 +0000223 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
224 /// by tblgen. Others should not call it.
Dan Gohmanf350b272008-08-23 02:25:05 +0000225 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
Evan Chengb2c121a2006-07-27 06:36:49 +0000226
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +0000227
228public:
Chris Lattner75548062006-10-11 03:58:02 +0000229 // Calls to these predicates are generated by tblgen.
Dan Gohman475871a2008-07-27 21:46:04 +0000230 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000231 int64_t DesiredMaskS) const;
Dan Gohman475871a2008-07-27 21:46:04 +0000232 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000233 int64_t DesiredMaskS) const;
Chris Lattner75548062006-10-11 03:58:02 +0000234
Chris Lattner050a03d2010-02-16 07:21:10 +0000235
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
Dan Gohmanfb76fe02010-02-22 04:10:52 +0000245 /// CheckNodePredicate - This function is generated by tblgen in the target.
246 /// It runs node predicate number PredNo and returns true if it succeeds or
Chris Lattner050a03d2010-02-16 07:21:10 +0000247 /// 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
Chris Lattnerbd12fe82010-02-17 00:41:34 +0000254 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
Chris Lattnerbeff6a32010-02-21 03:15:11 +0000260 virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
261 assert(0 && "Tblgen shoudl generate this!");
262 return SDValue();
263 }
264
Chris Lattnerf1b7c7d2010-03-03 07:31:15 +0000265 SDNode *SelectCodeCommon(SDNode *NodeToMatch,
266 const unsigned char *MatcherTable,
267 unsigned TableSize);
268
269private:
Chris Lattnerbeff6a32010-02-21 03:15:11 +0000270
Dan Gohmane1f188f2009-10-29 22:30:23 +0000271 // Calls to these functions are generated by tblgen.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000272 SDNode *Select_INLINEASM(SDNode *N);
273 SDNode *Select_UNDEF(SDNode *N);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000274 void CannotYetSelect(SDNode *N);
Dan Gohmane1f188f2009-10-29 22:30:23 +0000275
Chris Lattner80d8a932005-01-17 17:14:43 +0000276private:
Chris Lattner7c306da2010-03-02 06:34:30 +0000277 void DoInstructionSelection();
Chris Lattner3ff1e4d2010-03-02 06:55:04 +0000278 SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
279 const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo);
Chris Lattner7c306da2010-03-02 06:34:30 +0000280
Dan Gohman25208642010-04-14 19:53:31 +0000281 void PrepareEHLandingPad(MachineBasicBlock *BB);
Dan Gohman46510a72010-04-15 01:51:59 +0000282 void SelectAllBasicBlocks(const Function &Fn);
Dan Gohman7c3234c2008-08-27 23:52:12 +0000283 void FinishBasicBlock();
Misha Brukmanea61c352005-04-21 20:39:54 +0000284
Dan Gohman46510a72010-04-15 01:51:59 +0000285 void SelectBasicBlock(const BasicBlock *LLVMBB,
286 BasicBlock::const_iterator Begin,
287 BasicBlock::const_iterator End,
Dan Gohmanb4afb132009-11-20 02:51:26 +0000288 bool &HadTailCall);
Dan Gohmanf350b272008-08-23 02:25:05 +0000289 void CodeGenAndEmitDAG();
Dan Gohman46510a72010-04-15 01:51:59 +0000290 void LowerArguments(const BasicBlock *BB);
Chris Lattneread0d882008-06-17 06:09:18 +0000291
Evan Chengd40d03e2010-01-06 19:38:29 +0000292 void ShrinkDemandedOps();
Dan Gohmanf350b272008-08-23 02:25:05 +0000293 void ComputeLiveOutVRegInfo();
294
Dan Gohman46510a72010-04-15 01:51:59 +0000295 void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +0000296
Dan Gohman46510a72010-04-15 01:51:59 +0000297 bool HandlePHINodesInSuccessorBlocksFast(const BasicBlock *LLVMBB,
298 FastISel *F);
Dan Gohman3df24e62008-09-03 23:12:08 +0000299
Dan Gohman0a3776d2009-02-06 18:26:51 +0000300 /// Create the scheduler. If a specific scheduler was specified
301 /// via the SchedulerRegistry, use it, otherwise select the
302 /// one preferred by the target.
303 ///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000304 ScheduleDAGSDNodes *CreateScheduler();
Chris Lattner7390eeb2010-03-01 18:47:11 +0000305
306 /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
307 /// state machines that start with a OPC_SwitchOpcode node.
308 std::vector<unsigned> OpcodeOffset;
Chris Lattner82dd3d32010-03-02 07:50:03 +0000309
310 void UpdateChainsAndFlags(SDNode *NodeToMatch, SDValue InputChain,
311 const SmallVectorImpl<SDNode*> &ChainNodesMatched,
312 SDValue InputFlag,const SmallVectorImpl<SDNode*> &F,
313 bool isMorphNodeTo);
314
Chris Lattnercc524ca2005-01-07 07:46:03 +0000315};
316
317}
318
319#endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */