Skip to content

Commit 910a73d

Browse files
topperctru
authored andcommitted
[RISCV] Fix a UBSAN failure for passing INT64_MIN to std::abs.
clang recently started checking for INT64_MIN being passed to 64-bit std::abs. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D158304 (cherry picked from commit 078eb4b)
1 parent 9d0f5bf commit 910a73d

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,12 +3113,13 @@ static SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
31133113
bool Negate = false;
31143114
int64_t SplatStepVal = StepNumerator;
31153115
unsigned StepOpcode = ISD::MUL;
3116-
if (StepNumerator != 1) {
3117-
if (isPowerOf2_64(std::abs(StepNumerator))) {
3118-
Negate = StepNumerator < 0;
3119-
StepOpcode = ISD::SHL;
3120-
SplatStepVal = Log2_64(std::abs(StepNumerator));
3121-
}
3116+
// Exclude INT64_MIN to avoid passing it to std::abs. We won't optimize it
3117+
// anyway as the shift of 63 won't fit in uimm5.
3118+
if (StepNumerator != 1 && StepNumerator != INT64_MIN &&
3119+
isPowerOf2_64(std::abs(StepNumerator))) {
3120+
Negate = StepNumerator < 0;
3121+
StepOpcode = ISD::SHL;
3122+
SplatStepVal = Log2_64(std::abs(StepNumerator));
31223123
}
31233124

31243125
// Only emit VIDs with suitably-small steps/addends. We use imm5 is a

0 commit comments

Comments
 (0)