Counter example where this miscompiles:
define i8 @tinky() {
%sext = sext i1 1 to i16 %hibit = lshr i16 %sext, 15 %tr = trunc i16 %hibit to i8 ret i8 %tr
}
define i8 @winky() {
%sext = sext i1 1 to i8 ret i8 %sext
}
which gets folded to:
define i8 @tinky() {
ret i8 1
}
define i8 @winky() {
ret i8 -1
}
Alive confirms: http://rise4fun.com/Alive/plX
This needs to be checked in every case, so maybe it should be factored out.