pub struct BasicBlock<'ctx> { /* private fields */ }
Expand description

A BasicBlock is a container of instructions.

BasicBlocks are values because they can be referenced by instructions (ie branching and switches).

A well formed BasicBlock is a list of non terminating instructions followed by a single terminating instruction. BasicBlocks are allowed to be malformed prior to running validation because it may be useful when constructing or modifying a program.

Implementations§

Obtains the FunctionValue that this BasicBlock belongs to, if any.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);

let basic_block = context.append_basic_block(function, "entry");

assert_eq!(basic_block.get_parent().unwrap(), function);

basic_block.remove_from_function();

assert!(basic_block.get_parent().is_none());

Gets the BasicBlock preceeding the current one, in its own scope, if any.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function1 = module.add_function("do_nothing", fn_type, None);

let basic_block1 = context.append_basic_block(function1, "entry");

assert!(basic_block1.get_previous_basic_block().is_none());

let function2 = module.add_function("do_nothing", fn_type, None);

let basic_block2 = context.append_basic_block(function2, "entry");
let basic_block3 = context.append_basic_block(function2, "next");

assert!(basic_block2.get_previous_basic_block().is_none());
assert_eq!(basic_block3.get_previous_basic_block().unwrap(), basic_block2);

Gets the BasicBlock succeeding the current one, in its own scope, if any.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function1 = module.add_function("do_nothing", fn_type, None);

let basic_block1 = context.append_basic_block(function1, "entry");

assert!(basic_block1.get_next_basic_block().is_none());

let function2 = module.add_function("do_nothing", fn_type, None);

let basic_block2 = context.append_basic_block(function2, "entry");
let basic_block3 = context.append_basic_block(function2, "next");

assert!(basic_block1.get_next_basic_block().is_none());
assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block3);
assert!(basic_block3.get_next_basic_block().is_none());

Prepends one BasicBlock before another. It returns Err(()) when either BasicBlock has no parent, as LLVM assumes they both have parents.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);

let basic_block1 = context.append_basic_block(function, "entry");
let basic_block2 = context.append_basic_block(function, "next");

basic_block2.move_before(basic_block1);

assert!(basic_block1.get_next_basic_block().is_none());
assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block1);

Appends one BasicBlock after another. It returns Err(()) when either BasicBlock has no parent, as LLVM assumes they both have parents.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);

let basic_block1 = context.append_basic_block(function, "entry");
let basic_block2 = context.append_basic_block(function, "next");

basic_block1.move_after(basic_block2);

assert!(basic_block1.get_next_basic_block().is_none());
assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block1);

Obtains the first InstructionValue in this BasicBlock, if any.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;
use inkwell::values::InstructionOpcode;

let context = Context::create();
let builder = context.create_builder();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);
builder.build_return(None);

assert_eq!(basic_block.get_first_instruction().unwrap().get_opcode(), InstructionOpcode::Return);

Obtains the last InstructionValue in this BasicBlock, if any. A BasicBlock must have a last instruction to be valid.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;
use inkwell::values::InstructionOpcode;

let context = Context::create();
let builder = context.create_builder();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);
builder.build_return(None);

assert_eq!(basic_block.get_last_instruction().unwrap().get_opcode(), InstructionOpcode::Return);

Obtains the terminating InstructionValue in this BasicBlock, if any. A BasicBlock must have a terminating instruction to be valid.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;
use inkwell::values::InstructionOpcode;

let context = Context::create();
let builder = context.create_builder();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);
builder.build_return(None);

assert_eq!(basic_block.get_terminator().unwrap().get_opcode(), InstructionOpcode::Return);

Removes this BasicBlock from its parent FunctionValue. It returns Err(()) when it has no parent to remove from.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

assert_eq!(basic_block.get_parent().unwrap(), function);

basic_block.remove_from_function();

assert!(basic_block.get_parent().is_none());

Removes this BasicBlock completely from memory. This is unsafe because you could easily have other references to the same BasicBlock. It returns Err(()) when it has no parent to delete from, as LLVM assumes it has a parent.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

unsafe {
    basic_block.delete();
}
assert!(function.get_basic_blocks().is_empty());

Obtains the ContextRef this BasicBlock belongs to.

Example
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::builder::Builder;

let context = Context::create();
let module = context.create_module("my_module");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let function = module.add_function("do_nothing", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

assert_eq!(context, basic_block.get_context());

Gets the name of a BasicBlock.

Example
use inkwell::context::Context;

let context = Context::create();
let builder = context.create_builder();
let module = context.create_module("my_mod");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let fn_val = module.add_function("my_fn", fn_type, None);
let bb = context.append_basic_block(fn_val, "entry");

assert_eq!(bb.get_name().to_str(), Ok("entry"));

Set name of the BasicBlock.

Replaces all uses of this basic block with another.

Example
use inkwell::context::Context;

let context = Context::create();
let builder = context.create_builder();
let module = context.create_module("my_mod");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let fn_val = module.add_function("my_fn", fn_type, None);
let entry = context.append_basic_block(fn_val, "entry");
let bb1 = context.append_basic_block(fn_val, "bb1");
let bb2 = context.append_basic_block(fn_val, "bb2");
builder.position_at_end(entry);
let branch_inst = builder.build_unconditional_branch(bb1);

bb1.replace_all_uses_with(&bb2);

assert_eq!(branch_inst.get_operand(0).unwrap().right().unwrap(), bb2);

Gets the first use of this BasicBlock if any.

The following example,

use inkwell::AddressSpace;
use inkwell::context::Context;
use inkwell::values::BasicValue;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let fn_val = module.add_function("my_fn", fn_type, None);
let entry = context.append_basic_block(fn_val, "entry");
let bb1 = context.append_basic_block(fn_val, "bb1");
let bb2 = context.append_basic_block(fn_val, "bb2");
builder.position_at_end(entry);
let branch_inst = builder.build_unconditional_branch(bb1);

assert!(bb2.get_first_use().is_none());
assert!(bb1.get_first_use().is_some());

Gets the address of this BasicBlock if possible. Returns None if self is the entry block to a function.

Safety

The returned PointerValue may only be used for call and indirect_branch instructions

Example
use inkwell::context::Context;
let context = Context::create();
let module = context.create_module("my_mod");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let fn_val = module.add_function("my_fn", fn_type, None);
let entry_bb = context.append_basic_block(fn_val, "entry");
let next_bb = context.append_basic_block(fn_val, "next");

assert!(unsafe { entry_bb.get_address() }.is_none());
assert!(unsafe { next_bb.get_address() }.is_some());

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.