[llvm-commits] proposed patch: shift when shift amount >= bit width
Dale Johannesen
dalej at apple.com
Wed Jun 13 10:52:59 PDT 2007
On Jun 12, 2007, at 1:19 PM, Lauro Ramos Venancio wrote:
> The attached patch unify the handle of shifts when the shift amount is
> greater or equal the bit width.
>
> Nowadays, LLVM evaluates shl(i32 X,32) to 0 in some places and to
> undef in other places.
> For compatibility purpose, this patch implements the gcc behavior.
This is not enough to make a compiler handle shifts by greater than the
word size consistently; in both gcc and llvm-gcc, for example, the
result of
int y=32;
x>>y
depends on optimization level. There is probably a target dependency
as well; for example, the 68020 used the low 6 bits of a variable
shift count,
while most other chips use only 5 bits. I don't think we can
ever promise consistent behavior for things like this, and I'm not sure
implementing a consistent behavior in one particular case is a good
idea.
I'd rather see users educated to understand that this construct is
not valid C
and they shouldn't be doing it (a warning, perhaps, in cases where
the compiler
can detect it).
> This fix the miscompilation of the file ffmpeg/libavformat/mpeg.c.
I'm not familiar with this file, but if it depends on the handling of
shifts by larger
than the word size, it is not correct C, so speaking of a
"miscompilation" is
a bit harsh.
> Proposed behavior:
> === shl(X,N) =====
> if (N<bitwidth(X)) result = X << N
> if (N>=bitwidth(X)) result = 0;
>
> === Lshr(X,N) =====
> if (N<bitwidth(X)) result = X >> N
> if (N>=bitwidth(X)) result = 0;
>
> === Ashr(X,N) =====
> if (N<bitwidth(X)) result = X >> N
> if (N>=bitwidth(X) && X<0) result = -1;
> if (N>=bitwidth(X) && X>=0) result = 0;
>
>
> Lauro
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shift.patch
Type: text/x-patch
Size: 14756 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20070613/9a6efa95/attachment.bin>
-------------- next part --------------
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list