Fix incorrect cast in gtFoldExprConst
authorMike Danes <onemihaid@hotmail.com>
Sat, 4 Feb 2017 10:14:54 +0000 (12:14 +0200)
committerMike Danes <onemihaid@hotmail.com>
Fri, 24 Feb 2017 19:56:41 +0000 (21:56 +0200)
commit8390c7212facecf4cad72a8ec7c74ff54cdf8d8d
tree0cefb887c2f54869912028e44f8ef016beb2877a
parent571aa6a9a0d6ab1c7529ceca4a969cda7ae0145a
Fix incorrect cast in gtFoldExprConst

The fact that an operation is unsigned affects the operation itself but that doesn't mean that the result of the operation is also unsigned. Constants are stored as ssize_t and the node type is TYP_INT so the result has to be sign extended, not zero extended.

Otherwise code that uses the return of IconValue() without first narrowing it to int will behave incorrectly. Such code does exists, even gtFoldExprConst does this when folding shift operations:

uint a = uint.MaxValue;
uint b = 0;
int r = (int)checked(a + b);
Console.WriteLine((r >> 2).ToString("X"));

The above code prints 3FFFFFFF instead of the expected FFFFFFFF.

This also makes gtFoldExprConst consistent with ValueNumStore::EvalFuncForConstantArgs which evaluates TYP_INT nodes as int and then casts to ssize_t.

Commit migrated from https://github.com/dotnet/coreclr/commit/176e2c7964846bf5dea22557f0828475deac05e1
src/coreclr/src/jit/gentree.cpp