LLVM IR to Asm Part 4

LLVM IR to Asm Part 4

9 IR Memory Representation:

9.1 Value class Reference

///LLVM Value Representation
///This is a very important LLVM class. It is the base class of all values
///computed by a program that may be used as operands to other values. Value is
///the super class of other important classes such as Instruction and Function.
///All Values  have a Type. Type is not a subclass of Value. Some values  can
///have a name and they belong to some Module. Setting the name on the Value
///automatically updates the module's symbol table.
///Every value has a "use list" that keeps track of which other Values  are
///using this Value. A Value can also have an arbitrary number of ValueHandle
///objects that watch it and listen to RAUW and Destroy events. See
///llvm/IR/ValueHandle.h for details.
class Value {
  Type *VTy;
  Use *UseList;
  const unsigned char SubclassID;//Subclass identifier (for isa/dyn_cast)
  unsigned char HasValueHandle: 1;//Has a ValueHandle pointing to this?
protected:
 ///Hold subclass data that can be dropped.
 ///This member is similar to SubclassData, however it is for holding
 ///information which may be used to aid optimization, but which may be
 ///cleared to zero without affecting conservative interpretation.
  unsigned char SubclassOptionalData: 7;
private:
 ///Hold arbitrary subclass data.
 ///This member is defined by this class, but is not used for anything.
 ///Subclasses can use it to hold whatever state they find useful. This
 ///field is initialized to zero by the ctor.
  unsigned short SubclassData;
protected:
 ///The number of operands in the subclass.
 ///This member is defined by this class, but not used for anything.
 ///Subclasses can use it to store their number of operands, if they have
 ///any.
 ///This is stored here to save space in User on 64-bit hosts. Since most
 ///instances of Value have operands, 32-bit hosts aren't significantly
 ///affected.
 ///Note, this should *NOT* be used directly by any class other than User.
 ///User uses this value to find the Use list.
  enum: unsigned {NumUserOperandsBits = 28 };
  unsigned NumUserOperands: NumUserOperandsBits;
 //Use the same type as the bitfield above so that MSVC will pack them.
  unsigned IsUsedByMD: 1;
  unsigned HasName: 1;
  unsigned HasHungOffUses: 1;
  unsigned HasDescriptor: 1;
};

9.2 Use

///A Use represents the edge between a Value definition and its users.
///This is notionally a two-dimensional linked list. It supports traversing
///all of the uses for a particular value definition. It also supports jumping
///directly to the used value when we arrive from the User's operands, and
///jumping directly to the User when we arrive from the Value's uses.
class Use {
  Value *Val = nullptr;
  Use *Next = nullptr;
  Use **Prev = nullptr;
  User *Parent = nullptr;
};

9.3 User:Value

class User: public Value {};

9.4 Module

///A Module instance is used to store all the information related to an
///LLVM module. Modules are the top level container of all other LLVM
///Intermediate Representation (IR) objects. Each module directly contains a
///list of globals variables, a list of functions, a list of libraries (or
///other modules) this module depends on, a symbol table, and various data
///about the target's characteristics.
///A module maintains a GlobalValRefMap object that is used to hold all
///constant references to global variables in the module. When a global
///variable is destroyed, it should have no entries in the GlobalValueRefMap.
///The main container class for the LLVM Intermediate Representation.
class Module {
  LLVMContext &Context;///< The LLVMContext from which types and
                                 ///< constants are allocated.
  GlobalListType GlobalList;///< The Global Variables in the module
  FunctionListType FunctionList;///< The Functions in the module
  AliasListType AliasList;///< The Aliases in the module
  IFuncListType IFuncList;///< The IFuncs in the module
  NamedMDListType NamedMDList;///< The named metadata in the module
  std::string GlobalScopeAsm;///< Inline Asm at global scope.
  std::unique_ptr<ValueSymbolTable> ValSymTab;///< Symbol table for values
  ComdatSymTabType ComdatSymTab;///< Symbol table for COMDATs
  std::unique_ptr<MemoryBuffer>
  OwnedMemoryBuffer;///< Memory buffer directly owned by this
                                 ///< module, for legacy clients only.
  std::unique_ptr<GVMaterializer>
  Materializer;///< Used to materialize GlobalValues
  std::string ModuleID;///< Human readable identifier for the module
  std::string SourceFileName;///< Original source file name for module,
                                 ///< recorded in bitcode.
  std::string TargetTriple;///< Platform target triple Module compiled on
                                 ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
  NamedMDSymTabType NamedMDSymTab;///< NamedMDNode names.
  DataLayout DL;///< DataLayout associated with the module
};

9.5 ValueSymbolTable

///This class provides a symbol table of name/value pairs. It is essentially
///a std::map<std::string,Value*> but has a controlled interface provided by
///LLVM as well as ensuring uniqueness of names.
///
class ValueSymbolTable {
  ValueMap vmap;///< The map that holds the symbol table.
  mutable uint32_t LastUnique = 0;///< Counter for tracking unique names
};

9.6 Function:GlobalObject,ilist node <Function>

