Struggling to define a simple instruction

I’m trying to figure out how I can define and make LLVM target a simple instruction.

The instruction is very simple, it adds all of the elements of the vector register. Lets’ call it sum_reduce.

Defining a vector add was simple. All I had to do is to simply change the register type used by a regular add instruction and that was it. However I’m really struggling to define this new instruction.

Initially I thought that I would replace a chain of 3 adds with insert_element instructions, but that defeats the purpose of sum_reduce, since it would mean that I have to insert operators 4 times into a vector register and then add everything. This turns it into more instructions than the original chain of 3 adds.

I’ve also looked at X86 HADD instructions but also have difficulty understanding how to implement something similar to it in my target.

Hi Sam.

Thanks for the reply. This is not exactly what I was looking for.

Say I have a loop that looks like this:

sum = 0
for (i = 0; i < 4; i++)
sum += (a[i] + b[i])

I would like a compiler to do the following:

vector.load vector.r0, addressOfA
vector.load vector.r1, addressOfB

vector.add vector.r0, vector.r0, vector.r1
sumreduce gpr.r0, vector.r0

Another scenario is adding all the elements of the array (this is probably a better example)
sum = 0
for (i = i; i < 4; i++)
sum += a[i]

would be converted to
vector.load vector.r0, addressOfA
sumreduce gpr.r0, vector.r0

If you can assist with this, I would greatly appreciate it.