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