I have originally posted this on StackOverflow (https://stackoverflow.com/questions/58084302/reading-and-producing-an-extra-return-value-in-flags-in-llvm), but there were no replies, so I am trying my luck here.
I was watching Herb Sutter’s talk on CppCon 2019, and one idea is to use control flag register bits to mark that an exception has been produced during a function call. E.g. (assuming x86 target) a function that raises an error could emit a stc
instruction just before returning, and the caller could execute a conditional jump immediately after it returns to check if an error should be handled. It’s smart, its efficient (much better than returning the condition flag in a separate register), and it’s simple.
However, how would one go about implementing something like this within LLVM? I suppose that one can use MachineBasicBlock
to emit flag set intructions, but does one have a guarantee that LLVM won’t insert an instruction afterwards that might change the flag value? Also, how would one test for the flag value and jump to a LLVM IR block?
Note that my motivation is purely curiosity. I am not actually working on a compiler — I just want to know how these things work.
Thank you,
Taras