1 /*
2 * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "opto/addnode.hpp"
26 #include "opto/callnode.hpp"
27 #include "opto/cfgnode.hpp"
28 #include "opto/idealKit.hpp"
29 #include "opto/runtime.hpp"
30
31 // Static initialization
32
33 // This declares the position where vars are kept in the cvstate
34 // For some degree of consistency we use the TypeFunc enum to
35 // soak up spots in the inputs even though we only use early Control
36 // and Memory slots. (So far.)
37 const uint IdealKit::first_var = TypeFunc::Parms + 1;
38
39 //----------------------------IdealKit-----------------------------------------
40 IdealKit::IdealKit(GraphKit* gkit, bool delay_all_transforms, bool has_declarations) :
41 C(gkit->C), _gvn(gkit->gvn()) {
42 _initial_ctrl = gkit->control();
43 _initial_memory = gkit->merged_memory();
44 _initial_i_o = gkit->i_o();
45 _delay_all_transforms = delay_all_transforms;
46 _var_ct = 0;
47 _cvstate = nullptr;
48 // We can go memory state free or else we need the entire memory state
49 assert(_initial_memory == nullptr || _initial_memory->Opcode() == Op_MergeMem, "memory must be pre-split");
50 assert(!_gvn.is_IterGVN(), "IdealKit can't be used during Optimize phase");
51 int init_size = 5;
52 _pending_cvstates = new (C->node_arena()) GrowableArray<Node*>(C->node_arena(), init_size, 0, nullptr);
53 DEBUG_ONLY(_state = new (C->node_arena()) GrowableArray<int>(C->node_arena(), init_size, 0, 0));
54 if (!has_declarations) {
55 declarations_done();
56 }
57 }
58
59 //----------------------------sync_kit-----------------------------------------
60 void IdealKit::sync_kit(GraphKit* gkit) {
61 set_all_memory(gkit->merged_memory());
62 set_i_o(gkit->i_o());
63 set_ctrl(gkit->control());
64 }
65
66 //-------------------------------if_then-------------------------------------
67 // Create: if(left relop right)
68 // / \
69 // iffalse iftrue
70 // Push the iffalse cvstate onto the stack. The iftrue becomes the current cvstate.
71 void IdealKit::if_then(Node* left, BoolTest::mask relop,
72 Node* right, float prob, float cnt, bool push_new_state) {
73 assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new If");
74 Node* bol;
75 if (left->bottom_type()->isa_ptr() == nullptr) {
76 if (left->bottom_type()->isa_int() != nullptr) {
77 bol = Bool(CmpI(left, right), relop);
78 } else {
79 assert(left->bottom_type()->isa_long() != nullptr, "what else?");
80 bol = Bool(CmpL(left, right), relop);
81 }
82
83 } else {
84 bol = Bool(CmpP(left, right), relop);
85 }
86 // Delay gvn.transform on if-nodes until construction is finished
87 // to prevent a constant bool input from discarding a control output.
88 IfNode* iff = delay_transform(new IfNode(ctrl(), bol, prob, cnt))->as_If();
89 Node* then = IfTrue(iff);
90 Node* elsen = IfFalse(iff);
91 Node* else_cvstate = copy_cvstate();
92 else_cvstate->set_req(TypeFunc::Control, elsen);
93 _pending_cvstates->push(else_cvstate);
94 DEBUG_ONLY(if (push_new_state) _state->push(IfThenS));
95 set_ctrl(then);
96 }
97
98 //-------------------------------else_-------------------------------------
99 // Pop the else cvstate off the stack, and push the (current) then cvstate.
100 // The else cvstate becomes the current cvstate.
101 void IdealKit::else_() {
102 assert(state() == IfThenS, "bad state for new Else");
103 Node* else_cvstate = _pending_cvstates->pop();
104 DEBUG_ONLY(_state->pop());
105 // save current (then) cvstate for later use at endif
106 _pending_cvstates->push(_cvstate);
107 DEBUG_ONLY(_state->push(ElseS));
108 _cvstate = else_cvstate;
109 }
110
111 //-------------------------------end_if-------------------------------------
112 // Merge the "then" and "else" cvstates.
113 //
114 // The if_then() pushed a copy of the current state for later use
115 // as the initial state for a future "else" clause. The
116 // current state then became the initial state for the
117 // then clause. If an "else" clause was encountered, it will
118 // pop the top state and use it for it's initial state.
119 // It will also push the current state (the state at the end of
120 // the "then" clause) for latter use at the end_if.
121 //
122 // At the endif, the states are:
123 // 1) else exists a) current state is end of "else" clause
124 // b) top stack state is end of "then" clause
125 //
126 // 2) no else: a) current state is end of "then" clause
127 // b) top stack state is from the "if_then" which
128 // would have been the initial state of the else.
129 //
130 // Merging the states is accomplished by:
131 // 1) make a label for the merge
132 // 2) terminate the current state with a goto to the label
133 // 3) pop the top state from the stack and make it the
134 // current state
135 // 4) bind the label at the current state. Binding a label
136 // terminates the current state with a goto to the
137 // label and makes the label's state the current state.
138 //
139 void IdealKit::end_if() {
140 assert(state() & (IfThenS|ElseS), "bad state for new Endif");
141 Node* lab = make_label(1);
142
143 // Node* join_state = _pending_cvstates->pop();
144 /* merging, join */
145 goto_(lab);
146 _cvstate = _pending_cvstates->pop();
147
148 bind(lab);
149 DEBUG_ONLY(_state->pop());
150 }
151
152 //-------------------------------loop-------------------------------------
153 // Create the loop head portion (*) of:
154 // * iv = init
155 // * top: (region node)
156 // * if (iv relop limit) {
157 // loop body
158 // i = i + 1
159 // goto top
160 // * } else // exits loop
161 //
162 // Pushes the loop top cvstate first, then the else (loop exit) cvstate
163 // onto the stack.
164 void IdealKit::loop(GraphKit* gkit, int nargs, IdealVariable& iv, Node* init, BoolTest::mask relop, Node* limit, float prob, float cnt) {
165 assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new loop");
166 // Sync IdealKit and graphKit.
167 gkit->sync_kit(*this);
168 // Add Parse Predicates.
169 gkit->add_parse_predicates(nargs);
170 // Update IdealKit memory.
171 sync_kit(gkit);
172 set(iv, init);
173 Node* head = make_label(1);
174 bind(head);
175 _pending_cvstates->push(head); // push for use at end_loop
176 _cvstate = copy_cvstate();
177 if_then(value(iv), relop, limit, prob, cnt, false /* no new state */);
178 DEBUG_ONLY(_state->push(LoopS));
179 assert(ctrl()->is_IfTrue(), "true branch stays in loop");
180 assert(_pending_cvstates->top()->in(TypeFunc::Control)->is_IfFalse(), "false branch exits loop");
181 }
182
183 //-------------------------------end_loop-------------------------------------
184 // Creates the goto top label.
185 // Expects the else (loop exit) cvstate to be on top of the
186 // stack, and the loop top cvstate to be 2nd.
187 void IdealKit::end_loop() {
188 assert((state() == LoopS), "bad state for new end_loop");
189 Node* exit = _pending_cvstates->pop();
190 Node* head = _pending_cvstates->pop();
191 goto_(head);
192 clear(head);
193 DEBUG_ONLY(_state->pop());
194 _cvstate = exit;
195 }
196
197 //-------------------------------make_label-------------------------------------
198 // Creates a label. The number of goto's
199 // must be specified (which should be 1 less than
200 // the number of precedessors.)
201 Node* IdealKit::make_label(int goto_ct) {
202 assert(_cvstate != nullptr, "must declare variables before labels");
203 Node* lab = new_cvstate();
204 int sz = 1 + goto_ct + 1 /* fall thru */;
205 Node* reg = delay_transform(new RegionNode(sz));
206 lab->init_req(TypeFunc::Control, reg);
207 return lab;
208 }
209
210 //-------------------------------bind-------------------------------------
211 // Bind a label at the current cvstate by simulating
212 // a goto to the label.
213 void IdealKit::bind(Node* lab) {
214 goto_(lab, true /* bind */);
215 _cvstate = lab;
216 }
217
218 //-------------------------------goto_-------------------------------------
219 // Make the current cvstate a predecessor of the label,
220 // creating phi's to merge values. If bind is true and
221 // this is not the last control edge, then ensure that
222 // all live values have phis created. Used to create phis
223 // at loop-top regions.
224 void IdealKit::goto_(Node* lab, bool bind) {
225 Node* reg = lab->in(TypeFunc::Control);
226 // find next empty slot in region
227 uint slot = 1;
228 while (slot < reg->req() && reg->in(slot) != nullptr) slot++;
229 assert(slot < reg->req(), "too many gotos");
230 // If this is last predecessor, then don't force phi creation
231 if (slot == reg->req() - 1) bind = false;
232 reg->init_req(slot, ctrl());
233 assert(first_var + _var_ct == _cvstate->req(), "bad _cvstate size");
234 for (uint i = first_var; i < _cvstate->req(); i++) {
235
236 // l is the value of var reaching the label. Could be a single value
237 // reaching the label, or a phi that merges multiples values reaching
238 // the label. The latter is true if the label's input: in(..) is
239 // a phi whose control input is the region node for the label.
240
241 Node* l = lab->in(i);
242 // Get the current value of the var
243 Node* m = _cvstate->in(i);
244 // If the var went unused no need for a phi
245 if (m == nullptr) {
246 continue;
247 } else if (l == nullptr || m == l) {
248 // Only one unique value "m" is known to reach this label so a phi
249 // is not yet necessary unless:
250 // the label is being bound and all predecessors have not been seen,
251 // in which case "bind" will be true.
252 if (bind) {
253 m = promote_to_phi(m, reg);
254 }
255 // Record the phi/value used for this var in the label's cvstate
256 lab->set_req(i, m);
257 } else {
258 // More than one value for the variable reaches this label so
259 // a create a phi if one does not already exist.
260 if (!was_promoted_to_phi(l, reg)) {
261 l = promote_to_phi(l, reg);
262 lab->set_req(i, l);
263 }
264 // Record in the phi, the var's value from the current state
265 l->set_req(slot, m);
266 }
267 }
268 do_memory_merge(_cvstate, lab);
269 stop();
270 }
271
272 //-----------------------------promote_to_phi-----------------------------------
273 Node* IdealKit::promote_to_phi(Node* n, Node* reg) {
274 assert(!was_promoted_to_phi(n, reg), "n already promoted to phi on this region");
275 // Get a conservative type for the phi
276 const BasicType bt = n->bottom_type()->basic_type();
277 const Type* ct = Type::get_const_basic_type(bt);
278 return delay_transform(PhiNode::make(reg, n, ct));
279 }
280
281 //-----------------------------declarations_done-------------------------------
282 void IdealKit::declarations_done() {
283 _cvstate = new_cvstate(); // initialize current cvstate
284 set_ctrl(_initial_ctrl); // initialize control in current cvstate
285 set_all_memory(_initial_memory);// initialize memory in current cvstate
286 set_i_o(_initial_i_o); // initialize i_o in current cvstate
287 DEBUG_ONLY(_state->push(BlockS));
288 }
289
290 //-----------------------------transform-----------------------------------
291 Node* IdealKit::transform(Node* n) {
292 if (_delay_all_transforms) {
293 return delay_transform(n);
294 } else {
295 n = gvn().transform(n);
296 C->record_for_igvn(n);
297 return n;
298 }
299 }
300
301 //-----------------------------delay_transform-----------------------------------
302 Node* IdealKit::delay_transform(Node* n) {
303 // Delay transform until IterativeGVN
304 gvn().set_type(n, n->bottom_type());
305 C->record_for_igvn(n);
306 return n;
307 }
308
309 //-----------------------------new_cvstate-----------------------------------
310 Node* IdealKit::new_cvstate() {
311 uint sz = _var_ct + first_var;
312 return new Node(sz);
313 }
314
315 //-----------------------------copy_cvstate-----------------------------------
316 Node* IdealKit::copy_cvstate() {
317 Node* ns = new_cvstate();
318 for (uint i = 0; i < ns->req(); i++) ns->init_req(i, _cvstate->in(i));
319 // We must clone memory since it will be updated as we do stores.
320 ns->set_req(TypeFunc::Memory, MergeMemNode::make(ns->in(TypeFunc::Memory)));
321 return ns;
322 }
323
324 //-----------------------------clear-----------------------------------
325 void IdealKit::clear(Node* m) {
326 for (uint i = 0; i < m->req(); i++) m->set_req(i, nullptr);
327 }
328
329 //-----------------------------IdealVariable----------------------------
330 IdealVariable::IdealVariable(IdealKit &k) {
331 k.declare(this);
332 }
333
334 Node* IdealKit::memory(uint alias_idx) {
335 MergeMemNode* mem = merged_memory();
336 Node* p = mem->memory_at(alias_idx);
337 _gvn.set_type(p, Type::MEMORY); // must be mapped
338 return p;
339 }
340
341 void IdealKit::set_memory(Node* mem, uint alias_idx) {
342 merged_memory()->set_memory_at(alias_idx, mem);
343 }
344
345 //----------------------------- make_load ----------------------------
346 Node* IdealKit::load(Node* ctl,
347 Node* adr,
348 const Type* t,
349 BasicType bt,
350 int adr_idx,
351 bool require_atomic_access,
352 MemNode::MemOrd mo,
353 LoadNode::ControlDependency control_dependency) {
354
355 assert(adr_idx != Compile::AliasIdxTop, "use other make_load factory" );
356 const TypePtr* adr_type = nullptr; // debug-mode-only argument
357 DEBUG_ONLY(adr_type = C->get_adr_type(adr_idx));
358 Node* mem = memory(adr_idx);
359 Node* ld = LoadNode::make(_gvn, ctl, mem, adr, adr_type, t, bt, mo, control_dependency, require_atomic_access);
360 return transform(ld);
361 }
362
363 // Load AOT runtime constant
364 Node* IdealKit::load_aot_const(Node* adr, const Type* t) {
365 BasicType bt = t->basic_type();
366 const TypePtr* adr_type = nullptr; // debug-mode-only argument
367 DEBUG_ONLY(adr_type = C->get_adr_type(Compile::AliasIdxRaw));
368 Node* ctl = (Node*)C->root(); // Raw memory access needs control
369 Node* ld = LoadNode::make(_gvn, ctl, C->immutable_memory(), adr, adr_type, t, bt, MemNode::unordered,
370 LoadNode::DependsOnlyOnTest, false, false, false, false, 0);
371 return transform(ld);
372 }
373
374 Node* IdealKit::store(Node* ctl, Node* adr, Node *val, BasicType bt,
375 int adr_idx,
376 MemNode::MemOrd mo, bool require_atomic_access,
377 bool mismatched) {
378 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory");
379 const TypePtr* adr_type = nullptr;
380 DEBUG_ONLY(adr_type = C->get_adr_type(adr_idx));
381 Node *mem = memory(adr_idx);
382 Node* st = StoreNode::make(_gvn, ctl, mem, adr, adr_type, val, bt, mo, require_atomic_access);
383 if (mismatched) {
384 st->as_Store()->set_mismatched_access();
385 }
386 st = transform(st);
387 set_memory(st, adr_idx);
388
389 return st;
390 }
391
392 //---------------------------- do_memory_merge --------------------------------
393 // The memory from one merging cvstate needs to be merged with the memory for another
394 // join cvstate. If the join cvstate doesn't have a merged memory yet then we
395 // can just copy the state from the merging cvstate
396
397 // Merge one slow path into the rest of memory.
398 void IdealKit::do_memory_merge(Node* merging, Node* join) {
399
400 // Get the region for the join state
401 Node* join_region = join->in(TypeFunc::Control);
402 assert(join_region != nullptr, "join region must exist");
403 if (join->in(TypeFunc::I_O) == nullptr ) {
404 join->set_req(TypeFunc::I_O, merging->in(TypeFunc::I_O));
405 }
406 if (join->in(TypeFunc::Memory) == nullptr ) {
407 join->set_req(TypeFunc::Memory, merging->in(TypeFunc::Memory));
408 return;
409 }
410
411 // The control flow for merging must have already been attached to the join region
412 // we need its index for the phis.
413 uint slot;
414 for (slot = 1; slot < join_region->req() ; slot ++ ) {
415 if (join_region->in(slot) == merging->in(TypeFunc::Control)) break;
416 }
417 assert(slot != join_region->req(), "edge must already exist");
418
419 MergeMemNode* join_m = join->in(TypeFunc::Memory)->as_MergeMem();
420 MergeMemNode* merging_m = merging->in(TypeFunc::Memory)->as_MergeMem();
421
422 // join_m should be an ancestor mergemem of merging
423 // Slow path memory comes from the current map (which is from a slow call)
424 // Fast path/null path memory comes from the call's input
425
426 // Merge the other fast-memory inputs with the new slow-default memory.
427 // for (MergeMemStream mms(merged_memory(), fast_mem->as_MergeMem()); mms.next_non_empty2(); ) {
428 for (MergeMemStream mms(join_m, merging_m); mms.next_non_empty2(); ) {
429 Node* join_slice = mms.force_memory();
430 Node* merging_slice = mms.memory2();
431 if (join_slice != merging_slice) {
432 PhiNode* phi;
433 // bool new_phi = false;
434 // Is the phi for this slice one that we created for this join region or simply
435 // one we copied? If it is ours then add
436 if (join_slice->is_Phi() && join_slice->as_Phi()->region() == join_region) {
437 phi = join_slice->as_Phi();
438 } else {
439 // create the phi with join_slice filling supplying memory for all of the
440 // control edges to the join region
441 phi = PhiNode::make(join_region, join_slice, Type::MEMORY, mms.adr_type(C));
442 phi = (PhiNode*) delay_transform(phi);
443 // gvn().set_type(phi, Type::MEMORY);
444 // new_phi = true;
445 }
446 // Now update the phi with the slice for the merging slice
447 phi->set_req(slot, merging_slice/* slow_path, slow_slice */);
448 // this updates join_m with the phi
449 mms.set_memory(phi);
450 }
451 }
452
453 Node* join_io = join->in(TypeFunc::I_O);
454 Node* merging_io = merging->in(TypeFunc::I_O);
455 if (join_io != merging_io) {
456 PhiNode* phi;
457 if (join_io->is_Phi() && join_io->as_Phi()->region() == join_region) {
458 phi = join_io->as_Phi();
459 } else {
460 phi = PhiNode::make(join_region, join_io, Type::ABIO);
461 phi = (PhiNode*) delay_transform(phi);
462 join->set_req(TypeFunc::I_O, phi);
463 }
464 phi->set_req(slot, merging_io);
465 }
466 }
467
468
469 //----------------------------- make_call ----------------------------
470 // Trivial runtime call
471 Node* IdealKit::make_leaf_call(const TypeFunc *slow_call_type,
472 address slow_call,
473 const char *leaf_name,
474 Node* parm0,
475 Node* parm1,
476 Node* parm2,
477 Node* parm3) {
478
479 // We only handle taking in RawMem and modifying RawMem
480 const TypePtr* adr_type = TypeRawPtr::BOTTOM;
481 uint adr_idx = C->get_alias_index(adr_type);
482
483 // Slow-path leaf call
484 CallNode *call = (CallNode*)new CallLeafNode( slow_call_type, slow_call, leaf_name, adr_type);
485
486 // Set fixed predefined input arguments
487 call->init_req( TypeFunc::Control, ctrl() );
488 call->init_req( TypeFunc::I_O , top() ) ; // does no i/o
489 // Narrow memory as only memory input
490 call->init_req( TypeFunc::Memory , memory(adr_idx));
491 call->init_req( TypeFunc::FramePtr, top() /* frameptr() */ );
492 call->init_req( TypeFunc::ReturnAdr, top() );
493
494 if (parm0 != nullptr) call->init_req(TypeFunc::Parms+0, parm0);
495 if (parm1 != nullptr) call->init_req(TypeFunc::Parms+1, parm1);
496 if (parm2 != nullptr) call->init_req(TypeFunc::Parms+2, parm2);
497 if (parm3 != nullptr) call->init_req(TypeFunc::Parms+3, parm3);
498
499 // Node *c = _gvn.transform(call);
500 call = (CallNode *) _gvn.transform(call);
501 Node *c = call; // dbx gets confused with call call->dump()
502
503 // Slow leaf call has no side-effects, sets few values
504
505 set_ctrl(transform( new ProjNode(call,TypeFunc::Control) ));
506
507 // Make memory for the call
508 Node* mem = _gvn.transform( new ProjNode(call, TypeFunc::Memory) );
509
510 // Set the RawPtr memory state only.
511 set_memory(mem, adr_idx);
512
513 assert(C->alias_type(call->adr_type()) == C->alias_type(adr_type),
514 "call node must be constructed correctly");
515 Node* res = nullptr;
516 if (slow_call_type->range()->cnt() > TypeFunc::Parms) {
517 assert(slow_call_type->range()->cnt() == TypeFunc::Parms+1, "only one return value");
518 res = transform(new ProjNode(call, TypeFunc::Parms));
519 }
520 return res;
521 }
522
523 void IdealKit::make_leaf_call_no_fp(const TypeFunc *slow_call_type,
524 address slow_call,
525 const char *leaf_name,
526 const TypePtr* adr_type,
527 Node* parm0,
528 Node* parm1,
529 Node* parm2,
530 Node* parm3) {
531
532 // We only handle taking in RawMem and modifying RawMem
533 uint adr_idx = C->get_alias_index(adr_type);
534
535 // Slow-path leaf call
536 CallNode *call = (CallNode*)new CallLeafNoFPNode( slow_call_type, slow_call, leaf_name, adr_type);
537
538 // Set fixed predefined input arguments
539 call->init_req( TypeFunc::Control, ctrl() );
540 call->init_req( TypeFunc::I_O , top() ) ; // does no i/o
541 // Narrow memory as only memory input
542 call->init_req( TypeFunc::Memory , memory(adr_idx));
543 call->init_req( TypeFunc::FramePtr, top() /* frameptr() */ );
544 call->init_req( TypeFunc::ReturnAdr, top() );
545
546 if (parm0 != nullptr) call->init_req(TypeFunc::Parms+0, parm0);
547 if (parm1 != nullptr) call->init_req(TypeFunc::Parms+1, parm1);
548 if (parm2 != nullptr) call->init_req(TypeFunc::Parms+2, parm2);
549 if (parm3 != nullptr) call->init_req(TypeFunc::Parms+3, parm3);
550
551 // Node *c = _gvn.transform(call);
552 call = (CallNode *) _gvn.transform(call);
553 Node *c = call; // dbx gets confused with call call->dump()
554
555 // Slow leaf call has no side-effects, sets few values
556
557 set_ctrl(transform( new ProjNode(call,TypeFunc::Control) ));
558
559 // Make memory for the call
560 Node* mem = _gvn.transform( new ProjNode(call, TypeFunc::Memory) );
561
562 // Set the RawPtr memory state only.
563 set_memory(mem, adr_idx);
564
565 assert(C->alias_type(call->adr_type()) == C->alias_type(adr_type),
566 "call node must be constructed correctly");
567 }