1 //===-- FastISel.h - Definition of the FastISel class ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the FastISel class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_FASTISEL_H
15 #define LLVM_CODEGEN_FASTISEL_H
17 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/CodeGen/ValueTypes.h"
28 class MachineBasicBlock;
29 class MachineConstantPool;
30 class MachineFunction;
32 class MachineFrameInfo;
33 class MachineRegisterInfo;
35 class TargetInstrInfo;
38 class TargetRegisterClass;
40 /// FastISel - This is a fast-path instruction selection class that
41 /// generates poor code and doesn't support illegal types or non-trivial
42 /// lowering, but runs quickly.
45 MachineBasicBlock *MBB;
46 DenseMap<const Value *, unsigned> LocalValueMap;
47 DenseMap<const Value *, unsigned> &ValueMap;
48 DenseMap<const BasicBlock *, MachineBasicBlock *> &MBBMap;
49 DenseMap<const AllocaInst *, int> &StaticAllocaMap;
50 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate;
52 SmallSet<const Instruction *, 8> &CatchInfoLost;
55 MachineRegisterInfo &MRI;
56 MachineFrameInfo &MFI;
57 MachineConstantPool &MCP;
59 const TargetMachine &TM;
61 const TargetInstrInfo &TII;
62 const TargetLowering &TLI;
66 /// startNewBlock - Set the current block to which generated machine
67 /// instructions will be appended, and clear the local CSE map.
69 void startNewBlock(MachineBasicBlock *mbb) {
71 LocalValueMap.clear();
74 /// setCurrentBlock - Set the current block to which generated machine
75 /// instructions will be appended.
77 void setCurrentBlock(MachineBasicBlock *mbb) {
81 /// getCurDebugLoc() - Return current debug location information.
82 DebugLoc getCurDebugLoc() const { return DL; }
84 /// SelectInstruction - Do "fast" instruction selection for the given
85 /// LLVM IR instruction, and append generated machine instructions to
86 /// the current block. Return true if selection was successful.
88 bool SelectInstruction(const Instruction *I);
90 /// SelectOperator - Do "fast" instruction selection for the given
91 /// LLVM IR operator (Instruction or ConstantExpr), and append
92 /// generated machine instructions to the current block. Return true
93 /// if selection was successful.
95 bool SelectOperator(const User *I, unsigned Opcode);
97 /// getRegForValue - Create a virtual register and arrange for it to
98 /// be assigned the value for the given LLVM value.
99 unsigned getRegForValue(const Value *V);
101 /// lookUpRegForValue - Look up the value to see if its value is already
102 /// cached in a register. It may be defined by instructions across blocks or
104 unsigned lookUpRegForValue(const Value *V);
106 /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
107 /// takes care of truncating or sign-extending the given getelementptr
109 unsigned getRegForGEPIndex(const Value *V);
114 FastISel(MachineFunction &mf,
115 DenseMap<const Value *, unsigned> &vm,
116 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
117 DenseMap<const AllocaInst *, int> &am,
118 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate
120 , SmallSet<const Instruction *, 8> &cil
124 /// TargetSelectInstruction - This method is called by target-independent
125 /// code when the normal FastISel process fails to select an instruction.
126 /// This gives targets a chance to emit code for anything that doesn't
127 /// fit into FastISel's framework. It returns true if it was successful.
130 TargetSelectInstruction(const Instruction *I) = 0;
132 /// FastEmit_r - This method is called by target-independent code
133 /// to request that an instruction with the given type and opcode
135 virtual unsigned FastEmit_(MVT VT,
139 /// FastEmit_r - This method is called by target-independent code
140 /// to request that an instruction with the given type, opcode, and
141 /// register operand be emitted.
143 virtual unsigned FastEmit_r(MVT VT,
145 unsigned Opcode, unsigned Op0);
147 /// FastEmit_rr - This method is called by target-independent code
148 /// to request that an instruction with the given type, opcode, and
149 /// register operands be emitted.
151 virtual unsigned FastEmit_rr(MVT VT,
154 unsigned Op0, unsigned Op1);
156 /// FastEmit_ri - This method is called by target-independent code
157 /// to request that an instruction with the given type, opcode, and
158 /// register and immediate operands be emitted.
160 virtual unsigned FastEmit_ri(MVT VT,
163 unsigned Op0, uint64_t Imm);
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.
169 virtual unsigned FastEmit_rf(MVT VT,
172 unsigned Op0, const ConstantFP *FPImm);
174 /// FastEmit_rri - This method is called by target-independent code
175 /// to request that an instruction with the given type, opcode, and
176 /// register and immediate operands be emitted.
178 virtual unsigned FastEmit_rri(MVT VT,
181 unsigned Op0, unsigned Op1, uint64_t Imm);
183 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
184 /// to emit an instruction with an immediate operand using FastEmit_ri.
185 /// If that fails, it materializes the immediate into a register and try
186 /// FastEmit_rr instead.
187 unsigned FastEmit_ri_(MVT VT,
189 unsigned Op0, uint64_t Imm,
192 /// FastEmit_rf_ - This method is a wrapper of FastEmit_rf. It first tries
193 /// to emit an instruction with an immediate operand using FastEmit_rf.
194 /// If that fails, it materializes the immediate into a register and try
195 /// FastEmit_rr instead.
196 unsigned FastEmit_rf_(MVT VT,
198 unsigned Op0, const ConstantFP *FPImm,
201 /// FastEmit_i - This method is called by target-independent code
202 /// to request that an instruction with the given type, opcode, and
203 /// immediate operand be emitted.
204 virtual unsigned FastEmit_i(MVT VT,
209 /// FastEmit_f - This method is called by target-independent code
210 /// to request that an instruction with the given type, opcode, and
211 /// floating-point immediate operand be emitted.
212 virtual unsigned FastEmit_f(MVT VT,
215 const ConstantFP *FPImm);
217 /// FastEmitInst_ - Emit a MachineInstr with no operands and a
218 /// result register in the given register class.
220 unsigned FastEmitInst_(unsigned MachineInstOpcode,
221 const TargetRegisterClass *RC);
223 /// FastEmitInst_r - Emit a MachineInstr with one register operand
224 /// and a result register in the given register class.
226 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
227 const TargetRegisterClass *RC,
230 /// FastEmitInst_rr - Emit a MachineInstr with two register operands
231 /// and a result register in the given register class.
233 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
234 const TargetRegisterClass *RC,
235 unsigned Op0, unsigned Op1);
237 /// FastEmitInst_ri - Emit a MachineInstr with two register operands
238 /// and a result register in the given register class.
240 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
241 const TargetRegisterClass *RC,
242 unsigned Op0, uint64_t Imm);
244 /// FastEmitInst_rf - Emit a MachineInstr with two register operands
245 /// and a result register in the given register class.
247 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
248 const TargetRegisterClass *RC,
249 unsigned Op0, const ConstantFP *FPImm);
251 /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
252 /// an immediate, and a result register in the given register class.
254 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
255 const TargetRegisterClass *RC,
256 unsigned Op0, unsigned Op1, uint64_t Imm);
258 /// FastEmitInst_i - Emit a MachineInstr with a single immediate
259 /// operand, and a result register in the given register class.
260 unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
261 const TargetRegisterClass *RC,
264 /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
265 /// from a specified index of a superregister to a specified type.
266 unsigned FastEmitInst_extractsubreg(MVT RetVT,
267 unsigned Op0, uint32_t Idx);
269 /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
270 /// with all but the least significant bit set to zero.
271 unsigned FastEmitZExtFromI1(MVT VT,
274 /// FastEmitBranch - Emit an unconditional branch to the given block,
275 /// unless it is the immediate (fall-through) successor, and update
277 void FastEmitBranch(MachineBasicBlock *MBB);
279 unsigned UpdateValueMap(const Value* I, unsigned Reg);
281 unsigned createResultReg(const TargetRegisterClass *RC);
283 /// TargetMaterializeConstant - Emit a constant in a register using
284 /// target-specific logic, such as constant pool loads.
285 virtual unsigned TargetMaterializeConstant(const Constant* C) {
289 /// TargetMaterializeAlloca - Emit an alloca address in a register using
290 /// target-specific logic.
291 virtual unsigned TargetMaterializeAlloca(const AllocaInst* C) {
296 bool SelectBinaryOp(const User *I, unsigned ISDOpcode);
298 bool SelectFNeg(const User *I);
300 bool SelectGetElementPtr(const User *I);
302 bool SelectCall(const User *I);
304 bool SelectBitCast(const User *I);
306 bool SelectCast(const User *I, unsigned Opcode);
308 /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
309 /// Emit code to ensure constants are copied into registers when needed.
310 /// Remember the virtual registers that need to be added to the Machine PHI
311 /// nodes as input. We cannot just directly add them, because expansion
312 /// might result in multiple MBB's for one BB. As such, the start of the
313 /// BB might correspond to a different MBB than the end.
314 bool HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
316 /// materializeRegForValue - Helper for getRegForVale. This function is
317 /// called when the value isn't already available in a register and must
318 /// be materialized with new instructions.
319 unsigned materializeRegForValue(const Value *V, MVT VT);