class Function: public GlobalObject, public ilist_node<Function> {
 //Important things that make up a function!
  BasicBlockListType BasicBlocks;///< The basic blocks
  mutable Argument *Arguments = nullptr;///< The formal arguments
  size_t NumArgs;
  std::unique_ptr<ValueSymbolTable>
  SymTab;///< Symbol table of args/instructions
  AttributeList AttributeSets;///< Parameter attributes
};

9.7 Argument:Value

///This class represents an incoming formal argument to a Function. A formal
///argument, since it is ``formal'', does not contain an actual value but
///instead represents the type, argument number, and attributes of an argument
///for a specific function. When used in the body of said function, the
///argument of course represents the value of the actual argument that the
///function was called with.
class Argument final: public Value {
  Function *Parent;
  unsigned ArgNo;
};

9.8 BasicBlock:Value,ilist node with parent <BasicBlock,Function>

///LLVM Basic Block Representation
///
///This represents a single basic block in LLVM. A basic block is simply a
///container of instructions that execute sequentially. Basic blocks are Values
///because they are referenced by instructions such as branches and switch
///tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
///represents a label to which a branch can jump.
///
///A well formed basic block is formed of a list of non-terminating
///instructions followed by a single terminator instruction. Terminator
///instructions may not occur in the middle of basic blocks, and must terminate
///the blocks. The BasicBlock class allows malformed basic blocks to occur
///because it may be useful in the intermediate stage of constructing or
///modifying a program. However, the verifier will ensure that basic blocks are
///"well formed".
class BasicBlock final: public Value,//Basic blocks are data objects also
                         public ilist_node_with_parent<BasicBlock, Function> {
  InstListType InstList;
  Function *Parent;
};

9.9 Instruction:User,ilist node with parent <Instruction,BasicBlock>

class Instruction: public User,
                    public ilist_node_with_parent<Instruction, BasicBlock> {
  BasicBlock *Parent;
  DebugLoc DbgLoc;//'dbg' Metadata cache.
 ///Relative order of this instruction in its parent basic block. Used for
 ///O(1) local dominance checks between instructions.
  mutable unsigned Order = 0;
};

9.10 GlobalVariable:GlobalObject,ilist node <GlobalVariable>

class GlobalVariable: public GlobalObject, public ilist_node<GlobalVariable> {
  AttributeSet Attrs;
  bool isConstantGlobal: 1;//Is this a global constant?
  bool isExternallyInitializedConstant: 1;//Is this a global whose value
                                              //can change from its initial
                                              //value before global
                                              //initializers are run?
};

9.11 GlobalAlias:GlobalIndirectSymbol,ilist node <GlobalAlias>

class GlobalAlias: public GlobalIndirectSymbol,
                    public ilist_node<GlobalAlias> {};

9.12 GlobalIFunc:GlobalIndirectSymbol,ilist node <GlobalIFunc>

class GlobalIFunc final: public GlobalIndirectSymbol,
                          public ilist_node<GlobalIFunc> {};

9.13 NamedMDNode:ilist node <NamedMDNode>

//===--------------------------------------------- -------------------------===//
///A tuple of MDNodes.
///
///Despite its name, a NamedMDNode isn't itself an MDNode.
///
///NamedMDNodes are named module-level entities that contain lists of MDNodes.
///
///It is illegal for a NamedMDNode to appear as an operand of an MDNode.
class NamedMDNode: public ilist_node<NamedMDNode> {
  std::string Name;
  Module *Parent = nullptr;
  void *Operands;//SmallVector<TrackingMDRef, 4>
};

10 IRBuilder:IRBuilderBase :

///This provides a uniform API for creating instructions and inserting
///them into a basic block: either at the end of a BasicBlock, or at a specific
///iterator location in a block.
///Note that the builder does not expose the full generality of LLVM
///instructions. For access to extra instruction properties, use the mutators
///(eg setVolatile) on the instructions after they have been
///created. Convenience state exists to specify fast-math flags and fp-math
///tags.
///The first template argument specifies a class to use for creating constants.
///This defaults to creating minimally folded constants. The second template
///argument allows clients to specify custom insertion hooks that are called on
///every newly created insertion.
template <typename FolderTy = ConstantFolder,
          typename InserterTy = IRBuilderDefaultInserter>
class IRBuilder : public IRBuilderBase {
private:
  FolderTy Folder;
  InserterTy Inserter;};
///Common base class shared among various IRBuilders.
class IRBuilderBase {
 ///Pairs of (metadata kind, MDNode *) that should be added to all newly
 ///created instructions, like !dbg metadata.
  SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
  BasicBlock *BB;
  BasicBlock::iterator InsertPt;
  LLVMContext &Context;
  const IRBuilderFolder &Folder;
  const IRBuilderDefaultInserter &Inserter;
  MDNode *DefaultFPMathTag;
  FastMathFlags FMF;
  bool IsFPConstrained;
  fp::ExceptionBehavior DefaultConstrainedExcept;
  RoundingMode DefaultConstrainedRounding;
  ArrayRef<OperandBundleDef> DefaultOperandBundles;};