9df3aa9ae6f9e30e1cab21ba841e409a191fbdcf
[oota-llvm.git] / include / llvm / CodeGen / FastISel.h
1 //===-- FastISel.h - Definition of the FastISel class ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the FastISel class.
11 //  
12 //===----------------------------------------------------------------------===//
13   
14 #ifndef LLVM_CODEGEN_FASTISEL_H
15 #define LLVM_CODEGEN_FASTISEL_H
16
17 #include "llvm/BasicBlock.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/CodeGen/SelectionDAGNodes.h"
20
21 namespace llvm {
22
23 class ConstantFP;
24 class MachineBasicBlock;
25 class MachineConstantPool;
26 class MachineFunction;
27 class MachineRegisterInfo;
28 class TargetData;
29 class TargetInstrInfo;
30 class TargetLowering;
31 class TargetMachine;
32 class TargetRegisterClass;
33
34 /// FastISel - This is a fast-path instruction selection class that
35 /// generates poor code and doesn't support illegal types or non-trivial
36 /// lowering, but runs quickly.
37 class FastISel {
38 protected:
39   MachineBasicBlock *MBB;
40   DenseMap<const Value *, unsigned> LocalValueMap;
41   DenseMap<const Value *, unsigned> &ValueMap;
42   DenseMap<const BasicBlock *, MachineBasicBlock *> &MBBMap;
43   MachineFunction &MF;
44   MachineRegisterInfo &MRI;
45   const TargetMachine &TM;
46   const TargetData &TD;
47   const TargetInstrInfo &TII;
48   const TargetLowering &TLI;
49
50 public:
51   /// setCurrentBlock - Set the current block, to which generated
52   /// machine instructions will be appended.
53   ///
54   void setCurrentBlock(MachineBasicBlock *mbb) {
55     MBB = mbb;
56   }
57
58   /// SelectInstruction - Do "fast" instruction selection for the given
59   /// LLVM IR instruction, and append generated machine instructions to
60   /// the current block. Return true if selection was successful.
61   ///
62   bool SelectInstruction(Instruction *I);
63
64   /// TargetSelectInstruction - This method is called by target-independent
65   /// code when the normal FastISel process fails to select an instruction.
66   /// This gives targets a chance to emit code for anything that doesn't
67   /// fit into FastISel's framework. It returns true if it was successful.
68   ///
69   virtual bool
70   TargetSelectInstruction(Instruction *I) = 0;
71
72   /// getRegForValue - Create a virtual register and arrange for it to
73   /// be assigned the value for the given LLVM value.
74   unsigned getRegForValue(Value *V);
75
76   virtual ~FastISel();
77
78 protected:
79   FastISel(MachineFunction &mf,
80            DenseMap<const Value *, unsigned> &vm,
81            DenseMap<const BasicBlock *, MachineBasicBlock *> &bm);
82
83   /// FastEmit_r - This method is called by target-independent code
84   /// to request that an instruction with the given type and opcode
85   /// be emitted.
86   virtual unsigned FastEmit_(MVT::SimpleValueType VT,
87                              MVT::SimpleValueType RetVT,
88                              ISD::NodeType Opcode);
89
90   /// FastEmit_r - This method is called by target-independent code
91   /// to request that an instruction with the given type, opcode, and
92   /// register operand be emitted.
93   ///
94   virtual unsigned FastEmit_r(MVT::SimpleValueType VT,
95                               MVT::SimpleValueType RetVT,
96                               ISD::NodeType Opcode, unsigned Op0);
97
98   /// FastEmit_rr - This method is called by target-independent code
99   /// to request that an instruction with the given type, opcode, and
100   /// register operands be emitted.
101   ///
102   virtual unsigned FastEmit_rr(MVT::SimpleValueType VT,
103                                MVT::SimpleValueType RetVT,
104                                ISD::NodeType Opcode,
105                                unsigned Op0, unsigned Op1);
106
107   /// FastEmit_ri - This method is called by target-independent code
108   /// to request that an instruction with the given type, opcode, and
109   /// register and immediate operands be emitted.
110   ///
111   virtual unsigned FastEmit_ri(MVT::SimpleValueType VT,
112                                MVT::SimpleValueType RetVT,
113                                ISD::NodeType Opcode,
114                                unsigned Op0, uint64_t Imm);
115
116   /// FastEmit_rf - This method is called by target-independent code
117   /// to request that an instruction with the given type, opcode, and
118   /// register and floating-point immediate operands be emitted.
119   ///
120   virtual unsigned FastEmit_rf(MVT::SimpleValueType VT,
121                                MVT::SimpleValueType RetVT,
122                                ISD::NodeType Opcode,
123                                unsigned Op0, ConstantFP *FPImm);
124
125   /// FastEmit_rri - This method is called by target-independent code
126   /// to request that an instruction with the given type, opcode, and
127   /// register and immediate operands be emitted.
128   ///
129   virtual unsigned FastEmit_rri(MVT::SimpleValueType VT,
130                                 MVT::SimpleValueType RetVT,
131                                 ISD::NodeType Opcode,
132                                 unsigned Op0, unsigned Op1, uint64_t Imm);
133
134   /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
135   /// to emit an instruction with an immediate operand using FastEmit_ri.
136   /// If that fails, it materializes the immediate into a register and try
137   /// FastEmit_rr instead.
138   unsigned FastEmit_ri_(MVT::SimpleValueType VT,
139                         ISD::NodeType Opcode,
140                         unsigned Op0, uint64_t Imm,
141                         MVT::SimpleValueType ImmType);
142   
143   /// FastEmit_rf_ - This method is a wrapper of FastEmit_rf. It first tries
144   /// to emit an instruction with an immediate operand using FastEmit_rf.
145   /// If that fails, it materializes the immediate into a register and try
146   /// FastEmit_rr instead.
147   unsigned FastEmit_rf_(MVT::SimpleValueType VT,
148                         ISD::NodeType Opcode,
149                         unsigned Op0, ConstantFP *FPImm,
150                         MVT::SimpleValueType ImmType);
151   
152   /// FastEmit_i - This method is called by target-independent code
153   /// to request that an instruction with the given type, opcode, and
154   /// immediate operand be emitted.
155   virtual unsigned FastEmit_i(MVT::SimpleValueType VT,
156                               MVT::SimpleValueType RetVT,
157                               ISD::NodeType Opcode,
158                               uint64_t Imm);
159
160   /// FastEmit_f - This method is called by target-independent code
161   /// to request that an instruction with the given type, opcode, and
162   /// floating-point immediate operand be emitted.
163   virtual unsigned FastEmit_f(MVT::SimpleValueType VT,
164                               MVT::SimpleValueType RetVT,
165                               ISD::NodeType Opcode,
166                               ConstantFP *FPImm);
167
168   /// FastEmitInst_ - Emit a MachineInstr with no operands and a
169   /// result register in the given register class.
170   ///
171   unsigned FastEmitInst_(unsigned MachineInstOpcode,
172                          const TargetRegisterClass *RC);
173
174   /// FastEmitInst_r - Emit a MachineInstr with one register operand
175   /// and a result register in the given register class.
176   ///
177   unsigned FastEmitInst_r(unsigned MachineInstOpcode,
178                           const TargetRegisterClass *RC,
179                           unsigned Op0);
180
181   /// FastEmitInst_rr - Emit a MachineInstr with two register operands
182   /// and a result register in the given register class.
183   ///
184   unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
185                            const TargetRegisterClass *RC,
186                            unsigned Op0, unsigned Op1);
187
188   /// FastEmitInst_ri - Emit a MachineInstr with two register operands
189   /// and a result register in the given register class.
190   ///
191   unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
192                            const TargetRegisterClass *RC,
193                            unsigned Op0, uint64_t Imm);
194
195   /// FastEmitInst_rf - Emit a MachineInstr with two register operands
196   /// and a result register in the given register class.
197   ///
198   unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
199                            const TargetRegisterClass *RC,
200                            unsigned Op0, ConstantFP *FPImm);
201
202   /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
203   /// an immediate, and a result register in the given register class.
204   ///
205   unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
206                             const TargetRegisterClass *RC,
207                             unsigned Op0, unsigned Op1, uint64_t Imm);
208   
209   /// FastEmitInst_i - Emit a MachineInstr with a single immediate
210   /// operand, and a result register in the given register class.
211   unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
212                           const TargetRegisterClass *RC,
213                           uint64_t Imm);
214
215   /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
216   /// from a specified index of a superregister.
217   unsigned FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx);
218
219   void UpdateValueMap(Value* I, unsigned Reg);
220
221   unsigned createResultReg(const TargetRegisterClass *RC);
222   
223   virtual unsigned TargetSelectConstantPoolLoad(Constant* C,
224                                                 MachineConstantPool* MCP) {
225     return 0;
226   }
227
228 private:
229   bool SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode);
230
231   bool SelectGetElementPtr(Instruction *I);
232
233   bool SelectBitCast(Instruction *I);
234   
235   bool SelectCast(Instruction *I, ISD::NodeType Opcode);
236 };
237
238 }
239
240 #endif