From 4fa0394a1aca85a76aea518534c9f70e94e6998e Mon Sep 17 00:00:00 2001 From: Andrew Hughes Date: Mon, 13 Jul 2026 21:05:29 +0100 Subject: [PATCH] Add 25u backports of JDK-8347901 & JDK-8378713 C2 performance fixes Resolves: RHEL-210993 --- java-25-openjdk.spec | 13 +- jdk8347901-c2_unused_leaf_removal.patch | 772 ++++++++++++++++++ ...8713-c2_missing_pow_constant_folding.patch | 626 ++++++++++++++ 3 files changed, 1409 insertions(+), 2 deletions(-) create mode 100644 jdk8347901-c2_unused_leaf_removal.patch create mode 100644 jdk8378713-c2_missing_pow_constant_folding.patch diff --git a/java-25-openjdk.spec b/java-25-openjdk.spec index a112e8e..5c39c36 100644 --- a/java-25-openjdk.spec +++ b/java-25-openjdk.spec @@ -9,7 +9,7 @@ %global updatever 3 %global patchver 0 %global buildver 9 -%global portablerelease 2 +%global portablerelease 3 %global rpmrelease 1 # Define IcedTea version used for SystemTap tapsets and desktop file @@ -1397,6 +1397,10 @@ Patch1001: fips-%{featurever}u-%{fipsver}.patch # JDK-8375294: (fs) Files.copy can fail with EOPNOTSUPP when copy_file_range not supported Patch2001: jdk8375294-handle-EOPNOTSUPP-in-copying.patch +# JDK-8347901: C2 should remove unused leaf / pure runtime calls +Patch2002: jdk8347901-c2_unused_leaf_removal.patch +# JDK-83787313: C2: performance regression due to missing constant folding for Math.pow() +Patch2003: jdk8378713-c2_missing_pow_constant_folding.patch ############################################# # @@ -1921,6 +1925,9 @@ pushd %{top_level_dir_name} %patch -P1001 -p1 # Add EOPNOTSUPP patch %patch -P2001 -p1 +# Add C2 patches +%patch -P2002 -p1 +%patch -P2003 -p1 popd # openjdk # Patch NSS adapter @@ -2595,7 +2602,7 @@ exit 0 %endif %changelog -* Tue Jul 14 2026 Andrew Hughes - 1:25.0.3.0.9-2.1 +* Tue Jul 14 2026 Andrew Hughes - 1:25.0.3.0.9-3.1 - Cleanup tagging and gating scripts to appease shellcheck: - * scripts/builds/build_vanilla.sh: Use an array to handle the varying arguments to rhpkg. - * scripts/builds/check_signatures.sh: Quote variable usage. @@ -2606,10 +2613,12 @@ exit 0 - * scripts/builds/waive_usual_tier0.sh: Remove redundant 'test "x"' usage. - Remove macro references in comments where possible (%dnl not compatible enough yet) - Move version information and core NVR definitions back towards the top of the file +- Add 25u backports of JDK-8347901 & JDK-8378713 C2 performance fixes - Related: RHEL-155327 - Related: RHEL-155339 - Resolves: RHEL-150977 - Resolves: RHEL-210972 +- Resolves: RHEL-210993 * Sat Apr 18 2026 Andrew Hughes - 1:25.0.3.0.9-1 - Update to jdk-25.0.3+9 (GA) diff --git a/jdk8347901-c2_unused_leaf_removal.patch b/jdk8347901-c2_unused_leaf_removal.patch new file mode 100644 index 0000000..bae136b --- /dev/null +++ b/jdk8347901-c2_unused_leaf_removal.patch @@ -0,0 +1,772 @@ +From 09853461a6896ab1f15469c753c21ca212e7e650 Mon Sep 17 00:00:00 2001 +From: duke +Date: Tue, 7 Apr 2026 15:14:53 +0000 +Subject: [PATCH 1/2] Backport ed70910b0f3e1b19d915ec13ac3434407d01bc5d + +--- + src/hotspot/share/opto/callnode.cpp | 53 +++++++- + src/hotspot/share/opto/callnode.hpp | 29 ++++- + src/hotspot/share/opto/classes.hpp | 2 + + src/hotspot/share/opto/compile.cpp | 19 +++ + src/hotspot/share/opto/divnode.cpp | 158 ++++++++++-------------- + src/hotspot/share/opto/divnode.hpp | 35 +++--- + src/hotspot/share/opto/graphKit.cpp | 29 +++-- + src/hotspot/share/opto/graphKit.hpp | 1 + + src/hotspot/share/opto/library_call.cpp | 2 +- + src/hotspot/share/opto/macro.cpp | 15 +-- + src/hotspot/share/opto/multnode.cpp | 13 ++ + src/hotspot/share/opto/multnode.hpp | 46 +++++++ + src/hotspot/share/opto/node.cpp | 12 +- + src/hotspot/share/opto/node.hpp | 5 +- + src/hotspot/share/opto/parse2.cpp | 8 +- + 15 files changed, 282 insertions(+), 145 deletions(-) + +diff --git a/src/hotspot/share/opto/callnode.cpp b/src/hotspot/share/opto/callnode.cpp +index 6f13ce0f809..fc4b0c35dff 100644 +--- a/src/hotspot/share/opto/callnode.cpp ++++ b/src/hotspot/share/opto/callnode.cpp +@@ -935,7 +935,7 @@ Node *CallNode::result_cast() { + } + + +-void CallNode::extract_projections(CallProjections* projs, bool separate_io_proj, bool do_asserts) { ++void CallNode::extract_projections(CallProjections* projs, bool separate_io_proj, bool do_asserts) const { + projs->fallthrough_proj = nullptr; + projs->fallthrough_catchproj = nullptr; + projs->fallthrough_ioproj = nullptr; +@@ -1319,6 +1319,57 @@ void CallLeafVectorNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_ + + + //============================================================================= ++bool CallLeafPureNode::is_unused() const { ++ return proj_out_or_null(TypeFunc::Parms) == nullptr; ++} ++ ++bool CallLeafPureNode::is_dead() const { ++ return proj_out_or_null(TypeFunc::Control) == nullptr; ++} ++ ++/* We make a tuple of the global input state + TOP for the output values. ++ * We use this to delete a pure function that is not used: by replacing the call with ++ * such a tuple, we let output Proj's idealization pick the corresponding input of the ++ * pure call, so jumping over it, and effectively, removing the call from the graph. ++ * This avoids doing the graph surgery manually, but leaves that to IGVN ++ * that is specialized for doing that right. We need also tuple components for output ++ * values of the function to respect the return arity, and in case there is a projection ++ * that would pick an output (which shouldn't happen at the moment). ++ */ ++TupleNode* CallLeafPureNode::make_tuple_of_input_state_and_top_return_values(const Compile* C) const { ++ // Transparently propagate input state but parameters ++ TupleNode* tuple = TupleNode::make( ++ tf()->range(), ++ in(TypeFunc::Control), ++ in(TypeFunc::I_O), ++ in(TypeFunc::Memory), ++ in(TypeFunc::FramePtr), ++ in(TypeFunc::ReturnAdr)); ++ ++ // And add TOPs for the return values ++ for (uint i = TypeFunc::Parms; i < tf()->range()->cnt(); i++) { ++ tuple->set_req(i, C->top()); ++ } ++ ++ return tuple; ++} ++ ++Node* CallLeafPureNode::Ideal(PhaseGVN* phase, bool can_reshape) { ++ if (is_dead()) { ++ return nullptr; ++ } ++ ++ // We need to wait until IGVN because during parsing, usages might still be missing ++ // and we would remove the call immediately. ++ if (can_reshape && is_unused()) { ++ // The result is not used. We remove the call by replacing it with a tuple, that ++ // is later disintegrated by the projections. ++ return make_tuple_of_input_state_and_top_return_values(phase->C); ++ } ++ ++ return CallRuntimeNode::Ideal(phase, can_reshape); ++} ++ + #ifndef PRODUCT + void CallLeafNode::dump_spec(outputStream *st) const { + st->print("# "); +diff --git a/src/hotspot/share/opto/callnode.hpp b/src/hotspot/share/opto/callnode.hpp +index 213fbda4e89..2462a88143f 100644 +--- a/src/hotspot/share/opto/callnode.hpp ++++ b/src/hotspot/share/opto/callnode.hpp +@@ -752,7 +752,7 @@ class CallNode : public SafePointNode { + // Collect all the interesting edges from a call for use in + // replacing the call by something else. Used by macro expansion + // and the late inlining support. +- void extract_projections(CallProjections* projs, bool separate_io_proj, bool do_asserts = true); ++ void extract_projections(CallProjections* projs, bool separate_io_proj, bool do_asserts = true) const; + + virtual uint match_edge(uint idx) const; + +@@ -928,6 +928,33 @@ class CallLeafNode : public CallRuntimeNode { + #endif + }; + ++/* A pure function call, they are assumed not to be safepoints, not to read or write memory, ++ * have no exception... They just take parameters, return a value without side effect. It is ++ * always correct to create some, or remove them, if the result is not used. ++ * ++ * They still have control input to allow easy lowering into other kind of calls that require ++ * a control, but this is more a technical than a moral constraint. ++ * ++ * Pure calls must have only control and data input and output: I/O, Memory and so on must be top. ++ * Nevertheless, pure calls can typically be expensive math operations so care must be taken ++ * when letting the node float. ++ */ ++class CallLeafPureNode : public CallLeafNode { ++protected: ++ bool is_unused() const; ++ bool is_dead() const; ++ TupleNode* make_tuple_of_input_state_and_top_return_values(const Compile* C) const; ++ ++public: ++ CallLeafPureNode(const TypeFunc* tf, address addr, const char* name, ++ const TypePtr* adr_type) ++ : CallLeafNode(tf, addr, name, adr_type) { ++ init_class_id(Class_CallLeafPure); ++ } ++ int Opcode() const override; ++ Node* Ideal(PhaseGVN* phase, bool can_reshape) override; ++}; ++ + //------------------------------CallLeafNoFPNode------------------------------- + // CallLeafNode, not using floating point or using it in the same manner as + // the generated code +diff --git a/src/hotspot/share/opto/classes.hpp b/src/hotspot/share/opto/classes.hpp +index bc259eed2d1..587d5fad8f2 100644 +--- a/src/hotspot/share/opto/classes.hpp ++++ b/src/hotspot/share/opto/classes.hpp +@@ -61,6 +61,7 @@ macro(CallDynamicJava) + macro(CallJava) + macro(CallLeaf) + macro(CallLeafNoFP) ++macro(CallLeafPure) + macro(CallLeafVector) + macro(CallRuntime) + macro(CallStaticJava) +@@ -372,6 +373,7 @@ macro(SubI) + macro(SubL) + macro(TailCall) + macro(TailJump) ++macro(Tuple) + macro(MacroLogicV) + macro(ThreadLocal) + macro(Unlock) +diff --git a/src/hotspot/share/opto/compile.cpp b/src/hotspot/share/opto/compile.cpp +index 2b956dcb5d8..9f7f48b9a34 100644 +--- a/src/hotspot/share/opto/compile.cpp ++++ b/src/hotspot/share/opto/compile.cpp +@@ -3298,6 +3298,25 @@ void Compile::final_graph_reshaping_main_switch(Node* n, Final_Reshape_Counts& f + case Op_Opaque1: // Remove Opaque Nodes before matching + n->subsume_by(n->in(1), this); + break; ++ case Op_CallLeafPure: { ++ // If the pure call is not supported, then lower to a CallLeaf. ++ if (!Matcher::match_rule_supported(Op_CallLeafPure)) { ++ CallNode* call = n->as_Call(); ++ CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(), ++ call->_name, TypeRawPtr::BOTTOM); ++ new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control)); ++ new_call->init_req(TypeFunc::I_O, C->top()); ++ new_call->init_req(TypeFunc::Memory, C->top()); ++ new_call->init_req(TypeFunc::ReturnAdr, C->top()); ++ new_call->init_req(TypeFunc::FramePtr, C->top()); ++ for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain()->cnt(); i++) { ++ new_call->init_req(i, call->in(i)); ++ } ++ n->subsume_by(new_call, this); ++ } ++ frc.inc_call_count(); ++ break; ++ } + case Op_CallStaticJava: + case Op_CallJava: + case Op_CallDynamicJava: +diff --git a/src/hotspot/share/opto/divnode.cpp b/src/hotspot/share/opto/divnode.cpp +index a70194274a7..5dd8be877ff 100644 +--- a/src/hotspot/share/opto/divnode.cpp ++++ b/src/hotspot/share/opto/divnode.cpp +@@ -42,19 +42,19 @@ + + #include + +-ModFloatingNode::ModFloatingNode(Compile* C, const TypeFunc* tf, const char* name) : CallLeafNode(tf, nullptr, name, TypeRawPtr::BOTTOM) { ++ModFloatingNode::ModFloatingNode(Compile* C, const TypeFunc* tf, address addr, const char* name) : CallLeafPureNode(tf, addr, name, TypeRawPtr::BOTTOM) { + add_flag(Flag_is_macro); + C->add_macro_node(this); + } + +-ModDNode::ModDNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::Math_DD_D_Type(), "drem") { ++ModDNode::ModDNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::Math_DD_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::drem), "drem") { + init_req(TypeFunc::Parms + 0, a); + init_req(TypeFunc::Parms + 1, C->top()); + init_req(TypeFunc::Parms + 2, b); + init_req(TypeFunc::Parms + 3, C->top()); + } + +-ModFNode::ModFNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::modf_Type(), "frem") { ++ModFNode::ModFNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::modf_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::frem), "frem") { + init_req(TypeFunc::Parms + 0, a); + init_req(TypeFunc::Parms + 1, b); + } +@@ -1516,137 +1516,109 @@ const Type* UModLNode::Value(PhaseGVN* phase) const { + return unsigned_mod_value(phase, this); + } + +-Node* ModFNode::Ideal(PhaseGVN* phase, bool can_reshape) { +- if (!can_reshape) { +- return nullptr; +- } +- PhaseIterGVN* igvn = phase->is_IterGVN(); +- +- bool result_is_unused = proj_out_or_null(TypeFunc::Parms) == nullptr; +- bool not_dead = proj_out_or_null(TypeFunc::Control) != nullptr; +- if (result_is_unused && not_dead) { +- return replace_with_con(igvn, TypeF::make(0.)); +- } +- +- // Either input is TOP ==> the result is TOP +- const Type* t1 = phase->type(dividend()); +- const Type* t2 = phase->type(divisor()); +- if (t1 == Type::TOP || t2 == Type::TOP) { +- return phase->C->top(); +- } +- ++const Type* ModFNode::get_result_if_constant(const Type* dividend, const Type* divisor) const { + // If either number is not a constant, we know nothing. +- if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) { ++ if ((dividend->base() != Type::FloatCon) || (divisor->base() != Type::FloatCon)) { + return nullptr; // note: x%x can be either NaN or 0 + } + +- float f1 = t1->getf(); +- float f2 = t2->getf(); +- jint x1 = jint_cast(f1); // note: *(int*)&f1, not just (int)f1 +- jint x2 = jint_cast(f2); ++ float dividend_f = dividend->getf(); ++ float divisor_f = divisor->getf(); ++ jint dividend_i = jint_cast(dividend_f); // note: *(int*)&f1, not just (int)f1 ++ jint divisor_i = jint_cast(divisor_f); + + // If either is a NaN, return an input NaN +- if (g_isnan(f1)) { +- return replace_with_con(igvn, t1); ++ if (g_isnan(dividend_f)) { ++ return dividend; + } +- if (g_isnan(f2)) { +- return replace_with_con(igvn, t2); ++ if (g_isnan(divisor_f)) { ++ return divisor; + } + + // If an operand is infinity or the divisor is +/- zero, punt. +- if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint) { ++ if (!g_isfinite(dividend_f) || !g_isfinite(divisor_f) || divisor_i == 0 || divisor_i == min_jint) { + return nullptr; + } + + // We must be modulo'ing 2 float constants. + // Make sure that the sign of the fmod is equal to the sign of the dividend +- jint xr = jint_cast(fmod(f1, f2)); +- if ((x1 ^ xr) < 0) { ++ jint xr = jint_cast(fmod(dividend_f, divisor_f)); ++ if ((dividend_i ^ xr) < 0) { + xr ^= min_jint; + } + +- return replace_with_con(igvn, TypeF::make(jfloat_cast(xr))); ++ return TypeF::make(jfloat_cast(xr)); + } + +-Node* ModDNode::Ideal(PhaseGVN* phase, bool can_reshape) { +- if (!can_reshape) { +- return nullptr; +- } +- PhaseIterGVN* igvn = phase->is_IterGVN(); +- +- bool result_is_unused = proj_out_or_null(TypeFunc::Parms) == nullptr; +- bool not_dead = proj_out_or_null(TypeFunc::Control) != nullptr; +- if (result_is_unused && not_dead) { +- return replace_with_con(igvn, TypeD::make(0.)); +- } +- +- // Either input is TOP ==> the result is TOP +- const Type* t1 = phase->type(dividend()); +- const Type* t2 = phase->type(divisor()); +- if (t1 == Type::TOP || t2 == Type::TOP) { +- return nullptr; +- } +- ++const Type* ModDNode::get_result_if_constant(const Type* dividend, const Type* divisor) const { + // If either number is not a constant, we know nothing. +- if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) { ++ if ((dividend->base() != Type::DoubleCon) || (divisor->base() != Type::DoubleCon)) { + return nullptr; // note: x%x can be either NaN or 0 + } + +- double f1 = t1->getd(); +- double f2 = t2->getd(); +- jlong x1 = jlong_cast(f1); // note: *(long*)&f1, not just (long)f1 +- jlong x2 = jlong_cast(f2); ++ double dividend_d = dividend->getd(); ++ double divisor_d = divisor->getd(); ++ jlong dividend_l = jlong_cast(dividend_d); // note: *(long*)&f1, not just (long)f1 ++ jlong divisor_l = jlong_cast(divisor_d); + + // If either is a NaN, return an input NaN +- if (g_isnan(f1)) { +- return replace_with_con(igvn, t1); ++ if (g_isnan(dividend_d)) { ++ return dividend; + } +- if (g_isnan(f2)) { +- return replace_with_con(igvn, t2); ++ if (g_isnan(divisor_d)) { ++ return divisor; + } + + // If an operand is infinity or the divisor is +/- zero, punt. +- if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jlong) { ++ if (!g_isfinite(dividend_d) || !g_isfinite(divisor_d) || divisor_l == 0 || divisor_l == min_jlong) { + return nullptr; + } + + // We must be modulo'ing 2 double constants. + // Make sure that the sign of the fmod is equal to the sign of the dividend +- jlong xr = jlong_cast(fmod(f1, f2)); +- if ((x1 ^ xr) < 0) { ++ jlong xr = jlong_cast(fmod(dividend_d, divisor_d)); ++ if ((dividend_l ^ xr) < 0) { + xr ^= min_jlong; + } + +- return replace_with_con(igvn, TypeD::make(jdouble_cast(xr))); ++ return TypeD::make(jdouble_cast(xr)); + } + +-Node* ModFloatingNode::replace_with_con(PhaseIterGVN* phase, const Type* con) { +- Compile* C = phase->C; +- Node* con_node = phase->makecon(con); +- CallProjections projs; +- extract_projections(&projs, false, false); +- phase->replace_node(projs.fallthrough_proj, in(TypeFunc::Control)); +- if (projs.fallthrough_catchproj != nullptr) { +- phase->replace_node(projs.fallthrough_catchproj, in(TypeFunc::Control)); +- } +- if (projs.fallthrough_memproj != nullptr) { +- phase->replace_node(projs.fallthrough_memproj, in(TypeFunc::Memory)); +- } +- if (projs.catchall_memproj != nullptr) { +- phase->replace_node(projs.catchall_memproj, C->top()); +- } +- if (projs.fallthrough_ioproj != nullptr) { +- phase->replace_node(projs.fallthrough_ioproj, in(TypeFunc::I_O)); +- } +- assert(projs.catchall_ioproj == nullptr, "no exceptions from floating mod"); +- assert(projs.catchall_catchproj == nullptr, "no exceptions from floating mod"); +- if (projs.resproj != nullptr) { +- phase->replace_node(projs.resproj, con_node); ++Node* ModFloatingNode::Ideal(PhaseGVN* phase, bool can_reshape) { ++ if (can_reshape) { ++ PhaseIterGVN* igvn = phase->is_IterGVN(); ++ ++ // Either input is TOP ==> the result is TOP ++ const Type* dividend_type = phase->type(dividend()); ++ const Type* divisor_type = phase->type(divisor()); ++ if (dividend_type == Type::TOP || divisor_type == Type::TOP) { ++ return phase->C->top(); ++ } ++ const Type* constant_result = get_result_if_constant(dividend_type, divisor_type); ++ if (constant_result != nullptr) { ++ return make_tuple_of_input_state_and_constant_result(igvn, constant_result); ++ } + } +- phase->replace_node(this, C->top()); +- C->remove_macro_node(this); +- disconnect_inputs(C); +- return nullptr; ++ ++ return CallLeafPureNode::Ideal(phase, can_reshape); ++} ++ ++/* Give a tuple node for ::Ideal to return, made of the input state (control to return addr) ++ * and the given constant result. Idealization of projections will make sure to transparently ++ * propagate the input state and replace the result by the said constant. ++ */ ++TupleNode* ModFloatingNode::make_tuple_of_input_state_and_constant_result(PhaseIterGVN* phase, const Type* con) const { ++ Node* con_node = phase->makecon(con); ++ TupleNode* tuple = TupleNode::make( ++ tf()->range(), ++ in(TypeFunc::Control), ++ in(TypeFunc::I_O), ++ in(TypeFunc::Memory), ++ in(TypeFunc::FramePtr), ++ in(TypeFunc::ReturnAdr), ++ con_node); ++ ++ return tuple; + } + + //============================================================================= +diff --git a/src/hotspot/share/opto/divnode.hpp b/src/hotspot/share/opto/divnode.hpp +index 127e2431b0b..b13460c89f5 100644 +--- a/src/hotspot/share/opto/divnode.hpp ++++ b/src/hotspot/share/opto/divnode.hpp +@@ -156,40 +156,45 @@ class ModLNode : public Node { + }; + + // Base class for float and double modulus +-class ModFloatingNode : public CallLeafNode { ++class ModFloatingNode : public CallLeafPureNode { ++ TupleNode* make_tuple_of_input_state_and_constant_result(PhaseIterGVN* phase, const Type* con) const; ++ + protected: +- Node* replace_with_con(PhaseIterGVN* phase, const Type* con); ++ virtual Node* dividend() const = 0; ++ virtual Node* divisor() const = 0; ++ virtual const Type* get_result_if_constant(const Type* dividend, const Type* divisor) const = 0; + + public: +- ModFloatingNode(Compile* C, const TypeFunc* tf, const char *name); ++ ModFloatingNode(Compile* C, const TypeFunc* tf, address addr, const char* name); ++ Node* Ideal(PhaseGVN* phase, bool can_reshape) override; + }; + + // Float Modulus + class ModFNode : public ModFloatingNode { + private: +- Node* dividend() const { return in(TypeFunc::Parms + 0); } +- Node* divisor() const { return in(TypeFunc::Parms + 1); } ++ Node* dividend() const override { return in(TypeFunc::Parms + 0); } ++ Node* divisor() const override { return in(TypeFunc::Parms + 1); } ++ const Type* get_result_if_constant(const Type* dividend, const Type* divisor) const override; + + public: + ModFNode(Compile* C, Node* a, Node* b); +- virtual int Opcode() const; +- virtual uint ideal_reg() const { return Op_RegF; } +- virtual uint size_of() const { return sizeof(*this); } +- virtual Node* Ideal(PhaseGVN* phase, bool can_reshape); ++ int Opcode() const override; ++ uint ideal_reg() const override { return Op_RegF; } ++ uint size_of() const override { return sizeof(*this); } + }; + + // Double Modulus + class ModDNode : public ModFloatingNode { + private: +- Node* dividend() const { return in(TypeFunc::Parms + 0); } +- Node* divisor() const { return in(TypeFunc::Parms + 2); } ++ Node* dividend() const override { return in(TypeFunc::Parms + 0); } ++ Node* divisor() const override { return in(TypeFunc::Parms + 2); } ++ const Type* get_result_if_constant(const Type* dividend, const Type* divisor) const override; + + public: + ModDNode(Compile* C, Node* a, Node* b); +- virtual int Opcode() const; +- virtual uint ideal_reg() const { return Op_RegD; } +- virtual uint size_of() const { return sizeof(*this); } +- virtual Node* Ideal(PhaseGVN* phase, bool can_reshape); ++ int Opcode() const override; ++ uint ideal_reg() const override { return Op_RegD; } ++ uint size_of() const override { return sizeof(*this); } + }; + + //------------------------------UModINode--------------------------------------- +diff --git a/src/hotspot/share/opto/graphKit.cpp b/src/hotspot/share/opto/graphKit.cpp +index 20feca26ede..1b8b7008578 100644 +--- a/src/hotspot/share/opto/graphKit.cpp ++++ b/src/hotspot/share/opto/graphKit.cpp +@@ -1880,14 +1880,20 @@ Node* GraphKit::set_results_for_java_call(CallJavaNode* call, bool separate_io_p + // after the call, if this call has restricted memory effects. + Node* GraphKit::set_predefined_input_for_runtime_call(SafePointNode* call, Node* narrow_mem) { + // Set fixed predefined input arguments +- Node* memory = reset_memory(); +- Node* m = narrow_mem == nullptr ? memory : narrow_mem; +- call->init_req( TypeFunc::Control, control() ); +- call->init_req( TypeFunc::I_O, top() ); // does no i/o +- call->init_req( TypeFunc::Memory, m ); // may gc ptrs +- call->init_req( TypeFunc::FramePtr, frameptr() ); +- call->init_req( TypeFunc::ReturnAdr, top() ); +- return memory; ++ call->init_req(TypeFunc::Control, control()); ++ call->init_req(TypeFunc::I_O, top()); // does no i/o ++ call->init_req(TypeFunc::ReturnAdr, top()); ++ if (call->is_CallLeafPure()) { ++ call->init_req(TypeFunc::Memory, top()); ++ call->init_req(TypeFunc::FramePtr, top()); ++ return nullptr; ++ } else { ++ Node* memory = reset_memory(); ++ Node* m = narrow_mem == nullptr ? memory : narrow_mem; ++ call->init_req(TypeFunc::Memory, m); // may gc ptrs ++ call->init_req(TypeFunc::FramePtr, frameptr()); ++ return memory; ++ } + } + + //-------------------set_predefined_output_for_runtime_call-------------------- +@@ -1905,6 +1911,11 @@ void GraphKit::set_predefined_output_for_runtime_call(Node* call, + const TypePtr* hook_mem) { + // no i/o + set_control(_gvn.transform( new ProjNode(call,TypeFunc::Control) )); ++ if (call->is_CallLeafPure()) { ++ // Pure function have only control (for now) and data output, in particular ++ // they don't touch the memory, so we don't want a memory proj that is set after. ++ return; ++ } + if (keep_mem) { + // First clone the existing memory state + set_all_memory(keep_mem); +@@ -2491,6 +2502,8 @@ Node* GraphKit::make_runtime_call(int flags, + } else if (flags & RC_VECTOR){ + uint num_bits = call_type->range()->field_at(TypeFunc::Parms)->is_vect()->length_in_bytes() * BitsPerByte; + call = new CallLeafVectorNode(call_type, call_addr, call_name, adr_type, num_bits); ++ } else if (flags & RC_PURE) { ++ call = new CallLeafPureNode(call_type, call_addr, call_name, adr_type); + } else { + call = new CallLeafNode(call_type, call_addr, call_name, adr_type); + } +diff --git a/src/hotspot/share/opto/graphKit.hpp b/src/hotspot/share/opto/graphKit.hpp +index 28773d75333..806a211d7e2 100644 +--- a/src/hotspot/share/opto/graphKit.hpp ++++ b/src/hotspot/share/opto/graphKit.hpp +@@ -784,6 +784,7 @@ class GraphKit : public Phase { + RC_NARROW_MEM = 16, // input memory is same as output + RC_UNCOMMON = 32, // freq. expected to be like uncommon trap + RC_VECTOR = 64, // CallLeafVectorNode ++ RC_PURE = 128, // CallLeaf is pure + RC_LEAF = 0 // null value: no flags set + }; + +diff --git a/src/hotspot/share/opto/library_call.cpp b/src/hotspot/share/opto/library_call.cpp +index f74af38387c..12960d101a8 100644 +--- a/src/hotspot/share/opto/library_call.cpp ++++ b/src/hotspot/share/opto/library_call.cpp +@@ -1802,7 +1802,7 @@ bool LibraryCallKit::runtime_math(const TypeFunc* call_type, address funcAddr, c + Node* b = (call_type == OptoRuntime::Math_DD_D_Type()) ? argument(2) : nullptr; + + const TypePtr* no_memory_effects = nullptr; +- Node* trig = make_runtime_call(RC_LEAF, call_type, funcAddr, funcName, ++ Node* trig = make_runtime_call(RC_LEAF | RC_PURE, call_type, funcAddr, funcName, + no_memory_effects, + a, top(), b, b ? top() : nullptr); + Node* value = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+0)); +diff --git a/src/hotspot/share/opto/macro.cpp b/src/hotspot/share/opto/macro.cpp +index acf1dbabf19..aafc5d3c170 100644 +--- a/src/hotspot/share/opto/macro.cpp ++++ b/src/hotspot/share/opto/macro.cpp +@@ -2638,17 +2638,14 @@ bool PhaseMacroExpand::expand_macro_nodes() { + switch (n->Opcode()) { + case Op_ModD: + case Op_ModF: { +- bool is_drem = n->Opcode() == Op_ModD; + CallNode* mod_macro = n->as_Call(); +- CallNode* call = new CallLeafNode(mod_macro->tf(), +- is_drem ? CAST_FROM_FN_PTR(address, SharedRuntime::drem) +- : CAST_FROM_FN_PTR(address, SharedRuntime::frem), +- is_drem ? "drem" : "frem", TypeRawPtr::BOTTOM); ++ CallNode* call = new CallLeafPureNode(mod_macro->tf(), mod_macro->entry_point(), ++ mod_macro->_name, TypeRawPtr::BOTTOM); + call->init_req(TypeFunc::Control, mod_macro->in(TypeFunc::Control)); +- call->init_req(TypeFunc::I_O, mod_macro->in(TypeFunc::I_O)); +- call->init_req(TypeFunc::Memory, mod_macro->in(TypeFunc::Memory)); +- call->init_req(TypeFunc::ReturnAdr, mod_macro->in(TypeFunc::ReturnAdr)); +- call->init_req(TypeFunc::FramePtr, mod_macro->in(TypeFunc::FramePtr)); ++ call->init_req(TypeFunc::I_O, C->top()); ++ call->init_req(TypeFunc::Memory, C->top()); ++ call->init_req(TypeFunc::ReturnAdr, C->top()); ++ call->init_req(TypeFunc::FramePtr, C->top()); + for (unsigned int i = 0; i < mod_macro->tf()->domain()->cnt() - TypeFunc::Parms; i++) { + call->init_req(TypeFunc::Parms + i, mod_macro->in(TypeFunc::Parms + i)); + } +diff --git a/src/hotspot/share/opto/multnode.cpp b/src/hotspot/share/opto/multnode.cpp +index 736e84315ee..f429d5daac0 100644 +--- a/src/hotspot/share/opto/multnode.cpp ++++ b/src/hotspot/share/opto/multnode.cpp +@@ -120,6 +120,10 @@ const TypePtr *ProjNode::adr_type() const { + if (bottom_type() == Type::MEMORY) { + // in(0) might be a narrow MemBar; otherwise we will report TypePtr::BOTTOM + Node* ctrl = in(0); ++ if (ctrl->Opcode() == Op_Tuple) { ++ // Jumping over Tuples: the i-th projection of a Tuple is the i-th input of the Tuple. ++ ctrl = ctrl->in(_con); ++ } + if (ctrl == nullptr) return nullptr; // node is dead + const TypePtr* adr_type = ctrl->adr_type(); + #ifdef ASSERT +@@ -163,6 +167,15 @@ void ProjNode::check_con() const { + assert(_con < t->is_tuple()->cnt(), "ProjNode::_con must be in range"); + } + ++//------------------------------Identity--------------------------------------- ++Node* ProjNode::Identity(PhaseGVN* phase) { ++ if (in(0) != nullptr && in(0)->Opcode() == Op_Tuple) { ++ // Jumping over Tuples: the i-th projection of a Tuple is the i-th input of the Tuple. ++ return in(0)->in(_con); ++ } ++ return this; ++} ++ + //------------------------------Value------------------------------------------ + const Type* ProjNode::Value(PhaseGVN* phase) const { + if (in(0) == nullptr) return Type::TOP; +diff --git a/src/hotspot/share/opto/multnode.hpp b/src/hotspot/share/opto/multnode.hpp +index dff2caed38d..834dcfdca6d 100644 +--- a/src/hotspot/share/opto/multnode.hpp ++++ b/src/hotspot/share/opto/multnode.hpp +@@ -82,6 +82,7 @@ class ProjNode : public Node { + virtual const Type *bottom_type() const; + virtual const TypePtr *adr_type() const; + virtual bool pinned() const; ++ virtual Node* Identity(PhaseGVN* phase); + virtual const Type* Value(PhaseGVN* phase) const; + virtual uint ideal_reg() const; + virtual const RegMask &out_RegMask() const; +@@ -105,4 +106,49 @@ class ProjNode : public Node { + ProjNode* other_if_proj() const; + }; + ++/* Tuples are used to avoid manual graph surgery. When a node with Proj outputs (such as a call) ++ * must be removed and its ouputs replaced by its input, or some other value, we can make its ++ * ::Ideal return a tuple of what we want for each output: the ::Identity of output Proj will ++ * take care to jump over the Tuple and directly pick up the right input of the Tuple. ++ * ++ * For instance, if a function call is proven to have no side effect and return the constant 0, ++ * we can replace it with the 6-tuple: ++ * (control input, IO input, memory input, frame ptr input, return addr input, Con:0) ++ * all the output projections will pick up the input of the now gone call, except for the result ++ * projection that is replaced by 0. ++ * ++ * Using TupleNode avoid manual graph surgery and leave that to our expert surgeon: IGVN. ++ * Since the user of a Tuple are expected to be Proj, when creating a tuple during idealization, ++ * the output Proj should be enqueued for IGVN immediately after, and the tuple should not survive ++ * after the current IGVN. ++ */ ++class TupleNode : public MultiNode { ++ const TypeTuple* _tf; ++ ++ template ++ static void make_helper(TupleNode* tn, uint i, Node* node, NN... nn) { ++ tn->set_req(i, node); ++ make_helper(tn, i + 1, nn...); ++ } ++ ++ static void make_helper(TupleNode*, uint) {} ++ ++public: ++ TupleNode(const TypeTuple* tf) : MultiNode(tf->cnt()), _tf(tf) {} ++ ++ int Opcode() const override; ++ const Type* bottom_type() const override { return _tf; } ++ ++ /* Give as many `Node*` as you want in the `nn` pack: ++ * TupleNode::make(tf, input1) ++ * TupleNode::make(tf, input1, input2, input3, input4) ++ */ ++ template ++ static TupleNode* make(const TypeTuple* tf, NN... nn) { ++ TupleNode* tn = new TupleNode(tf); ++ make_helper(tn, 0, nn...); ++ return tn; ++ } ++}; ++ + #endif // SHARE_OPTO_MULTNODE_HPP +diff --git a/src/hotspot/share/opto/node.cpp b/src/hotspot/share/opto/node.cpp +index 8f6c67c16f5..5ecc038954d 100644 +--- a/src/hotspot/share/opto/node.cpp ++++ b/src/hotspot/share/opto/node.cpp +@@ -2946,23 +2946,13 @@ bool Node::is_dead_loop_safe() const { + bool Node::is_div_or_mod(BasicType bt) const { return Opcode() == Op_Div(bt) || Opcode() == Op_Mod(bt) || + Opcode() == Op_UDiv(bt) || Opcode() == Op_UMod(bt); } + +-bool Node::is_pure_function() const { +- switch (Opcode()) { +- case Op_ModD: +- case Op_ModF: +- return true; +- default: +- return false; +- } +-} +- + // `maybe_pure_function` is assumed to be the input of `this`. This is a bit redundant, + // but we already have and need maybe_pure_function in all the call sites, so + // it makes it obvious that the `maybe_pure_function` is the same node as in the caller, + // while it takes more thinking to realize that a locally computed in(0) must be equal to + // the local in the caller. + bool Node::is_data_proj_of_pure_function(const Node* maybe_pure_function) const { +- return Opcode() == Op_Proj && as_Proj()->_con == TypeFunc::Parms && maybe_pure_function->is_pure_function(); ++ return Opcode() == Op_Proj && as_Proj()->_con == TypeFunc::Parms && maybe_pure_function->is_CallLeafPure(); + } + + //============================================================================= +diff --git a/src/hotspot/share/opto/node.hpp b/src/hotspot/share/opto/node.hpp +index 2bbb10879f5..dc0ac474c4b 100644 +--- a/src/hotspot/share/opto/node.hpp ++++ b/src/hotspot/share/opto/node.hpp +@@ -54,6 +54,7 @@ class CallDynamicJavaNode; + class CallJavaNode; + class CallLeafNode; + class CallLeafNoFPNode; ++class CallLeafPureNode; + class CallNode; + class CallRuntimeNode; + class CallStaticJavaNode; +@@ -673,6 +674,7 @@ class Node { + DEFINE_CLASS_ID(CallRuntime, Call, 1) + DEFINE_CLASS_ID(CallLeaf, CallRuntime, 0) + DEFINE_CLASS_ID(CallLeafNoFP, CallLeaf, 0) ++ DEFINE_CLASS_ID(CallLeafPure, CallLeaf, 1) + DEFINE_CLASS_ID(Allocate, Call, 2) + DEFINE_CLASS_ID(AllocateArray, Allocate, 0) + DEFINE_CLASS_ID(AbstractLock, Call, 3) +@@ -907,6 +909,7 @@ class Node { + DEFINE_CLASS_QUERY(CallJava) + DEFINE_CLASS_QUERY(CallLeaf) + DEFINE_CLASS_QUERY(CallLeafNoFP) ++ DEFINE_CLASS_QUERY(CallLeafPure) + DEFINE_CLASS_QUERY(CallRuntime) + DEFINE_CLASS_QUERY(CallStaticJava) + DEFINE_CLASS_QUERY(Catch) +@@ -1289,8 +1292,6 @@ class Node { + + bool is_div_or_mod(BasicType bt) const; + +- bool is_pure_function() const; +- + bool is_data_proj_of_pure_function(const Node* maybe_pure_function) const; + + //----------------- Printing, etc +diff --git a/src/hotspot/share/opto/parse2.cpp b/src/hotspot/share/opto/parse2.cpp +index 1a4c3c91c4f..04b6e49b620 100644 +--- a/src/hotspot/share/opto/parse2.cpp ++++ b/src/hotspot/share/opto/parse2.cpp +@@ -1097,11 +1097,11 @@ void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, + + Node* Parse::floating_point_mod(Node* a, Node* b, BasicType type) { + assert(type == BasicType::T_FLOAT || type == BasicType::T_DOUBLE, "only float and double are floating points"); +- CallNode* mod = type == BasicType::T_DOUBLE ? static_cast(new ModDNode(C, a, b)) : new ModFNode(C, a, b); ++ CallLeafPureNode* mod = type == BasicType::T_DOUBLE ? static_cast(new ModDNode(C, a, b)) : new ModFNode(C, a, b); + +- Node* prev_mem = set_predefined_input_for_runtime_call(mod); +- mod = _gvn.transform(mod)->as_Call(); +- set_predefined_output_for_runtime_call(mod, prev_mem, TypeRawPtr::BOTTOM); ++ set_predefined_input_for_runtime_call(mod); ++ mod = _gvn.transform(mod)->as_CallLeafPure(); ++ set_predefined_output_for_runtime_call(mod); + Node* result = _gvn.transform(new ProjNode(mod, TypeFunc::Parms + 0)); + record_for_igvn(mod); + return result; +-- +2.53.0 + diff --git a/jdk8378713-c2_missing_pow_constant_folding.patch b/jdk8378713-c2_missing_pow_constant_folding.patch new file mode 100644 index 0000000..6e6bb11 --- /dev/null +++ b/jdk8378713-c2_missing_pow_constant_folding.patch @@ -0,0 +1,626 @@ +From 1f61b6754b5abce07db45fd385a19c2cf9296a08 Mon Sep 17 00:00:00 2001 +From: Kangcheng Xu +Date: Mon, 27 Apr 2026 10:14:39 -0400 +Subject: [PATCH 2/2] Backport 7f631ea958e30246b927f3524f0fbf37334422b9 + +--- + src/hotspot/share/opto/callnode.cpp | 175 ++++++++++++++ + src/hotspot/share/opto/callnode.hpp | 17 ++ + src/hotspot/share/opto/classes.hpp | 1 + + src/hotspot/share/opto/library_call.cpp | 62 +---- + src/hotspot/share/opto/macro.cpp | 19 +- + .../intrinsics/math/PowDNodeTests.java | 218 ++++++++++++++++++ + .../compiler/lib/ir_framework/IRNode.java | 11 + + 7 files changed, 437 insertions(+), 66 deletions(-) + create mode 100644 test/hotspot/jtreg/compiler/intrinsics/math/PowDNodeTests.java + +diff --git a/src/hotspot/share/opto/callnode.cpp b/src/hotspot/share/opto/callnode.cpp +index fc4b0c35dff..cb6cc2082b3 100644 +--- a/src/hotspot/share/opto/callnode.cpp ++++ b/src/hotspot/share/opto/callnode.cpp +@@ -42,6 +42,7 @@ + #include "opto/rootnode.hpp" + #include "opto/runtime.hpp" + #include "runtime/sharedRuntime.hpp" ++#include "runtime/stubRoutines.hpp" + #include "utilities/powerOfTwo.hpp" + #include "code/vmreg.hpp" + +@@ -1354,6 +1355,25 @@ TupleNode* CallLeafPureNode::make_tuple_of_input_state_and_top_return_values(con + return tuple; + } + ++CallLeafPureNode* CallLeafPureNode::inline_call_leaf_pure_node(Node* control) const { ++ Node* top = Compile::current()->top(); ++ if (control == nullptr) { ++ control = in(TypeFunc::Control); ++ } ++ ++ CallLeafPureNode* call = new CallLeafPureNode(tf(), entry_point(), _name, nullptr); ++ call->init_req(TypeFunc::Control, control); ++ call->init_req(TypeFunc::I_O, top); ++ call->init_req(TypeFunc::Memory, top); ++ call->init_req(TypeFunc::ReturnAdr, top); ++ call->init_req(TypeFunc::FramePtr, top); ++ for (unsigned int i = 0; i < tf()->domain()->cnt() - TypeFunc::Parms; i++) { ++ call->init_req(TypeFunc::Parms + i, in(TypeFunc::Parms + i)); ++ } ++ ++ return call; ++} ++ + Node* CallLeafPureNode::Ideal(PhaseGVN* phase, bool can_reshape) { + if (is_dead()) { + return nullptr; +@@ -2424,3 +2444,158 @@ bool CallNode::may_modify_arraycopy_helper(const TypeOopPtr* dest_t, const TypeO + + return true; + } ++ ++PowDNode::PowDNode(Compile* C, Node* base, Node* exp) ++ : CallLeafPureNode( ++ OptoRuntime::Math_DD_D_Type(), ++ StubRoutines::dpow() != nullptr ? StubRoutines::dpow() : CAST_FROM_FN_PTR(address, SharedRuntime::dpow), ++ "pow", ++ nullptr) { ++ add_flag(Flag_is_macro); ++ C->add_macro_node(this); ++ ++ init_req(TypeFunc::Parms + 0, base); ++ init_req(TypeFunc::Parms + 1, C->top()); // double slot padding ++ init_req(TypeFunc::Parms + 2, exp); ++ init_req(TypeFunc::Parms + 3, C->top()); // double slot padding ++} ++ ++const Type* PowDNode::Value(PhaseGVN* phase) const { ++ const Type* t_base = phase->type(base()); ++ const Type* t_exp = phase->type(exp()); ++ ++ if (t_base == Type::TOP || t_exp == Type::TOP) { ++ return Type::TOP; ++ } ++ ++ const TypeD* base_con = t_base->isa_double_constant(); ++ const TypeD* exp_con = t_exp->isa_double_constant(); ++ const TypeD* result_t = nullptr; ++ ++ // constant folding: both inputs are constants ++ if (base_con != nullptr && exp_con != nullptr) { ++ result_t = TypeD::make(SharedRuntime::dpow(base_con->getd(), exp_con->getd())); ++ } ++ ++ // Special cases when only the exponent is known: ++ if (exp_con != nullptr) { ++ double e = exp_con->getd(); ++ ++ // If the second argument is positive or negative zero, then the result is 1.0. ++ // i.e., pow(x, +/-0.0D) => 1.0 ++ if (e == 0.0) { // true for both -0.0 and +0.0 ++ result_t = TypeD::ONE; ++ } ++ ++ // If the second argument is NaN, then the result is NaN. ++ // i.e., pow(x, NaN) => NaN ++ if (g_isnan(e)) { ++ result_t = TypeD::make(NAN); ++ } ++ } ++ ++ if (result_t != nullptr) { ++ // We can't simply return a TypeD here, it must be a tuple type to be compatible with call nodes. ++ const Type** fields = TypeTuple::fields(2); ++ fields[TypeFunc::Parms + 0] = result_t; ++ fields[TypeFunc::Parms + 1] = Type::HALF; ++ return TypeTuple::make(TypeFunc::Parms + 2, fields); ++ } ++ ++ return tf()->range(); ++} ++ ++Node* PowDNode::Ideal(PhaseGVN* phase, bool can_reshape) { ++ if (!can_reshape) { ++ return nullptr; // wait for igvn ++ } ++ ++ PhaseIterGVN* igvn = phase->is_IterGVN(); ++ Node* base = this->base(); ++ Node* exp = this->exp(); ++ ++ const Type* t_exp = phase->type(exp); ++ const TypeD* exp_con = t_exp->isa_double_constant(); ++ ++ // Special cases when only the exponent is known: ++ if (exp_con != nullptr) { ++ double e = exp_con->getd(); ++ ++ // If the second argument is 1.0, then the result is the same as the first argument. ++ // i.e., pow(x, 1.0) => x ++ if (e == 1.0) { ++ return make_tuple_of_input_state_and_result(igvn, base); ++ } ++ ++ // If the second argument is 2.0, then strength reduce to multiplications. ++ // i.e., pow(x, 2.0) => x * x ++ if (e == 2.0) { ++ Node* mul = igvn->transform(new MulDNode(base, base)); ++ return make_tuple_of_input_state_and_result(igvn, mul); ++ } ++ ++ // If the second argument is 0.5, the strength reduce to square roots. ++ // i.e., pow(x, 0.5) => sqrt(x) iff x > 0 ++ if (e == 0.5 && Matcher::match_rule_supported(Op_SqrtD)) { ++ Node* ctrl = in(TypeFunc::Control); ++ Node* zero = igvn->zerocon(T_DOUBLE); ++ ++ // According to the API specs, pow(-0.0, 0.5) = 0.0 and sqrt(-0.0) = -0.0. ++ // So pow(-0.0, 0.5) shouldn't be replaced with sqrt(-0.0). ++ // -0.0/+0.0 are both excluded since floating-point comparison doesn't distinguish -0.0 from +0.0. ++ Node* cmp = igvn->register_new_node_with_optimizer(new CmpDNode(base, zero)); ++ Node* test = igvn->register_new_node_with_optimizer(new BoolNode(cmp, BoolTest::le)); ++ ++ IfNode* iff = new IfNode(ctrl, test, PROB_UNLIKELY_MAG(3), COUNT_UNKNOWN); ++ igvn->register_new_node_with_optimizer(iff); ++ Node* if_slow = igvn->register_new_node_with_optimizer(new IfTrueNode(iff)); // x <= 0 ++ Node* if_fast = igvn->register_new_node_with_optimizer(new IfFalseNode(iff)); // x > 0 ++ ++ // slow path: call pow(x, 0.5) ++ Node* call = igvn->register_new_node_with_optimizer(inline_call_leaf_pure_node(if_slow)); ++ Node* call_ctrl = igvn->register_new_node_with_optimizer(new ProjNode(call, TypeFunc::Control)); ++ Node* call_result = igvn->register_new_node_with_optimizer(new ProjNode(call, TypeFunc::Parms + 0)); ++ ++ // fast path: sqrt(x) ++ Node* sqrt = igvn->register_new_node_with_optimizer(new SqrtDNode(igvn->C, if_fast, base)); ++ ++ // merge paths ++ RegionNode* region = new RegionNode(3); ++ igvn->register_new_node_with_optimizer(region); ++ region->init_req(1, call_ctrl); // slow path ++ region->init_req(2, if_fast); // fast path ++ ++ PhiNode* phi = new PhiNode(region, Type::DOUBLE); ++ igvn->register_new_node_with_optimizer(phi); ++ phi->init_req(1, call_result); // slow: pow() result ++ phi->init_req(2, sqrt); // fast: sqrt() result ++ ++ igvn->C->set_has_split_ifs(true); // Has chance for split-if optimization ++ ++ return make_tuple_of_input_state_and_result(igvn, phi, region); ++ } ++ } ++ ++ return CallLeafPureNode::Ideal(phase, can_reshape); ++} ++ ++// We can't simply have Ideal() returning a Con or MulNode since the users are still expecting a Call node, but we could ++// produce a tuple that follows the same pattern so users can still get control, io, memory, etc.. ++TupleNode* PowDNode::make_tuple_of_input_state_and_result(PhaseIterGVN* phase, Node* result, Node* control) { ++ if (control == nullptr) { ++ control = in(TypeFunc::Control); ++ } ++ ++ Compile* C = phase->C; ++ C->remove_macro_node(this); ++ TupleNode* tuple = TupleNode::make( ++ tf()->range(), ++ control, ++ in(TypeFunc::I_O), ++ in(TypeFunc::Memory), ++ in(TypeFunc::FramePtr), ++ in(TypeFunc::ReturnAdr), ++ result, ++ C->top()); ++ return tuple; ++} +diff --git a/src/hotspot/share/opto/callnode.hpp b/src/hotspot/share/opto/callnode.hpp +index 2462a88143f..bb87746a3f6 100644 +--- a/src/hotspot/share/opto/callnode.hpp ++++ b/src/hotspot/share/opto/callnode.hpp +@@ -953,6 +953,8 @@ class CallLeafPureNode : public CallLeafNode { + } + int Opcode() const override; + Node* Ideal(PhaseGVN* phase, bool can_reshape) override; ++ ++ CallLeafPureNode* inline_call_leaf_pure_node(Node* control = nullptr) const; + }; + + //------------------------------CallLeafNoFPNode------------------------------- +@@ -1304,4 +1306,19 @@ class UnlockNode : public AbstractLockNode { + JVMState* dbg_jvms() const { return nullptr; } + #endif + }; ++ ++//------------------------------PowDNode-------------------------------------- ++class PowDNode : public CallLeafPureNode { ++ TupleNode* make_tuple_of_input_state_and_result(PhaseIterGVN* phase, Node* result, Node* control = nullptr); ++ ++public: ++ PowDNode(Compile* C, Node* base, Node* exp); ++ int Opcode() const override; ++ const Type* Value(PhaseGVN* phase) const override; ++ Node* Ideal(PhaseGVN* phase, bool can_reshape) override; ++ ++ Node* base() const { return in(TypeFunc::Parms + 0); } ++ Node* exp() const { return in(TypeFunc::Parms + 2); } ++}; ++ + #endif // SHARE_OPTO_CALLNODE_HPP +diff --git a/src/hotspot/share/opto/classes.hpp b/src/hotspot/share/opto/classes.hpp +index 587d5fad8f2..b7ba16e99a0 100644 +--- a/src/hotspot/share/opto/classes.hpp ++++ b/src/hotspot/share/opto/classes.hpp +@@ -283,6 +283,7 @@ macro(OpaqueZeroTripGuard) + macro(OpaqueNotNull) + macro(OpaqueInitializedAssertionPredicate) + macro(OpaqueTemplateAssertionPredicate) ++macro(PowD) + macro(ProfileBoolean) + macro(OrI) + macro(OrL) +diff --git a/src/hotspot/share/opto/library_call.cpp b/src/hotspot/share/opto/library_call.cpp +index 12960d101a8..6c71a61ba75 100644 +--- a/src/hotspot/share/opto/library_call.cpp ++++ b/src/hotspot/share/opto/library_call.cpp +@@ -1817,61 +1817,17 @@ bool LibraryCallKit::runtime_math(const TypeFunc* call_type, address funcAddr, c + + //------------------------------inline_math_pow----------------------------- + bool LibraryCallKit::inline_math_pow() { ++ Node* base = argument(0); + Node* exp = argument(2); +- const TypeD* d = _gvn.type(exp)->isa_double_constant(); +- if (d != nullptr) { +- if (d->getd() == 2.0) { +- // Special case: pow(x, 2.0) => x * x +- Node* base = argument(0); +- set_result(_gvn.transform(new MulDNode(base, base))); +- return true; +- } else if (d->getd() == 0.5 && Matcher::match_rule_supported(Op_SqrtD)) { +- // Special case: pow(x, 0.5) => sqrt(x) +- Node* base = argument(0); +- Node* zero = _gvn.zerocon(T_DOUBLE); +- +- RegionNode* region = new RegionNode(3); +- Node* phi = new PhiNode(region, Type::DOUBLE); +- +- Node* cmp = _gvn.transform(new CmpDNode(base, zero)); +- // According to the API specs, pow(-0.0, 0.5) = 0.0 and sqrt(-0.0) = -0.0. +- // So pow(-0.0, 0.5) shouldn't be replaced with sqrt(-0.0). +- // -0.0/+0.0 are both excluded since floating-point comparison doesn't distinguish -0.0 from +0.0. +- Node* test = _gvn.transform(new BoolNode(cmp, BoolTest::le)); +- +- Node* if_pow = generate_slow_guard(test, nullptr); +- Node* value_sqrt = _gvn.transform(new SqrtDNode(C, control(), base)); +- phi->init_req(1, value_sqrt); +- region->init_req(1, control()); +- +- if (if_pow != nullptr) { +- set_control(if_pow); +- address target = StubRoutines::dpow() != nullptr ? StubRoutines::dpow() : +- CAST_FROM_FN_PTR(address, SharedRuntime::dpow); +- const TypePtr* no_memory_effects = nullptr; +- Node* trig = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(), target, "POW", +- no_memory_effects, base, top(), exp, top()); +- Node* value_pow = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+0)); +-#ifdef ASSERT +- Node* value_top = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+1)); +- assert(value_top == top(), "second value must be top"); +-#endif +- phi->init_req(2, value_pow); +- region->init_req(2, _gvn.transform(new ProjNode(trig, TypeFunc::Control))); +- } +- +- C->set_has_split_ifs(true); // Has chance for split-if optimization +- set_control(_gvn.transform(region)); +- record_for_igvn(region); +- set_result(_gvn.transform(phi)); + +- return true; +- } +- } +- +- return StubRoutines::dpow() != nullptr ? +- runtime_math(OptoRuntime::Math_DD_D_Type(), StubRoutines::dpow(), "dpow") : +- runtime_math(OptoRuntime::Math_DD_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::dpow), "POW"); ++ CallNode* pow = new PowDNode(C, base, exp); ++ set_predefined_input_for_runtime_call(pow); ++ pow = _gvn.transform(pow)->as_CallLeafPure(); ++ set_predefined_output_for_runtime_call(pow); ++ Node* result = _gvn.transform(new ProjNode(pow, TypeFunc::Parms + 0)); ++ record_for_igvn(pow); ++ set_result(result); ++ return true; + } + + //------------------------------inline_math_native----------------------------- +diff --git a/src/hotspot/share/opto/macro.cpp b/src/hotspot/share/opto/macro.cpp +index aafc5d3c170..a28043c8d8f 100644 +--- a/src/hotspot/share/opto/macro.cpp ++++ b/src/hotspot/share/opto/macro.cpp +@@ -2484,6 +2484,7 @@ void PhaseMacroExpand::eliminate_macro_nodes() { + assert(n->Opcode() == Op_LoopLimit || + n->Opcode() == Op_ModD || + n->Opcode() == Op_ModF || ++ n->Opcode() == Op_PowD || + n->is_OpaqueNotNull() || + n->is_OpaqueInitializedAssertionPredicate() || + n->Opcode() == Op_MaxL || +@@ -2637,19 +2638,11 @@ bool PhaseMacroExpand::expand_macro_nodes() { + default: + switch (n->Opcode()) { + case Op_ModD: +- case Op_ModF: { +- CallNode* mod_macro = n->as_Call(); +- CallNode* call = new CallLeafPureNode(mod_macro->tf(), mod_macro->entry_point(), +- mod_macro->_name, TypeRawPtr::BOTTOM); +- call->init_req(TypeFunc::Control, mod_macro->in(TypeFunc::Control)); +- call->init_req(TypeFunc::I_O, C->top()); +- call->init_req(TypeFunc::Memory, C->top()); +- call->init_req(TypeFunc::ReturnAdr, C->top()); +- call->init_req(TypeFunc::FramePtr, C->top()); +- for (unsigned int i = 0; i < mod_macro->tf()->domain()->cnt() - TypeFunc::Parms; i++) { +- call->init_req(TypeFunc::Parms + i, mod_macro->in(TypeFunc::Parms + i)); +- } +- _igvn.replace_node(mod_macro, call); ++ case Op_ModF: ++ case Op_PowD: { ++ CallLeafPureNode* call_macro = n->as_CallLeafPure(); ++ CallLeafPureNode* call = call_macro->inline_call_leaf_pure_node(); ++ _igvn.replace_node(call_macro, call); + transform_later(call); + break; + } +diff --git a/test/hotspot/jtreg/compiler/intrinsics/math/PowDNodeTests.java b/test/hotspot/jtreg/compiler/intrinsics/math/PowDNodeTests.java +new file mode 100644 +index 00000000000..e28cc5ab346 +--- /dev/null ++++ b/test/hotspot/jtreg/compiler/intrinsics/math/PowDNodeTests.java +@@ -0,0 +1,218 @@ ++/* ++ * Copyright (c) 2026, IBM and/or its affiliates. All rights reserved. ++ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ++ * ++ * This code is free software; you can redistribute it and/or modify it ++ * under the terms of the GNU General Public License version 2 only, as ++ * published by the Free Software Foundation. ++ * ++ * This code is distributed in the hope that it will be useful, but WITHOUT ++ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ++ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ++ * version 2 for more details (a copy is included in the LICENSE file that ++ * accompanied this code). ++ * ++ * You should have received a copy of the GNU General Public License version ++ * 2 along with this work; if not, write to the Free Software Foundation, ++ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ++ * ++ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ++ * or visit www.oracle.com if you need additional information or have any ++ * questions. ++ * ++ */ ++package compiler.intrinsics.math; ++ ++import jdk.test.lib.Asserts; ++ ++import compiler.lib.ir_framework.*; ++import compiler.lib.generators.*; ++import static compiler.lib.generators.Generators.*; ++ ++import java.util.Random; ++ ++/* ++ * @test ++ * @bug 8378713 ++ * @key randomness ++ * @summary Math.pow(base, exp) should constant propagate ++ * @library /test/lib / ++ * @run driver ${test.main.class} ++ */ ++public class PowDNodeTests { ++ public static final Generator UNIFORMS = G.uniformDoubles(); // [0, 1) ++ ++ public static final double B = UNIFORMS.next() * 1000.0d; ++ public static final double E = UNIFORMS.next() * 1000.0d + 3.0d; // e >= 3 to avoid strength reduction code ++ ++ public static void main(String[] args) { ++ TestFramework.run(); ++ ++ testCorrectness(); ++ } ++ ++ // Test 1: pow(2.0, 10.0) -> 1024.0 ++ @Test ++ @IR(failOn = {IRNode.POW_D}) ++ public static double constantLiteralFolding() { ++ return Math.pow(2.0, 10.0); // should fold to 1024.0 ++ } ++ ++ // Test 2: pow(final B, final E) -> B^E ++ @Test ++ @IR(failOn = {IRNode.POW_D}) ++ public static double constantStaticFolding() { ++ return Math.pow(B, E); // should fold to B^E ++ } ++ ++ // Test 3: pow(b, 0.0) -> 1.0 ++ @Test ++ @IR(failOn = {IRNode.POW_D}) ++ @Arguments(values = {Argument.RANDOM_EACH}) ++ public static double expZero(double b) { ++ return Math.pow(b, 0.0); ++ } ++ ++ // Test 4: pow(b, 1.0) -> b (identity) ++ @Test ++ @IR(failOn = {IRNode.POW_D}) ++ @Arguments(values = {Argument.RANDOM_EACH}) ++ public static double expOne(double b) { ++ return Math.pow(b, 1.0); ++ } ++ ++ // Test 5: pow(b, NaN) -> NaN ++ @Test ++ @IR(failOn = {IRNode.POW_D}) ++ @Arguments(values = {Argument.RANDOM_EACH}) ++ public static double expNaN(double b) { ++ return Math.pow(b, Double.NaN); ++ } ++ ++ // Test 6: pow(b, 2.0) -> b * b ++ // More tests in TestPow2Opt.java ++ @Test ++ @IR(failOn = {IRNode.POW_D}) ++ @IR(counts = {IRNode.MUL_D, "1"}) ++ @Arguments(values = {Argument.RANDOM_EACH}) ++ public static double expTwo(double b) { ++ return Math.pow(b, 2.0); ++ } ++ ++ // Test 7: pow(b, 0.5) -> b <= 0.0 ? pow(b, 0.5) : sqrt(b) ++ // More tests in TestPow0Dot5Opt.java ++ @Test ++ @IR(counts = {IRNode.IF, "1"}) ++ @IR(counts = {IRNode.SQRT_D, "1"}) ++ @IR(counts = {".*CallLeaf.*pow.*", "1"}, phase = CompilePhase.BEFORE_MATCHING) ++ @Arguments(values = {Argument.RANDOM_EACH}) ++ public static double expDot5(double b) { ++ return Math.pow(b, 0.5); // expand to: if (b > 0) { sqrt(b) } else { call(b) } ++ } ++ ++ // Test 8: non-constant exponent stays as call ++ @Test ++ @IR(counts = {IRNode.POW_D, "1"}) ++ @Arguments(values = {Argument.RANDOM_EACH, Argument.RANDOM_EACH}) ++ public static double nonConstant(double b, double e) { ++ return Math.pow(b, e); ++ } ++ ++ // Test 9: late constant discovery on base (after loop opts) ++ @Test ++ @IR(counts = {IRNode.POW_D, "1"}, phase = CompilePhase.AFTER_PARSING) ++ @IR(failOn = {IRNode.POW_D}) ++ public static double lateBaseConstant() { ++ double base = 0; ++ for (int i = 0; i < 4; i++) { ++ if ((i % 2) == 0) { ++ base = B; ++ } ++ } ++ // After loop opts, base == B (constant), so pow(B, E) folds ++ return Math.pow(base, E); ++ } ++ ++ // Test 10: late constant discovery on exp (after loop opts) ++ @Test ++ @IR(counts = {IRNode.POW_D, "1"}, phase = CompilePhase.AFTER_PARSING) ++ @IR(failOn = {IRNode.POW_D}) ++ public static double lateExpConstant() { ++ double exp = 0; ++ for (int i = 0; i < 4; i++) { ++ if ((i % 2) == 0) { ++ exp = E; ++ } ++ } ++ // After loop opts, exp == E (constant), so pow(B, E) folds ++ return Math.pow(B, exp); ++ } ++ ++ // Test 11: late constant discoveries on both base and exp (after loop opts) ++ @Test ++ @IR(counts = {IRNode.POW_D, "1"}, phase = CompilePhase.AFTER_PARSING) ++ @IR(failOn = {IRNode.POW_D}) ++ public static double lateBothConstant() { ++ double base = 0, exp = 0; ++ for (int i = 0; i < 4; i++) { ++ if ((i % 2) == 0) { ++ base = B; ++ exp = E; ++ } ++ } ++ // After loop opts, base = B, exp == E, so pow(B, E) folds ++ return Math.pow(base, exp); ++ } ++ ++ private static void assertEQWithinOneUlp(double expected, double observed) { ++ if (Double.isNaN(expected) && Double.isNaN(observed)) return; ++ ++ // Math.pow() requires result must be within 1 ulp of the respective magnitude ++ double ulp = Math.max(Math.ulp(expected), Math.ulp(observed)); ++ if (Math.abs(expected - observed) > ulp) { ++ throw new AssertionError(String.format( ++ "expect = %x, observed = %x, ulp = %x", ++ Double.doubleToRawLongBits(expected), Double.doubleToRawLongBits(observed), Double.doubleToRawLongBits(ulp) ++ )); ++ } ++ } ++ ++ private static void testCorrectness() { ++ // No need to warm up for intrinsics ++ Asserts.assertEQ(1024.0d, constantLiteralFolding()); ++ ++ double BE = StrictMath.pow(B, E); ++ assertEQWithinOneUlp(BE, constantStaticFolding()); ++ assertEQWithinOneUlp(BE, lateBaseConstant()); ++ assertEQWithinOneUlp(BE, lateExpConstant()); ++ assertEQWithinOneUlp(BE, lateBothConstant()); ++ ++ Generator anyBits = G.anyBitsDouble(); ++ Generator largeDoubles = G.uniformDoubles(Long.MAX_VALUE, Double.MAX_VALUE); ++ Generator doubles = G.doubles(); ++ double[] values = { ++ Double.MIN_VALUE, Double.MIN_NORMAL, -42.0d, -1.0d, -0.0d, +0.0d, 0.5d, 1.0d, 2.0d, 123d, Double.MAX_VALUE, ++ Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN, ++ UNIFORMS.next(), UNIFORMS.next(), ++ largeDoubles.next(), -largeDoubles.next(), // some sufficiently large magnitudes ++ anyBits.next(), anyBits.next(), // any bits with potentially more NaN representation ++ doubles.next(), doubles.next() // a healthy sprinkle of whatever else is possible ++ }; ++ ++ for (double b : values) { ++ // Strength reduced, so we know the bits matches exactly ++ Asserts.assertEQ(1.0d, expZero(b)); ++ Asserts.assertEQ(b, expOne(b)); ++ Asserts.assertEQ(b * b, expTwo(b)); ++ ++ assertEQWithinOneUlp(Double.NaN, expNaN(b)); ++ ++ // Runtime calls, so make sure the result is within 1 ulp ++ assertEQWithinOneUlp(StrictMath.pow(b, 0.5d), expDot5(b)); ++ ++ for (double e : values) { ++ assertEQWithinOneUlp(StrictMath.pow(b, e), nonConstant(b, e)); ++ } ++ } ++ } ++} +diff --git a/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java b/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java +index 7b751f1a72c..6a95a2b5da7 100644 +--- a/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java ++++ b/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java +@@ -1858,6 +1858,11 @@ public class IRNode { + beforeMatchingNameRegex(SQRT_HF, "SqrtHF"); + } + ++ public static final String SQRT_D = PREFIX + "SQRT_D" + POSTFIX; ++ static { ++ beforeMatchingNameRegex(SQRT_D, "SqrtD"); ++ } ++ + public static final String SQRT_F = PREFIX + "SQRT_F" + POSTFIX; + static { + beforeMatchingNameRegex(SQRT_F, "SqrtF"); +@@ -2825,6 +2830,12 @@ public class IRNode { + macroNodes(MOD_D, regex); + } + ++ public static final String POW_D = PREFIX + "POW_D" + POSTFIX; ++ static { ++ String regex = START + "PowD" + MID + END; ++ macroNodes(POW_D, regex); ++ } ++ + public static final String BLACKHOLE = PREFIX + "BLACKHOLE" + POSTFIX; + static { + fromBeforeRemoveUselessToFinalCode(BLACKHOLE, "Blackhole"); +-- +2.53.0 +