graph-assembler.cc 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/graph-assembler.h"

#include "src/code-factory.h"
#include "src/compiler/linkage.h"

namespace v8 {
namespace internal {
namespace compiler {

GraphAssembler::GraphAssembler(JSGraph* jsgraph, Node* effect, Node* control,
                               Zone* zone)
    : temp_zone_(zone),
      jsgraph_(jsgraph),
      current_effect_(effect),
      current_control_(control) {}

Node* GraphAssembler::IntPtrConstant(intptr_t value) {
  return jsgraph()->IntPtrConstant(value);
}

Node* GraphAssembler::Int32Constant(int32_t value) {
  return jsgraph()->Int32Constant(value);
}

29 30 31 32
Node* GraphAssembler::Int64Constant(int64_t value) {
  return jsgraph()->Int64Constant(value);
}

33 34 35 36
Node* GraphAssembler::UniqueIntPtrConstant(intptr_t value) {
  return graph()->NewNode(
      machine()->Is64() ? common()->Int64Constant(value)
                        : common()->Int32Constant(static_cast<int32_t>(value)));
37 38
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
Node* GraphAssembler::SmiConstant(int32_t value) {
  return jsgraph()->SmiConstant(value);
}

Node* GraphAssembler::Uint32Constant(int32_t value) {
  return jsgraph()->Uint32Constant(value);
}

Node* GraphAssembler::Float64Constant(double value) {
  return jsgraph()->Float64Constant(value);
}

Node* GraphAssembler::HeapConstant(Handle<HeapObject> object) {
  return jsgraph()->HeapConstant(object);
}


Node* GraphAssembler::ExternalConstant(ExternalReference ref) {
  return jsgraph()->ExternalConstant(ref);
}

Node* GraphAssembler::CEntryStubConstant(int result_size) {
  return jsgraph()->CEntryStubConstant(result_size);
}

64 65 66 67
Node* GraphAssembler::LoadFramePointer() {
  return graph()->NewNode(machine()->LoadFramePointer());
}

68 69 70 71
#define SINGLETON_CONST_DEF(Name) \
  Node* GraphAssembler::Name() { return jsgraph()->Name(); }
JSGRAPH_SINGLETON_CONSTANT_LIST(SINGLETON_CONST_DEF)
#undef SINGLETON_CONST_DEF
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

#define PURE_UNOP_DEF(Name)                            \
  Node* GraphAssembler::Name(Node* input) {            \
    return graph()->NewNode(machine()->Name(), input); \
  }
PURE_ASSEMBLER_MACH_UNOP_LIST(PURE_UNOP_DEF)
#undef PURE_UNOP_DEF

#define PURE_BINOP_DEF(Name)                                 \
  Node* GraphAssembler::Name(Node* left, Node* right) {      \
    return graph()->NewNode(machine()->Name(), left, right); \
  }
PURE_ASSEMBLER_MACH_BINOP_LIST(PURE_BINOP_DEF)
#undef PURE_BINOP_DEF

#define CHECKED_BINOP_DEF(Name)                                                \
  Node* GraphAssembler::Name(Node* left, Node* right) {                        \
    return graph()->NewNode(machine()->Name(), left, right, current_control_); \
  }
CHECKED_ASSEMBLER_MACH_BINOP_LIST(CHECKED_BINOP_DEF)
#undef CHECKED_BINOP_DEF

Node* GraphAssembler::Float64RoundDown(Node* value) {
95 96 97 98 99 100 101
  CHECK(machine()->Float64RoundDown().IsSupported());
  return graph()->NewNode(machine()->Float64RoundDown().op(), value);
}

Node* GraphAssembler::Float64RoundTruncate(Node* value) {
  CHECK(machine()->Float64RoundTruncate().IsSupported());
  return graph()->NewNode(machine()->Float64RoundTruncate().op(), value);
102 103 104 105 106 107 108
}

Node* GraphAssembler::Projection(int index, Node* value) {
  return graph()->NewNode(common()->Projection(index), value, current_control_);
}

Node* GraphAssembler::Allocate(PretenureFlag pretenure, Node* size) {
109 110
  return current_control_ = current_effect_ =
             graph()->NewNode(simplified()->AllocateRaw(Type::Any(), pretenure),
111
                              size, current_effect_, current_control_);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
}

Node* GraphAssembler::LoadField(FieldAccess const& access, Node* object) {
  return current_effect_ =
             graph()->NewNode(simplified()->LoadField(access), object,
                              current_effect_, current_control_);
}

Node* GraphAssembler::LoadElement(ElementAccess const& access, Node* object,
                                  Node* index) {
  return current_effect_ =
             graph()->NewNode(simplified()->LoadElement(access), object, index,
                              current_effect_, current_control_);
}

Node* GraphAssembler::StoreField(FieldAccess const& access, Node* object,
                                 Node* value) {
  return current_effect_ =
             graph()->NewNode(simplified()->StoreField(access), object, value,
                              current_effect_, current_control_);
}

Node* GraphAssembler::StoreElement(ElementAccess const& access, Node* object,
                                   Node* index, Node* value) {
  return current_effect_ =
             graph()->NewNode(simplified()->StoreElement(access), object, index,
                              value, current_effect_, current_control_);
}

141 142 143 144 145
Node* GraphAssembler::DebugBreak() {
  return current_effect_ = graph()->NewNode(machine()->DebugBreak(),
                                            current_effect_, current_control_);
}

146 147 148 149 150
Node* GraphAssembler::Unreachable() {
  return current_effect_ = graph()->NewNode(common()->Unreachable(),
                                            current_effect_, current_control_);
}

151 152 153 154 155 156 157
Node* GraphAssembler::Store(StoreRepresentation rep, Node* object, Node* offset,
                            Node* value) {
  return current_effect_ =
             graph()->NewNode(machine()->Store(rep), object, offset, value,
                              current_effect_, current_control_);
}

158 159 160 161 162 163
Node* GraphAssembler::Load(MachineType rep, Node* object, Node* offset) {
  return current_effect_ =
             graph()->NewNode(machine()->Load(rep), object, offset,
                              current_effect_, current_control_);
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
Node* GraphAssembler::StoreUnaligned(MachineRepresentation rep, Node* object,
                                     Node* offset, Node* value) {
  Operator const* const op =
      (rep == MachineRepresentation::kWord8 ||
       machine()->UnalignedStoreSupported(rep))
          ? machine()->Store(StoreRepresentation(rep, kNoWriteBarrier))
          : machine()->UnalignedStore(rep);
  return current_effect_ = graph()->NewNode(op, object, offset, value,
                                            current_effect_, current_control_);
}

Node* GraphAssembler::LoadUnaligned(MachineType rep, Node* object,
                                    Node* offset) {
  Operator const* const op =
      (rep.representation() == MachineRepresentation::kWord8 ||
       machine()->UnalignedLoadSupported(rep.representation()))
          ? machine()->Load(rep)
          : machine()->UnalignedLoad(rep);
  return current_effect_ = graph()->NewNode(op, object, offset, current_effect_,
                                            current_control_);
}

186 187 188 189 190 191 192 193 194 195 196 197
Node* GraphAssembler::Retain(Node* buffer) {
  return current_effect_ =
             graph()->NewNode(common()->Retain(), buffer, current_effect_);
}

Node* GraphAssembler::UnsafePointerAdd(Node* base, Node* external) {
  return current_effect_ =
             graph()->NewNode(machine()->UnsafePointerAdd(), base, external,
                              current_effect_, current_control_);
}

Node* GraphAssembler::ToNumber(Node* value) {
198 199 200
  return current_effect_ =
             graph()->NewNode(ToNumberOperator(), ToNumberBuiltinConstant(),
                              value, NoContextConstant(), current_effect_);
201 202
}

203 204 205 206 207 208
Node* GraphAssembler::BitcastWordToTagged(Node* value) {
  return current_effect_ =
             graph()->NewNode(machine()->BitcastWordToTagged(), value,
                              current_effect_, current_control_);
}

209 210 211 212 213 214
Node* GraphAssembler::BitcastTaggedToWord(Node* value) {
  return current_effect_ =
             graph()->NewNode(machine()->BitcastTaggedToWord(), value,
                              current_effect_, current_control_);
}

215 216 217 218 219 220
Node* GraphAssembler::Word32PoisonOnSpeculation(Node* value) {
  return current_effect_ =
             graph()->NewNode(machine()->Word32PoisonOnSpeculation(), value,
                              current_effect_, current_control_);
}

221 222
Node* GraphAssembler::DeoptimizeIf(DeoptimizeReason reason,
                                   VectorSlotPair const& feedback,
223 224
                                   Node* condition, Node* frame_state,
                                   IsSafetyCheck is_safety_check) {
225
  return current_control_ = current_effect_ = graph()->NewNode(
226 227
             common()->DeoptimizeIf(DeoptimizeKind::kEager, reason, feedback,
                                    is_safety_check),
228
             condition, frame_state, current_effect_, current_control_);
229 230
}

231 232
Node* GraphAssembler::DeoptimizeIfNot(DeoptimizeReason reason,
                                      VectorSlotPair const& feedback,
233 234 235 236 237 238
                                      Node* condition, Node* frame_state,
                                      IsSafetyCheck is_safety_check) {
  return current_control_ = current_effect_ = graph()->NewNode(
             common()->DeoptimizeUnless(DeoptimizeKind::kEager, reason,
                                        feedback, is_safety_check),
             condition, frame_state, current_effect_, current_control_);
239 240
}

241
void GraphAssembler::Branch(Node* condition, GraphAssemblerLabel<0u>* if_true,
242 243
                            GraphAssemblerLabel<0u>* if_false,
                            IsSafetyCheck is_safety_check) {
244 245 246 247 248 249 250
  DCHECK_NOT_NULL(current_control_);

  BranchHint hint = BranchHint::kNone;
  if (if_true->IsDeferred() != if_false->IsDeferred()) {
    hint = if_false->IsDeferred() ? BranchHint::kTrue : BranchHint::kFalse;
  }

251 252
  Node* branch = graph()->NewNode(common()->Branch(hint, is_safety_check),
                                  condition, current_control_);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

  current_control_ = graph()->NewNode(common()->IfTrue(), branch);
  MergeState(if_true);

  current_control_ = graph()->NewNode(common()->IfFalse(), branch);
  MergeState(if_false);

  current_control_ = nullptr;
  current_effect_ = nullptr;
}

// Extractors (should be only used when destructing the assembler.
Node* GraphAssembler::ExtractCurrentControl() {
  Node* result = current_control_;
  current_control_ = nullptr;
  return result;
}

Node* GraphAssembler::ExtractCurrentEffect() {
  Node* result = current_effect_;
  current_effect_ = nullptr;
  return result;
}

void GraphAssembler::Reset(Node* effect, Node* control) {
  current_effect_ = effect;
  current_control_ = control;
}

Operator const* GraphAssembler::ToNumberOperator() {
  if (!to_number_operator_.is_set()) {
284 285
    Callable callable =
        Builtins::CallableFor(jsgraph()->isolate(), Builtins::kToNumber);
286
    CallDescriptor::Flags flags = CallDescriptor::kNoFlags;
287 288 289 290
    auto call_descriptor = Linkage::GetStubCallDescriptor(
        graph()->zone(), callable.descriptor(),
        callable.descriptor().GetStackParameterCount(), flags,
        Operator::kEliminatable);
291
    to_number_operator_.set(common()->Call(call_descriptor));
292 293 294 295 296 297 298
  }
  return to_number_operator_.get();
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8