Re-apply bottom-up fast-isel, with fixes. Be very careful to avoid emitting
[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 #include "llvm/CodeGen/MachineBasicBlock.h"
23
24 namespace llvm {
25
26 class AllocaInst;
27 class ConstantFP;
28 class FunctionLoweringInfo;
29 class Instruction;
30 class MachineBasicBlock;
31 class MachineConstantPool;
32 class MachineFunction;
33 class MachineInstr;
34 class MachineFrameInfo;
35 class MachineRegisterInfo;
36 class TargetData;
37 class TargetInstrInfo;
38 class TargetLowering;
39 class TargetMachine;
40 class TargetRegisterClass;
41 class TargetRegisterInfo;
42
43 /// FastISel - This is a fast-path instruction selection class that
44 /// generates poor code and doesn't support illegal types or non-trivial
45 /// lowering, but runs quickly.
46 class FastISel {
47 protected:
48   DenseMap<const Value *, unsigned> LocalValueMap;
49   FunctionLoweringInfo &FuncInfo;
50   MachineRegisterInfo &MRI;
51   MachineFrameInfo &MFI;
52   MachineConstantPool &MCP;
53   DebugLoc DL;
54   const TargetMachine &TM;
55   const TargetData &TD;
56   const TargetInstrInfo &TII;
57   const TargetLowering &TLI;
58   const TargetRegisterInfo &TRI;
59   MachineInstr *LastLocalValue;
60
61 public:
62   /// getLastLocalValue - Return the position of the last instruction
63   /// emitted for materializing constants for use in the current block.
64   MachineInstr *getLastLocalValue() { return LastLocalValue; }
65
66   /// setLastLocalValue - Update the position of the last instruction
67   /// emitted for materializing constants for use in the current block.
68   void setLastLocalValue(MachineInstr *I) { LastLocalValue = I; }
69
70   /// startNewBlock - Set the current block to which generated machine
71   /// instructions will be appended, and clear the local CSE map.
72   ///
73   void startNewBlock();
74
75   /// getCurDebugLoc() - Return current debug location information.
76   DebugLoc getCurDebugLoc() const { return DL; }
77
78   /// SelectInstruction - Do "fast" instruction selection for the given
79   /// LLVM IR instruction, and append generated machine instructions to
80   /// the current block. Return true if selection was successful.
81   ///
82   bool SelectInstruction(const Instruction *I);
83
84   /// SelectOperator - Do "fast" instruction selection for the given
85   /// LLVM IR operator (Instruction or ConstantExpr), and append
86   /// generated machine instructions to the current block. Return true
87   /// if selection was successful.
88   ///
89   bool SelectOperator(const User *I, unsigned Opcode);
90
91   /// getRegForValue - Create a virtual register and arrange for it to
92   /// be assigned the value for the given LLVM value.
93   unsigned getRegForValue(const Value *V);
94
95   /// lookUpRegForValue - Look up the value to see if its value is already
96   /// cached in a register. It may be defined by instructions across blocks or
97   /// defined locally.
98   unsigned lookUpRegForValue(const Value *V);
99
100   /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
101   /// takes care of truncating or sign-extending the given getelementptr
102   /// index value.
103   std::pair<unsigned, bool> getRegForGEPIndex(const Value *V);
104
105   /// recomputeInsertPt - Reset InsertPt to prepare for insterting instructions
106   /// into the current block.
107   void recomputeInsertPt();
108
109   /// enterLocalValueArea - Prepare InsertPt to begin inserting instructions
110   /// into the local value area and return the old insert position.
111   MachineBasicBlock::iterator enterLocalValueArea();
112
113   /// leaveLocalValueArea - Reset InsertPt to the given old insert position
114   void leaveLocalValueArea(MachineBasicBlock::iterator OldInsertPt);
115
116   virtual ~FastISel();
117
118 protected:
119   explicit FastISel(FunctionLoweringInfo &funcInfo);
120
121   /// TargetSelectInstruction - This method is called by target-independent
122   /// code when the normal FastISel process fails to select an instruction.
123   /// This gives targets a chance to emit code for anything that doesn't
124   /// fit into FastISel's framework. It returns true if it was successful.
125   ///
126   virtual bool
127   TargetSelectInstruction(const Instruction *I) = 0;
128
129   /// FastEmit_r - This method is called by target-independent code
130   /// to request that an instruction with the given type and opcode
131   /// be emitted.
132   virtual unsigned FastEmit_(MVT VT,
133                              MVT RetVT,
134                              unsigned Opcode);
135
136   /// FastEmit_r - This method is called by target-independent code
137   /// to request that an instruction with the given type, opcode, and
138   /// register operand be emitted.
139   ///
140   virtual unsigned FastEmit_r(MVT VT,
141                               MVT RetVT,
142                               unsigned Opcode,
143                               unsigned Op0, bool Op0IsKill);
144
145   /// FastEmit_rr - This method is called by target-independent code
146   /// to request that an instruction with the given type, opcode, and
147   /// register operands be emitted.
148   ///
149   virtual unsigned FastEmit_rr(MVT VT,
150                                MVT RetVT,
151                                unsigned Opcode,
152                                unsigned Op0, bool Op0IsKill,
153                                unsigned Op1, bool Op1IsKill);
154
155   /// FastEmit_ri - This method is called by target-independent code
156   /// to request that an instruction with the given type, opcode, and
157   /// register and immediate operands be emitted.
158   ///
159   virtual unsigned FastEmit_ri(MVT VT,
160                                MVT RetVT,
161                                unsigned Opcode,
162                                unsigned Op0, bool Op0IsKill,
163                                uint64_t Imm);
164
165   /// FastEmit_rf - This method is called by target-independent code
166   /// to request that an instruction with the given type, opcode, and
167   /// register and floating-point immediate operands be emitted.
168   ///
169   virtual unsigned FastEmit_rf(MVT VT,
170                                MVT RetVT,
171                                unsigned Opcode,
172                                unsigned Op0, bool Op0IsKill,
173                                const 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, bool Op0IsKill,
183                                 unsigned Op1, bool Op1IsKill,
184                                 uint64_t Imm);
185
186   /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
187   /// to emit an instruction with an immediate operand using FastEmit_ri.
188   /// If that fails, it materializes the immediate into a register and try
189   /// FastEmit_rr instead.
190   unsigned FastEmit_ri_(MVT VT,
191                         unsigned Opcode,
192                         unsigned Op0, bool Op0IsKill,
193                         uint64_t Imm, MVT ImmType);
194   
195   /// FastEmit_rf_ - This method is a wrapper of FastEmit_rf. It first tries
196   /// to emit an instruction with an immediate operand using FastEmit_rf.
197   /// If that fails, it materializes the immediate into a register and try
198   /// FastEmit_rr instead.
199   unsigned FastEmit_rf_(MVT VT,
200                         unsigned Opcode,
201                         unsigned Op0, bool Op0IsKill,
202                         const ConstantFP *FPImm, MVT ImmType);
203   
204   /// FastEmit_i - This method is called by target-independent code
205   /// to request that an instruction with the given type, opcode, and
206   /// immediate operand be emitted.
207   virtual unsigned FastEmit_i(MVT VT,
208                               MVT RetVT,
209                               unsigned Opcode,
210                               uint64_t Imm);
211
212   /// FastEmit_f - This method is called by target-independent code
213   /// to request that an instruction with the given type, opcode, and
214   /// floating-point immediate operand be emitted.
215   virtual unsigned FastEmit_f(MVT VT,
216                               MVT RetVT,
217                               unsigned Opcode,
218                               const ConstantFP *FPImm);
219
220   /// FastEmitInst_ - Emit a MachineInstr with no operands and a
221   /// result register in the given register class.
222   ///
223   unsigned FastEmitInst_(unsigned MachineInstOpcode,
224                          const TargetRegisterClass *RC);
225
226   /// FastEmitInst_r - Emit a MachineInstr with one register operand
227   /// and a result register in the given register class.
228   ///
229   unsigned FastEmitInst_r(unsigned MachineInstOpcode,
230                           const TargetRegisterClass *RC,
231                           unsigned Op0, bool Op0IsKill);
232
233   /// FastEmitInst_rr - Emit a MachineInstr with two register operands
234   /// and a result register in the given register class.
235   ///
236   unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
237                            const TargetRegisterClass *RC,
238                            unsigned Op0, bool Op0IsKill,
239                            unsigned Op1, bool Op1IsKill);
240
241   /// FastEmitInst_ri - Emit a MachineInstr with two register operands
242   /// and a result register in the given register class.
243   ///
244   unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
245                            const TargetRegisterClass *RC,
246                            unsigned Op0, bool Op0IsKill,
247                            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, bool Op0IsKill,
255                            const ConstantFP *FPImm);
256
257   /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
258   /// an immediate, and a result register in the given register class.
259   ///
260   unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
261                             const TargetRegisterClass *RC,
262                             unsigned Op0, bool Op0IsKill,
263                             unsigned Op1, bool Op1IsKill,
264                             uint64_t Imm);
265   
266   /// FastEmitInst_i - Emit a MachineInstr with a single immediate
267   /// operand, and a result register in the given register class.
268   unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
269                           const TargetRegisterClass *RC,
270                           uint64_t Imm);
271
272   /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
273   /// from a specified index of a superregister to a specified type.
274   unsigned FastEmitInst_extractsubreg(MVT RetVT,
275                                       unsigned Op0, bool Op0IsKill,
276                                       uint32_t Idx);
277
278   /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
279   /// with all but the least significant bit set to zero.
280   unsigned FastEmitZExtFromI1(MVT VT,
281                               unsigned Op0, bool Op0IsKill);
282
283   /// FastEmitBranch - Emit an unconditional branch to the given block,
284   /// unless it is the immediate (fall-through) successor, and update
285   /// the CFG.
286   void FastEmitBranch(MachineBasicBlock *MBB, DebugLoc DL);
287
288   unsigned UpdateValueMap(const Value* I, unsigned Reg);
289
290   unsigned createResultReg(const TargetRegisterClass *RC);
291   
292   /// TargetMaterializeConstant - Emit a constant in a register using 
293   /// target-specific logic, such as constant pool loads.
294   virtual unsigned TargetMaterializeConstant(const Constant* C) {
295     return 0;
296   }
297
298   /// TargetMaterializeAlloca - Emit an alloca address in a register using
299   /// target-specific logic.
300   virtual unsigned TargetMaterializeAlloca(const AllocaInst* C) {
301     return 0;
302   }
303
304 private:
305   bool SelectLoad(const User *I);
306
307   bool SelectBinaryOp(const User *I, unsigned ISDOpcode);
308
309   bool SelectFNeg(const User *I);
310
311   bool SelectGetElementPtr(const User *I);
312
313   bool SelectCall(const User *I);
314
315   bool SelectBitCast(const User *I);
316   
317   bool SelectCast(const User *I, unsigned Opcode);
318
319   /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
320   /// Emit code to ensure constants are copied into registers when needed.
321   /// Remember the virtual registers that need to be added to the Machine PHI
322   /// nodes as input.  We cannot just directly add them, because expansion
323   /// might result in multiple MBB's for one BB.  As such, the start of the
324   /// BB might correspond to a different MBB than the end.
325   bool HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
326
327   /// materializeRegForValue - Helper for getRegForVale. This function is
328   /// called when the value isn't already available in a register and must
329   /// be materialized with new instructions.
330   unsigned materializeRegForValue(const Value *V, MVT VT);
331
332   /// hasTrivialKill - Test whether the given value has exactly one use.
333   bool hasTrivialKill(const Value *V) const;
334 };
335
336 }
337
338 #endif