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