An individual scalar element can be extracted from a vector. LLVM provides the extractelement
instruction for the same. The first operand of an extractelement
instruction is a value of vector type. The second operand is an index indicating the position from which to extract the element.
The extractelement
instruction looks like the following:
result = extractelement <4 x i32> %vec, i32 %idx
This can be further understood by keeping the following in mind:
vec
is a vectoridx
is the index at which the data to be extracted liesresult
is of scalar type, which isi32
here
Let's take an example where we want to add all the elements of a given vector and return an integer.
Consider the following code:
#include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" #include <vector> using namespace llvm; static LLVMContext &Context = getGlobalContext(); static Module *ModuleOb = new Module("my...