1 //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SelectionDAGISel class, which is used as the common
11 // base class for SelectionDAG-based instruction selectors.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_ISEL_H
16 #define LLVM_CODEGEN_SELECTIONDAG_ISEL_H
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Constant.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
26 class SelectionDAGBuilder;
28 class MachineRegisterInfo;
29 class MachineBasicBlock;
30 class MachineFunction;
32 class MachineModuleInfo;
35 class TargetInstrInfo;
36 class FunctionLoweringInfo;
37 class ScheduleHazardRecognizer;
39 class ScheduleDAGSDNodes;
41 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
42 /// pattern-matching instruction selectors.
43 class SelectionDAGISel : public MachineFunctionPass {
45 const TargetMachine &TM;
47 FunctionLoweringInfo *FuncInfo;
49 MachineRegisterInfo *RegInfo;
51 SelectionDAGBuilder *SDB;
52 MachineBasicBlock *BB;
55 CodeGenOpt::Level OptLevel;
58 explicit SelectionDAGISel(TargetMachine &tm,
59 CodeGenOpt::Level OL = CodeGenOpt::Default);
60 virtual ~SelectionDAGISel();
62 TargetLowering &getTargetLowering() { return TLI; }
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
66 virtual bool runOnMachineFunction(MachineFunction &MF);
68 unsigned MakeReg(EVT VT);
70 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
72 /// PreprocessISelDAG - This hook allows targets to hack on the graph before
73 /// instruction selection starts.
74 virtual void PreprocessISelDAG() {}
76 /// PostprocessISelDAG() - This hook allows the target to hack on the graph
77 /// right after selection.
78 virtual void PostprocessISelDAG() {}
80 /// Select - Main hook targets implement to select a node.
81 virtual SDNode *Select(SDNode *N) = 0;
83 /// SelectInlineAsmMemoryOperand - Select the specified address as a target
84 /// addressing mode, according to the specified constraint code. If this does
85 /// not match or is not implemented, return true. The resultant operands
86 /// (which will appear in the machine instruction) should be added to the
88 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
90 std::vector<SDValue> &OutOps) {
94 /// IsProfitableToFold - Returns true if it's profitable to fold the specific
95 /// operand node N of U during instruction selection that starts at Root.
96 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
98 /// IsLegalToFold - Returns true if the specific operand node N of
99 /// U can be folded during instruction selection that starts at Root.
100 bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root,
101 bool IgnoreChains = false) const;
103 /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
104 /// to use for this target when scheduling the DAG.
105 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
108 // Opcodes used by the DAG state machine:
109 enum BuiltinOpcodes {
112 OPC_RecordChild0, OPC_RecordChild1, OPC_RecordChild2, OPC_RecordChild3,
113 OPC_RecordChild4, OPC_RecordChild5, OPC_RecordChild6, OPC_RecordChild7,
115 OPC_CaptureFlagInput,
119 OPC_CheckPatternPredicate,
124 OPC_CheckChild0Type, OPC_CheckChild1Type, OPC_CheckChild2Type,
125 OPC_CheckChild3Type, OPC_CheckChild4Type, OPC_CheckChild5Type,
126 OPC_CheckChild6Type, OPC_CheckChild7Type,
131 OPC_CheckAndImm, OPC_CheckOrImm,
132 OPC_CheckFoldableChainNode,
136 OPC_EmitConvertToTarget,
137 OPC_EmitMergeInputChains,
147 OPFL_None = 0, // Node has no chain or flag input and isn't variadic.
148 OPFL_Chain = 1, // Node has a chain input.
149 OPFL_FlagInput = 2, // Node has a flag input.
150 OPFL_FlagOutput = 4, // Node has a flag output.
151 OPFL_MemRefs = 8, // Node gets accumulated MemRefs.
152 OPFL_Variadic0 = 1<<4, // Node is variadic, root has 0 fixed inputs.
153 OPFL_Variadic1 = 2<<4, // Node is variadic, root has 1 fixed inputs.
154 OPFL_Variadic2 = 3<<4, // Node is variadic, root has 2 fixed inputs.
155 OPFL_Variadic3 = 4<<4, // Node is variadic, root has 3 fixed inputs.
156 OPFL_Variadic4 = 5<<4, // Node is variadic, root has 4 fixed inputs.
157 OPFL_Variadic5 = 6<<4, // Node is variadic, root has 5 fixed inputs.
158 OPFL_Variadic6 = 7<<4, // Node is variadic, root has 6 fixed inputs.
160 OPFL_VariadicInfo = OPFL_Variadic6
163 /// getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the
164 /// number of fixed arity values that should be skipped when copying from the
166 static inline int getNumFixedFromVariadicInfo(unsigned Flags) {
167 return ((Flags&OPFL_VariadicInfo) >> 4)-1;
172 /// DAGSize - Size of DAG being instruction selected.
176 /// ISelPosition - Node iterator marking the current position of
177 /// instruction selection as it procedes through the topologically-sorted
179 SelectionDAG::allnodes_iterator ISelPosition;
182 /// ISelUpdater - helper class to handle updates of the
183 /// instruction selection graph.
184 class ISelUpdater : public SelectionDAG::DAGUpdateListener {
185 SelectionDAG::allnodes_iterator &ISelPosition;
187 explicit ISelUpdater(SelectionDAG::allnodes_iterator &isp)
188 : ISelPosition(isp) {}
190 /// NodeDeleted - Handle nodes deleted from the graph. If the
191 /// node being deleted is the current ISelPosition node, update
194 virtual void NodeDeleted(SDNode *N, SDNode *E) {
195 if (ISelPosition == SelectionDAG::allnodes_iterator(N))
199 /// NodeUpdated - Ignore updates for now.
200 virtual void NodeUpdated(SDNode *N) {}
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);
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);
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);
225 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
226 /// by tblgen. Others should not call it.
227 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
229 // Calls to these predicates are generated by tblgen.
230 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
231 int64_t DesiredMaskS) const;
232 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
233 int64_t DesiredMaskS) const;
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!");
245 /// CheckNodePredicate - This function is generated by tblgen in the target.
246 /// It runs node predicate number PredNo and returns true if it succeeds or
247 /// 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!");
254 virtual bool CheckComplexPattern(SDNode *Root, SDValue N, unsigned PatternNo,
255 SmallVectorImpl<SDValue> &Result) {
256 assert(0 && "Tblgen should generate the implementation of this!");
260 virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {
261 assert(0 && "Tblgen shoudl generate this!");
266 // Calls to these functions are generated by tblgen.
267 SDNode *Select_INLINEASM(SDNode *N);
268 SDNode *Select_UNDEF(SDNode *N);
269 SDNode *Select_EH_LABEL(SDNode *N);
271 SDNode *SelectCodeCommon(SDNode *NodeToMatch,
272 const unsigned char *MatcherTable,
274 void CannotYetSelect(SDNode *N);
275 void CannotYetSelectIntrinsic(SDNode *N);
278 void DoInstructionSelection();
279 SDNode *MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTs,
280 const SDValue *Ops, unsigned NumOps, unsigned EmitNodeInfo);
282 void SelectAllBasicBlocks(Function &Fn, MachineFunction &MF,
283 MachineModuleInfo *MMI,
285 const TargetInstrInfo &TII);
286 void FinishBasicBlock();
288 void SelectBasicBlock(BasicBlock *LLVMBB,
289 BasicBlock::iterator Begin,
290 BasicBlock::iterator End,
292 void CodeGenAndEmitDAG();
293 void LowerArguments(BasicBlock *BB);
295 void ShrinkDemandedOps();
296 void ComputeLiveOutVRegInfo();
298 void HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB);
300 bool HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, FastISel *F);
302 /// Create the scheduler. If a specific scheduler was specified
303 /// via the SchedulerRegistry, use it, otherwise select the
304 /// one preferred by the target.
306 ScheduleDAGSDNodes *CreateScheduler();
308 /// OpcodeOffset - This is a cache used to dispatch efficiently into isel
309 /// state machines that start with a OPC_SwitchOpcode node.
310 std::vector<unsigned> OpcodeOffset;
312 void UpdateChainsAndFlags(SDNode *NodeToMatch, SDValue InputChain,
313 const SmallVectorImpl<SDNode*> &ChainNodesMatched,
314 SDValue InputFlag,const SmallVectorImpl<SDNode*> &F,
321 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */