[][src]Module llir::values

LLVM Values

LLVM Values has the following hierarchy

Function & Global

You can get Functions & Globals from Module:

for func in module.iter_functions() {
  // Do things to function
}
for glob in module.iter_globals() {
  // Do things to global
}

Instruction

You can get instructions by iterating through blocks

for block in func.iter_blocks() {
  for instr in block.iter_instructions() {
    // Do things to instruction...
  }
}

You can match the instruction with any particular kind of instruction by using pattern matching. From there you can access the instruction specific accessors. As an example, here's how you get the call instruction specific accessor:

match instr {
  Instruction::Call(call) => {
    let callee = call.callee_function();
    // Do things to the callee function...
  }
}

All instructions inherits methods like operand, num_operands, parent_block, next_instruction and so. Checkout InstructionTrait.

Operand

You can get operand from instructions

let operand_0 = instr.operand(0).unwrap()

To check which type of operand this is, you do a pattern matching

match op {
  Operand::Constant(cons) => {
    // Do things to constant
  }
  Operand::Instruction(instr) => {
    // Do things to instruction
  }
  // ...
}

Metadata is also a kind of operand, as its used in some intrinsic instructions.

Constant

Constant is also a enum, to get a specific kind of constant, you also do pattern matching

match my_constant {
  Constant::ConstExpr(const_expr) => {
    match const_expr {
      ConstExpr::GetElementPtr(gep_const_expr) => {
        // Get the integer constant indices from the GEP constant expression
        let indices = gep_const_expr.int_indices();
        // ...
      }
    }
  },
  Constant::Function(f) => { /* ... */ },
  Constant::Global(g) => { /* ... */ },
  // ...
}

Constant expression is a kind of constant. There are only a limited amount of instruction opcode that can apply to constant expressions. To get each different kinds of constant expressions, you should do another pattern matching.

Functions & Globals are also constants.

Other constants involve integer, float, null and so on.

Structs

AllocaInstruction

Alloca instruction

Argument

Function argument value

ArrayConstant

Array constant

BinaryConstExpr

Binary constant expression

BinaryInstruction

Binary instruction

Block

A block inside of function

BlockAddress

Block Address

CallBrInstruction

CallBr instruction

CallInstruction

Call instruction

ConditionalBranchInstruction

Conditional Branch Instruction

ConstantAsMetadata

Constant as Metadata

DIExpression

DI Expression Metadata

DILabel

DI Label Metadata

DILocalVariable

DI Local Variable Metadata

DILocation

DI Location Metadata

DistinctMDOperandPlaceholder

Distinct Metadata Operand Placeholder

ExtractValueInstruction

Extract value instruction

FCmpConstExpr

FCmp constant expression

FCmpInstruction

Floating point comparison (FCmp) instruction

FloatConstant

Float constant

Function

Function value

GenericDINode

Generic DI Node Metadata

GenericValue

A placeholder value; used when the value kind is not supported yet

GetElementPtrConstExpr

Get element pointer constant expression

GetElementPtrInstruction

Get Element Pointer (GEP) instruction

GlobalAlias

Global alias

GlobalVariable

Global variable

ICmpConstExpr

ICmp constant expression

ICmpInstruction

Integer comparison (ICmp) instruction

IndirectBranchInstruction

Indirect Branch instruction

InlineAsm

Inline Assembly value

InsertValueInstruction

Insert value instruction

IntConstant

Integer constant

LoadInstruction

Load instruction

LocalAsMetadata

Local as Metadata

MDTuple

MD Tuple Metadata

NullConstant

Null constant

PhiIncoming

Incoming block & value for PHI instruction

PhiInstruction

PHI instruction

ReturnInstruction

Return (Ret) instruction

SelectInstruction

Select instruction

StoreInstruction

Store instruction

StructConstant

Struct constant

SwitchCase

One Case of switch

SwitchInstruction

Switch instruction

UnaryConstExpr

Unary constant expression

UnaryInstruction

Unary instruction

UnconditionalBranchInstruction

Unconditional branch instruction

Undef

Undefined value

UnreachableInstruction

Unreachable instruction

VectorConstant

Vector Constant

Enums

BinaryOpcode

Binary Opcode

BranchInstruction

Branch instruction

ConstExpr

Constant expression

Constant

Constants

FCmpPredicate

Floating point comparison predicate

Global

Global container enum

ICmpPredicate

Integer conparison predicate

Instruction

Instruction

Metadata

Metadata

Opcode
Operand

Container class for operand

UnaryOpcode

Unary opcode

Traits

AsConstExpr

Turn constant expression subclass into a ConstExpr enum

AsConstant

Turn constant subclass into a constant enum

AsGenericValue

Turn the value into a GenericValue, implemented for every value class

AsInstruction

Turn instructions into Instruction Container Enum

AsMetadata

Turn metadata subclass into a Metadata enum

AsOperand

Trait that turns a value into an operand

ConstExprTrait
GetDebugLoc

GetDebugLoc trait is implemented when there's debug location with respect to a value

GetDebugMetadata

Get debug metadata for any value

GetType

GetType trait provides get_type functions

GlobalValueTrait
InstructionDebugLoc

InstructionDebugLoc is implemented whenever it is an instruction

InstructionTrait
ValueOpcode