Line data Source code
1 : // Copyright 2013 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/compiler/backend/code-generator.h"
6 :
7 : #include "src/address-map.h"
8 : #include "src/assembler-inl.h"
9 : #include "src/base/adapters.h"
10 : #include "src/compiler/backend/code-generator-impl.h"
11 : #include "src/compiler/linkage.h"
12 : #include "src/compiler/pipeline.h"
13 : #include "src/compiler/wasm-compiler.h"
14 : #include "src/counters.h"
15 : #include "src/eh-frame.h"
16 : #include "src/frames.h"
17 : #include "src/log.h"
18 : #include "src/macro-assembler-inl.h"
19 : #include "src/objects/smi.h"
20 : #include "src/optimized-compilation-info.h"
21 : #include "src/string-constants.h"
22 :
23 : namespace v8 {
24 : namespace internal {
25 : namespace compiler {
26 :
27 : class CodeGenerator::JumpTable final : public ZoneObject {
28 : public:
29 : JumpTable(JumpTable* next, Label** targets, size_t target_count)
30 344 : : next_(next), targets_(targets), target_count_(target_count) {}
31 :
32 688 : Label* label() { return &label_; }
33 : JumpTable* next() const { return next_; }
34 : Label** targets() const { return targets_; }
35 : size_t target_count() const { return target_count_; }
36 :
37 : private:
38 : Label label_;
39 : JumpTable* const next_;
40 : Label** const targets_;
41 : size_t const target_count_;
42 : };
43 :
44 2638929 : CodeGenerator::CodeGenerator(
45 : Zone* codegen_zone, Frame* frame, Linkage* linkage,
46 : InstructionSequence* instructions, OptimizedCompilationInfo* info,
47 : Isolate* isolate, base::Optional<OsrHelper> osr_helper,
48 : int start_source_position, JumpOptimizationInfo* jump_opt,
49 : PoisoningMitigationLevel poisoning_level, const AssemblerOptions& options,
50 : int32_t builtin_index, std::unique_ptr<AssemblerBuffer> buffer)
51 : : zone_(codegen_zone),
52 : isolate_(isolate),
53 : frame_access_state_(nullptr),
54 : linkage_(linkage),
55 : instructions_(instructions),
56 : unwinding_info_writer_(zone()),
57 : info_(info),
58 2637622 : labels_(zone()->NewArray<Label>(instructions->InstructionBlockCount())),
59 : current_block_(RpoNumber::Invalid()),
60 : start_source_position_(start_source_position),
61 : current_source_position_(SourcePosition::Unknown()),
62 : tasm_(isolate, options, CodeObjectRequired::kNo, std::move(buffer)),
63 : resolver_(this),
64 : safepoints_(zone()),
65 : handlers_(zone()),
66 : deoptimization_exits_(zone()),
67 : deoptimization_states_(zone()),
68 : deoptimization_literals_(zone()),
69 : translations_(zone()),
70 : caller_registers_saved_(false),
71 : jump_tables_(nullptr),
72 : ools_(nullptr),
73 : osr_helper_(std::move(osr_helper)),
74 : osr_pc_offset_(-1),
75 : optimized_out_literal_id_(-1),
76 : source_position_table_builder_(
77 : SourcePositionTableBuilder::RECORD_SOURCE_POSITIONS),
78 : protected_instructions_(zone()),
79 : result_(kSuccess),
80 : poisoning_level_(poisoning_level),
81 : block_starts_(zone()),
82 23748517 : instr_starts_(zone()) {
83 43603634 : for (int i = 0; i < instructions->InstructionBlockCount(); ++i) {
84 20482716 : new (&labels_[i]) Label;
85 : }
86 2638202 : CreateFrameAccessState(frame);
87 2637821 : CHECK_EQ(info->is_osr(), osr_helper_.has_value());
88 : tasm_.set_jump_optimization_info(jump_opt);
89 : Code::Kind code_kind = info->code_kind();
90 5275642 : if (code_kind == Code::WASM_FUNCTION ||
91 2637821 : code_kind == Code::WASM_TO_JS_FUNCTION ||
92 6921204 : code_kind == Code::WASM_INTERPRETER_ENTRY ||
93 123088 : (Builtins::IsBuiltinId(builtin_index) &&
94 123088 : Builtins::IsWasmRuntimeStub(builtin_index))) {
95 : tasm_.set_abort_hard(true);
96 : }
97 : tasm_.set_builtin_index(builtin_index);
98 2637821 : }
99 :
100 383755 : bool CodeGenerator::wasm_runtime_exception_support() const {
101 : DCHECK_NOT_NULL(info_);
102 767510 : return info_->wasm_runtime_exception_support();
103 : }
104 :
105 241084 : void CodeGenerator::AddProtectedInstructionLanding(uint32_t instr_offset,
106 : uint32_t landing_offset) {
107 483055 : protected_instructions_.push_back({instr_offset, landing_offset});
108 241971 : }
109 :
110 2637946 : void CodeGenerator::CreateFrameAccessState(Frame* frame) {
111 2637946 : FinishFrame(frame);
112 2637796 : frame_access_state_ = new (zone()) FrameAccessState(frame);
113 2637796 : }
114 :
115 3332421 : CodeGenerator::CodeGenResult CodeGenerator::AssembleDeoptimizerCall(
116 : int deoptimization_id, SourcePosition pos) {
117 3332421 : if (deoptimization_id > Deoptimizer::kMaxNumberOfEntries) {
118 : return kTooManyDeoptimizationBailouts;
119 : }
120 :
121 : DeoptimizeKind deopt_kind = GetDeoptimizationKind(deoptimization_id);
122 : DeoptimizeReason deoptimization_reason =
123 : GetDeoptimizationReason(deoptimization_id);
124 : Address deopt_entry =
125 3332423 : Deoptimizer::GetDeoptimizationEntry(tasm()->isolate(), deopt_kind);
126 3332419 : if (info()->is_source_positions_enabled()) {
127 101957 : tasm()->RecordDeoptReason(deoptimization_reason, pos, deoptimization_id);
128 : }
129 3332420 : tasm()->CallForDeoptimization(deopt_entry, deoptimization_id);
130 3332422 : return kSuccess;
131 : }
132 :
133 36 : void CodeGenerator::MaybeEmitOutOfLineConstantPool() {
134 : tasm()->MaybeEmitOutOfLineConstantPool();
135 36 : }
136 :
137 2640330 : void CodeGenerator::AssembleCode() {
138 : OptimizedCompilationInfo* info = this->info();
139 :
140 : // Open a frame scope to indicate that there is a frame on the stack. The
141 : // MANUAL indicates that the scope shouldn't actually generate code to set up
142 : // the frame (that is done in AssemblePrologue).
143 2637993 : FrameScope frame_scope(tasm(), StackFrame::MANUAL);
144 :
145 2640330 : if (info->is_source_positions_enabled()) {
146 11361 : AssembleSourcePosition(start_source_position());
147 : }
148 :
149 : // Check that {kJavaScriptCallCodeStartRegister} has been set correctly.
150 2639456 : if (FLAG_debug_code && (info->code_kind() == Code::OPTIMIZED_FUNCTION ||
151 : info->code_kind() == Code::BYTECODE_HANDLER)) {
152 113 : tasm()->RecordComment("-- Prologue: check code start register --");
153 113 : AssembleCodeStartRegisterCheck();
154 : }
155 :
156 : // We want to bailout only from JS functions, which are the only ones
157 : // that are optimized.
158 2639456 : if (info->IsOptimizing()) {
159 : DCHECK(linkage()->GetIncomingDescriptor()->IsJSFunctionCall());
160 463646 : tasm()->RecordComment("-- Prologue: check for deoptimization --");
161 463647 : BailoutIfDeoptimized();
162 : }
163 :
164 2639459 : InitializeSpeculationPoison();
165 :
166 : // Define deoptimization literals for all inlined functions.
167 : DCHECK_EQ(0u, deoptimization_literals_.size());
168 2704329 : for (OptimizedCompilationInfo::InlinedFunctionHolder& inlined :
169 : info->inlined_functions()) {
170 65949 : if (!inlined.shared_info.equals(info->shared_info())) {
171 65519 : int index = DefineDeoptimizationLiteral(
172 65519 : DeoptimizationLiteral(inlined.shared_info));
173 : inlined.RegisterInlinedFunctionId(index);
174 : }
175 : }
176 2638380 : inlined_function_count_ = deoptimization_literals_.size();
177 :
178 : // Define deoptimization literals for all BytecodeArrays to which we might
179 : // deopt to ensure they are strongly held by the optimized code.
180 2638380 : if (info->has_bytecode_array()) {
181 463646 : DefineDeoptimizationLiteral(DeoptimizationLiteral(info->bytecode_array()));
182 : }
183 2704328 : for (OptimizedCompilationInfo::InlinedFunctionHolder& inlined :
184 : info->inlined_functions()) {
185 65949 : DefineDeoptimizationLiteral(DeoptimizationLiteral(inlined.bytecode_array));
186 : }
187 :
188 : unwinding_info_writer_.SetNumberOfInstructionBlocks(
189 : instructions()->InstructionBlockCount());
190 :
191 2637505 : if (info->trace_turbo_json_enabled()) {
192 27 : block_starts_.assign(instructions()->instruction_blocks().size(), -1);
193 27 : instr_starts_.assign(instructions()->instructions().size(), -1);
194 : }
195 :
196 : // Assemble instructions in assembly order.
197 23126436 : for (const InstructionBlock* block : instructions()->ao_blocks()) {
198 : // Align loop headers on vendor recommended boundaries.
199 20484056 : if (block->ShouldAlign() && !tasm()->jump_optimization_info()) {
200 56613 : tasm()->CodeTargetAlign();
201 : }
202 20484716 : if (info->trace_turbo_json_enabled()) {
203 42 : block_starts_[block->rpo_number().ToInt()] = tasm()->pc_offset();
204 : }
205 : // Bind a label for a block.
206 20484716 : current_block_ = block->rpo_number();
207 20484716 : unwinding_info_writer_.BeginInstructionBlock(tasm()->pc_offset(), block);
208 20484736 : if (FLAG_code_comments) {
209 4006 : std::ostringstream buffer;
210 2003 : buffer << "-- B" << block->rpo_number().ToInt() << " start";
211 2003 : if (block->IsDeferred()) buffer << " (deferred)";
212 2003 : if (!block->needs_frame()) buffer << " (no frame)";
213 2003 : if (block->must_construct_frame()) buffer << " (construct frame)";
214 2003 : if (block->must_deconstruct_frame()) buffer << " (deconstruct frame)";
215 :
216 2003 : if (block->IsLoopHeader()) {
217 17 : buffer << " (loop up to " << block->loop_end().ToInt() << ")";
218 : }
219 2003 : if (block->loop_header().IsValid()) {
220 793 : buffer << " (in loop " << block->loop_header().ToInt() << ")";
221 : }
222 2003 : buffer << " --";
223 4006 : tasm()->RecordComment(buffer.str().c_str());
224 : }
225 :
226 20484736 : frame_access_state()->MarkHasFrame(block->needs_frame());
227 :
228 20484733 : tasm()->bind(GetLabel(current_block_));
229 :
230 20485206 : TryInsertBranchPoisoning(block);
231 :
232 20484498 : if (block->must_construct_frame()) {
233 2660554 : AssembleConstructFrame();
234 : // We need to setup the root register after we assemble the prologue, to
235 : // avoid clobbering callee saved registers in case of C linkage and
236 : // using the roots.
237 : // TODO(mtrofin): investigate how we can avoid doing this repeatedly.
238 2660891 : if (linkage()->GetIncomingDescriptor()->InitializeRootRegister()) {
239 888556 : tasm()->InitializeRootRegister();
240 : }
241 : }
242 :
243 : if (FLAG_enable_embedded_constant_pool && !block->needs_frame()) {
244 : ConstantPoolUnavailableScope constant_pool_unavailable(tasm());
245 : result_ = AssembleBlock(block);
246 : } else {
247 20484835 : result_ = AssembleBlock(block);
248 : }
249 20488584 : if (result_ != kSuccess) return;
250 20488794 : unwinding_info_writer_.EndInstructionBlock(block);
251 : }
252 :
253 : // Assemble all out-of-line code.
254 2642380 : if (ools_) {
255 248394 : tasm()->RecordComment("-- Out of line code --");
256 1009282 : for (OutOfLineCode* ool = ools_; ool; ool = ool->next()) {
257 760944 : tasm()->bind(ool->entry());
258 760964 : ool->Generate();
259 1138357 : if (ool->exit()->is_bound()) tasm()->jmp(ool->exit());
260 : }
261 : }
262 :
263 : // This nop operation is needed to ensure that the trampoline is not
264 : // confused with the pc of the call before deoptimization.
265 : // The test regress/regress-259 is an example of where we need it.
266 2642324 : tasm()->nop();
267 :
268 : // Assemble deoptimization exits.
269 : int last_updated = 0;
270 5926269 : for (DeoptimizationExit* exit : deoptimization_exits_) {
271 3287728 : tasm()->bind(exit->label());
272 : int trampoline_pc = tasm()->pc_offset();
273 : int deoptimization_id = exit->deoptimization_id();
274 3287729 : DeoptimizationState* ds = deoptimization_states_[deoptimization_id];
275 :
276 3287729 : if (ds->kind() == DeoptimizeKind::kLazy) {
277 : last_updated = safepoints()->UpdateDeoptimizationInfo(
278 2956230 : ds->pc_offset(), trampoline_pc, last_updated);
279 : }
280 3287732 : result_ = AssembleDeoptimizerCall(deoptimization_id, exit->pos());
281 3287729 : if (result_ != kSuccess) return;
282 : }
283 :
284 : // TODO(jgruber): Move all inlined metadata generation into a new,
285 : // architecture-independent version of FinishCode. Currently, this includes
286 : // the safepoint table, handler table, constant pool, and code comments, in
287 : // that order.
288 2638541 : FinishCode();
289 :
290 : // Emit the jump tables.
291 2637798 : if (jump_tables_) {
292 342 : tasm()->Align(kSystemPointerSize);
293 686 : for (JumpTable* table = jump_tables_; table; table = table->next()) {
294 344 : tasm()->bind(table->label());
295 344 : AssembleJumpTable(table->targets(), table->target_count());
296 : }
297 : }
298 :
299 : // The PerfJitLogger logs code up until here, excluding the safepoint
300 : // table. Resolve the unwinding info now so it is aware of the same code
301 : // size as reported by perf.
302 : unwinding_info_writer_.Finish(tasm()->pc_offset());
303 :
304 2637798 : safepoints()->Emit(tasm(), frame()->GetTotalFrameSlotCount());
305 :
306 : // Emit the exception handler table.
307 2638889 : if (!handlers_.empty()) {
308 20375 : handler_table_offset_ = HandlerTable::EmitReturnTableStart(tasm());
309 473605 : for (size_t i = 0; i < handlers_.size(); ++i) {
310 226615 : HandlerTable::EmitReturnEntry(tasm(), handlers_[i].pc_offset,
311 453230 : handlers_[i].handler->pos());
312 : }
313 : }
314 :
315 : tasm()->MaybeEmitOutOfLineConstantPool();
316 2638888 : tasm()->FinalizeJumpOptimizationInfo();
317 :
318 2637993 : result_ = kSuccess;
319 : }
320 :
321 20485643 : void CodeGenerator::TryInsertBranchPoisoning(const InstructionBlock* block) {
322 : // See if our predecessor was a basic block terminated by a branch_and_poison
323 : // instruction. If yes, then perform the masking based on the flags.
324 26982211 : if (block->PredecessorCount() != 1) return;
325 13988861 : RpoNumber pred_rpo = (block->predecessors())[0];
326 13988861 : const InstructionBlock* pred = instructions()->InstructionBlockAt(pred_rpo);
327 13987845 : if (pred->code_start() == pred->code_end()) return;
328 13988059 : Instruction* instr = instructions()->InstructionAt(pred->code_end() - 1);
329 13988529 : FlagsMode mode = FlagsModeField::decode(instr->opcode());
330 13988529 : switch (mode) {
331 : case kFlags_branch_and_poison: {
332 : BranchInfo branch;
333 0 : RpoNumber target = ComputeBranchInfo(&branch, instr);
334 0 : if (!target.IsValid()) {
335 : // Non-trivial branch, add the masking code.
336 0 : FlagsCondition condition = branch.condition;
337 0 : if (branch.false_label == GetLabel(block->rpo_number())) {
338 : condition = NegateFlagsCondition(condition);
339 : }
340 0 : AssembleBranchPoisoning(condition, instr);
341 : }
342 : break;
343 : }
344 : case kFlags_deoptimize_and_poison: {
345 0 : UNREACHABLE();
346 : break;
347 : }
348 : default:
349 : break;
350 : }
351 : }
352 :
353 119281 : void CodeGenerator::AssembleArchBinarySearchSwitchRange(
354 : Register input, RpoNumber def_block, std::pair<int32_t, Label*>* begin,
355 : std::pair<int32_t, Label*>* end) {
356 119281 : if (end - begin < kBinarySearchSwitchMinimalCases) {
357 469546 : while (begin != end) {
358 392000 : tasm()->JumpIfEqual(input, begin->first, begin->second);
359 195999 : ++begin;
360 : }
361 77547 : AssembleArchJump(def_block);
362 77546 : return;
363 : }
364 41733 : auto middle = begin + (end - begin) / 2;
365 41733 : Label less_label;
366 83466 : tasm()->JumpIfLessThan(input, middle->first, &less_label);
367 41734 : AssembleArchBinarySearchSwitchRange(input, def_block, middle, end);
368 41734 : tasm()->bind(&less_label);
369 41734 : AssembleArchBinarySearchSwitchRange(input, def_block, begin, middle);
370 : }
371 :
372 992932 : OwnedVector<byte> CodeGenerator::GetSourcePositionTable() {
373 992932 : return source_position_table_builder_.ToSourcePositionTableVector();
374 : }
375 :
376 : OwnedVector<trap_handler::ProtectedInstructionData>
377 993620 : CodeGenerator::GetProtectedInstructions() {
378 : return OwnedVector<trap_handler::ProtectedInstructionData>::Of(
379 993620 : protected_instructions_);
380 : }
381 :
382 1588815 : MaybeHandle<Code> CodeGenerator::FinalizeCode() {
383 1588815 : if (result_ != kSuccess) {
384 : tasm()->AbortedCodeGeneration();
385 0 : return MaybeHandle<Code>();
386 : }
387 :
388 : // Allocate the source position table.
389 : Handle<ByteArray> source_positions =
390 1588815 : source_position_table_builder_.ToSourcePositionTable(isolate());
391 :
392 : // Allocate deoptimization data.
393 1588815 : Handle<DeoptimizationData> deopt_data = GenerateDeoptimizationData();
394 :
395 : // Allocate and install the code.
396 1588814 : CodeDesc desc;
397 3177628 : tasm()->GetCode(isolate(), &desc, safepoints(), handler_table_offset_);
398 :
399 : #if defined(V8_OS_WIN_X64)
400 : if (Builtins::IsBuiltinId(info_->builtin_index())) {
401 : isolate_->SetBuiltinUnwindData(info_->builtin_index(),
402 : tasm()->GetUnwindInfo());
403 : }
404 : #endif
405 :
406 1588814 : if (unwinding_info_writer_.eh_frame_writer()) {
407 22 : unwinding_info_writer_.eh_frame_writer()->GetEhFrame(&desc);
408 : }
409 :
410 : MaybeHandle<Code> maybe_code = isolate()->factory()->TryNewCode(
411 : desc, info()->code_kind(), Handle<Object>(), info()->builtin_index(),
412 : source_positions, deopt_data, kMovable, true,
413 1588814 : frame()->GetTotalFrameSlotCount());
414 :
415 : Handle<Code> code;
416 1588815 : if (!maybe_code.ToHandle(&code)) {
417 : tasm()->AbortedCodeGeneration();
418 0 : return MaybeHandle<Code>();
419 : }
420 :
421 : isolate()->counters()->total_compiled_code_size()->Increment(
422 1588815 : code->raw_instruction_size());
423 :
424 1588886 : LOG_CODE_EVENT(isolate(),
425 : CodeLinePosInfoRecordEvent(code->raw_instruction_start(),
426 : *source_positions));
427 :
428 1588816 : return code;
429 : }
430 :
431 15801722 : bool CodeGenerator::IsNextInAssemblyOrder(RpoNumber block) const {
432 : return instructions()
433 : ->InstructionBlockAt(current_block_)
434 15801833 : ->ao_number()
435 31603581 : .IsNext(instructions()->InstructionBlockAt(block)->ao_number());
436 : }
437 :
438 6270309 : void CodeGenerator::RecordSafepoint(ReferenceMap* references,
439 : Safepoint::Kind kind,
440 : Safepoint::DeoptMode deopt_mode) {
441 6270309 : Safepoint safepoint = safepoints()->DefineSafepoint(tasm(), kind, deopt_mode);
442 : int stackSlotToSpillSlotDelta =
443 6271507 : frame()->GetTotalFrameSlotCount() - frame()->GetSpillSlotCount();
444 87064791 : for (const InstructionOperand& operand : references->reference_operands()) {
445 80792990 : if (operand.IsStackSlot()) {
446 : int index = LocationOperand::cast(operand).index();
447 : DCHECK_LE(0, index);
448 : // We might index values in the fixed part of the frame (i.e. the
449 : // closure pointer or the context pointer); these are not spill slots
450 : // and therefore don't work with the SafepointTable currently, but
451 : // we also don't need to worry about them, since the GC has special
452 : // knowledge about those fields anyway.
453 15837749 : if (index < stackSlotToSpillSlotDelta) continue;
454 : safepoint.DefinePointerSlot(index);
455 64955241 : } else if (operand.IsRegister() && (kind & Safepoint::kWithRegisters)) {
456 0 : Register reg = LocationOperand::cast(operand).GetRegister();
457 0 : safepoint.DefinePointerRegister(reg);
458 : }
459 : }
460 6271801 : }
461 :
462 7864696 : bool CodeGenerator::IsMaterializableFromRoot(Handle<HeapObject> object,
463 : RootIndex* index_return) {
464 : const CallDescriptor* incoming_descriptor =
465 : linkage()->GetIncomingDescriptor();
466 7864696 : if (incoming_descriptor->flags() & CallDescriptor::kCanUseRoots) {
467 8927978 : return isolate()->roots_table().IsRootHandle(object, index_return) &&
468 1906132 : RootsTable::IsImmortalImmovable(*index_return);
469 : }
470 : return false;
471 : }
472 :
473 20484804 : CodeGenerator::CodeGenResult CodeGenerator::AssembleBlock(
474 : const InstructionBlock* block) {
475 158387168 : for (int i = block->code_start(); i < block->code_end(); ++i) {
476 68947781 : if (info()->trace_turbo_json_enabled()) {
477 120 : instr_starts_[i] = tasm()->pc_offset();
478 : }
479 : Instruction* instr = instructions()->InstructionAt(i);
480 68949894 : CodeGenResult result = AssembleInstruction(instr, block);
481 68950416 : if (result != kSuccess) return result;
482 : }
483 : return kSuccess;
484 : }
485 :
486 39180 : bool CodeGenerator::IsValidPush(InstructionOperand source,
487 : CodeGenerator::PushTypeFlags push_type) {
488 39180 : if (source.IsImmediate() &&
489 : ((push_type & CodeGenerator::kImmediatePush) != 0)) {
490 : return true;
491 : }
492 64516 : if (source.IsRegister() &&
493 : ((push_type & CodeGenerator::kRegisterPush) != 0)) {
494 : return true;
495 : }
496 26908 : if (source.IsStackSlot() &&
497 : ((push_type & CodeGenerator::kStackSlotPush) != 0)) {
498 : return true;
499 : }
500 780 : return false;
501 : }
502 :
503 119863 : void CodeGenerator::GetPushCompatibleMoves(Instruction* instr,
504 : PushTypeFlags push_type,
505 : ZoneVector<MoveOperands*>* pushes) {
506 : pushes->clear();
507 205386 : for (int i = Instruction::FIRST_GAP_POSITION;
508 325249 : i <= Instruction::LAST_GAP_POSITION; ++i) {
509 : Instruction::GapPosition inner_pos =
510 : static_cast<Instruction::GapPosition>(i);
511 : ParallelMove* parallel_move = instr->GetParallelMove(inner_pos);
512 222555 : if (parallel_move != nullptr) {
513 553976 : for (auto move : *parallel_move) {
514 434531 : InstructionOperand source = move->source();
515 434531 : InstructionOperand destination = move->destination();
516 : int first_push_compatible_index =
517 : V8_TARGET_ARCH_STORES_RETURN_ADDRESS_ON_STACK ? 1 : 0;
518 : // If there are any moves from slots that will be overridden by pushes,
519 : // then the full gap resolver must be used since optimization with
520 : // pushes don't participate in the parallel move and might clobber
521 : // values needed for the gap resolve.
522 465292 : if (source.IsStackSlot() && LocationOperand::cast(source).index() >=
523 : first_push_compatible_index) {
524 : pushes->clear();
525 : return;
526 : }
527 : // TODO(danno): Right now, only consider moves from the FIRST gap for
528 : // pushes. Theoretically, we could extract pushes for both gaps (there
529 : // are cases where this happens), but the logic for that would also have
530 : // to check to make sure that non-memory inputs to the pushes from the
531 : // LAST gap don't get clobbered in the FIRST gap.
532 417362 : if (i == Instruction::FIRST_GAP_POSITION) {
533 475838 : if (destination.IsStackSlot() &&
534 : LocationOperand::cast(destination).index() >=
535 : first_push_compatible_index) {
536 : int index = LocationOperand::cast(destination).index();
537 39180 : if (IsValidPush(source, push_type)) {
538 38400 : if (index >= static_cast<int>(pushes->size())) {
539 38400 : pushes->resize(index + 1);
540 : }
541 76800 : (*pushes)[index] = move;
542 : }
543 : }
544 : }
545 : }
546 : }
547 : }
548 :
549 : // For now, only support a set of continuous pushes at the end of the list.
550 : size_t push_count_upper_bound = pushes->size();
551 : size_t push_begin = push_count_upper_bound;
552 134374 : for (auto move : base::Reversed(*pushes)) {
553 44732 : if (move == nullptr) break;
554 31680 : push_begin--;
555 : }
556 102694 : size_t push_count = pushes->size() - push_begin;
557 : std::copy(pushes->begin() + push_begin,
558 : pushes->begin() + push_begin + push_count, pushes->begin());
559 102694 : pushes->resize(push_count);
560 : }
561 :
562 37876070 : CodeGenerator::MoveType::Type CodeGenerator::MoveType::InferMove(
563 : InstructionOperand* source, InstructionOperand* destination) {
564 37876070 : if (source->IsConstant()) {
565 19011758 : if (destination->IsAnyRegister()) {
566 : return MoveType::kConstantToRegister;
567 : } else {
568 : DCHECK(destination->IsAnyStackSlot());
569 45340 : return MoveType::kConstantToStack;
570 : }
571 : }
572 : DCHECK(LocationOperand::cast(source)->IsCompatible(
573 : LocationOperand::cast(destination)));
574 18864312 : if (source->IsAnyRegister()) {
575 9913723 : if (destination->IsAnyRegister()) {
576 : return MoveType::kRegisterToRegister;
577 : } else {
578 : DCHECK(destination->IsAnyStackSlot());
579 5689393 : return MoveType::kRegisterToStack;
580 : }
581 : } else {
582 : DCHECK(source->IsAnyStackSlot());
583 8950589 : if (destination->IsAnyRegister()) {
584 : return MoveType::kStackToRegister;
585 : } else {
586 : DCHECK(destination->IsAnyStackSlot());
587 50194 : return MoveType::kStackToStack;
588 : }
589 : }
590 : }
591 :
592 78744 : CodeGenerator::MoveType::Type CodeGenerator::MoveType::InferSwap(
593 : InstructionOperand* source, InstructionOperand* destination) {
594 : DCHECK(LocationOperand::cast(source)->IsCompatible(
595 : LocationOperand::cast(destination)));
596 78744 : if (source->IsAnyRegister()) {
597 75087 : if (destination->IsAnyRegister()) {
598 : return MoveType::kRegisterToRegister;
599 : } else {
600 : DCHECK(destination->IsAnyStackSlot());
601 6675 : return MoveType::kRegisterToStack;
602 : }
603 : } else {
604 : DCHECK(source->IsAnyStackSlot());
605 : DCHECK(destination->IsAnyStackSlot());
606 : return MoveType::kStackToStack;
607 : }
608 : }
609 :
610 5372538 : RpoNumber CodeGenerator::ComputeBranchInfo(BranchInfo* branch,
611 : Instruction* instr) {
612 : // Assemble a branch after this instruction.
613 : InstructionOperandConverter i(this, instr);
614 5372538 : RpoNumber true_rpo = i.InputRpo(instr->InputCount() - 2);
615 5372564 : RpoNumber false_rpo = i.InputRpo(instr->InputCount() - 1);
616 :
617 5372592 : if (true_rpo == false_rpo) {
618 2292 : return true_rpo;
619 : }
620 5370300 : FlagsCondition condition = FlagsConditionField::decode(instr->opcode());
621 5370300 : if (IsNextInAssemblyOrder(true_rpo)) {
622 : // true block is next, can fall through if condition negated.
623 : std::swap(true_rpo, false_rpo);
624 : condition = NegateFlagsCondition(condition);
625 : }
626 5370304 : branch->condition = condition;
627 5370304 : branch->true_label = GetLabel(true_rpo);
628 5370304 : branch->false_label = GetLabel(false_rpo);
629 5370304 : branch->fallthru = IsNextInAssemblyOrder(false_rpo);
630 : return RpoNumber::Invalid();
631 : }
632 :
633 68949064 : CodeGenerator::CodeGenResult CodeGenerator::AssembleInstruction(
634 : Instruction* instr, const InstructionBlock* block) {
635 : int first_unused_stack_slot;
636 68949064 : FlagsMode mode = FlagsModeField::decode(instr->opcode());
637 68949064 : if (mode != kFlags_trap) {
638 68808364 : AssembleSourcePosition(instr);
639 : }
640 : bool adjust_stack =
641 68954523 : GetSlotAboveSPBeforeTailCall(instr, &first_unused_stack_slot);
642 68956553 : if (adjust_stack) AssembleTailCallBeforeGap(instr, first_unused_stack_slot);
643 : AssembleGaps(instr);
644 68957284 : if (adjust_stack) AssembleTailCallAfterGap(instr, first_unused_stack_slot);
645 : DCHECK_IMPLIES(
646 : block->must_deconstruct_frame(),
647 : instr != instructions()->InstructionAt(block->last_instruction_index()) ||
648 : instr->IsRet() || instr->IsJump());
649 68956908 : if (instr->IsJump() && block->must_deconstruct_frame()) {
650 62062 : AssembleDeconstructFrame();
651 : }
652 : // Assemble architecture-specific code for the instruction.
653 68956908 : CodeGenResult result = AssembleArchInstruction(instr);
654 68949616 : if (result != kSuccess) return result;
655 :
656 68950828 : FlagsCondition condition = FlagsConditionField::decode(instr->opcode());
657 68950828 : switch (mode) {
658 : case kFlags_branch:
659 : case kFlags_branch_and_poison: {
660 : BranchInfo branch;
661 5372573 : RpoNumber target = ComputeBranchInfo(&branch, instr);
662 5372611 : if (target.IsValid()) {
663 : // redundant branch.
664 2292 : if (!IsNextInAssemblyOrder(target)) {
665 365 : AssembleArchJump(target);
666 : }
667 2292 : return kSuccess;
668 : }
669 : // Assemble architecture-specific branch.
670 5370319 : AssembleArchBranch(instr, &branch);
671 5370231 : break;
672 : }
673 : case kFlags_deoptimize:
674 : case kFlags_deoptimize_and_poison: {
675 : // Assemble a conditional eager deoptimization after this instruction.
676 : InstructionOperandConverter i(this, instr);
677 331497 : size_t frame_state_offset = MiscField::decode(instr->opcode());
678 : DeoptimizationExit* const exit =
679 331497 : AddDeoptimizationExit(instr, frame_state_offset);
680 331499 : Label continue_label;
681 : BranchInfo branch;
682 331499 : branch.condition = condition;
683 331499 : branch.true_label = exit->label();
684 331499 : branch.false_label = &continue_label;
685 331499 : branch.fallthru = true;
686 : // Assemble architecture-specific branch.
687 331499 : AssembleArchDeoptBranch(instr, &branch);
688 331500 : tasm()->bind(&continue_label);
689 331499 : if (mode == kFlags_deoptimize_and_poison) {
690 0 : AssembleBranchPoisoning(NegateFlagsCondition(branch.condition), instr);
691 : }
692 : break;
693 : }
694 : case kFlags_set: {
695 : // Assemble a boolean materialization after this instruction.
696 376468 : AssembleArchBoolean(instr, condition);
697 376462 : break;
698 : }
699 : case kFlags_trap: {
700 142114 : AssembleArchTrap(instr, condition);
701 142113 : break;
702 : }
703 : case kFlags_none: {
704 : break;
705 : }
706 : }
707 :
708 : // TODO(jarin) We should thread the flag through rather than set it.
709 68948482 : if (instr->IsCall()) {
710 : ResetSpeculationPoison();
711 : }
712 :
713 : return kSuccess;
714 : }
715 :
716 69039911 : void CodeGenerator::AssembleSourcePosition(Instruction* instr) {
717 69039911 : SourcePosition source_position = SourcePosition::Unknown();
718 133326288 : if (instr->IsNop() && instr->AreMovesRedundant()) return;
719 43285506 : if (!instructions()->GetSourcePosition(instr, &source_position)) return;
720 4757807 : AssembleSourcePosition(source_position);
721 : }
722 :
723 4769192 : void CodeGenerator::AssembleSourcePosition(SourcePosition source_position) {
724 4769192 : if (source_position == current_source_position_) return;
725 3639863 : current_source_position_ = source_position;
726 3639863 : if (!source_position.IsKnown()) return;
727 3639958 : source_position_table_builder_.AddPosition(tasm()->pc_offset(),
728 3639958 : source_position, false);
729 3640297 : if (FLAG_code_comments) {
730 : OptimizedCompilationInfo* info = this->info();
731 1163 : if (info->IsNotOptimizedFunctionOrWasmFunction()) return;
732 2326 : std::ostringstream buffer;
733 1163 : buffer << "-- ";
734 : // Turbolizer only needs the source position, as it can reconstruct
735 : // the inlining stack from other information.
736 1163 : if (info->trace_turbo_json_enabled() || !tasm()->isolate() ||
737 : tasm()->isolate()->concurrent_recompilation_enabled()) {
738 169 : buffer << source_position;
739 : } else {
740 : AllowHeapAllocation allocation;
741 : AllowHandleAllocation handles;
742 : AllowHandleDereference deref;
743 1988 : buffer << source_position.InliningStack(info);
744 : }
745 1163 : buffer << " --";
746 2326 : tasm()->RecordComment(buffer.str().c_str());
747 : }
748 : }
749 :
750 68954546 : bool CodeGenerator::GetSlotAboveSPBeforeTailCall(Instruction* instr,
751 : int* slot) {
752 68954546 : if (instr->IsTailCall()) {
753 : InstructionOperandConverter g(this, instr);
754 239700 : *slot = g.InputInt32(instr->InputCount() - 1);
755 : return true;
756 : } else {
757 : return false;
758 : }
759 : }
760 :
761 569369 : StubCallMode CodeGenerator::DetermineStubCallMode() const {
762 : Code::Kind code_kind = info()->code_kind();
763 569369 : return (code_kind == Code::WASM_FUNCTION ||
764 : code_kind == Code::WASM_TO_JS_FUNCTION)
765 : ? StubCallMode::kCallWasmRuntimeStub
766 569369 : : StubCallMode::kCallCodeObject;
767 : }
768 :
769 12 : void CodeGenerator::AssembleGaps(Instruction* instr) {
770 137909685 : for (int i = Instruction::FIRST_GAP_POSITION;
771 206860522 : i <= Instruction::LAST_GAP_POSITION; i++) {
772 : Instruction::GapPosition inner_pos =
773 : static_cast<Instruction::GapPosition>(i);
774 : ParallelMove* move = instr->GetParallelMove(inner_pos);
775 186809474 : if (move != nullptr) resolver()->Resolve(move);
776 : }
777 12 : }
778 :
779 : namespace {
780 :
781 463493 : Handle<PodArray<InliningPosition>> CreateInliningPositions(
782 : OptimizedCompilationInfo* info, Isolate* isolate) {
783 : const OptimizedCompilationInfo::InlinedFunctionList& inlined_functions =
784 : info->inlined_functions();
785 463493 : if (inlined_functions.size() == 0) {
786 : return Handle<PodArray<InliningPosition>>::cast(
787 : isolate->factory()->empty_byte_array());
788 : }
789 : Handle<PodArray<InliningPosition>> inl_positions =
790 : PodArray<InliningPosition>::New(
791 : isolate, static_cast<int>(inlined_functions.size()),
792 13707 : AllocationType::kOld);
793 144885 : for (size_t i = 0; i < inlined_functions.size(); ++i) {
794 65589 : inl_positions->set(static_cast<int>(i), inlined_functions[i].position);
795 : }
796 13707 : return inl_positions;
797 : }
798 :
799 : } // namespace
800 :
801 1588812 : Handle<DeoptimizationData> CodeGenerator::GenerateDeoptimizationData() {
802 : OptimizedCompilationInfo* info = this->info();
803 1588812 : int deopt_count = static_cast<int>(deoptimization_states_.size());
804 2714133 : if (deopt_count == 0 && !info->is_osr()) {
805 1125319 : return DeoptimizationData::Empty(isolate());
806 : }
807 : Handle<DeoptimizationData> data =
808 463493 : DeoptimizationData::New(isolate(), deopt_count, AllocationType::kOld);
809 :
810 : Handle<ByteArray> translation_array =
811 463492 : translations_.CreateByteArray(isolate()->factory());
812 :
813 926985 : data->SetTranslationByteArray(*translation_array);
814 463493 : data->SetInlinedFunctionCount(
815 463493 : Smi::FromInt(static_cast<int>(inlined_function_count_)));
816 : data->SetOptimizationId(Smi::FromInt(info->optimization_id()));
817 :
818 463493 : if (info->has_shared_info()) {
819 926986 : data->SetSharedFunctionInfo(*info->shared_info());
820 : } else {
821 0 : data->SetSharedFunctionInfo(Smi::kZero);
822 : }
823 :
824 : Handle<FixedArray> literals = isolate()->factory()->NewFixedArray(
825 463493 : static_cast<int>(deoptimization_literals_.size()), AllocationType::kOld);
826 6337843 : for (unsigned i = 0; i < deoptimization_literals_.size(); i++) {
827 1803619 : Handle<Object> object = deoptimization_literals_[i].Reify(isolate());
828 3607238 : literals->set(i, *object);
829 : }
830 926986 : data->SetLiteralArray(*literals);
831 :
832 : Handle<PodArray<InliningPosition>> inl_pos =
833 463493 : CreateInliningPositions(info, isolate());
834 926986 : data->SetInliningPositions(*inl_pos);
835 :
836 463493 : if (info->is_osr()) {
837 : DCHECK_LE(0, osr_pc_offset_);
838 4652 : data->SetOsrBytecodeOffset(Smi::FromInt(info_->osr_offset().ToInt()));
839 4652 : data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
840 : } else {
841 : BailoutId osr_offset = BailoutId::None();
842 : data->SetOsrBytecodeOffset(Smi::FromInt(osr_offset.ToInt()));
843 : data->SetOsrPcOffset(Smi::FromInt(-1));
844 : }
845 :
846 : // Populate deoptimization entries.
847 7121749 : for (int i = 0; i < deopt_count; i++) {
848 3329128 : DeoptimizationState* deoptimization_state = deoptimization_states_[i];
849 : data->SetBytecodeOffset(i, deoptimization_state->bailout_id());
850 3329128 : CHECK(deoptimization_state);
851 : data->SetTranslationIndex(
852 : i, Smi::FromInt(deoptimization_state->translation_id()));
853 : data->SetPc(i, Smi::FromInt(deoptimization_state->pc_offset()));
854 : }
855 :
856 463493 : return data;
857 : }
858 :
859 344 : Label* CodeGenerator::AddJumpTable(Label** targets, size_t target_count) {
860 688 : jump_tables_ = new (zone()) JumpTable(jump_tables_, targets, target_count);
861 344 : return jump_tables_->label();
862 : }
863 :
864 6040644 : void CodeGenerator::RecordCallPosition(Instruction* instr) {
865 6040644 : CallDescriptor::Flags flags(MiscField::decode(instr->opcode()));
866 :
867 : bool needs_frame_state = (flags & CallDescriptor::kNeedsFrameState);
868 :
869 6040644 : RecordSafepoint(
870 : instr->reference_map(), Safepoint::kSimple,
871 6040644 : needs_frame_state ? Safepoint::kLazyDeopt : Safepoint::kNoLazyDeopt);
872 :
873 6040988 : if (flags & CallDescriptor::kHasExceptionHandler) {
874 : InstructionOperandConverter i(this, instr);
875 226613 : RpoNumber handler_rpo = i.InputRpo(instr->InputCount() - 1);
876 679839 : handlers_.push_back({GetLabel(handler_rpo), tasm()->pc_offset()});
877 : }
878 :
879 6040986 : if (needs_frame_state) {
880 : MarkLazyDeoptSite();
881 : // If the frame state is present, it starts at argument 2 - after
882 : // the code address and the poison-alias index.
883 : size_t frame_state_offset = 2;
884 : FrameStateDescriptor* descriptor =
885 2956234 : GetDeoptimizationEntry(instr, frame_state_offset).descriptor();
886 : int pc_offset = tasm()->pc_offset();
887 : int deopt_state_id = BuildTranslation(instr, pc_offset, frame_state_offset,
888 2956234 : descriptor->state_combine());
889 :
890 : DeoptimizationExit* const exit = new (zone())
891 5912463 : DeoptimizationExit(deopt_state_id, current_source_position_);
892 2956231 : deoptimization_exits_.push_back(exit);
893 2956232 : safepoints()->RecordLazyDeoptimizationIndex(deopt_state_id);
894 : }
895 6040984 : }
896 :
897 7764010 : int CodeGenerator::DefineDeoptimizationLiteral(DeoptimizationLiteral literal) {
898 7764010 : int result = static_cast<int>(deoptimization_literals_.size());
899 150874910 : for (unsigned i = 0; i < deoptimization_literals_.size(); ++i) {
900 51074753 : if (deoptimization_literals_[i] == literal) return i;
901 : }
902 1804951 : deoptimization_literals_.push_back(literal);
903 1804951 : return result;
904 : }
905 :
906 6288654 : DeoptimizationEntry const& CodeGenerator::GetDeoptimizationEntry(
907 : Instruction* instr, size_t frame_state_offset) {
908 : InstructionOperandConverter i(this, instr);
909 : int const state_id = i.InputInt32(frame_state_offset);
910 6288654 : return instructions()->GetDeoptimizationEntry(state_id);
911 : }
912 :
913 0 : DeoptimizeKind CodeGenerator::GetDeoptimizationKind(
914 : int deoptimization_id) const {
915 : size_t const index = static_cast<size_t>(deoptimization_id);
916 : DCHECK_LT(index, deoptimization_states_.size());
917 3332423 : return deoptimization_states_[index]->kind();
918 : }
919 :
920 0 : DeoptimizeReason CodeGenerator::GetDeoptimizationReason(
921 : int deoptimization_id) const {
922 : size_t const index = static_cast<size_t>(deoptimization_id);
923 : DCHECK_LT(index, deoptimization_states_.size());
924 3332423 : return deoptimization_states_[index]->reason();
925 : }
926 :
927 49835766 : void CodeGenerator::TranslateStateValueDescriptor(
928 : StateValueDescriptor* desc, StateValueList* nested,
929 : Translation* translation, InstructionOperandIterator* iter) {
930 : // Note:
931 : // If translation is null, we just skip the relevant instruction operands.
932 49835766 : if (desc->IsNested()) {
933 90390 : if (translation != nullptr) {
934 90390 : translation->BeginCapturedObject(static_cast<int>(nested->size()));
935 : }
936 628409 : for (auto field : *nested) {
937 : TranslateStateValueDescriptor(field.desc, field.nested, translation,
938 538019 : iter);
939 : }
940 49745376 : } else if (desc->IsArgumentsElements()) {
941 5983 : if (translation != nullptr) {
942 5983 : translation->ArgumentsElements(desc->arguments_type());
943 : }
944 49739393 : } else if (desc->IsArgumentsLength()) {
945 6291 : if (translation != nullptr) {
946 6291 : translation->ArgumentsLength(desc->arguments_type());
947 : }
948 49733102 : } else if (desc->IsDuplicate()) {
949 19983 : if (translation != nullptr) {
950 19983 : translation->DuplicateObject(static_cast<int>(desc->id()));
951 : }
952 49713119 : } else if (desc->IsPlain()) {
953 : InstructionOperand* op = iter->Advance();
954 21982239 : if (translation != nullptr) {
955 : AddTranslationForOperand(translation, iter->instruction(), op,
956 21982259 : desc->type());
957 : }
958 : } else {
959 : DCHECK(desc->IsOptimizedOut());
960 27730880 : if (translation != nullptr) {
961 27730889 : if (optimized_out_literal_id_ == -1) {
962 926907 : optimized_out_literal_id_ = DefineDeoptimizationLiteral(
963 463453 : DeoptimizationLiteral(isolate()->factory()->optimized_out()));
964 : }
965 27730888 : translation->StoreLiteral(optimized_out_literal_id_);
966 : }
967 : }
968 49835646 : }
969 :
970 3689381 : void CodeGenerator::TranslateFrameStateDescriptorOperands(
971 : FrameStateDescriptor* desc, InstructionOperandIterator* iter,
972 : Translation* translation) {
973 : size_t index = 0;
974 : StateValueList* values = desc->GetStateValueDescriptors();
975 102283885 : for (StateValueList::iterator it = values->begin(); it != values->end();
976 : ++it, ++index) {
977 49297299 : TranslateStateValueDescriptor((*it).desc, (*it).nested, translation, iter);
978 : }
979 : DCHECK_EQ(desc->GetSize(), index);
980 3689334 : }
981 :
982 3689333 : void CodeGenerator::BuildTranslationForFrameStateDescriptor(
983 : FrameStateDescriptor* descriptor, InstructionOperandIterator* iter,
984 : Translation* translation, OutputFrameStateCombine state_combine) {
985 : // Outer-most state must be added to translation first.
986 3689333 : if (descriptor->outer_state() != nullptr) {
987 : BuildTranslationForFrameStateDescriptor(descriptor->outer_state(), iter,
988 356915 : translation, state_combine);
989 : }
990 :
991 : Handle<SharedFunctionInfo> shared_info;
992 3689330 : if (!descriptor->shared_info().ToHandle(&shared_info)) {
993 2085 : if (!info()->has_shared_info()) {
994 : return; // Stub with no SharedFunctionInfo.
995 : }
996 : shared_info = info()->shared_info();
997 : }
998 : int shared_info_id =
999 3689330 : DefineDeoptimizationLiteral(DeoptimizationLiteral(shared_info));
1000 :
1001 3689336 : switch (descriptor->type()) {
1002 : case FrameStateType::kInterpretedFunction: {
1003 : int return_offset = 0;
1004 : int return_count = 0;
1005 3562848 : if (!state_combine.IsOutputIgnored()) {
1006 2119122 : return_offset = static_cast<int>(state_combine.GetOffsetToPokeAt());
1007 2119122 : return_count = static_cast<int>(iter->instruction()->OutputCount());
1008 : }
1009 3562848 : translation->BeginInterpretedFrame(
1010 : descriptor->bailout_id(), shared_info_id,
1011 : static_cast<unsigned int>(descriptor->locals_count() + 1),
1012 3562848 : return_offset, return_count);
1013 3562847 : break;
1014 : }
1015 : case FrameStateType::kArgumentsAdaptor:
1016 85071 : translation->BeginArgumentsAdaptorFrame(
1017 : shared_info_id,
1018 85071 : static_cast<unsigned int>(descriptor->parameters_count()));
1019 85071 : break;
1020 : case FrameStateType::kConstructStub:
1021 : DCHECK(descriptor->bailout_id().IsValidForConstructStub());
1022 26702 : translation->BeginConstructStubFrame(
1023 : descriptor->bailout_id(), shared_info_id,
1024 26702 : static_cast<unsigned int>(descriptor->parameters_count() + 1));
1025 26702 : break;
1026 : case FrameStateType::kBuiltinContinuation: {
1027 2085 : BailoutId bailout_id = descriptor->bailout_id();
1028 : int parameter_count =
1029 : static_cast<unsigned int>(descriptor->parameters_count());
1030 2085 : translation->BeginBuiltinContinuationFrame(bailout_id, shared_info_id,
1031 2085 : parameter_count);
1032 : break;
1033 : }
1034 : case FrameStateType::kJavaScriptBuiltinContinuation: {
1035 12355 : BailoutId bailout_id = descriptor->bailout_id();
1036 : int parameter_count =
1037 : static_cast<unsigned int>(descriptor->parameters_count());
1038 12355 : translation->BeginJavaScriptBuiltinContinuationFrame(
1039 12355 : bailout_id, shared_info_id, parameter_count);
1040 : break;
1041 : }
1042 : case FrameStateType::kJavaScriptBuiltinContinuationWithCatch: {
1043 274 : BailoutId bailout_id = descriptor->bailout_id();
1044 : int parameter_count =
1045 : static_cast<unsigned int>(descriptor->parameters_count());
1046 274 : translation->BeginJavaScriptBuiltinContinuationWithCatchFrame(
1047 274 : bailout_id, shared_info_id, parameter_count);
1048 : break;
1049 : }
1050 : }
1051 :
1052 3689335 : TranslateFrameStateDescriptorOperands(descriptor, iter, translation);
1053 : }
1054 :
1055 3332423 : int CodeGenerator::BuildTranslation(Instruction* instr, int pc_offset,
1056 : size_t frame_state_offset,
1057 : OutputFrameStateCombine state_combine) {
1058 : DeoptimizationEntry const& entry =
1059 3332423 : GetDeoptimizationEntry(instr, frame_state_offset);
1060 : FrameStateDescriptor* const descriptor = entry.descriptor();
1061 3332425 : frame_state_offset++;
1062 :
1063 3332425 : int update_feedback_count = entry.feedback().IsValid() ? 1 : 0;
1064 : Translation translation(&translations_,
1065 3332422 : static_cast<int>(descriptor->GetFrameCount()),
1066 3332425 : static_cast<int>(descriptor->GetJSFrameCount()),
1067 6664844 : update_feedback_count, zone());
1068 3332418 : if (entry.feedback().IsValid()) {
1069 : DeoptimizationLiteral literal =
1070 : DeoptimizationLiteral(entry.feedback().vector());
1071 40460 : int literal_id = DefineDeoptimizationLiteral(literal);
1072 40460 : translation.AddUpdateFeedback(literal_id, entry.feedback().slot().ToInt());
1073 : }
1074 : InstructionOperandIterator iter(instr, frame_state_offset);
1075 : BuildTranslationForFrameStateDescriptor(descriptor, &iter, &translation,
1076 3332418 : state_combine);
1077 :
1078 3332424 : int deoptimization_id = static_cast<int>(deoptimization_states_.size());
1079 :
1080 6664842 : deoptimization_states_.push_back(new (zone()) DeoptimizationState(
1081 : descriptor->bailout_id(), translation.index(), pc_offset, entry.kind(),
1082 : entry.reason()));
1083 :
1084 3332419 : return deoptimization_id;
1085 : }
1086 :
1087 21982435 : void CodeGenerator::AddTranslationForOperand(Translation* translation,
1088 : Instruction* instr,
1089 : InstructionOperand* op,
1090 : MachineType type) {
1091 21982435 : if (op->IsStackSlot()) {
1092 14814225 : if (type.representation() == MachineRepresentation::kBit) {
1093 21491 : translation->StoreBoolStackSlot(LocationOperand::cast(op)->index());
1094 44377172 : } else if (type == MachineType::Int8() || type == MachineType::Int16() ||
1095 : type == MachineType::Int32()) {
1096 214701 : translation->StoreInt32StackSlot(LocationOperand::cast(op)->index());
1097 43734107 : } else if (type == MachineType::Uint8() || type == MachineType::Uint16() ||
1098 : type == MachineType::Uint32()) {
1099 7394 : translation->StoreUint32StackSlot(LocationOperand::cast(op)->index());
1100 14570639 : } else if (type == MachineType::Int64()) {
1101 114 : translation->StoreInt64StackSlot(LocationOperand::cast(op)->index());
1102 : } else {
1103 : #if defined(V8_COMPRESS_POINTERS)
1104 : CHECK(MachineRepresentation::kTagged == type.representation() ||
1105 : MachineRepresentation::kCompressed == type.representation());
1106 : #else
1107 14570525 : CHECK(MachineRepresentation::kTagged == type.representation());
1108 : #endif
1109 14570525 : translation->StoreStackSlot(LocationOperand::cast(op)->index());
1110 : }
1111 7168210 : } else if (op->IsFPStackSlot()) {
1112 258886 : if (type.representation() == MachineRepresentation::kFloat64) {
1113 257950 : translation->StoreDoubleStackSlot(LocationOperand::cast(op)->index());
1114 : } else {
1115 936 : CHECK_EQ(MachineRepresentation::kFloat32, type.representation());
1116 936 : translation->StoreFloatStackSlot(LocationOperand::cast(op)->index());
1117 : }
1118 6909324 : } else if (op->IsRegister()) {
1119 : InstructionOperandConverter converter(this, instr);
1120 503195 : if (type.representation() == MachineRepresentation::kBit) {
1121 3295 : translation->StoreBoolRegister(converter.ToRegister(op));
1122 1499116 : } else if (type == MachineType::Int8() || type == MachineType::Int16() ||
1123 : type == MachineType::Int32()) {
1124 35187 : translation->StoreInt32Register(converter.ToRegister(op));
1125 1394139 : } else if (type == MachineType::Uint8() || type == MachineType::Uint16() ||
1126 : type == MachineType::Uint32()) {
1127 1227 : translation->StoreUint32Register(converter.ToRegister(op));
1128 463486 : } else if (type == MachineType::Int64()) {
1129 14 : translation->StoreInt64Register(converter.ToRegister(op));
1130 : } else {
1131 : #if defined(V8_COMPRESS_POINTERS)
1132 : CHECK(MachineRepresentation::kTagged == type.representation() ||
1133 : MachineRepresentation::kCompressed == type.representation());
1134 : #else
1135 463472 : CHECK(MachineRepresentation::kTagged == type.representation());
1136 : #endif
1137 463472 : translation->StoreRegister(converter.ToRegister(op));
1138 : }
1139 6406129 : } else if (op->IsFPRegister()) {
1140 : InstructionOperandConverter converter(this, instr);
1141 63767 : if (type.representation() == MachineRepresentation::kFloat64) {
1142 63639 : translation->StoreDoubleRegister(converter.ToDoubleRegister(op));
1143 : } else {
1144 128 : CHECK_EQ(MachineRepresentation::kFloat32, type.representation());
1145 128 : translation->StoreFloatRegister(converter.ToFloatRegister(op));
1146 : }
1147 : } else {
1148 6342362 : CHECK(op->IsImmediate());
1149 : InstructionOperandConverter converter(this, instr);
1150 6342362 : Constant constant = converter.ToConstant(op);
1151 : DeoptimizationLiteral literal;
1152 6342388 : switch (constant.type()) {
1153 : case Constant::kInt32:
1154 12827 : if (type.representation() == MachineRepresentation::kTagged) {
1155 : // When pointers are 4 bytes, we can use int32 constants to represent
1156 : // Smis.
1157 : DCHECK_EQ(4, kSystemPointerSize);
1158 11 : Smi smi(static_cast<Address>(constant.ToInt32()));
1159 : DCHECK(smi->IsSmi());
1160 11 : literal = DeoptimizationLiteral(smi->value());
1161 12816 : } else if (type.representation() == MachineRepresentation::kBit) {
1162 783 : if (constant.ToInt32() == 0) {
1163 : literal =
1164 340 : DeoptimizationLiteral(isolate()->factory()->false_value());
1165 : } else {
1166 : DCHECK_EQ(1, constant.ToInt32());
1167 443 : literal = DeoptimizationLiteral(isolate()->factory()->true_value());
1168 : }
1169 : } else {
1170 : DCHECK(type == MachineType::Int32() ||
1171 : type == MachineType::Uint32() ||
1172 : type.representation() == MachineRepresentation::kWord32 ||
1173 : type.representation() == MachineRepresentation::kNone);
1174 : DCHECK(type.representation() != MachineRepresentation::kNone ||
1175 : constant.ToInt32() == FrameStateDescriptor::kImpossibleValue);
1176 12033 : if (type == MachineType::Uint32()) {
1177 : literal = DeoptimizationLiteral(
1178 8 : static_cast<uint32_t>(constant.ToInt32()));
1179 : } else {
1180 12025 : literal = DeoptimizationLiteral(constant.ToInt32());
1181 : }
1182 : }
1183 : break;
1184 : case Constant::kInt64:
1185 : DCHECK_EQ(8, kSystemPointerSize);
1186 563757 : if (type.representation() == MachineRepresentation::kWord64) {
1187 : literal =
1188 0 : DeoptimizationLiteral(static_cast<double>(constant.ToInt64()));
1189 : } else {
1190 : // When pointers are 8 bytes, we can use int64 constants to represent
1191 : // Smis.
1192 : DCHECK_EQ(MachineRepresentation::kTagged, type.representation());
1193 : Smi smi(static_cast<Address>(constant.ToInt64()));
1194 : DCHECK(smi->IsSmi());
1195 563757 : literal = DeoptimizationLiteral(smi->value());
1196 : }
1197 : break;
1198 : case Constant::kFloat32:
1199 : DCHECK(type.representation() == MachineRepresentation::kFloat32 ||
1200 : type.representation() == MachineRepresentation::kTagged);
1201 14 : literal = DeoptimizationLiteral(constant.ToFloat32());
1202 14 : break;
1203 : case Constant::kFloat64:
1204 : DCHECK(type.representation() == MachineRepresentation::kFloat64 ||
1205 : type.representation() == MachineRepresentation::kTagged);
1206 89670 : literal = DeoptimizationLiteral(constant.ToFloat64().value());
1207 89670 : break;
1208 : case Constant::kHeapObject:
1209 : DCHECK_EQ(MachineRepresentation::kTagged, type.representation());
1210 5673586 : literal = DeoptimizationLiteral(constant.ToHeapObject());
1211 5673587 : break;
1212 : case Constant::kDelayedStringConstant:
1213 : DCHECK_EQ(MachineRepresentation::kTagged, type.representation());
1214 2534 : literal = DeoptimizationLiteral(constant.ToDelayedStringConstant());
1215 2534 : break;
1216 : default:
1217 0 : UNREACHABLE();
1218 : }
1219 6342389 : if (literal.object().equals(info()->closure())) {
1220 3366653 : translation->StoreJSFrameFunction();
1221 : } else {
1222 2975736 : int literal_id = DefineDeoptimizationLiteral(literal);
1223 2975725 : translation->StoreLiteral(literal_id);
1224 : }
1225 : }
1226 21982419 : }
1227 :
1228 0 : void CodeGenerator::MarkLazyDeoptSite() {
1229 2956234 : last_lazy_deopt_pc_ = tasm()->pc_offset();
1230 0 : }
1231 :
1232 331497 : DeoptimizationExit* CodeGenerator::AddDeoptimizationExit(
1233 : Instruction* instr, size_t frame_state_offset) {
1234 : int const deoptimization_id = BuildTranslation(
1235 331497 : instr, -1, frame_state_offset, OutputFrameStateCombine::Ignore());
1236 :
1237 : DeoptimizationExit* const exit = new (zone())
1238 662999 : DeoptimizationExit(deoptimization_id, current_source_position_);
1239 331499 : deoptimization_exits_.push_back(exit);
1240 331499 : return exit;
1241 : }
1242 :
1243 2638169 : void CodeGenerator::InitializeSpeculationPoison() {
1244 2638169 : if (poisoning_level_ == PoisoningMitigationLevel::kDontPoison) return;
1245 :
1246 : // Initialize {kSpeculationPoisonRegister} either by comparing the expected
1247 : // with the actual call target, or by unconditionally using {-1} initially.
1248 : // Masking register arguments with it only makes sense in the first case.
1249 0 : if (info()->called_with_code_start_register()) {
1250 0 : tasm()->RecordComment("-- Prologue: generate speculation poison --");
1251 0 : GenerateSpeculationPoisonFromCodeStartRegister();
1252 0 : if (info()->is_poisoning_register_arguments()) {
1253 0 : AssembleRegisterArgumentPoisoning();
1254 : }
1255 : } else {
1256 : ResetSpeculationPoison();
1257 : }
1258 : }
1259 :
1260 4652 : void CodeGenerator::ResetSpeculationPoison() {
1261 6074704 : if (poisoning_level_ != PoisoningMitigationLevel::kDontPoison) {
1262 0 : tasm()->ResetSpeculationPoisonRegister();
1263 : }
1264 4652 : }
1265 :
1266 761504 : OutOfLineCode::OutOfLineCode(CodeGenerator* gen)
1267 2284512 : : frame_(gen->frame()), tasm_(gen->tasm()), next_(gen->ools_) {
1268 761504 : gen->ools_ = this;
1269 761504 : }
1270 :
1271 : OutOfLineCode::~OutOfLineCode() = default;
1272 :
1273 1803619 : Handle<Object> DeoptimizationLiteral::Reify(Isolate* isolate) const {
1274 1803619 : switch (kind_) {
1275 : case DeoptimizationLiteralKind::kObject: {
1276 1723605 : return object_;
1277 : }
1278 : case DeoptimizationLiteralKind::kNumber: {
1279 79378 : return isolate->factory()->NewNumber(number_);
1280 : }
1281 : case DeoptimizationLiteralKind::kString: {
1282 636 : return string_->AllocateStringConstant(isolate);
1283 : }
1284 : }
1285 0 : UNREACHABLE();
1286 : }
1287 :
1288 : } // namespace compiler
1289 : } // namespace internal
1290 122028 : } // namespace v8
|