LLVM API Documentation

FastISel.h

Go to the documentation of this file.
00001 //===-- FastISel.h - Definition of the FastISel class ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the FastISel class.
00011 //  
00012 //===----------------------------------------------------------------------===//
00013   
00014 #ifndef LLVM_CODEGEN_FASTISEL_H
00015 #define LLVM_CODEGEN_FASTISEL_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/SmallSet.h"
00019 #include "llvm/CodeGen/DebugLoc.h"
00020 #include "llvm/CodeGen/SelectionDAGNodes.h"
00021 
00022 namespace llvm {
00023 
00024 class AllocaInst;
00025 class ConstantFP;
00026 class Instruction;
00027 class MachineBasicBlock;
00028 class MachineConstantPool;
00029 class MachineFunction;
00030 class MachineFrameInfo;
00031 class MachineModuleInfo;
00032 class DwarfWriter;
00033 class MachineRegisterInfo;
00034 class TargetData;
00035 class TargetInstrInfo;
00036 class TargetLowering;
00037 class TargetMachine;
00038 class TargetRegisterClass;
00039 
00040 /// FastISel - This is a fast-path instruction selection class that
00041 /// generates poor code and doesn't support illegal types or non-trivial
00042 /// lowering, but runs quickly.
00043 class FastISel {
00044 protected:
00045   MachineBasicBlock *MBB;
00046   DenseMap<const Value *, unsigned> LocalValueMap;
00047   DenseMap<const Value *, unsigned> &ValueMap;
00048   DenseMap<const BasicBlock *, MachineBasicBlock *> &MBBMap;
00049   DenseMap<const AllocaInst *, int> &StaticAllocaMap;
00050 #ifndef NDEBUG
00051   SmallSet<Instruction*, 8> &CatchInfoLost;
00052 #endif
00053   MachineFunction &MF;
00054   MachineModuleInfo *MMI;
00055   DwarfWriter *DW;
00056   MachineRegisterInfo &MRI;
00057   MachineFrameInfo &MFI;
00058   MachineConstantPool &MCP;
00059   DebugLoc DL;
00060   const TargetMachine &TM;
00061   const TargetData &TD;
00062   const TargetInstrInfo &TII;
00063   const TargetLowering &TLI;
00064 
00065 public:
00066   /// startNewBlock - Set the current block to which generated machine
00067   /// instructions will be appended, and clear the local CSE map.
00068   ///
00069   void startNewBlock(MachineBasicBlock *mbb) {
00070     setCurrentBlock(mbb);
00071     LocalValueMap.clear();
00072   }
00073 
00074   /// setCurrentBlock - Set the current block to which generated machine
00075   /// instructions will be appended.
00076   ///
00077   void setCurrentBlock(MachineBasicBlock *mbb) {
00078     MBB = mbb;
00079   }
00080 
00081   /// setCurDebugLoc - Set the current debug location information, which is used
00082   /// when creating a machine instruction.
00083   ///
00084   void setCurDebugLoc(DebugLoc dl) { DL = dl; }
00085 
00086   /// SelectInstruction - Do "fast" instruction selection for the given
00087   /// LLVM IR instruction, and append generated machine instructions to
00088   /// the current block. Return true if selection was successful.
00089   ///
00090   bool SelectInstruction(Instruction *I);
00091 
00092   /// SelectInstruction - Do "fast" instruction selection for the given
00093   /// LLVM IR operator (Instruction or ConstantExpr), and append
00094   /// generated machine instructions to the current block. Return true
00095   /// if selection was successful.
00096   ///
00097   bool SelectOperator(User *I, unsigned Opcode);
00098 
00099   /// TargetSelectInstruction - This method is called by target-independent
00100   /// code when the normal FastISel process fails to select an instruction.
00101   /// This gives targets a chance to emit code for anything that doesn't
00102   /// fit into FastISel's framework. It returns true if it was successful.
00103   ///
00104   virtual bool
00105   TargetSelectInstruction(Instruction *I) = 0;
00106 
00107   /// getRegForValue - Create a virtual register and arrange for it to
00108   /// be assigned the value for the given LLVM value.
00109   unsigned getRegForValue(Value *V);
00110 
00111   /// lookUpRegForValue - Look up the value to see if its value is already
00112   /// cached in a register. It may be defined by instructions across blocks or
00113   /// defined locally.
00114   unsigned lookUpRegForValue(Value *V);
00115 
00116   /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
00117   /// takes care of truncating or sign-extending the given getelementptr
00118   /// index value.
00119   unsigned getRegForGEPIndex(Value *V);
00120 
00121   virtual ~FastISel();
00122 
00123 protected:
00124   FastISel(MachineFunction &mf,
00125            MachineModuleInfo *mmi,
00126            DwarfWriter *dw,
00127            DenseMap<const Value *, unsigned> &vm,
00128            DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
00129            DenseMap<const AllocaInst *, int> &am
00130 #ifndef NDEBUG
00131            , SmallSet<Instruction*, 8> &cil
00132 #endif
00133            );
00134 
00135   /// FastEmit_r - This method is called by target-independent code
00136   /// to request that an instruction with the given type and opcode
00137   /// be emitted.
00138   virtual unsigned FastEmit_(MVT::SimpleValueType VT,
00139                              MVT::SimpleValueType RetVT,
00140                              ISD::NodeType Opcode);
00141 
00142   /// FastEmit_r - This method is called by target-independent code
00143   /// to request that an instruction with the given type, opcode, and
00144   /// register operand be emitted.
00145   ///
00146   virtual unsigned FastEmit_r(MVT::SimpleValueType VT,
00147                               MVT::SimpleValueType RetVT,
00148                               ISD::NodeType Opcode, unsigned Op0);
00149 
00150   /// FastEmit_rr - This method is called by target-independent code
00151   /// to request that an instruction with the given type, opcode, and
00152   /// register operands be emitted.
00153   ///
00154   virtual unsigned FastEmit_rr(MVT::SimpleValueType VT,
00155                                MVT::SimpleValueType RetVT,
00156                                ISD::NodeType Opcode,
00157                                unsigned Op0, unsigned Op1);
00158 
00159   /// FastEmit_ri - This method is called by target-independent code
00160   /// to request that an instruction with the given type, opcode, and
00161   /// register and immediate operands be emitted.
00162   ///
00163   virtual unsigned FastEmit_ri(MVT::SimpleValueType VT,
00164                                MVT::SimpleValueType RetVT,
00165                                ISD::NodeType Opcode,
00166                                unsigned Op0, uint64_t Imm);
00167 
00168   /// FastEmit_rf - This method is called by target-independent code
00169   /// to request that an instruction with the given type, opcode, and
00170   /// register and floating-point immediate operands be emitted.
00171   ///
00172   virtual unsigned FastEmit_rf(MVT::SimpleValueType VT,
00173                                MVT::SimpleValueType RetVT,
00174                                ISD::NodeType Opcode,
00175                                unsigned Op0, ConstantFP *FPImm);
00176 
00177   /// FastEmit_rri - This method is called by target-independent code
00178   /// to request that an instruction with the given type, opcode, and
00179   /// register and immediate operands be emitted.
00180   ///
00181   virtual unsigned FastEmit_rri(MVT::SimpleValueType VT,
00182                                 MVT::SimpleValueType RetVT,
00183                                 ISD::NodeType Opcode,
00184                                 unsigned Op0, unsigned Op1, uint64_t Imm);
00185 
00186   /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
00187   /// to emit an instruction with an immediate operand using FastEmit_ri.
00188   /// If that fails, it materializes the immediate into a register and try
00189   /// FastEmit_rr instead.
00190   unsigned FastEmit_ri_(MVT::SimpleValueType VT,
00191                         ISD::NodeType Opcode,
00192                         unsigned Op0, uint64_t Imm,
00193                         MVT::SimpleValueType ImmType);
00194   
00195   /// FastEmit_rf_ - This method is a wrapper of FastEmit_rf. It first tries
00196   /// to emit an instruction with an immediate operand using FastEmit_rf.
00197   /// If that fails, it materializes the immediate into a register and try
00198   /// FastEmit_rr instead.
00199   unsigned FastEmit_rf_(MVT::SimpleValueType VT,
00200                         ISD::NodeType Opcode,
00201                         unsigned Op0, ConstantFP *FPImm,
00202                         MVT::SimpleValueType ImmType);
00203   
00204   /// FastEmit_i - This method is called by target-independent code
00205   /// to request that an instruction with the given type, opcode, and
00206   /// immediate operand be emitted.
00207   virtual unsigned FastEmit_i(MVT::SimpleValueType VT,
00208                               MVT::SimpleValueType RetVT,
00209                               ISD::NodeType Opcode,
00210                               uint64_t Imm);
00211 
00212   /// FastEmit_f - This method is called by target-independent code
00213   /// to request that an instruction with the given type, opcode, and
00214   /// floating-point immediate operand be emitted.
00215   virtual unsigned FastEmit_f(MVT::SimpleValueType VT,
00216                               MVT::SimpleValueType RetVT,
00217                               ISD::NodeType Opcode,
00218                               ConstantFP *FPImm);
00219 
00220   /// FastEmitInst_ - Emit a MachineInstr with no operands and a
00221   /// result register in the given register class.
00222   ///
00223   unsigned FastEmitInst_(unsigned MachineInstOpcode,
00224                          const TargetRegisterClass *RC);
00225 
00226   /// FastEmitInst_r - Emit a MachineInstr with one register operand
00227   /// and a result register in the given register class.
00228   ///
00229   unsigned FastEmitInst_r(unsigned MachineInstOpcode,
00230                           const TargetRegisterClass *RC,
00231                           unsigned Op0);
00232 
00233   /// FastEmitInst_rr - Emit a MachineInstr with two register operands
00234   /// and a result register in the given register class.
00235   ///
00236   unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
00237                            const TargetRegisterClass *RC,
00238                            unsigned Op0, unsigned Op1);
00239 
00240   /// FastEmitInst_ri - Emit a MachineInstr with two register operands
00241   /// and a result register in the given register class.
00242   ///
00243   unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
00244                            const TargetRegisterClass *RC,
00245                            unsigned Op0, uint64_t Imm);
00246 
00247   /// FastEmitInst_rf - Emit a MachineInstr with two register operands
00248   /// and a result register in the given register class.
00249   ///
00250   unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
00251                            const TargetRegisterClass *RC,
00252                            unsigned Op0, ConstantFP *FPImm);
00253 
00254   /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
00255   /// an immediate, and a result register in the given register class.
00256   ///
00257   unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
00258                             const TargetRegisterClass *RC,
00259                             unsigned Op0, unsigned Op1, uint64_t Imm);
00260   
00261   /// FastEmitInst_i - Emit a MachineInstr with a single immediate
00262   /// operand, and a result register in the given register class.
00263   unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
00264                           const TargetRegisterClass *RC,
00265                           uint64_t Imm);
00266 
00267   /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
00268   /// from a specified index of a superregister to a specified type.
00269   unsigned FastEmitInst_extractsubreg(MVT::SimpleValueType RetVT,
00270                                       unsigned Op0, uint32_t Idx);
00271 
00272   /// FastEmitBranch - Emit an unconditional branch to the given block,
00273   /// unless it is the immediate (fall-through) successor, and update
00274   /// the CFG.
00275   void FastEmitBranch(MachineBasicBlock *MBB);
00276 
00277   void UpdateValueMap(Value* I, unsigned Reg);
00278 
00279   unsigned createResultReg(const TargetRegisterClass *RC);
00280   
00281   /// TargetMaterializeConstant - Emit a constant in a register using 
00282   /// target-specific logic, such as constant pool loads.
00283   virtual unsigned TargetMaterializeConstant(Constant* C) {
00284     return 0;
00285   }
00286 
00287   /// TargetMaterializeAlloca - Emit an alloca address in a register using
00288   /// target-specific logic.
00289   virtual unsigned TargetMaterializeAlloca(AllocaInst* C) {
00290     return 0;
00291   }
00292 
00293 private:
00294   bool SelectBinaryOp(User *I, ISD::NodeType ISDOpcode);
00295 
00296   bool SelectGetElementPtr(User *I);
00297 
00298   bool SelectCall(User *I);
00299 
00300   bool SelectBitCast(User *I);
00301   
00302   bool SelectCast(User *I, ISD::NodeType Opcode);
00303 };
00304 
00305 }
00306 
00307 #endif