]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/include/llvm/CodeGen/FastISel.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / 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/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20
21 namespace llvm {
22
23 class AllocaInst;
24 class Constant;
25 class ConstantFP;
26 class FunctionLoweringInfo;
27 class Instruction;
28 class LoadInst;
29 class MachineBasicBlock;
30 class MachineConstantPool;
31 class MachineFunction;
32 class MachineInstr;
33 class MachineFrameInfo;
34 class MachineRegisterInfo;
35 class DataLayout;
36 class TargetInstrInfo;
37 class TargetLibraryInfo;
38 class TargetLowering;
39 class TargetMachine;
40 class TargetRegisterClass;
41 class TargetRegisterInfo;
42 class User;
43 class Value;
44
45 /// FastISel - This is a fast-path instruction selection class that
46 /// generates poor code and doesn't support illegal types or non-trivial
47 /// lowering, but runs quickly.
48 class FastISel {
49 protected:
50   DenseMap<const Value *, unsigned> LocalValueMap;
51   FunctionLoweringInfo &FuncInfo;
52   MachineRegisterInfo &MRI;
53   MachineFrameInfo &MFI;
54   MachineConstantPool &MCP;
55   DebugLoc DL;
56   const TargetMachine &TM;
57   const DataLayout &TD;
58   const TargetInstrInfo &TII;
59   const TargetLowering &TLI;
60   const TargetRegisterInfo &TRI;
61   const TargetLibraryInfo *LibInfo;
62
63   /// The position of the last instruction for materializing constants
64   /// for use in the current block. It resets to EmitStartPt when it
65   /// makes sense (for example, it's usually profitable to avoid function
66   /// calls between the definition and the use)
67   MachineInstr *LastLocalValue;
68
69   /// The top most instruction in the current block that is allowed for
70   /// emitting local variables. LastLocalValue resets to EmitStartPt when
71   /// it makes sense (for example, on function calls)
72   MachineInstr *EmitStartPt;
73
74 public:
75   /// getLastLocalValue - Return the position of the last instruction
76   /// emitted for materializing constants for use in the current block.
77   MachineInstr *getLastLocalValue() { return LastLocalValue; }
78
79   /// setLastLocalValue - Update the position of the last instruction
80   /// emitted for materializing constants for use in the current block.
81   void setLastLocalValue(MachineInstr *I) {
82     EmitStartPt = I;
83     LastLocalValue = I;
84   }
85
86   /// startNewBlock - Set the current block to which generated machine
87   /// instructions will be appended, and clear the local CSE map.
88   ///
89   void startNewBlock();
90
91   /// getCurDebugLoc() - Return current debug location information.
92   DebugLoc getCurDebugLoc() const { return DL; }
93   
94   /// LowerArguments - Do "fast" instruction selection for function arguments
95   /// and append machine instructions to the current block. Return true if
96   /// it is successful.
97   bool LowerArguments();
98
99   /// SelectInstruction - Do "fast" instruction selection for the given
100   /// LLVM IR instruction, and append generated machine instructions to
101   /// the current block. Return true if selection was successful.
102   ///
103   bool SelectInstruction(const Instruction *I);
104
105   /// SelectOperator - Do "fast" instruction selection for the given
106   /// LLVM IR operator (Instruction or ConstantExpr), and append
107   /// generated machine instructions to the current block. Return true
108   /// if selection was successful.
109   ///
110   bool SelectOperator(const User *I, unsigned Opcode);
111
112   /// getRegForValue - Create a virtual register and arrange for it to
113   /// be assigned the value for the given LLVM value.
114   unsigned getRegForValue(const Value *V);
115
116   /// lookUpRegForValue - Look up the value to see if its value is already
117   /// cached in a register. It may be defined by instructions across blocks or
118   /// defined locally.
119   unsigned lookUpRegForValue(const Value *V);
120
121   /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
122   /// takes care of truncating or sign-extending the given getelementptr
123   /// index value.
124   std::pair<unsigned, bool> getRegForGEPIndex(const Value *V);
125
126   /// \brief We're checking to see if we can fold \p LI into \p FoldInst.
127   /// Note that we could have a sequence where multiple LLVM IR instructions
128   /// are folded into the same machineinstr.  For example we could have:
129   ///   A: x = load i32 *P
130   ///   B: y = icmp A, 42
131   ///   C: br y, ...
132   ///
133   /// In this scenario, \p LI is "A", and \p FoldInst is "C".  We know
134   /// about "B" (and any other folded instructions) because it is between
135   /// A and C.
136   ///
137   /// If we succeed folding, return true.
138   ///
139   bool tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst);
140
141   /// \brief The specified machine instr operand is a vreg, and that
142   /// vreg is being provided by the specified load instruction.  If possible,
143   /// try to fold the load as an operand to the instruction, returning true if
144   /// possible.
145   /// This method should be implemented by targets.
146   virtual bool tryToFoldLoadIntoMI(MachineInstr * /*MI*/, unsigned /*OpNo*/,
147                                    const LoadInst * /*LI*/) {
148     return false;
149   }
150
151   /// recomputeInsertPt - Reset InsertPt to prepare for inserting instructions
152   /// into the current block.
153   void recomputeInsertPt();
154
155   /// removeDeadCode - Remove all dead instructions between the I and E.
156   void removeDeadCode(MachineBasicBlock::iterator I,
157                       MachineBasicBlock::iterator E);
158
159   struct SavePoint {
160     MachineBasicBlock::iterator InsertPt;
161     DebugLoc DL;
162   };
163
164   /// enterLocalValueArea - Prepare InsertPt to begin inserting instructions
165   /// into the local value area and return the old insert position.
166   SavePoint enterLocalValueArea();
167
168   /// leaveLocalValueArea - Reset InsertPt to the given old insert position.
169   void leaveLocalValueArea(SavePoint Old);
170
171   virtual ~FastISel();
172
173 protected:
174   explicit FastISel(FunctionLoweringInfo &funcInfo,
175                     const TargetLibraryInfo *libInfo);
176
177   /// TargetSelectInstruction - This method is called by target-independent
178   /// code when the normal FastISel process fails to select an instruction.
179   /// This gives targets a chance to emit code for anything that doesn't
180   /// fit into FastISel's framework. It returns true if it was successful.
181   ///
182   virtual bool
183   TargetSelectInstruction(const Instruction *I) = 0;
184   
185   /// FastLowerArguments - This method is called by target-independent code to
186   /// do target specific argument lowering. It returns true if it was
187   /// successful.
188   virtual bool FastLowerArguments();
189
190   /// FastEmit_r - This method is called by target-independent code
191   /// to request that an instruction with the given type and opcode
192   /// be emitted.
193   virtual unsigned FastEmit_(MVT VT,
194                              MVT RetVT,
195                              unsigned Opcode);
196
197   /// FastEmit_r - This method is called by target-independent code
198   /// to request that an instruction with the given type, opcode, and
199   /// register operand be emitted.
200   ///
201   virtual unsigned FastEmit_r(MVT VT,
202                               MVT RetVT,
203                               unsigned Opcode,
204                               unsigned Op0, bool Op0IsKill);
205
206   /// FastEmit_rr - This method is called by target-independent code
207   /// to request that an instruction with the given type, opcode, and
208   /// register operands be emitted.
209   ///
210   virtual unsigned FastEmit_rr(MVT VT,
211                                MVT RetVT,
212                                unsigned Opcode,
213                                unsigned Op0, bool Op0IsKill,
214                                unsigned Op1, bool Op1IsKill);
215
216   /// FastEmit_ri - This method is called by target-independent code
217   /// to request that an instruction with the given type, opcode, and
218   /// register and immediate operands be emitted.
219   ///
220   virtual unsigned FastEmit_ri(MVT VT,
221                                MVT RetVT,
222                                unsigned Opcode,
223                                unsigned Op0, bool Op0IsKill,
224                                uint64_t Imm);
225
226   /// FastEmit_rf - This method is called by target-independent code
227   /// to request that an instruction with the given type, opcode, and
228   /// register and floating-point immediate operands be emitted.
229   ///
230   virtual unsigned FastEmit_rf(MVT VT,
231                                MVT RetVT,
232                                unsigned Opcode,
233                                unsigned Op0, bool Op0IsKill,
234                                const ConstantFP *FPImm);
235
236   /// FastEmit_rri - This method is called by target-independent code
237   /// to request that an instruction with the given type, opcode, and
238   /// register and immediate operands be emitted.
239   ///
240   virtual unsigned FastEmit_rri(MVT VT,
241                                 MVT RetVT,
242                                 unsigned Opcode,
243                                 unsigned Op0, bool Op0IsKill,
244                                 unsigned Op1, bool Op1IsKill,
245                                 uint64_t Imm);
246
247   /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
248   /// to emit an instruction with an immediate operand using FastEmit_ri.
249   /// If that fails, it materializes the immediate into a register and try
250   /// FastEmit_rr instead.
251   unsigned FastEmit_ri_(MVT VT,
252                         unsigned Opcode,
253                         unsigned Op0, bool Op0IsKill,
254                         uint64_t Imm, MVT ImmType);
255
256   /// FastEmit_i - This method is called by target-independent code
257   /// to request that an instruction with the given type, opcode, and
258   /// immediate operand be emitted.
259   virtual unsigned FastEmit_i(MVT VT,
260                               MVT RetVT,
261                               unsigned Opcode,
262                               uint64_t Imm);
263
264   /// FastEmit_f - This method is called by target-independent code
265   /// to request that an instruction with the given type, opcode, and
266   /// floating-point immediate operand be emitted.
267   virtual unsigned FastEmit_f(MVT VT,
268                               MVT RetVT,
269                               unsigned Opcode,
270                               const ConstantFP *FPImm);
271
272   /// FastEmitInst_ - Emit a MachineInstr with no operands and a
273   /// result register in the given register class.
274   ///
275   unsigned FastEmitInst_(unsigned MachineInstOpcode,
276                          const TargetRegisterClass *RC);
277
278   /// FastEmitInst_r - Emit a MachineInstr with one register operand
279   /// and a result register in the given register class.
280   ///
281   unsigned FastEmitInst_r(unsigned MachineInstOpcode,
282                           const TargetRegisterClass *RC,
283                           unsigned Op0, bool Op0IsKill);
284
285   /// FastEmitInst_rr - Emit a MachineInstr with two register operands
286   /// and a result register in the given register class.
287   ///
288   unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
289                            const TargetRegisterClass *RC,
290                            unsigned Op0, bool Op0IsKill,
291                            unsigned Op1, bool Op1IsKill);
292
293   /// FastEmitInst_rrr - Emit a MachineInstr with three register operands
294   /// and a result register in the given register class.
295   ///
296   unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
297                            const TargetRegisterClass *RC,
298                            unsigned Op0, bool Op0IsKill,
299                            unsigned Op1, bool Op1IsKill,
300                            unsigned Op2, bool Op2IsKill);
301
302   /// FastEmitInst_ri - Emit a MachineInstr with a register operand,
303   /// an immediate, and a result register in the given register class.
304   ///
305   unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
306                            const TargetRegisterClass *RC,
307                            unsigned Op0, bool Op0IsKill,
308                            uint64_t Imm);
309
310   /// FastEmitInst_rii - Emit a MachineInstr with one register operand
311   /// and two immediate operands.
312   ///
313   unsigned FastEmitInst_rii(unsigned MachineInstOpcode,
314                            const TargetRegisterClass *RC,
315                            unsigned Op0, bool Op0IsKill,
316                            uint64_t Imm1, uint64_t Imm2);
317
318   /// FastEmitInst_rf - Emit a MachineInstr with two register operands
319   /// and a result register in the given register class.
320   ///
321   unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
322                            const TargetRegisterClass *RC,
323                            unsigned Op0, bool Op0IsKill,
324                            const ConstantFP *FPImm);
325
326   /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
327   /// an immediate, and a result register in the given register class.
328   ///
329   unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
330                             const TargetRegisterClass *RC,
331                             unsigned Op0, bool Op0IsKill,
332                             unsigned Op1, bool Op1IsKill,
333                             uint64_t Imm);
334
335   /// FastEmitInst_rrii - Emit a MachineInstr with two register operands,
336   /// two immediates operands, and a result register in the given register
337   /// class.
338   unsigned FastEmitInst_rrii(unsigned MachineInstOpcode,
339                              const TargetRegisterClass *RC,
340                              unsigned Op0, bool Op0IsKill,
341                              unsigned Op1, bool Op1IsKill,
342                              uint64_t Imm1, uint64_t Imm2);
343
344   /// FastEmitInst_i - Emit a MachineInstr with a single immediate
345   /// operand, and a result register in the given register class.
346   unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
347                           const TargetRegisterClass *RC,
348                           uint64_t Imm);
349
350   /// FastEmitInst_ii - Emit a MachineInstr with a two immediate operands.
351   unsigned FastEmitInst_ii(unsigned MachineInstrOpcode,
352                           const TargetRegisterClass *RC,
353                           uint64_t Imm1, uint64_t Imm2);
354
355   /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
356   /// from a specified index of a superregister to a specified type.
357   unsigned FastEmitInst_extractsubreg(MVT RetVT,
358                                       unsigned Op0, bool Op0IsKill,
359                                       uint32_t Idx);
360
361   /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
362   /// with all but the least significant bit set to zero.
363   unsigned FastEmitZExtFromI1(MVT VT,
364                               unsigned Op0, bool Op0IsKill);
365
366   /// FastEmitBranch - Emit an unconditional branch to the given block,
367   /// unless it is the immediate (fall-through) successor, and update
368   /// the CFG.
369   void FastEmitBranch(MachineBasicBlock *MBB, DebugLoc DL);
370
371   void UpdateValueMap(const Value* I, unsigned Reg, unsigned NumRegs = 1);
372
373   unsigned createResultReg(const TargetRegisterClass *RC);
374
375   /// TargetMaterializeConstant - Emit a constant in a register using
376   /// target-specific logic, such as constant pool loads.
377   virtual unsigned TargetMaterializeConstant(const Constant* C) {
378     return 0;
379   }
380
381   /// TargetMaterializeAlloca - Emit an alloca address in a register using
382   /// target-specific logic.
383   virtual unsigned TargetMaterializeAlloca(const AllocaInst* C) {
384     return 0;
385   }
386
387   virtual unsigned TargetMaterializeFloatZero(const ConstantFP* CF) {
388     return 0;
389   }
390
391 private:
392   bool SelectBinaryOp(const User *I, unsigned ISDOpcode);
393
394   bool SelectFNeg(const User *I);
395
396   bool SelectGetElementPtr(const User *I);
397
398   bool SelectCall(const User *I);
399
400   bool SelectBitCast(const User *I);
401
402   bool SelectCast(const User *I, unsigned Opcode);
403
404   bool SelectExtractValue(const User *I);
405
406   bool SelectInsertValue(const User *I);
407
408   /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
409   /// Emit code to ensure constants are copied into registers when needed.
410   /// Remember the virtual registers that need to be added to the Machine PHI
411   /// nodes as input.  We cannot just directly add them, because expansion
412   /// might result in multiple MBB's for one BB.  As such, the start of the
413   /// BB might correspond to a different MBB than the end.
414   bool HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
415
416   /// materializeRegForValue - Helper for getRegForVale. This function is
417   /// called when the value isn't already available in a register and must
418   /// be materialized with new instructions.
419   unsigned materializeRegForValue(const Value *V, MVT VT);
420
421   /// flushLocalValueMap - clears LocalValueMap and moves the area for the
422   /// new local variables to the beginning of the block. It helps to avoid
423   /// spilling cached variables across heavy instructions like calls.
424   void flushLocalValueMap();
425
426   /// hasTrivialKill - Test whether the given value has exactly one use.
427   bool hasTrivialKill(const Value *V) const;
428 };
429
430 }
431
432 #endif