Machine IR


Q&A

  • How does it handle .got addressing?

References:

Tasks for LLVM Machine Representation:

  • Resource Allocation: registers, stack space, …
  • Lowering: ABI, Exception Handling, Debug Info, …
  • Optimization: Peephole, Instruction/Block Scheduling, ..

Basics

Pass Manage Setup

Pass manager pipeline is setup in TargetPassConfig.

  • Target overrides methods to add, remove or replace passes.
  • There is also insertPass and subtitutePass.

MachineInstruction

  • class MachineInstruction MI
  • Opcode
  • Pointer to Machine Basic Block
  • Operand Array; Memory Operand Array
  • Debugging Location

Operands

  • class MachineOperand MOP
  • Register, RegisterMask
  • Immediates
  • Indexes: Frame, ConstantPool, Target…
  • Addresses: ExternalSymbol, BlockAddress, …
  • Predicate, Metadata, …

Instruction Description

  • class MCInstrDesc: Opcode/Instruction Description
  • Describes operands types, register classes
  • Flags describing instruction:
    • Format
    • Semantic
    • Filter for target callbacks
    • Side Effects
    • Transformation Hints/Constraints

Basic Blocks

  • class MachineBasicBlock (MBB)
  • double linked list of instructions ???
  • Pointer to Machine Function and IR Basic Block

Functions

  • class MachineFunction (MF)
  • Double linked list of basic blocks
  • Poniters to IR Function, TargetMachine, TargetSubtargetInfo, MCContext, …
  • State: MachineRegisterInfo, MachineFrameInfo, MachineConstantPool, MachineJumpTableInfo, …
MachineConstantPool

Register Allocation

Physical Registers

  • MachineRegisterInfo maintains list of uses and definitions per register.

Virtual Registers

  • MachineRegisterInfo
    • differentiate virtual/physical: .isVirtualRegister(Reg), .isPhysicalRegister(Reg)
    • Register == 0: no register used.

Prolog Epilog Insertion Pass

  • Setup call frames, setup stack frame
  • Save/Restore callee saved registers
  • Resolve frame indexes
  • Register scavenging

TargetFrameLowering class

class TargetFrameLowering { // ...
  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
  /// the function.
  virtual void emitPrologue(MachineFunction &MF,
                            MachineBasicBlock &MBB) const = 0;
  virtual void emitEpilogue(MachineFunction &MF,
                            MachineBasicBlock &MBB) const = 0;
  virtual void determineCalleeSaves(MachineFunction &MF, BitVector /*...*/);
  virtual void processFunctionBeforeFrameFinalized(MachineFunction /*...*/);
  
}
  • MachineFunctionPass
  • References: llvm/lib/CodeGen/MachineFunctionPass.cpp llvm/include/llvm/CodeGen/MachineFunctionPass.h Pass definition and creation /// MachineFunctionPass - This class adapts the FunctionPass interface to /// allow convenient creation of passes that operate on the MachineFunction /// representation. Instead of overriding runOnFunction, subclasses /// override runOnMachineFunction. classMachineFunctionPass : public FunctionPass { public: bool doInitialization(Module&) override { // Cache the properties info at module-init time so we don't have to // construct them for every function.

Created Nov 21, 2019 // Last Updated Aug 15, 2020

If you could revise
the fundmental principles of
computer system design
to improve security...

... what would you change?