<div dir="ltr">Your hardware-level instruction set doesn't need to be exactly represented in the LLVM IR. For example, when lowering C to LLVM IR, Clang converts `~x` to `xor %x, -1`, this is then pattern matched during instruction selection to turn it into a hardware-level `NOT` opcode. You can do the same for the toffoli gate: generate equivalent logical operations in the instructions that LLVM supports, and then pattern match Toffoli gates during instruction selection.<div>
<br></div><div>From looking at Wikipedia, I see that Toffoli gate is just `<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:19.1875px">c XOR (a AND b)`. By expanding it to this, LLVM will automatically be able to apply its full arsenal of optimizations to optimize the program. For example, it will automatically simplify `Toffoli(a,b,c) XOR Toffoli(b,a,c)` to 0. If you add a new toffoli intrinsic or instruction, then you will have to teach LLVM to do these kinds of optimizations (and there are *lots* of them).</span><br>
<div><br></div><div style>-- Sean Silva</div></div></div>