InsertElementInst and ExtractElementInst

Hello,

I am create a <3 x i32> vector in LLVM IR. Then I insert 3 instructions and later on I try to load one instruction from the vector. The insertion seems to work, however, when I try to load a specific instruction from a vector I seems that it does not work.

This is the part of my IR:

%"ins or1" = insertelement <3 x i32> undef, i32 %38, i32 0
%"ins and2" = insertelement <3 x i32> undef, i32 %41, i32 1
%"ins xor3" = insertelement <3 x i32> undef, i32 %43, i32 2
%extract4 = extractelement <3 x i32> undef, i32 %35
...
store i32 %extract4, i32* %46, align 4

The output of my program is different that the expected one, like the extractelement takes an undefined value.

The instructions I used are:
Instruction *Insert0 = InsertElementInst::Create(vector, Or_set, index0, "ins or");
..
Instruction *extract = ExtractElementInst::Create(vector, ch, "extract"); //where ch takes a value from 0 to 2
instr->replaceAllUsesWith(extract); //where I want to replace the instr instruction with the instruction from the vector.

I am new in the LLVM and I am not sure if I have done something wrong. Any suggestions are welcomed.

Regards,
Vasilis

Hi Vasilis,

%"ins or1" = insertelement <3 x i32> undef, i32 %38, i32 0
%"ins and2" = insertelement <3 x i32> undef, i32 %41, i32 1
%"ins xor3" = insertelement <3 x i32> undef, i32 %43, i32 2
%extract4 = extractelement <3 x i32> undef, i32 %35

You're always inserting and extracting with "undef". There's nothing
linking these operations at all. You actually want them to form a
chain (so %"ins and2" inserts into the %"ins or1" value, ..., %extrac4
extracts from "%ins xor3").

The output of my program is different that the expected one, like the
extractelement takes an undefined value.

It does.

The instructions I used are:
Instruction *Insert0 = InsertElementInst::Create(vector, Or_set, index0,
"ins or");
..
Instruction *extract = ExtractElementInst::Create(vector, ch, "extract");
//where ch takes a value from 0 to 2
instr->replaceAllUsesWith(extract); //where I want to replace the instr
instruction with the instruction from the vector.

OK, it looks like you're trying to use the IR in a C-like (non SSA)
manner. You might want to read around about SSA ("single static
assignment") form if you haven't already.

In this case, the variable "vector" isn't changed by any of these
operations, it starts as UNDEF (I assume) and remains UNDEF throughout
(the instructions don't assign to it). The vector-with-Or_set-inserted
is "Insert0", so that's what you should use in the next insert, and so
on.

Cheers.

Tim.

Hello Tim,

Yes finally it works, the vector was not initialized. Thanks for the help!

Regards,
Vasilis