Home | History | Annotate | Download | only in CodeGen
      1 //===-- JumpInstrTables.h: Jump-Instruction Tables --------------*- C++ -*-===//
      2 //
      3 // This file is distributed under the University of Illinois Open Source
      4 // License. See LICENSE.TXT for details.
      5 //
      6 //===----------------------------------------------------------------------===//
      7 ///
      8 /// \file
      9 /// \brief An implementation of tables consisting of jump instructions
     10 ///
     11 //===----------------------------------------------------------------------===//
     12 
     13 #ifndef LLVM_CODEGEN_JUMPINSTRTABLES_H
     14 #define LLVM_CODEGEN_JUMPINSTRTABLES_H
     15 
     16 #include "llvm/ADT/DenseMap.h"
     17 #include "llvm/Pass.h"
     18 #include "llvm/Target/TargetOptions.h"
     19 
     20 namespace llvm {
     21 class Constant;
     22 class Function;
     23 class FunctionType;
     24 class JumpInstrTableInfo;
     25 class Module;
     26 
     27 /// A class to manage a set of jump tables indexed on function type. It looks at
     28 /// each function in the module to find all the functions that have the
     29 /// jumptable attribute set. For each such function, it creates a new
     30 /// jump-instruction-table function and stores the mapping in the ImmutablePass
     31 /// JumpInstrTableInfo.
     32 ///
     33 /// These special functions get lowered in AsmPrinter to assembly of the form:
     34 /// \verbatim
     35 ///   .globl f
     36 ///   .type f,@function
     37 ///   .align 8,0x90
     38 /// f:
     39 ///   jmp f_orig@PLT
     40 /// \endverbatim
     41 ///
     42 /// Support for an architecture depends on two functions in TargetInstrInfo:
     43 /// getUnconditionalBranch, and getTrap. AsmPrinter uses these to generate the
     44 /// appropriate instructions for the jump statement (an unconditional branch)
     45 /// and for padding to make the table have a size that is a power of two. This
     46 /// padding uses a trap instruction to ensure that calls to this area halt the
     47 /// program. The default implementations of these functions call
     48 /// llvm_unreachable.
     49 class JumpInstrTables : public ModulePass {
     50 public:
     51   static char ID;
     52 
     53   JumpInstrTables();
     54   JumpInstrTables(JumpTable::JumpTableType JTT);
     55   virtual ~JumpInstrTables();
     56   bool runOnModule(Module &M) override;
     57   const char *getPassName() const override { return "Jump-Instruction Tables"; }
     58   void getAnalysisUsage(AnalysisUsage &AU) const override;
     59 
     60   /// Creates a jump-instruction table function for the Target and adds it to
     61   /// the tables.
     62   Function *insertEntry(Module &M, Function *Target);
     63 
     64   /// Checks to see if there is already a table for the given FunctionType.
     65   bool hasTable(FunctionType *FunTy);
     66 
     67 private:
     68   /// The metadata used while a jump table is being built
     69   struct TableMeta {
     70     /// The number of this table
     71     unsigned TableNum;
     72 
     73     /// The current number of jump entries in the table.
     74     unsigned Count;
     75   };
     76 
     77   typedef DenseMap<FunctionType *, struct TableMeta> JumpMap;
     78 
     79   /// Maps the function into a subset of function types, depending on the
     80   /// jump-instruction table style selected from JumpTableTypes in
     81   /// JumpInstrTables.cpp. The choice of mapping determines the number of
     82   /// jump-instruction tables generated by this pass. E.g., the simplest mapping
     83   /// converts every function type into void f(); so, all functions end up in a
     84   /// single table.
     85   FunctionType *transformType(FunctionType *FunTy);
     86 
     87   /// The current state of functions and jump entries in the table(s).
     88   JumpMap Metadata;
     89 
     90   /// The ImmutablePass that stores information about the generated tables.
     91   JumpInstrTableInfo *JITI;
     92 
     93   /// The total number of tables.
     94   unsigned TableCount;
     95 
     96   /// The type of tables to build.
     97   JumpTable::JumpTableType JTType;
     98 };
     99 
    100 /// Creates a JumpInstrTables pass for the given type of jump table.
    101 ModulePass *createJumpInstrTablesPass(JumpTable::JumpTableType JTT);
    102 }
    103 
    104 #endif /* LLVM_CODEGEN_JUMPINSTRTABLES_H */
    105