UB only licenses the program to do anything if it is reachable. Unreachable, unexecuted, cant-be-executed UB simply can't change a program's behavior. I'm not sure why you think it can.
Do you think this program can "do anything" or "re-write any part of itself"?
unsigned int x = <input from user>;
unsigned int y = <input from user>;
printf("%u\n", x / y);
The compiler must output valid code for that program, and
execution (not compilation) can only "do anything"
if the user enters values that invoke UB (like "y == 0" for example). For all valid inputs this program must execute correctly and can not "do anything", even at runtime, do you agree?
What about this one?
unsigned int x = <input from user>;
unsigned int y = <input from user>;
if (y == 0) {
printf("%u\n", x / y);
} else {
printf("%u\n", x * y);
}
Same idea: this program can only "do anything" if the user enters "y == 0". The compiler can see that "x / y" would invoke UB, and remove the entire "if (y == 0) { ... }" branch (because the compiler can assume it is never reached, otherwise it would invoke UB), but the compiler can not do anything to change the behavior of the "else" branch - entering any non-zero "y" value must produce correct output, so the program can't just be compiled to "do anything".
Only the paths related to the UB can be optimized/modified/rewritten by the compiler based on that UB, and if those optimizations or modifications affect the proper execution of paths unrelated to the UB (i.e. paths where the UB can not be reached from), then the optimization or modification in question is a compiler bug.