LLVM API Documentation

JumpInstrTables.h
Go to the documentation of this file.
00001 //===-- JumpInstrTables.h: Jump-Instruction Tables --------------*- C++ -*-===//
00002 //
00003 // This file is distributed under the University of Illinois Open Source
00004 // License. See LICENSE.TXT for details.
00005 //
00006 //===----------------------------------------------------------------------===//
00007 ///
00008 /// \file
00009 /// \brief An implementation of tables consisting of jump instructions
00010 ///
00011 //===----------------------------------------------------------------------===//
00012 
00013 #ifndef LLVM_CODEGEN_JUMPINSTRTABLES_H
00014 #define LLVM_CODEGEN_JUMPINSTRTABLES_H
00015 
00016 #include "llvm/ADT/DenseMap.h"
00017 #include "llvm/Pass.h"
00018 #include "llvm/Target/TargetOptions.h"
00019 
00020 namespace llvm {
00021 class Constant;
00022 class Function;
00023 class FunctionType;
00024 class JumpInstrTableInfo;
00025 class Module;
00026 
00027 /// A class to manage a set of jump tables indexed on function type. It looks at
00028 /// each function in the module to find all the functions that have the
00029 /// jumptable attribute set. For each such function, it creates a new
00030 /// jump-instruction-table function and stores the mapping in the ImmutablePass
00031 /// JumpInstrTableInfo.
00032 ///
00033 /// These special functions get lowered in AsmPrinter to assembly of the form:
00034 /// \verbatim
00035 ///   .globl f
00036 ///   .type f,@function
00037 ///   .align 8,0x90
00038 /// f:
00039 ///   jmp f_orig@PLT
00040 /// \endverbatim
00041 ///
00042 /// Support for an architecture depends on two functions in TargetInstrInfo:
00043 /// getUnconditionalBranch, and getTrap. AsmPrinter uses these to generate the
00044 /// appropriate instructions for the jump statement (an unconditional branch)
00045 /// and for padding to make the table have a size that is a power of two. This
00046 /// padding uses a trap instruction to ensure that calls to this area halt the
00047 /// program. The default implementations of these functions call
00048 /// llvm_unreachable.
00049 class JumpInstrTables : public ModulePass {
00050 public:
00051   static char ID;
00052 
00053   JumpInstrTables();
00054   JumpInstrTables(JumpTable::JumpTableType JTT);
00055   virtual ~JumpInstrTables();
00056   bool runOnModule(Module &M) override;
00057   const char *getPassName() const override { return "Jump-Instruction Tables"; }
00058   void getAnalysisUsage(AnalysisUsage &AU) const override;
00059 
00060   /// Creates a jump-instruction table function for the Target and adds it to
00061   /// the tables.
00062   Function *insertEntry(Module &M, Function *Target);
00063 
00064   /// Checks to see if there is already a table for the given FunctionType.
00065   bool hasTable(FunctionType *FunTy);
00066 
00067 private:
00068   /// The metadata used while a jump table is being built
00069   struct TableMeta {
00070     /// The number of this table
00071     unsigned TableNum;
00072 
00073     /// The current number of jump entries in the table.
00074     unsigned Count;
00075   };
00076 
00077   typedef DenseMap<FunctionType *, struct TableMeta> JumpMap;
00078 
00079   /// Maps the function into a subset of function types, depending on the
00080   /// jump-instruction table style selected from JumpTableTypes in
00081   /// JumpInstrTables.cpp. The choice of mapping determines the number of
00082   /// jump-instruction tables generated by this pass. E.g., the simplest mapping
00083   /// converts every function type into void f(); so, all functions end up in a
00084   /// single table.
00085   FunctionType *transformType(FunctionType *FunTy);
00086 
00087   /// The current state of functions and jump entries in the table(s).
00088   JumpMap Metadata;
00089 
00090   /// The ImmutablePass that stores information about the generated tables.
00091   JumpInstrTableInfo *JITI;
00092 
00093   /// The total number of tables.
00094   unsigned TableCount;
00095 
00096   /// The type of tables to build.
00097   JumpTable::JumpTableType JTType;
00098 };
00099 
00100 /// Creates a JumpInstrTables pass for the given type of jump table.
00101 ModulePass *createJumpInstrTablesPass(JumpTable::JumpTableType JTT);
00102 }
00103 
00104 #endif /* LLVM_CODEGEN_JUMPINSTRTABLES_H */