This addresses PR39845.
CGP (CodeGenPrepare) performs a variety of optimizations using a transactional approach, i.e. it makes changes to the instructions and rolls these changes back when it finds that they don't improve the code. In particular, the undoing involves the reversal or RAUWs, which involves the tracking of uses of a value along with replacing these uses with the original value.
Currently Metadata uses are not tracked, however. In particular, any references from dbg.value instructions to locals that were RAUWed by the initial optimization attempt are not restored, negatively affecting debug info.
This patch tries to at least partially solve this problem by performing a reverse RAUW to undo the substitution, but this is obviously only correct when the replacement value was not referenced by Metadata at the time of the first replacement.
The complete solution would require keeping track of existing MD-uses of the replacement value, something I didn't want to attempt without consultation of someone with more in-depth knowledge of Metadata handling. However, the patch should improve a lot of cases, as many values created by CGP's optimizations are newly created.
Any suggestions on how to handle this better are welcome.