make MachineFunction keep track of its ID and make
[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/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/CodeGen/SelectionDAGNodes.h"
20
21 namespace llvm {
22
23 class AllocaInst;
24 class ConstantFP;
25 class Instruction;
26 class MachineBasicBlock;
27 class MachineConstantPool;
28 class MachineFunction;
29 class MachineFrameInfo;
30 class MachineModuleInfo;
31 class DwarfWriter;
32 class MachineRegisterInfo;
33 class TargetData;
34 class TargetInstrInfo;
35 class TargetLowering;
36 class TargetMachine;
37 class TargetRegisterClass;
38
39 /// FastISel - This is a fast-path instruction selection class that
40 /// generates poor code and doesn't support illegal types or non-trivial
41 /// lowering, but runs quickly.
42 class FastISel {
43 protected:
44   MachineBasicBlock *MBB;
45   DenseMap<const Value *, unsigned> LocalValueMap;
46   DenseMap<const Value *, unsigned> &ValueMap;
47   DenseMap<const BasicBlock *, MachineBasicBlock *> &MBBMap;
48   DenseMap<const AllocaInst *, int> &StaticAllocaMap;
49 #ifndef NDEBUG
50   SmallSet<Instruction*, 8> &CatchInfoLost;
51 #endif
52   MachineFunction &MF;
53   MachineModuleInfo *MMI;
54   DwarfWriter *DW;
55   MachineRegisterInfo &MRI;
56   MachineFrameInfo &MFI;
57   MachineConstantPool &MCP;
58   DebugLoc DL;
59   const TargetMachine &TM;
60   const TargetData &TD;
61   const TargetInstrInfo &TII;
62   const TargetLowering &TLI;
63
64 public:
65   /// startNewBlock - Set the current block to which generated machine
66   /// instructions will be appended, and clear the local CSE map.
67   ///
68   void startNewBlock(MachineBasicBlock *mbb) {
69     setCurrentBlock(mbb);
70     LocalValueMap.clear();
71   }
72
73   /// setCurrentBlock - Set the current block to which generated machine
74   /// instructions will be appended.
75   ///
76   void setCurrentBlock(MachineBasicBlock *mbb) {
77     MBB = mbb;
78   }
79
80   /// setCurDebugLoc - Set the current debug location information, which is used
81   /// when creating a machine instruction.
82   ///
83   void setCurDebugLoc(DebugLoc dl) { DL = dl; }
84
85   /// getCurDebugLoc() - Return current debug location information.
86   DebugLoc getCurDebugLoc() const { return DL; }
87
88   /// SelectInstruction - Do "fast" instruction selection for the given
89   /// LLVM IR instruction, and append generated machine instructions to
90   /// the current block. Return true if selection was successful.
91   ///
92   bool SelectInstruction(Instruction *I);
93
94   /// SelectOperator - Do "fast" instruction selection for the given
95   /// LLVM IR operator (Instruction or ConstantExpr), and append
96   /// generated machine instructions to the current block. Return true
97   /// if selection was successful.
98   ///
99   bool SelectOperator(User *I, unsigned Opcode);
100
101   /// getRegForValue - Create a virtual register and arrange for it to
102   /// be assigned the value for the given LLVM value.
103   unsigned getRegForValue(Value *V);
104
105   /// lookUpRegForValue - Look up the value to see if its value is already
106   /// cached in a register. It may be defined by instructions across blocks or
107   /// defined locally.
108   unsigned lookUpRegForValue(Value *V);
109
110   /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
111   /// takes care of truncating or sign-extending the given getelementptr
112   /// index value.
113   unsigned getRegForGEPIndex(Value *V);
114
115   virtual ~FastISel();
116
117 protected:
118   FastISel(MachineFunction &mf,
119            MachineModuleInfo *mmi,
120            DwarfWriter *dw,
121            DenseMap<const Value *, unsigned> &vm,
122            DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
123            DenseMap<const AllocaInst *, int> &am
124 #ifndef NDEBUG
125            , SmallSet<Instruction*, 8> &cil
126 #endif
127            );
128
129   /// TargetSelectInstruction - This method is called by target-independent
130   /// code when the normal FastISel process fails to select an instruction.
131   /// This gives targets a chance to emit code for anything that doesn't
132   /// fit into FastISel's framework. It returns true if it was successful.
133   ///
134   virtual bool
135   TargetSelectInstruction(Instruction *I) = 0;
136
137   /// FastEmit_r - This method is called by target-independent code
138   /// to request that an instruction with the given type and opcode
139   /// be emitted.
140   virtual unsigned FastEmit_(MVT VT,
141                              MVT RetVT,
142                              unsigned Opcode);
143
144   /// FastEmit_r - This method is called by target-independent code
145   /// to request that an instruction with the given type, opcode, and
146   /// register operand be emitted.
147   ///
148   virtual unsigned FastEmit_r(MVT VT,
149                               MVT RetVT,
150                               unsigned Opcode, unsigned Op0);
151
152   /// FastEmit_rr - This method is called by target-independent code
153   /// to request that an instruction with the given type, opcode, and
154   /// register operands be emitted.
155   ///
156   virtual unsigned FastEmit_rr(MVT VT,
157                                MVT RetVT,
158                                unsigned Opcode,
159                                unsigned Op0, unsigned Op1);
160
161   /// FastEmit_ri - This method is called by target-independent code
162   /// to request that an instruction with the given type, opcode, and
163   /// register and immediate operands be emitted.
164   ///
165   virtual unsigned FastEmit_ri(MVT VT,
166                                MVT RetVT,
167                                unsigned Opcode,
168                                unsigned Op0, uint64_t Imm);
169
170   /// FastEmit_rf - This method is called by target-independent code
171   /// to request that an instruction with the given type, opcode, and
172   /// register and floating-point immediate operands be emitted.
173   ///
174   virtual unsigned FastEmit_rf(MVT VT,
175                                MVT RetVT,
176                                unsigned Opcode,
177                                unsigned Op0, ConstantFP *FPImm);
178
179   /// FastEmit_rri - This method is called by target-independent code
180   /// to request that an instruction with the given type, opcode, and
181   /// register and immediate operands be emitted.
182   ///
183   virtual unsigned FastEmit_rri(MVT VT,
184                                 MVT RetVT,
185                                 unsigned Opcode,
186                                 unsigned Op0, unsigned Op1, uint64_t Imm);
187
188   /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
189   /// to emit an instruction with an immediate operand using FastEmit_ri.
190   /// If that fails, it materializes the immediate into a register and try
191   /// FastEmit_rr instead.
192   unsigned FastEmit_ri_(MVT VT,
193                         unsigned Opcode,
194                         unsigned Op0, uint64_t Imm,
195                         MVT ImmType);
196   
197   /// FastEmit_rf_ - This method is a wrapper of FastEmit_rf. It first tries
198   /// to emit an instruction with an immediate operand using FastEmit_rf.
199   /// If that fails, it materializes the immediate into a register and try
200   /// FastEmit_rr instead.
201   unsigned FastEmit_rf_(MVT VT,
202                         unsigned Opcode,
203                         unsigned Op0, ConstantFP *FPImm,
204                         MVT ImmType);
205   
206   /// FastEmit_i - This method is called by target-independent code
207   /// to request that an instruction with the given type, opcode, and
208   /// immediate operand be emitted.
209   virtual unsigned FastEmit_i(MVT VT,
210                               MVT RetVT,
211                               unsigned Opcode,
212                               uint64_t Imm);
213
214   /// FastEmit_f - This method is called by target-independent code
215   /// to request that an instruction with the given type, opcode, and
216   /// floating-point immediate operand be emitted.
217   virtual unsigned FastEmit_f(MVT VT,
218                               MVT RetVT,
219                               unsigned Opcode,
220                               ConstantFP *FPImm);
221
222   /// FastEmitInst_ - Emit a MachineInstr with no operands and a
223   /// result register in the given register class.
224   ///
225   unsigned FastEmitInst_(unsigned MachineInstOpcode,
226                          const TargetRegisterClass *RC);
227
228   /// FastEmitInst_r - Emit a MachineInstr with one register operand
229   /// and a result register in the given register class.
230   ///
231   unsigned FastEmitInst_r(unsigned MachineInstOpcode,
232                           const TargetRegisterClass *RC,
233                           unsigned Op0);
234
235   /// FastEmitInst_rr - Emit a MachineInstr with two register operands
236   /// and a result register in the given register class.
237   ///
238   unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
239                            const TargetRegisterClass *RC,
240                            unsigned Op0, unsigned Op1);
241
242   /// FastEmitInst_ri - Emit a MachineInstr with two register operands
243   /// and a result register in the given register class.
244   ///
245   unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
246                            const TargetRegisterClass *RC,
247                            unsigned Op0, uint64_t Imm);
248
249   /// FastEmitInst_rf - Emit a MachineInstr with two register operands
250   /// and a result register in the given register class.
251   ///
252   unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
253                            const TargetRegisterClass *RC,
254                            unsigned Op0, ConstantFP *FPImm);
255
256   /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
257   /// an immediate, and a result register in the given register class.
258   ///
259   unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
260                             const TargetRegisterClass *RC,
261                             unsigned Op0, unsigned Op1, uint64_t Imm);
262   
263   /// FastEmitInst_i - Emit a MachineInstr with a single immediate
264   /// operand, and a result register in the given register class.
265   unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
266                           const TargetRegisterClass *RC,
267                           uint64_t Imm);
268
269   /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
270   /// from a specified index of a superregister to a specified type.
271   unsigned FastEmitInst_extractsubreg(MVT RetVT,
272                                       unsigned Op0, uint32_t Idx);
273
274   /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
275   /// with all but the least significant bit set to zero.
276   unsigned FastEmitZExtFromI1(MVT VT,
277                               unsigned Op);
278
279   /// FastEmitBranch - Emit an unconditional branch to the given block,
280   /// unless it is the immediate (fall-through) successor, and update
281   /// the CFG.
282   void FastEmitBranch(MachineBasicBlock *MBB);
283
284   unsigned UpdateValueMap(Value* I, unsigned Reg);
285
286   unsigned createResultReg(const TargetRegisterClass *RC);
287   
288   /// TargetMaterializeConstant - Emit a constant in a register using 
289   /// target-specific logic, such as constant pool loads.
290   virtual unsigned TargetMaterializeConstant(Constant* C) {
291     return 0;
292   }
293
294   /// TargetMaterializeAlloca - Emit an alloca address in a register using
295   /// target-specific logic.
296   virtual unsigned TargetMaterializeAlloca(AllocaInst* C) {
297     return 0;
298   }
299
300 private:
301   bool SelectBinaryOp(User *I, unsigned ISDOpcode);
302
303   bool SelectFNeg(User *I);
304
305   bool SelectGetElementPtr(User *I);
306
307   bool SelectCall(User *I);
308
309   bool SelectBitCast(User *I);
310   
311   bool SelectCast(User *I, unsigned Opcode);
312 };
313
314 }
315
316 #endif