blob: 1b9c26860500c87a33a65543b4fdc84643411eb9 [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"
20#include "llvm/Constant.h"
21#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023
24namespace llvm {
Dan Gohmanca4857a2008-09-03 23:12:08 +000025 class FastISel;
Dan Gohman7ec20512009-11-23 18:04:58 +000026 class SelectionDAGBuilder;
Dan Gohman8181bd12008-07-27 21:46:04 +000027 class SDValue;
Chris Lattner1b989192007-12-31 04:13:23 +000028 class MachineRegisterInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029 class MachineBasicBlock;
30 class MachineFunction;
31 class MachineInstr;
Dan Gohman76dd96e2008-09-23 21:53:34 +000032 class MachineModuleInfo;
Devang Patelfcf1c752009-01-13 00:35:13 +000033 class DwarfWriter;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 class TargetLowering;
Dan Gohman9dd43582008-10-14 23:54:11 +000035 class TargetInstrInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 class FunctionLoweringInfo;
Dan Gohmandd6547d2009-01-15 22:18:12 +000037 class ScheduleHazardRecognizer;
Gordon Henriksen1aed5992008-08-17 18:44:35 +000038 class GCFunctionInfo;
Dan Gohman7c968a82009-02-11 04:27:20 +000039 class ScheduleDAGSDNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41/// SelectionDAGISel - This is the common base class used for SelectionDAG-based
42/// pattern-matching instruction selectors.
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000043class SelectionDAGISel : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044public:
Dan Gohman96eb47a2009-01-15 19:20:50 +000045 const TargetMachine &TM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 TargetLowering &TLI;
Dan Gohman0f2d71d2008-08-27 23:52:12 +000047 FunctionLoweringInfo *FuncInfo;
Dan Gohman96eb47a2009-01-15 19:20:50 +000048 MachineFunction *MF;
49 MachineRegisterInfo *RegInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 SelectionDAG *CurDAG;
Dan Gohman7ec20512009-11-23 18:04:58 +000051 SelectionDAGBuilder *SDB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 MachineBasicBlock *BB;
Dan Gohmancc863aa2007-08-27 16:26:13 +000053 AliasAnalysis *AA;
Gordon Henriksen1aed5992008-08-17 18:44:35 +000054 GCFunctionInfo *GFI;
Bill Wendling5ed22ac2009-04-29 23:29:43 +000055 CodeGenOpt::Level OptLevel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 static char ID;
57
Bill Wendling5ed22ac2009-04-29 23:29:43 +000058 explicit SelectionDAGISel(TargetMachine &tm,
59 CodeGenOpt::Level OL = CodeGenOpt::Default);
Dan Gohman0f2d71d2008-08-27 23:52:12 +000060 virtual ~SelectionDAGISel();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 TargetLowering &getTargetLowering() { return TLI; }
63
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
65
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000066 virtual bool runOnMachineFunction(MachineFunction &MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
Owen Andersonac9de032009-08-10 22:56:29 +000068 unsigned MakeReg(EVT VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
70 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
Dan Gohman14a66442008-08-23 02:25:05 +000071 virtual void InstructionSelect() = 0;
Evan Cheng34fd4f32008-06-30 20:45:06 +000072
Dan Gohman6a4a5362008-08-21 16:06:51 +000073 void SelectRootInit() {
Dan Gohman2d2a7a32008-09-30 18:30:35 +000074 DAGSize = CurDAG->AssignTopologicalOrder();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 }
76
77 /// 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 Gohman8181bd12008-07-27 21:46:04 +000082 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +000084 std::vector<SDValue> &OutOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 return true;
86 }
87
Evan Chengf80681e2010-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 Korobeynikovda76d322009-05-08 18:51:58 +000095
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
97 /// to use for this target when scheduling the DAG.
Dan Gohmandd6547d2009-01-15 22:18:12 +000098 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100protected:
Evan Cheng220dfe82008-07-01 18:49:06 +0000101 /// DAGSize - Size of DAG being instruction selected.
102 ///
103 unsigned DAGSize;
104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
106 /// by tblgen. Others should not call it.
Dan Gohman14a66442008-08-23 02:25:05 +0000107 void SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
109 // Calls to these predicates are generated by tblgen.
Dan Gohman8181bd12008-07-27 21:46:04 +0000110 bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmand6098272007-07-24 23:00:27 +0000111 int64_t DesiredMaskS) const;
Dan Gohman8181bd12008-07-27 21:46:04 +0000112 bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
Dan Gohmand6098272007-07-24 23:00:27 +0000113 int64_t DesiredMaskS) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
Chris Lattnerfdbdc8c2010-02-16 07:21:10 +0000115
116 /// CheckPatternPredicate - This function is generated by tblgen in the
117 /// target. It runs the specified pattern predicate and returns true if it
118 /// succeeds or false if it fails. The number is a private implementation
119 /// detail to the code tblgen produces.
120 virtual bool CheckPatternPredicate(unsigned PredNo) const {
121 assert(0 && "Tblgen should generate the implementation of this!");
122 return 0;
123 }
124
125 /// CheckNodePredicate - This function is generated by tblgen in the
126 /// target. It runs node predicate #PredNo and returns true if it succeeds or
127 /// false if it fails. The number is a private implementation
128 /// detail to the code tblgen produces.
129 virtual bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {
130 assert(0 && "Tblgen should generate the implementation of this!");
131 return 0;
132 }
133
Chris Lattner4a37ffb22010-02-17 00:41:34 +0000134 virtual bool CheckComplexPattern(SDNode *Root, SDValue N, unsigned PatternNo,
135 SmallVectorImpl<SDValue> &Result) {
136 assert(0 && "Tblgen should generate the implementation of this!");
137 return false;
138 }
139
Dan Gohmanb65e3ac2009-10-29 22:30:23 +0000140 // Calls to these functions are generated by tblgen.
Dan Gohman5f082a72010-01-05 01:24:18 +0000141 SDNode *Select_INLINEASM(SDNode *N);
142 SDNode *Select_UNDEF(SDNode *N);
143 SDNode *Select_EH_LABEL(SDNode *N);
144 void CannotYetSelect(SDNode *N);
145 void CannotYetSelectIntrinsic(SDNode *N);
Dan Gohmanb65e3ac2009-10-29 22:30:23 +0000146
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147private:
Dan Gohman76dd96e2008-09-23 21:53:34 +0000148 void SelectAllBasicBlocks(Function &Fn, MachineFunction &MF,
Dan Gohman9dd43582008-10-14 23:54:11 +0000149 MachineModuleInfo *MMI,
Devang Patelfcf1c752009-01-13 00:35:13 +0000150 DwarfWriter *DW,
Dan Gohman9dd43582008-10-14 23:54:11 +0000151 const TargetInstrInfo &TII);
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000152 void FinishBasicBlock();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
Dan Gohman14a66442008-08-23 02:25:05 +0000154 void SelectBasicBlock(BasicBlock *LLVMBB,
155 BasicBlock::iterator Begin,
Dan Gohman40b0a2e2009-11-20 02:51:26 +0000156 BasicBlock::iterator End,
157 bool &HadTailCall);
Dan Gohman14a66442008-08-23 02:25:05 +0000158 void CodeGenAndEmitDAG();
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000159 void LowerArguments(BasicBlock *BB);
Chris Lattner68068cc2008-06-17 06:09:18 +0000160
Evan Cheng095dac22010-01-06 19:38:29 +0000161 void ShrinkDemandedOps();
Dan Gohman14a66442008-08-23 02:25:05 +0000162 void ComputeLiveOutVRegInfo();
163
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000164 void HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Dan Gohmanca4857a2008-09-03 23:12:08 +0000166 bool HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, FastISel *F);
167
Dan Gohman29800e22009-02-06 18:26:51 +0000168 /// Create the scheduler. If a specific scheduler was specified
169 /// via the SchedulerRegistry, use it, otherwise select the
170 /// one preferred by the target.
171 ///
Dan Gohman7c968a82009-02-11 04:27:20 +0000172 ScheduleDAGSDNodes *CreateScheduler();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173};
174
175}
176
177#endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */