Line data Source code
1 : // Copyright 2014 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 : #ifndef V8_COMPILER_LINKAGE_H_
6 : #define V8_COMPILER_LINKAGE_H_
7 :
8 : #include "src/base/compiler-specific.h"
9 : #include "src/base/flags.h"
10 : #include "src/compiler/frame.h"
11 : #include "src/compiler/operator.h"
12 : #include "src/globals.h"
13 : #include "src/interface-descriptors.h"
14 : #include "src/machine-type.h"
15 : #include "src/register-arch.h"
16 : #include "src/reglist.h"
17 : #include "src/runtime/runtime.h"
18 : #include "src/signature.h"
19 : #include "src/zone/zone.h"
20 :
21 : namespace v8 {
22 : namespace internal {
23 :
24 : class CallInterfaceDescriptor;
25 : class OptimizedCompilationInfo;
26 :
27 : namespace compiler {
28 :
29 : const RegList kNoCalleeSaved = 0;
30 :
31 : class Node;
32 : class OsrHelper;
33 :
34 : // Describes the location for a parameter or a return value to a call.
35 : class LinkageLocation {
36 : public:
37 : bool operator==(const LinkageLocation& other) const {
38 0 : return bit_field_ == other.bit_field_;
39 : }
40 :
41 : bool operator!=(const LinkageLocation& other) const {
42 : return !(*this == other);
43 : }
44 :
45 : static LinkageLocation ForAnyRegister(
46 : MachineType type = MachineType::None()) {
47 : return LinkageLocation(REGISTER, ANY_REGISTER, type);
48 : }
49 :
50 : static LinkageLocation ForRegister(int32_t reg,
51 : MachineType type = MachineType::None()) {
52 : DCHECK_LE(0, reg);
53 : return LinkageLocation(REGISTER, reg, type);
54 : }
55 :
56 : static LinkageLocation ForCallerFrameSlot(int32_t slot, MachineType type) {
57 : DCHECK_GT(0, slot);
58 : return LinkageLocation(STACK_SLOT, slot, type);
59 : }
60 :
61 : static LinkageLocation ForCalleeFrameSlot(int32_t slot, MachineType type) {
62 : // TODO(titzer): bailout instead of crashing here.
63 : DCHECK(slot >= 0 && slot < LinkageLocation::MAX_STACK_SLOT);
64 : return LinkageLocation(STACK_SLOT, slot, type);
65 : }
66 :
67 : static LinkageLocation ForSavedCallerReturnAddress() {
68 : return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
69 : StandardFrameConstants::kCallerPCOffset) /
70 : kSystemPointerSize,
71 : MachineType::Pointer());
72 : }
73 :
74 : static LinkageLocation ForSavedCallerFramePtr() {
75 : return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
76 : StandardFrameConstants::kCallerFPOffset) /
77 : kSystemPointerSize,
78 : MachineType::Pointer());
79 : }
80 :
81 : static LinkageLocation ForSavedCallerConstantPool() {
82 : DCHECK(V8_EMBEDDED_CONSTANT_POOL);
83 : return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
84 : StandardFrameConstants::kConstantPoolOffset) /
85 : kSystemPointerSize,
86 : MachineType::AnyTagged());
87 : }
88 :
89 : static LinkageLocation ForSavedCallerFunction() {
90 : return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
91 : StandardFrameConstants::kFunctionOffset) /
92 : kSystemPointerSize,
93 : MachineType::AnyTagged());
94 : }
95 :
96 : static LinkageLocation ConvertToTailCallerLocation(
97 : LinkageLocation caller_location, int stack_param_delta) {
98 514927 : if (!caller_location.IsRegister()) {
99 : return LinkageLocation(STACK_SLOT,
100 : caller_location.GetLocation() + stack_param_delta,
101 58441 : caller_location.GetType());
102 : }
103 : return caller_location;
104 : }
105 :
106 : MachineType GetType() const { return machine_type_; }
107 :
108 : int GetSize() const {
109 159263 : return 1 << ElementSizeLog2Of(GetType().representation());
110 : }
111 :
112 : int GetSizeInPointers() const {
113 : // Round up
114 159269 : return (GetSize() + kSystemPointerSize - 1) / kSystemPointerSize;
115 : }
116 :
117 : int32_t GetLocation() const {
118 : // We can't use LocationField::decode here because it doesn't work for
119 : // negative values!
120 : return static_cast<int32_t>(bit_field_ & LocationField::kMask) >>
121 63746252 : LocationField::kShift;
122 : }
123 :
124 40343303 : bool IsRegister() const { return TypeField::decode(bit_field_) == REGISTER; }
125 : bool IsAnyRegister() const {
126 55795906 : return IsRegister() && GetLocation() == ANY_REGISTER;
127 : }
128 39722888 : bool IsCallerFrameSlot() const { return !IsRegister() && GetLocation() < 0; }
129 25950841 : bool IsCalleeFrameSlot() const { return !IsRegister() && GetLocation() >= 0; }
130 :
131 851616 : int32_t AsRegister() const {
132 : DCHECK(IsRegister());
133 : return GetLocation();
134 : }
135 : int32_t AsCallerFrameSlot() const {
136 : DCHECK(IsCallerFrameSlot());
137 : return GetLocation();
138 : }
139 : int32_t AsCalleeFrameSlot() const {
140 : DCHECK(IsCalleeFrameSlot());
141 : return GetLocation();
142 : }
143 :
144 : private:
145 : enum LocationType { REGISTER, STACK_SLOT };
146 :
147 : class TypeField : public BitField<LocationType, 0, 1> {};
148 : class LocationField : public BitField<int32_t, TypeField::kNext, 31> {};
149 :
150 : static constexpr int32_t ANY_REGISTER = -1;
151 : static constexpr int32_t MAX_STACK_SLOT = 32767;
152 :
153 : LinkageLocation(LocationType type, int32_t location,
154 : MachineType machine_type) {
155 4702667 : bit_field_ = TypeField::encode(type) |
156 23504243 : ((location << LocationField::kShift) & LocationField::kMask);
157 : machine_type_ = machine_type;
158 : }
159 :
160 : int32_t bit_field_;
161 : MachineType machine_type_;
162 : };
163 :
164 : typedef Signature<LinkageLocation> LocationSignature;
165 :
166 : // Describes a call to various parts of the compiler. Every call has the notion
167 : // of a "target", which is the first input to the call.
168 : class V8_EXPORT_PRIVATE CallDescriptor final
169 : : public NON_EXPORTED_BASE(ZoneObject) {
170 : public:
171 : // Describes the kind of this call, which determines the target.
172 : enum Kind {
173 : kCallCodeObject, // target is a Code object
174 : kCallJSFunction, // target is a JSFunction object
175 : kCallAddress, // target is a machine pointer
176 : kCallWasmFunction, // target is a wasm function
177 : kCallWasmImportWrapper, // target is a wasm import wrapper
178 : kCallBuiltinPointer, // target is a builtin pointer
179 : };
180 :
181 : enum Flag {
182 : kNoFlags = 0u,
183 : kNeedsFrameState = 1u << 0,
184 : kHasExceptionHandler = 1u << 1,
185 : kCanUseRoots = 1u << 2,
186 : // Causes the code generator to initialize the root register.
187 : kInitializeRootRegister = 1u << 3,
188 : // Does not ever try to allocate space on our heap.
189 : kNoAllocate = 1u << 4,
190 : // Push argument count as part of function prologue.
191 : kPushArgumentCount = 1u << 5,
192 : // Use retpoline for this call if indirect.
193 : kRetpoline = 1u << 6,
194 : // Use the kJavaScriptCallCodeStartRegister (fixed) register for the
195 : // indirect target address when calling.
196 : kFixedTargetRegister = 1u << 7,
197 : kAllowCallThroughSlot = 1u << 8
198 : };
199 : typedef base::Flags<Flag> Flags;
200 :
201 : CallDescriptor(Kind kind, MachineType target_type, LinkageLocation target_loc,
202 : LocationSignature* location_sig, size_t stack_param_count,
203 : Operator::Properties properties,
204 : RegList callee_saved_registers,
205 : RegList callee_saved_fp_registers, Flags flags,
206 : const char* debug_name = "",
207 : const RegList allocatable_registers = 0,
208 : size_t stack_return_count = 0)
209 : : kind_(kind),
210 : target_type_(target_type),
211 : target_loc_(target_loc),
212 : location_sig_(location_sig),
213 : stack_param_count_(stack_param_count),
214 : stack_return_count_(stack_return_count),
215 : properties_(properties),
216 : callee_saved_registers_(callee_saved_registers),
217 : callee_saved_fp_registers_(callee_saved_fp_registers),
218 : allocatable_registers_(allocatable_registers),
219 : flags_(flags),
220 8695805 : debug_name_(debug_name) {}
221 :
222 : // Returns the kind of this call.
223 : Kind kind() const { return kind_; }
224 :
225 : // Returns {true} if this descriptor is a call to a C function.
226 : bool IsCFunctionCall() const { return kind_ == kCallAddress; }
227 :
228 : // Returns {true} if this descriptor is a call to a JSFunction.
229 180 : bool IsJSFunctionCall() const { return kind_ == kCallJSFunction; }
230 :
231 : // Returns {true} if this descriptor is a call to a WebAssembly function.
232 : bool IsWasmFunctionCall() const { return kind_ == kCallWasmFunction; }
233 :
234 : // Returns {true} if this descriptor is a call to a WebAssembly function.
235 : bool IsWasmImportWrapper() const { return kind_ == kCallWasmImportWrapper; }
236 :
237 2949399 : bool RequiresFrameAsIncoming() const {
238 2949399 : return IsCFunctionCall() || IsJSFunctionCall() || IsWasmFunctionCall();
239 : }
240 :
241 : // The number of return values from this call.
242 25478912 : size_t ReturnCount() const { return location_sig_->return_count(); }
243 :
244 : // The number of C parameters to this call.
245 2440260 : size_t ParameterCount() const { return location_sig_->parameter_count(); }
246 :
247 : // The number of stack parameters to the call.
248 : size_t StackParameterCount() const { return stack_param_count_; }
249 :
250 : // The number of stack return values from the call.
251 : size_t StackReturnCount() const { return stack_return_count_; }
252 :
253 : // The number of parameters to the JS function call.
254 : size_t JSParameterCount() const {
255 : DCHECK(IsJSFunctionCall());
256 : return stack_param_count_;
257 : }
258 :
259 : // The total number of inputs to this call, which includes the target,
260 : // receiver, context, etc.
261 : // TODO(titzer): this should input the framestate input too.
262 29066583 : size_t InputCount() const { return 1 + location_sig_->parameter_count(); }
263 :
264 4969669 : size_t FrameStateCount() const { return NeedsFrameState() ? 1 : 0; }
265 :
266 : Flags flags() const { return flags_; }
267 :
268 : bool NeedsFrameState() const { return flags() & kNeedsFrameState; }
269 : bool PushArgumentCount() const { return flags() & kPushArgumentCount; }
270 : bool InitializeRootRegister() const {
271 : return flags() & kInitializeRootRegister;
272 : }
273 :
274 : LinkageLocation GetReturnLocation(size_t index) const {
275 8733426 : return location_sig_->GetReturn(index);
276 : }
277 :
278 26811124 : LinkageLocation GetInputLocation(size_t index) const {
279 26811124 : if (index == 0) return target_loc_;
280 25981087 : return location_sig_->GetParam(index - 1);
281 : }
282 :
283 : MachineSignature* GetMachineSignature(Zone* zone) const;
284 :
285 : MachineType GetReturnType(size_t index) const {
286 884944 : return location_sig_->GetReturn(index).GetType();
287 : }
288 :
289 : MachineType GetInputType(size_t index) const {
290 9006768 : if (index == 0) return target_type_;
291 8822157 : return location_sig_->GetParam(index - 1).GetType();
292 : }
293 :
294 : MachineType GetParameterType(size_t index) const {
295 24545 : return location_sig_->GetParam(index).GetType();
296 : }
297 :
298 : // Operator properties describe how this call can be optimized, if at all.
299 : Operator::Properties properties() const { return properties_; }
300 :
301 : // Get the callee-saved registers, if any, across this call.
302 : RegList CalleeSavedRegisters() const { return callee_saved_registers_; }
303 :
304 : // Get the callee-saved FP registers, if any, across this call.
305 : RegList CalleeSavedFPRegisters() const { return callee_saved_fp_registers_; }
306 :
307 : const char* debug_name() const { return debug_name_; }
308 :
309 : bool UsesOnlyRegisters() const;
310 :
311 : bool HasSameReturnLocationsAs(const CallDescriptor* other) const;
312 :
313 : // Returns the first stack slot that is not used by the stack parameters.
314 : int GetFirstUnusedStackSlot() const;
315 :
316 : int GetStackParameterDelta(const CallDescriptor* tail_caller) const;
317 :
318 : bool CanTailCall(const Node* call) const;
319 :
320 : int CalculateFixedFrameSize() const;
321 :
322 : RegList AllocatableRegisters() const { return allocatable_registers_; }
323 :
324 : bool HasRestrictedAllocatableRegisters() const {
325 : return allocatable_registers_ != 0;
326 : }
327 :
328 341 : void set_save_fp_mode(SaveFPRegsMode mode) { save_fp_mode_ = mode; }
329 :
330 : SaveFPRegsMode get_save_fp_mode() const { return save_fp_mode_; }
331 :
332 : private:
333 : friend class Linkage;
334 : SaveFPRegsMode save_fp_mode_ = kSaveFPRegs;
335 :
336 : const Kind kind_;
337 : const MachineType target_type_;
338 : const LinkageLocation target_loc_;
339 : const LocationSignature* const location_sig_;
340 : const size_t stack_param_count_;
341 : const size_t stack_return_count_;
342 : const Operator::Properties properties_;
343 : const RegList callee_saved_registers_;
344 : const RegList callee_saved_fp_registers_;
345 : // Non-zero value means restricting the set of allocatable registers for
346 : // register allocator to use.
347 : const RegList allocatable_registers_;
348 : const Flags flags_;
349 : const char* const debug_name_;
350 :
351 : DISALLOW_COPY_AND_ASSIGN(CallDescriptor);
352 : };
353 :
354 : DEFINE_OPERATORS_FOR_FLAGS(CallDescriptor::Flags)
355 :
356 : std::ostream& operator<<(std::ostream& os, const CallDescriptor& d);
357 : V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
358 : const CallDescriptor::Kind& k);
359 :
360 : // Defines the linkage for a compilation, including the calling conventions
361 : // for incoming parameters and return value(s) as well as the outgoing calling
362 : // convention for any kind of call. Linkage is generally architecture-specific.
363 : //
364 : // Can be used to translate {arg_index} (i.e. index of the call node input) as
365 : // well as {param_index} (i.e. as stored in parameter nodes) into an operator
366 : // representing the architecture-specific location. The following call node
367 : // layouts are supported (where {n} is the number of value inputs):
368 : //
369 : // #0 #1 #2 [...] #n
370 : // Call[CodeStub] code, arg 1, arg 2, [...], context
371 : // Call[JSFunction] function, rcvr, arg 1, [...], new, #arg, context
372 : // Call[Runtime] CEntry, arg 1, arg 2, [...], fun, #arg, context
373 : // Call[BytecodeDispatch] address, arg 1, arg 2, [...]
374 : class V8_EXPORT_PRIVATE Linkage : public NON_EXPORTED_BASE(ZoneObject) {
375 : public:
376 2949878 : explicit Linkage(CallDescriptor* incoming) : incoming_(incoming) {}
377 :
378 : static CallDescriptor* ComputeIncoming(Zone* zone,
379 : OptimizedCompilationInfo* info);
380 :
381 : // The call descriptor for this compilation unit describes the locations
382 : // of incoming parameters and the outgoing return value(s).
383 36054152 : CallDescriptor* GetIncomingDescriptor() const { return incoming_; }
384 : static CallDescriptor* GetJSCallDescriptor(Zone* zone, bool is_osr,
385 : int parameter_count,
386 : CallDescriptor::Flags flags);
387 :
388 : static CallDescriptor* GetRuntimeCallDescriptor(
389 : Zone* zone, Runtime::FunctionId function, int js_parameter_count,
390 : Operator::Properties properties, CallDescriptor::Flags flags);
391 :
392 : static CallDescriptor* GetCEntryStubCallDescriptor(
393 : Zone* zone, int return_count, int js_parameter_count,
394 : const char* debug_name, Operator::Properties properties,
395 : CallDescriptor::Flags flags);
396 :
397 : static CallDescriptor* GetStubCallDescriptor(
398 : Zone* zone, const CallInterfaceDescriptor& descriptor,
399 : int stack_parameter_count, CallDescriptor::Flags flags,
400 : Operator::Properties properties = Operator::kNoProperties,
401 : StubCallMode stub_mode = StubCallMode::kCallCodeObject);
402 :
403 : static CallDescriptor* GetBytecodeDispatchCallDescriptor(
404 : Zone* zone, const CallInterfaceDescriptor& descriptor,
405 : int stack_parameter_count);
406 :
407 : // Creates a call descriptor for simplified C calls that is appropriate
408 : // for the host platform. This simplified calling convention only supports
409 : // integers and pointers of one word size each, i.e. no floating point,
410 : // structs, pointers to members, etc.
411 : static CallDescriptor* GetSimplifiedCDescriptor(
412 : Zone* zone, const MachineSignature* sig,
413 : bool set_initialize_root_flag = false);
414 :
415 : // Get the location of an (incoming) parameter to this function.
416 : LinkageLocation GetParameterLocation(int index) const {
417 5658285 : return incoming_->GetInputLocation(index + 1); // + 1 to skip target.
418 : }
419 :
420 : // Get the machine type of an (incoming) parameter to this function.
421 3477041 : MachineType GetParameterType(int index) const {
422 6954082 : return incoming_->GetInputType(index + 1); // + 1 to skip target.
423 : }
424 :
425 : // Get the location where this function should place its return value.
426 : LinkageLocation GetReturnLocation(size_t index = 0) const {
427 3253642 : return incoming_->GetReturnLocation(index);
428 : }
429 :
430 : // Get the machine type of this function's return value.
431 : MachineType GetReturnType(size_t index = 0) const {
432 0 : return incoming_->GetReturnType(index);
433 : }
434 :
435 : bool ParameterHasSecondaryLocation(int index) const;
436 : LinkageLocation GetParameterSecondaryLocation(int index) const;
437 :
438 : static bool NeedsFrameStateInput(Runtime::FunctionId function);
439 :
440 : // Get the location where an incoming OSR value is stored.
441 : LinkageLocation GetOsrValueLocation(int index) const;
442 :
443 : // A special {Parameter} index for Stub Calls that represents context.
444 : static int GetStubCallContextParamIndex(int parameter_count) {
445 : return parameter_count + 0; // Parameter (arity + 0) is special.
446 : }
447 :
448 : // A special {Parameter} index for JSCalls that represents the new target.
449 : static int GetJSCallNewTargetParamIndex(int parameter_count) {
450 : return parameter_count + 0; // Parameter (arity + 0) is special.
451 : }
452 :
453 : // A special {Parameter} index for JSCalls that represents the argument count.
454 : static int GetJSCallArgCountParamIndex(int parameter_count) {
455 1481022 : return parameter_count + 1; // Parameter (arity + 1) is special.
456 : }
457 :
458 : // A special {Parameter} index for JSCalls that represents the context.
459 : static int GetJSCallContextParamIndex(int parameter_count) {
460 2180109 : return parameter_count + 2; // Parameter (arity + 2) is special.
461 : }
462 :
463 : // A special {Parameter} index for JSCalls that represents the closure.
464 : static const int kJSCallClosureParamIndex = -1;
465 :
466 : // A special {OsrValue} index to indicate the context spill slot.
467 : static const int kOsrContextSpillSlotIndex = -1;
468 :
469 : // A special {OsrValue} index to indicate the accumulator register.
470 : static const int kOsrAccumulatorRegisterIndex = -1;
471 :
472 : private:
473 : CallDescriptor* const incoming_;
474 :
475 : DISALLOW_COPY_AND_ASSIGN(Linkage);
476 : };
477 :
478 : } // namespace compiler
479 : } // namespace internal
480 : } // namespace v8
481 :
482 : #endif // V8_COMPILER_LINKAGE_H_
|