blob: 2a0341caee78207fd62426de2a46d33576038fca [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"
Nate Begemanf15485a2006-03-27 01:32:24 +000020#include "llvm/Constant.h"
Evan Chengfe8dc2e2006-08-07 22:16:08 +000021#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanad2afc22009-07-31 18:16:33 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnercc524ca2005-01-07 07:46:03 +000023
24namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +000025 class FastISel;
Dan Gohman2048b852009-11-23 18:04:58 +000026 class SelectionDAGBuilder;
Dan Gohman475871a2008-07-27 21:46:04 +000027 class SDValue;
Chris Lattner84bc5422007-12-31 04:13:23 +000028 class MachineRegisterInfo;
Chris Lattnercc524ca2005-01-07 07:46:03 +000029 class MachineBasicBlock;
30 class MachineFunction;
31 class MachineInstr;
Dan Gohmand57dd5f2008-09-23 21:53:34 +000032 class MachineModuleInfo;
Devang Patel83489bb2009-01-13 00:35:13 +000033 class DwarfWriter;
Chris Lattnercc524ca2005-01-07 07:46:03 +000034 class TargetLowering;
Dan Gohmandd5b58a2008-10-14 23:54:11 +000035 class TargetInstrInfo;
Chris Lattnercc524ca2005-01-07 07:46:03 +000036 class FunctionLoweringInfo;
Dan Gohmanfc54c552009-01-15 22:18:12 +000037 class ScheduleHazardRecognizer;
Gordon Henriksen5eca0752008-08-17 18:44:35 +000038 class GCFunctionInfo;
Dan Gohman47ac0f02009-02-11 04:27:20 +000039 class ScheduleDAGSDNodes;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +000040
Chris Lattnercc524ca2005-01-07 07:46:03 +000041/// SelectionDAGISel - This is the common base class used for SelectionDAG-based
42/// pattern-matching instruction selectors.
Dan Gohmanad2afc22009-07-31 18:16:33 +000043class SelectionDAGISel : public MachineFunctionPass {
Chris Lattnercc524ca2005-01-07 07:46:03 +000044public:
Dan Gohman79ce2762009-01-15 19:20:50 +000045 const TargetMachine &TM;
Chris Lattnercc524ca2005-01-07 07:46:03 +000046 TargetLowering &TLI;
Dan Gohman7c3234c2008-08-27 23:52:12 +000047 FunctionLoweringInfo *FuncInfo;
Dan Gohman79ce2762009-01-15 19:20:50 +000048 MachineFunction *MF;
49 MachineRegisterInfo *RegInfo;
Chris Lattnercc524ca2005-01-07 07:46:03 +000050 SelectionDAG *CurDAG;
Dan Gohman2048b852009-11-23 18:04:58 +000051 SelectionDAGBuilder *SDB;
Chris Lattnercc524ca2005-01-07 07:46:03 +000052 MachineBasicBlock *BB;
Dan Gohman5f43f922007-08-27 16:26:13 +000053 AliasAnalysis *AA;
Gordon Henriksen5eca0752008-08-17 18:44:35 +000054 GCFunctionInfo *GFI;
Bill Wendling98a366d2009-04-29 23:29:43 +000055 CodeGenOpt::Level OptLevel;
Devang Patel19974732007-05-03 01:11:54 +000056 static char ID;
Chris Lattnercc524ca2005-01-07 07:46:03 +000057
Bill Wendling98a366d2009-04-29 23:29:43 +000058 explicit SelectionDAGISel(TargetMachine &tm,
59 CodeGenOpt::Level OL = CodeGenOpt::Default);
Dan Gohman7c3234c2008-08-27 23:52:12 +000060 virtual ~SelectionDAGISel();
Jim Laskey9373beb2006-08-01 19:14:14 +000061
62 TargetLowering &getTargetLowering() { return TLI; }
Chris Lattnercc524ca2005-01-07 07:46:03 +000063
Chris Lattnerc809b682005-08-17 06:46:50 +000064 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Chris Lattnercc524ca2005-01-07 07:46:03 +000065
Dan Gohmanad2afc22009-07-31 18:16:33 +000066 virtual bool runOnMachineFunction(MachineFunction &MF);
Chris Lattnercc524ca2005-01-07 07:46:03 +000067
Owen Andersone50ed302009-08-10 22:56:29 +000068 unsigned MakeReg(EVT VT);
Chris Lattnercc524ca2005-01-07 07:46:03 +000069
Chris Lattnercc13b762005-05-13 07:23:03 +000070 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
Dan Gohmanf350b272008-08-23 02:25:05 +000071 virtual void InstructionSelect() = 0;
Evan Chengdb8d56b2008-06-30 20:45:06 +000072
Dan Gohman815ffa22008-08-21 16:06:51 +000073 void SelectRootInit() {
Dan Gohmanf06c8352008-09-30 18:30:35 +000074 DAGSize = CurDAG->AssignTopologicalOrder();
Evan Chengfe8dc2e2006-08-07 22:16:08 +000075 }
Misha Brukmanea61c352005-04-21 20:39:54 +000076
Chris Lattner4c12e712006-02-24 02:12:52 +000077 /// SelectInlineAsmMemoryOperand - Select the specified address as a target
78 /// addressing mode, according to the specified constraint code. If this does
79 /// not match or is not implemented, return true. The resultant operands
80 /// (which will appear in the machine instruction) should be added to the
81 /// OutOps vector.
Dan Gohman475871a2008-07-27 21:46:04 +000082 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattner4c12e712006-02-24 02:12:52 +000083 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +000084 std::vector<SDValue> &OutOps) {
Chris Lattner4c12e712006-02-24 02:12:52 +000085 return true;
86 }
Evan Chengb2c121a2006-07-27 06:36:49 +000087
Evan Cheng014bf212010-02-15 19:41:07 +000088 /// IsProfitableToFold - Returns true if it's profitable to fold the specific
89 /// operand node N of U during instruction selection that starts at Root.
90 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
91
92 /// IsLegalToFold - Returns true if the specific operand node N of
93 /// U can be folded during instruction selection that starts at Root.
94 virtual bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root) const;
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +000095
Jim Laskey9ff542f2006-08-01 18:29:48 +000096 /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
97 /// to use for this target when scheduling the DAG.
Dan Gohmanfc54c552009-01-15 22:18:12 +000098 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
Jim Laskey9ff542f2006-08-01 18:29:48 +000099
Chris Lattneraa6d7082010-02-28 21:58:42 +0000100
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_CaptureFlagInput,
109 OPC_MoveChild,
110 OPC_MoveParent,
111 OPC_CheckSame,
112 OPC_CheckPatternPredicate,
113 OPC_CheckPredicate,
114 OPC_CheckOpcode,
115 OPC_CheckMultiOpcode,
116 OPC_CheckType,
117 OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
118 OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
119 OPC_CheckChild6Type, OPC_CheckChild7Type,
Chris Lattner53106542010-02-28 22:14:32 +0000120 OPC_CheckInteger,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000121 OPC_CheckCondCode,
122 OPC_CheckValueType,
123 OPC_CheckComplexPat,
Chris Lattner53106542010-02-28 22:14:32 +0000124 OPC_CheckAndImm, OPC_CheckOrImm,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000125 OPC_CheckFoldableChainNode,
126 OPC_CheckChainCompatible,
127
Chris Lattner53106542010-02-28 22:14:32 +0000128 OPC_EmitInteger,
Chris Lattneraa6d7082010-02-28 21:58:42 +0000129 OPC_EmitRegister,
130 OPC_EmitConvertToTarget,
131 OPC_EmitMergeInputChains,
132 OPC_EmitCopyToReg,
133 OPC_EmitNodeXForm,
134 OPC_EmitNode,
135 OPC_MorphNodeTo,
136 OPC_MarkFlagResults,
137 OPC_CompleteMatch
138 };
139
140 enum {
141 OPFL_None = 0, // Node has no chain or flag input and isn't variadic.
142 OPFL_Chain = 1, // Node has a chain input.
143 OPFL_FlagInput = 2, // Node has a flag input.
144 OPFL_FlagOutput = 4, // Node has a flag output.
145 OPFL_MemRefs = 8, // Node gets accumulated MemRefs.
146 OPFL_Variadic0 = 1<<4, // Node is variadic, root has 0 fixed inputs.
147 OPFL_Variadic1 = 2<<4, // Node is variadic, root has 1 fixed inputs.
148 OPFL_Variadic2 = 3<<4, // Node is variadic, root has 2 fixed inputs.
149 OPFL_Variadic3 = 4<<4, // Node is variadic, root has 3 fixed inputs.
150 OPFL_Variadic4 = 5<<4, // Node is variadic, root has 4 fixed inputs.
151 OPFL_Variadic5 = 6<<4, // Node is variadic, root has 5 fixed inputs.
152 OPFL_Variadic6 = 7<<4, // Node is variadic, root has 6 fixed inputs.
153
154 OPFL_VariadicInfo = OPFL_Variadic6
155 };
156
Chris Lattner2a49d572010-02-28 22:37:22 +0000157 /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
158 /// number of fixed arity values that should be skipped when copying from the
159 /// root.
160 static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
161 return ((Flags&OPFL_VariadicInfo) >> 4)-1;
162 }
163
164
Chris Lattner66bac3c2005-08-18 18:44:33 +0000165protected:
Evan Chenge2c0a4f2008-07-01 18:49:06 +0000166 /// DAGSize - Size of DAG being instruction selected.
167 ///
168 unsigned DAGSize;
169
Chris Lattner4c12e712006-02-24 02:12:52 +0000170 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
171 /// by tblgen. Others should not call it.
Dan Gohmanf350b272008-08-23 02:25:05 +0000172 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
Evan Chengb2c121a2006-07-27 06:36:49 +0000173
Chris Lattner75548062006-10-11 03:58:02 +0000174 // Calls to these predicates are generated by tblgen.
Dan Gohman475871a2008-07-27 21:46:04 +0000175 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000176 int64_t DesiredMaskS) const;
Dan Gohman475871a2008-07-27 21:46:04 +0000177 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000178 int64_t DesiredMaskS) const;
Chris Lattner75548062006-10-11 03:58:02 +0000179
Chris Lattner050a03d2010-02-16 07:21:10 +0000180
181 /// CheckPatternPredicate - This function is generated by tblgen in the
182 /// target. It runs the specified pattern predicate and returns true if it
183 /// succeeds or false if it fails. The number is a private implementation
184 /// detail to the code tblgen produces.
185 virtual bool CheckPatternPredicate(unsigned PredNo) const {
186 assert(0 && "Tblgen should generate the implementation of this!");
187 return 0;
188 }
189
Dan Gohmanfb76fe02010-02-22 04:10:52 +0000190 /// CheckNodePredicate - This function is generated by tblgen in the target.
191 /// It runs node predicate number PredNo and returns true if it succeeds or
Chris Lattner050a03d2010-02-16 07:21:10 +0000192 /// false if it fails. The number is a private implementation
193 /// detail to the code tblgen produces.
194 virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
195 assert(0 && "Tblgen should generate the implementation of this!");
196 return 0;
197 }
198
Chris Lattnerbd12fe82010-02-17 00:41:34 +0000199 virtual bool CheckComplexPattern(SDNode *Root, SDValue N, unsigned PatternNo,
200 SmallVectorImpl<SDValue> &Result) {
201 assert(0 && "Tblgen should generate the implementation of this!");
202 return false;
203 }
204
Chris Lattnerbeff6a32010-02-21 03:15:11 +0000205 virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
206 assert(0 && "Tblgen shoudl generate this!");
207 return SDValue();
208 }
209
210
Dan Gohmane1f188f2009-10-29 22:30:23 +0000211 // Calls to these functions are generated by tblgen.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000212 SDNode *Select_INLINEASM(SDNode *N);
213 SDNode *Select_UNDEF(SDNode *N);
214 SDNode *Select_EH_LABEL(SDNode *N);
Chris Lattner2a49d572010-02-28 22:37:22 +0000215
216 SDNode *SelectCodeCommon(SDNode *NodeToMatch,
217 const unsigned char *MatcherTable,
218 unsigned TableSize);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000219 void CannotYetSelect(SDNode *N);
220 void CannotYetSelectIntrinsic(SDNode *N);
Dan Gohmane1f188f2009-10-29 22:30:23 +0000221
Chris Lattner80d8a932005-01-17 17:14:43 +0000222private:
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000223 void SelectAllBasicBlocks(Function &Fn, MachineFunction &MF,
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000224 MachineModuleInfo *MMI,
Devang Patel83489bb2009-01-13 00:35:13 +0000225 DwarfWriter *DW,
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000226 const TargetInstrInfo &TII);
Dan Gohman7c3234c2008-08-27 23:52:12 +0000227 void FinishBasicBlock();
Misha Brukmanea61c352005-04-21 20:39:54 +0000228
Dan Gohmanf350b272008-08-23 02:25:05 +0000229 void SelectBasicBlock(BasicBlock *LLVMBB,
230 BasicBlock::iterator Begin,
Dan Gohmanb4afb132009-11-20 02:51:26 +0000231 BasicBlock::iterator End,
232 bool &HadTailCall);
Dan Gohmanf350b272008-08-23 02:25:05 +0000233 void CodeGenAndEmitDAG();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000234 void LowerArguments(BasicBlock *BB);
Chris Lattneread0d882008-06-17 06:09:18 +0000235
Evan Chengd40d03e2010-01-06 19:38:29 +0000236 void ShrinkDemandedOps();
Dan Gohmanf350b272008-08-23 02:25:05 +0000237 void ComputeLiveOutVRegInfo();
238
Dan Gohman7c3234c2008-08-27 23:52:12 +0000239 void HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +0000240
Dan Gohman3df24e62008-09-03 23:12:08 +0000241 bool HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, FastISel *F);
242
Dan Gohman0a3776d2009-02-06 18:26:51 +0000243 /// Create the scheduler. If a specific scheduler was specified
244 /// via the SchedulerRegistry, use it, otherwise select the
245 /// one preferred by the target.
246 ///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000247 ScheduleDAGSDNodes *CreateScheduler();
Chris Lattnercc524ca2005-01-07 07:46:03 +0000248};
249
250}
251
252#endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */