<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I have originally posted this on StackOverflow (<a href="https://stackoverflow.com/questions/58084302/reading-and-producing-an-extra-return-value-in-flags-in-llvm" class="">https://stackoverflow.com/questions/58084302/reading-and-producing-an-extra-return-value-in-flags-in-llvm</a>), but there were no replies, so I am trying my luck here.<div class=""><br class=""></div><div class=""><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; line-height: inherit; font-size: 15px; vertical-align: baseline; box-sizing: inherit; clear: both; caret-color: rgb(36, 39, 41); color: rgb(36, 39, 41);" class="">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 <code style="margin: 0px; padding: 1px 5px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-style: inherit; font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: pre-wrap;" class="">stc</code> 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.</p><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; line-height: inherit; font-size: 15px; vertical-align: baseline; box-sizing: inherit; clear: both; caret-color: rgb(36, 39, 41); color: rgb(36, 39, 41);" class="">However, how would one go about implementing something like this within LLVM? I suppose that one can use <code style="margin: 0px; padding: 1px 5px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-style: inherit; font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: pre-wrap;" class="">MachineBasicBlock</code> 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? </p><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-stretch: inherit; line-height: inherit; font-size: 15px; vertical-align: baseline; box-sizing: inherit; clear: both; caret-color: rgb(36, 39, 41); color: rgb(36, 39, 41);" class="">Note that my motivation is purely curiosity. I am not actually working on a compiler — I just want to know how these things work.</p></div><div class="">Thank you, </div><div class=""><br class=""></div><div class="">Taras</div></body></html>