From 8f966cedea594d9a91e585e88a80a42c04049e6c Mon Sep 17 00:00:00 2001 From: Shengchen Kan Date: Tue, 2 May 2023 13:49:15 +0800 Subject: [PATCH] [SelectionDAG] Use int64_t to store the integer power of llvm.powi https://llvm.org/docs/LangRef.html#llvm-powi-intrinsic The max length of the integer power of `llvm.powi` intrinsic is 32, and the value can be negative. If we use `int32_t` to store this value, `-Val` will underflow when it is `INT32_MIN` The issue was reported in D149033. --- .../SelectionDAG/SelectionDAGBuilder.cpp | 2 +- llvm/test/CodeGen/X86/powi-int32min.ll | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 llvm/test/CodeGen/X86/powi-int32min.ll diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 37976337a5fd82..9d6ad1144e292b 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -5451,7 +5451,7 @@ static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, // it's beneficial on the target, otherwise we end up lowering to a call to // __powidf2 (for example). if (ConstantSDNode *RHSC = dyn_cast(RHS)) { - int Val = RHSC->getSExtValue(); + int64_t Val = RHSC->getSExtValue(); // powi(x, 0) -> 1.0 if (Val == 0) diff --git a/llvm/test/CodeGen/X86/powi-int32min.ll b/llvm/test/CodeGen/X86/powi-int32min.ll new file mode 100644 index 00000000000000..3a1304c61c49ac --- /dev/null +++ b/llvm/test/CodeGen/X86/powi-int32min.ll @@ -0,0 +1,17 @@ +; RUN: llc -mtriple=x86_64-unknown-unknown < %s | FileCheck %s + +define float @test_powi(ptr %p) nounwind { +; CHECK-LABEL: test_powi: +; CHECK: # %bb.0: # %bb +; CHECK-NEXT: movss {{.*#+}} xmm1 = mem[0],zero,zero,zero +; CHECK-COUNT-31: mulss %xmm1, %xmm1 +; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero +; CHECK-NEXT: divss %xmm1, %xmm0 +; CHECK-NEXT: retq +bb: + %load = load float, ptr %p, align 4 + %call = call contract float @llvm.powi.f32.i32(float %load, i32 -2147483648) + ret float %call +} + +declare float @llvm.powi.f32.i32(float, i32)