Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Always sign-extend from int32 in gtFoldExprConst
GT_CNS_INT of TYP_INT on 64 hosts has an implicit contract:
the value returned by IconValue() must "fit" into 32 bit
signed integer, i. e. "static_cast<int>(IconValue()) == IconValue()".
gtFoldExprConst was failing to uphold this contract when the target
was 32 bit and the host was 64 bit.

Fix this by always truncating before calling SetIconValue().
  • Loading branch information
SingleAccretion committed Jun 25, 2021
commit 7e030a88f7671b1d7893464b415c09e6c534be1b
12 changes: 4 additions & 8 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14864,19 +14864,15 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
JITDUMP("\nFolding operator with constant nodes into a constant:\n");
DISPTREE(tree);

#ifdef TARGET_64BIT
// Some operations are performed as 64 bit instead of 32 bit so the upper 32 bits
// need to be discarded. Since constant values are stored as ssize_t and the node
// has TYP_INT the result needs to be sign extended rather than zero extended.
i1 = INT32(i1);
#endif // TARGET_64BIT

// Also all conditional folding jumps here since the node hanging from
// GT_JTRUE has to be a GT_CNS_INT - value 0 or 1.

tree->ChangeOperConst(GT_CNS_INT);
tree->ChangeType(TYP_INT);
tree->AsIntCon()->SetIconValue(i1);
// Some operations are performed as 64 bit instead of 32 bit so the upper 32 bits
// need to be discarded. Since constant values are stored as ssize_t and the node
// has TYP_INT the result needs to be sign extended rather than zero extended.
tree->AsIntCon()->SetIconValue(static_cast<int>(i1));
tree->AsIntCon()->gtFieldSeq = fieldSeq;
if (vnStore != nullptr)
{
Expand Down