Skip to content

Commit 7cc9a0b

Browse files
Fix undefined behavior in matchCombineShlOfAnd combine
1 parent 0614651 commit 7cc9a0b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,11 +2628,19 @@ bool CombinerHelper::matchCombineShlOfAnd(MachineInstr &MI,
26282628
m_GShl(m_OneNonDBGUse(m_GAnd(m_Reg(Reg), m_ICst(AndImm))),
26292629
m_ICst(ShiftImm))))
26302630
return false;
2631-
// Check if AndImm has bits set only in positions that will be shifted out by
2632-
// ShiftImm. If any significant bits remain after the shift, the AND operation
2631+
2632+
// Shift is out of range, handled by different combines.
2633+
if (ShiftImm < 0 || ShiftImm >= 64)
2634+
return false;
2635+
2636+
uint64_t AndVal = static_cast<uint64_t>(AndImm);
2637+
uint64_t ShAmount = static_cast<uint64_t>(ShiftImm);
2638+
2639+
// Check if AndVal has bits set only in positions that will be shifted out by
2640+
// ShAmount. If any significant bits remain after the shift, the AND operation
26332641
// cannot be removed.
26342642
uint64_t Mask = ~0ULL >> (64 - Size);
2635-
return !((~AndImm << ShiftImm) & Mask);
2643+
return !((~AndVal << ShAmount) & Mask);
26362644
}
26372645

26382646
void CombinerHelper::applyCombineShlOfAnd(MachineInstr &MI,

0 commit comments

Comments
 (0)