Skip to content

Commit 9144553

Browse files
committed
Revert "[RISCV] Remove unneeded casts from int64_t to uint64_t in RISCVMatInt.cpp. NFC"
LLVM is built with C++17, where left shift of any negative value is still UB. Detected with UBSan on the buildbot. This reverts commit 0647d10.
1 parent b5f6cc9 commit 9144553

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,30 @@ static void generateInstSeqImpl(int64_t Val, const MCSubtargetInfo &STI,
115115
Val >>= ShiftAmount;
116116

117117
// If the remaining bits don't fit in 12 bits, we might be able to reduce
118-
// the shift amount in order to use LUI which will zero the lower 12 bits.
118+
// the // shift amount in order to use LUI which will zero the lower 12
119+
// bits.
119120
if (ShiftAmount > 12 && !isInt<12>(Val)) {
120-
if (isInt<32>(Val << 12)) {
121+
if (isInt<32>((uint64_t)Val << 12)) {
121122
// Reduce the shift amount and add zeros to the LSBs so it will match
122123
// LUI.
123124
ShiftAmount -= 12;
124-
Val = Val << 12;
125-
} else if (isUInt<32>(Val << 12) &&
125+
Val = (uint64_t)Val << 12;
126+
} else if (isUInt<32>((uint64_t)Val << 12) &&
126127
STI.hasFeature(RISCV::FeatureStdExtZba)) {
127128
// Reduce the shift amount and add zeros to the LSBs so it will match
128129
// LUI, then shift left with SLLI.UW to clear the upper 32 set bits.
129130
ShiftAmount -= 12;
130-
Val = SignExtend64<32>(Val << 12);
131+
Val = ((uint64_t)Val << 12) | (0xffffffffull << 32);
131132
Unsigned = true;
132133
}
133134
}
134135

135136
// Try to use SLLI_UW for Val when it is uint32 but not int32.
136-
if (isUInt<32>(Val) && !isInt<32>(Val) &&
137+
if (isUInt<32>((uint64_t)Val) && !isInt<32>((uint64_t)Val) &&
137138
STI.hasFeature(RISCV::FeatureStdExtZba)) {
138-
// Use LUI+ADDI(W) or LUI to compose, then clear the upper 32 bits with
139+
// Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with
139140
// SLLI_UW.
140-
Val = SignExtend64<32>(Val);
141+
Val = ((uint64_t)Val) | (0xffffffffull << 32);
141142
Unsigned = true;
142143
}
143144
}

0 commit comments

Comments
 (0)