Workaround MSVC 32-bit miscompile of getCondCodeAction.

Use 32-bit types for the array instead of 64. This should
generally be better anyway.

In optimized + assert builds, I saw a failure when a
cond code / type combination that is never set was loading
a non-zero value and hitting the != Promote assert.

It turns out when loading the 64-bit value to do the shift,
the assembly loads the 2 32-bit halves from non-consecutive
addresses. The address the second half of the loaded uint64_t
doesn't include the offset of the array in the struct. Instead
of being offset + 4, it's just + 4.

I'm not entirely sure why this wasn't observed before.
setCondCodeAction isn't heavily used by the in-tree targets,
and not with the higher valued vector SimpleValueTypes. Only
PPC is using one of the > 32 valued types, and that is probably
never used by anyone on a 32-bit MSVC compiled host.

I ran into this when upgrading LLVM versions, so I guess the
value loaded from the nonsense address happened to work out
before.

No test since I'm not really sure if / how it can be reproduced
with the current in tree targets, and it's not supposed to change
anything.

llvm-svn: 193650
This commit is contained in:
Matt Arsenault 2013-10-29 20:59:29 +00:00
parent 9ab670fb54
commit a1ca46d003
1 changed files with 13 additions and 15 deletions

View File

@ -520,13 +520,12 @@ public:
LegalizeAction LegalizeAction
getCondCodeAction(ISD::CondCode CC, MVT VT) const { getCondCodeAction(ISD::CondCode CC, MVT VT) const {
assert((unsigned)CC < array_lengthof(CondCodeActions) && assert((unsigned)CC < array_lengthof(CondCodeActions) &&
(unsigned)VT.SimpleTy < sizeof(CondCodeActions[0])*4 && ((unsigned)VT.SimpleTy >> 4) < array_lengthof(CondCodeActions[0]) &&
"Table isn't big enough!"); "Table isn't big enough!");
/// The lower 5 bits of the SimpleTy index into Nth 2bit set from the 64bit // See setCondCodeAction for how this is encoded.
/// value and the upper 27 bits index into the second dimension of the uint32_t Shift = 2 * (VT.SimpleTy & 0xF);
/// array to select what 64bit value to use. uint32_t Value = CondCodeActions[CC][VT.SimpleTy >> 4];
LegalizeAction Action = (LegalizeAction) LegalizeAction Action = (LegalizeAction) ((Value >> Shift) & 0x3);
((CondCodeActions[CC][VT.SimpleTy >> 5] >> (2*(VT.SimpleTy & 0x1F))) & 3);
assert(Action != Promote && "Can't promote condition code!"); assert(Action != Promote && "Can't promote condition code!");
return Action; return Action;
} }
@ -1020,13 +1019,12 @@ protected:
assert(VT < MVT::LAST_VALUETYPE && assert(VT < MVT::LAST_VALUETYPE &&
(unsigned)CC < array_lengthof(CondCodeActions) && (unsigned)CC < array_lengthof(CondCodeActions) &&
"Table isn't big enough!"); "Table isn't big enough!");
/// The lower 5 bits of the SimpleTy index into Nth 2bit set from the 64bit /// The lower 5 bits of the SimpleTy index into Nth 2bit set from the 32-bit
/// value and the upper 27 bits index into the second dimension of the /// value and the upper 27 bits index into the second dimension of the array
/// array to select what 64bit value to use. /// to select what 32-bit value to use.
CondCodeActions[(unsigned)CC][VT.SimpleTy >> 5] uint32_t Shift = 2 * (VT.SimpleTy & 0xF);
&= ~(uint64_t(3UL) << (VT.SimpleTy & 0x1F)*2); CondCodeActions[CC][VT.SimpleTy >> 4] &= ~((uint32_t)0x3 << Shift);
CondCodeActions[(unsigned)CC][VT.SimpleTy >> 5] CondCodeActions[CC][VT.SimpleTy >> 4] |= (uint32_t)Action << Shift;
|= (uint64_t)Action << (VT.SimpleTy & 0x1F)*2;
} }
/// If Opc/OrigVT is specified as being promoted, the promotion code defaults /// If Opc/OrigVT is specified as being promoted, the promotion code defaults
@ -1448,8 +1446,8 @@ private:
/// ///
/// Because each CC action takes up 2 bits, we need to have the array size be /// Because each CC action takes up 2 bits, we need to have the array size be
/// large enough to fit all of the value types. This can be done by dividing /// large enough to fit all of the value types. This can be done by dividing
/// the MVT::LAST_VALUETYPE by 32 and adding one. /// the MVT::LAST_VALUETYPE by 16 and adding one.
uint64_t CondCodeActions[ISD::SETCC_INVALID][(MVT::LAST_VALUETYPE / 32) + 1]; uint32_t CondCodeActions[ISD::SETCC_INVALID][(MVT::LAST_VALUETYPE + 15) / 16];
ValueTypeActionImpl ValueTypeActions; ValueTypeActionImpl ValueTypeActions;