How to add and test new pseudo instruction


I want to add new register to Sparc back-end for educational purposes.
I also want to add new pseudo instruction that would add its argument
to this register. And the most complicated (to my mind) - I want to be
able to test (use) it from C source: something like int x = 5;
__domyinstruction(x); /* adds five */ int y = __getmyregister();

I think I can add register (by simply add a line to file). But adding instruction is a bit more
complicated as I have to add a SelectionDAG pattern in As far as I understand I have to add:
def MYINSTRUCTION: Pseudo<(outs IntRegs:$dst), (ins IntRegs:$src),
            "; some asm code", [SelectionDAG pattern list]>;
So I don't know how to write this pattern and I'm not sure I need it.
All I want is to add some kind of built-in function to Clang that
would somehow produce MYINSTRUCTION. And currently I don't have any
idea of how to implement __getmyregister(). Any tips?

Thank you, yours,
Vladimir M.

Maybe I don’t follow exactly what you’re trying to do but it seems like something you can solve with an intrinsic that you can put in an SDAG pattern and create a builtin for clang to emit this intrinsic. There are many examples of this in include/llvm/IR/ and the respective target’s instruction info .td files.

For a completely random example on PowerPC, you can see the int_ppc_vsx_xvresp/__builtin_vsx_xvresp combo.